diff --git a/packages/hms-video-store/src/device-manager/DeviceManager.ts b/packages/hms-video-store/src/device-manager/DeviceManager.ts index 3898560b06..7f6021e858 100644 --- a/packages/hms-video-store/src/device-manager/DeviceManager.ts +++ b/packages/hms-video-store/src/device-manager/DeviceManager.ts @@ -500,20 +500,25 @@ export class DeviceManager implements HMSDeviceManager { if (localAudioTrack.settings.deviceId === externalDeviceID && this.earpieceSelected) { return; } - if (!this.earpieceSelected) { - if (bluetoothDevice?.deviceId === externalDeviceID) { + + try { + if (!this.earpieceSelected) { + if (bluetoothDevice?.deviceId === externalDeviceID) { + this.earpieceSelected = true; + return; + } + await localAudioTrack.setSettings({ deviceId: earpiece?.deviceId }, true); this.earpieceSelected = true; - return; } - await localAudioTrack.setSettings({ deviceId: earpiece?.deviceId }, true); - this.earpieceSelected = true; - } - await localAudioTrack.setSettings( - { - deviceId: externalDeviceID, - }, - true, - ); + await localAudioTrack.setSettings( + { + deviceId: externalDeviceID, + }, + true, + ); + } catch (error) { + this.eventBus.error.publish(error as HMSException); + } }; // eslint-disable-next-line complexity diff --git a/packages/hms-video-store/src/events/EventBus.ts b/packages/hms-video-store/src/events/EventBus.ts index 096b4bba32..6e003fd5ea 100644 --- a/packages/hms-video-store/src/events/EventBus.ts +++ b/packages/hms-video-store/src/events/EventBus.ts @@ -81,4 +81,6 @@ export class EventBus { readonly autoplayError = new HMSInternalEvent(HMSEvents.AUTOPLAY_ERROR, this.eventEmitter); readonly leave = new HMSInternalEvent(HMSEvents.LEAVE, this.eventEmitter); + + readonly error = new HMSInternalEvent(HMSEvents.ERROR, this.eventEmitter); } diff --git a/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts b/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts index 69fd62e186..6cadd54b9f 100644 --- a/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts +++ b/packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts @@ -115,7 +115,11 @@ export class HMSLocalAudioTrack extends HMSAudioTrack { ); } else { HMSLogger.d(this.TAG, 'On visibile replacing track as it is not publishing'); - await this.replaceTrackWith(this.settings); + try { + await this.replaceTrackWith(this.settings); + } catch (error) { + this.eventBus.error.publish(error as HMSException); + } this.eventBus.analytics.publish( this.sendInterruptionEvent({ started: false, @@ -334,9 +338,13 @@ export class HMSLocalAudioTrack extends HMSAudioTrack { reason, }), ); - await this.setEnabled(this.enabled); - // whatsapp call doesn't seem to send video unmute natively, so use audio unmute to play video - this.eventBus.localAudioUnmutedNatively.publish(); + try { + await this.setEnabled(this.enabled); + // whatsapp call doesn't seem to send video unmute natively, so use audio unmute to play video + this.eventBus.localAudioUnmutedNatively.publish(); + } catch (error) { + this.eventBus.error.publish(error as HMSException); + } }; private replaceSenderTrack = async () => { @@ -348,9 +356,7 @@ export class HMSLocalAudioTrack extends HMSAudioTrack { }; private shouldReacquireTrack = () => { - return ( - isEmptyTrack(this.nativeTrack) || this.isTrackNotPublishing() || this.audioLevelMonitor?.isSilentThisInstant() - ); + return isEmptyTrack(this.nativeTrack) || this.isTrackNotPublishing(); }; private buildNewSettings(settings: Partial) { diff --git a/packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts b/packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts index 9995f303ed..3a1399d9a2 100644 --- a/packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts +++ b/packages/hms-video-store/src/media/tracks/HMSLocalVideoTrack.ts @@ -5,6 +5,7 @@ import AnalyticsEventFactory from '../../analytics/AnalyticsEventFactory'; import { DeviceStorageManager } from '../../device-manager/DeviceStorage'; import { ErrorFactory } from '../../error/ErrorFactory'; import { HMSAction } from '../../error/HMSAction'; +import { HMSException } from '../../error/HMSException'; import { EventBus } from '../../events/EventBus'; import { HMSFacingMode, @@ -587,7 +588,11 @@ export class HMSLocalVideoTrack extends HMSVideoTrack { } else { HMSLogger.d(this.TAG, 'visibility visible, restoring track state', this.enabledStateBeforeBackground); if (this.enabledStateBeforeBackground) { - await this.setEnabled(true); + try { + await this.setEnabled(true); + } catch (error) { + this.eventBus.error.publish(error as HMSException); + } } // ended interruption event this.eventBus.analytics.publish( diff --git a/packages/hms-video-store/src/sdk/index.ts b/packages/hms-video-store/src/sdk/index.ts index eaa7c2f82f..033d09495d 100644 --- a/packages/hms-video-store/src/sdk/index.ts +++ b/packages/hms-video-store/src/sdk/index.ts @@ -260,6 +260,7 @@ export class HMSSdk implements HMSInterface { this.eventBus.localVideoUnmutedNatively.subscribe(this.unpauseRemoteVideoTracks); this.eventBus.localAudioUnmutedNatively.subscribe(this.unpauseRemoteVideoTracks); this.eventBus.audioPluginFailed.subscribe(this.handleAudioPluginError); + this.eventBus.error.subscribe(this.handleError); } private validateJoined(name: string) { @@ -600,6 +601,17 @@ export class HMSSdk implements HMSInterface { this.errorListener?.onError(error); }; + /** + * This is to handle errors thrown from internal handling of audio video track changes + * For example, handling visibility change and making a new gum can throw an error which is currently + * unhandled. This will notify the app of the error. + * @param {HMSException} error + */ + private handleError = (error: HMSException) => { + HMSLogger.e(this.TAG, error); + this.errorListener?.onError(error); + }; + // eslint-disable-next-line complexity async join(config: HMSConfig, listener: HMSUpdateListener) { validateMediaDevicesExistence(); @@ -696,6 +708,7 @@ export class HMSSdk implements HMSInterface { this.eventBus.analytics.unsubscribe(this.sendAnalyticsEvent); this.eventBus.localVideoUnmutedNatively.unsubscribe(this.unpauseRemoteVideoTracks); this.eventBus.localAudioUnmutedNatively.unsubscribe(this.unpauseRemoteVideoTracks); + this.eventBus.error.unsubscribe(this.handleError); this.analyticsTimer.cleanup(); DeviceStorageManager.cleanup(); this.playlistManager.cleanup(); diff --git a/packages/hms-video-store/src/utils/constants.ts b/packages/hms-video-store/src/utils/constants.ts index 021dc07030..5b75405ab6 100644 --- a/packages/hms-video-store/src/utils/constants.ts +++ b/packages/hms-video-store/src/utils/constants.ts @@ -56,6 +56,7 @@ export const HMSEvents = { AUDIO_TRACK_REMOVED: 'audio-track-removed', AUTOPLAY_ERROR: 'autoplay-error', LEAVE: 'leave', + ERROR: 'error', }; export const PROTOCOL_VERSION = '2.5';