-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCUtil.js
180 lines (155 loc) · 5.18 KB
/
RTCUtil.js
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
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
const DEFAULT_AUDIO_CONSTRAINTS = {};
const DEFAULT_VIDEO_CONSTRAINTS = {
facingMode: 'user',
frameRate: 30,
height: 720,
width: 1280
};
const ASPECT_RATIO = 16 / 9;
const STANDARD_OA_OPTIONS = {
icerestart: 'IceRestart',
offertoreceiveaudio: 'OfferToReceiveAudio',
offertoreceivevideo: 'OfferToReceiveVideo',
voiceactivitydetection: 'VoiceActivityDetection'
};
function getDefaultMediaConstraints(mediaType) {
switch(mediaType) {
case 'audio':
return DEFAULT_AUDIO_CONSTRAINTS;
case 'video':
return DEFAULT_VIDEO_CONSTRAINTS;
default:
throw new TypeError(`Invalid media type: ${mediaType}`);
}
}
function extractString(constraints, prop) {
const value = constraints[prop];
const type = typeof value;
if (type === 'object') {
for (const v of [ 'exact', 'ideal' ]) {
if (value[v]) {
return value[v];
}
}
} else if (type === 'string') {
return value;
}
}
function extractNumber(constraints, prop) {
const value = constraints[prop];
const type = typeof value;
if (type === 'number') {
return Number.parseInt(value);
} else if (type === 'object') {
for (const v of [ 'exact', 'ideal', 'min', 'max' ]) {
if (value[v]) {
return Number.parseInt(value[v]);
}
}
}
}
function normalizeMediaConstraints(constraints, mediaType) {
switch(mediaType) {
case 'audio':
return constraints;
case 'video': {
let c;
if (constraints.mandatory) {
// Old style.
c = {
deviceId: extractString(constraints.optional || {}, 'sourceId'),
facingMode: extractString(constraints, 'facingMode'),
frameRate: extractNumber(constraints.mandatory, 'minFrameRate'),
height: extractNumber(constraints.mandatory, 'minHeight'),
width: extractNumber(constraints.mandatory, 'minWidth')
};
} else {
// New style.
c = {
deviceId: extractString(constraints, 'deviceId'),
facingMode: extractString(constraints, 'facingMode'),
frameRate: extractNumber(constraints, 'frameRate'),
height: extractNumber(constraints, 'height'),
width: extractNumber(constraints, 'width')
};
}
if (!c.deviceId) {
delete c.deviceId;
}
if (!c.facingMode || (c.facingMode !== 'user' && c.facingMode !== 'environment')) {
c.facingMode = DEFAULT_VIDEO_CONSTRAINTS.facingMode;
}
if (!c.frameRate) {
c.frameRate = DEFAULT_VIDEO_CONSTRAINTS.frameRate;
}
if (!c.height && !c.width) {
c.height = DEFAULT_VIDEO_CONSTRAINTS.height;
c.width = DEFAULT_VIDEO_CONSTRAINTS.width;
} else if (!c.height) {
c.height = Math.round(c.width / ASPECT_RATIO);
} else if (!c.width) {
c.width = Math.round(c.height * ASPECT_RATIO);
}
return c;
}
default:
throw new TypeError(`Invalid media type: ${mediaType}`);
}
}
/**
* Utility for deep cloning an object. Object.assign() only does a shallow copy.
*
* @param {Object} obj - object to be cloned
* @return {Object} cloned obj
*/
export function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Normalize options passed to createOffer() / createAnswer().
*
* @param {Object} options - user supplied options
* @return {Object} newOptions - normalized options
*/
export function normalizeOfferAnswerOptions(options = {}) {
const newOptions = {};
if (!options) {
return newOptions;
}
// Support legacy constraints.
if (options.mandatory) {
options = options.mandatory;
}
// Convert standard options into WebRTC internal constant names.
// See: https://github.com/jitsi/webrtc/blob/0cd6ce4de669bed94ba47b88cb71b9be0341bb81/sdk/media_constraints.cc#L113
for (const [ key, value ] of Object.entries(options)) {
const newKey = STANDARD_OA_OPTIONS[key.toLowerCase()];
if (newKey) {
newOptions[newKey] = String(Boolean(value));
}
}
return newOptions;
}
/**
* Normalize the given constraints in something we can work with.
*/
export function normalizeConstraints(constraints) {
const c = deepClone(constraints);
for (const mediaType of [ 'audio', 'video' ]) {
const mediaTypeConstraints = c[mediaType];
const typeofMediaTypeConstraints = typeof mediaTypeConstraints;
if (typeofMediaTypeConstraints !== 'undefined') {
if (typeofMediaTypeConstraints === 'boolean') {
if (mediaTypeConstraints) {
c[mediaType] = getDefaultMediaConstraints(mediaType);
}
} else if (typeofMediaTypeConstraints === 'object') {
c[mediaType] = normalizeMediaConstraints(mediaTypeConstraints, mediaType);
} else {
throw new TypeError(`constraints.${mediaType} is neither a boolean nor a dictionary`);
}
}
}
return c;
}