diff --git a/static/js/directives/screenshare.js b/static/js/directives/screenshare.js index 0eb8ae12..6a060ba9 100644 --- a/static/js/directives/screenshare.js +++ b/static/js/directives/screenshare.js @@ -281,7 +281,10 @@ define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials console.log("Screen share stopped."); } - $scope.layout.screenshare = false; + if ($scope.layout.screenshare) { + screensharing.cancelGetScreen(); + $scope.layout.screenshare = false; + } }; @@ -297,6 +300,10 @@ define(['jquery', 'underscore', 'text!partials/screenshare.html', 'text!partials }; + mediaStream.webrtc.e.on("done", function() { + $scope.stopScreenshare(); + }); + $scope.$watch("layout.screenshare", function(newval, oldval) { if (newval && !oldval) { $scope.doScreenshare(); diff --git a/static/js/services/chromeextension.js b/static/js/services/chromeextension.js index 532c8909..2853d743 100644 --- a/static/js/services/chromeextension.js +++ b/static/js/services/chromeextension.js @@ -54,14 +54,22 @@ define(["underscore"], function(_) { case "Call": var deferred = this.registry[data.n]; if (deferred) { - delete this.registry[data.n]; var call = data.Call; - if (call.Type === "Result") { + switch (call.Type) { + case "Result": + delete this.registry[data.n]; //console.log("Call complete with result", call); deferred.resolve(call.Result); - } else { + break; + case "Notify": + //console.log("Notify", call); + deferred.notify(call.Notify); + break; + case "Error": + delete this.registry[data.n]; //console.log("Call failed with error", call); deferred.reject(call.Error); + break } } else { console.warn("Unknown call reference received", data, this.registry, this); @@ -80,7 +88,7 @@ define(["underscore"], function(_) { $window.addEventListener("message", function(event) { //console.log("message", event.origin, event.source === window, event); if (event.source === window && event.data.answer) { - // Only let through our own messages marked as answer. + // Only process answers to avoid loops. extension.onMessage(event); } }); diff --git a/static/js/services/screensharing.js b/static/js/services/screensharing.js index 1c0341b0..2a7450fd 100644 --- a/static/js/services/screensharing.js +++ b/static/js/services/screensharing.js @@ -25,7 +25,12 @@ define(['underscore', 'webrtc.adapter'], function(_) { // Check if we can do screensharing. var supported = false; + + // Define our helpers. var prepare = null; + var cancel = null; + + // Chrome support. if ($window.webrtcDetectedBrowser === "chrome") { if ($window.webrtcDetectedVersion >= 32 && $window.webrtcDetectedVersion < 37) { @@ -52,6 +57,7 @@ define(['underscore', 'webrtc.adapter'], function(_) { if (chromeExtension.available) { supported = true; + var pending = null; prepare = function(options) { var select = chromeExtension.call({ Type: "Action", @@ -59,7 +65,8 @@ define(['underscore', 'webrtc.adapter'], function(_) { }); var d = $q.defer(); select.then(function(id) { - //console.log("Prepare screensharing success", id); + // Success with id. + pending = null; if (id) { var opts = _.extend({ chromeMediaSource: "desktop", @@ -70,15 +77,30 @@ define(['underscore', 'webrtc.adapter'], function(_) { d.resolve(null); } }, function(err) { + // Error. + pending = null; console.log("Failed to prepare screensharing", err); d.reject(err); + }, function(data) { + // Notify. + pending = data; }); return d.promise; }; + cancel = function() { + if (pending !== null) { + chromeExtension.call({ + Type: "Action", + Action: "cancelChooseDesktopMedia", + Args: pending + }); + pending = null; + } + }; } } else { - // Currently Chrome only. + // Currently Chrome only - sorry. } // public API. @@ -92,6 +114,11 @@ define(['underscore', 'webrtc.adapter'], function(_) { d.reject("No implementation to get screen."); return d.promise; } + }, + cancelGetScreen: function() { + if (cancel) { + cancel(); + } } }