Skip to content

Commit 424ad25

Browse files
Trigger ice restart in case of the network changed
1 parent 095b524 commit 424ad25

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/js/webrtc_adaptor.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ export class WebRTCAdaptor {
162162
*/
163163
this.debug = false;
164164

165+
/**
166+
* This is the flag to indicate if the stream is published or not after the connection fails
167+
* @type {boolean}
168+
*/
169+
this.iceRestart = false;
170+
165171
/**
166172
* This is the Stream Id for the publisher. One @WebRCTCAdaptor supports only one publishing
167173
* session for now (23.02.2022).
@@ -1092,8 +1098,18 @@ export class WebRTCAdaptor {
10921098
this.onTrack(event, closedStreamId);
10931099
}
10941100

1095-
this.remotePeerConnection[streamId].onnegotiationneeded = event => {
1101+
this.remotePeerConnection[streamId].onnegotiationneeded = async event => {
10961102
Logger.debug("onnegotiationneeded");
1103+
//If ice restart is not true, then server will handle negotiation
1104+
if (!this.iceRestart) {
1105+
return;
1106+
}
1107+
try {
1108+
await this.remotePeerConnection[streamId].setLocalDescription(await this.remotePeerConnection[streamId].createOffer({iceRestart: this.iceRestart}));
1109+
this.webSocketAdaptor.send({desc: this.remotePeerConnection[streamId].localDescription});
1110+
} catch (error) {
1111+
Logger.error('Error during negotiation', error);
1112+
}
10971113
}
10981114

10991115
if (this.dataChannelEnabled) {
@@ -1136,7 +1152,12 @@ export class WebRTCAdaptor {
11361152

11371153
this.remotePeerConnection[streamId].oniceconnectionstatechange = event => {
11381154
var obj = { state: this.remotePeerConnection[streamId].iceConnectionState, streamId: streamId };
1139-
if (obj.state == "failed" || obj.state == "disconnected" || obj.state == "closed") {
1155+
if (obj.state === "stable") {
1156+
this.iceRestart = false;
1157+
} else if (obj.state === "failed") {
1158+
this.iceRestart = true;
1159+
this.remotePeerConnection[streamId].restartIce();
1160+
} else if (obj.state === "disconnected" || obj.state === "closed") {
11401161
this.reconnectIfRequired(3000);
11411162
}
11421163
this.notifyEventListeners("ice_connection_state_changed", obj);

src/test/js/webrtc_adaptor.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,28 @@ describe("WebRTCAdaptor", function() {
19231923

19241924
});
19251925

1926+
describe("oniceconnectionstatechange", function () {
1927+
let adaptor;
1928+
let mockPeerConnection;
1929+
1930+
beforeEach(function () {
1931+
adaptor = new WebRTCAdaptor({
1932+
websocketURL: "ws://example.com",
1933+
initializeComponents: false,
1934+
});
1935+
mockPeerConnection = { iceConnectionState: "", restartIce: sinon.fake(), oniceconnectionstatechange: sinon.fake()};
1936+
adaptor.remotePeerConnection["stream1"] = mockPeerConnection;
1937+
});
1938+
1939+
it("should set iceRestart to false when state is stable", function () {
1940+
mockPeerConnection.iceConnectionState = "stable";
1941+
1942+
adaptor.remotePeerConnection["stream1"].oniceconnectionstatechange();
1943+
1944+
expect(adaptor.iceRestart).to.be.false;
1945+
});
1946+
});
1947+
19261948

19271949

19281950
});

0 commit comments

Comments
 (0)