@100mslive/hms-virtual-background 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1606 +0,0 @@
1
- import '@tensorflow/tfjs-backend-webgl';
2
- import { parseGIF, decompressFrames } from 'gifuct-js';
3
- import { HMSVideoPluginType } from '@100mslive/hms-video';
4
-
5
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
6
- try {
7
- var info = gen[key](arg);
8
- var value = info.value;
9
- } catch (error) {
10
- reject(error);
11
- return;
12
- }
13
-
14
- if (info.done) {
15
- resolve(value);
16
- } else {
17
- Promise.resolve(value).then(_next, _throw);
18
- }
19
- }
20
-
21
- function _asyncToGenerator(fn) {
22
- return function () {
23
- var self = this,
24
- args = arguments;
25
- return new Promise(function (resolve, reject) {
26
- var gen = fn.apply(self, args);
27
-
28
- function _next(value) {
29
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
- }
31
-
32
- function _throw(err) {
33
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
34
- }
35
-
36
- _next(undefined);
37
- });
38
- };
39
- }
40
-
41
- function createCommonjsModule(fn, module) {
42
- return module = { exports: {} }, fn(module, module.exports), module.exports;
43
- }
44
-
45
- var runtime_1 = createCommonjsModule(function (module) {
46
- /**
47
- * Copyright (c) 2014-present, Facebook, Inc.
48
- *
49
- * This source code is licensed under the MIT license found in the
50
- * LICENSE file in the root directory of this source tree.
51
- */
52
-
53
- var runtime = (function (exports) {
54
-
55
- var Op = Object.prototype;
56
- var hasOwn = Op.hasOwnProperty;
57
- var undefined$1; // More compressible than void 0.
58
- var $Symbol = typeof Symbol === "function" ? Symbol : {};
59
- var iteratorSymbol = $Symbol.iterator || "@@iterator";
60
- var asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator";
61
- var toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
62
-
63
- function define(obj, key, value) {
64
- Object.defineProperty(obj, key, {
65
- value: value,
66
- enumerable: true,
67
- configurable: true,
68
- writable: true
69
- });
70
- return obj[key];
71
- }
72
- try {
73
- // IE 8 has a broken Object.defineProperty that only works on DOM objects.
74
- define({}, "");
75
- } catch (err) {
76
- define = function(obj, key, value) {
77
- return obj[key] = value;
78
- };
79
- }
80
-
81
- function wrap(innerFn, outerFn, self, tryLocsList) {
82
- // If outerFn provided and outerFn.prototype is a Generator, then outerFn.prototype instanceof Generator.
83
- var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator;
84
- var generator = Object.create(protoGenerator.prototype);
85
- var context = new Context(tryLocsList || []);
86
-
87
- // The ._invoke method unifies the implementations of the .next,
88
- // .throw, and .return methods.
89
- generator._invoke = makeInvokeMethod(innerFn, self, context);
90
-
91
- return generator;
92
- }
93
- exports.wrap = wrap;
94
-
95
- // Try/catch helper to minimize deoptimizations. Returns a completion
96
- // record like context.tryEntries[i].completion. This interface could
97
- // have been (and was previously) designed to take a closure to be
98
- // invoked without arguments, but in all the cases we care about we
99
- // already have an existing method we want to call, so there's no need
100
- // to create a new function object. We can even get away with assuming
101
- // the method takes exactly one argument, since that happens to be true
102
- // in every case, so we don't have to touch the arguments object. The
103
- // only additional allocation required is the completion record, which
104
- // has a stable shape and so hopefully should be cheap to allocate.
105
- function tryCatch(fn, obj, arg) {
106
- try {
107
- return { type: "normal", arg: fn.call(obj, arg) };
108
- } catch (err) {
109
- return { type: "throw", arg: err };
110
- }
111
- }
112
-
113
- var GenStateSuspendedStart = "suspendedStart";
114
- var GenStateSuspendedYield = "suspendedYield";
115
- var GenStateExecuting = "executing";
116
- var GenStateCompleted = "completed";
117
-
118
- // Returning this object from the innerFn has the same effect as
119
- // breaking out of the dispatch switch statement.
120
- var ContinueSentinel = {};
121
-
122
- // Dummy constructor functions that we use as the .constructor and
123
- // .constructor.prototype properties for functions that return Generator
124
- // objects. For full spec compliance, you may wish to configure your
125
- // minifier not to mangle the names of these two functions.
126
- function Generator() {}
127
- function GeneratorFunction() {}
128
- function GeneratorFunctionPrototype() {}
129
-
130
- // This is a polyfill for %IteratorPrototype% for environments that
131
- // don't natively support it.
132
- var IteratorPrototype = {};
133
- define(IteratorPrototype, iteratorSymbol, function () {
134
- return this;
135
- });
136
-
137
- var getProto = Object.getPrototypeOf;
138
- var NativeIteratorPrototype = getProto && getProto(getProto(values([])));
139
- if (NativeIteratorPrototype &&
140
- NativeIteratorPrototype !== Op &&
141
- hasOwn.call(NativeIteratorPrototype, iteratorSymbol)) {
142
- // This environment has a native %IteratorPrototype%; use it instead
143
- // of the polyfill.
144
- IteratorPrototype = NativeIteratorPrototype;
145
- }
146
-
147
- var Gp = GeneratorFunctionPrototype.prototype =
148
- Generator.prototype = Object.create(IteratorPrototype);
149
- GeneratorFunction.prototype = GeneratorFunctionPrototype;
150
- define(Gp, "constructor", GeneratorFunctionPrototype);
151
- define(GeneratorFunctionPrototype, "constructor", GeneratorFunction);
152
- GeneratorFunction.displayName = define(
153
- GeneratorFunctionPrototype,
154
- toStringTagSymbol,
155
- "GeneratorFunction"
156
- );
157
-
158
- // Helper for defining the .next, .throw, and .return methods of the
159
- // Iterator interface in terms of a single ._invoke method.
160
- function defineIteratorMethods(prototype) {
161
- ["next", "throw", "return"].forEach(function(method) {
162
- define(prototype, method, function(arg) {
163
- return this._invoke(method, arg);
164
- });
165
- });
166
- }
167
-
168
- exports.isGeneratorFunction = function(genFun) {
169
- var ctor = typeof genFun === "function" && genFun.constructor;
170
- return ctor
171
- ? ctor === GeneratorFunction ||
172
- // For the native GeneratorFunction constructor, the best we can
173
- // do is to check its .name property.
174
- (ctor.displayName || ctor.name) === "GeneratorFunction"
175
- : false;
176
- };
177
-
178
- exports.mark = function(genFun) {
179
- if (Object.setPrototypeOf) {
180
- Object.setPrototypeOf(genFun, GeneratorFunctionPrototype);
181
- } else {
182
- genFun.__proto__ = GeneratorFunctionPrototype;
183
- define(genFun, toStringTagSymbol, "GeneratorFunction");
184
- }
185
- genFun.prototype = Object.create(Gp);
186
- return genFun;
187
- };
188
-
189
- // Within the body of any async function, `await x` is transformed to
190
- // `yield regeneratorRuntime.awrap(x)`, so that the runtime can test
191
- // `hasOwn.call(value, "__await")` to determine if the yielded value is
192
- // meant to be awaited.
193
- exports.awrap = function(arg) {
194
- return { __await: arg };
195
- };
196
-
197
- function AsyncIterator(generator, PromiseImpl) {
198
- function invoke(method, arg, resolve, reject) {
199
- var record = tryCatch(generator[method], generator, arg);
200
- if (record.type === "throw") {
201
- reject(record.arg);
202
- } else {
203
- var result = record.arg;
204
- var value = result.value;
205
- if (value &&
206
- typeof value === "object" &&
207
- hasOwn.call(value, "__await")) {
208
- return PromiseImpl.resolve(value.__await).then(function(value) {
209
- invoke("next", value, resolve, reject);
210
- }, function(err) {
211
- invoke("throw", err, resolve, reject);
212
- });
213
- }
214
-
215
- return PromiseImpl.resolve(value).then(function(unwrapped) {
216
- // When a yielded Promise is resolved, its final value becomes
217
- // the .value of the Promise<{value,done}> result for the
218
- // current iteration.
219
- result.value = unwrapped;
220
- resolve(result);
221
- }, function(error) {
222
- // If a rejected Promise was yielded, throw the rejection back
223
- // into the async generator function so it can be handled there.
224
- return invoke("throw", error, resolve, reject);
225
- });
226
- }
227
- }
228
-
229
- var previousPromise;
230
-
231
- function enqueue(method, arg) {
232
- function callInvokeWithMethodAndArg() {
233
- return new PromiseImpl(function(resolve, reject) {
234
- invoke(method, arg, resolve, reject);
235
- });
236
- }
237
-
238
- return previousPromise =
239
- // If enqueue has been called before, then we want to wait until
240
- // all previous Promises have been resolved before calling invoke,
241
- // so that results are always delivered in the correct order. If
242
- // enqueue has not been called before, then it is important to
243
- // call invoke immediately, without waiting on a callback to fire,
244
- // so that the async generator function has the opportunity to do
245
- // any necessary setup in a predictable way. This predictability
246
- // is why the Promise constructor synchronously invokes its
247
- // executor callback, and why async functions synchronously
248
- // execute code before the first await. Since we implement simple
249
- // async functions in terms of async generators, it is especially
250
- // important to get this right, even though it requires care.
251
- previousPromise ? previousPromise.then(
252
- callInvokeWithMethodAndArg,
253
- // Avoid propagating failures to Promises returned by later
254
- // invocations of the iterator.
255
- callInvokeWithMethodAndArg
256
- ) : callInvokeWithMethodAndArg();
257
- }
258
-
259
- // Define the unified helper method that is used to implement .next,
260
- // .throw, and .return (see defineIteratorMethods).
261
- this._invoke = enqueue;
262
- }
263
-
264
- defineIteratorMethods(AsyncIterator.prototype);
265
- define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
266
- return this;
267
- });
268
- exports.AsyncIterator = AsyncIterator;
269
-
270
- // Note that simple async functions are implemented on top of
271
- // AsyncIterator objects; they just return a Promise for the value of
272
- // the final result produced by the iterator.
273
- exports.async = function(innerFn, outerFn, self, tryLocsList, PromiseImpl) {
274
- if (PromiseImpl === void 0) PromiseImpl = Promise;
275
-
276
- var iter = new AsyncIterator(
277
- wrap(innerFn, outerFn, self, tryLocsList),
278
- PromiseImpl
279
- );
280
-
281
- return exports.isGeneratorFunction(outerFn)
282
- ? iter // If outerFn is a generator, return the full iterator.
283
- : iter.next().then(function(result) {
284
- return result.done ? result.value : iter.next();
285
- });
286
- };
287
-
288
- function makeInvokeMethod(innerFn, self, context) {
289
- var state = GenStateSuspendedStart;
290
-
291
- return function invoke(method, arg) {
292
- if (state === GenStateExecuting) {
293
- throw new Error("Generator is already running");
294
- }
295
-
296
- if (state === GenStateCompleted) {
297
- if (method === "throw") {
298
- throw arg;
299
- }
300
-
301
- // Be forgiving, per 25.3.3.3.3 of the spec:
302
- // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorresume
303
- return doneResult();
304
- }
305
-
306
- context.method = method;
307
- context.arg = arg;
308
-
309
- while (true) {
310
- var delegate = context.delegate;
311
- if (delegate) {
312
- var delegateResult = maybeInvokeDelegate(delegate, context);
313
- if (delegateResult) {
314
- if (delegateResult === ContinueSentinel) continue;
315
- return delegateResult;
316
- }
317
- }
318
-
319
- if (context.method === "next") {
320
- // Setting context._sent for legacy support of Babel's
321
- // function.sent implementation.
322
- context.sent = context._sent = context.arg;
323
-
324
- } else if (context.method === "throw") {
325
- if (state === GenStateSuspendedStart) {
326
- state = GenStateCompleted;
327
- throw context.arg;
328
- }
329
-
330
- context.dispatchException(context.arg);
331
-
332
- } else if (context.method === "return") {
333
- context.abrupt("return", context.arg);
334
- }
335
-
336
- state = GenStateExecuting;
337
-
338
- var record = tryCatch(innerFn, self, context);
339
- if (record.type === "normal") {
340
- // If an exception is thrown from innerFn, we leave state ===
341
- // GenStateExecuting and loop back for another invocation.
342
- state = context.done
343
- ? GenStateCompleted
344
- : GenStateSuspendedYield;
345
-
346
- if (record.arg === ContinueSentinel) {
347
- continue;
348
- }
349
-
350
- return {
351
- value: record.arg,
352
- done: context.done
353
- };
354
-
355
- } else if (record.type === "throw") {
356
- state = GenStateCompleted;
357
- // Dispatch the exception by looping back around to the
358
- // context.dispatchException(context.arg) call above.
359
- context.method = "throw";
360
- context.arg = record.arg;
361
- }
362
- }
363
- };
364
- }
365
-
366
- // Call delegate.iterator[context.method](context.arg) and handle the
367
- // result, either by returning a { value, done } result from the
368
- // delegate iterator, or by modifying context.method and context.arg,
369
- // setting context.delegate to null, and returning the ContinueSentinel.
370
- function maybeInvokeDelegate(delegate, context) {
371
- var method = delegate.iterator[context.method];
372
- if (method === undefined$1) {
373
- // A .throw or .return when the delegate iterator has no .throw
374
- // method always terminates the yield* loop.
375
- context.delegate = null;
376
-
377
- if (context.method === "throw") {
378
- // Note: ["return"] must be used for ES3 parsing compatibility.
379
- if (delegate.iterator["return"]) {
380
- // If the delegate iterator has a return method, give it a
381
- // chance to clean up.
382
- context.method = "return";
383
- context.arg = undefined$1;
384
- maybeInvokeDelegate(delegate, context);
385
-
386
- if (context.method === "throw") {
387
- // If maybeInvokeDelegate(context) changed context.method from
388
- // "return" to "throw", let that override the TypeError below.
389
- return ContinueSentinel;
390
- }
391
- }
392
-
393
- context.method = "throw";
394
- context.arg = new TypeError(
395
- "The iterator does not provide a 'throw' method");
396
- }
397
-
398
- return ContinueSentinel;
399
- }
400
-
401
- var record = tryCatch(method, delegate.iterator, context.arg);
402
-
403
- if (record.type === "throw") {
404
- context.method = "throw";
405
- context.arg = record.arg;
406
- context.delegate = null;
407
- return ContinueSentinel;
408
- }
409
-
410
- var info = record.arg;
411
-
412
- if (! info) {
413
- context.method = "throw";
414
- context.arg = new TypeError("iterator result is not an object");
415
- context.delegate = null;
416
- return ContinueSentinel;
417
- }
418
-
419
- if (info.done) {
420
- // Assign the result of the finished delegate to the temporary
421
- // variable specified by delegate.resultName (see delegateYield).
422
- context[delegate.resultName] = info.value;
423
-
424
- // Resume execution at the desired location (see delegateYield).
425
- context.next = delegate.nextLoc;
426
-
427
- // If context.method was "throw" but the delegate handled the
428
- // exception, let the outer generator proceed normally. If
429
- // context.method was "next", forget context.arg since it has been
430
- // "consumed" by the delegate iterator. If context.method was
431
- // "return", allow the original .return call to continue in the
432
- // outer generator.
433
- if (context.method !== "return") {
434
- context.method = "next";
435
- context.arg = undefined$1;
436
- }
437
-
438
- } else {
439
- // Re-yield the result returned by the delegate method.
440
- return info;
441
- }
442
-
443
- // The delegate iterator is finished, so forget it and continue with
444
- // the outer generator.
445
- context.delegate = null;
446
- return ContinueSentinel;
447
- }
448
-
449
- // Define Generator.prototype.{next,throw,return} in terms of the
450
- // unified ._invoke helper method.
451
- defineIteratorMethods(Gp);
452
-
453
- define(Gp, toStringTagSymbol, "Generator");
454
-
455
- // A Generator should always return itself as the iterator object when the
456
- // @@iterator function is called on it. Some browsers' implementations of the
457
- // iterator prototype chain incorrectly implement this, causing the Generator
458
- // object to not be returned from this call. This ensures that doesn't happen.
459
- // See https://github.com/facebook/regenerator/issues/274 for more details.
460
- define(Gp, iteratorSymbol, function() {
461
- return this;
462
- });
463
-
464
- define(Gp, "toString", function() {
465
- return "[object Generator]";
466
- });
467
-
468
- function pushTryEntry(locs) {
469
- var entry = { tryLoc: locs[0] };
470
-
471
- if (1 in locs) {
472
- entry.catchLoc = locs[1];
473
- }
474
-
475
- if (2 in locs) {
476
- entry.finallyLoc = locs[2];
477
- entry.afterLoc = locs[3];
478
- }
479
-
480
- this.tryEntries.push(entry);
481
- }
482
-
483
- function resetTryEntry(entry) {
484
- var record = entry.completion || {};
485
- record.type = "normal";
486
- delete record.arg;
487
- entry.completion = record;
488
- }
489
-
490
- function Context(tryLocsList) {
491
- // The root entry object (effectively a try statement without a catch
492
- // or a finally block) gives us a place to store values thrown from
493
- // locations where there is no enclosing try statement.
494
- this.tryEntries = [{ tryLoc: "root" }];
495
- tryLocsList.forEach(pushTryEntry, this);
496
- this.reset(true);
497
- }
498
-
499
- exports.keys = function(object) {
500
- var keys = [];
501
- for (var key in object) {
502
- keys.push(key);
503
- }
504
- keys.reverse();
505
-
506
- // Rather than returning an object with a next method, we keep
507
- // things simple and return the next function itself.
508
- return function next() {
509
- while (keys.length) {
510
- var key = keys.pop();
511
- if (key in object) {
512
- next.value = key;
513
- next.done = false;
514
- return next;
515
- }
516
- }
517
-
518
- // To avoid creating an additional object, we just hang the .value
519
- // and .done properties off the next function object itself. This
520
- // also ensures that the minifier will not anonymize the function.
521
- next.done = true;
522
- return next;
523
- };
524
- };
525
-
526
- function values(iterable) {
527
- if (iterable) {
528
- var iteratorMethod = iterable[iteratorSymbol];
529
- if (iteratorMethod) {
530
- return iteratorMethod.call(iterable);
531
- }
532
-
533
- if (typeof iterable.next === "function") {
534
- return iterable;
535
- }
536
-
537
- if (!isNaN(iterable.length)) {
538
- var i = -1, next = function next() {
539
- while (++i < iterable.length) {
540
- if (hasOwn.call(iterable, i)) {
541
- next.value = iterable[i];
542
- next.done = false;
543
- return next;
544
- }
545
- }
546
-
547
- next.value = undefined$1;
548
- next.done = true;
549
-
550
- return next;
551
- };
552
-
553
- return next.next = next;
554
- }
555
- }
556
-
557
- // Return an iterator with no values.
558
- return { next: doneResult };
559
- }
560
- exports.values = values;
561
-
562
- function doneResult() {
563
- return { value: undefined$1, done: true };
564
- }
565
-
566
- Context.prototype = {
567
- constructor: Context,
568
-
569
- reset: function(skipTempReset) {
570
- this.prev = 0;
571
- this.next = 0;
572
- // Resetting context._sent for legacy support of Babel's
573
- // function.sent implementation.
574
- this.sent = this._sent = undefined$1;
575
- this.done = false;
576
- this.delegate = null;
577
-
578
- this.method = "next";
579
- this.arg = undefined$1;
580
-
581
- this.tryEntries.forEach(resetTryEntry);
582
-
583
- if (!skipTempReset) {
584
- for (var name in this) {
585
- // Not sure about the optimal order of these conditions:
586
- if (name.charAt(0) === "t" &&
587
- hasOwn.call(this, name) &&
588
- !isNaN(+name.slice(1))) {
589
- this[name] = undefined$1;
590
- }
591
- }
592
- }
593
- },
594
-
595
- stop: function() {
596
- this.done = true;
597
-
598
- var rootEntry = this.tryEntries[0];
599
- var rootRecord = rootEntry.completion;
600
- if (rootRecord.type === "throw") {
601
- throw rootRecord.arg;
602
- }
603
-
604
- return this.rval;
605
- },
606
-
607
- dispatchException: function(exception) {
608
- if (this.done) {
609
- throw exception;
610
- }
611
-
612
- var context = this;
613
- function handle(loc, caught) {
614
- record.type = "throw";
615
- record.arg = exception;
616
- context.next = loc;
617
-
618
- if (caught) {
619
- // If the dispatched exception was caught by a catch block,
620
- // then let that catch block handle the exception normally.
621
- context.method = "next";
622
- context.arg = undefined$1;
623
- }
624
-
625
- return !! caught;
626
- }
627
-
628
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
629
- var entry = this.tryEntries[i];
630
- var record = entry.completion;
631
-
632
- if (entry.tryLoc === "root") {
633
- // Exception thrown outside of any try block that could handle
634
- // it, so set the completion value of the entire function to
635
- // throw the exception.
636
- return handle("end");
637
- }
638
-
639
- if (entry.tryLoc <= this.prev) {
640
- var hasCatch = hasOwn.call(entry, "catchLoc");
641
- var hasFinally = hasOwn.call(entry, "finallyLoc");
642
-
643
- if (hasCatch && hasFinally) {
644
- if (this.prev < entry.catchLoc) {
645
- return handle(entry.catchLoc, true);
646
- } else if (this.prev < entry.finallyLoc) {
647
- return handle(entry.finallyLoc);
648
- }
649
-
650
- } else if (hasCatch) {
651
- if (this.prev < entry.catchLoc) {
652
- return handle(entry.catchLoc, true);
653
- }
654
-
655
- } else if (hasFinally) {
656
- if (this.prev < entry.finallyLoc) {
657
- return handle(entry.finallyLoc);
658
- }
659
-
660
- } else {
661
- throw new Error("try statement without catch or finally");
662
- }
663
- }
664
- }
665
- },
666
-
667
- abrupt: function(type, arg) {
668
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
669
- var entry = this.tryEntries[i];
670
- if (entry.tryLoc <= this.prev &&
671
- hasOwn.call(entry, "finallyLoc") &&
672
- this.prev < entry.finallyLoc) {
673
- var finallyEntry = entry;
674
- break;
675
- }
676
- }
677
-
678
- if (finallyEntry &&
679
- (type === "break" ||
680
- type === "continue") &&
681
- finallyEntry.tryLoc <= arg &&
682
- arg <= finallyEntry.finallyLoc) {
683
- // Ignore the finally entry if control is not jumping to a
684
- // location outside the try/catch block.
685
- finallyEntry = null;
686
- }
687
-
688
- var record = finallyEntry ? finallyEntry.completion : {};
689
- record.type = type;
690
- record.arg = arg;
691
-
692
- if (finallyEntry) {
693
- this.method = "next";
694
- this.next = finallyEntry.finallyLoc;
695
- return ContinueSentinel;
696
- }
697
-
698
- return this.complete(record);
699
- },
700
-
701
- complete: function(record, afterLoc) {
702
- if (record.type === "throw") {
703
- throw record.arg;
704
- }
705
-
706
- if (record.type === "break" ||
707
- record.type === "continue") {
708
- this.next = record.arg;
709
- } else if (record.type === "return") {
710
- this.rval = this.arg = record.arg;
711
- this.method = "return";
712
- this.next = "end";
713
- } else if (record.type === "normal" && afterLoc) {
714
- this.next = afterLoc;
715
- }
716
-
717
- return ContinueSentinel;
718
- },
719
-
720
- finish: function(finallyLoc) {
721
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
722
- var entry = this.tryEntries[i];
723
- if (entry.finallyLoc === finallyLoc) {
724
- this.complete(entry.completion, entry.afterLoc);
725
- resetTryEntry(entry);
726
- return ContinueSentinel;
727
- }
728
- }
729
- },
730
-
731
- "catch": function(tryLoc) {
732
- for (var i = this.tryEntries.length - 1; i >= 0; --i) {
733
- var entry = this.tryEntries[i];
734
- if (entry.tryLoc === tryLoc) {
735
- var record = entry.completion;
736
- if (record.type === "throw") {
737
- var thrown = record.arg;
738
- resetTryEntry(entry);
739
- }
740
- return thrown;
741
- }
742
- }
743
-
744
- // The context.catch method must only be called with a location
745
- // argument that corresponds to a known catch block.
746
- throw new Error("illegal catch attempt");
747
- },
748
-
749
- delegateYield: function(iterable, resultName, nextLoc) {
750
- this.delegate = {
751
- iterator: values(iterable),
752
- resultName: resultName,
753
- nextLoc: nextLoc
754
- };
755
-
756
- if (this.method === "next") {
757
- // Deliberately forget the last sent value so that we don't
758
- // accidentally pass it on to the delegate.
759
- this.arg = undefined$1;
760
- }
761
-
762
- return ContinueSentinel;
763
- }
764
- };
765
-
766
- // Regardless of whether this script is executing as a CommonJS module
767
- // or not, return the runtime object so that we can declare the variable
768
- // regeneratorRuntime in the outer scope, which allows this module to be
769
- // injected easily by `bin/regenerator --include-runtime script.js`.
770
- return exports;
771
-
772
- }(
773
- // If this script is executing as a CommonJS module, use module.exports
774
- // as the regeneratorRuntime namespace. Otherwise create a new empty
775
- // object. Either way, the resulting object will be used to initialize
776
- // the regeneratorRuntime variable at the top of this file.
777
- module.exports
778
- ));
779
-
780
- try {
781
- regeneratorRuntime = runtime;
782
- } catch (accidentalStrictMode) {
783
- // This module should not be running in strict mode, so the above
784
- // assignment should always work unless something is misconfigured. Just
785
- // in case runtime.js accidentally runs in strict mode, in modern engines
786
- // we can explicitly access globalThis. In older engines we can escape
787
- // strict mode using a global Function call. This could conceivably fail
788
- // if a Content Security Policy forbids using Function, but in that case
789
- // the proper solution is to fix the accidental strict mode problem. If
790
- // you've misconfigured your bundler to force strict mode and applied a
791
- // CSP to forbid Function, and you're not willing to fix either of those
792
- // problems, please detail your unique predicament in a GitHub issue.
793
- if (typeof globalThis === "object") {
794
- globalThis.regeneratorRuntime = runtime;
795
- } else {
796
- Function("r", "regeneratorRuntime = r")(runtime);
797
- }
798
- }
799
- });
800
-
801
- var pkg = /*#__PURE__*/require("../package.json");
802
-
803
- var BASE_URL = "https://unpkg.com/" + pkg.name + "/src";
804
- var TAG = 'VBProcessor';
805
- var TFLITE_JS_FILE = 'tflite/tflite.js';
806
- var TFLITE_SIMD_JS_FILE = 'tflite/tflite-simd.js';
807
- var MODEL_FILE_NAME = 'models/selfie_segmentation_landscape.tflite';
808
-
809
- var loadScript = function loadScript(src) {
810
- return new Promise(function (resolve, reject) {
811
- var s = document.createElement('script');
812
- s.src = src;
813
- s.onload = resolve;
814
- s.onerror = reject;
815
- document.head.appendChild(s);
816
- });
817
- };
818
-
819
- var loadTFLiteModel = /*#__PURE__*/function () {
820
- var _ref = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
821
- var tfLite, path;
822
- return runtime_1.wrap(function _callee$(_context) {
823
- while (1) {
824
- switch (_context.prev = _context.next) {
825
- case 0:
826
- path = BASE_URL + "/" + TFLITE_SIMD_JS_FILE;
827
- _context.next = 3;
828
- return loadScript(path);
829
-
830
- case 3:
831
- _context.prev = 3;
832
- _context.next = 6;
833
- return createTFLiteSIMDModule();
834
-
835
- case 6:
836
- tfLite = _context.sent;
837
- _context.next = 18;
838
- break;
839
-
840
- case 9:
841
- _context.prev = 9;
842
- _context.t0 = _context["catch"](3);
843
- console.warn('SIMD not supported. You may experience poor virtual background effect.');
844
- path = BASE_URL + "/" + TFLITE_JS_FILE;
845
- _context.next = 15;
846
- return loadScript(path);
847
-
848
- case 15:
849
- _context.next = 17;
850
- return createTFLiteModule();
851
-
852
- case 17:
853
- tfLite = _context.sent;
854
-
855
- case 18:
856
- return _context.abrupt("return", tfLite);
857
-
858
- case 19:
859
- case "end":
860
- return _context.stop();
861
- }
862
- }
863
- }, _callee, null, [[3, 9]]);
864
- }));
865
-
866
- return function loadTFLiteModel() {
867
- return _ref.apply(this, arguments);
868
- };
869
- }();
870
-
871
- var loadTFLite = /*#__PURE__*/function () {
872
- var _ref2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
873
- var modelPath, _yield$Promise$all, tfLite, modelResponse, model, modelBufferOffset;
874
-
875
- return runtime_1.wrap(function _callee2$(_context2) {
876
- while (1) {
877
- switch (_context2.prev = _context2.next) {
878
- case 0:
879
- modelPath = BASE_URL + "/" + MODEL_FILE_NAME;
880
- _context2.next = 3;
881
- return Promise.all([loadTFLiteModel(), fetch(modelPath)]);
882
-
883
- case 3:
884
- _yield$Promise$all = _context2.sent;
885
- tfLite = _yield$Promise$all[0];
886
- modelResponse = _yield$Promise$all[1];
887
- _context2.next = 8;
888
- return modelResponse.arrayBuffer();
889
-
890
- case 8:
891
- model = _context2.sent;
892
- modelBufferOffset = tfLite._getModelBufferMemoryOffset();
893
- tfLite.HEAPU8.set(new Uint8Array(model), modelBufferOffset);
894
-
895
- tfLite._loadModel(model.byteLength);
896
-
897
- console.log(TAG, 'Input memory offset:', tfLite._getInputMemoryOffset());
898
- console.log(TAG, 'Input height:', tfLite._getInputHeight());
899
- console.log(TAG, 'Input width:', tfLite._getInputWidth());
900
- console.log(TAG, 'Input channels:', tfLite._getInputChannelCount());
901
- return _context2.abrupt("return", tfLite);
902
-
903
- case 17:
904
- case "end":
905
- return _context2.stop();
906
- }
907
- }
908
- }, _callee2);
909
- }));
910
-
911
- return function loadTFLite() {
912
- return _ref2.apply(this, arguments);
913
- };
914
- }();
915
-
916
- var TAG$1 = 'VBProcessor';
917
- var DEFAULT_DELAY = 33;
918
-
919
- var pkg$1 = /*#__PURE__*/require('../package.json');
920
-
921
- var minVideoWidthForSharpening = 214;
922
- var maxVideoWidthForSharpening = 855;
923
- var minVideoHeightForSharpening = 120;
924
- var maxVideoHeightForSharpening = 720;
925
- var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
926
- function HMSVirtualBackgroundPlugin(background, enableSharpening) {
927
- if (enableSharpening === void 0) {
928
- enableSharpening = false;
929
- }
930
-
931
- this.backgroundType = 'none';
932
- this.background = background;
933
- this.enableSharpening = enableSharpening;
934
- this.backgroundImage = null;
935
- this.backgroundVideo = null;
936
- this.personMaskWidth = 256;
937
- this.personMaskHeight = 144;
938
- this.isVirtualBackground = false;
939
- this.blurValue = '10px';
940
- this.loadModelCalled = false;
941
- this.tfLite = null;
942
- this.modelName = 'landscape-segmentation';
943
- this.outputCtx = null;
944
- this.input = null;
945
- this.output = null;
946
- this.timerID = 0;
947
- this.imageAspectRatio = 1;
948
- this.personMaskPixelCount = this.personMaskWidth * this.personMaskHeight;
949
- this.personMask = new ImageData(this.personMaskWidth, this.personMaskHeight);
950
- this.personMaskCanvas = document.createElement('canvas');
951
- this.personMaskCanvas.width = this.personMaskWidth;
952
- this.personMaskCanvas.height = this.personMaskHeight;
953
- this.personMaskCtx = this.personMaskCanvas.getContext('2d');
954
- this.filters = {};
955
- this.gifFrames = null;
956
- this.gifFramesIndex = 0;
957
- this.gifFrameImageData = null;
958
- this.tempGifCanvas = document.createElement('canvas');
959
- this.tempGifContext = this.tempGifCanvas.getContext('2d');
960
- this.giflocalCount = 0;
961
- this.enableSharpening = enableSharpening;
962
- this.log(TAG$1, 'Virtual Background plugin created');
963
- this.setBackground(this.background);
964
- }
965
-
966
- var _proto = HMSVirtualBackgroundPlugin.prototype;
967
-
968
- _proto.init = /*#__PURE__*/function () {
969
- var _init = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee() {
970
- return runtime_1.wrap(function _callee$(_context) {
971
- while (1) {
972
- switch (_context.prev = _context.next) {
973
- case 0:
974
- if (this.loadModelCalled) {
975
- _context.next = 9;
976
- break;
977
- }
978
-
979
- this.log(TAG$1, 'PREVIOUS LOADED MODEL IS ', this.tfLite);
980
- this.loadModelCalled = true;
981
- this.tfLitePromise = loadTFLite();
982
- _context.next = 6;
983
- return this.tfLitePromise;
984
-
985
- case 6:
986
- this.tfLite = _context.sent;
987
- _context.next = 11;
988
- break;
989
-
990
- case 9:
991
- _context.next = 11;
992
- return this.tfLitePromise;
993
-
994
- case 11:
995
- if (this.enableSharpening) {
996
- this.initSharpenFilter();
997
- }
998
-
999
- case 12:
1000
- case "end":
1001
- return _context.stop();
1002
- }
1003
- }
1004
- }, _callee, this);
1005
- }));
1006
-
1007
- function init() {
1008
- return _init.apply(this, arguments);
1009
- }
1010
-
1011
- return init;
1012
- }();
1013
-
1014
- _proto.isSupported = function isSupported() {
1015
- //support chrome, firefox, edge TODO: check this
1016
- return navigator.userAgent.indexOf('Chrome') !== -1 || navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Edg') !== -1;
1017
- };
1018
-
1019
- _proto.getName = function getName() {
1020
- return pkg$1.name;
1021
- };
1022
-
1023
- _proto.getPluginType = function getPluginType() {
1024
- return HMSVideoPluginType.TRANSFORM;
1025
- };
1026
-
1027
- _proto.setBackground = /*#__PURE__*/function () {
1028
- var _setBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(path) {
1029
- var _this = this;
1030
-
1031
- var img;
1032
- return runtime_1.wrap(function _callee3$(_context3) {
1033
- while (1) {
1034
- switch (_context3.prev = _context3.next) {
1035
- case 0:
1036
- if (!(path !== '')) {
1037
- _context3.next = 50;
1038
- break;
1039
- }
1040
-
1041
- if (!(path === 'none')) {
1042
- _context3.next = 8;
1043
- break;
1044
- }
1045
-
1046
- this.log(TAG$1, 'setting background to :', path);
1047
- this.background = 'none';
1048
- this.backgroundType = 'none';
1049
- this.isVirtualBackground = false;
1050
- _context3.next = 48;
1051
- break;
1052
-
1053
- case 8:
1054
- if (!(path === 'blur')) {
1055
- _context3.next = 15;
1056
- break;
1057
- }
1058
-
1059
- this.log(TAG$1, 'setting background to :', path);
1060
- this.background = 'blur';
1061
- this.backgroundType = 'blur';
1062
- this.isVirtualBackground = false;
1063
- _context3.next = 48;
1064
- break;
1065
-
1066
- case 15:
1067
- if (!(path instanceof HTMLImageElement)) {
1068
- _context3.next = 29;
1069
- break;
1070
- }
1071
-
1072
- this.log('setting background to image', path);
1073
- _context3.next = 19;
1074
- return this.setImage(path);
1075
-
1076
- case 19:
1077
- img = _context3.sent;
1078
-
1079
- if (!(!img || !img.complete || !img.naturalHeight)) {
1080
- _context3.next = 24;
1081
- break;
1082
- }
1083
-
1084
- throw new Error('Invalid image. Provide a valid and successfully loaded HTMLImageElement');
1085
-
1086
- case 24:
1087
- this.isVirtualBackground = true;
1088
- this.backgroundImage = img;
1089
- this.backgroundType = 'image';
1090
-
1091
- case 27:
1092
- _context3.next = 48;
1093
- break;
1094
-
1095
- case 29:
1096
- if (!(path instanceof HTMLVideoElement)) {
1097
- _context3.next = 38;
1098
- break;
1099
- }
1100
-
1101
- this.log('setting background to video', path);
1102
- this.backgroundVideo = path;
1103
- this.backgroundVideo.crossOrigin = 'anonymous';
1104
- this.backgroundVideo.muted = true;
1105
- this.backgroundVideo.loop = true;
1106
- this.backgroundVideo.oncanplaythrough = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
1107
- return runtime_1.wrap(function _callee2$(_context2) {
1108
- while (1) {
1109
- switch (_context2.prev = _context2.next) {
1110
- case 0:
1111
- if (!(_this.backgroundVideo != null)) {
1112
- _context2.next = 5;
1113
- break;
1114
- }
1115
-
1116
- _context2.next = 3;
1117
- return _this.backgroundVideo.play();
1118
-
1119
- case 3:
1120
- _this.isVirtualBackground = true;
1121
- _this.backgroundType = 'video';
1122
-
1123
- case 5:
1124
- case "end":
1125
- return _context2.stop();
1126
- }
1127
- }
1128
- }, _callee2);
1129
- }));
1130
- _context3.next = 48;
1131
- break;
1132
-
1133
- case 38:
1134
- console.log('setting gif to background');
1135
- _context3.next = 41;
1136
- return this.setGiF(path);
1137
-
1138
- case 41:
1139
- this.gifFrames = _context3.sent;
1140
-
1141
- if (!(this.gifFrames != null && this.gifFrames.length > 0)) {
1142
- _context3.next = 47;
1143
- break;
1144
- }
1145
-
1146
- this.backgroundType = 'gif';
1147
- this.isVirtualBackground = true;
1148
- _context3.next = 48;
1149
- break;
1150
-
1151
- case 47:
1152
- throw new Error('Invalid background supplied, see the docs to check supported background type');
1153
-
1154
- case 48:
1155
- _context3.next = 51;
1156
- break;
1157
-
1158
- case 50:
1159
- throw new Error('Invalid background supplied, see the docs to check supported background type');
1160
-
1161
- case 51:
1162
- case "end":
1163
- return _context3.stop();
1164
- }
1165
- }
1166
- }, _callee3, this);
1167
- }));
1168
-
1169
- function setBackground(_x) {
1170
- return _setBackground.apply(this, arguments);
1171
- }
1172
-
1173
- return setBackground;
1174
- }();
1175
-
1176
- _proto.stop = function stop() {
1177
- if (this.isVirtualBackground) {
1178
- var _this$backgroundImage, _this$backgroundVideo;
1179
-
1180
- (_this$backgroundImage = this.backgroundImage) == null ? void 0 : _this$backgroundImage.removeAttribute('src');
1181
- (_this$backgroundVideo = this.backgroundVideo) == null ? void 0 : _this$backgroundVideo.removeAttribute('src');
1182
-
1183
- if (this.backgroundType === 'video') {
1184
- this.backgroundVideo.loop = false;
1185
- this.backgroundVideo = null;
1186
- }
1187
- }
1188
-
1189
- if (this.outputCtx) {
1190
- this.outputCtx.fillStyle = "rgb(0, 0, 0)";
1191
- this.outputCtx.fillRect(0, 0, this.output.width, this.output.height);
1192
- } //gif related
1193
-
1194
-
1195
- this.gifFrameImageData = null;
1196
- this.gifFrames = null;
1197
- this.giflocalCount = 0;
1198
- this.gifFramesIndex = 0;
1199
- };
1200
-
1201
- _proto.processVideoFrame = function processVideoFrame(input, output, skipProcessing) {
1202
- var _this2 = this;
1203
-
1204
- if (!input || !output) {
1205
- throw new Error('Plugin invalid input/output');
1206
- }
1207
-
1208
- this.input = input;
1209
- this.output = output;
1210
- var ctx = output.getContext('2d');
1211
-
1212
- if (ctx.canvas.width !== input.width) {
1213
- ctx.canvas.width = input.width;
1214
- }
1215
-
1216
- if (ctx.canvas.height !== input.height) {
1217
- ctx.canvas.height = input.height;
1218
- }
1219
-
1220
- if (this.backgroundType === 'video') {
1221
- this.backgroundVideo.width = input.width;
1222
- this.backgroundVideo.height = input.height;
1223
- }
1224
-
1225
- this.outputCtx = ctx;
1226
- this.imageAspectRatio = input.width / input.height;
1227
-
1228
- if (this.imageAspectRatio <= 0) {
1229
- throw new Error('Invalid input width/height');
1230
- }
1231
-
1232
- var process = /*#__PURE__*/function () {
1233
- var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4() {
1234
- return runtime_1.wrap(function _callee4$(_context4) {
1235
- while (1) {
1236
- switch (_context4.prev = _context4.next) {
1237
- case 0:
1238
- _context4.next = 2;
1239
- return _this2.runSegmentation(skipProcessing);
1240
-
1241
- case 2:
1242
- case "end":
1243
- return _context4.stop();
1244
- }
1245
- }
1246
- }, _callee4);
1247
- }));
1248
-
1249
- return function process() {
1250
- return _ref2.apply(this, arguments);
1251
- };
1252
- }();
1253
-
1254
- if (this.background === 'none' && !this.isVirtualBackground) {
1255
- this.outputCtx.globalCompositeOperation = 'copy';
1256
- this.outputCtx.filter = 'none';
1257
- this.outputCtx.drawImage(input, 0, 0, input.width, input.height);
1258
- } else {
1259
- process();
1260
- }
1261
- };
1262
-
1263
- _proto.setImage = /*#__PURE__*/function () {
1264
- var _setImage = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(image) {
1265
- return runtime_1.wrap(function _callee5$(_context5) {
1266
- while (1) {
1267
- switch (_context5.prev = _context5.next) {
1268
- case 0:
1269
- image.crossOrigin = 'anonymous';
1270
- return _context5.abrupt("return", new Promise(function (resolve, reject) {
1271
- image.onload = function () {
1272
- return resolve(image);
1273
- };
1274
-
1275
- image.onerror = reject;
1276
- }));
1277
-
1278
- case 2:
1279
- case "end":
1280
- return _context5.stop();
1281
- }
1282
- }
1283
- }, _callee5);
1284
- }));
1285
-
1286
- function setImage(_x2) {
1287
- return _setImage.apply(this, arguments);
1288
- }
1289
-
1290
- return setImage;
1291
- }();
1292
-
1293
- _proto.setGiF = function setGiF(url) {
1294
- return fetch(url).then(function (resp) {
1295
- return resp.arrayBuffer();
1296
- }).then(function (buff) {
1297
- return parseGIF(buff);
1298
- }).then(function (gif) {
1299
- return decompressFrames(gif, true);
1300
- });
1301
- };
1302
-
1303
- _proto.log = function log(tag) {
1304
- var _console;
1305
-
1306
- for (var _len = arguments.length, data = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1307
- data[_key - 1] = arguments[_key];
1308
- }
1309
-
1310
- (_console = console).info.apply(_console, [tag].concat(data));
1311
- };
1312
-
1313
- _proto.resizeInputData = function resizeInputData() {
1314
- this.personMaskCtx.drawImage(this.input, 0, 0, this.input.width, this.input.height, 0, 0, this.personMaskWidth, this.personMaskHeight);
1315
- var imageData = this.personMaskCtx.getImageData(0, 0, this.personMaskWidth, this.personMaskHeight); //
1316
-
1317
- var inputMemoryOffset = this.tfLite._getInputMemoryOffset() / 4;
1318
-
1319
- for (var i = 0; i < this.personMaskPixelCount; i++) {
1320
- this.tfLite.HEAPF32[inputMemoryOffset + i * 3] = imageData.data[i * 4] / 255;
1321
- this.tfLite.HEAPF32[inputMemoryOffset + i * 3 + 1] = imageData.data[i * 4 + 1] / 255;
1322
- this.tfLite.HEAPF32[inputMemoryOffset + i * 3 + 2] = imageData.data[i * 4 + 2] / 255;
1323
- }
1324
- };
1325
-
1326
- _proto.infer = function infer(skipProcessing) {
1327
- if (!skipProcessing) {
1328
- this.tfLite._runInference();
1329
- }
1330
-
1331
- var outputMemoryOffset = this.tfLite._getOutputMemoryOffset() / 4;
1332
-
1333
- for (var i = 0; i < this.personMaskPixelCount; i++) {
1334
- if (this.modelName === 'meet') {
1335
- var background = this.tfLite.HEAPF32[outputMemoryOffset + i * 2];
1336
- var person = this.tfLite.HEAPF32[outputMemoryOffset + i * 2 + 1];
1337
- var shift = Math.max(background, person);
1338
- var backgroundExp = Math.exp(background - shift);
1339
- var personExp = Math.exp(person - shift); // Sets only the alpha component of each pixel.
1340
-
1341
- this.personMask.data[i * 4 + 3] = 255 * personExp / (backgroundExp + personExp);
1342
- } else if (this.modelName === 'landscape-segmentation') {
1343
- var _person = this.tfLite.HEAPF32[outputMemoryOffset + i];
1344
- this.personMask.data[i * 4 + 3] = 255 * _person;
1345
- }
1346
- }
1347
-
1348
- this.personMaskCtx.putImageData(this.personMask, 0, 0);
1349
- };
1350
-
1351
- _proto.postProcessing = function postProcessing() {
1352
- this.outputCtx.globalCompositeOperation = 'copy';
1353
- this.outputCtx.filter = 'none';
1354
-
1355
- if (this.isVirtualBackground) {
1356
- this.outputCtx.filter = 'blur(4px)';
1357
- } else {
1358
- this.outputCtx.filter = 'blur(8px)';
1359
- }
1360
-
1361
- this.drawPersonMask();
1362
- this.outputCtx.globalCompositeOperation = 'source-in';
1363
- this.outputCtx.filter = 'none'; // //Draw the foreground
1364
-
1365
- this.outputCtx.drawImage(this.input, 0, 0);
1366
-
1367
- if (this.enableSharpening && this.output.width > minVideoWidthForSharpening && // minimum and maximum resolution to enable sharpening filter
1368
- this.output.height > minVideoHeightForSharpening && this.output.width < maxVideoWidthForSharpening && this.output.height < maxVideoHeightForSharpening) {
1369
- this.sharpenFilter();
1370
- } // //Draw the background
1371
-
1372
-
1373
- this.drawSegmentedBackground();
1374
- };
1375
-
1376
- _proto.sharpenFilter = function sharpenFilter() {
1377
- // adding sharpening filter to each frame to improve edges and brightness
1378
- // The basic idea is that you take the weighed sum of a rectangle of pixels from the source image and use that as the output value using convolution filter
1379
- // It is applied intermediate output with black background and only mask data in frame
1380
- // Filter currently used is 3 x 3 sharpening filter with values as shown:
1381
- // [ 0, -1, 0,
1382
- // -1, 5, -1,
1383
- // 0, -1, 0 ]
1384
- var outputImageData = this.outputCtx.getImageData(0, 0, this.output.width, this.output.height); // filters you may try
1385
- // [-1, -1, -1, -1, 9, -1, -1, -1, -1]
1386
- //[0, -1, 0, -1, 5, -1, 0, -1, 0]
1387
-
1388
- var output = this.filters.convolute(outputImageData);
1389
- this.outputCtx.putImageData(output, 0, 0);
1390
- };
1391
-
1392
- _proto.drawPersonMask = function drawPersonMask() {
1393
- this.outputCtx.drawImage(this.personMaskCanvas, 0, 0, this.personMaskWidth, this.personMaskHeight, 0, 0, this.output.width, this.output.height);
1394
- };
1395
-
1396
- _proto.drawSegmentedBackground = function drawSegmentedBackground() {
1397
- this.outputCtx.globalCompositeOperation = 'destination-over';
1398
- this.outputCtx.imageSmoothingEnabled = true;
1399
- this.outputCtx.imageSmoothingQuality = 'high';
1400
-
1401
- if (this.isVirtualBackground) {
1402
- if (this.backgroundType === 'video' && this.backgroundVideo != null && this.backgroundVideo.readyState >= 4) {
1403
- this.fitVideoToBackground();
1404
- } else if (this.backgroundType === 'image') {
1405
- this.fitImageToBackground();
1406
- } else if (this.backgroundType === 'gif') {
1407
- if (this.giflocalCount > this.gifFrames[this.gifFramesIndex].delay / DEFAULT_DELAY) {
1408
- this.gifFramesIndex++;
1409
-
1410
- if (this.gifFramesIndex >= this.gifFrames.length) {
1411
- this.gifFramesIndex = 0;
1412
- }
1413
-
1414
- this.giflocalCount = 0;
1415
- } else {
1416
- this.giflocalCount++;
1417
- }
1418
-
1419
- this.fitGifToBackground();
1420
- }
1421
- } else {
1422
- this.addBlurToBackground();
1423
- }
1424
- };
1425
-
1426
- _proto.runSegmentation = /*#__PURE__*/function () {
1427
- var _runSegmentation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee6(skipProcessing) {
1428
- return runtime_1.wrap(function _callee6$(_context6) {
1429
- while (1) {
1430
- switch (_context6.prev = _context6.next) {
1431
- case 0:
1432
- if (!this.tfLite) {
1433
- _context6.next = 5;
1434
- break;
1435
- }
1436
-
1437
- // const start = performance.now();
1438
- this.resizeInputData();
1439
- _context6.next = 4;
1440
- return this.infer(skipProcessing);
1441
-
1442
- case 4:
1443
- this.postProcessing(); // const end = performance.now();
1444
- // this.log(TAG,"time taken",end -start);
1445
-
1446
- case 5:
1447
- case "end":
1448
- return _context6.stop();
1449
- }
1450
- }
1451
- }, _callee6, this);
1452
- }));
1453
-
1454
- function runSegmentation(_x3) {
1455
- return _runSegmentation.apply(this, arguments);
1456
- }
1457
-
1458
- return runSegmentation;
1459
- }();
1460
-
1461
- _proto.fitVideoToBackground = function fitVideoToBackground() {
1462
- this.fitData(this.backgroundVideo, this.backgroundVideo.videoWidth, this.backgroundVideo.videoHeight);
1463
- };
1464
-
1465
- _proto.fitImageToBackground = function fitImageToBackground() {
1466
- this.fitData(this.backgroundImage, this.backgroundImage.width, this.backgroundImage.height);
1467
- };
1468
-
1469
- _proto.fitGifToBackground = function fitGifToBackground() {
1470
- if (this.gifFrameImageData == null) {
1471
- var dims = this.gifFrames[this.gifFramesIndex].dims;
1472
- this.tempGifCanvas.width = dims.width;
1473
- this.tempGifCanvas.height = dims.height;
1474
- this.gifFrameImageData = this.tempGifContext.createImageData(dims.width, dims.height);
1475
- } // set the patch data as an override
1476
-
1477
-
1478
- this.gifFrameImageData.data.set(this.gifFrames[this.gifFramesIndex].patch);
1479
- this.tempGifContext.putImageData(this.gifFrameImageData, 0, 0);
1480
- this.fitData(this.tempGifCanvas, this.gifFrameImageData.width, this.gifFrameImageData.height);
1481
- };
1482
-
1483
- _proto.fitData = function fitData(data, dataWidth, dataHeight) {
1484
- var inputWidth, inputHeight, xoffset, yoffset;
1485
-
1486
- if (dataWidth / dataHeight < this.imageAspectRatio) {
1487
- inputWidth = dataWidth;
1488
- inputHeight = dataWidth / this.imageAspectRatio;
1489
- xoffset = 0;
1490
- yoffset = (dataHeight - inputHeight) / 2;
1491
- } else {
1492
- inputHeight = dataHeight;
1493
- inputWidth = dataHeight * this.imageAspectRatio;
1494
- yoffset = 0;
1495
- xoffset = (dataWidth - inputWidth) / 2;
1496
- }
1497
-
1498
- this.outputCtx.drawImage(data, xoffset, yoffset, inputWidth, inputHeight, 0, 0, this.output.width, this.output.height);
1499
- };
1500
-
1501
- _proto.addBlurToBackground = /*#__PURE__*/function () {
1502
- var _addBlurToBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
1503
- var blurValue;
1504
- return runtime_1.wrap(function _callee7$(_context7) {
1505
- while (1) {
1506
- switch (_context7.prev = _context7.next) {
1507
- case 0:
1508
- blurValue = '15px';
1509
- if (this.input.width <= 160) blurValue = '5px';else if (this.input.width <= 320) blurValue = '10px';else if (this.input.width <= 640) blurValue = '15px';else if (this.input.width <= 960) blurValue = '20px';else if (this.input.width <= 1280) blurValue = '25px';else if (this.input.width <= 1920) blurValue = '30px';
1510
- this.outputCtx.filter = "blur(" + blurValue + ")";
1511
- this.outputCtx.drawImage(this.input, 0, 0, this.output.width, this.output.height);
1512
-
1513
- case 4:
1514
- case "end":
1515
- return _context7.stop();
1516
- }
1517
- }
1518
- }, _callee7, this);
1519
- }));
1520
-
1521
- function addBlurToBackground() {
1522
- return _addBlurToBackground.apply(this, arguments);
1523
- }
1524
-
1525
- return addBlurToBackground;
1526
- }();
1527
-
1528
- _proto.initSharpenFilter = function initSharpenFilter() {
1529
- var _this3 = this;
1530
-
1531
- this.filters.tmpCanvas = document.createElement('canvas');
1532
- this.filters.tmpCtx = this.filters.tmpCanvas.getContext('2d');
1533
-
1534
- this.filters.createImageData = function (w, h) {
1535
- return _this3.filters.tmpCtx.createImageData(w, h);
1536
- };
1537
-
1538
- this.filters.convolute = function (pixels, weights, opaque) {
1539
- if (weights === void 0) {
1540
- weights = [0, -1, 0, -1, 5, -1, 0, -1, 0];
1541
- }
1542
-
1543
- var side = Math.round(Math.sqrt(weights.length));
1544
- var halfSide = Math.floor(side / 2);
1545
- var src = pixels.data;
1546
- var sw = pixels.width;
1547
- var sh = pixels.height; // pad output by the convolution matrix
1548
-
1549
- var w = sw;
1550
- var h = sh;
1551
-
1552
- var output = _this3.filters.createImageData(w, h);
1553
-
1554
- var dst = output.data; // go through the destination image pixels
1555
-
1556
- var alphaFac = opaque ? 1 : 0;
1557
-
1558
- for (var y = 0; y < h; y = y + 1) {
1559
- for (var x = 0; x < w; x = x + 1) {
1560
- var dstOff = (y * w + x) * 4;
1561
-
1562
- if (src[dstOff + 3] === 0) {
1563
- continue;
1564
- } else if (x < w && y < h) {
1565
- var sy = y;
1566
- var sx = x; // calculate the weighed sum of the source image pixels that
1567
- // fall under the convolution matrix
1568
-
1569
- var r = 0,
1570
- g = 0,
1571
- b = 0,
1572
- a = 0;
1573
-
1574
- for (var cy = 0; cy < side; cy++) {
1575
- for (var cx = 0; cx < side; cx++) {
1576
- var scy = sy + cy - halfSide;
1577
- var scx = sx + cx - halfSide;
1578
-
1579
- if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
1580
- var srcOff = (scy * sw + scx) * 4;
1581
- var wt = weights[cy * side + cx];
1582
- r += src[srcOff] * wt;
1583
- g += src[srcOff + 1] * wt;
1584
- b += src[srcOff + 2] * wt;
1585
- a += src[srcOff + 3] * wt;
1586
- }
1587
- }
1588
- }
1589
-
1590
- dst[dstOff] = r;
1591
- dst[dstOff + 1] = g;
1592
- dst[dstOff + 2] = b;
1593
- dst[dstOff + 3] = a + alphaFac * (255 - a);
1594
- }
1595
- }
1596
- }
1597
-
1598
- return output;
1599
- };
1600
- };
1601
-
1602
- return HMSVirtualBackgroundPlugin;
1603
- }();
1604
-
1605
- export { HMSVirtualBackgroundPlugin };
1606
- //# sourceMappingURL=hms-virtual-background.esm.js.map