|
|
@@ -8,32 +8,88 @@ import Statistics from './modules/statistics/statistics';
|
|
8
|
8
|
|
|
9
|
9
|
import * as JitsiMediaDevicesEvents from './JitsiMediaDevicesEvents';
|
|
10
|
10
|
|
|
11
|
|
-const eventEmitter = new EventEmitter();
|
|
12
|
|
-
|
|
13
|
11
|
/**
|
|
14
|
|
- * Gathers data and sends it to statistics.
|
|
15
|
|
- * @param deviceID the device id to log
|
|
16
|
|
- * @param devices list of devices
|
|
|
12
|
+ * Media devices utilities for Jitsi.
|
|
17
|
13
|
*/
|
|
18
|
|
-function logOutputDevice(deviceID, devices) {
|
|
19
|
|
- const device
|
|
20
|
|
- = devices.find(
|
|
21
|
|
- d => d.kind === 'audiooutput' && d.deviceId === deviceID);
|
|
22
|
|
-
|
|
23
|
|
- if (device) {
|
|
24
|
|
- Statistics.sendActiveDeviceListEvent(
|
|
25
|
|
- RTC.getEventDataForActiveDevice(device));
|
|
|
14
|
+class JitsiMediaDevices {
|
|
|
15
|
+ /**
|
|
|
16
|
+ * Initializes a {@code JitsiMediaDevices} object. There will be a single
|
|
|
17
|
+ * instance of this class.
|
|
|
18
|
+ */
|
|
|
19
|
+ constructor() {
|
|
|
20
|
+ this._eventEmitter = new EventEmitter();
|
|
|
21
|
+ this._grantedPermissions = {};
|
|
|
22
|
+
|
|
|
23
|
+ RTC.addListener(
|
|
|
24
|
+ RTCEvents.DEVICE_LIST_CHANGED,
|
|
|
25
|
+ devices =>
|
|
|
26
|
+ this._eventEmitter.emit(
|
|
|
27
|
+ JitsiMediaDevicesEvents.DEVICE_LIST_CHANGED,
|
|
|
28
|
+ devices));
|
|
|
29
|
+ RTC.addListener(
|
|
|
30
|
+ RTCEvents.DEVICE_LIST_AVAILABLE,
|
|
|
31
|
+ devices =>
|
|
|
32
|
+ this._logOutputDevice(
|
|
|
33
|
+ this.getAudioOutputDevice(),
|
|
|
34
|
+ devices));
|
|
|
35
|
+ RTC.addListener(
|
|
|
36
|
+ RTCEvents.GRANTED_PERMISSIONS,
|
|
|
37
|
+ grantedPermissions =>
|
|
|
38
|
+ this._handleGrantedPermissions(grantedPermissions));
|
|
|
39
|
+
|
|
|
40
|
+ // Test if the W3C Permissions API is implemented and the 'camera' and
|
|
|
41
|
+ // 'microphone' permissions are implemented. (Testing for at least one
|
|
|
42
|
+ // of them seems sufficient).
|
|
|
43
|
+ this._permissionsApiSupported = new Promise(resolve => {
|
|
|
44
|
+ if (!navigator.permissions) {
|
|
|
45
|
+ resolve(false);
|
|
|
46
|
+
|
|
|
47
|
+ return;
|
|
|
48
|
+ }
|
|
|
49
|
+
|
|
|
50
|
+ navigator.permissions.query({ name: 'camera ' })
|
|
|
51
|
+ .then(() => resolve(true), () => resolve(false));
|
|
|
52
|
+ });
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ /**
|
|
|
56
|
+ * Updated the local granted permissions cache. A permissions might be
|
|
|
57
|
+ * granted, denied, or undefined. This is represented by having its media
|
|
|
58
|
+ * type key set to {@code true} or {@code false} respectively.
|
|
|
59
|
+ *
|
|
|
60
|
+ * @param {Object} grantedPermissions - Array with the permissions
|
|
|
61
|
+ * which were granted.
|
|
|
62
|
+ */
|
|
|
63
|
+ _handleGrantedPermissions(grantedPermissions) {
|
|
|
64
|
+ this._grantedPermissions = {
|
|
|
65
|
+ ...this._grantedPermissions,
|
|
|
66
|
+ ...grantedPermissions
|
|
|
67
|
+ };
|
|
|
68
|
+ }
|
|
|
69
|
+
|
|
|
70
|
+ /**
|
|
|
71
|
+ * Gathers data and sends it to statistics.
|
|
|
72
|
+ * @param deviceID the device id to log
|
|
|
73
|
+ * @param devices list of devices
|
|
|
74
|
+ */
|
|
|
75
|
+ _logOutputDevice(deviceID, devices) {
|
|
|
76
|
+ const device
|
|
|
77
|
+ = devices.find(
|
|
|
78
|
+ d => d.kind === 'audiooutput' && d.deviceId === deviceID);
|
|
|
79
|
+
|
|
|
80
|
+ if (device) {
|
|
|
81
|
+ Statistics.sendActiveDeviceListEvent(
|
|
|
82
|
+ RTC.getEventDataForActiveDevice(device));
|
|
|
83
|
+ }
|
|
26
|
84
|
}
|
|
27
|
|
-}
|
|
28
|
85
|
|
|
29
|
|
-const JitsiMediaDevices = {
|
|
30
|
86
|
/**
|
|
31
|
87
|
* Executes callback with list of media devices connected.
|
|
32
|
88
|
* @param {function} callback
|
|
33
|
89
|
*/
|
|
34
|
90
|
enumerateDevices(callback) {
|
|
35
|
91
|
RTC.enumerateDevices(callback);
|
|
36
|
|
- },
|
|
|
92
|
+ }
|
|
37
|
93
|
|
|
38
|
94
|
/**
|
|
39
|
95
|
* Checks if its possible to enumerate available cameras/micropones.
|
|
|
@@ -43,7 +99,7 @@ const JitsiMediaDevices = {
|
|
43
|
99
|
*/
|
|
44
|
100
|
isDeviceListAvailable() {
|
|
45
|
101
|
return RTC.isDeviceListAvailable();
|
|
46
|
|
- },
|
|
|
102
|
+ }
|
|
47
|
103
|
|
|
48
|
104
|
/**
|
|
49
|
105
|
* Returns true if changing the input (camera / microphone) or output
|
|
|
@@ -54,26 +110,58 @@ const JitsiMediaDevices = {
|
|
54
|
110
|
*/
|
|
55
|
111
|
isDeviceChangeAvailable(deviceType) {
|
|
56
|
112
|
return RTC.isDeviceChangeAvailable(deviceType);
|
|
57
|
|
- },
|
|
|
113
|
+ }
|
|
58
|
114
|
|
|
59
|
115
|
/**
|
|
60
|
|
- * Returns true if user granted permission to media devices.
|
|
|
116
|
+ * Checks if the permission for the given device was granted.
|
|
|
117
|
+ *
|
|
61
|
118
|
* @param {'audio'|'video'} [type] - type of devices to check,
|
|
62
|
119
|
* undefined stands for both 'audio' and 'video' together
|
|
63
|
|
- * @returns {boolean}
|
|
|
120
|
+ * @returns {Promise<boolean>}
|
|
64
|
121
|
*/
|
|
65
|
122
|
isDevicePermissionGranted(type) {
|
|
66
|
|
- const permissions = RTC.getDeviceAvailability();
|
|
67
|
|
-
|
|
68
|
|
- switch (type) {
|
|
69
|
|
- case MediaType.VIDEO:
|
|
70
|
|
- return permissions.video === true;
|
|
71
|
|
- case MediaType.AUDIO:
|
|
72
|
|
- return permissions.audio === true;
|
|
73
|
|
- default:
|
|
74
|
|
- return permissions.video === true && permissions.audio === true;
|
|
75
|
|
- }
|
|
76
|
|
- },
|
|
|
123
|
+ return new Promise(resolve => {
|
|
|
124
|
+ // Shortcut: first check if we already know the permission was
|
|
|
125
|
+ // granted.
|
|
|
126
|
+ if (type in this._grantedPermissions) {
|
|
|
127
|
+ resolve(this._grantedPermissions[type]);
|
|
|
128
|
+
|
|
|
129
|
+ return;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ // Check using the Permissions API.
|
|
|
133
|
+ this._permissionsApiSupported.then(supported => {
|
|
|
134
|
+ if (!supported) {
|
|
|
135
|
+ resolve(false);
|
|
|
136
|
+
|
|
|
137
|
+ return;
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ const promises = [];
|
|
|
141
|
+
|
|
|
142
|
+ switch (type) {
|
|
|
143
|
+ case MediaType.VIDEO:
|
|
|
144
|
+ promises.push(
|
|
|
145
|
+ navigator.permissions.query({ name: 'camera' }));
|
|
|
146
|
+ break;
|
|
|
147
|
+ case MediaType.AUDIO:
|
|
|
148
|
+ promises.push(
|
|
|
149
|
+ navigator.permissions.query({ name: 'microphone' }));
|
|
|
150
|
+ break;
|
|
|
151
|
+ default:
|
|
|
152
|
+ promises.push(
|
|
|
153
|
+ navigator.permissions.query({ name: 'camera' }));
|
|
|
154
|
+ promises.push(
|
|
|
155
|
+ navigator.permissions.query({ name: 'microphone' }));
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ Promise.all(promises).then(
|
|
|
159
|
+ r => resolve(r.every(Boolean)),
|
|
|
160
|
+ () => resolve(false)
|
|
|
161
|
+ );
|
|
|
162
|
+ });
|
|
|
163
|
+ });
|
|
|
164
|
+ }
|
|
77
|
165
|
|
|
78
|
166
|
/**
|
|
79
|
167
|
* Returns true if it is possible to be simultaneously capturing audio
|
|
|
@@ -83,7 +171,7 @@ const JitsiMediaDevices = {
|
|
83
|
171
|
*/
|
|
84
|
172
|
isMultipleAudioInputSupported() {
|
|
85
|
173
|
return !browser.isFirefox();
|
|
86
|
|
- },
|
|
|
174
|
+ }
|
|
87
|
175
|
|
|
88
|
176
|
/**
|
|
89
|
177
|
* Returns currently used audio output device id, 'default' stands
|
|
|
@@ -92,7 +180,7 @@ const JitsiMediaDevices = {
|
|
92
|
180
|
*/
|
|
93
|
181
|
getAudioOutputDevice() {
|
|
94
|
182
|
return RTC.getAudioOutputDevice();
|
|
95
|
|
- },
|
|
|
183
|
+ }
|
|
96
|
184
|
|
|
97
|
185
|
/**
|
|
98
|
186
|
* Sets current audio output device.
|
|
|
@@ -103,18 +191,18 @@ const JitsiMediaDevices = {
|
|
103
|
191
|
* otherwise
|
|
104
|
192
|
*/
|
|
105
|
193
|
setAudioOutputDevice(deviceId) {
|
|
106
|
|
-
|
|
107
|
194
|
const availableDevices = RTC.getCurrentlyAvailableMediaDevices();
|
|
108
|
195
|
|
|
109
|
196
|
if (availableDevices && availableDevices.length > 0) {
|
|
110
|
197
|
// if we have devices info report device to stats
|
|
111
|
198
|
// normally this will not happen on startup as this method is called
|
|
112
|
199
|
// too early. This will happen only on user selection of new device
|
|
113
|
|
- logOutputDevice(deviceId, RTC.getCurrentlyAvailableMediaDevices());
|
|
|
200
|
+ this._logOutputDevice(
|
|
|
201
|
+ deviceId, RTC.getCurrentlyAvailableMediaDevices());
|
|
114
|
202
|
}
|
|
115
|
203
|
|
|
116
|
204
|
return RTC.setAudioOutputDevice(deviceId);
|
|
117
|
|
- },
|
|
|
205
|
+ }
|
|
118
|
206
|
|
|
119
|
207
|
/**
|
|
120
|
208
|
* Adds an event handler.
|
|
|
@@ -122,8 +210,8 @@ const JitsiMediaDevices = {
|
|
122
|
210
|
* @param {function} handler - event handler
|
|
123
|
211
|
*/
|
|
124
|
212
|
addEventListener(event, handler) {
|
|
125
|
|
- eventEmitter.addListener(event, handler);
|
|
126
|
|
- },
|
|
|
213
|
+ this._eventEmitter.addListener(event, handler);
|
|
|
214
|
+ }
|
|
127
|
215
|
|
|
128
|
216
|
/**
|
|
129
|
217
|
* Removes event handler.
|
|
|
@@ -131,16 +219,16 @@ const JitsiMediaDevices = {
|
|
131
|
219
|
* @param {function} handler - event handler
|
|
132
|
220
|
*/
|
|
133
|
221
|
removeEventListener(event, handler) {
|
|
134
|
|
- eventEmitter.removeListener(event, handler);
|
|
135
|
|
- },
|
|
|
222
|
+ this._eventEmitter.removeListener(event, handler);
|
|
|
223
|
+ }
|
|
136
|
224
|
|
|
137
|
225
|
/**
|
|
138
|
226
|
* Emits an event.
|
|
139
|
227
|
* @param {string} event - event name
|
|
140
|
228
|
*/
|
|
141
|
229
|
emitEvent(event, ...args) {
|
|
142
|
|
- eventEmitter.emit(event, ...args);
|
|
143
|
|
- },
|
|
|
230
|
+ this._eventEmitter.emit(event, ...args);
|
|
|
231
|
+ }
|
|
144
|
232
|
|
|
145
|
233
|
/**
|
|
146
|
234
|
* Returns whether or not the current browser can support capturing video,
|
|
|
@@ -154,20 +242,6 @@ const JitsiMediaDevices = {
|
|
154
|
242
|
// JitsiMediaDevices.
|
|
155
|
243
|
return browser.supportsVideo();
|
|
156
|
244
|
}
|
|
157
|
|
-};
|
|
158
|
|
-
|
|
159
|
|
-
|
|
160
|
|
-RTC.addListener(
|
|
161
|
|
- RTCEvents.DEVICE_LIST_CHANGED,
|
|
162
|
|
- devices =>
|
|
163
|
|
- eventEmitter.emit(
|
|
164
|
|
- JitsiMediaDevicesEvents.DEVICE_LIST_CHANGED,
|
|
165
|
|
- devices));
|
|
166
|
|
-RTC.addListener(
|
|
167
|
|
- RTCEvents.DEVICE_LIST_AVAILABLE,
|
|
168
|
|
- devices =>
|
|
169
|
|
- logOutputDevice(
|
|
170
|
|
- JitsiMediaDevices.getAudioOutputDevice(),
|
|
171
|
|
- devices));
|
|
172
|
|
-
|
|
173
|
|
-export default JitsiMediaDevices;
|
|
|
245
|
+}
|
|
|
246
|
+
|
|
|
247
|
+export default new JitsiMediaDevices();
|