From 775d8ed1b8dbbc411d2b18dcbde4cc1b10bd24ed Mon Sep 17 00:00:00 2001 From: raviteja83 Date: Fri, 2 Aug 2024 14:54:38 +0530 Subject: [PATCH 1/3] fix: handle errors on migration --- .../hms-video-store/src/transport/index.ts | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/packages/hms-video-store/src/transport/index.ts b/packages/hms-video-store/src/transport/index.ts index fa05fb1aaf..90f6a9f24f 100644 --- a/packages/hms-video-store/src/transport/index.ts +++ b/packages/hms-video-store/src/transport/index.ts @@ -947,16 +947,23 @@ export default class HMSTransport { HMSLogger.e(TAG, 'Publish peer connection not found, cannot negotiate'); return false; } - const offer = await this.publishConnection.createOffer(this.trackStates); - await this.publishConnection.setLocalDescription(offer); - const answer = await this.signal.offer(offer, this.trackStates, this.sfuNodeId); - await this.publishConnection.setRemoteDescription(answer); - for (const candidate of this.publishConnection.candidates) { - await this.publishConnection.addIceCandidate(candidate); - } + try { + const offer = await this.publishConnection.createOffer(this.trackStates); + await this.publishConnection.setLocalDescription(offer); + const answer = await this.signal.offer(offer, this.trackStates, this.sfuNodeId); + await this.publishConnection.setRemoteDescription(answer); + for (const candidate of this.publishConnection.candidates) { + await this.publishConnection.addIceCandidate(candidate); + } - this.publishConnection.initAfterJoin(); - return !!answer; + this.publishConnection.initAfterJoin(); + return !!answer; + } catch (ex) { + if (ex instanceof HMSException && ex.code === 400) { + return true; + } + throw ex; + } } private async performPublishRenegotiation(constraints?: RTCOfferOptions) { @@ -989,7 +996,11 @@ export default class HMSTransport { ex = ErrorFactory.GenericErrors.Unknown(HMSAction.PUBLISH, (err as Error).message); } - callback!.promise.reject(ex); + if (ex.code === 400) { + callback!.promise.resolve(true); + } else { + callback!.promise.reject(ex); + } HMSLogger.d(TAG, `[role=PUBLISH] onRenegotiationNeeded FAILED ❌`); } } From a285ce76e303a09499e8cb8d946c524a1d832cf6 Mon Sep 17 00:00:00 2001 From: raviteja83 Date: Fri, 2 Aug 2024 15:25:21 +0530 Subject: [PATCH 2/3] fix: update error code and add node id to offer,answer,trickle --- .../hms-video-store/src/signal/jsonrpc/index.ts | 17 +++++++++-------- packages/hms-video-store/src/transport/index.ts | 11 +++++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/hms-video-store/src/signal/jsonrpc/index.ts b/packages/hms-video-store/src/signal/jsonrpc/index.ts index ab4cf7c5cd..51e357ce56 100644 --- a/packages/hms-video-store/src/signal/jsonrpc/index.ts +++ b/packages/hms-video-store/src/signal/jsonrpc/index.ts @@ -91,6 +91,7 @@ export default class JsonRpcSignal { private _isConnected = false; private id = 0; + private sfuNodeId: string | undefined; private onCloseHandler: (event: CloseEvent) => void = () => {}; @@ -98,6 +99,10 @@ export default class JsonRpcSignal { return this._isConnected; } + setSfuNodeId(sfuNodeId?: string) { + this.sfuNodeId = sfuNodeId; + } + public setIsConnected(newValue: boolean, reason = '') { HMSLogger.d(this.TAG, `isConnected set id: ${this.id}, oldValue: ${this._isConnected}, newValue: ${newValue}`); if (this._isConnected === newValue) { @@ -275,27 +280,23 @@ export default class JsonRpcSignal { trickle(target: HMSConnectionRole, candidate: RTCIceCandidateInit) { if (this.isJoinCompleted) { - this.notify(HMSSignalMethod.TRICKLE, { target, candidate }); + this.notify(HMSSignalMethod.TRICKLE, { target, candidate, sfu_node_id: this.sfuNodeId }); } else { this.pendingTrickle.push({ target, candidate }); } } - async offer( - desc: RTCSessionDescriptionInit, - tracks: Map, - sfuNodeId?: string, - ): Promise { + async offer(desc: RTCSessionDescriptionInit, tracks: Map): Promise { const response = await this.call(HMSSignalMethod.OFFER, { desc, tracks: Object.fromEntries(tracks), - sfu_node_id: sfuNodeId, + sfu_node_id: this.sfuNodeId, }); return response as RTCSessionDescriptionInit; } answer(desc: RTCSessionDescriptionInit) { - this.notify(HMSSignalMethod.ANSWER, { desc }); + this.notify(HMSSignalMethod.ANSWER, { desc, sfu_node_id: this.sfuNodeId }); } trackUpdate(tracks: Map) { diff --git a/packages/hms-video-store/src/transport/index.ts b/packages/hms-video-store/src/transport/index.ts index 90f6a9f24f..9a6e8b8b0e 100644 --- a/packages/hms-video-store/src/transport/index.ts +++ b/packages/hms-video-store/src/transport/index.ts @@ -420,6 +420,7 @@ export default class HMSTransport { } setSFUNodeId(id: string) { + this.signal.setSfuNodeId(id); if (!this.sfuNodeId) { this.sfuNodeId = id; } else if (this.sfuNodeId !== id) { @@ -950,7 +951,7 @@ export default class HMSTransport { try { const offer = await this.publishConnection.createOffer(this.trackStates); await this.publishConnection.setLocalDescription(offer); - const answer = await this.signal.offer(offer, this.trackStates, this.sfuNodeId); + const answer = await this.signal.offer(offer, this.trackStates); await this.publishConnection.setRemoteDescription(answer); for (const candidate of this.publishConnection.candidates) { await this.publishConnection.addIceCandidate(candidate); @@ -959,7 +960,8 @@ export default class HMSTransport { this.publishConnection.initAfterJoin(); return !!answer; } catch (ex) { - if (ex instanceof HMSException && ex.code === 400) { + // resolve for now as this might happen during migration + if (ex instanceof HMSException && ex.code === 421) { return true; } throw ex; @@ -982,7 +984,7 @@ export default class HMSTransport { const offer = await this.publishConnection.createOffer(this.trackStates, constraints); await this.publishConnection.setLocalDescription(offer); HMSLogger.time(`renegotiation-offer-exchange`); - const answer = await this.signal.offer(offer, this.trackStates, this.sfuNodeId); + const answer = await this.signal.offer(offer, this.trackStates); this.callbacks.delete(RENEGOTIATION_CALLBACK_ID); HMSLogger.timeEnd(`renegotiation-offer-exchange`); await this.publishConnection.setRemoteDescription(answer); @@ -996,7 +998,8 @@ export default class HMSTransport { ex = ErrorFactory.GenericErrors.Unknown(HMSAction.PUBLISH, (err as Error).message); } - if (ex.code === 400) { + // resolve for now as this might happen during migration + if (ex.code === 421) { callback!.promise.resolve(true); } else { callback!.promise.reject(ex); From 394f9fac03c6cbc95b82ec24ee9ab237b4179a7c Mon Sep 17 00:00:00 2001 From: raviteja83 Date: Fri, 2 Aug 2024 15:53:32 +0530 Subject: [PATCH 3/3] fix: handle node id from join --- packages/hms-video-store/src/transport/index.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/hms-video-store/src/transport/index.ts b/packages/hms-video-store/src/transport/index.ts index 9a6e8b8b0e..b110f204ac 100644 --- a/packages/hms-video-store/src/transport/index.ts +++ b/packages/hms-video-store/src/transport/index.ts @@ -913,6 +913,8 @@ export default class HMSTransport { onDemandTracks, offer, ); + this.sfuNodeId = answer?.sfu_node_id; + this.signal.setSfuNodeId(this.sfuNodeId); await this.publishConnection.setRemoteDescription(answer); for (const candidate of this.publishConnection.candidates) { await this.publishConnection.addIceCandidate(candidate); @@ -936,6 +938,7 @@ export default class HMSTransport { onDemandTracks, ); this.sfuNodeId = response?.sfu_node_id; + this.signal.setSfuNodeId(this.sfuNodeId); return !!response; }