31 lines
1.6 KiB
JavaScript
31 lines
1.6 KiB
JavaScript
(function(root) {
|
|
class QualidadeAuto {
|
|
constructor(fps = 30, bitrate = 2500000) { this.level = 0; this.good = 0; this.fps = fps; this.bitrate = bitrate; }
|
|
static constraints(c, caps = {}) {
|
|
const v = {facingMode:{exact:c.cameraLado || 'user'}};
|
|
const res = c.videoResolucao || 'auto', fps = Number(c.videoFPS || 0);
|
|
if (res === 'auto') { v.width = {ideal:1920}; v.height = {ideal:1080}; }
|
|
else if (res === 'max') {
|
|
v.width = {ideal:caps.width?.max || 7680}; v.height = {ideal:caps.height?.max || 4320};
|
|
} else {
|
|
const h = Number(res); v.height = {exact:h}; v.width = {exact: h === 480 ? 640 : Math.round(h*16/9)};
|
|
}
|
|
v.frameRate = fps === -1 ? {ideal:caps.frameRate?.max || 240} : fps > 0 ? {exact:fps} : {ideal:30};
|
|
return v;
|
|
}
|
|
update(s) {
|
|
const bad = (Number.isFinite(s.fps) && s.fps > 0 && s.fps < this.fps*.73) || s.rtt > .3 || s.bitrate < [this.bitrate*.48, this.bitrate*.26, 0][this.level];
|
|
if (bad) { this.level = Math.min(2, this.level + 1); this.good = 0; }
|
|
else if (s.fps >= this.fps*.9 && s.rtt < .15 && s.bitrate > this.bitrate*.72) {
|
|
if (++this.good >= 4) { this.level = Math.max(0, this.level - 1); this.good = 0; }
|
|
} else this.good = 0;
|
|
return this.parameters();
|
|
}
|
|
parameters() {
|
|
return {maxFramerate:this.fps, maxBitrate:Math.round(this.bitrate*[1,.52,.24][this.level]), scaleResolutionDownBy:[1,1.5,2][this.level]};
|
|
}
|
|
}
|
|
root.QualidadeAuto = QualidadeAuto;
|
|
if (typeof module !== 'undefined') module.exports = QualidadeAuto;
|
|
})(typeof globalThis !== 'undefined' ? globalThis : window);
|