-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js.html
More file actions
320 lines (209 loc) · 9.68 KB
/
index.js.html
File metadata and controls
320 lines (209 loc) · 9.68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: index.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>import VirtualBackgroundWorker from "worker-loader!./offscreen.worker.js";
/** The primary interface for creating virtual Background filters. */
class VirtualBackgroundFilter {
/**
* @description BackgroundFilter initialization
*
* @param {MediaStream} input Input to the BackgroundFilter; A MediaStream object from getUserMedia
* @param {object} params
* @param {string | HTMLImageElement | HTMLCanvasElement | ImageBitmap | ImageData } [params.background] -
* For Virtual Background Images, provide any type of Image source supported by [createImageBitmap](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap)
* <br/>To disable backgrounds without changing the stream, set the background to "none"
* @param {number} [params.frameRate=20] - Framerate used for running the virtual background filter
* @param {number} [params.width] - Width used for rendering the virtual background. Defaults to the MediaStreamTrack width
* @param {number} [params.height] - Height used for rendering the virtual background. Defaults to the MediaStreamTrack height
*/
constructor(input, params) {
this.input = input;
const renderSize = this.getVideoDimensions(input);
this.ended = false;
this.renderCanvas = this.createCanvas(renderSize);
this.renderContext = this.renderCanvas.getContext('bitmaprenderer');
this.frameRate = params.frameRate || 20;
this.initialized = this.createWorker(params, renderSize);
}
/**
* @description Worker initializalization
*
* @param {object} params
* @param {string | HTMLImageElement | HTMLCanvasElement | ImageBitmap | ImageData } [params.background] -
* For Virtual Background Images, provide any type of Image source supported by [createImageBitmap](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap)
* <br/>To disable backgrounds without changing the stream, set the background to "none"
* @param {number} [params.frameRate=20] - Framerate used for running the virtual background filter
* @param {object} size
* @param {number} [size.width] - Width used for rendering the virtual background. Defaults to the MediaStreamTrack width
* @param {number} [size.height] - Height used for rendering the virtual background. Defaults to the MediaStreamTrack height
* @inner
*
*/
async createWorker(params, size) {
const worker = new VirtualBackgroundWorker();
const originalCanvas = this.createCanvas(size);
const offScreenCanvas = originalCanvas.transferControlToOffscreen();
this.originalCanvas =originalCanvas;
const background = await createImageBitmap(params.background);
worker.postMessage({msg: 'init', offScreenCanvas, background, width: size.width, height: size.height}, [offScreenCanvas]);
await new Promise(function (resolve) {
worker.addEventListener('message', function (e){
if(e.data.msg === "initialized"){
resolve();
}
});
});
this.worker = worker;
return true;
}
/**
* @description Utility function for creating new Canvases (both the rendering canvas and the offscreen canvas)
*
* @param {object} size
* @param {number} [size.width] - Width used for rendering the virtual background. Defaults to the MediaStreamTrack width
* @param {number} [size.height] - Height used for rendering the virtual background. Defaults to the MediaStreamTrack height
* @inner
*
*/
createCanvas(size){
const newCanvas = document.createElement('canvas');
newCanvas.height = size.height;
newCanvas.width = size.width;
newCanvas.style.display = "none";
newCanvas.style.background = "black";
document.body.appendChild(newCanvas);
return newCanvas
}
/**
* @description Initiate the render loop by sending a "render" message to the worker. When a "Rendered" message is recieved, the returned bitmap is rendered onto the main-thread rendering canvas and the next render step is scheduled
* @inner
*/
initRenderLoop(){
const video = document.createElement('video');
video.srcObject = this.input;
document.body.appendChild(video);
video.play();
video.style.display = "none";
this.video = video;
const ctx = this.renderContext;
const filter = this;
if(video.readyState < 1) video.oncanplay = ()=> {filter.render()};
else filter.render();
this.worker.addEventListener('message', function (e){
switch (e.data.msg){
case "rendered":
filter.lastRenderTime = Date.now();
ctx.transferFromImageBitmap(e.data.bitmap);
e.data.bitmap.close();
filter.scheduleNextRender();
break;
}
});
}
/**
* @description Internal method for scheduling the next render, throttling the render rate to the specified framerate, as specified in the initial params. If the render cycle is not throttled, it could lead to arbitrarily high framerates (100+), causing high cpu usage and poor performance.
* @inner
*/
scheduleNextRender(){
let debounceTime = Math.round(1000/this.frameRate) - (Date.now() - this.lastRenderTime);
const filter = this;
if(debounceTime > 0) setTimeout(function (){
filter.render();
}, debounceTime);
else filter.render();
}
/**
* @description Internal method for the render step. It takes an image bitmap from the current video source, and sends it to the worker via the render command.
* @inner
*/
async render(){
if(this.ended) return;
const source = this.video;
const bitmap = await createImageBitmap(source);
this.worker.postMessage({msg: 'render',bitmap}, [bitmap]);
bitmap.close();
}
/**
* @description Change the background
* @param {string | HTMLImageElement | HTMLCanvasElement | ImageBitmap | ImageData } [background] -
* For Virtual Background Images, provide any type of Image source supported by [createImageBitmap](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/createImageBitmap)
* <br/>To disable backgrounds without changing the stream, set the background to "none"
*/
async changeBackground(background){
if(background === 'none') return this.worker.postMessage({msg: 'change-background', background});
background = await createImageBitmap(background);
return this.worker.postMessage({msg: 'change-background', background}, [background]);
}
/**
* @description Get the filtered output video stream, as a MediaStream
* @return MediaStream
*/
async getOutput(){
await this.initialized;
this.initRenderLoop();
this.outStream = this.renderCanvas.captureStream();
return this.outStream;
}
/**
* @description Internal method for calculating the dimensions to be used for processing the video stream. Defaults to the videoTrack settings from the input
* @param {MediaStream} [input] - the Input MediaStream from getUserMedia
* @inner
*/
getVideoDimensions(input){
let width = 640;
let height = 360;
if(input.getVideoTracks().length >0){
const videoTrackSettings = input.getVideoTracks()[0].getSettings();
width = videoTrackSettings.width;
height = videoTrackSettings.height;
}
return {width, height}
}
/**
* @description Shuts down the output MediaStream, stops all processing, terminates the worker and removes all unused resources
*/
async destroy(){
this.ended = true;
this.outStream.getTracks().forEach((track)=> track.stop());
this.renderCanvas.remove();
this.video.remove();
this.originalCanvas.remove();
const worker = this.worker;
worker.terminate();
delete this.worker;
delete this;
}
static isSupported(){
return (typeof HTMLCanvasElement.prototype.transferControlToOffscreen === "function")
}
}
export default VirtualBackgroundFilter</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Pipeline.html">Pipeline</a></li><li><a href="Step.html">Step</a></li><li><a href="TFLiteStep.html">TFLiteStep</a></li><li><a href="VirtualBackgroundFilter.html">VirtualBackgroundFilter</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sat Jan 08 2022 19:43:10 GMT-0800 (Pacific Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>