Skip to content
New issue

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

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

Already on GitHub? # to your account

Implementing ice restart functionality #716

Open
wants to merge 1 commit 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
18 changes: 15 additions & 3 deletions src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function Peer(options) {
this.receiveMedia = options.receiveMedia || this.parent.config.receiveMedia;
this.channels = {};
this.sid = options.sid || Date.now().toString();
this.iceRestart = false;
// Create an RTCPeerConnection via the polyfill
this.pc = new PeerConnection(this.parent.config.peerConnectionConfig, this.parent.config.peerConnectionConstraints);
this.pc.on('ice', this.onIceCandidate.bind(this));
Expand Down Expand Up @@ -153,6 +154,11 @@ Peer.prototype.send = function (messageType, payload) {
payload: payload,
prefix: webrtcSupport.prefix
};
if (this.iceRestart) {
message.iceRestart = true;
message.oldSessionId = this.oldSessionId;
this.iceRestart = false;
}
this.logger.log('sending', messageType, message);
this.parent.emit('message', message);
};
Expand Down Expand Up @@ -225,10 +231,16 @@ Peer.prototype.start = function () {
//self.send('offer', sessionDescription);
});
};

Peer.prototype.icerestart = function () {
/*
@sessionId - webrtc id from which the icerestart has been initiated. Using it to keep the old
session.
*/
Peer.prototype.icerestart = function (sessionId) {
var constraints = this.receiveMedia;
constraints.mandatory.IceRestart = true;
this.iceRestart = true;
this.oldSessionId = sessionId;
constraints.iceRestart = true;
this.parent.emit('iceRestart');
this.pc.offer(constraints, function (err, success) { });
};

Expand Down
28 changes: 25 additions & 3 deletions src/simplewebrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function SimpleWebRTC(opts) {
};
var item, connection;

this.iceRestart = false;

// We also allow a 'logger' option. It can be any object that implements
// log, warn, and error methods.
// We log nothing by default, following "the rule of silence":
Expand Down Expand Up @@ -80,13 +82,25 @@ function SimpleWebRTC(opts) {
});

connection.on('message', function (message) {
var peers = self.webrtc.getPeers(message.from, message.roomType);
var peer;
var peers, peer;
if (message.iceRestart) {
peers = self.webrtc.getPeers(message.oldSessionId, message.roomType);

self.iceRestart = true;
}
else{
peers = self.webrtc.getPeers(message.from, message.roomType);
}

if (message.type === 'offer') {
if (peers.length) {
peers.forEach(function (p) {
if (p.sid == message.sid) peer = p;
if (p.sid == message.sid) {
peer = p;
if (message.iceRestart) {
self.webrtc.updatePeerId(peer, message.from);
}
}
});
//if (!peer) peer = peers[0]; // fallback for old protocol versions
}
Expand Down Expand Up @@ -248,6 +262,14 @@ function SimpleWebRTC(opts) {
}
});

this.webrtc.on('iceRestart', function() {
self.connection.emit('join', self.roomName, function(err, roomDescription) {
if(err) {
self.emit('error', err)
}
});
});

if (this.config.autoRequestMedia) this.startLocalVideo();
}

Expand Down
4 changes: 4 additions & 0 deletions src/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ WebRTC.prototype.createPeer = function (opts) {
return peer;
};

WebRTC.prototype.updatePeerId = function(peer, sessionId) {
peer.id = sessionId;
};

// removes peers
WebRTC.prototype.removePeers = function (id, type) {
this.getPeers(id, type).forEach(function (peer) {
Expand Down