From 9396548d9ac6cc8e612587864ff9ff0808d2bf86 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Sun, 26 Apr 2015 12:50:11 +0200 Subject: [PATCH] Make it possible to create peer connection in renegotiation mode without any stream. --- static/js/mediastream/usermedia.js | 44 +++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/static/js/mediastream/usermedia.js b/static/js/mediastream/usermedia.js index a700472f..ad6f87c1 100644 --- a/static/js/mediastream/usermedia.js +++ b/static/js/mediastream/usermedia.js @@ -119,6 +119,17 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ } })(); + // Create a dummy stream. + var DummyStream = function() {}; + DummyStream.prototype.stop = function() {}; + DummyStream.prototype.getAudioTracks = function() { return [] }; + DummyStream.prototype.getVideoTracks = function() { return [] }; + DummyStream.not = function(stream) { + // Helper to test if stream is a dummy. + return !stream || stream.stop !== DummyStream.prototype.stop; + }; + + // UserMedia. var UserMedia = function(options) { this.options = $.extend({}, options); @@ -189,8 +200,12 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ // Update stream support. if (oldstream) { _.each(this.peerconnections, function(pc) { - pc.removeStream(oldstream); - pc.addStream(stream); + if (DummyStream.not(oldstream)) { + pc.removeStream(oldstream); + } + if (DummyStream.not(stream)) { + pc.addStream(stream); + } console.log("Updated usermedia stream at peer connection", pc, stream); }); } @@ -269,6 +284,15 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ } } + if (this.renegotiation && this.audioMute && this.videoMute) { + // Fast path as nothing should be shared. + _.defer(_.bind(function() { + this.onUserMediaSuccess(new DummyStream()); + }, this)); + this.started = true; + return true + } + var constraints = $.extend(true, {}, mediaConstraints); if (this.audioMute) { constraints.audio = false; @@ -417,7 +441,7 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ // Remove audio stream, by creating a new stream and doing renegotiation. This // is the way to go to disable the mic when audio is muted. - if (this.localStream) { + if (this.started) { if (this.audioMute !== m) { this.audioMute = m; this.doGetUserMediaWithConstraints(); @@ -464,7 +488,7 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ // Remove video stream, by creating a new stream and doing renegotiation. This // is the way to go to disable the camera when video is muted. - if (this.localStream) { + if (this.started) { if (this.videoMute !== m) { this.videoMute = m; this.doGetUserMediaWithConstraints(); @@ -483,7 +507,9 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ console.log("Add usermedia stream to peer connection", pc, this.localStream); if (this.localStream) { - pc.addStream(this.localStream); + if (DummyStream.not(this.localStream)) { + pc.addStream(this.localStream); + } var id = pc.id; if (!this.peerconnections.hasOwnProperty(id)) { this.peerconnections[id] = pc; @@ -499,7 +525,9 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ console.log("Remove usermedia stream from peer connection", pc, this.localStream); if (this.localStream) { - pc.removeStream(this.localStream); + if (DummyStream.not(this.localStream)) { + pc.removeStream(this.localStream); + } if (this.peerconnections.hasOwnProperty(pc.id)) { delete this.peerconnections[pc.id]; } @@ -509,7 +537,9 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ UserMedia.prototype.attachMediaStream = function(video) { - window.attachMediaStream(video, this.localStream); + if (this.localStream && DummyStream.not(this.localStream)) { + window.attachMediaStream(video, this.localStream); + } };