1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-26 10:35:59 +01:00

Lock filter framerate to webcam framerate.

This commit is contained in:
Juliusz Chroboczek 2021-01-12 03:36:51 +01:00
parent ca6020a27e
commit a5b57976d2

View file

@ -783,9 +783,11 @@ async function setMaxVideoThroughput(c, bps) {
*/ */
function Filter(stream, f) { function Filter(stream, f) {
/** @type {MediaStream} */ /** @type {MediaStream} */
this.input = stream; this.inputStream = stream;
/** @type {(this: Filter, src: HTMLElement, dest: HTMLCanvasElement) => void} */ /** @type {(this: Filter, src: HTMLElement, dest: HTMLCanvasElement) => void} */
this.f = f; this.f = f;
/** @type {number} */
this.frameRate = 30;
/** @type {HTMLVideoElement} */ /** @type {HTMLVideoElement} */
this.video = document.createElement('video'); this.video = document.createElement('video');
/** @type {HTMLCanvasElement} */ /** @type {HTMLCanvasElement} */
@ -796,13 +798,16 @@ function Filter(stream, f) {
this.outputStream = null; this.outputStream = null;
/** @type {number} */ /** @type {number} */
this.timer = null; this.timer = null;
/** @type {number} */
this.count = 0;
//this.input.getTracks().forEach(t => console.log(t.getSettings()));
/** @ts-ignore */ /** @ts-ignore */
this.captureStream = this.canvas.captureStream(0); this.captureStream = this.canvas.captureStream(0);
this.outputStream = new MediaStream(); this.outputStream = new MediaStream();
this.outputStream.addTrack(this.captureStream.getTracks()[0]); this.outputStream.addTrack(this.captureStream.getTracks()[0]);
this.input.getTracks().forEach(t => { this.inputStream.getTracks().forEach(t => {
t.onended = e => this.stop(); t.onended = e => this.stop();
if(t.kind != 'video') if(t.kind != 'video')
this.outputStream.addTrack(t); this.outputStream.addTrack(t);
@ -810,10 +815,27 @@ function Filter(stream, f) {
this.video.srcObject = stream; this.video.srcObject = stream;
this.video.muted = true; this.video.muted = true;
this.video.play(); this.video.play();
this.timer = setInterval(() => this.draw(), 1000 / 30); this.timer = setInterval(() => this.draw(), 1000 / this.frameRate);
} }
Filter.prototype.draw = function() { Filter.prototype.draw = function() {
// check framerate evecry 30 frames
if((this.count % 30) === 0) {
let frameRate = 0;
this.inputStream.getTracks().forEach(t => {
if(t.kind === 'video') {
let r = t.getSettings().frameRate;
if(r)
frameRate = r;
}
});
if(frameRate && frameRate != this.frameRate) {
clearTimeout(this.timer);
this.timer = setInterval(() => this.draw(), 1000 / this.frameRate);
}
}
this.count++;
this.canvas.width = this.video.videoWidth; this.canvas.width = this.video.videoWidth;
this.canvas.height = this.video.videoHeight; this.canvas.height = this.video.videoHeight;
this.f(this.video, this.canvas); this.f(this.video, this.canvas);