@vonage/media-processor 3.0.1-alpha.2 → 3.0.2
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.
|
@@ -37,7 +37,7 @@ function Y() {
|
|
|
37
37
|
const u = /* @__PURE__ */ new WeakMap(), m = /* @__PURE__ */ new WeakMap(), d = /* @__PURE__ */ new WeakMap(), P = /* @__PURE__ */ Symbol("anyProducer"), D = Promise.resolve(), M = /* @__PURE__ */ Symbol("listenerAdded"), R = /* @__PURE__ */ Symbol("listenerRemoved");
|
|
38
38
|
let I = !1, F = !1;
|
|
39
39
|
const x = (r) => typeof r == "string" || typeof r == "symbol" || typeof r == "number";
|
|
40
|
-
function
|
|
40
|
+
function w(r) {
|
|
41
41
|
if (!x(r))
|
|
42
42
|
throw new TypeError("`eventName` must be a string, symbol, or number");
|
|
43
43
|
}
|
|
@@ -180,7 +180,7 @@ class h {
|
|
|
180
180
|
on(e, t, { signal: s } = {}) {
|
|
181
181
|
k(t), e = Array.isArray(e) ? e : [e];
|
|
182
182
|
for (const n of e) {
|
|
183
|
-
|
|
183
|
+
w(n);
|
|
184
184
|
let o = _(this, n);
|
|
185
185
|
o || (o = /* @__PURE__ */ new Set(), m.get(this).set(n, o)), o.add(t), this.logIfDebugEnabled("subscribe", n, void 0), b(n) || E(this, M, { eventName: n, listener: t });
|
|
186
186
|
}
|
|
@@ -192,7 +192,7 @@ class h {
|
|
|
192
192
|
off(e, t) {
|
|
193
193
|
k(t), e = Array.isArray(e) ? e : [e];
|
|
194
194
|
for (const s of e) {
|
|
195
|
-
|
|
195
|
+
w(s);
|
|
196
196
|
const i = _(this, s);
|
|
197
197
|
i && (i.delete(t), i.size === 0 && m.get(this).delete(s)), this.logIfDebugEnabled("unsubscribe", s, void 0), b(s) || E(this, R, { eventName: s, listener: t });
|
|
198
198
|
}
|
|
@@ -211,11 +211,11 @@ class h {
|
|
|
211
211
|
events(e) {
|
|
212
212
|
e = Array.isArray(e) ? e : [e];
|
|
213
213
|
for (const t of e)
|
|
214
|
-
|
|
214
|
+
w(t);
|
|
215
215
|
return V(this, e);
|
|
216
216
|
}
|
|
217
217
|
async emit(e, t) {
|
|
218
|
-
if (
|
|
218
|
+
if (w(e), b(e) && !I)
|
|
219
219
|
throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");
|
|
220
220
|
this.logIfDebugEnabled("emit", e, t), N(this, e, t);
|
|
221
221
|
const s = _(this, e) ?? /* @__PURE__ */ new Set(), i = u.get(this), n = [...s], o = b(e) ? [] : [...i];
|
|
@@ -231,7 +231,7 @@ class h {
|
|
|
231
231
|
]);
|
|
232
232
|
}
|
|
233
233
|
async emitSerial(e, t) {
|
|
234
|
-
if (
|
|
234
|
+
if (w(e), b(e) && !I)
|
|
235
235
|
throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");
|
|
236
236
|
this.logIfDebugEnabled("emitSerial", e, t);
|
|
237
237
|
const s = _(this, e) ?? /* @__PURE__ */ new Set(), i = u.get(this), n = [...s], o = [...i];
|
|
@@ -285,7 +285,7 @@ class h {
|
|
|
285
285
|
t += u.get(this).size + (_(this, s)?.size ?? 0) + (T(this, s)?.size ?? 0) + (T(this)?.size ?? 0);
|
|
286
286
|
continue;
|
|
287
287
|
}
|
|
288
|
-
s !== void 0 &&
|
|
288
|
+
s !== void 0 && w(s), t += u.get(this).size;
|
|
289
289
|
for (const i of m.get(this).values())
|
|
290
290
|
t += i.size;
|
|
291
291
|
for (const i of d.get(this).values())
|
|
@@ -428,7 +428,7 @@ async function me(r, e) {
|
|
|
428
428
|
S(r, await e.instance[t](...s ?? []));
|
|
429
429
|
}
|
|
430
430
|
const ge = $("registeredWorkers", {});
|
|
431
|
-
function
|
|
431
|
+
function ye(r, e) {
|
|
432
432
|
if (!r.args)
|
|
433
433
|
throw "Missing className while initializing worker";
|
|
434
434
|
const [t, s] = r.args, i = ge[t];
|
|
@@ -438,7 +438,7 @@ function we(r, e) {
|
|
|
438
438
|
throw `unknown worker class ${t}`;
|
|
439
439
|
C(s, p), S(r, typeof e.instance !== void 0);
|
|
440
440
|
}
|
|
441
|
-
async function
|
|
441
|
+
async function we(r, e) {
|
|
442
442
|
const { args: t } = r;
|
|
443
443
|
if (!e.instance)
|
|
444
444
|
throw "instance not initialized";
|
|
@@ -456,13 +456,13 @@ function be() {
|
|
|
456
456
|
const t = e.data;
|
|
457
457
|
switch (t.type) {
|
|
458
458
|
case g.INIT:
|
|
459
|
-
|
|
459
|
+
ye(t, r);
|
|
460
460
|
break;
|
|
461
461
|
case g.FORWARD:
|
|
462
462
|
me(t, r);
|
|
463
463
|
break;
|
|
464
464
|
case g.TERMINATE:
|
|
465
|
-
|
|
465
|
+
we(t, r);
|
|
466
466
|
break;
|
|
467
467
|
case g.GLOBALS_SYNC:
|
|
468
468
|
_e(t);
|
|
@@ -528,7 +528,7 @@ class z {
|
|
|
528
528
|
};
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
|
-
const A = "3.0.
|
|
531
|
+
const A = "3.0.2", O = class O {
|
|
532
532
|
};
|
|
533
533
|
O.updates = {
|
|
534
534
|
transformer_new: "New transformer",
|
|
@@ -569,11 +569,11 @@ class Pe {
|
|
|
569
569
|
}
|
|
570
570
|
async onFrameTransformed(e = {}, t = !1) {
|
|
571
571
|
this.startAt === 0 && (this.startAt = Date.now()), this.frameTransformedCount++;
|
|
572
|
-
const { startAt: s, frameTransformedCount: i, frameFromSourceCount: n } = this, o = Date.now(), a = (o - s) / 1e3, l = i / a,
|
|
572
|
+
const { startAt: s, frameTransformedCount: i, frameFromSourceCount: n } = this, o = Date.now(), a = (o - s) / 1e3, l = i / a, y = n / a;
|
|
573
573
|
return t || this.frameTransformedCount >= this.config.loggingIntervalFrameCount ? (this.frameFromSourceCount = 0, this.frameTransformedCount = 0, this.startAt = o, this.reporter.config = this.config, this.reporter.send({
|
|
574
574
|
...this.config.report,
|
|
575
575
|
variation: "QoS",
|
|
576
|
-
fps:
|
|
576
|
+
fps: y,
|
|
577
577
|
transformedFps: l,
|
|
578
578
|
framesTransformed: i,
|
|
579
579
|
...e
|
|
@@ -899,9 +899,9 @@ class Fe {
|
|
|
899
899
|
constructor() {
|
|
900
900
|
super(), this.arrays = [], this.arrayOffset = 0, this.port.onmessage = ({ data: l }) => this.arrays.push(l), this.emptyArray = new Float32Array(0);
|
|
901
901
|
}
|
|
902
|
-
process(l, [[
|
|
903
|
-
for (let W = 0; W <
|
|
904
|
-
(!this.array || this.arrayOffset >= this.array.length) && (this.array = this.arrays.shift() || this.emptyArray, this.arrayOffset = 0),
|
|
902
|
+
process(l, [[y]]) {
|
|
903
|
+
for (let W = 0; W < y.length; W++)
|
|
904
|
+
(!this.array || this.arrayOffset >= this.array.length) && (this.array = this.arrays.shift() || this.emptyArray, this.arrayOffset = 0), y[W] = this.array[this.arrayOffset++] || 0;
|
|
905
905
|
return !0;
|
|
906
906
|
}
|
|
907
907
|
}
|
|
@@ -931,7 +931,12 @@ class Ge {
|
|
|
931
931
|
async start(s) {
|
|
932
932
|
t.addEventListener(
|
|
933
933
|
"ended",
|
|
934
|
-
() =>
|
|
934
|
+
() => {
|
|
935
|
+
try {
|
|
936
|
+
s.close();
|
|
937
|
+
} catch {
|
|
938
|
+
}
|
|
939
|
+
},
|
|
935
940
|
{ once: !0 }
|
|
936
941
|
), this.video = document.createElement("video"), this.video.srcObject = new MediaStream([t]), await Promise.all([
|
|
937
942
|
this.video.play(),
|
|
@@ -946,12 +951,22 @@ class Ge {
|
|
|
946
951
|
}), this.t1 = performance.now();
|
|
947
952
|
},
|
|
948
953
|
async pull(s) {
|
|
949
|
-
if (t.readyState == "ended")
|
|
950
|
-
|
|
954
|
+
if (t.readyState == "ended") {
|
|
955
|
+
try {
|
|
956
|
+
s.close();
|
|
957
|
+
} catch {
|
|
958
|
+
}
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
951
961
|
const i = t.getSettings().frameRate || 30;
|
|
952
962
|
for (; performance.now() - this.t1 < 1e3 / i; )
|
|
953
|
-
if (await new Promise((n) => requestAnimationFrame(n)), t.readyState == "ended")
|
|
954
|
-
|
|
963
|
+
if (await new Promise((n) => requestAnimationFrame(n)), t.readyState == "ended") {
|
|
964
|
+
try {
|
|
965
|
+
s.close();
|
|
966
|
+
} catch {
|
|
967
|
+
}
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
955
970
|
this.canvas.width = this.video.videoWidth, this.canvas.height = this.video.videoHeight, this.t1 = performance.now(), this.ctx.drawImage(this.video, 0, 0), s.enqueue(
|
|
956
971
|
new VideoFrame(this.canvas, {
|
|
957
972
|
timestamp: this.t1
|
|
@@ -962,7 +977,12 @@ class Ge {
|
|
|
962
977
|
async start(s) {
|
|
963
978
|
t.addEventListener(
|
|
964
979
|
"ended",
|
|
965
|
-
() =>
|
|
980
|
+
() => {
|
|
981
|
+
try {
|
|
982
|
+
s.close();
|
|
983
|
+
} catch {
|
|
984
|
+
}
|
|
985
|
+
},
|
|
966
986
|
{ once: !0 }
|
|
967
987
|
), this.ac = new AudioContext(), this.arrays = [];
|
|
968
988
|
function i() {
|
|
@@ -983,13 +1003,23 @@ class Ge {
|
|
|
983
1003
|
);
|
|
984
1004
|
},
|
|
985
1005
|
async pull(s) {
|
|
986
|
-
if (t.readyState == "ended")
|
|
987
|
-
|
|
1006
|
+
if (t.readyState == "ended") {
|
|
1007
|
+
try {
|
|
1008
|
+
s.close();
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
988
1013
|
for (; !this.arrays.length; )
|
|
989
1014
|
if (await new Promise(
|
|
990
1015
|
(o) => this.node.port.onmessage = o
|
|
991
|
-
), t.readyState == "ended")
|
|
992
|
-
|
|
1016
|
+
), t.readyState == "ended") {
|
|
1017
|
+
try {
|
|
1018
|
+
s.close();
|
|
1019
|
+
} catch {
|
|
1020
|
+
}
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
993
1023
|
const [i] = this.arrays.shift(), n = new Float32Array(
|
|
994
1024
|
i.reduce((o, a) => o + a.length, 0)
|
|
995
1025
|
);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const{keys:_a}=Object;const{defineProperty:_b}=Object;async function _d(){return new Promise(async (r,e)=>!K()&&(await Y()||B())?r():e('Your browser does not support the MediaStreamTrack API for Insertable Streams of Media.'))}function B(){return self.MediaStreamTrackProcessor!==void 0&&self.MediaStreamTrackGenerator!==void 0||Z()}function Z(){return [self.ReadableStream,self.WritableStream,self.MediaStream,self.OffscreenCanvas,self.VideoFrame,self.AudioContext,self.AudioWorkletNode].filter(r=>r===void 0).length===0}function K(){let r=navigator.userAgent.toLowerCase();return !(navigator.vendor&&navigator.vendor.toLowerCase().indexOf('apple')>-1)||(r.indexOf('applewebkit')>-1&&r.indexOf('version')===-1)?!1:parseInt((r.split(' ').find(s=>s.includes('version/'))??'/0').split('/')[1])<26}function Y(){return new Promise(r=>{function e(){self.postMessage(typeof MediaStreamTrackProcessor<'u'&&typeof VideoTrackGenerator<'u')}let t=new Worker(`data:text/javascript,(${`${e}`})()`);t.onmessage=({data:s})=>r(s)})}const u=new WeakMap,m=new WeakMap,d=new WeakMap,P=Symbol('anyProducer'),D=Promise.resolve(),M=Symbol('listenerAdded'),R=Symbol('listenerRemoved');let I=!1,F=!1;let x=r=>typeof r=='string'||typeof r=='symbol'||typeof r=='number',H=Object.getOwnPropertyNames(h.prototype).filter(r=>r!=='constructor'),v,p=$('globals',{}),g=(r=>(r.INIT='INIT',r.FORWARD='FORWARD',r.TERMINATE='TERMINATE',r.GLOBALS_SYNC='GLOBALS_SYNC',r))(g||{}),q=$('workerized',{}),[aX,aY]=aW('metadata'),{isArray:_c}=Array;function
|
|
2
|
-
data: ${n}`)})}logIfDebugEnabled(e,t,s){(h.isDebugEnabled||this.debug.enabled)&&this.debug.logger(e,this.debug.name,t,s)}on(e,t,{signal:s}={}){k(t),e=_c(e)?e:[e];for(const n of e){y(n);let o=_(this,n);o||(o=new Set,m.get(this).set(n,o)),o.add(t),this.logIfDebugEnabled('subscribe',n,void 0),b(n)||E(this,M,{eventName:n,listener:t})}let i=()=>(this.off(e,t),s?.removeEventListener('abort',i));return s?.addEventListener('abort',i,{once:!0}),s?.aborted&&i(),i}off(e,t){k(t),e=_c(e)?e:[e];for(const s of e){y(s);let i=_(this,s);i&&(i.delete(t),i.size===0&&m.get(this).delete(s)),this.logIfDebugEnabled('unsubscribe',s,void 0),b(s)||E(this,R,{eventName:s,listener:t})}}once(e,t){if(t!==void 0&&typeof t!='function')throw TypeError('predicate must be a function');let s;let i=new Promise(n=>s=this.on(e,o=>t&&!t(o)||(s(),n(o))));return i.off=s,i}events(e){!_c(e)&&(e=[e]);for(const t of e)y(t);return V(this,e)}async emit(e,t){if(y(e),b(e)&&!I)throw TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');this.logIfDebugEnabled('emit',e,t),N(this,e,t);const s=_(this,e)??new Set,i=u.get(this),n=[...s],o=b(e)?[]:[...i];await D,await Promise.all([...n.map(a=>{if(s.has(a))return a(t)}),...o.map(a=>{if(i.has(a))return a(e,t)})])}async emitSerial(e,t){if(y(e),b(e)&&!I)throw TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');this.logIfDebugEnabled('emitSerial',e,t);const s=_(this,e)??new Set,i=u.get(this),n=[...s],o=[...i];await D;for(const a of n)s.has(a)&&await a(t);for(const a of o)i.has(a)&&await a(e,t)}onAny(e,{signal:t}={}){k(e),this.logIfDebugEnabled('subscribeAny',void 0,void 0),u.get(this).add(e),E(this,M,{listener:e});let s=()=>(this.offAny(e),t?.removeEventListener('abort',s));return t?.addEventListener('abort',s,{once:!0}),t?.aborted&&s(),s}anyEvent(){return V(this)}offAny(e){k(e),this.logIfDebugEnabled('unsubscribeAny',void 0,void 0),E(this,R,{listener:e}),u.get(this).delete(e)}clearListeners(e){!_c(e)&&(e=[e]);for(const t of e)if(this.logIfDebugEnabled('clear',t,void 0),x(t)){let s=_(this,t);s?.clear();let i=T(this,t);if(i){for(const n of i)n.finish();i.clear()}}else{u.get(this).clear();for(const[s,i] of m.get(this).entries())i.clear(),m.get(this).delete(s);for(const[s,i] of d.get(this).entries()){for(const n of i)n.finish();i.clear(),d.get(this).delete(s)}}}listenerCount(e){!_c(e)&&(e=[e]);let t=0;for(const s of e){if(x(s)){t+=u.get(this).size+(_(this,s)?.size??0)+(T(this,s)?.size??0)+(T(this)?.size??0);continue}s!==void 0&&y(s),t+=u.get(this).size;for(const i of m.get(this).values())t+=i.size;for(const i of d.get(this).values())t+=i.size}return t}bindMethods(e,t){if(typeof e!='object'||e===null)throw TypeError('`target` must be an object');t=j(t);for(const s of t){if(e[s]!==void 0)throw Error(`The property \`${s}\` already exists on \`target\``);_b(e,s,{enumerable:!1,value:this[s].bind(this)})}}}_b(h,'listenerAdded',{value:M,writable:!1,enumerable:!0,configurable:!1});_b(h,'listenerRemoved',{value:R,writable:!1,enumerable:!0,configurable:!1});var aA=Object.defineProperty,aB=(r,e,t)=>e in r?aA(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,aC=(r,e,t)=>(aB(r,`${e}`,t),t),aD=(r=>(r.automation='automation',r.test='test',r.vbc='vbc',r.video='video',r.voice='voice',r))(aD||{});const aE='hlg.tokbox.com/prod/logging/vcp_webrtc',aF='https://',aG=1e4;let aH=new Uint8Array(16);function aI(){if(!v&&(v=typeof crypto<'u'&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!v))throw Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');return v(aH)}let c=[];for(let r=0;r<256;++r)c.push((r+256).toString(16).slice(1));function aJ(r,e=0){return (c[r[e+0]]+c[r[e+1]]+c[r[e+2]]+c[r[e+3]]+'-'+c[r[e+4]]+c[r[e+5]]+'-'+c[r[e+6]]+c[r[e+7]]+'-'+c[r[e+8]]+c[r[e+9]]+'-'+c[r[e+10]]+c[r[e+11]]+c[r[e+12]]+c[r[e+13]]+c[r[e+14]]+c[r[e+15]]).toLowerCase()}const aK=typeof crypto<'u'&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Q={randomUUID:aK};function aL(r,e,t){if(Q.randomUUID&&!r)return Q.randomUUID();r=r||{};let s=r.random||(r.rng||aI)();return s[6]=s[6]&15|64,s[8]=s[8]&63|128,aJ(s)}function $(r,e){globalThis.vonage||(globalThis.vonage={}),globalThis.vonage.workerizer||(globalThis.vonage.workerizer={});let t=globalThis.vonage.workerizer;return t[r]||(t[r]=e),t[r]}function J(r){return [ImageBitmap,ReadableStream,WritableStream].some(t=>r instanceof t)}let aM=0;function aN(r,e,t,s,i){let n=aM++;return r.postMessage({id:n,type:e,functionName:t,args:s},s.filter(a=>J(a))),new Promise(a=>i?.set(n,a))}function S(r,e){const{id:t,type:s}=r,i=_c(e)?e:[e];postMessage({id:t,type:s,result:e},i.filter(n=>J(n)))}function X(){return typeof WorkerGlobalScope<'u'&&self instanceof WorkerGlobalScope}async function aO(){if(X())S({type:g.GLOBALS_SYNC},p);else{let r=[];for(const e in q){const{worker:t,resolvers:s}=q[e].workerContext;t&&r.push(aN(t,g.GLOBALS_SYNC,'',[p],s))}await Promise.all(r)}}function C(r,e){if(_c(e))e.splice(0,e.length);else if(typeof e=='object')for(const t in e)delete e[t];for(const t in r)_c(r[t])?(e[t]=[],C(r[t],e[t])):typeof r[t]=='object'?(e[t]={},C(r[t],e[t])):e[t]=r[t]}async function aP(r,e){const{functionName:t,args:s}=r;if(!e.instance)throw 'instance not initialized';if(!t)throw 'missing function name to call';if(!e.instance[t])throw `undefined function [${t}] in class ${e.instance.constructor.workerId}`;S(r,await e.instance[t](...s??[]))}let aQ=$('registeredWorkers',{});function aR(r,e){if(!r.args)throw 'Missing className while initializing worker';const[t,s]=r.args,i=aQ[t];if(i)e.instance=new i(r.args.slice(1));else throw `unknown worker class ${t}`;C(s,p),S(r,typeof e.instance!==void 0)}async function aS(r,e){const{args:t}=r;if(!e.instance)throw 'instance not initialized';let s;e.instance.terminate&&(s=await e.instance.terminate(...t??[])),S(r,s)}function aT(r){if(!r.args)throw 'Missing globals while syncing';C(r.args[0],p),S(r,{})}function aU(){let r={};onmessage=e=>{let t=e.data;switch(t.type) {case g.INIT:aR(t,r);break;case g.FORWARD:aP(t,r);break;case g.TERMINATE:aS(t,r);break;case g.GLOBALS_SYNC:aT(t);break}}}X()&&aU();function aV(r,e){return p[r]||(p[r]=e),[()=>p[r],async t=>(p[r]=t,await aO())]}function aW(r,e){return aV(r,e)}function aZ(r){aY(r)}function L(){return aX()}class z{constructor(e,t){aC(this,'uuid'),this.config=e,this.uuid=t??aL()}async send(e){var t,s,i;const{appId:n,sourceType:o}=(t=L())!=null?t:{};if(!n||!o)return'metadata missing';const a=new AbortController;return await ((i=(s=this.config)==null?void 0:s.fetch)!=null?i:fetch)(this.getUrl(),{method:'POST',headers:this.getHeaders(),body:JSON.stringify(this.buildReport(e)),signal:a.signal}),clearTimeout(setTimeout(()=>a.abort(),aG)),'success'}getUrl(){var e,t=(e=L().proxyUrl)!=null?e:aF;return t+=(t.at(-1)==='/'?'':'/')+aE,t}getHeaders(){return{'Content-Type':'application/json'}}buildReport(e){let t=L();return{guid:this.uuid,...e,applicationId:t.appId,timestamp:Date.now(),proxyUrl:t.proxyUrl,source:t.sourceType}}}const A='3.0.1-alpha.2',O=class O{};O.updates={transformer_new:'New transformer',transformer_null:'Null transformer'},O.errors={transformer_none:'No transformers provided',transformer_start:'Cannot start transformer',transformer_transform:'Cannot transform frame',transformer_flush:'Cannot flush transformer',readable_null:'Readable is null',writable_null:'Writable is null'};let f=O;function bA(r){return typeof r=='object'&&r!==null&&'message' in r&&typeof r.message=='string'}function bB(r){if(bA(r))return r;try{return Error(JSON.stringify(r))}catch{return Error(`${r}`)}}function G(r){return bB(r).message}class Pe{constructor(e,t){this.config=e,this.frameTransformedCount=0,this.frameFromSourceCount=0,this.startAt=0,this.reporter=new z(e, t)}async onFrameFromSource(){this.frameFromSourceCount++}get fps(){const{startAt:e,frameFromSourceCount:t}=this,i=(Date.now()-e)/1e3;return t/i}async onFrameTransformed(e={},t=!1){this.startAt===0&&(this.startAt=Date.now()),this.frameTransformedCount++;const{startAt:s,frameTransformedCount:i,frameFromSourceCount:n}=this,o=Date.now(),a=(o-s)/1e3,l=i/a,w=n/a;return t||this.frameTransformedCount>=this.config.loggingIntervalFrameCount?(this.frameFromSourceCount=0,this.frameTransformedCount=0,this.startAt=o,this.reporter.config=this.config,this.reporter.send({...this.config.report,variation:'QoS',fps:w,transformedFps:l,framesTransformed:i,...e})):'success'}}var bC=(r=>(r.FPS_DROP='fps_drop',r))(bC||{}),bD=(r=>(r.start='start',r.transform='transform',r.flush='flush',r))(bD||{}),U=(r=>(r.pipeline_ended='pipeline_ended',r.pipeline_ended_with_error='pipeline_ended_with_error',r.pipeline_started='pipeline_started',r.pipeline_started_with_error='pipeline_started_with_error',r.pipeline_restarted='pipeline_restarted',r.pipeline_restarted_with_error='pipeline_restarted_with_error',r))(U||{});const bE=500,bF=0.8;class Ce extends h{constructor(e,t){super(),this.reporter_=new z,this.reporterQos_=new Pe({loggingIntervalFrameCount:bE,report:{version:A}}),this.index_=t,this.transformer_=e,this.shouldStop_=!1,this.isFlashed_=!1,this.mediaTransformerQosReportStartTimestamp_=0,this.videoHeight_=0,this.videoWidth_=0,this.trackExpectedRate_=-1,this.transformerType_='Custom','getTransformerType' in e&&(this.transformerType_=e.getTransformerType()),this.report({variation:'Create'})}setTrackExpectedRate(e){this.trackExpectedRate_=e}async start(e){if(this.controller_=e,this.transformer_&&typeof this.transformer_.start=='function')try{await this.transformer_.start(e)}catch(t){this.report({message:f.errors.transformer_start,variation:'Error',error:G(t)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:t,function:'start'})}}async transform(e,t){if(this.mediaTransformerQosReportStartTimestamp_===0&&(this.mediaTransformerQosReportStartTimestamp_=Date.now()),e instanceof VideoFrame&&(this.videoHeight_=e?.displayHeight??0,this.videoWidth_=e?.displayWidth??0),this.reporterQos_.onFrameFromSource(),this.transformer_)if(this.shouldStop_)console.warn('[Pipeline] flush from transform'),e.close(),this.flush(t),t.terminate();else{try{await this.transformer_.transform?.(e,t),this.reportQos()}catch(s){this.report({message:f.errors.transformer_transform,variation:'Error',error:G(s)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:s,function:'transform'})}if(this.trackExpectedRate_!=-1&&this.trackExpectedRate_*bF>this.reporterQos_.fps){this.emit('warn',{eventMetaData:{transformerIndex:this.index_},warningType:'fps_drop',dropInfo:{requested:this.trackExpectedRate_,current:this.reporterQos_.fps}})}}}async flush(e){if(this.transformer_&&typeof this.transformer_.flush=='function'&&!this.isFlashed_){this.isFlashed_=!0;try{await this.transformer_.flush(e)}catch(t){this.report({message:f.errors.transformer_flush,variation:'Error',error:G(t)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:t,function:'flush'})}}this.reportQos(!0),this.report({variation:'Delete'})}stop(){console.log('[Pipeline] Stop stream.'),this.controller_&&(this.flush(this.controller_),this.controller_.terminate()),this.shouldStop_=!0}report(e){this.reporter_.send({version:A,action:'MediaTransformer',transformerType:this.transformerType_,...e})}reportQos(e=!1){this.reporterQos_.config={...this.reporterQos_.config},this.reporterQos_.onFrameTransformed({version:A,action:'MediaTransformer',transformerType:this.transformerType_,videoWidth:this.videoWidth_,videoHeight:this.videoHeight_},e)}}class Oe extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new Ce(e[t], t);s.on('error',i=>this.emit('error',i)),s.on('warn',i=>this.emit('warn',i)),this.transformers_.push(s)}}setTrackExpectedRate(e){this.trackExpectedRate_=e;for(let t of this.transformers_)t.setTrackExpectedRate(this.trackExpectedRate_)}async start(e,t){if(!this.transformers_||this.transformers_.length===0){console.log('[Pipeline] No transformers.');return}try{let s=e;for(let i of this.transformers_)e=e.pipeThrough(new TransformStream(i));e.pipeTo(t).then(async ()=>(console.log('[Pipeline] Setup.'),await t.abort(),await s.cancel(),this.emit('pipelineInfo','pipeline_ended'))).catch(async i=>(e.cancel().then(()=>console.log('[Pipeline] Shutting down streams after abort.')).catch(n=>console.error('[Pipeline] Error from stream transform:',n)),await t.abort(i),await s.cancel(i),this.emit('pipelineInfo','pipeline_ended_with_error')))}catch{this.emit('pipelineInfo','pipeline_started_with_error'),this.destroy();return}this.emit('pipelineInfo','pipeline_started'),console.log('[Pipeline] Pipeline started.')}async destroy(){console.log('[Pipeline] Destroying Pipeline.');for(let e of this.transformers_)e.stop()}}class We extends h{constructor(){super(),this.reporter_=new z,this.trackExpectedRate_=-1,this.report({variation:'Create'})}setTrackExpectedRate(e){this.trackExpectedRate_=e,this.pipeline_?.setTrackExpectedRate(this.trackExpectedRate_)}async transform(e,t){if(this.isTrack(e)){if(e.kind==='video'){const s=new MediaStreamTrackProcessor({track:e}),i=new VideoTrackGenerator;return this.readable_=s.readable,this.writable_=i.writable,await this.transformInternal(),i.track}throw Error('Cannot process audio tracks inside a web worker when passing a track to transform')}else return this.readable_=e,this.writable_=t,this.transformInternal()}transformInternal(){return new Promise((e,t)=>{if(!this.transformers_||this.transformers_.length===0){this.report({message:f.errors.transformer_none,variation:'Error'}),t('[MediaProcessor] Need to set transformers.');return}if(!this.readable_){this.report({variation:'Error',message:f.errors.readable_null}),t('[MediaProcessor] Readable is null.');return}if(!this.writable_){this.report({variation:'Error',message:f.errors.writable_null}),t('[MediaProcessor] Writable is null.');return}let s=!1;this.pipeline_&&(s=!0,this.pipeline_.clearListeners(),this.pipeline_.destroy()),this.pipeline_=new Oe(this.transformers_),this.pipeline_.on('warn',i=>this.emit('warn',i)),this.pipeline_.on('error',i=>this.emit('error',i)),this.pipeline_.on('pipelineInfo',i=>(s&&(i==='pipeline_started'?i=U.pipeline_restarted:i==='pipeline_started_with_error'&&(i=U.pipeline_restarted_with_error)),this.emit('pipelineInfo',i))),this.trackExpectedRate_!=-1&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_),this.pipeline_.start(this.readable_,this.writable_).then(()=>e()).catch(i=>t(i))})}setTransformers(e){return this.report({variation:'Update',message:f.updates.transformer_new}),this.transformers_=e,this.readable_&&this.writable_?this.transformInternal():Promise.resolve()}destroy(){return new Promise(e=>(this.pipeline_?.destroy(),this.report({variation:'Delete'}),e()))}report(e){this.reporter_.send({version:A,action:'MediaProcessor',...e})}isTrack(e){return 'kind' in e}}class Fe{constructor({kind:e}){if(e=='video'){const t=document.createElement('canvas'),s=t.getContext('2d',{desynchronized:!0}),i=t.captureStream().getVideoTracks()[0];return i.writable=new WritableStream({write(n){t.width=n.displayWidth,t.height=n.displayHeight,s.drawImage(n,0,0,t.width,t.height),n.close()}}),i}if(e=='audio'){const t=new AudioContext,s=t.createMediaStreamDestination(),[i]=s.stream.getAudioTracks();return i.writable=new WritableStream({async start(n){this.arrays=[];function o(){registerProcessor('mstg-shim',class extends AudioWorkletProcessor{constructor(){super(),this.arrays=[],this.arrayOffset=0,this.port.onmessage=({data:l})=>this.arrays.push(l),this.emptyArray=new Float32Array(0)}process(l,[[w]]){for(let W=0;W<w.length;W++)(!this.array||this.arrayOffset>=this.array.length)&&(this.array=this.arrays.shift()||this.emptyArray,this.arrayOffset=0),w[W]=this.array[this.arrayOffset++]||0;return!0}})}return await t.audioWorklet.addModule(`data:text/javascript,(${`${o}`})()`),this.node=new AudioWorkletNode(t, 'mstg-shim'),this.node.connect(s),i},write(n){let o=new Float32Array(n.numberOfFrames*n.numberOfChannels);n.copyTo(o,{planeIndex:0}),this.node.port.postMessage(o,[o.buffer]),n.close()}}),i}}}function bG(){return self.MediaStreamTrackGenerator||Fe}class Ge{constructor(e){let t=e.track??e;t.kind=='video'?this.readable=new ReadableStream({async start(s){t.addEventListener('ended',()=>s.close(),{once:!0}),this.video=document.createElement('video'),this.video.srcObject=new MediaStream([t]),await Promise.all([this.video.play(),new Promise(i=>this.video.onloadedmetadata=i)]),this.track=t,this.canvas=new OffscreenCanvas(this.video.videoWidth, this.video.videoHeight),this.ctx=this.canvas.getContext('2d',{desynchronized:!0}),this.t1=performance.now()},async pull(s){if(t.readyState=='ended')return s.close();let i=t.getSettings().frameRate||30;for(;performance.now()-this.t1<1e3/i;)if(await new Promise(n=>requestAnimationFrame(n)),t.readyState=='ended')return s.close();this.canvas.width=this.video.videoWidth,this.canvas.height=this.video.videoHeight,this.t1=performance.now(),this.ctx.drawImage(this.video,0,0),s.enqueue(new VideoFrame(this.canvas, {timestamp:this.t1}))}}):t.kind=='audio'&&(this.readable=new ReadableStream({async start(s){t.addEventListener('ended',()=>s.close(),{once:!0}),this.ac=new AudioContext,this.arrays=[];function i(){registerProcessor('mstp-shim',class extends AudioWorkletProcessor{process(o){return this.port.postMessage(o),!0}})}await this.ac.audioWorklet.addModule(`data:text/javascript,(${`${i}`})()`),this.node=new AudioWorkletNode(this.ac, 'mstp-shim'),this.ac.createMediaStreamSource(new MediaStream([t])).connect(this.node),this.node.port.addEventListener('message',({data:n})=>n[0][0]&&this.arrays.push(n))},async pull(s){if(t.readyState=='ended')return s.close();for(;!this.arrays.length;)if(await new Promise(o=>this.node.port.onmessage=o),t.readyState=='ended')return s.close();const[i]=this.arrays.shift(),n=new Float32Array(i.reduce((o,a)=>o+a.length,0));i.reduce((o,a)=>(n.set(a,o),o+a.length),0),s.enqueue(new AudioData({format:'f32-planar',sampleRate:this.ac.sampleRate,numberOfFrames:i[0].length,numberOfChannels:i.length,timestamp:this.ac.currentTime*1e6|0,data:n,transfer:[n.buffer]}))}}))}}function bH(){return self.MediaStreamTrackProcessor||Ge}class $e{constructor(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{let i=bH();this.processor_=new i(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{let i=bG();e.kind==='audio'?this.generator_=new i({kind:'audio'}):e.kind==='video'?this.generator_=new i({kind:'video'}):s('kind not supported')}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackGenerator failed: ${i}`),s(i)}t()})}getReadable(){return this.processor_.readable}getWriteable(){return this.generator_.writable}getProccesorTrack(){return this.generator_}}class He{constructor(e){this.mediaProcessor_=e}async setTrack(e){if(!(this.mediaProcessor_ instanceof We)&&await Y()){if(e.kind==='audio')throw Error('Cannot process audio tracks inside a web worker when passing a track to transform');return await this.mediaProcessor_.transform(e)}if(B()){let t=new $e;return await t.init(e),await this.mediaProcessor_.transform(t.getReadable(),t.getWriteable()),t.getProccesorTrack()}throw Error('MediaStreamTrack API for Insertable Streams of Media is not supported')}destroy(){return new Promise((e,t)=>this.mediaProcessor_?this.mediaProcessor_.destroy().then(()=>e()).catch(s=>t(s)):t('no processor'))}}export{bD as ErrorFunction,We as MediaProcessor,He as MediaProcessorConnector,U as PipelineInfoData,aD as VonageSourceType,bC as WarningType,L as getVonageMetadata,_d as isSupported,aZ as setVonageMetadata};
|
|
1
|
+
const{keys:_a}=Object;const{defineProperty:_b}=Object;async function _d(){return new Promise(async (r,e)=>!K()&&(await Y()||B())?r():e('Your browser does not support the MediaStreamTrack API for Insertable Streams of Media.'))}function B(){return self.MediaStreamTrackProcessor!==void 0&&self.MediaStreamTrackGenerator!==void 0||Z()}function Z(){return [self.ReadableStream,self.WritableStream,self.MediaStream,self.OffscreenCanvas,self.VideoFrame,self.AudioContext,self.AudioWorkletNode].filter(r=>r===void 0).length===0}function K(){let r=navigator.userAgent.toLowerCase();return !(navigator.vendor&&navigator.vendor.toLowerCase().indexOf('apple')>-1)||(r.indexOf('applewebkit')>-1&&r.indexOf('version')===-1)?!1:parseInt((r.split(' ').find(s=>s.includes('version/'))??'/0').split('/')[1])<26}function Y(){return new Promise(r=>{function e(){self.postMessage(typeof MediaStreamTrackProcessor<'u'&&typeof VideoTrackGenerator<'u')}let t=new Worker(`data:text/javascript,(${`${e}`})()`);t.onmessage=({data:s})=>r(s)})}const u=new WeakMap,m=new WeakMap,d=new WeakMap,P=Symbol('anyProducer'),D=Promise.resolve(),M=Symbol('listenerAdded'),R=Symbol('listenerRemoved');let I=!1,F=!1;let x=r=>typeof r=='string'||typeof r=='symbol'||typeof r=='number',H=Object.getOwnPropertyNames(h.prototype).filter(r=>r!=='constructor'),v,p=$('globals',{}),g=(r=>(r.INIT='INIT',r.FORWARD='FORWARD',r.TERMINATE='TERMINATE',r.GLOBALS_SYNC='GLOBALS_SYNC',r))(g||{}),q=$('workerized',{}),[aX,aY]=aW('metadata'),{isArray:_c}=Array;function w(r){if(!x(r))throw TypeError('`eventName` must be a string, symbol, or number')}function k(r){if(typeof r!='function')throw TypeError('listener must be a function')}function _(r,e){let t=m.get(r);if(t.has(e))return t.get(e)}function T(r,e){const t=x(e)?e:P,s=d.get(r);if(s.has(t))return s.get(t)}function N(r,e,t){let s=d.get(r);if(s.has(e))for(const i of s.get(e))i.enqueue(t);if(s.has(P)){let i=Promise.all([e,t]);for(const n of s.get(P))n.enqueue(i)}}function V(r,e){!_c(e)&&(e=[e]);let t=!1,s=()=>{},i=[];let n={enqueue(o){i.push(o),s()},finish(){t=!0,s()}};for(const o of e){let a=T(r,o);a||(a=new Set,d.get(r).set(o,a)),a.add(n)}return{async next(){return i?i.length===0?t?(i=void 0,this.next()):(await new Promise(o=>s=o),this.next()):{done:!1,value:await i.shift()}:{done:!0}},async return(o){i=void 0;for(const a of e){let l=T(r,a);l&&(l.delete(n),l.size===0&&d.get(r).delete(a))}return s(),arguments.length>0?{done:!0,value:await o}:{done:!0}},[Symbol.asyncIterator](){return this}}}function j(r){if(r===void 0)return H;if(!_c(r))throw TypeError('`methodNames` must be an array of strings');for(const e of r)if(!H.includes(e))throw typeof e!='string'?TypeError('`methodNames` element must be a string'):Error(`${e} is not Emittery method`);return r}let b=r=>r===M||r===R;function E(r,e,t){if(b(e))try{I=!0,r.emit(e,t)}finally{I=!1}}class h{static mixin(e,t){return t=j(t),s=>{if(typeof s!='function')throw TypeError('`target` must be function');for(const o of t)if(s.prototype[o]!==void 0)throw Error(`The property \`${o}\` already exists on \`target\``);function i(){return _b(this,e,{enumerable:!1,value:new h}),this[e]}_b(s.prototype,e,{enumerable:!1,get:i});let n=o=>function(...a){return this[e][o](...a)};for(const o of t)_b(s.prototype,o,{enumerable:!1,value:n(o)});return s}}static get isDebugEnabled(){if(typeof globalThis.process?.env!='object')return F;const{env:e}=globalThis.process??{env:{}};return e.DEBUG==='emittery'||e.DEBUG==='*'||F}static set isDebugEnabled(e){F=e}constructor(e={}){u.set(this,new Set),m.set(this,new Map),d.set(this,new Map),d.get(this).set(P,new Set),this.debug=e.debug??{},this.debug.enabled===void 0&&(this.debug.enabled=!1),this.debug.logger||(this.debug.logger=(t,s,i,n)=>{try{n=JSON.stringify(n)}catch{n=`Object with the following keys failed to stringify: ${_a(n).join(',')}`}(typeof i=='symbol'||typeof i=='number')&&(i=`${i}`);const o=new Date,a=`${o.getHours()}:${o.getMinutes()}:${o.getSeconds()}.${o.getMilliseconds()}`;console.log(`[${a}][emittery:${t}][${s}] Event Name: ${i}
|
|
2
|
+
data: ${n}`)})}logIfDebugEnabled(e,t,s){(h.isDebugEnabled||this.debug.enabled)&&this.debug.logger(e,this.debug.name,t,s)}on(e,t,{signal:s}={}){k(t),e=_c(e)?e:[e];for(const n of e){w(n);let o=_(this,n);o||(o=new Set,m.get(this).set(n,o)),o.add(t),this.logIfDebugEnabled('subscribe',n,void 0),b(n)||E(this,M,{eventName:n,listener:t})}let i=()=>(this.off(e,t),s?.removeEventListener('abort',i));return s?.addEventListener('abort',i,{once:!0}),s?.aborted&&i(),i}off(e,t){k(t),e=_c(e)?e:[e];for(const s of e){w(s);let i=_(this,s);i&&(i.delete(t),i.size===0&&m.get(this).delete(s)),this.logIfDebugEnabled('unsubscribe',s,void 0),b(s)||E(this,R,{eventName:s,listener:t})}}once(e,t){if(t!==void 0&&typeof t!='function')throw TypeError('predicate must be a function');let s;let i=new Promise(n=>s=this.on(e,o=>t&&!t(o)||(s(),n(o))));return i.off=s,i}events(e){!_c(e)&&(e=[e]);for(const t of e)w(t);return V(this,e)}async emit(e,t){if(w(e),b(e)&&!I)throw TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');this.logIfDebugEnabled('emit',e,t),N(this,e,t);const s=_(this,e)??new Set,i=u.get(this),n=[...s],o=b(e)?[]:[...i];await D,await Promise.all([...n.map(a=>{if(s.has(a))return a(t)}),...o.map(a=>{if(i.has(a))return a(e,t)})])}async emitSerial(e,t){if(w(e),b(e)&&!I)throw TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');this.logIfDebugEnabled('emitSerial',e,t);const s=_(this,e)??new Set,i=u.get(this),n=[...s],o=[...i];await D;for(const a of n)s.has(a)&&await a(t);for(const a of o)i.has(a)&&await a(e,t)}onAny(e,{signal:t}={}){k(e),this.logIfDebugEnabled('subscribeAny',void 0,void 0),u.get(this).add(e),E(this,M,{listener:e});let s=()=>(this.offAny(e),t?.removeEventListener('abort',s));return t?.addEventListener('abort',s,{once:!0}),t?.aborted&&s(),s}anyEvent(){return V(this)}offAny(e){k(e),this.logIfDebugEnabled('unsubscribeAny',void 0,void 0),E(this,R,{listener:e}),u.get(this).delete(e)}clearListeners(e){!_c(e)&&(e=[e]);for(const t of e)if(this.logIfDebugEnabled('clear',t,void 0),x(t)){let s=_(this,t);s?.clear();let i=T(this,t);if(i){for(const n of i)n.finish();i.clear()}}else{u.get(this).clear();for(const[s,i] of m.get(this).entries())i.clear(),m.get(this).delete(s);for(const[s,i] of d.get(this).entries()){for(const n of i)n.finish();i.clear(),d.get(this).delete(s)}}}listenerCount(e){!_c(e)&&(e=[e]);let t=0;for(const s of e){if(x(s)){t+=u.get(this).size+(_(this,s)?.size??0)+(T(this,s)?.size??0)+(T(this)?.size??0);continue}s!==void 0&&w(s),t+=u.get(this).size;for(const i of m.get(this).values())t+=i.size;for(const i of d.get(this).values())t+=i.size}return t}bindMethods(e,t){if(typeof e!='object'||e===null)throw TypeError('`target` must be an object');t=j(t);for(const s of t){if(e[s]!==void 0)throw Error(`The property \`${s}\` already exists on \`target\``);_b(e,s,{enumerable:!1,value:this[s].bind(this)})}}}_b(h,'listenerAdded',{value:M,writable:!1,enumerable:!0,configurable:!1});_b(h,'listenerRemoved',{value:R,writable:!1,enumerable:!0,configurable:!1});var aA=Object.defineProperty,aB=(r,e,t)=>e in r?aA(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,aC=(r,e,t)=>(aB(r,`${e}`,t),t),aD=(r=>(r.automation='automation',r.test='test',r.vbc='vbc',r.video='video',r.voice='voice',r))(aD||{});const aE='hlg.tokbox.com/prod/logging/vcp_webrtc',aF='https://',aG=1e4;let aH=new Uint8Array(16);function aI(){if(!v&&(v=typeof crypto<'u'&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!v))throw Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');return v(aH)}let c=[];for(let r=0;r<256;++r)c.push((r+256).toString(16).slice(1));function aJ(r,e=0){return (c[r[e+0]]+c[r[e+1]]+c[r[e+2]]+c[r[e+3]]+'-'+c[r[e+4]]+c[r[e+5]]+'-'+c[r[e+6]]+c[r[e+7]]+'-'+c[r[e+8]]+c[r[e+9]]+'-'+c[r[e+10]]+c[r[e+11]]+c[r[e+12]]+c[r[e+13]]+c[r[e+14]]+c[r[e+15]]).toLowerCase()}const aK=typeof crypto<'u'&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Q={randomUUID:aK};function aL(r,e,t){if(Q.randomUUID&&!r)return Q.randomUUID();r=r||{};let s=r.random||(r.rng||aI)();return s[6]=s[6]&15|64,s[8]=s[8]&63|128,aJ(s)}function $(r,e){globalThis.vonage||(globalThis.vonage={}),globalThis.vonage.workerizer||(globalThis.vonage.workerizer={});let t=globalThis.vonage.workerizer;return t[r]||(t[r]=e),t[r]}function J(r){return [ImageBitmap,ReadableStream,WritableStream].some(t=>r instanceof t)}let aM=0;function aN(r,e,t,s,i){let n=aM++;return r.postMessage({id:n,type:e,functionName:t,args:s},s.filter(a=>J(a))),new Promise(a=>i?.set(n,a))}function S(r,e){const{id:t,type:s}=r,i=_c(e)?e:[e];postMessage({id:t,type:s,result:e},i.filter(n=>J(n)))}function X(){return typeof WorkerGlobalScope<'u'&&self instanceof WorkerGlobalScope}async function aO(){if(X())S({type:g.GLOBALS_SYNC},p);else{let r=[];for(const e in q){const{worker:t,resolvers:s}=q[e].workerContext;t&&r.push(aN(t,g.GLOBALS_SYNC,'',[p],s))}await Promise.all(r)}}function C(r,e){if(_c(e))e.splice(0,e.length);else if(typeof e=='object')for(const t in e)delete e[t];for(const t in r)_c(r[t])?(e[t]=[],C(r[t],e[t])):typeof r[t]=='object'?(e[t]={},C(r[t],e[t])):e[t]=r[t]}async function aP(r,e){const{functionName:t,args:s}=r;if(!e.instance)throw 'instance not initialized';if(!t)throw 'missing function name to call';if(!e.instance[t])throw `undefined function [${t}] in class ${e.instance.constructor.workerId}`;S(r,await e.instance[t](...s??[]))}let aQ=$('registeredWorkers',{});function aR(r,e){if(!r.args)throw 'Missing className while initializing worker';const[t,s]=r.args,i=aQ[t];if(i)e.instance=new i(r.args.slice(1));else throw `unknown worker class ${t}`;C(s,p),S(r,typeof e.instance!==void 0)}async function aS(r,e){const{args:t}=r;if(!e.instance)throw 'instance not initialized';let s;e.instance.terminate&&(s=await e.instance.terminate(...t??[])),S(r,s)}function aT(r){if(!r.args)throw 'Missing globals while syncing';C(r.args[0],p),S(r,{})}function aU(){let r={};onmessage=e=>{let t=e.data;switch(t.type) {case g.INIT:aR(t,r);break;case g.FORWARD:aP(t,r);break;case g.TERMINATE:aS(t,r);break;case g.GLOBALS_SYNC:aT(t);break}}}X()&&aU();function aV(r,e){return p[r]||(p[r]=e),[()=>p[r],async t=>(p[r]=t,await aO())]}function aW(r,e){return aV(r,e)}function aZ(r){aY(r)}function L(){return aX()}class z{constructor(e,t){aC(this,'uuid'),this.config=e,this.uuid=t??aL()}async send(e){var t,s,i;const{appId:n,sourceType:o}=(t=L())!=null?t:{};if(!n||!o)return'metadata missing';const a=new AbortController;return await ((i=(s=this.config)==null?void 0:s.fetch)!=null?i:fetch)(this.getUrl(),{method:'POST',headers:this.getHeaders(),body:JSON.stringify(this.buildReport(e)),signal:a.signal}),clearTimeout(setTimeout(()=>a.abort(),aG)),'success'}getUrl(){var e,t=(e=L().proxyUrl)!=null?e:aF;return t+=(t.at(-1)==='/'?'':'/')+aE,t}getHeaders(){return{'Content-Type':'application/json'}}buildReport(e){let t=L();return{guid:this.uuid,...e,applicationId:t.appId,timestamp:Date.now(),proxyUrl:t.proxyUrl,source:t.sourceType}}}const A='3.0.2',O=class O{};O.updates={transformer_new:'New transformer',transformer_null:'Null transformer'},O.errors={transformer_none:'No transformers provided',transformer_start:'Cannot start transformer',transformer_transform:'Cannot transform frame',transformer_flush:'Cannot flush transformer',readable_null:'Readable is null',writable_null:'Writable is null'};let f=O;function bA(r){return typeof r=='object'&&r!==null&&'message' in r&&typeof r.message=='string'}function bB(r){if(bA(r))return r;try{return Error(JSON.stringify(r))}catch{return Error(`${r}`)}}function G(r){return bB(r).message}class Pe{constructor(e,t){this.config=e,this.frameTransformedCount=0,this.frameFromSourceCount=0,this.startAt=0,this.reporter=new z(e, t)}async onFrameFromSource(){this.frameFromSourceCount++}get fps(){const{startAt:e,frameFromSourceCount:t}=this,i=(Date.now()-e)/1e3;return t/i}async onFrameTransformed(e={},t=!1){this.startAt===0&&(this.startAt=Date.now()),this.frameTransformedCount++;const{startAt:s,frameTransformedCount:i,frameFromSourceCount:n}=this,o=Date.now(),a=(o-s)/1e3,l=i/a,y=n/a;return t||this.frameTransformedCount>=this.config.loggingIntervalFrameCount?(this.frameFromSourceCount=0,this.frameTransformedCount=0,this.startAt=o,this.reporter.config=this.config,this.reporter.send({...this.config.report,variation:'QoS',fps:y,transformedFps:l,framesTransformed:i,...e})):'success'}}var bC=(r=>(r.FPS_DROP='fps_drop',r))(bC||{}),bD=(r=>(r.start='start',r.transform='transform',r.flush='flush',r))(bD||{}),U=(r=>(r.pipeline_ended='pipeline_ended',r.pipeline_ended_with_error='pipeline_ended_with_error',r.pipeline_started='pipeline_started',r.pipeline_started_with_error='pipeline_started_with_error',r.pipeline_restarted='pipeline_restarted',r.pipeline_restarted_with_error='pipeline_restarted_with_error',r))(U||{});const bE=500,bF=0.8;class Ce extends h{constructor(e,t){super(),this.reporter_=new z,this.reporterQos_=new Pe({loggingIntervalFrameCount:bE,report:{version:A}}),this.index_=t,this.transformer_=e,this.shouldStop_=!1,this.isFlashed_=!1,this.mediaTransformerQosReportStartTimestamp_=0,this.videoHeight_=0,this.videoWidth_=0,this.trackExpectedRate_=-1,this.transformerType_='Custom','getTransformerType' in e&&(this.transformerType_=e.getTransformerType()),this.report({variation:'Create'})}setTrackExpectedRate(e){this.trackExpectedRate_=e}async start(e){if(this.controller_=e,this.transformer_&&typeof this.transformer_.start=='function')try{await this.transformer_.start(e)}catch(t){this.report({message:f.errors.transformer_start,variation:'Error',error:G(t)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:t,function:'start'})}}async transform(e,t){if(this.mediaTransformerQosReportStartTimestamp_===0&&(this.mediaTransformerQosReportStartTimestamp_=Date.now()),e instanceof VideoFrame&&(this.videoHeight_=e?.displayHeight??0,this.videoWidth_=e?.displayWidth??0),this.reporterQos_.onFrameFromSource(),this.transformer_)if(this.shouldStop_)console.warn('[Pipeline] flush from transform'),e.close(),this.flush(t),t.terminate();else{try{await this.transformer_.transform?.(e,t),this.reportQos()}catch(s){this.report({message:f.errors.transformer_transform,variation:'Error',error:G(s)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:s,function:'transform'})}if(this.trackExpectedRate_!=-1&&this.trackExpectedRate_*bF>this.reporterQos_.fps){this.emit('warn',{eventMetaData:{transformerIndex:this.index_},warningType:'fps_drop',dropInfo:{requested:this.trackExpectedRate_,current:this.reporterQos_.fps}})}}}async flush(e){if(this.transformer_&&typeof this.transformer_.flush=='function'&&!this.isFlashed_){this.isFlashed_=!0;try{await this.transformer_.flush(e)}catch(t){this.report({message:f.errors.transformer_flush,variation:'Error',error:G(t)});this.emit('error',{eventMetaData:{transformerIndex:this.index_},error:t,function:'flush'})}}this.reportQos(!0),this.report({variation:'Delete'})}stop(){console.log('[Pipeline] Stop stream.'),this.controller_&&(this.flush(this.controller_),this.controller_.terminate()),this.shouldStop_=!0}report(e){this.reporter_.send({version:A,action:'MediaTransformer',transformerType:this.transformerType_,...e})}reportQos(e=!1){this.reporterQos_.config={...this.reporterQos_.config},this.reporterQos_.onFrameTransformed({version:A,action:'MediaTransformer',transformerType:this.transformerType_,videoWidth:this.videoWidth_,videoHeight:this.videoHeight_},e)}}class Oe extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new Ce(e[t], t);s.on('error',i=>this.emit('error',i)),s.on('warn',i=>this.emit('warn',i)),this.transformers_.push(s)}}setTrackExpectedRate(e){this.trackExpectedRate_=e;for(let t of this.transformers_)t.setTrackExpectedRate(this.trackExpectedRate_)}async start(e,t){if(!this.transformers_||this.transformers_.length===0){console.log('[Pipeline] No transformers.');return}try{let s=e;for(let i of this.transformers_)e=e.pipeThrough(new TransformStream(i));e.pipeTo(t).then(async ()=>(console.log('[Pipeline] Setup.'),await t.abort(),await s.cancel(),this.emit('pipelineInfo','pipeline_ended'))).catch(async i=>(e.cancel().then(()=>console.log('[Pipeline] Shutting down streams after abort.')).catch(n=>console.error('[Pipeline] Error from stream transform:',n)),await t.abort(i),await s.cancel(i),this.emit('pipelineInfo','pipeline_ended_with_error')))}catch{this.emit('pipelineInfo','pipeline_started_with_error'),this.destroy();return}this.emit('pipelineInfo','pipeline_started'),console.log('[Pipeline] Pipeline started.')}async destroy(){console.log('[Pipeline] Destroying Pipeline.');for(let e of this.transformers_)e.stop()}}class We extends h{constructor(){super(),this.reporter_=new z,this.trackExpectedRate_=-1,this.report({variation:'Create'})}setTrackExpectedRate(e){this.trackExpectedRate_=e,this.pipeline_?.setTrackExpectedRate(this.trackExpectedRate_)}async transform(e,t){if(this.isTrack(e)){if(e.kind==='video'){const s=new MediaStreamTrackProcessor({track:e}),i=new VideoTrackGenerator;return this.readable_=s.readable,this.writable_=i.writable,await this.transformInternal(),i.track}throw Error('Cannot process audio tracks inside a web worker when passing a track to transform')}else return this.readable_=e,this.writable_=t,this.transformInternal()}transformInternal(){return new Promise((e,t)=>{if(!this.transformers_||this.transformers_.length===0){this.report({message:f.errors.transformer_none,variation:'Error'}),t('[MediaProcessor] Need to set transformers.');return}if(!this.readable_){this.report({variation:'Error',message:f.errors.readable_null}),t('[MediaProcessor] Readable is null.');return}if(!this.writable_){this.report({variation:'Error',message:f.errors.writable_null}),t('[MediaProcessor] Writable is null.');return}let s=!1;this.pipeline_&&(s=!0,this.pipeline_.clearListeners(),this.pipeline_.destroy()),this.pipeline_=new Oe(this.transformers_),this.pipeline_.on('warn',i=>this.emit('warn',i)),this.pipeline_.on('error',i=>this.emit('error',i)),this.pipeline_.on('pipelineInfo',i=>(s&&(i==='pipeline_started'?i=U.pipeline_restarted:i==='pipeline_started_with_error'&&(i=U.pipeline_restarted_with_error)),this.emit('pipelineInfo',i))),this.trackExpectedRate_!=-1&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_),this.pipeline_.start(this.readable_,this.writable_).then(()=>e()).catch(i=>t(i))})}setTransformers(e){return this.report({variation:'Update',message:f.updates.transformer_new}),this.transformers_=e,this.readable_&&this.writable_?this.transformInternal():Promise.resolve()}destroy(){return new Promise(e=>(this.pipeline_?.destroy(),this.report({variation:'Delete'}),e()))}report(e){this.reporter_.send({version:A,action:'MediaProcessor',...e})}isTrack(e){return 'kind' in e}}class Fe{constructor({kind:e}){if(e=='video'){const t=document.createElement('canvas'),s=t.getContext('2d',{desynchronized:!0}),i=t.captureStream().getVideoTracks()[0];return i.writable=new WritableStream({write(n){t.width=n.displayWidth,t.height=n.displayHeight,s.drawImage(n,0,0,t.width,t.height),n.close()}}),i}if(e=='audio'){const t=new AudioContext,s=t.createMediaStreamDestination(),[i]=s.stream.getAudioTracks();return i.writable=new WritableStream({async start(n){this.arrays=[];function o(){registerProcessor('mstg-shim',class extends AudioWorkletProcessor{constructor(){super(),this.arrays=[],this.arrayOffset=0,this.port.onmessage=({data:l})=>this.arrays.push(l),this.emptyArray=new Float32Array(0)}process(l,[[y]]){for(let W=0;W<y.length;W++)(!this.array||this.arrayOffset>=this.array.length)&&(this.array=this.arrays.shift()||this.emptyArray,this.arrayOffset=0),y[W]=this.array[this.arrayOffset++]||0;return!0}})}return await t.audioWorklet.addModule(`data:text/javascript,(${`${o}`})()`),this.node=new AudioWorkletNode(t, 'mstg-shim'),this.node.connect(s),i},write(n){let o=new Float32Array(n.numberOfFrames*n.numberOfChannels);n.copyTo(o,{planeIndex:0}),this.node.port.postMessage(o,[o.buffer]),n.close()}}),i}}}function bG(){return self.MediaStreamTrackGenerator||Fe}class Ge{constructor(e){let t=e.track??e;t.kind=='video'?this.readable=new ReadableStream({async start(s){t.addEventListener('ended',()=>{try{s.close()}catch{}},{once:!0}),this.video=document.createElement('video'),this.video.srcObject=new MediaStream([t]),await Promise.all([this.video.play(),new Promise(i=>this.video.onloadedmetadata=i)]),this.track=t,this.canvas=new OffscreenCanvas(this.video.videoWidth, this.video.videoHeight),this.ctx=this.canvas.getContext('2d',{desynchronized:!0}),this.t1=performance.now()},async pull(s){if(t.readyState=='ended'){try{s.close()}catch{}return}let i=t.getSettings().frameRate||30;for(;performance.now()-this.t1<1e3/i;)if(await new Promise(n=>requestAnimationFrame(n)),t.readyState=='ended'){try{s.close()}catch{}return}this.canvas.width=this.video.videoWidth,this.canvas.height=this.video.videoHeight,this.t1=performance.now(),this.ctx.drawImage(this.video,0,0),s.enqueue(new VideoFrame(this.canvas, {timestamp:this.t1}))}}):t.kind=='audio'&&(this.readable=new ReadableStream({async start(s){t.addEventListener('ended',()=>{try{s.close()}catch{}},{once:!0}),this.ac=new AudioContext,this.arrays=[];function i(){registerProcessor('mstp-shim',class extends AudioWorkletProcessor{process(o){return this.port.postMessage(o),!0}})}await this.ac.audioWorklet.addModule(`data:text/javascript,(${`${i}`})()`),this.node=new AudioWorkletNode(this.ac, 'mstp-shim'),this.ac.createMediaStreamSource(new MediaStream([t])).connect(this.node),this.node.port.addEventListener('message',({data:n})=>n[0][0]&&this.arrays.push(n))},async pull(s){if(t.readyState=='ended'){try{s.close()}catch{}return}for(;!this.arrays.length;)if(await new Promise(o=>this.node.port.onmessage=o),t.readyState=='ended'){try{s.close()}catch{}return}const[i]=this.arrays.shift(),n=new Float32Array(i.reduce((o,a)=>o+a.length,0));i.reduce((o,a)=>(n.set(a,o),o+a.length),0),s.enqueue(new AudioData({format:'f32-planar',sampleRate:this.ac.sampleRate,numberOfFrames:i[0].length,numberOfChannels:i.length,timestamp:this.ac.currentTime*1e6|0,data:n,transfer:[n.buffer]}))}}))}}function bH(){return self.MediaStreamTrackProcessor||Ge}class $e{constructor(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{let i=bH();this.processor_=new i(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{let i=bG();e.kind==='audio'?this.generator_=new i({kind:'audio'}):e.kind==='video'?this.generator_=new i({kind:'video'}):s('kind not supported')}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackGenerator failed: ${i}`),s(i)}t()})}getReadable(){return this.processor_.readable}getWriteable(){return this.generator_.writable}getProccesorTrack(){return this.generator_}}class He{constructor(e){this.mediaProcessor_=e}async setTrack(e){if(!(this.mediaProcessor_ instanceof We)&&await Y()){if(e.kind==='audio')throw Error('Cannot process audio tracks inside a web worker when passing a track to transform');return await this.mediaProcessor_.transform(e)}if(B()){let t=new $e;return await t.init(e),await this.mediaProcessor_.transform(t.getReadable(),t.getWriteable()),t.getProccesorTrack()}throw Error('MediaStreamTrack API for Insertable Streams of Media is not supported')}destroy(){return new Promise((e,t)=>this.mediaProcessor_?this.mediaProcessor_.destroy().then(()=>e()).catch(s=>t(s)):t('no processor'))}}export{bD as ErrorFunction,We as MediaProcessor,He as MediaProcessorConnector,U as PipelineInfoData,aD as VonageSourceType,bC as WarningType,L as getVonageMetadata,_d as isSupported,aZ as setVonageMetadata};
|
|
@@ -37,7 +37,7 @@ function Y() {
|
|
|
37
37
|
const u = /* @__PURE__ */ new WeakMap(), m = /* @__PURE__ */ new WeakMap(), d = /* @__PURE__ */ new WeakMap(), P = /* @__PURE__ */ Symbol("anyProducer"), D = Promise.resolve(), M = /* @__PURE__ */ Symbol("listenerAdded"), R = /* @__PURE__ */ Symbol("listenerRemoved");
|
|
38
38
|
let I = !1, F = !1;
|
|
39
39
|
const x = (r) => typeof r == "string" || typeof r == "symbol" || typeof r == "number";
|
|
40
|
-
function
|
|
40
|
+
function w(r) {
|
|
41
41
|
if (!x(r))
|
|
42
42
|
throw new TypeError("`eventName` must be a string, symbol, or number");
|
|
43
43
|
}
|
|
@@ -180,7 +180,7 @@ class h {
|
|
|
180
180
|
on(e, t, { signal: s } = {}) {
|
|
181
181
|
k(t), e = Array.isArray(e) ? e : [e];
|
|
182
182
|
for (const n of e) {
|
|
183
|
-
|
|
183
|
+
w(n);
|
|
184
184
|
let o = _(this, n);
|
|
185
185
|
o || (o = /* @__PURE__ */ new Set(), m.get(this).set(n, o)), o.add(t), this.logIfDebugEnabled("subscribe", n, void 0), b(n) || E(this, M, { eventName: n, listener: t });
|
|
186
186
|
}
|
|
@@ -192,7 +192,7 @@ class h {
|
|
|
192
192
|
off(e, t) {
|
|
193
193
|
k(t), e = Array.isArray(e) ? e : [e];
|
|
194
194
|
for (const s of e) {
|
|
195
|
-
|
|
195
|
+
w(s);
|
|
196
196
|
const i = _(this, s);
|
|
197
197
|
i && (i.delete(t), i.size === 0 && m.get(this).delete(s)), this.logIfDebugEnabled("unsubscribe", s, void 0), b(s) || E(this, R, { eventName: s, listener: t });
|
|
198
198
|
}
|
|
@@ -211,11 +211,11 @@ class h {
|
|
|
211
211
|
events(e) {
|
|
212
212
|
e = Array.isArray(e) ? e : [e];
|
|
213
213
|
for (const t of e)
|
|
214
|
-
|
|
214
|
+
w(t);
|
|
215
215
|
return V(this, e);
|
|
216
216
|
}
|
|
217
217
|
async emit(e, t) {
|
|
218
|
-
if (
|
|
218
|
+
if (w(e), b(e) && !I)
|
|
219
219
|
throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");
|
|
220
220
|
this.logIfDebugEnabled("emit", e, t), N(this, e, t);
|
|
221
221
|
const s = _(this, e) ?? /* @__PURE__ */ new Set(), i = u.get(this), n = [...s], o = b(e) ? [] : [...i];
|
|
@@ -231,7 +231,7 @@ class h {
|
|
|
231
231
|
]);
|
|
232
232
|
}
|
|
233
233
|
async emitSerial(e, t) {
|
|
234
|
-
if (
|
|
234
|
+
if (w(e), b(e) && !I)
|
|
235
235
|
throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");
|
|
236
236
|
this.logIfDebugEnabled("emitSerial", e, t);
|
|
237
237
|
const s = _(this, e) ?? /* @__PURE__ */ new Set(), i = u.get(this), n = [...s], o = [...i];
|
|
@@ -285,7 +285,7 @@ class h {
|
|
|
285
285
|
t += u.get(this).size + (_(this, s)?.size ?? 0) + (T(this, s)?.size ?? 0) + (T(this)?.size ?? 0);
|
|
286
286
|
continue;
|
|
287
287
|
}
|
|
288
|
-
s !== void 0 &&
|
|
288
|
+
s !== void 0 && w(s), t += u.get(this).size;
|
|
289
289
|
for (const i of m.get(this).values())
|
|
290
290
|
t += i.size;
|
|
291
291
|
for (const i of d.get(this).values())
|
|
@@ -428,7 +428,7 @@ async function me(r, e) {
|
|
|
428
428
|
S(r, await e.instance[t](...s ?? []));
|
|
429
429
|
}
|
|
430
430
|
const ge = $("registeredWorkers", {});
|
|
431
|
-
function
|
|
431
|
+
function ye(r, e) {
|
|
432
432
|
if (!r.args)
|
|
433
433
|
throw "Missing className while initializing worker";
|
|
434
434
|
const [t, s] = r.args, i = ge[t];
|
|
@@ -438,7 +438,7 @@ function we(r, e) {
|
|
|
438
438
|
throw `unknown worker class ${t}`;
|
|
439
439
|
C(s, p), S(r, typeof e.instance !== void 0);
|
|
440
440
|
}
|
|
441
|
-
async function
|
|
441
|
+
async function we(r, e) {
|
|
442
442
|
const { args: t } = r;
|
|
443
443
|
if (!e.instance)
|
|
444
444
|
throw "instance not initialized";
|
|
@@ -456,13 +456,13 @@ function be() {
|
|
|
456
456
|
const t = e.data;
|
|
457
457
|
switch (t.type) {
|
|
458
458
|
case g.INIT:
|
|
459
|
-
|
|
459
|
+
ye(t, r);
|
|
460
460
|
break;
|
|
461
461
|
case g.FORWARD:
|
|
462
462
|
me(t, r);
|
|
463
463
|
break;
|
|
464
464
|
case g.TERMINATE:
|
|
465
|
-
|
|
465
|
+
we(t, r);
|
|
466
466
|
break;
|
|
467
467
|
case g.GLOBALS_SYNC:
|
|
468
468
|
_e(t);
|
|
@@ -528,7 +528,7 @@ class z {
|
|
|
528
528
|
};
|
|
529
529
|
}
|
|
530
530
|
}
|
|
531
|
-
const A = "3.0.
|
|
531
|
+
const A = "3.0.2", O = class O {
|
|
532
532
|
};
|
|
533
533
|
O.updates = {
|
|
534
534
|
transformer_new: "New transformer",
|
|
@@ -569,11 +569,11 @@ class Pe {
|
|
|
569
569
|
}
|
|
570
570
|
async onFrameTransformed(e = {}, t = !1) {
|
|
571
571
|
this.startAt === 0 && (this.startAt = Date.now()), this.frameTransformedCount++;
|
|
572
|
-
const { startAt: s, frameTransformedCount: i, frameFromSourceCount: n } = this, o = Date.now(), a = (o - s) / 1e3, l = i / a,
|
|
572
|
+
const { startAt: s, frameTransformedCount: i, frameFromSourceCount: n } = this, o = Date.now(), a = (o - s) / 1e3, l = i / a, y = n / a;
|
|
573
573
|
return t || this.frameTransformedCount >= this.config.loggingIntervalFrameCount ? (this.frameFromSourceCount = 0, this.frameTransformedCount = 0, this.startAt = o, this.reporter.config = this.config, this.reporter.send({
|
|
574
574
|
...this.config.report,
|
|
575
575
|
variation: "QoS",
|
|
576
|
-
fps:
|
|
576
|
+
fps: y,
|
|
577
577
|
transformedFps: l,
|
|
578
578
|
framesTransformed: i,
|
|
579
579
|
...e
|
|
@@ -899,9 +899,9 @@ class Fe {
|
|
|
899
899
|
constructor() {
|
|
900
900
|
super(), this.arrays = [], this.arrayOffset = 0, this.port.onmessage = ({ data: l }) => this.arrays.push(l), this.emptyArray = new Float32Array(0);
|
|
901
901
|
}
|
|
902
|
-
process(l, [[
|
|
903
|
-
for (let W = 0; W <
|
|
904
|
-
(!this.array || this.arrayOffset >= this.array.length) && (this.array = this.arrays.shift() || this.emptyArray, this.arrayOffset = 0),
|
|
902
|
+
process(l, [[y]]) {
|
|
903
|
+
for (let W = 0; W < y.length; W++)
|
|
904
|
+
(!this.array || this.arrayOffset >= this.array.length) && (this.array = this.arrays.shift() || this.emptyArray, this.arrayOffset = 0), y[W] = this.array[this.arrayOffset++] || 0;
|
|
905
905
|
return !0;
|
|
906
906
|
}
|
|
907
907
|
}
|
|
@@ -931,7 +931,12 @@ class Ge {
|
|
|
931
931
|
async start(s) {
|
|
932
932
|
t.addEventListener(
|
|
933
933
|
"ended",
|
|
934
|
-
() =>
|
|
934
|
+
() => {
|
|
935
|
+
try {
|
|
936
|
+
s.close();
|
|
937
|
+
} catch {
|
|
938
|
+
}
|
|
939
|
+
},
|
|
935
940
|
{ once: !0 }
|
|
936
941
|
), this.video = document.createElement("video"), this.video.srcObject = new MediaStream([t]), await Promise.all([
|
|
937
942
|
this.video.play(),
|
|
@@ -946,12 +951,22 @@ class Ge {
|
|
|
946
951
|
}), this.t1 = performance.now();
|
|
947
952
|
},
|
|
948
953
|
async pull(s) {
|
|
949
|
-
if (t.readyState == "ended")
|
|
950
|
-
|
|
954
|
+
if (t.readyState == "ended") {
|
|
955
|
+
try {
|
|
956
|
+
s.close();
|
|
957
|
+
} catch {
|
|
958
|
+
}
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
951
961
|
const i = t.getSettings().frameRate || 30;
|
|
952
962
|
for (; performance.now() - this.t1 < 1e3 / i; )
|
|
953
|
-
if (await new Promise((n) => requestAnimationFrame(n)), t.readyState == "ended")
|
|
954
|
-
|
|
963
|
+
if (await new Promise((n) => requestAnimationFrame(n)), t.readyState == "ended") {
|
|
964
|
+
try {
|
|
965
|
+
s.close();
|
|
966
|
+
} catch {
|
|
967
|
+
}
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
955
970
|
this.canvas.width = this.video.videoWidth, this.canvas.height = this.video.videoHeight, this.t1 = performance.now(), this.ctx.drawImage(this.video, 0, 0), s.enqueue(
|
|
956
971
|
new VideoFrame(this.canvas, {
|
|
957
972
|
timestamp: this.t1
|
|
@@ -962,7 +977,12 @@ class Ge {
|
|
|
962
977
|
async start(s) {
|
|
963
978
|
t.addEventListener(
|
|
964
979
|
"ended",
|
|
965
|
-
() =>
|
|
980
|
+
() => {
|
|
981
|
+
try {
|
|
982
|
+
s.close();
|
|
983
|
+
} catch {
|
|
984
|
+
}
|
|
985
|
+
},
|
|
966
986
|
{ once: !0 }
|
|
967
987
|
), this.ac = new AudioContext(), this.arrays = [];
|
|
968
988
|
function i() {
|
|
@@ -983,13 +1003,23 @@ class Ge {
|
|
|
983
1003
|
);
|
|
984
1004
|
},
|
|
985
1005
|
async pull(s) {
|
|
986
|
-
if (t.readyState == "ended")
|
|
987
|
-
|
|
1006
|
+
if (t.readyState == "ended") {
|
|
1007
|
+
try {
|
|
1008
|
+
s.close();
|
|
1009
|
+
} catch {
|
|
1010
|
+
}
|
|
1011
|
+
return;
|
|
1012
|
+
}
|
|
988
1013
|
for (; !this.arrays.length; )
|
|
989
1014
|
if (await new Promise(
|
|
990
1015
|
(o) => this.node.port.onmessage = o
|
|
991
|
-
), t.readyState == "ended")
|
|
992
|
-
|
|
1016
|
+
), t.readyState == "ended") {
|
|
1017
|
+
try {
|
|
1018
|
+
s.close();
|
|
1019
|
+
} catch {
|
|
1020
|
+
}
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
993
1023
|
const [i] = this.arrays.shift(), n = new Float32Array(
|
|
994
1024
|
i.reduce((o, a) => o + a.length, 0)
|
|
995
1025
|
);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(function(l,S){typeof exports=="object"&&typeof module<"u"?S(exports):typeof define=="function"&&define.amd?define(["exports"],S):(l=typeof globalThis<"u"?globalThis:l||self,S(l["media-processor"]={}))})(this,(function(l){"use strict";async function S(){return new Promise(async(r,e)=>{!ie()&&(await H()||j())?r():e("Your browser does not support the MediaStreamTrack API for Insertable Streams of Media.")})}function j(){return self.MediaStreamTrackProcessor!==void 0&&self.MediaStreamTrackGenerator!==void 0||se()}function se(){return[self.ReadableStream,self.WritableStream,self.MediaStream,self.OffscreenCanvas,self.VideoFrame,self.AudioContext,self.AudioWorkletNode].filter(r=>r===void 0).length===0}function ie(){const r=navigator.userAgent.toLowerCase();return!(navigator.vendor&&navigator.vendor.toLowerCase().indexOf("apple")>-1&&!(r.indexOf("applewebkit")>-1&&r.indexOf("version")===-1))?!1:parseInt((r.split(" ").find(s=>s.includes("version/"))??"/0").split("/")[1])<26}function H(){return new Promise(r=>{function e(){self.postMessage(typeof MediaStreamTrackProcessor<"u"&&typeof VideoTrackGenerator<"u")}const t=new Worker(`data:text/javascript,(${e.toString()})()`);t.onmessage=({data:s})=>r(s)})}const p=new WeakMap,g=new WeakMap,f=new WeakMap,v=Symbol("anyProducer"),Q=Promise.resolve(),P=Symbol("listenerAdded"),M=Symbol("listenerRemoved");let A=!1,G=!1;const R=r=>typeof r=="string"||typeof r=="symbol"||typeof r=="number";function w(r){if(!R(r))throw new TypeError("`eventName` must be a string, symbol, or number")}function I(r){if(typeof r!="function")throw new TypeError("listener must be a function")}function
|
|
2
|
-
data: ${n}`)})}logIfDebugEnabled(e,t,s){(h.isDebugEnabled||this.debug.enabled)&&this.debug.logger(e,this.debug.name,t,s)}on(e,t,{signal:s}={}){I(t),e=Array.isArray(e)?e:[e];for(const n of e){w(n);let o=b(this,n);o||(o=new Set,g.get(this).set(n,o)),o.add(t),this.logIfDebugEnabled("subscribe",n,void 0),_(n)||C(this,P,{eventName:n,listener:t})}const i=()=>{this.off(e,t),s?.removeEventListener("abort",i)};return s?.addEventListener("abort",i,{once:!0}),s?.aborted&&i(),i}off(e,t){I(t),e=Array.isArray(e)?e:[e];for(const s of e){w(s);const i=b(this,s);i&&(i.delete(t),i.size===0&&g.get(this).delete(s)),this.logIfDebugEnabled("unsubscribe",s,void 0),_(s)||C(this,M,{eventName:s,listener:t})}}once(e,t){if(t!==void 0&&typeof t!="function")throw new TypeError("predicate must be a function");let s;const i=new Promise(n=>{s=this.on(e,o=>{t&&!t(o)||(s(),n(o))})});return i.off=s,i}events(e){e=Array.isArray(e)?e:[e];for(const t of e)w(t);return q(this,e)}async emit(e,t){if(w(e),_(e)&&!A)throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");this.logIfDebugEnabled("emit",e,t),ne(this,e,t);const s=b(this,e)??new Set,i=p.get(this),n=[...s],o=_(e)?[]:[...i];await Q,await Promise.all([...n.map(async a=>{if(s.has(a))return a(t)}),...o.map(async a=>{if(i.has(a))return a(e,t)})])}async emitSerial(e,t){if(w(e),_(e)&&!A)throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");this.logIfDebugEnabled("emitSerial",e,t);const s=b(this,e)??new Set,i=p.get(this),n=[...s],o=[...i];await Q;for(const a of n)s.has(a)&&await a(t);for(const a of o)i.has(a)&&await a(e,t)}onAny(e,{signal:t}={}){I(e),this.logIfDebugEnabled("subscribeAny",void 0,void 0),p.get(this).add(e),C(this,P,{listener:e});const s=()=>{this.offAny(e),t?.removeEventListener("abort",s)};return t?.addEventListener("abort",s,{once:!0}),t?.aborted&&s(),s}anyEvent(){return q(this)}offAny(e){I(e),this.logIfDebugEnabled("unsubscribeAny",void 0,void 0),C(this,M,{listener:e}),p.get(this).delete(e)}clearListeners(e){e=Array.isArray(e)?e:[e];for(const t of e)if(this.logIfDebugEnabled("clear",t,void 0),R(t)){const s=b(this,t);s&&s.clear();const i=k(this,t);if(i){for(const n of i)n.finish();i.clear()}}else{p.get(this).clear();for(const[s,i]of g.get(this).entries())i.clear(),g.get(this).delete(s);for(const[s,i]of f.get(this).entries()){for(const n of i)n.finish();i.clear(),f.get(this).delete(s)}}}listenerCount(e){e=Array.isArray(e)?e:[e];let t=0;for(const s of e){if(R(s)){t+=p.get(this).size+(b(this,s)?.size??0)+(k(this,s)?.size??0)+(k(this)?.size??0);continue}s!==void 0&&w(s),t+=p.get(this).size;for(const i of g.get(this).values())t+=i.size;for(const i of f.get(this).values())t+=i.size}return t}bindMethods(e,t){if(typeof e!="object"||e===null)throw new TypeError("`target` must be an object");t=B(t);for(const s of t){if(e[s]!==void 0)throw new Error(`The property \`${s}\` already exists on \`target\``);Object.defineProperty(e,s,{enumerable:!1,value:this[s].bind(this)})}}}const Y=Object.getOwnPropertyNames(h.prototype).filter(r=>r!=="constructor");Object.defineProperty(h,"listenerAdded",{value:P,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(h,"listenerRemoved",{value:M,writable:!1,enumerable:!0,configurable:!1});var oe=Object.defineProperty,ae=(r,e,t)=>e in r?oe(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,ce=(r,e,t)=>(ae(r,e+"",t),t),J=(r=>(r.automation="automation",r.test="test",r.vbc="vbc",r.video="video",r.voice="voice",r))(J||{});const le="hlg.tokbox.com/prod/logging/vcp_webrtc",de="https://",fe=1e4;let x;const he=new Uint8Array(16);function ue(){if(!x&&(x=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!x))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return x(he)}const c=[];for(let r=0;r<256;++r)c.push((r+256).toString(16).slice(1));function pe(r,e=0){return(c[r[e+0]]+c[r[e+1]]+c[r[e+2]]+c[r[e+3]]+"-"+c[r[e+4]]+c[r[e+5]]+"-"+c[r[e+6]]+c[r[e+7]]+"-"+c[r[e+8]]+c[r[e+9]]+"-"+c[r[e+10]]+c[r[e+11]]+c[r[e+12]]+c[r[e+13]]+c[r[e+14]]+c[r[e+15]]).toLowerCase()}const X={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function me(r,e,t){if(X.randomUUID&&!r)return X.randomUUID();r=r||{};const s=r.random||(r.rng||ue)();return s[6]=s[6]&15|64,s[8]=s[8]&63|128,pe(s)}function $(r,e){globalThis.vonage||(globalThis.vonage={}),globalThis.vonage.workerizer||(globalThis.vonage.workerizer={});let t=globalThis.vonage.workerizer;return t[r]||(t[r]=e),t[r]}const m=$("globals",{});var y=(r=>(r.INIT="INIT",r.FORWARD="FORWARD",r.TERMINATE="TERMINATE",r.GLOBALS_SYNC="GLOBALS_SYNC",r))(y||{});function Z(r){return[ImageBitmap,ReadableStream,WritableStream].some(t=>r instanceof t)}let ge=0;function ye(r,e,t,s,i){const n=ge++;return r.postMessage({id:n,type:e,functionName:t,args:s},s.filter(a=>Z(a))),new Promise(a=>{i?.set(n,a)})}function E(r,e){const{id:t,type:s}=r,i=Array.isArray(e)?e:[e];postMessage({id:t,type:s,result:e},i.filter(n=>Z(n)))}const K=$("workerized",{});function N(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}async function we(){if(N())E({type:y.GLOBALS_SYNC},m);else{const r=[];for(const e in K){const{worker:t,resolvers:s}=K[e].workerContext;t&&r.push(ye(t,y.GLOBALS_SYNC,"",[m],s))}await Promise.all(r)}}function O(r,e){if(Array.isArray(e))e.splice(0,e.length);else if(typeof e=="object")for(const t in e)delete e[t];for(const t in r)Array.isArray(r[t])?(e[t]=[],O(r[t],e[t])):typeof r[t]=="object"?(e[t]={},O(r[t],e[t])):e[t]=r[t]}async function be(r,e){const{functionName:t,args:s}=r;if(!e.instance)throw"instance not initialized";if(!t)throw"missing function name to call";if(!e.instance[t])throw`undefined function [${t}] in class ${e.instance.constructor.workerId}`;E(r,await e.instance[t](...s??[]))}const _e=$("registeredWorkers",{});function Te(r,e){if(!r.args)throw"Missing className while initializing worker";const[t,s]=r.args,i=_e[t];if(i)e.instance=new i(r.args.slice(1));else throw`unknown worker class ${t}`;O(s,m),E(r,typeof e.instance!==void 0)}async function Se(r,e){const{args:t}=r;if(!e.instance)throw"instance not initialized";let s;e.instance.terminate&&(s=await e.instance.terminate(...t??[])),E(r,s)}function ke(r){if(!r.args)throw"Missing globals while syncing";O(r.args[0],m),E(r,{})}function Ee(){const r={};onmessage=async e=>{const t=e.data;switch(t.type){case y.INIT:Te(t,r);break;case y.FORWARD:be(t,r);break;case y.TERMINATE:Se(t,r);break;case y.GLOBALS_SYNC:ke(t);break}}}N()&&Ee();function ve(r,e){return m[r]||(m[r]=e),[()=>m[r],async t=>{m[r]=t,await we()}]}function Pe(r,e){return ve(r,e)}const[Me,Ae]=Pe("metadata");function Re(r){Ae(r)}function W(){return Me()}class D{constructor(e,t){ce(this,"uuid"),this.config=e,this.uuid=t??me()}async send(e){var t,s,i;const{appId:n,sourceType:o}=(t=W())!=null?t:{};if(!n||!o)return"metadata missing";const a=new AbortController,d=setTimeout(()=>a.abort(),fe);return await((i=(s=this.config)==null?void 0:s.fetch)!=null?i:fetch)(this.getUrl(),{method:"POST",headers:this.getHeaders(),body:JSON.stringify(this.buildReport(e)),signal:a.signal}),clearTimeout(d),"success"}getUrl(){var e;let t=(e=W().proxyUrl)!=null?e:de;return t+=(t.at(-1)==="/"?"":"/")+le,t}getHeaders(){return{"Content-Type":"application/json"}}buildReport(e){const t=W();return{guid:this.uuid,...e,applicationId:t.appId,timestamp:Date.now(),proxyUrl:t.proxyUrl,source:t.sourceType}}}const F="3.0.1-alpha.2",U=class U{};U.updates={transformer_new:"New transformer",transformer_null:"Null transformer"},U.errors={transformer_none:"No transformers provided",transformer_start:"Cannot start transformer",transformer_transform:"Cannot transform frame",transformer_flush:"Cannot flush transformer",readable_null:"Readable is null",writable_null:"Writable is null"};let u=U;function Ie(r){return typeof r=="object"&&r!==null&&"message"in r&&typeof r.message=="string"}function Ce(r){if(Ie(r))return r;try{return new Error(JSON.stringify(r))}catch{return new Error(String(r))}}function V(r){return Ce(r).message}class xe{constructor(e,t){this.config=e,this.frameTransformedCount=0,this.frameFromSourceCount=0,this.startAt=0,this.reporter=new D(e,t)}async onFrameFromSource(){this.frameFromSourceCount++}get fps(){const{startAt:e,frameFromSourceCount:t}=this,i=(Date.now()-e)/1e3;return t/i}async onFrameTransformed(e={},t=!1){this.startAt===0&&(this.startAt=Date.now()),this.frameTransformedCount++;const{startAt:s,frameTransformedCount:i,frameFromSourceCount:n}=this,o=Date.now(),a=(o-s)/1e3,d=i/a,T=n/a;return t||this.frameTransformedCount>=this.config.loggingIntervalFrameCount?(this.frameFromSourceCount=0,this.frameTransformedCount=0,this.startAt=o,this.reporter.config=this.config,this.reporter.send({...this.config.report,variation:"QoS",fps:T,transformedFps:d,framesTransformed:i,...e})):"success"}}var ee=(r=>(r.FPS_DROP="fps_drop",r))(ee||{}),te=(r=>(r.start="start",r.transform="transform",r.flush="flush",r))(te||{}),L=(r=>(r.pipeline_ended="pipeline_ended",r.pipeline_ended_with_error="pipeline_ended_with_error",r.pipeline_started="pipeline_started",r.pipeline_started_with_error="pipeline_started_with_error",r.pipeline_restarted="pipeline_restarted",r.pipeline_restarted_with_error="pipeline_restarted_with_error",r))(L||{});const Oe=500,We=.8;class Fe extends h{constructor(e,t){super(),this.reporter_=new D,this.reporterQos_=new xe({loggingIntervalFrameCount:Oe,report:{version:F}}),this.index_=t,this.transformer_=e,this.shouldStop_=!1,this.isFlashed_=!1,this.mediaTransformerQosReportStartTimestamp_=0,this.videoHeight_=0,this.videoWidth_=0,this.trackExpectedRate_=-1,this.transformerType_="Custom","getTransformerType"in e&&(this.transformerType_=e.getTransformerType()),this.report({variation:"Create"})}setTrackExpectedRate(e){this.trackExpectedRate_=e}async start(e){if(this.controller_=e,this.transformer_&&typeof this.transformer_.start=="function")try{await this.transformer_.start(e)}catch(t){this.report({message:u.errors.transformer_start,variation:"Error",error:V(t)});const s={eventMetaData:{transformerIndex:this.index_},error:t,function:"start"};this.emit("error",s)}}async transform(e,t){if(this.mediaTransformerQosReportStartTimestamp_===0&&(this.mediaTransformerQosReportStartTimestamp_=Date.now()),e instanceof VideoFrame&&(this.videoHeight_=e?.displayHeight??0,this.videoWidth_=e?.displayWidth??0),this.reporterQos_.onFrameFromSource(),this.transformer_)if(this.shouldStop_)console.warn("[Pipeline] flush from transform"),e.close(),this.flush(t),t.terminate();else{try{await this.transformer_.transform?.(e,t),this.reportQos()}catch(s){this.report({message:u.errors.transformer_transform,variation:"Error",error:V(s)});const i={eventMetaData:{transformerIndex:this.index_},error:s,function:"transform"};this.emit("error",i)}if(this.trackExpectedRate_!=-1&&this.trackExpectedRate_*We>this.reporterQos_.fps){const s={eventMetaData:{transformerIndex:this.index_},warningType:"fps_drop",dropInfo:{requested:this.trackExpectedRate_,current:this.reporterQos_.fps}};this.emit("warn",s)}}}async flush(e){if(this.transformer_&&typeof this.transformer_.flush=="function"&&!this.isFlashed_){this.isFlashed_=!0;try{await this.transformer_.flush(e)}catch(t){this.report({message:u.errors.transformer_flush,variation:"Error",error:V(t)});const s={eventMetaData:{transformerIndex:this.index_},error:t,function:"flush"};this.emit("error",s)}}this.reportQos(!0),this.report({variation:"Delete"})}stop(){console.log("[Pipeline] Stop stream."),this.controller_&&(this.flush(this.controller_),this.controller_.terminate()),this.shouldStop_=!0}report(e){this.reporter_.send({version:F,action:"MediaTransformer",transformerType:this.transformerType_,...e})}reportQos(e=!1){this.reporterQos_.config={...this.reporterQos_.config},this.reporterQos_.onFrameTransformed({version:F,action:"MediaTransformer",transformerType:this.transformerType_,videoWidth:this.videoWidth_,videoHeight:this.videoHeight_},e)}}class Le extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new Fe(e[t],t);s.on("error",i=>{this.emit("error",i)}),s.on("warn",i=>{this.emit("warn",i)}),this.transformers_.push(s)}}setTrackExpectedRate(e){this.trackExpectedRate_=e;for(let t of this.transformers_)t.setTrackExpectedRate(this.trackExpectedRate_)}async start(e,t){if(!this.transformers_||this.transformers_.length===0){console.log("[Pipeline] No transformers.");return}try{let s=e;for(let i of this.transformers_)e=e.pipeThrough(new TransformStream(i));e.pipeTo(t).then(async()=>{console.log("[Pipeline] Setup."),await t.abort(),await s.cancel(),this.emit("pipelineInfo","pipeline_ended")}).catch(async i=>{e.cancel().then(()=>{console.log("[Pipeline] Shutting down streams after abort.")}).catch(n=>{console.error("[Pipeline] Error from stream transform:",n)}),await t.abort(i),await s.cancel(i),this.emit("pipelineInfo","pipeline_ended_with_error")})}catch{this.emit("pipelineInfo","pipeline_started_with_error"),this.destroy();return}this.emit("pipelineInfo","pipeline_started"),console.log("[Pipeline] Pipeline started.")}async destroy(){console.log("[Pipeline] Destroying Pipeline.");for(let e of this.transformers_)e.stop()}}class re extends h{constructor(){super(),this.reporter_=new D,this.trackExpectedRate_=-1,this.report({variation:"Create"})}setTrackExpectedRate(e){this.trackExpectedRate_=e,this.pipeline_&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_)}async transform(e,t){if(this.isTrack(e)){if(e.kind==="video"){const s=new MediaStreamTrackProcessor({track:e}),i=new VideoTrackGenerator;return this.readable_=s.readable,this.writable_=i.writable,await this.transformInternal(),i.track}throw Error("Cannot process audio tracks inside a web worker when passing a track to transform")}else return this.readable_=e,this.writable_=t,this.transformInternal()}transformInternal(){return new Promise(async(e,t)=>{if(!this.transformers_||this.transformers_.length===0){this.report({message:u.errors.transformer_none,variation:"Error"}),t("[MediaProcessor] Need to set transformers.");return}if(!this.readable_){this.report({variation:"Error",message:u.errors.readable_null}),t("[MediaProcessor] Readable is null.");return}if(!this.writable_){this.report({variation:"Error",message:u.errors.writable_null}),t("[MediaProcessor] Writable is null.");return}let s=!1;this.pipeline_&&(s=!0,this.pipeline_.clearListeners(),this.pipeline_.destroy()),this.pipeline_=new Le(this.transformers_),this.pipeline_.on("warn",i=>{this.emit("warn",i)}),this.pipeline_.on("error",i=>{this.emit("error",i)}),this.pipeline_.on("pipelineInfo",i=>{s&&(i==="pipeline_started"?i=L.pipeline_restarted:i==="pipeline_started_with_error"&&(i=L.pipeline_restarted_with_error)),this.emit("pipelineInfo",i)}),this.trackExpectedRate_!=-1&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_),this.pipeline_.start(this.readable_,this.writable_).then(()=>{e()}).catch(i=>{t(i)})})}setTransformers(e){return this.report({variation:"Update",message:u.updates.transformer_new}),this.transformers_=e,this.readable_&&this.writable_?this.transformInternal():Promise.resolve()}destroy(){return new Promise(async e=>{this.pipeline_&&this.pipeline_.destroy(),this.report({variation:"Delete"}),e()})}report(e){this.reporter_.send({version:F,action:"MediaProcessor",...e})}isTrack(e){return"kind"in e}}class Ue{constructor({kind:e}){if(e=="video"){const t=document.createElement("canvas"),s=t.getContext("2d",{desynchronized:!0}),i=t.captureStream().getVideoTracks()[0];return i.writable=new WritableStream({write(n){t.width=n.displayWidth,t.height=n.displayHeight,s.drawImage(n,0,0,t.width,t.height),n.close()}}),i}else if(e=="audio"){const t=new AudioContext,s=t.createMediaStreamDestination(),[i]=s.stream.getAudioTracks();return i.writable=new WritableStream({async start(n){this.arrays=[];function o(){registerProcessor("mstg-shim",class extends AudioWorkletProcessor{constructor(){super(),this.arrays=[],this.arrayOffset=0,this.port.onmessage=({data:d})=>this.arrays.push(d),this.emptyArray=new Float32Array(0)}process(d,[[T]]){for(let z=0;z<T.length;z++)(!this.array||this.arrayOffset>=this.array.length)&&(this.array=this.arrays.shift()||this.emptyArray,this.arrayOffset=0),T[z]=this.array[this.arrayOffset++]||0;return!0}})}return await t.audioWorklet.addModule(`data:text/javascript,(${o.toString()})()`),this.node=new AudioWorkletNode(t,"mstg-shim"),this.node.connect(s),i},write(n){const o=new Float32Array(n.numberOfFrames*n.numberOfChannels);n.copyTo(o,{planeIndex:0}),this.node.port.postMessage(o,[o.buffer]),n.close()}}),i}}}function Ge(){return self.MediaStreamTrackGenerator?self.MediaStreamTrackGenerator:Ue}class $e{constructor(e){const t=e.track??e;t.kind=="video"?this.readable=new ReadableStream({async start(s){t.addEventListener("ended",()=>s.close(),{once:!0}),this.video=document.createElement("video"),this.video.srcObject=new MediaStream([t]),await Promise.all([this.video.play(),new Promise(i=>this.video.onloadedmetadata=i)]),this.track=t,this.canvas=new OffscreenCanvas(this.video.videoWidth,this.video.videoHeight),this.ctx=this.canvas.getContext("2d",{desynchronized:!0}),this.t1=performance.now()},async pull(s){if(t.readyState=="ended")return s.close();const i=t.getSettings().frameRate||30;for(;performance.now()-this.t1<1e3/i;)if(await new Promise(n=>requestAnimationFrame(n)),t.readyState=="ended")return s.close();this.canvas.width=this.video.videoWidth,this.canvas.height=this.video.videoHeight,this.t1=performance.now(),this.ctx.drawImage(this.video,0,0),s.enqueue(new VideoFrame(this.canvas,{timestamp:this.t1}))}}):t.kind=="audio"&&(this.readable=new ReadableStream({async start(s){t.addEventListener("ended",()=>s.close(),{once:!0}),this.ac=new AudioContext,this.arrays=[];function i(){registerProcessor("mstp-shim",class extends AudioWorkletProcessor{process(o){return this.port.postMessage(o),!0}})}await this.ac.audioWorklet.addModule(`data:text/javascript,(${i.toString()})()`),this.node=new AudioWorkletNode(this.ac,"mstp-shim"),this.ac.createMediaStreamSource(new MediaStream([t])).connect(this.node),this.node.port.addEventListener("message",({data:n})=>n[0][0]&&this.arrays.push(n))},async pull(s){if(t.readyState=="ended")return s.close();for(;!this.arrays.length;)if(await new Promise(o=>this.node.port.onmessage=o),t.readyState=="ended")return s.close();const[i]=this.arrays.shift(),n=new Float32Array(i.reduce((o,a)=>o+a.length,0));i.reduce((o,a)=>(n.set(a,o),o+a.length),0),s.enqueue(new AudioData({format:"f32-planar",sampleRate:this.ac.sampleRate,numberOfFrames:i[0].length,numberOfChannels:i.length,timestamp:this.ac.currentTime*1e6|0,data:n,transfer:[n.buffer]}))}}))}}function De(){return self.MediaStreamTrackProcessor?self.MediaStreamTrackProcessor:$e}class Ve{constructor(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{const i=De();this.processor_=new i(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{const i=Ge();e.kind==="audio"?this.generator_=new i({kind:"audio"}):e.kind==="video"?this.generator_=new i({kind:"video"}):s("kind not supported")}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackGenerator failed: ${i}`),s(i)}t()})}getReadable(){return this.processor_.readable}getWriteable(){return this.generator_.writable}getProccesorTrack(){return this.generator_}}class ze{constructor(e){this.mediaProcessor_=e}async setTrack(e){if(!(this.mediaProcessor_ instanceof re)&&await H()){if(e.kind==="audio")throw Error("Cannot process audio tracks inside a web worker when passing a track to transform");return await this.mediaProcessor_.transform(e)}if(j()){const t=new Ve;return await t.init(e),await this.mediaProcessor_.transform(t.getReadable(),t.getWriteable()),t.getProccesorTrack()}throw Error("MediaStreamTrack API for Insertable Streams of Media is not supported")}destroy(){return new Promise((e,t)=>{this.mediaProcessor_?this.mediaProcessor_.destroy().then(()=>{e()}).catch(s=>{t(s)}):t("no processor")})}}l.ErrorFunction=te,l.MediaProcessor=re,l.MediaProcessorConnector=ze,l.PipelineInfoData=L,l.VonageSourceType=J,l.WarningType=ee,l.getVonageMetadata=W,l.isSupported=S,l.setVonageMetadata=Re,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(function(l,S){typeof exports=="object"&&typeof module<"u"?S(exports):typeof define=="function"&&define.amd?define(["exports"],S):(l=typeof globalThis<"u"?globalThis:l||self,S(l["media-processor"]={}))})(this,(function(l){"use strict";async function S(){return new Promise(async(r,e)=>{!ie()&&(await H()||j())?r():e("Your browser does not support the MediaStreamTrack API for Insertable Streams of Media.")})}function j(){return self.MediaStreamTrackProcessor!==void 0&&self.MediaStreamTrackGenerator!==void 0||se()}function se(){return[self.ReadableStream,self.WritableStream,self.MediaStream,self.OffscreenCanvas,self.VideoFrame,self.AudioContext,self.AudioWorkletNode].filter(r=>r===void 0).length===0}function ie(){const r=navigator.userAgent.toLowerCase();return!(navigator.vendor&&navigator.vendor.toLowerCase().indexOf("apple")>-1&&!(r.indexOf("applewebkit")>-1&&r.indexOf("version")===-1))?!1:parseInt((r.split(" ").find(s=>s.includes("version/"))??"/0").split("/")[1])<26}function H(){return new Promise(r=>{function e(){self.postMessage(typeof MediaStreamTrackProcessor<"u"&&typeof VideoTrackGenerator<"u")}const t=new Worker(`data:text/javascript,(${e.toString()})()`);t.onmessage=({data:s})=>r(s)})}const p=new WeakMap,g=new WeakMap,f=new WeakMap,v=Symbol("anyProducer"),Q=Promise.resolve(),P=Symbol("listenerAdded"),M=Symbol("listenerRemoved");let A=!1,G=!1;const R=r=>typeof r=="string"||typeof r=="symbol"||typeof r=="number";function w(r){if(!R(r))throw new TypeError("`eventName` must be a string, symbol, or number")}function I(r){if(typeof r!="function")throw new TypeError("listener must be a function")}function _(r,e){const t=g.get(r);if(t.has(e))return t.get(e)}function k(r,e){const t=R(e)?e:v,s=f.get(r);if(s.has(t))return s.get(t)}function ne(r,e,t){const s=f.get(r);if(s.has(e))for(const i of s.get(e))i.enqueue(t);if(s.has(v)){const i=Promise.all([e,t]);for(const n of s.get(v))n.enqueue(i)}}function q(r,e){e=Array.isArray(e)?e:[e];let t=!1,s=()=>{},i=[];const n={enqueue(o){i.push(o),s()},finish(){t=!0,s()}};for(const o of e){let a=k(r,o);a||(a=new Set,f.get(r).set(o,a)),a.add(n)}return{async next(){return i?i.length===0?t?(i=void 0,this.next()):(await new Promise(o=>{s=o}),this.next()):{done:!1,value:await i.shift()}:{done:!0}},async return(o){i=void 0;for(const a of e){const d=k(r,a);d&&(d.delete(n),d.size===0&&f.get(r).delete(a))}return s(),arguments.length>0?{done:!0,value:await o}:{done:!0}},[Symbol.asyncIterator](){return this}}}function B(r){if(r===void 0)return Y;if(!Array.isArray(r))throw new TypeError("`methodNames` must be an array of strings");for(const e of r)if(!Y.includes(e))throw typeof e!="string"?new TypeError("`methodNames` element must be a string"):new Error(`${e} is not Emittery method`);return r}const b=r=>r===P||r===M;function C(r,e,t){if(b(e))try{A=!0,r.emit(e,t)}finally{A=!1}}class h{static mixin(e,t){return t=B(t),s=>{if(typeof s!="function")throw new TypeError("`target` must be function");for(const o of t)if(s.prototype[o]!==void 0)throw new Error(`The property \`${o}\` already exists on \`target\``);function i(){return Object.defineProperty(this,e,{enumerable:!1,value:new h}),this[e]}Object.defineProperty(s.prototype,e,{enumerable:!1,get:i});const n=o=>function(...a){return this[e][o](...a)};for(const o of t)Object.defineProperty(s.prototype,o,{enumerable:!1,value:n(o)});return s}}static get isDebugEnabled(){if(typeof globalThis.process?.env!="object")return G;const{env:e}=globalThis.process??{env:{}};return e.DEBUG==="emittery"||e.DEBUG==="*"||G}static set isDebugEnabled(e){G=e}constructor(e={}){p.set(this,new Set),g.set(this,new Map),f.set(this,new Map),f.get(this).set(v,new Set),this.debug=e.debug??{},this.debug.enabled===void 0&&(this.debug.enabled=!1),this.debug.logger||(this.debug.logger=(t,s,i,n)=>{try{n=JSON.stringify(n)}catch{n=`Object with the following keys failed to stringify: ${Object.keys(n).join(",")}`}(typeof i=="symbol"||typeof i=="number")&&(i=i.toString());const o=new Date,a=`${o.getHours()}:${o.getMinutes()}:${o.getSeconds()}.${o.getMilliseconds()}`;console.log(`[${a}][emittery:${t}][${s}] Event Name: ${i}
|
|
2
|
+
data: ${n}`)})}logIfDebugEnabled(e,t,s){(h.isDebugEnabled||this.debug.enabled)&&this.debug.logger(e,this.debug.name,t,s)}on(e,t,{signal:s}={}){I(t),e=Array.isArray(e)?e:[e];for(const n of e){w(n);let o=_(this,n);o||(o=new Set,g.get(this).set(n,o)),o.add(t),this.logIfDebugEnabled("subscribe",n,void 0),b(n)||C(this,P,{eventName:n,listener:t})}const i=()=>{this.off(e,t),s?.removeEventListener("abort",i)};return s?.addEventListener("abort",i,{once:!0}),s?.aborted&&i(),i}off(e,t){I(t),e=Array.isArray(e)?e:[e];for(const s of e){w(s);const i=_(this,s);i&&(i.delete(t),i.size===0&&g.get(this).delete(s)),this.logIfDebugEnabled("unsubscribe",s,void 0),b(s)||C(this,M,{eventName:s,listener:t})}}once(e,t){if(t!==void 0&&typeof t!="function")throw new TypeError("predicate must be a function");let s;const i=new Promise(n=>{s=this.on(e,o=>{t&&!t(o)||(s(),n(o))})});return i.off=s,i}events(e){e=Array.isArray(e)?e:[e];for(const t of e)w(t);return q(this,e)}async emit(e,t){if(w(e),b(e)&&!A)throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");this.logIfDebugEnabled("emit",e,t),ne(this,e,t);const s=_(this,e)??new Set,i=p.get(this),n=[...s],o=b(e)?[]:[...i];await Q,await Promise.all([...n.map(async a=>{if(s.has(a))return a(t)}),...o.map(async a=>{if(i.has(a))return a(e,t)})])}async emitSerial(e,t){if(w(e),b(e)&&!A)throw new TypeError("`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`");this.logIfDebugEnabled("emitSerial",e,t);const s=_(this,e)??new Set,i=p.get(this),n=[...s],o=[...i];await Q;for(const a of n)s.has(a)&&await a(t);for(const a of o)i.has(a)&&await a(e,t)}onAny(e,{signal:t}={}){I(e),this.logIfDebugEnabled("subscribeAny",void 0,void 0),p.get(this).add(e),C(this,P,{listener:e});const s=()=>{this.offAny(e),t?.removeEventListener("abort",s)};return t?.addEventListener("abort",s,{once:!0}),t?.aborted&&s(),s}anyEvent(){return q(this)}offAny(e){I(e),this.logIfDebugEnabled("unsubscribeAny",void 0,void 0),C(this,M,{listener:e}),p.get(this).delete(e)}clearListeners(e){e=Array.isArray(e)?e:[e];for(const t of e)if(this.logIfDebugEnabled("clear",t,void 0),R(t)){const s=_(this,t);s&&s.clear();const i=k(this,t);if(i){for(const n of i)n.finish();i.clear()}}else{p.get(this).clear();for(const[s,i]of g.get(this).entries())i.clear(),g.get(this).delete(s);for(const[s,i]of f.get(this).entries()){for(const n of i)n.finish();i.clear(),f.get(this).delete(s)}}}listenerCount(e){e=Array.isArray(e)?e:[e];let t=0;for(const s of e){if(R(s)){t+=p.get(this).size+(_(this,s)?.size??0)+(k(this,s)?.size??0)+(k(this)?.size??0);continue}s!==void 0&&w(s),t+=p.get(this).size;for(const i of g.get(this).values())t+=i.size;for(const i of f.get(this).values())t+=i.size}return t}bindMethods(e,t){if(typeof e!="object"||e===null)throw new TypeError("`target` must be an object");t=B(t);for(const s of t){if(e[s]!==void 0)throw new Error(`The property \`${s}\` already exists on \`target\``);Object.defineProperty(e,s,{enumerable:!1,value:this[s].bind(this)})}}}const Y=Object.getOwnPropertyNames(h.prototype).filter(r=>r!=="constructor");Object.defineProperty(h,"listenerAdded",{value:P,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(h,"listenerRemoved",{value:M,writable:!1,enumerable:!0,configurable:!1});var oe=Object.defineProperty,ae=(r,e,t)=>e in r?oe(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,ce=(r,e,t)=>(ae(r,e+"",t),t),J=(r=>(r.automation="automation",r.test="test",r.vbc="vbc",r.video="video",r.voice="voice",r))(J||{});const le="hlg.tokbox.com/prod/logging/vcp_webrtc",de="https://",fe=1e4;let x;const he=new Uint8Array(16);function ue(){if(!x&&(x=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!x))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return x(he)}const c=[];for(let r=0;r<256;++r)c.push((r+256).toString(16).slice(1));function pe(r,e=0){return(c[r[e+0]]+c[r[e+1]]+c[r[e+2]]+c[r[e+3]]+"-"+c[r[e+4]]+c[r[e+5]]+"-"+c[r[e+6]]+c[r[e+7]]+"-"+c[r[e+8]]+c[r[e+9]]+"-"+c[r[e+10]]+c[r[e+11]]+c[r[e+12]]+c[r[e+13]]+c[r[e+14]]+c[r[e+15]]).toLowerCase()}const X={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function me(r,e,t){if(X.randomUUID&&!r)return X.randomUUID();r=r||{};const s=r.random||(r.rng||ue)();return s[6]=s[6]&15|64,s[8]=s[8]&63|128,pe(s)}function $(r,e){globalThis.vonage||(globalThis.vonage={}),globalThis.vonage.workerizer||(globalThis.vonage.workerizer={});let t=globalThis.vonage.workerizer;return t[r]||(t[r]=e),t[r]}const m=$("globals",{});var y=(r=>(r.INIT="INIT",r.FORWARD="FORWARD",r.TERMINATE="TERMINATE",r.GLOBALS_SYNC="GLOBALS_SYNC",r))(y||{});function Z(r){return[ImageBitmap,ReadableStream,WritableStream].some(t=>r instanceof t)}let ge=0;function ye(r,e,t,s,i){const n=ge++;return r.postMessage({id:n,type:e,functionName:t,args:s},s.filter(a=>Z(a))),new Promise(a=>{i?.set(n,a)})}function E(r,e){const{id:t,type:s}=r,i=Array.isArray(e)?e:[e];postMessage({id:t,type:s,result:e},i.filter(n=>Z(n)))}const K=$("workerized",{});function N(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}async function we(){if(N())E({type:y.GLOBALS_SYNC},m);else{const r=[];for(const e in K){const{worker:t,resolvers:s}=K[e].workerContext;t&&r.push(ye(t,y.GLOBALS_SYNC,"",[m],s))}await Promise.all(r)}}function O(r,e){if(Array.isArray(e))e.splice(0,e.length);else if(typeof e=="object")for(const t in e)delete e[t];for(const t in r)Array.isArray(r[t])?(e[t]=[],O(r[t],e[t])):typeof r[t]=="object"?(e[t]={},O(r[t],e[t])):e[t]=r[t]}async function _e(r,e){const{functionName:t,args:s}=r;if(!e.instance)throw"instance not initialized";if(!t)throw"missing function name to call";if(!e.instance[t])throw`undefined function [${t}] in class ${e.instance.constructor.workerId}`;E(r,await e.instance[t](...s??[]))}const be=$("registeredWorkers",{});function Te(r,e){if(!r.args)throw"Missing className while initializing worker";const[t,s]=r.args,i=be[t];if(i)e.instance=new i(r.args.slice(1));else throw`unknown worker class ${t}`;O(s,m),E(r,typeof e.instance!==void 0)}async function Se(r,e){const{args:t}=r;if(!e.instance)throw"instance not initialized";let s;e.instance.terminate&&(s=await e.instance.terminate(...t??[])),E(r,s)}function ke(r){if(!r.args)throw"Missing globals while syncing";O(r.args[0],m),E(r,{})}function Ee(){const r={};onmessage=async e=>{const t=e.data;switch(t.type){case y.INIT:Te(t,r);break;case y.FORWARD:_e(t,r);break;case y.TERMINATE:Se(t,r);break;case y.GLOBALS_SYNC:ke(t);break}}}N()&&Ee();function ve(r,e){return m[r]||(m[r]=e),[()=>m[r],async t=>{m[r]=t,await we()}]}function Pe(r,e){return ve(r,e)}const[Me,Ae]=Pe("metadata");function Re(r){Ae(r)}function W(){return Me()}class D{constructor(e,t){ce(this,"uuid"),this.config=e,this.uuid=t??me()}async send(e){var t,s,i;const{appId:n,sourceType:o}=(t=W())!=null?t:{};if(!n||!o)return"metadata missing";const a=new AbortController,d=setTimeout(()=>a.abort(),fe);return await((i=(s=this.config)==null?void 0:s.fetch)!=null?i:fetch)(this.getUrl(),{method:"POST",headers:this.getHeaders(),body:JSON.stringify(this.buildReport(e)),signal:a.signal}),clearTimeout(d),"success"}getUrl(){var e;let t=(e=W().proxyUrl)!=null?e:de;return t+=(t.at(-1)==="/"?"":"/")+le,t}getHeaders(){return{"Content-Type":"application/json"}}buildReport(e){const t=W();return{guid:this.uuid,...e,applicationId:t.appId,timestamp:Date.now(),proxyUrl:t.proxyUrl,source:t.sourceType}}}const F="3.0.2",U=class U{};U.updates={transformer_new:"New transformer",transformer_null:"Null transformer"},U.errors={transformer_none:"No transformers provided",transformer_start:"Cannot start transformer",transformer_transform:"Cannot transform frame",transformer_flush:"Cannot flush transformer",readable_null:"Readable is null",writable_null:"Writable is null"};let u=U;function Ie(r){return typeof r=="object"&&r!==null&&"message"in r&&typeof r.message=="string"}function Ce(r){if(Ie(r))return r;try{return new Error(JSON.stringify(r))}catch{return new Error(String(r))}}function V(r){return Ce(r).message}class xe{constructor(e,t){this.config=e,this.frameTransformedCount=0,this.frameFromSourceCount=0,this.startAt=0,this.reporter=new D(e,t)}async onFrameFromSource(){this.frameFromSourceCount++}get fps(){const{startAt:e,frameFromSourceCount:t}=this,i=(Date.now()-e)/1e3;return t/i}async onFrameTransformed(e={},t=!1){this.startAt===0&&(this.startAt=Date.now()),this.frameTransformedCount++;const{startAt:s,frameTransformedCount:i,frameFromSourceCount:n}=this,o=Date.now(),a=(o-s)/1e3,d=i/a,T=n/a;return t||this.frameTransformedCount>=this.config.loggingIntervalFrameCount?(this.frameFromSourceCount=0,this.frameTransformedCount=0,this.startAt=o,this.reporter.config=this.config,this.reporter.send({...this.config.report,variation:"QoS",fps:T,transformedFps:d,framesTransformed:i,...e})):"success"}}var ee=(r=>(r.FPS_DROP="fps_drop",r))(ee||{}),te=(r=>(r.start="start",r.transform="transform",r.flush="flush",r))(te||{}),L=(r=>(r.pipeline_ended="pipeline_ended",r.pipeline_ended_with_error="pipeline_ended_with_error",r.pipeline_started="pipeline_started",r.pipeline_started_with_error="pipeline_started_with_error",r.pipeline_restarted="pipeline_restarted",r.pipeline_restarted_with_error="pipeline_restarted_with_error",r))(L||{});const Oe=500,We=.8;class Fe extends h{constructor(e,t){super(),this.reporter_=new D,this.reporterQos_=new xe({loggingIntervalFrameCount:Oe,report:{version:F}}),this.index_=t,this.transformer_=e,this.shouldStop_=!1,this.isFlashed_=!1,this.mediaTransformerQosReportStartTimestamp_=0,this.videoHeight_=0,this.videoWidth_=0,this.trackExpectedRate_=-1,this.transformerType_="Custom","getTransformerType"in e&&(this.transformerType_=e.getTransformerType()),this.report({variation:"Create"})}setTrackExpectedRate(e){this.trackExpectedRate_=e}async start(e){if(this.controller_=e,this.transformer_&&typeof this.transformer_.start=="function")try{await this.transformer_.start(e)}catch(t){this.report({message:u.errors.transformer_start,variation:"Error",error:V(t)});const s={eventMetaData:{transformerIndex:this.index_},error:t,function:"start"};this.emit("error",s)}}async transform(e,t){if(this.mediaTransformerQosReportStartTimestamp_===0&&(this.mediaTransformerQosReportStartTimestamp_=Date.now()),e instanceof VideoFrame&&(this.videoHeight_=e?.displayHeight??0,this.videoWidth_=e?.displayWidth??0),this.reporterQos_.onFrameFromSource(),this.transformer_)if(this.shouldStop_)console.warn("[Pipeline] flush from transform"),e.close(),this.flush(t),t.terminate();else{try{await this.transformer_.transform?.(e,t),this.reportQos()}catch(s){this.report({message:u.errors.transformer_transform,variation:"Error",error:V(s)});const i={eventMetaData:{transformerIndex:this.index_},error:s,function:"transform"};this.emit("error",i)}if(this.trackExpectedRate_!=-1&&this.trackExpectedRate_*We>this.reporterQos_.fps){const s={eventMetaData:{transformerIndex:this.index_},warningType:"fps_drop",dropInfo:{requested:this.trackExpectedRate_,current:this.reporterQos_.fps}};this.emit("warn",s)}}}async flush(e){if(this.transformer_&&typeof this.transformer_.flush=="function"&&!this.isFlashed_){this.isFlashed_=!0;try{await this.transformer_.flush(e)}catch(t){this.report({message:u.errors.transformer_flush,variation:"Error",error:V(t)});const s={eventMetaData:{transformerIndex:this.index_},error:t,function:"flush"};this.emit("error",s)}}this.reportQos(!0),this.report({variation:"Delete"})}stop(){console.log("[Pipeline] Stop stream."),this.controller_&&(this.flush(this.controller_),this.controller_.terminate()),this.shouldStop_=!0}report(e){this.reporter_.send({version:F,action:"MediaTransformer",transformerType:this.transformerType_,...e})}reportQos(e=!1){this.reporterQos_.config={...this.reporterQos_.config},this.reporterQos_.onFrameTransformed({version:F,action:"MediaTransformer",transformerType:this.transformerType_,videoWidth:this.videoWidth_,videoHeight:this.videoHeight_},e)}}class Le extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new Fe(e[t],t);s.on("error",i=>{this.emit("error",i)}),s.on("warn",i=>{this.emit("warn",i)}),this.transformers_.push(s)}}setTrackExpectedRate(e){this.trackExpectedRate_=e;for(let t of this.transformers_)t.setTrackExpectedRate(this.trackExpectedRate_)}async start(e,t){if(!this.transformers_||this.transformers_.length===0){console.log("[Pipeline] No transformers.");return}try{let s=e;for(let i of this.transformers_)e=e.pipeThrough(new TransformStream(i));e.pipeTo(t).then(async()=>{console.log("[Pipeline] Setup."),await t.abort(),await s.cancel(),this.emit("pipelineInfo","pipeline_ended")}).catch(async i=>{e.cancel().then(()=>{console.log("[Pipeline] Shutting down streams after abort.")}).catch(n=>{console.error("[Pipeline] Error from stream transform:",n)}),await t.abort(i),await s.cancel(i),this.emit("pipelineInfo","pipeline_ended_with_error")})}catch{this.emit("pipelineInfo","pipeline_started_with_error"),this.destroy();return}this.emit("pipelineInfo","pipeline_started"),console.log("[Pipeline] Pipeline started.")}async destroy(){console.log("[Pipeline] Destroying Pipeline.");for(let e of this.transformers_)e.stop()}}class re extends h{constructor(){super(),this.reporter_=new D,this.trackExpectedRate_=-1,this.report({variation:"Create"})}setTrackExpectedRate(e){this.trackExpectedRate_=e,this.pipeline_&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_)}async transform(e,t){if(this.isTrack(e)){if(e.kind==="video"){const s=new MediaStreamTrackProcessor({track:e}),i=new VideoTrackGenerator;return this.readable_=s.readable,this.writable_=i.writable,await this.transformInternal(),i.track}throw Error("Cannot process audio tracks inside a web worker when passing a track to transform")}else return this.readable_=e,this.writable_=t,this.transformInternal()}transformInternal(){return new Promise(async(e,t)=>{if(!this.transformers_||this.transformers_.length===0){this.report({message:u.errors.transformer_none,variation:"Error"}),t("[MediaProcessor] Need to set transformers.");return}if(!this.readable_){this.report({variation:"Error",message:u.errors.readable_null}),t("[MediaProcessor] Readable is null.");return}if(!this.writable_){this.report({variation:"Error",message:u.errors.writable_null}),t("[MediaProcessor] Writable is null.");return}let s=!1;this.pipeline_&&(s=!0,this.pipeline_.clearListeners(),this.pipeline_.destroy()),this.pipeline_=new Le(this.transformers_),this.pipeline_.on("warn",i=>{this.emit("warn",i)}),this.pipeline_.on("error",i=>{this.emit("error",i)}),this.pipeline_.on("pipelineInfo",i=>{s&&(i==="pipeline_started"?i=L.pipeline_restarted:i==="pipeline_started_with_error"&&(i=L.pipeline_restarted_with_error)),this.emit("pipelineInfo",i)}),this.trackExpectedRate_!=-1&&this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_),this.pipeline_.start(this.readable_,this.writable_).then(()=>{e()}).catch(i=>{t(i)})})}setTransformers(e){return this.report({variation:"Update",message:u.updates.transformer_new}),this.transformers_=e,this.readable_&&this.writable_?this.transformInternal():Promise.resolve()}destroy(){return new Promise(async e=>{this.pipeline_&&this.pipeline_.destroy(),this.report({variation:"Delete"}),e()})}report(e){this.reporter_.send({version:F,action:"MediaProcessor",...e})}isTrack(e){return"kind"in e}}class Ue{constructor({kind:e}){if(e=="video"){const t=document.createElement("canvas"),s=t.getContext("2d",{desynchronized:!0}),i=t.captureStream().getVideoTracks()[0];return i.writable=new WritableStream({write(n){t.width=n.displayWidth,t.height=n.displayHeight,s.drawImage(n,0,0,t.width,t.height),n.close()}}),i}else if(e=="audio"){const t=new AudioContext,s=t.createMediaStreamDestination(),[i]=s.stream.getAudioTracks();return i.writable=new WritableStream({async start(n){this.arrays=[];function o(){registerProcessor("mstg-shim",class extends AudioWorkletProcessor{constructor(){super(),this.arrays=[],this.arrayOffset=0,this.port.onmessage=({data:d})=>this.arrays.push(d),this.emptyArray=new Float32Array(0)}process(d,[[T]]){for(let z=0;z<T.length;z++)(!this.array||this.arrayOffset>=this.array.length)&&(this.array=this.arrays.shift()||this.emptyArray,this.arrayOffset=0),T[z]=this.array[this.arrayOffset++]||0;return!0}})}return await t.audioWorklet.addModule(`data:text/javascript,(${o.toString()})()`),this.node=new AudioWorkletNode(t,"mstg-shim"),this.node.connect(s),i},write(n){const o=new Float32Array(n.numberOfFrames*n.numberOfChannels);n.copyTo(o,{planeIndex:0}),this.node.port.postMessage(o,[o.buffer]),n.close()}}),i}}}function Ge(){return self.MediaStreamTrackGenerator?self.MediaStreamTrackGenerator:Ue}class $e{constructor(e){const t=e.track??e;t.kind=="video"?this.readable=new ReadableStream({async start(s){t.addEventListener("ended",()=>{try{s.close()}catch{}},{once:!0}),this.video=document.createElement("video"),this.video.srcObject=new MediaStream([t]),await Promise.all([this.video.play(),new Promise(i=>this.video.onloadedmetadata=i)]),this.track=t,this.canvas=new OffscreenCanvas(this.video.videoWidth,this.video.videoHeight),this.ctx=this.canvas.getContext("2d",{desynchronized:!0}),this.t1=performance.now()},async pull(s){if(t.readyState=="ended"){try{s.close()}catch{}return}const i=t.getSettings().frameRate||30;for(;performance.now()-this.t1<1e3/i;)if(await new Promise(n=>requestAnimationFrame(n)),t.readyState=="ended"){try{s.close()}catch{}return}this.canvas.width=this.video.videoWidth,this.canvas.height=this.video.videoHeight,this.t1=performance.now(),this.ctx.drawImage(this.video,0,0),s.enqueue(new VideoFrame(this.canvas,{timestamp:this.t1}))}}):t.kind=="audio"&&(this.readable=new ReadableStream({async start(s){t.addEventListener("ended",()=>{try{s.close()}catch{}},{once:!0}),this.ac=new AudioContext,this.arrays=[];function i(){registerProcessor("mstp-shim",class extends AudioWorkletProcessor{process(o){return this.port.postMessage(o),!0}})}await this.ac.audioWorklet.addModule(`data:text/javascript,(${i.toString()})()`),this.node=new AudioWorkletNode(this.ac,"mstp-shim"),this.ac.createMediaStreamSource(new MediaStream([t])).connect(this.node),this.node.port.addEventListener("message",({data:n})=>n[0][0]&&this.arrays.push(n))},async pull(s){if(t.readyState=="ended"){try{s.close()}catch{}return}for(;!this.arrays.length;)if(await new Promise(o=>this.node.port.onmessage=o),t.readyState=="ended"){try{s.close()}catch{}return}const[i]=this.arrays.shift(),n=new Float32Array(i.reduce((o,a)=>o+a.length,0));i.reduce((o,a)=>(n.set(a,o),o+a.length),0),s.enqueue(new AudioData({format:"f32-planar",sampleRate:this.ac.sampleRate,numberOfFrames:i[0].length,numberOfChannels:i.length,timestamp:this.ac.currentTime*1e6|0,data:n,transfer:[n.buffer]}))}}))}}function De(){return self.MediaStreamTrackProcessor?self.MediaStreamTrackProcessor:$e}class Ve{constructor(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{const i=De();this.processor_=new i(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{const i=Ge();e.kind==="audio"?this.generator_=new i({kind:"audio"}):e.kind==="video"?this.generator_=new i({kind:"video"}):s("kind not supported")}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackGenerator failed: ${i}`),s(i)}t()})}getReadable(){return this.processor_.readable}getWriteable(){return this.generator_.writable}getProccesorTrack(){return this.generator_}}class ze{constructor(e){this.mediaProcessor_=e}async setTrack(e){if(!(this.mediaProcessor_ instanceof re)&&await H()){if(e.kind==="audio")throw Error("Cannot process audio tracks inside a web worker when passing a track to transform");return await this.mediaProcessor_.transform(e)}if(j()){const t=new Ve;return await t.init(e),await this.mediaProcessor_.transform(t.getReadable(),t.getWriteable()),t.getProccesorTrack()}throw Error("MediaStreamTrack API for Insertable Streams of Media is not supported")}destroy(){return new Promise((e,t)=>{this.mediaProcessor_?this.mediaProcessor_.destroy().then(()=>{e()}).catch(s=>{t(s)}):t("no processor")})}}l.ErrorFunction=te,l.MediaProcessor=re,l.MediaProcessorConnector=ze,l.PipelineInfoData=L,l.VonageSourceType=J,l.WarningType=ee,l.getVonageMetadata=W,l.isSupported=S,l.setVonageMetadata=Re,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
|