summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/geckoview/GeckoViewMedia.jsm
blob: 26a95a9d50efddaba144defa8179363be7ce8edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var EXPORTED_SYMBOLS = ["GeckoViewMedia", "GeckoViewRecordingMedia"];

const { GeckoViewModule } = ChromeUtils.import(
  "resource://gre/modules/GeckoViewModule.jsm"
);
const { GeckoViewUtils } = ChromeUtils.import(
  "resource://gre/modules/GeckoViewUtils.jsm"
);
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);

XPCOMUtils.defineLazyModuleGetters(this, {
  GeckoViewUtils: "resource://gre/modules/GeckoViewUtils.jsm",
});

XPCOMUtils.defineLazyServiceGetter(
  this,
  "MediaManagerService",
  "@mozilla.org/mediaManagerService;1",
  "nsIMediaManagerService"
);

const STATUS_RECORDING = "recording";
const STATUS_INACTIVE = "inactive";
const TYPE_CAMERA = "camera";
const TYPE_MICROPHONE = "microphone";

class GeckoViewMedia extends GeckoViewModule {
  onEnable() {
    this.registerListener([
      "GeckoView:MediaObserve",
      "GeckoView:MediaUnobserve",
      "GeckoView:MediaPlay",
      "GeckoView:MediaPause",
      "GeckoView:MediaSeek",
      "GeckoView:MediaSetVolume",
      "GeckoView:MediaSetMuted",
      "GeckoView:MediaSetPlaybackRate",
    ]);
  }

  onDisable() {
    this.unregisterListener();
  }

  onEvent(aEvent, aData, aCallback) {
    debug`onEvent: event=${aEvent}, data=${aData}`;
    this.messageManager.sendAsyncMessage(aEvent, aData);
  }
}

const GeckoViewRecordingMedia = {
  // The event listener for this is hooked up in GeckoViewStartup.jsm
  observe(aSubject, aTopic, aData) {
    debug`observe: aTopic=${aTopic}`;
    switch (aTopic) {
      case "recording-device-events": {
        this.handleRecordingDeviceEvents();
        break;
      }
    }
  },

  handleRecordingDeviceEvents() {
    const [dispatcher] = GeckoViewUtils.getActiveDispatcherAndWindow();
    if (dispatcher) {
      const windows = MediaManagerService.activeMediaCaptureWindows;
      const devices = [];

      const getStatusString = function(activityStatus) {
        switch (activityStatus) {
          case MediaManagerService.STATE_CAPTURE_ENABLED:
          case MediaManagerService.STATE_CAPTURE_DISABLED:
            return STATUS_RECORDING;
          case MediaManagerService.STATE_NOCAPTURE:
            return STATUS_INACTIVE;
          default:
            throw new Error("Unexpected activityStatus value");
        }
      };

      for (let i = 0; i < windows.length; i++) {
        const win = windows.queryElementAt(i, Ci.nsIDOMWindow);
        const hasCamera = {};
        const hasMicrophone = {};
        const screen = {};
        const window = {};
        const browser = {};
        const mediaDevices = {};
        MediaManagerService.mediaCaptureWindowState(
          win,
          hasCamera,
          hasMicrophone,
          screen,
          window,
          browser,
          mediaDevices
        );
        var cameraStatus = getStatusString(hasCamera.value);
        var microphoneStatus = getStatusString(hasMicrophone.value);
        if (hasCamera.value != MediaManagerService.STATE_NOCAPTURE) {
          devices.push({
            type: TYPE_CAMERA,
            status: cameraStatus,
          });
        }
        if (hasMicrophone.value != MediaManagerService.STATE_NOCAPTURE) {
          devices.push({
            type: TYPE_MICROPHONE,
            status: microphoneStatus,
          });
        }
      }
      dispatcher.sendRequestForResult({
        type: "GeckoView:MediaRecordingStatusChanged",
        devices,
      });
    } else {
      console.log("no dispatcher present");
    }
  },
};

const { debug, warn } = GeckoViewMedia.initLogging("GeckoViewMedia");