-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathOTSubscriberHelper.js
119 lines (110 loc) · 3.66 KB
/
OTSubscriberHelper.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
import { sanitizeBooleanProperty, reassignEvents } from './OTHelper';
/**
* This is the smallest positive int value for 2 bytes. Using Number.MAX_SAFE_INTEGER at JS level,
* could drive to problems when coverted to the native layer (Android & iOS).
* Since `32767` is a very high value for resolution and frame rate for all use case,
* we won't have any problem for the foreseeable future
*/
const MAX_SAFE_INTEGER = 32767;
const sanitizeSubscriberEvents = (events) => {
if (typeof events !== 'object') {
return {};
}
const customEvents = {
ios: {
connected: 'subscriberDidConnect',
disconnected: 'subscriberDidDisconnect',
reconnected: 'subscriberDidReconnect',
error: 'didFailWithError',
audioNetworkStats: 'audioNetworkStatsUpdated',
videoNetworkStats: 'videoNetworkStatsUpdated',
audioLevel: 'audioLevelUpdated',
videoDisabled: 'subscriberVideoDisabled',
videoEnabled: 'subscriberVideoEnabled',
videoDisableWarning: 'subscriberVideoDisableWarning',
videoDisableWarningLifted: 'subscriberVideoDisableWarningLifted',
videoDataReceived: 'subscriberVideoDataReceived',
},
android: {
connected: 'onConnected',
disconnected: 'onDisconnected',
reconnected: 'onReconnected',
error: 'onError',
audioNetworkStats: 'onAudioStats',
videoNetworkStats: 'onVideoStats',
audioLevel: 'onAudioLevelUpdated',
videoDisabled: 'onVideoDisabled',
videoEnabled: 'onVideoEnabled',
videoDisableWarning: 'onVideoDisableWarning',
videoDisableWarningLifted: 'onVideoDisableWarningLifted',
videoDataReceived: 'onVideoDataReceived',
},
};
return reassignEvents('subscriber', customEvents, events);
};
const sanitizeResolution = (resolution) => {
if ((typeof resolution !== 'object') || (resolution &&
resolution.width === void 0 &&
resolution.height === void 0) ||
(resolution === null)) {
return { width: MAX_SAFE_INTEGER, height: MAX_SAFE_INTEGER };
}
const videoDimensions = {};
if (resolution && resolution.height) {
if (isNaN(parseInt(resolution.height, 10))) {
videoDimensions.height = void 0;
}
videoDimensions.height = parseInt(resolution.height, 10);
} else {
videoDimensions.height = void 0;
}
if (resolution && resolution.width) {
if (isNaN(parseInt(resolution.width, 10))) {
videoDimensions.width = void 0;
}
videoDimensions.width = parseInt(resolution.width, 10);
} else {
videoDimensions.width = void 0;
}
return videoDimensions;
};
const sanitizeFrameRate = (frameRate) => {
switch (frameRate) {
case null:
return MAX_SAFE_INTEGER;
case 1:
return 1;
case 7:
return 7;
case 15:
return 15;
default:
return 30;
}
};
const sanitizeScaleBehavior = (scaleBehavior = 'fill') => (scaleBehavior === 'fill' ? 'fill' : 'fit');
const sanitizeProperties = (properties) => {
if (typeof properties !== 'object') {
return {
scaleBehavior: 'fill',
subscribeToAudio: true,
subscribeToVideo: true,
preferredResolution: sanitizeResolution(null),
preferredFrameRate: sanitizeFrameRate(null)
};
}
return {
scaleBehavior: sanitizeScaleBehavior(properties.scaleBehavior),
subscribeToAudio: sanitizeBooleanProperty(properties.subscribeToAudio),
subscribeToVideo: sanitizeBooleanProperty(properties.subscribeToVideo),
preferredResolution: sanitizeResolution(properties.preferredResolution),
preferredFrameRate: sanitizeFrameRate(properties.preferredFrameRate)
};
};
export {
sanitizeScaleBehavior,
sanitizeSubscriberEvents,
sanitizeProperties,
sanitizeFrameRate,
sanitizeResolution
};