Skip to content

Commit

Permalink
feat: add polling for devices when device change not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 authored Mar 15, 2024
1 parent 9567dee commit c9957ee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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,
);
}
}
};
Expand Down
5 changes: 1 addition & 4 deletions packages/hms-video-store/src/device-manager/DeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c9957ee

Please sign in to comment.