webrtcUI.jsm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. this.EXPORTED_SYMBOLS = ["webrtcUI"];
  6. const Cu = Components.utils;
  7. const Cc = Components.classes;
  8. const Ci = Components.interfaces;
  9. Cu.import("resource://gre/modules/Services.jsm");
  10. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  11. XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
  12. "resource://gre/modules/PluralForm.jsm");
  13. XPCOMUtils.defineLazyServiceGetter(this, "MediaManagerService",
  14. "@mozilla.org/mediaManagerService;1",
  15. "nsIMediaManagerService");
  16. this.webrtcUI = {
  17. init: function () {
  18. Services.obs.addObserver(handleRequest, "getUserMedia:request", false);
  19. Services.obs.addObserver(updateIndicators, "recording-device-events", false);
  20. Services.obs.addObserver(removeBrowserSpecificIndicator, "recording-window-ended", false);
  21. },
  22. uninit: function () {
  23. Services.obs.removeObserver(handleRequest, "getUserMedia:request");
  24. Services.obs.removeObserver(updateIndicators, "recording-device-events");
  25. Services.obs.removeObserver(removeBrowserSpecificIndicator, "recording-window-ended");
  26. },
  27. showGlobalIndicator: false,
  28. get activeStreams() {
  29. let contentWindowSupportsArray = MediaManagerService.activeMediaCaptureWindows;
  30. let count = contentWindowSupportsArray.Count();
  31. let activeStreams = [];
  32. for (let i = 0; i < count; i++) {
  33. let contentWindow = contentWindowSupportsArray.GetElementAt(i);
  34. let browser = getBrowserForWindow(contentWindow);
  35. let browserWindow = browser.ownerDocument.defaultView;
  36. let tab = browserWindow.gBrowser &&
  37. browserWindow.gBrowser._getTabForContentWindow(contentWindow.top);
  38. activeStreams.push({
  39. uri: contentWindow.location.href,
  40. tab: tab,
  41. browser: browser
  42. });
  43. }
  44. return activeStreams;
  45. }
  46. }
  47. function getBrowserForWindowId(aWindowID) {
  48. return getBrowserForWindow(Services.wm.getOuterWindowWithId(aWindowID));
  49. }
  50. function getBrowserForWindow(aContentWindow) {
  51. return aContentWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  52. .getInterface(Ci.nsIWebNavigation)
  53. .QueryInterface(Ci.nsIDocShell)
  54. .chromeEventHandler;
  55. }
  56. function handleRequest(aSubject, aTopic, aData) {
  57. let {windowID: windowID, callID: callID} = JSON.parse(aData);
  58. let params = aSubject.QueryInterface(Ci.nsIMediaStreamOptions);
  59. Services.wm.getMostRecentWindow(null).navigator.mozGetUserMediaDevices(
  60. function (devices) {
  61. prompt(windowID, callID, params.audio, params.video || params.picture, devices);
  62. },
  63. function (error) {
  64. // bug 827146 -- In the future, the UI should catch NO_DEVICES_FOUND
  65. // and allow the user to plug in a device, instead of immediately failing.
  66. denyRequest(callID, error);
  67. }
  68. );
  69. }
  70. function denyRequest(aCallID, aError) {
  71. let msg = null;
  72. if (aError) {
  73. msg = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
  74. msg.data = aError;
  75. }
  76. Services.obs.notifyObservers(msg, "getUserMedia:response:deny", aCallID);
  77. }
  78. function prompt(aWindowID, aCallID, aAudioRequested, aVideoRequested, aDevices) {
  79. let audioDevices = [];
  80. let videoDevices = [];
  81. for (let device of aDevices) {
  82. device = device.QueryInterface(Ci.nsIMediaDevice);
  83. switch (device.type) {
  84. case "audio":
  85. if (aAudioRequested)
  86. audioDevices.push(device);
  87. break;
  88. case "video":
  89. if (aVideoRequested)
  90. videoDevices.push(device);
  91. break;
  92. }
  93. }
  94. let requestType;
  95. if (audioDevices.length && videoDevices.length)
  96. requestType = "CameraAndMicrophone";
  97. else if (audioDevices.length)
  98. requestType = "Microphone";
  99. else if (videoDevices.length)
  100. requestType = "Camera";
  101. else {
  102. denyRequest(aCallID, "NO_DEVICES_FOUND");
  103. return;
  104. }
  105. let contentWindow = Services.wm.getOuterWindowWithId(aWindowID);
  106. let host = contentWindow.document.documentURIObject.host;
  107. let browser = getBrowserForWindow(contentWindow);
  108. let chromeDoc = browser.ownerDocument;
  109. let chromeWin = chromeDoc.defaultView;
  110. let stringBundle = chromeWin.gNavigatorBundle;
  111. let message = stringBundle.getFormattedString("getUserMedia.share" + requestType + ".message",
  112. [ host ]);
  113. let mainAction = {
  114. label: PluralForm.get(requestType == "CameraAndMicrophone" ? 2 : 1,
  115. stringBundle.getString("getUserMedia.shareSelectedDevices.label")),
  116. accessKey: stringBundle.getString("getUserMedia.shareSelectedDevices.accesskey"),
  117. // The real callback will be set during the "showing" event. The
  118. // empty function here is so that PopupNotifications.show doesn't
  119. // reject the action.
  120. callback: function() {}
  121. };
  122. let secondaryActions = [{
  123. label: stringBundle.getString("getUserMedia.denyRequest.label"),
  124. accessKey: stringBundle.getString("getUserMedia.denyRequest.accesskey"),
  125. callback: function () {
  126. denyRequest(aCallID);
  127. }
  128. }];
  129. let options = {
  130. eventCallback: function(aTopic, aNewBrowser) {
  131. if (aTopic == "swapping")
  132. return true;
  133. if (aTopic != "showing")
  134. return false;
  135. let chromeDoc = this.browser.ownerDocument;
  136. function listDevices(menupopup, devices) {
  137. while (menupopup.lastChild)
  138. menupopup.removeChild(menupopup.lastChild);
  139. let deviceIndex = 0;
  140. for (let device of devices) {
  141. addDeviceToList(menupopup, device.name, deviceIndex);
  142. deviceIndex++;
  143. }
  144. }
  145. function addDeviceToList(menupopup, deviceName, deviceIndex) {
  146. let menuitem = chromeDoc.createElement("menuitem");
  147. menuitem.setAttribute("value", deviceIndex);
  148. menuitem.setAttribute("label", deviceName);
  149. menuitem.setAttribute("tooltiptext", deviceName);
  150. menupopup.appendChild(menuitem);
  151. }
  152. chromeDoc.getElementById("webRTC-selectCamera").hidden = !videoDevices.length;
  153. chromeDoc.getElementById("webRTC-selectMicrophone").hidden = !audioDevices.length;
  154. let camMenupopup = chromeDoc.getElementById("webRTC-selectCamera-menupopup");
  155. let micMenupopup = chromeDoc.getElementById("webRTC-selectMicrophone-menupopup");
  156. listDevices(camMenupopup, videoDevices);
  157. listDevices(micMenupopup, audioDevices);
  158. if (requestType == "CameraAndMicrophone") {
  159. let stringBundle = chromeDoc.defaultView.gNavigatorBundle;
  160. addDeviceToList(camMenupopup, stringBundle.getString("getUserMedia.noVideo.label"), "-1");
  161. addDeviceToList(micMenupopup, stringBundle.getString("getUserMedia.noAudio.label"), "-1");
  162. }
  163. this.mainAction.callback = function() {
  164. let allowedDevices = Cc["@mozilla.org/supports-array;1"]
  165. .createInstance(Ci.nsISupportsArray);
  166. if (videoDevices.length) {
  167. let videoDeviceIndex = chromeDoc.getElementById("webRTC-selectCamera-menulist").value;
  168. if (videoDeviceIndex != "-1")
  169. allowedDevices.AppendElement(videoDevices[videoDeviceIndex]);
  170. }
  171. if (audioDevices.length) {
  172. let audioDeviceIndex = chromeDoc.getElementById("webRTC-selectMicrophone-menulist").value;
  173. if (audioDeviceIndex != "-1")
  174. allowedDevices.AppendElement(audioDevices[audioDeviceIndex]);
  175. }
  176. if (allowedDevices.Count() == 0) {
  177. denyRequest(aCallID);
  178. return;
  179. }
  180. Services.obs.notifyObservers(allowedDevices, "getUserMedia:response:allow", aCallID);
  181. };
  182. return true;
  183. }
  184. };
  185. chromeWin.PopupNotifications.show(browser, "webRTC-shareDevices", message,
  186. "webRTC-shareDevices-notification-icon", mainAction,
  187. secondaryActions, options);
  188. }
  189. function updateIndicators() {
  190. webrtcUI.showGlobalIndicator =
  191. MediaManagerService.activeMediaCaptureWindows.Count() > 0;
  192. let e = Services.wm.getEnumerator("navigator:browser");
  193. while (e.hasMoreElements())
  194. e.getNext().WebrtcIndicator.updateButton();
  195. for (let {browser: browser} of webrtcUI.activeStreams)
  196. showBrowserSpecificIndicator(browser);
  197. }
  198. function showBrowserSpecificIndicator(aBrowser) {
  199. let hasVideo = {};
  200. let hasAudio = {};
  201. MediaManagerService.mediaCaptureWindowState(aBrowser.contentWindow,
  202. hasVideo, hasAudio);
  203. let captureState;
  204. if (hasVideo.value && hasAudio.value) {
  205. captureState = "CameraAndMicrophone";
  206. } else if (hasVideo.value) {
  207. captureState = "Camera";
  208. } else if (hasAudio.value) {
  209. captureState = "Microphone";
  210. } else {
  211. Cu.reportError("showBrowserSpecificIndicator: got neither video nor audio access");
  212. return;
  213. }
  214. let chromeWin = aBrowser.ownerDocument.defaultView;
  215. let stringBundle = chromeWin.gNavigatorBundle;
  216. let message = stringBundle.getString("getUserMedia.sharing" + captureState + ".message2");
  217. let windowId = aBrowser.contentWindow
  218. .QueryInterface(Ci.nsIInterfaceRequestor)
  219. .getInterface(Ci.nsIDOMWindowUtils)
  220. .currentInnerWindowID;
  221. let mainAction = {
  222. label: "Continue Sharing", //stringBundle.getString("getUserMedia.continueSharing.label"),
  223. accessKey: "C", //stringBundle.getString("getUserMedia.continueSharing.accesskey"),
  224. callback: function () {},
  225. dismiss: true
  226. };
  227. let secondaryActions = [{
  228. label: "Stop Sharing", //stringBundle.getString("getUserMedia.stopSharing.label"),
  229. accessKey: "S", //stringBundle.getString("getUserMedia.stopSharing.accesskey"),
  230. callback: function () {
  231. Services.obs.notifyObservers(null, "getUserMedia:revoke", windowId);
  232. }
  233. }];
  234. let options = {
  235. hideNotNow: true,
  236. dismissed: true,
  237. eventCallback: function(aTopic) aTopic == "swapping"
  238. };
  239. chromeWin.PopupNotifications.show(aBrowser, "webRTC-sharingDevices", message,
  240. "webRTC-sharingDevices-notification-icon", mainAction,
  241. secondaryActions, options);
  242. }
  243. function removeBrowserSpecificIndicator(aSubject, aTopic, aData) {
  244. let browser = getBrowserForWindowId(aData);
  245. let PopupNotifications = browser.ownerDocument.defaultView.PopupNotifications;
  246. let notification = PopupNotifications &&
  247. PopupNotifications.getNotification("webRTC-sharingDevices",
  248. browser);
  249. if (notification)
  250. PopupNotifications.remove(notification);
  251. }