Skip to content

Commit

Permalink
fix: reconnection (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
eswarclynn authored Aug 14, 2024
1 parent 2dac85a commit 7fdb44c
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions packages/hms-video-store/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,17 @@ export default class HMSTransport {
}

private createPeerConnections() {
const logConnectionState = (
role: HMSConnectionRole,
newState: RTCIceConnectionState | RTCPeerConnectionState,
ice = false,
) => {
const log = ['disconnected', 'failed'].includes(newState)
? HMSLogger.w.bind(HMSLogger)
: HMSLogger.d.bind(HMSLogger);

log(TAG, `${HMSConnectionRole[role]} ${ice ? 'ice' : ''} connection state change: ${newState}`);
};
if (this.initConfig) {
const publishConnectionObserver: IPublishConnectionObserver = {
onRenegotiationNeeded: async () => {
Expand Down Expand Up @@ -721,13 +732,11 @@ export default class HMSTransport {
},

onIceConnectionChange: async (newState: RTCIceConnectionState) => {
const log = newState === 'disconnected' ? HMSLogger.w.bind(HMSLogger) : HMSLogger.d.bind(HMSLogger);
log(TAG, `Publish ice connection state change: ${newState}`);
logConnectionState(HMSConnectionRole.Publish, newState, true);
},

onConnectionStateChange: async (newState: RTCPeerConnectionState) => {
const log = newState === 'disconnected' ? HMSLogger.w.bind(HMSLogger) : HMSLogger.d.bind(HMSLogger);
log(TAG, `Publish connection state change: ${newState}`);
logConnectionState(HMSConnectionRole.Publish, newState, false);
if (newState === 'new') {
return;
}
Expand Down Expand Up @@ -783,8 +792,7 @@ export default class HMSTransport {
},

onIceConnectionChange: async (newState: RTCIceConnectionState) => {
const log = newState === 'disconnected' ? HMSLogger.w.bind(HMSLogger) : HMSLogger.d.bind(HMSLogger);
log(TAG, `Subscribe ice connection state change: ${newState}`);
logConnectionState(HMSConnectionRole.Subscribe, newState, true);

if (newState === 'connected') {
const callback = this.callbacks.get(SUBSCRIBE_ICE_CONNECTION_CALLBACK_ID);
Expand All @@ -798,8 +806,7 @@ export default class HMSTransport {
},

onConnectionStateChange: async (newState: RTCPeerConnectionState) => {
const log = newState === 'disconnected' ? HMSLogger.w.bind(HMSLogger) : HMSLogger.d.bind(HMSLogger);
log(TAG, `Subscribe connection state change: ${newState}`);
logConnectionState(HMSConnectionRole.Subscribe, newState, false);

if (newState === 'failed') {
await this.handleIceConnectionFailure(
Expand Down Expand Up @@ -1005,6 +1012,7 @@ export default class HMSTransport {
HMSLogger.d(TAG, `⏳ [role=PUBLISH] onRenegotiationNeeded START`, this.trackStates);
const callback = this.callbacks.get(RENEGOTIATION_CALLBACK_ID);
if (!callback) {
HMSLogger.w(TAG, 'no callback found for renegotiation');
return;
}

Expand Down Expand Up @@ -1033,9 +1041,9 @@ export default class HMSTransport {

// resolve for now as this might happen during migration
if (ex.code === 421) {
callback!.promise.resolve(true);
callback.promise.resolve(true);
} else {
callback!.promise.reject(ex);
callback.promise.reject(ex);
}
HMSLogger.d(TAG, `[role=PUBLISH] onRenegotiationNeeded FAILED ❌`);
}
Expand Down Expand Up @@ -1217,7 +1225,15 @@ export default class HMSTransport {
* Do iceRestart only if not connected
*/
if (this.publishConnection) {
const p = new Promise<boolean>((resolve, reject) => {
this.callbacks.set(RENEGOTIATION_CALLBACK_ID, {
promise: { resolve, reject },
action: HMSAction.RESTART_ICE,
extra: {},
});
});
await this.performPublishRenegotiation({ iceRestart: this.publishConnection.connectionState !== 'connected' });
await p;
}

return true;
Expand Down

0 comments on commit 7fdb44c

Please sign in to comment.