-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathVideoConstrainer.js
More file actions
350 lines (290 loc) · 8.62 KB
/
Copy pathVideoConstrainer.js
File metadata and controls
350 lines (290 loc) · 8.62 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
const QUALITY = {
THUMBNAIL: 0,
VERY_LOW: 1,
LOW: 2,
MEDIUM: 3,
HIGH: 4,
}
/**
* Helper to adjust the quality of a video stream.
*
* Despite having a common API, different browsers handle media constraints in
* different ways. For example, if a video is constrained to a low maximum
* resolution but then the constraint is relaxed with a higher maximum
* resolution some browsers will increase the resolution, but others will just
* stays with the previous resolution, as it still matches the constraints. In
* other cases, setting a low frame rate may fail, but it may work if given a
* little more room. This class tries to abstract all those details and provide
* a simple interface to set the constraints based on some general quality
* description.
*
* @param {object} trackConstrainer the track constrainer node on which apply
* the constraints.
*/
function VideoConstrainer(trackConstrainer) {
this._trackConstrainer = trackConstrainer
// The current quality is undefined until the constraints are applied at
// least once.
this._currentQuality = undefined
this._knownValidConstraintsForQuality = {}
}
VideoConstrainer.prototype = {
async applyConstraints(quality) {
if (this._pendingApplyConstraintsCount) {
console.debug('Deferring applying constraints for quality ' + quality)
this._pendingApplyConstraintsCount++
this._lastPendingQuality = quality
return
}
this._pendingApplyConstraintsCount = 1
// As "_applyConstraints" is asynchronous even if the current quality is
// the same as the given one the call will not immediately return. Due
// to this, even if the "applyConstraints(quality)" is called several
// times in a row with the current quality the calls will still be
// deferred, but this should not be a problem.
await this._applyConstraints(quality)
this._resetPendingApplyConstraintsCount()
},
_resetPendingApplyConstraintsCount() {
const applyConstraintsAgain = this._pendingApplyConstraintsCount > 1
this._pendingApplyConstraintsCount = 0
if (applyConstraintsAgain) {
this.applyConstraints(this._lastPendingQuality)
}
},
async _applyConstraints(quality) {
if (quality === this._currentQuality) {
return
}
if (!this._trackConstrainer.getOutputTrack() || this._trackConstrainer.getOutputTrack().kind !== 'video') {
console.warn('No video track to adjust its quality found')
return
}
await this._applyRoughConstraints(this._trackConstrainer, quality)
// Quality may not actually match the default constraints, but it is the
// best that can be done.
this._currentQuality = quality
this._logAppliedSettings(quality)
},
_logAppliedSettings(quality) {
const outputTrack = this._trackConstrainer.getOutputTrack()
if (!outputTrack || !outputTrack.getSettings) {
return
}
const { width, height, frameRate } = outputTrack.getSettings()
console.debug('Camera picked resolution for quality %d: %dx%d @ %s fps', quality, width, height, frameRate)
},
async _applyRoughConstraints(trackConstrainer, quality) {
let constraints = this._knownValidConstraintsForQuality[quality]
if (!constraints) {
constraints = this._getConstraintsForQuality(quality)
}
try {
await trackConstrainer.applyConstraints(constraints)
this._knownValidConstraintsForQuality[quality] = constraints
console.debug('Changed quality to %d', quality)
} catch (error) {
console.warn('Failed to set quality %d', quality, error)
const resolutionConstraints = {
width: constraints.width,
height: constraints.height,
}
await this._applyRoughResolutionConstraints(trackConstrainer, resolutionConstraints)
const frameRateConstraints = {
width: constraints.width,
height: constraints.height,
frameRate: constraints.frameRate,
}
try {
await this._applyRoughFrameRateConstraints(trackConstrainer, frameRateConstraints)
this._knownValidConstraintsForQuality[quality] = frameRateConstraints
} catch (error) {
// Frame rate could not be changed, but at least resolution
// was. Do not fail in that case and settle for this little
// victory.
this._knownValidConstraintsForQuality[quality] = resolutionConstraints
}
console.debug('Changed quality to ' + quality)
}
},
async _applyRoughResolutionConstraints(trackConstrainer, constraints) {
try {
await trackConstrainer.applyConstraints(constraints)
console.debug('Changed resolution', constraints)
} catch (error) {
console.warn('Failed to set resolution', constraints, error)
if (!this._increaseMaxResolution(constraints) && !this._decreaseMinResolution(constraints)) {
console.warn('Resolution range can not be further increased')
throw error
}
this._applyRoughResolutionConstraints(trackConstrainer, constraints)
}
},
async _applyRoughFrameRateConstraints(trackConstrainer, constraints) {
try {
await trackConstrainer.applyConstraints(constraints)
console.debug('Changed frame rate', constraints)
} catch (error) {
console.warn('Failed to set frame rate', constraints, error)
if (!this._increaseMaxFrameRate(constraints) && !this._decreaseMinFrameRate(constraints)) {
console.warn('Frame rate range can not be further increased')
throw error
}
this._applyRoughFrameRateConstraints(trackConstrainer, constraints)
}
},
_getConstraintsForQuality(quality) {
if (quality === QUALITY.HIGH) {
return {
width: {
max: 1920,
ideal: 1920,
min: 1280,
},
height: {
max: 1080,
ideal: 1080,
min: 720,
},
frameRate: {
max: 30,
ideal: 30,
min: 20,
},
resizeMode: 'none',
}
}
if (quality === QUALITY.MEDIUM) {
return {
width: {
max: 1280,
ideal: 1280,
min: 640,
},
height: {
max: 720,
ideal: 720,
min: 480,
},
frameRate: {
max: 30,
ideal: 30,
min: 20,
},
resizeMode: 'none',
}
}
if (quality === QUALITY.LOW) {
return {
width: {
max: 640,
ideal: 560,
min: 480,
},
height: {
max: 480,
ideal: 420,
min: 320,
},
frameRate: {
max: 30,
ideal: 30,
min: 20,
},
resizeMode: 'none',
}
}
if (quality === QUALITY.VERY_LOW) {
return {
width: {
max: 480,
ideal: 360,
min: 320,
},
height: {
max: 320,
ideal: 270,
min: 240,
},
frameRate: {
max: 30,
ideal: 30,
min: 20,
},
resizeMode: 'none',
}
}
return {
width: {
max: 320,
ideal: 240,
min: 160,
},
height: {
max: 240,
ideal: 180,
min: 120,
},
frameRate: {
max: 30,
ideal: 30,
min: 20,
},
resizeMode: 'none',
}
},
_increaseMaxResolution(constraints) {
let changed = false
if (constraints.width && constraints.width.max) {
const previousWidthMax = constraints.width.max
constraints.width.max = Math.min(Math.round(constraints.width.max * 1.5), 1920)
changed = previousWidthMax !== constraints.width.max
}
if (constraints.height && constraints.height.max) {
const previousHeightMax = constraints.height.max
constraints.height.max = Math.min(Math.round(constraints.height.max * 1.5), 1080)
changed = previousHeightMax !== constraints.height.max
}
return changed
},
_decreaseMinResolution(constraints) {
let changed = false
if (constraints.width && constraints.width.min) {
const previousWidthMin = constraints.width.min
constraints.width.min = Math.max(Math.round(constraints.width.min / 1.5), 64)
changed = previousWidthMin !== constraints.width.min
}
if (constraints.height && constraints.height.min) {
const previousHeightMin = constraints.height.min
constraints.height.min = Math.max(Math.round(constraints.height.min / 1.5), 64)
changed = previousHeightMin !== constraints.height.min
}
return changed
},
_increaseMaxFrameRate(constraints) {
let changed = false
if (constraints.frameRate && constraints.frameRate.max) {
const previousFrameRateMax = constraints.frameRate.max
constraints.frameRate.max = Math.min(Math.round(constraints.frameRate.max * 1.5), 60)
changed = previousFrameRateMax !== constraints.frameRate.max
}
return changed
},
_decreaseMinFrameRate(constraints) {
let changed = false
if (constraints.frameRate && constraints.frameRate.min) {
const previousFrameRateMin = constraints.frameRate.min
constraints.frameRate.min = Math.max(Math.round(constraints.frameRate.min / 1.5), 1)
changed = previousFrameRateMin !== constraints.frameRate.min
}
return changed
},
}
export {
QUALITY,
VideoConstrainer,
}