Commit cf6c1203 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Better typing for filters, allow filters to skip frames.

parent a5b57976
...@@ -779,12 +779,14 @@ async function setMaxVideoThroughput(c, bps) { ...@@ -779,12 +779,14 @@ async function setMaxVideoThroughput(c, bps) {
} }
/** /**
* @param {MediaStream} stream
* @param {(this: Filter, src: CanvasImageSource, dest: HTMLCanvasElement) => boolean} f
* @constructor * @constructor
*/ */
function Filter(stream, f) { function Filter(stream, f) {
/** @type {MediaStream} */ /** @type {MediaStream} */
this.inputStream = stream; this.inputStream = stream;
/** @type {(this: Filter, src: HTMLElement, dest: HTMLCanvasElement) => void} */ /** @type {(this: Filter, src: CanvasImageSource, dest: HTMLCanvasElement) => boolean} */
this.f = f; this.f = f;
/** @type {number} */ /** @type {number} */
this.frameRate = 30; this.frameRate = 30;
...@@ -801,7 +803,6 @@ function Filter(stream, f) { ...@@ -801,7 +803,6 @@ function Filter(stream, f) {
/** @type {number} */ /** @type {number} */
this.count = 0; 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);
...@@ -838,9 +839,11 @@ Filter.prototype.draw = function() { ...@@ -838,9 +839,11 @@ Filter.prototype.draw = function() {
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); let ok = this.f.call(this, this.video, this.canvas);
/** @ts-ignore */ if(ok) {
this.captureStream.getTracks()[0].requestFrame(); /** @ts-ignore */
this.captureStream.getTracks()[0].requestFrame();
}
}; };
Filter.prototype.stop = function() { Filter.prototype.stop = function() {
...@@ -851,16 +854,21 @@ Filter.prototype.stop = function() { ...@@ -851,16 +854,21 @@ Filter.prototype.stop = function() {
this.timer = null; this.timer = null;
}; };
/**
* @type {Object.<string, ((this: Filter, src: CanvasImageSource, dest: HTMLCanvasElement) => boolean)>}
*/
let filters = { let filters = {
'mirror-h': (video, canvas) => { 'mirror-h': function(video, canvas) {
let ctx = canvas.getContext('2d'); let ctx = canvas.getContext('2d');
ctx.scale(-1, 1); ctx.scale(-1, 1);
ctx.drawImage(video, -video.videoWidth, 0); ctx.drawImage(video, -canvas.width, 0);
return true;
}, },
'mirror-v': (video, canvas) => { 'mirror-v': function(video, canvas) {
let ctx = canvas.getContext('2d'); let ctx = canvas.getContext('2d');
ctx.scale(1, -1); ctx.scale(1, -1);
ctx.drawImage(video, 0, -video.videoHeight); ctx.drawImage(video, 0, -canvas.height);
return true;
}, },
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment