-
Notifications
You must be signed in to change notification settings - Fork 2
/
MediaDevices.ts
47 lines (44 loc) · 1.69 KB
/
MediaDevices.ts
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
import { MediaDevice } from "@observertc/schemas"
export type MediaDeviceKind = "videoinput" | "audioinput" | "audiooutput";
const EMPTY_ARRAY: MediaDevice[] = [];
export class MediaDevices {
private _mediaDevices: Map<string, MediaDevice> = new Map();
private _indexes: Map<string, string[]> = new Map();
public add(mediaDevice: MediaDevice): void {
if (!mediaDevice.id) return;
const mediaDeviceId = mediaDevice.id;
this._mediaDevices.set(mediaDeviceId, mediaDevice);
if (!mediaDevice.kind) {
return;
}
const index = mediaDevice.kind;
const keys = this._indexes.get(index) || [];
keys.push(mediaDeviceId);
this._indexes.set(index, keys);
}
public remove(mediaDeviceId: string): void {
const mediaDevice = this._mediaDevices.get(mediaDeviceId);
if (!mediaDevice) return;
this._mediaDevices.delete(mediaDeviceId);
if (!mediaDevice.kind) return;
const keys = this._indexes.get(mediaDevice.kind);
if (!keys) return;
const newKeys = keys.filter(key => key !== mediaDeviceId);
this._indexes.set(mediaDevice.kind, newKeys);
}
public values(index?: MediaDeviceKind): IterableIterator<MediaDevice> {
if (!index) {
return this._mediaDevices.values();
}
const keys = this._indexes.get(index);
if (!keys) {
return EMPTY_ARRAY.values();
}
const mediaDevices: MediaDevice[] = [];
for (const key of keys) {
const mediaDevice = this._mediaDevices.get(key);
if (mediaDevice) mediaDevices.push(mediaDevice);
}
return mediaDevices.values();
}
}