-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
UserMediaManager.ts
168 lines (141 loc) · 4.72 KB
/
UserMediaManager.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* eslint-env browser */
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
export const userMediaRequested: boolean = false;
export const mountedInstances: any[] = [];
async function requestLegacyUserMediaAsync(props): Promise<any[]> {
const optionalSource = id => ({ optional: [{ sourceId: id }] });
const constraintToSourceId = constraint => {
const { deviceId } = constraint;
if (typeof deviceId === 'string') {
return deviceId;
}
if (Array.isArray(deviceId) && deviceId.length > 0) {
return deviceId[0];
}
if (typeof deviceId === 'object' && deviceId.ideal) {
return deviceId.ideal;
}
return null;
};
const sources: any[] = await new Promise(resolve =>
// @ts-ignore: https://caniuse.com/#search=getSources Chrome for Android (78) & Samsung Internet (10.1) use this
MediaStreamTrack.getSources(sources => resolve(sources))
);
let audioSource = null;
let videoSource = null;
sources.forEach(source => {
if (source.kind === 'audio') {
audioSource = source.id;
} else if (source.kind === 'video') {
videoSource = source.id;
}
});
const audioSourceId = constraintToSourceId(props.audioConstraints);
if (audioSourceId) {
audioSource = audioSourceId;
}
const videoSourceId = constraintToSourceId(props.videoConstraints);
if (videoSourceId) {
videoSource = videoSourceId;
}
return [optionalSource(audioSource), optionalSource(videoSource)];
}
async function sourceSelectedAsync(
isMuted: boolean,
audioConstraints?: MediaTrackConstraints | boolean,
videoConstraints?: MediaTrackConstraints | boolean
): Promise<MediaStream> {
const constraints: MediaStreamConstraints = {
video: typeof videoConstraints !== 'undefined' ? videoConstraints : true,
};
if (!isMuted) {
constraints.audio = typeof audioConstraints !== 'undefined' ? audioConstraints : true;
}
return await getAnyUserMediaAsync(constraints);
}
export async function requestUserMediaAsync(
props: { audio?: any; video?: any },
isMuted: boolean = true
): Promise<MediaStream> {
if (canGetUserMedia()) {
return await sourceSelectedAsync(isMuted, props.audio, props.video);
}
const [audio, video] = await requestLegacyUserMediaAsync(props);
return await sourceSelectedAsync(isMuted, audio, video);
}
export async function getAnyUserMediaAsync(
constraints: MediaStreamConstraints,
ignoreConstraints: boolean = false
): Promise<MediaStream> {
try {
return await getUserMediaAsync({
...constraints,
video: ignoreConstraints || constraints.video,
});
} catch (error) {
if (!ignoreConstraints && error.name === 'ConstraintNotSatisfiedError') {
return await getAnyUserMediaAsync(constraints, true);
}
throw error;
}
}
export async function getUserMediaAsync(constraints: MediaStreamConstraints): Promise<MediaStream> {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
return navigator.mediaDevices.getUserMedia(constraints);
}
const _getUserMedia =
navigator['mozGetUserMedia'] || navigator['webkitGetUserMedia'] || navigator['msGetUserMedia'];
return new Promise((resolve, reject) =>
_getUserMedia.call(navigator, constraints, resolve, reject)
);
}
export function canGetUserMedia(): boolean {
return (
// SSR
canUseDOM &&
// Has any form of media API
!!(
(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) ||
navigator['mozGetUserMedia'] ||
navigator['webkitGetUserMedia'] ||
navigator['msGetUserMedia']
)
);
}
export async function isFrontCameraAvailableAsync(
devices?: MediaDeviceInfo[]
): Promise<null | string> {
return await supportsCameraType(['front', 'user', 'facetime'], 'user', devices);
}
export async function isBackCameraAvailableAsync(
devices?: MediaDeviceInfo[]
): Promise<null | string> {
return await supportsCameraType(['back', 'rear'], 'environment', devices);
}
async function supportsCameraType(
labels: string[],
type: string,
devices?: MediaDeviceInfo[]
): Promise<null | string> {
if (!devices) {
if (!navigator.mediaDevices.enumerateDevices) {
return null;
}
devices = await navigator.mediaDevices.enumerateDevices();
}
const cameras = devices.filter(t => t.kind === 'videoinput');
const [hasCamera] = cameras.filter(camera =>
labels.some(label => camera.label.toLowerCase().includes(label))
);
const [isCapable] = cameras.filter(camera => {
if (!('getCapabilities' in camera)) {
return null;
}
const capabilities = (camera as any).getCapabilities();
if (!capabilities.facingMode) {
return null;
}
return capabilities.facingMode.find((_: string) => type);
});
return isCapable?.deviceId || hasCamera?.deviceId || null;
}