Skip to content

Commit 868ed78

Browse files
authored
feat(ts) migrate ActiveDeviceDetector to TS
1 parent dea1a5f commit 868ed78

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

modules/detection/ActiveDeviceDetector.js renamed to modules/detection/ActiveDeviceDetector.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { getLogger } from '@jitsi/logger';
22

33
import * as JitsiTrackEvents from '../../JitsiTrackEvents';
4+
import JitsiLocalTrack from '../RTC/JitsiLocalTrack';
45
import RTC from '../RTC/RTC';
56
import Statistics from '../statistics/statistics';
67

8+
export interface IActiveDeviceInfo {
9+
deviceId: string;
10+
deviceLabel: string;
11+
}
712

813
const logger = getLogger('modules/detection/ActiveDeviceDetector');
914

@@ -15,19 +20,21 @@ const DETECTION_TIMEOUT = 3000;
1520
/**
1621
* Go through all audio devices on the system and return one that is active, i.e. has audio signal.
1722
*
18-
* @returns Promise<Object> - Object containing information about the found device.
23+
* @returns Promise<IActiveDeviceInfo> - Object containing information about the found device.
1924
*/
20-
export default function getActiveAudioDevice() {
25+
export default function getActiveAudioDevice(): Promise<IActiveDeviceInfo> {
2126

2227
return new Promise(resolve => {
23-
RTC.enumerateDevices(devices => {
28+
RTC.enumerateDevices((devices: MediaDeviceInfo[]) => {
2429
const audioDevices = devices.filter(device => device.kind === 'audioinput');
25-
const devicePromiseArray = [];
30+
const devicePromiseArray: Promise<JitsiLocalTrack>[] = [];
2631

2732

2833
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[]) => {
3138

3239
// We expect a single device to be available when obtained from obtainAudioAndVideoPermissions
3340
// that's why only take p.value[0].
@@ -46,8 +53,8 @@ export default function getActiveAudioDevice() {
4653
const rejectedPromises = outcomeArray.filter(p => p.status === 'rejected');
4754

4855

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);
5158

5259
for (const reason of rejectReasons) {
5360
logger.error('Failed to acquire audio device with error: ', reason);
@@ -62,8 +69,10 @@ export default function getActiveAudioDevice() {
6269
// no input.
6370
if (audioLevel > 0.008) {
6471
stopActiveDevices(availableDevices);
65-
resolve({ deviceId: device.deviceId,
66-
deviceLabel: device.track.label });
72+
resolve({
73+
deviceId: device.deviceId,
74+
deviceLabel: device.track.label
75+
});
6776
}
6877
});
6978
}
@@ -73,8 +82,8 @@ export default function getActiveAudioDevice() {
7382
stopActiveDevices(availableDevices);
7483
resolve({
7584
deviceId: '',
76-
deviceLabel: '' }
77-
);
85+
deviceLabel: ''
86+
});
7887
}, DETECTION_TIMEOUT);
7988

8089
});
@@ -89,7 +98,7 @@ export default function getActiveAudioDevice() {
8998
* @param {Array<JitsiLocalTrack>} deviceList - Array of JitsiLocalTracks to stop.
9099
* @returns {void}
91100
*/
92-
function stopActiveDevices(deviceList) {
101+
function stopActiveDevices(deviceList: JitsiLocalTrack[]): void {
93102
for (const device of deviceList) {
94103
device.stopStream();
95104
}

0 commit comments

Comments
 (0)