Skip to content

Commit

Permalink
fix: handle unhandled exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
raviteja83 authored Jan 8, 2025
1 parent bc61bc4 commit 66d2273
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
29 changes: 17 additions & 12 deletions packages/hms-video-store/src/device-manager/DeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/hms-video-store/src/events/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ export class EventBus {
readonly autoplayError = new HMSInternalEvent<HMSException>(HMSEvents.AUTOPLAY_ERROR, this.eventEmitter);

readonly leave = new HMSInternalEvent<HMSException | undefined>(HMSEvents.LEAVE, this.eventEmitter);

readonly error = new HMSInternalEvent<HMSException>(HMSEvents.ERROR, this.eventEmitter);
}
20 changes: 13 additions & 7 deletions packages/hms-video-store/src/media/tracks/HMSLocalAudioTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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<HMSAudioTrackSettings>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions packages/hms-video-store/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 66d2273

Please sign in to comment.