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: update error, add node id to offer/answer/trickle #3148

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions packages/hms-video-store/src/signal/jsonrpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ export default class JsonRpcSignal {

private _isConnected = false;
private id = 0;
private sfuNodeId: string | undefined;

private onCloseHandler: (event: CloseEvent) => void = () => {};

public get isConnected(): boolean {
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) {
Expand Down Expand Up @@ -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<string, any>,
sfuNodeId?: string,
): Promise<RTCSessionDescriptionInit> {
async offer(desc: RTCSessionDescriptionInit, tracks: Map<string, any>): Promise<RTCSessionDescriptionInit> {
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<string, Track>) {
Expand Down
14 changes: 10 additions & 4 deletions packages/hms-video-store/src/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export default class HMSTransport {
}

setSFUNodeId(id: string) {
this.signal.setSfuNodeId(id);
if (!this.sfuNodeId) {
this.sfuNodeId = id;
raviteja83 marked this conversation as resolved.
Show resolved Hide resolved
} else if (this.sfuNodeId !== id) {
Expand Down Expand Up @@ -912,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);
Expand All @@ -935,6 +938,7 @@ export default class HMSTransport {
onDemandTracks,
);
this.sfuNodeId = response?.sfu_node_id;
this.signal.setSfuNodeId(this.sfuNodeId);
return !!response;
}

Expand All @@ -950,7 +954,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);
Expand All @@ -959,7 +963,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;
Expand All @@ -982,7 +987,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);
Expand All @@ -996,7 +1001,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);
Expand Down
Loading