forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTCUtil.js
35 lines (33 loc) · 1.13 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
'use strict';
/**
* Internal util for deep clone object. Object.assign() only does a shallow copy
*
* @param {Object} obj - object to be cloned
* @return {Object} cloned obj
*/
function _deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
/**
* Merge custom constraints with the default one. The custom one take precedence.
*
* @param {Object} custom - custom webrtc constraints
* @param {Object} def - default webrtc constraints
* @return {Object} constraints - merged webrtc constraints
*/
export function mergeMediaConstraints(custom, def) {
const constraints = (def ? _deepClone(def) : {});
if (custom) {
if (custom.mandatory) {
constraints.mandatory = {...constraints.mandatory, ...custom.mandatory};
}
if (custom.optional && Array.isArray(custom.optional)) {
// `optional` is an array, webrtc only finds first and ignore the rest if duplicate.
constraints.optional = custom.optional.concat(constraints.optional);
}
if (custom.facingMode) {
constraints.facingMode = custom.facingMode.toString(); // string, 'user' or the default 'environment'
}
}
return constraints;
}