Skip to content

Commit

Permalink
Persisting audio & video device selection across local peer's role ch…
Browse files Browse the repository at this point in the history
…ange (#3377)
  • Loading branch information
ygit authored Nov 21, 2024
1 parent 6e74354 commit 939327b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/hms-video-store/src/device-manager/DeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 40 additions & 5 deletions packages/hms-video-store/src/sdk/RoleChangeManager.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'
);
}
}
1 change: 1 addition & 0 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 939327b

Please sign in to comment.