Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reconnection #3179

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading