diff --git a/static/js/controllers/uicontroller.js b/static/js/controllers/uicontroller.js index fadfe0d9..2113d56e 100644 --- a/static/js/controllers/uicontroller.js +++ b/static/js/controllers/uicontroller.js @@ -122,6 +122,7 @@ define(['jquery', 'underscore', 'bigscreen', 'moment', 'sjcl', 'modernizr', 'web $scope.chatMessagesUnseen = 0; $scope.autoAccept = null; $scope.isCollapsed = true; + $scope.usermedia = null; $scope.setStatus = function(status) { // This is the connection status to signaling server. @@ -147,7 +148,13 @@ define(['jquery', 'underscore', 'bigscreen', 'moment', 'sjcl', 'modernizr', 'web $scope.refreshWebrtcSettings = function() { // Refresh constraints. - constraints.refresh($scope.master.settings); + constraints.refresh($scope.master.settings).then(function() { + var um = $scope.usermedia; + if (um && um.renegotiation && um.started) { + // Trigger renegotiation if supported and started. + um.doGetUserMediaWithConstraints(mediaStream.webrtc.settings.mediaConstraints); + } + }); }; $scope.refreshWebrtcSettings(); // Call once for bootstrap. @@ -407,6 +414,12 @@ define(['jquery', 'underscore', 'bigscreen', 'moment', 'sjcl', 'modernizr', 'web alertify.dialog.alert(translation._("Oops") + "
" + message); }); + mediaStream.webrtc.e.on("usermedia", function(event, usermedia) { + safeApply($scope, function(scope) { + scope.usermedia = usermedia; + }); + }); + appData.flags.autoreconnect = true; appData.flags.autoreconnectDelay = 0; diff --git a/static/js/mediastream/usermedia.js b/static/js/mediastream/usermedia.js index 8f67c78c..a700472f 100644 --- a/static/js/mediastream/usermedia.js +++ b/static/js/mediastream/usermedia.js @@ -126,7 +126,6 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ this.localStream = null; this.started = false; - //this.delay = 0; this.peerconnections = {}; @@ -253,7 +252,6 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ if (!mediaConstraints) { mediaConstraints = currentcall.mediaConstraints; } - this.mediaConstraints = mediaConstraints; return this.doGetUserMediaWithConstraints(mediaConstraints); @@ -263,6 +261,12 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ if (!mediaConstraints) { mediaConstraints = this.mediaConstraints; + } else { + this.mediaConstraints = mediaConstraints; + if (this.localStream) { + // Release stream early if any to be able to apply new constraints. + this.replaceStream(null); + } } var constraints = $.extend(true, {}, mediaConstraints); @@ -310,34 +314,48 @@ define(['jquery', 'underscore', 'audiocontext', 'webrtc.adapter'], function($, _ }; - UserMedia.prototype.onLocalStream = function(stream) { + UserMedia.prototype.replaceStream = function(stream) { var oldStream = this.localStream; - if (oldStream) { - oldStream.onended = function() {}; + if (oldStream && oldStream.active) { + // Let old stream silently end. + oldStream.onended = function() { + console.log("Silently ended replaced user media stream."); + }; oldStream.stop(); + } + + if (stream) { + // Get notified of end events. + stream.onended = _.bind(function(event) { + console.log("User media stream ended."); + if (this.started) { + this.stop(); + } + }, this); + // Set new stream. + this.localStream = stream; + this.e.triggerHandler("localstream", [stream, oldStream, this]); + } + + return oldStream && stream; + + }; + + UserMedia.prototype.onLocalStream = function(stream) { + + if (this.replaceStream(stream)) { + // We replaced a stream. setTimeout(_.bind(function() { this.e.triggerHandler("mediachanged", [this]); }, this), 0); } else { - // Let webrtc handle the rest. + // We are new. setTimeout(_.bind(function() { this.e.triggerHandler("mediasuccess", [this]); }, this), 0); } - // Get notified of end events. - stream.onended = _.bind(function(event) { - console.log("User media stream ended."); - if (this.started) { - this.stop(); - } - }, this); - - // Set new stream. - this.localStream = stream; - this.e.triggerHandler("localstream", [stream, oldStream, this]); - }; UserMedia.prototype.stop = function() {