webapprt/WebRTCHandler.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/webapprt/WebRTCHandler.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [];
    1.11 +
    1.12 +let Cc = Components.classes;
    1.13 +let Ci = Components.interfaces;
    1.14 +let Cu = Components.utils;
    1.15 +
    1.16 +Cu.import("resource://gre/modules/Services.jsm");
    1.17 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.18 +
    1.19 +function handleRequest(aSubject, aTopic, aData) {
    1.20 +  let { windowID, callID } = aSubject;
    1.21 +  let constraints = aSubject.getConstraints();
    1.22 +  let contentWindow = Services.wm.getOuterWindowWithId(windowID);
    1.23 +
    1.24 +  contentWindow.navigator.mozGetUserMediaDevices(
    1.25 +    constraints,
    1.26 +    function (devices) {
    1.27 +      prompt(contentWindow, callID, constraints.audio,
    1.28 +             constraints.video || constraints.picture,
    1.29 +             devices);
    1.30 +    },
    1.31 +    function (error) {
    1.32 +      denyRequest(callID, error);
    1.33 +    });
    1.34 +}
    1.35 +
    1.36 +function prompt(aWindow, aCallID, aAudioRequested, aVideoRequested, aDevices) {
    1.37 +  let audioDevices = [];
    1.38 +  let videoDevices = [];
    1.39 +  for (let device of aDevices) {
    1.40 +    device = device.QueryInterface(Ci.nsIMediaDevice);
    1.41 +    switch (device.type) {
    1.42 +      case "audio":
    1.43 +        if (aAudioRequested) {
    1.44 +          audioDevices.push(device);
    1.45 +        }
    1.46 +        break;
    1.47 +
    1.48 +      case "video":
    1.49 +        if (aVideoRequested) {
    1.50 +          videoDevices.push(device);
    1.51 +        }
    1.52 +        break;
    1.53 +    }
    1.54 +  }
    1.55 +
    1.56 +  if (audioDevices.length == 0 && videoDevices.length == 0) {
    1.57 +    denyRequest(aCallID);
    1.58 +    return;
    1.59 +  }
    1.60 +
    1.61 +  let params = {
    1.62 +                 videoDevices: videoDevices,
    1.63 +                 audioDevices: audioDevices,
    1.64 +                 out: null
    1.65 +               };
    1.66 +  aWindow.openDialog("chrome://webapprt/content/getUserMediaDialog.xul", "",
    1.67 +                     "chrome, dialog, modal", params).focus();
    1.68 +
    1.69 +  if (!params.out) {
    1.70 +    denyRequest(aCallID);
    1.71 +    return;
    1.72 +  }
    1.73 +
    1.74 +  let allowedDevices = Cc["@mozilla.org/supports-array;1"].
    1.75 +                       createInstance(Ci.nsISupportsArray);
    1.76 +  let videoIndex = params.out.video;
    1.77 +  let audioIndex = params.out.audio;
    1.78 +
    1.79 +  if (videoIndex != -1) {
    1.80 +    allowedDevices.AppendElement(videoDevices[videoIndex]);
    1.81 +  }
    1.82 +
    1.83 +  if (audioIndex != -1) {
    1.84 +    allowedDevices.AppendElement(audioDevices[audioIndex]);
    1.85 +  }
    1.86 +
    1.87 +  if (allowedDevices.Count()) {
    1.88 +    Services.obs.notifyObservers(allowedDevices, "getUserMedia:response:allow",
    1.89 +                                 aCallID);
    1.90 +  } else {
    1.91 +    denyRequest(aCallID);
    1.92 +  }
    1.93 +}
    1.94 +
    1.95 +function denyRequest(aCallID, aError) {
    1.96 +  let msg = null;
    1.97 +  if (aError) {
    1.98 +    msg = Cc["@mozilla.org/supports-string;1"].
    1.99 +          createInstance(Ci.nsISupportsString);
   1.100 +    msg.data = aError;
   1.101 +  }
   1.102 +
   1.103 +  Services.obs.notifyObservers(msg, "getUserMedia:response:deny", aCallID);
   1.104 +}
   1.105 +
   1.106 +Services.obs.addObserver(handleRequest, "getUserMedia:request", false);

mercurial