From c9957ee9fdb3d1b5194aba79cfa43c63cd878bf3 Mon Sep 17 00:00:00 2001 From: Ravi theja Date: Fri, 15 Mar 2024 16:43:40 +0530 Subject: [PATCH] feat: add polling for devices when device change not supported --- .../audio-sink-manager/AudioSinkManager.ts | 30 ++++++++++++++++--- .../src/device-manager/DeviceManager.ts | 5 +--- .../src/media/tracks/HMSLocalAudioTrack.ts | 8 +++++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts index 647ffb7fd3..e4764736f3 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -47,6 +47,7 @@ export class AudioSinkManager { this.eventBus.audioTrackRemoved.subscribe(this.handleTrackRemove); this.eventBus.audioTrackUpdate.subscribe(this.handleTrackUpdate); this.eventBus.deviceChange.subscribe(this.handleAudioDeviceChange); + this.startPollingForDevices(); } setListener(listener?: HMSUpdateListener) { @@ -266,6 +267,19 @@ export class AudioSinkManager { } }; + private startPollingForDevices = () => { + // device change supported, no polling needed + if ('ondevicechange' in navigator.mediaDevices) { + return; + } + this.timer = setInterval(() => { + (async () => { + await this.deviceManager.init(true, false); + await this.autoSelectAudioOutput(); + })(); + }, 5000); + }; + /** * Mweb is not able to play via call channel by default, this is to switch from media channel to call channel */ @@ -294,10 +308,18 @@ export class AudioSinkManager { const localAudioTrack = this.store.getLocalPeer()?.audioTrack; if (localAudioTrack && earpiece) { const externalDeviceID = bluetoothDevice?.deviceId || wired?.deviceId || speakerPhone?.deviceId; - await localAudioTrack.setSettings({ deviceId: earpiece?.deviceId }); - await localAudioTrack.setSettings({ - deviceId: externalDeviceID, - }); + HMSLogger.d(this.TAG, 'externalDeviceID', externalDeviceID); + // already selected appropriate device + if (localAudioTrack.settings.deviceId === externalDeviceID) { + return; + } + await localAudioTrack.setSettings({ deviceId: earpiece?.deviceId }, true); + await localAudioTrack.setSettings( + { + deviceId: externalDeviceID, + }, + true, + ); } } }; diff --git a/packages/hms-video-store/src/device-manager/DeviceManager.ts b/packages/hms-video-store/src/device-manager/DeviceManager.ts index d704335654..926c341573 100644 --- a/packages/hms-video-store/src/device-manager/DeviceManager.ts +++ b/packages/hms-video-store/src/device-manager/DeviceManager.ts @@ -192,11 +192,8 @@ export class DeviceManager implements HMSDeviceManager { await this.enumerateDevices(); this.logDevices('After Device Change'); const localPeer = this.store.getLocalPeer(); - const audioTrack = localPeer?.audioTrack; await this.setOutputDevice(true); - if (audioTrack) { - await this.handleAudioInputDeviceChange(localPeer?.audioTrack); - } + await this.handleAudioInputDeviceChange(localPeer?.audioTrack); await this.handleVideoInputDeviceChange(localPeer?.videoTrack); this.eventBus.analytics.publish( AnalyticsEventFactory.deviceChange({ diff --git a/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts b/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts index caef6b0629..1ff76f8387 100644 --- a/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts +++ b/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts @@ -273,6 +273,14 @@ export class HMSLocalAudioTrack extends HMSAudioTrack { const hasPropertyChanged = generateHasPropertyChanged(settings, this.settings); if (hasPropertyChanged('deviceId')) { this.manuallySelectedDeviceId = !internal ? settings.deviceId : this.manuallySelectedDeviceId; + HMSLogger.d( + this.TAG, + 'device change', + 'manual selection:', + this.manuallySelectedDeviceId, + 'new device:', + settings.deviceId, + ); await this.replaceTrackWith(settings); const groupId = this.nativeTrack.getSettings().groupId; if (!internal && settings.deviceId) {