From 939327b7a06e3f84d5d3583044042b0eaeb24497 Mon Sep 17 00:00:00 2001 From: ygit Date: Thu, 21 Nov 2024 11:39:09 +0530 Subject: [PATCH] Persisting audio & video device selection across local peer's role change (#3377) --- .../src/device-manager/DeviceManager.ts | 6 +++ .../src/sdk/RoleChangeManager.ts | 45 ++++++++++++++++--- packages/hms-video-store/src/sdk/index.ts | 1 + 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/packages/hms-video-store/src/device-manager/DeviceManager.ts b/packages/hms-video-store/src/device-manager/DeviceManager.ts index fd7bfcf9f3..8fedbc647c 100644 --- a/packages/hms-video-store/src/device-manager/DeviceManager.ts +++ b/packages/hms-video-store/src/device-manager/DeviceManager.ts @@ -31,6 +31,12 @@ export class DeviceManager implements HMSDeviceManager { hasWebcamPermission = false; hasMicrophonePermission = false; + currentSelection: SelectedDevices = { + audioInput: undefined, + videoInput: undefined, + audioOutput: undefined, + }; + private readonly TAG = '[Device Manager]:'; private initialized = false; private videoInputChanged = false; diff --git a/packages/hms-video-store/src/sdk/RoleChangeManager.ts b/packages/hms-video-store/src/sdk/RoleChangeManager.ts index c4be09307b..c13b2519bf 100644 --- a/packages/hms-video-store/src/sdk/RoleChangeManager.ts +++ b/packages/hms-video-store/src/sdk/RoleChangeManager.ts @@ -1,6 +1,6 @@ import { Store } from './store'; import { DeviceManager } from '../device-manager'; -import { HMSRole } from '../interfaces'; +import { DeviceType, HMSRole } from '../interfaces'; import InitialSettings from '../interfaces/settings'; import { SimulcastLayers } from '../interfaces/simulcast-layers'; import { HMSPeerUpdate, HMSTrackUpdate, HMSUpdateListener } from '../interfaces/update-listener'; @@ -144,14 +144,49 @@ export default class RoleChangeManager { } private getSettings(): InitialSettings { - const initialSettings = this.store.getConfig()?.settings; + const { isAudioMuted, isVideoMuted } = this.getMutedStatus(); + const { audioInputDeviceId, audioOutputDeviceId } = this.getAudioDeviceSettings(); + const videoDeviceId = this.getVideoInputDeviceId(); + return { + isAudioMuted: isAudioMuted, + isVideoMuted: isVideoMuted, + audioInputDeviceId: audioInputDeviceId, + audioOutputDeviceId: audioOutputDeviceId, + videoDeviceId: videoDeviceId, + }; + } + private getMutedStatus(): { isAudioMuted: boolean; isVideoMuted: boolean } { + const initialSettings = this.store.getConfig()?.settings; return { isAudioMuted: initialSettings?.isAudioMuted ?? true, isVideoMuted: initialSettings?.isVideoMuted ?? true, - audioInputDeviceId: initialSettings?.audioInputDeviceId || 'default', - audioOutputDeviceId: initialSettings?.audioOutputDeviceId || 'default', - videoDeviceId: initialSettings?.videoDeviceId || 'default', }; } + + private getAudioDeviceSettings(): { audioInputDeviceId: string; audioOutputDeviceId: string } { + const initialSettings = this.store.getConfig()?.settings; + const audioInputDeviceId = + this.deviceManager.currentSelection[DeviceType.audioInput]?.deviceId || + initialSettings?.audioInputDeviceId || + 'default'; + const audioOutputDeviceId = + this.deviceManager.currentSelection[DeviceType.audioOutput]?.deviceId || + initialSettings?.audioOutputDeviceId || + 'default'; + + return { + audioInputDeviceId, + audioOutputDeviceId, + }; + } + + private getVideoInputDeviceId(): string { + const initialSettings = this.store.getConfig()?.settings; + return ( + this.deviceManager.currentSelection[DeviceType.videoInput]?.deviceId || + initialSettings?.videoDeviceId || + 'default' + ); + } } diff --git a/packages/hms-video-store/src/sdk/index.ts b/packages/hms-video-store/src/sdk/index.ts index fe6040e751..50bd36b0eb 100644 --- a/packages/hms-video-store/src/sdk/index.ts +++ b/packages/hms-video-store/src/sdk/index.ts @@ -1350,6 +1350,7 @@ export class HMSSdk implements HMSInterface { } private handleLocalRoleUpdate = async ({ oldRole, newRole }: { oldRole: HMSRole; newRole: HMSRole }) => { + this.deviceManager.currentSelection = this.deviceManager.getCurrentSelection(); await this.transport.handleLocalRoleUpdate({ oldRole, newRole }); await this.roleChangeManager?.handleLocalPeerRoleUpdate({ oldRole, newRole }); await this.interactivityCenter.whiteboard.handleLocalRoleUpdate();