Selaa lähdekoodia

feat(ts) migrate ActiveDeviceDetector to TS

master
Naman Jain 4 kuukautta sitten
vanhempi
commit
868ed78280
No account linked to committer's email address
1 muutettua tiedostoa jossa 22 lisäystä ja 13 poistoa
  1. 22
    13
      modules/detection/ActiveDeviceDetector.ts

modules/detection/ActiveDeviceDetector.js → modules/detection/ActiveDeviceDetector.ts Näytä tiedosto

1
 import { getLogger } from '@jitsi/logger';
1
 import { getLogger } from '@jitsi/logger';
2
 
2
 
3
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
3
 import * as JitsiTrackEvents from '../../JitsiTrackEvents';
4
+import JitsiLocalTrack from '../RTC/JitsiLocalTrack';
4
 import RTC from '../RTC/RTC';
5
 import RTC from '../RTC/RTC';
5
 import Statistics from '../statistics/statistics';
6
 import Statistics from '../statistics/statistics';
6
 
7
 
8
+export interface IActiveDeviceInfo {
9
+    deviceId: string;
10
+    deviceLabel: string;
11
+}
7
 
12
 
8
 const logger = getLogger('modules/detection/ActiveDeviceDetector');
13
 const logger = getLogger('modules/detection/ActiveDeviceDetector');
9
 
14
 
15
 /**
20
 /**
16
  * Go through all audio devices on the system and return one that is active, i.e. has audio signal.
21
  * Go through all audio devices on the system and return one that is active, i.e. has audio signal.
17
  *
22
  *
18
- * @returns Promise<Object> - Object containing information about the found device.
23
+ * @returns Promise<IActiveDeviceInfo> - Object containing information about the found device.
19
  */
24
  */
20
-export default function getActiveAudioDevice() {
25
+export default function getActiveAudioDevice(): Promise<IActiveDeviceInfo> {
21
 
26
 
22
     return new Promise(resolve => {
27
     return new Promise(resolve => {
23
-        RTC.enumerateDevices(devices => {
28
+        RTC.enumerateDevices((devices: MediaDeviceInfo[]) => {
24
             const audioDevices = devices.filter(device => device.kind === 'audioinput');
29
             const audioDevices = devices.filter(device => device.kind === 'audioinput');
25
-            const devicePromiseArray = [];
30
+            const devicePromiseArray: Promise<JitsiLocalTrack>[] = [];
26
 
31
 
27
 
32
 
28
             for (const micDevice of audioDevices) {
33
             for (const micDevice of audioDevices) {
29
-                const devicePromise = RTC.obtainAudioAndVideoPermissions({ devices: [ 'audio' ],
30
-                    micDeviceId: micDevice.deviceId }).then(tracks => {
34
+                const devicePromise = RTC.obtainAudioAndVideoPermissions({
35
+                    devices: [ 'audio' ],
36
+                    micDeviceId: micDevice.deviceId
37
+                }).then((tracks: JitsiLocalTrack[]) => {
31
 
38
 
32
                     // We expect a single device to be available when obtained from obtainAudioAndVideoPermissions
39
                     // We expect a single device to be available when obtained from obtainAudioAndVideoPermissions
33
                     // that's  why only take p.value[0].
40
                     // that's  why only take p.value[0].
46
                 const rejectedPromises = outcomeArray.filter(p => p.status === 'rejected');
53
                 const rejectedPromises = outcomeArray.filter(p => p.status === 'rejected');
47
 
54
 
48
 
55
 
49
-                const availableDevices = successfulPromises.map(p => p.value);
50
-                const rejectReasons = rejectedPromises.map(p => p.value);
56
+                const availableDevices = successfulPromises.map(p => (p as PromiseFulfilledResult<JitsiLocalTrack>).value);
57
+                const rejectReasons = rejectedPromises.map(p => (p as any).value);
51
 
58
 
52
                 for (const reason of rejectReasons) {
59
                 for (const reason of rejectReasons) {
53
                     logger.error('Failed to acquire audio device with error: ', reason);
60
                     logger.error('Failed to acquire audio device with error: ', reason);
62
                         // no input.
69
                         // no input.
63
                         if (audioLevel > 0.008) {
70
                         if (audioLevel > 0.008) {
64
                             stopActiveDevices(availableDevices);
71
                             stopActiveDevices(availableDevices);
65
-                            resolve({ deviceId: device.deviceId,
66
-                                deviceLabel: device.track.label });
72
+                            resolve({
73
+                                deviceId: device.deviceId,
74
+                                deviceLabel: device.track.label
75
+                            });
67
                         }
76
                         }
68
                     });
77
                     });
69
                 }
78
                 }
73
                     stopActiveDevices(availableDevices);
82
                     stopActiveDevices(availableDevices);
74
                     resolve({
83
                     resolve({
75
                         deviceId: '',
84
                         deviceId: '',
76
-                        deviceLabel: '' }
77
-                    );
85
+                        deviceLabel: ''
86
+                    });
78
                 }, DETECTION_TIMEOUT);
87
                 }, DETECTION_TIMEOUT);
79
 
88
 
80
             });
89
             });
89
  * @param {Array<JitsiLocalTrack>} deviceList - Array of JitsiLocalTracks to stop.
98
  * @param {Array<JitsiLocalTrack>} deviceList - Array of JitsiLocalTracks to stop.
90
  * @returns {void}
99
  * @returns {void}
91
  */
100
  */
92
-function stopActiveDevices(deviceList) {
101
+function stopActiveDevices(deviceList: JitsiLocalTrack[]): void {
93
     for (const device of deviceList) {
102
     for (const device of deviceList) {
94
         device.stopStream();
103
         device.stopStream();
95
     }
104
     }

Loading…
Peruuta
Tallenna