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

Trigger ice restart in case of the network changed #513

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions src/main/js/webrtc_adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export class WebRTCAdaptor {
*/
this.debug = false;

/**
* This is the flag to indicate if the stream is published or not after the connection fails
* @type {boolean}
*/
this.iceRestart = false;

/**
* This is the Stream Id for the publisher. One @WebRCTCAdaptor supports only one publishing
* session for now (23.02.2022).
Expand Down Expand Up @@ -1092,8 +1098,18 @@ export class WebRTCAdaptor {
this.onTrack(event, closedStreamId);
}

this.remotePeerConnection[streamId].onnegotiationneeded = event => {
this.remotePeerConnection[streamId].onnegotiationneeded = async event => {
Logger.debug("onnegotiationneeded");
//If ice restart is not true, then server will handle negotiation
if (!this.iceRestart) {
return;
}
try {
await this.remotePeerConnection[streamId].setLocalDescription(await this.remotePeerConnection[streamId].createOffer({iceRestart: this.iceRestart}));
this.webSocketAdaptor.send({desc: this.remotePeerConnection[streamId].localDescription});
} catch (error) {
Logger.error('Error during negotiation', error);
}
}

if (this.dataChannelEnabled) {
Expand Down Expand Up @@ -1136,7 +1152,12 @@ export class WebRTCAdaptor {

this.remotePeerConnection[streamId].oniceconnectionstatechange = event => {
var obj = { state: this.remotePeerConnection[streamId].iceConnectionState, streamId: streamId };
if (obj.state == "failed" || obj.state == "disconnected" || obj.state == "closed") {
if (obj.state === "stable") {
this.iceRestart = false;
} else if (obj.state === "failed") {
this.iceRestart = true;
this.remotePeerConnection[streamId].restartIce();
} else if (obj.state === "disconnected" || obj.state === "closed") {
this.reconnectIfRequired(3000);
}
this.notifyEventListeners("ice_connection_state_changed", obj);
Expand Down
103 changes: 103 additions & 0 deletions src/test/js/webrtc_adaptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,109 @@ describe("WebRTCAdaptor", function() {

});

describe("oniceconnectionstatechange", function () {
let adaptor;
let mockPeerConnection;

beforeEach(function () {
adaptor = new WebRTCAdaptor({
websocketURL: "ws://example.com",
initializeComponents: false,
});
mockPeerConnection = { iceConnectionState: "", restartIce: sinon.fake(), oniceconnectionstatechange: sinon.fake()};
adaptor.remotePeerConnection["stream1"] = mockPeerConnection;
});

it("should set iceRestart to false when state is stable", function () {
mockPeerConnection.iceConnectionState = "stable";

adaptor.remotePeerConnection["stream1"].oniceconnectionstatechange();

expect(adaptor.iceRestart).to.be.false;
});
});

it("should handle negotiation when iceRestart is true", async function() {
let adaptor;
let mockPeerConnection;

beforeEach(function () {
adaptor = new WebRTCAdaptor({
websocketURL: "ws://example.com",
initializeComponents: false,
});
mockPeerConnection = {
setLocalDescription: sinon.fake.resolves(),
createOffer: sinon.fake.resolves({ sdp: "mockSdp" }),
localDescription: { sdp: "mockSdp" }
};
adaptor.remotePeerConnection["stream1"] = mockPeerConnection;
});

const streamId = "stream1";
adaptor.remotePeerConnection[streamId] = mockPeerConnection;
adaptor.iceRestart = true;

await adaptor.remotePeerConnection[streamId].onnegotiationneeded();

expect(mockPeerConnection.createOffer.calledWith({ iceRestart: true })).to.be.true;
expect(mockPeerConnection.setLocalDescription.calledWith({ sdp: "mockSdp" })).to.be.true;
expect(adaptor.webSocketAdaptor.send.calledWith({ desc: { sdp: "mockSdp" } })).to.be.true;
});

it("should not handle negotiation when iceRestart is false", async function() {
let adaptor;
let mockPeerConnection;

beforeEach(function () {
adaptor = new WebRTCAdaptor({
websocketURL: "ws://example.com",
initializeComponents: false,
});
mockPeerConnection = {
setLocalDescription: sinon.fake(),
createOffer: sinon.fake()
};
adaptor.remotePeerConnection["stream1"] = mockPeerConnection;
});

const streamId = "stream1";
adaptor.remotePeerConnection[streamId] = mockPeerConnection;
adaptor.iceRestart = false;

await adaptor.remotePeerConnection[streamId].onnegotiationneeded();

expect(mockPeerConnection.createOffer.called).to.be.false;
expect(mockPeerConnection.setLocalDescription.called).to.be.false;
expect(adaptor.webSocketAdaptor.send.called).to.be.false;
});

it("should log error during negotiation", async function() {
let adaptor;
let mockPeerConnection;

beforeEach(function () {
adaptor = new WebRTCAdaptor({
websocketURL: "ws://example.com",
initializeComponents: false,
});
mockPeerConnection = {
setLocalDescription: sinon.fake.rejects(new Error("negotiation error")),
createOffer: sinon.fake.resolves({ sdp: "mockSdp" })
};
adaptor.remotePeerConnection["stream1"] = mockPeerConnection;
});

const streamId = "stream1";
adaptor.remotePeerConnection[streamId] = mockPeerConnection;
adaptor.iceRestart = true;
const loggerSpy = sinon.spy(Logger, "error");

await adaptor.remotePeerConnection[streamId].onnegotiationneeded();

expect(loggerSpy.calledWith('Error during negotiation', sinon.match.instanceOf(Error))).to.be.true;
loggerSpy.restore();
});


});
Loading