UNPKG

@vonage/ml-transformers

Version:

<!-- README for NPM; the one for GitHub is in .github directory. -->

1,558 lines 812 kB
function l2() { return new Promise((r, e) => { typeof MediaStreamTrackProcessor > "u" || typeof MediaStreamTrackGenerator > "u" ? e( "Your browser does not support the MediaStreamTrack API for Insertable Streams of Media." ) : r(); }); } class er { } er.updates = { transformer_new: "New transformer", transformer_null: "Null transformer" }; er.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" }; function c2(r) { return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r; } const rr = /* @__PURE__ */ new WeakMap(), bi = /* @__PURE__ */ new WeakMap(), zr = /* @__PURE__ */ new WeakMap(), bo = Symbol("anyProducer"), bs = Promise.resolve(), vi = Symbol("listenerAdded"), Gi = Symbol("listenerRemoved"); let so = !1; function Ir(r) { if (typeof r != "string" && typeof r != "symbol") throw new TypeError("eventName must be a string or a symbol"); } function oi(r) { if (typeof r != "function") throw new TypeError("listener must be a function"); } function Sr(r, e) { const i = bi.get(r); return i.has(e) || i.set(e, /* @__PURE__ */ new Set()), i.get(e); } function jr(r, e) { const i = typeof e == "string" || typeof e == "symbol" ? e : bo, m = zr.get(r); return m.has(i) || m.set(i, /* @__PURE__ */ new Set()), m.get(i); } function u2(r, e, i) { const m = zr.get(r); if (m.has(e)) for (const v of m.get(e)) v.enqueue(i); if (m.has(bo)) { const v = Promise.all([e, i]); for (const S of m.get(bo)) S.enqueue(v); } } function ys(r, e) { e = Array.isArray(e) ? e : [e]; let i = !1, m = () => { }, v = []; const S = { enqueue(L) { v.push(L), m(); }, finish() { i = !0, m(); } }; for (const L of e) jr(r, L).add(S); return { async next() { return v ? v.length === 0 ? i ? (v = void 0, this.next()) : (await new Promise((L) => { m = L; }), this.next()) : { done: !1, value: await v.shift() } : { done: !0 }; }, async return(L) { v = void 0; for (const T of e) jr(r, T).delete(S); return m(), arguments.length > 0 ? { done: !0, value: await L } : { done: !0 }; }, [Symbol.asyncIterator]() { return this; } }; } function gs(r) { if (r === void 0) return Zs; if (!Array.isArray(r)) throw new TypeError("`methodNames` must be an array of strings"); for (const e of r) if (!Zs.includes(e)) throw typeof e != "string" ? new TypeError("`methodNames` element must be a string") : new Error(`${e} is not Emittery method`); return r; } const lo = (r) => r === vi || r === Gi; let Oi = class yo { static mixin(e, i) { return i = gs(i), (m) => { if (typeof m != "function") throw new TypeError("`target` must be function"); for (const L of i) if (m.prototype[L] !== void 0) throw new Error(`The property \`${L}\` already exists on \`target\``); function v() { return Object.defineProperty(this, e, { enumerable: !1, value: new yo() }), this[e]; } Object.defineProperty(m.prototype, e, { enumerable: !1, get: v }); const S = (L) => function(...T) { return this[e][L](...T); }; for (const L of i) Object.defineProperty(m.prototype, L, { enumerable: !1, value: S(L) }); return m; }; } static get isDebugEnabled() { if (typeof process != "object") return so; const { env: e } = process || { env: {} }; return e.DEBUG === "emittery" || e.DEBUG === "*" || so; } static set isDebugEnabled(e) { so = e; } constructor(e = {}) { rr.set(this, /* @__PURE__ */ new Set()), bi.set(this, /* @__PURE__ */ new Map()), zr.set(this, /* @__PURE__ */ new Map()), this.debug = e.debug || {}, this.debug.enabled === void 0 && (this.debug.enabled = !1), this.debug.logger || (this.debug.logger = (i, m, v, S) => { try { S = JSON.stringify(S); } catch { S = `Object with the following keys failed to stringify: ${Object.keys(S).join(",")}`; } typeof v == "symbol" && (v = v.toString()); const L = /* @__PURE__ */ new Date(), T = `${L.getHours()}:${L.getMinutes()}:${L.getSeconds()}.${L.getMilliseconds()}`; console.log(`[${T}][emittery:${i}][${m}] Event Name: ${v} data: ${S}`); }); } logIfDebugEnabled(e, i, m) { (yo.isDebugEnabled || this.debug.enabled) && this.debug.logger(e, this.debug.name, i, m); } on(e, i) { oi(i), e = Array.isArray(e) ? e : [e]; for (const m of e) Ir(m), Sr(this, m).add(i), this.logIfDebugEnabled("subscribe", m, void 0), lo(m) || this.emit(vi, { eventName: m, listener: i }); return this.off.bind(this, e, i); } off(e, i) { oi(i), e = Array.isArray(e) ? e : [e]; for (const m of e) Ir(m), Sr(this, m).delete(i), this.logIfDebugEnabled("unsubscribe", m, void 0), lo(m) || this.emit(Gi, { eventName: m, listener: i }); } once(e) { return new Promise((i) => { const m = this.on(e, (v) => { m(), i(v); }); }); } events(e) { e = Array.isArray(e) ? e : [e]; for (const i of e) Ir(i); return ys(this, e); } async emit(e, i) { Ir(e), this.logIfDebugEnabled("emit", e, i), u2(this, e, i); const m = Sr(this, e), v = rr.get(this), S = [...m], L = lo(e) ? [] : [...v]; await bs, await Promise.all([ ...S.map(async (T) => { if (m.has(T)) return T(i); }), ...L.map(async (T) => { if (v.has(T)) return T(e, i); }) ]); } async emitSerial(e, i) { Ir(e), this.logIfDebugEnabled("emitSerial", e, i); const m = Sr(this, e), v = rr.get(this), S = [...m], L = [...v]; await bs; for (const T of S) m.has(T) && await T(i); for (const T of L) v.has(T) && await T(e, i); } onAny(e) { return oi(e), this.logIfDebugEnabled("subscribeAny", void 0, void 0), rr.get(this).add(e), this.emit(vi, { listener: e }), this.offAny.bind(this, e); } anyEvent() { return ys(this); } offAny(e) { oi(e), this.logIfDebugEnabled("unsubscribeAny", void 0, void 0), this.emit(Gi, { listener: e }), rr.get(this).delete(e); } clearListeners(e) { e = Array.isArray(e) ? e : [e]; for (const i of e) if (this.logIfDebugEnabled("clear", i, void 0), typeof i == "string" || typeof i == "symbol") { Sr(this, i).clear(); const m = jr(this, i); for (const v of m) v.finish(); m.clear(); } else { rr.get(this).clear(); for (const m of bi.get(this).values()) m.clear(); for (const m of zr.get(this).values()) { for (const v of m) v.finish(); m.clear(); } } } listenerCount(e) { e = Array.isArray(e) ? e : [e]; let i = 0; for (const m of e) { if (typeof m == "string") { i += rr.get(this).size + Sr(this, m).size + jr(this, m).size + jr(this).size; continue; } typeof m < "u" && Ir(m), i += rr.get(this).size; for (const v of bi.get(this).values()) i += v.size; for (const v of zr.get(this).values()) i += v.size; } return i; } bindMethods(e, i) { if (typeof e != "object" || e === null) throw new TypeError("`target` must be an object"); i = gs(i); for (const m of i) { if (e[m] !== void 0) throw new Error(`The property \`${m}\` already exists on \`target\``); Object.defineProperty(e, m, { enumerable: !1, value: this[m].bind(this) }); } } }; const Zs = Object.getOwnPropertyNames(Oi.prototype).filter((r) => r !== "constructor"); Object.defineProperty(Oi, "listenerAdded", { value: vi, writable: !1, enumerable: !0, configurable: !1 }); Object.defineProperty(Oi, "listenerRemoved", { value: Gi, writable: !1, enumerable: !0, configurable: !1 }); var a2 = Oi; const No = /* @__PURE__ */ c2(a2); function h2(r) { return typeof r == "object" && r !== null && "message" in r && typeof r.message == "string"; } function d2(r) { if (h2(r)) return r; try { return new Error(JSON.stringify(r)); } catch { return new Error(String(r)); } } function co(r) { return d2(r).message; } var f2 = Object.defineProperty, m2 = (r, e, i) => e in r ? f2(r, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : r[e] = i, p2 = (r, e, i) => (m2(r, e + "", i), i); const b2 = "hlg.tokbox.com/prod/logging/vcp_webrtc", y2 = "https://", g2 = 1e4; let si; const Z2 = new Uint8Array(16); function v2() { if (!si && (si = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !si)) throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported"); return si(Z2); } const Cn = []; for (let r = 0; r < 256; ++r) Cn.push((r + 256).toString(16).slice(1)); function G2(r, e = 0) { return (Cn[r[e + 0]] + Cn[r[e + 1]] + Cn[r[e + 2]] + Cn[r[e + 3]] + "-" + Cn[r[e + 4]] + Cn[r[e + 5]] + "-" + Cn[r[e + 6]] + Cn[r[e + 7]] + "-" + Cn[r[e + 8]] + Cn[r[e + 9]] + "-" + Cn[r[e + 10]] + Cn[r[e + 11]] + Cn[r[e + 12]] + Cn[r[e + 13]] + Cn[r[e + 14]] + Cn[r[e + 15]]).toLowerCase(); } const X2 = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), vs = { randomUUID: X2 }; function W2(r, e, i) { if (vs.randomUUID && !e && !r) return vs.randomUUID(); r = r || {}; const m = r.random || (r.rng || v2)(); return m[6] = m[6] & 15 | 64, m[8] = m[8] & 63 | 128, G2(m); } function Uo(r, e) { globalThis.vonage || (globalThis.vonage = {}), globalThis.vonage.workerizer || (globalThis.vonage.workerizer = {}); let i = globalThis.vonage.workerizer; return i[r] || (i[r] = e), i[r]; } const cr = Uo( "globals", {} ); var Gr = /* @__PURE__ */ ((r) => (r.INIT = "INIT", r.FORWARD = "FORWARD", r.TERMINATE = "TERMINATE", r.GLOBALS_SYNC = "GLOBALS_SYNC", r))(Gr || {}); function Ps(r) { return [ImageBitmap, ReadableStream, WritableStream].some((e) => r instanceof e); } let V2 = 0; function R2(r, e, i, m, v) { const S = V2++; return r.postMessage( { id: S, type: e, functionName: i, args: m }, m.filter((L) => Ps(L)) ), new Promise((L) => { v == null || v.set(S, L); }); } function $r(r, e) { const { id: i, type: m } = r, v = Array.isArray(e) ? e : [e]; postMessage( { id: i, type: m, result: e }, v.filter((S) => Ps(S)) ); } const Gs = Uo("workerized", {}); function Os() { return typeof WorkerGlobalScope < "u" && self instanceof WorkerGlobalScope; } async function x2() { if (Os()) $r({ type: Gr.GLOBALS_SYNC }, cr); else { const r = []; for (const e in Gs) { const { worker: i, resolvers: m } = Gs[e].workerContext; i && r.push( R2( i, Gr.GLOBALS_SYNC, "", [cr], m ) ); } await Promise.all(r); } } function Xi(r, e) { if (Array.isArray(e)) e.splice(0, e.length); else if (typeof e == "object") for (const i in e) delete e[i]; for (const i in r) Array.isArray(r[i]) ? (e[i] = [], Xi(r[i], e[i])) : typeof r[i] == "object" ? (e[i] = {}, Xi(r[i], e[i])) : e[i] = r[i]; } async function I2(r, e) { const { functionName: i, args: m } = r; if (!e.instance) throw "instance not initialized"; if (!i) throw "missing function name to call"; if (!e.instance[i]) throw `undefined function [${i}] in class ${e.instance.constructor.workerId}`; $r(r, await e.instance[i](...m ?? [])); } const S2 = Uo("registeredWorkers", {}); function T2(r, e) { if (!r.args) throw "Missing className while initializing worker"; const [i, m] = r.args, v = S2[i]; if (v) e.instance = new v(r.args.slice(1)); else throw `unknown worker class ${i}`; Xi(m, cr), $r(r, typeof e.instance !== void 0); } async function Y2(r, e) { const { args: i } = r; if (!e.instance) throw "instance not initialized"; let m; e.instance.terminate && (m = await e.instance.terminate(...i ?? [])), $r(r, m); } function F2(r) { if (!r.args) throw "Missing globals while syncing"; Xi(r.args[0], cr), $r(r, {}); } function w2() { const r = {}; onmessage = async (e) => { const i = e.data; switch (i.type) { case Gr.INIT: T2(i, r); break; case Gr.FORWARD: I2(i, r); break; case Gr.TERMINATE: Y2(i, r); break; case Gr.GLOBALS_SYNC: F2(i); break; } }; } Os() && w2(); function L2(r, e) { return cr[r] || (cr[r] = e), [ () => cr[r], async (i) => { cr[r] = i, await x2(); } ]; } function H2(r, e) { return L2(r, e); } const [C2, N2] = H2("metadata"); function U2(r) { N2(r); } function yi() { return C2(); } let Jo = class { constructor(e, i) { p2(this, "uuid"), this.config = e, this.uuid = i ?? W2(); } async send(e) { var i, m, v; const { appId: S, sourceType: L } = (i = yi()) != null ? i : {}; if (!S || !L) return "metadata missing"; const T = new AbortController(), J = setTimeout(() => T.abort(), g2); return await ((v = (m = this.config) == null ? void 0 : m.fetch) != null ? v : fetch)(this.getUrl(), { method: "POST", headers: this.getHeaders(), body: JSON.stringify(this.buildReport(e)), signal: T.signal }), clearTimeout(J), "success"; } getUrl() { var e; let i = (e = yi().proxyUrl) != null ? e : y2; return i += (i.at(-1) === "/" ? "" : "/") + b2, i; } getHeaders() { return { "Content-Type": "application/json" }; } buildReport(e) { const i = yi(); return { guid: this.uuid, ...e, applicationId: i.appId, timestamp: Date.now(), proxyUrl: i.proxyUrl, source: i.sourceType }; } }; const gi = "2.0.4"; class J2 { constructor(e, i) { this.config = e, this.frameTransformedCount = 0, this.frameFromSourceCount = 0, this.startAt = 0, this.reporter = new Jo(e, i); } async onFrameFromSource() { this.frameFromSourceCount++; } get fps() { const { startAt: e, frameFromSourceCount: i } = this, m = (Date.now() - e) / 1e3; return i / m; } async onFrameTransformed(e = {}, i = !1) { this.startAt === 0 && (this.startAt = Date.now()), this.frameTransformedCount++; const { startAt: m, frameTransformedCount: v, frameFromSourceCount: S } = this, L = Date.now(), T = (L - m) / 1e3, J = v / T, tt = S / T; return i || this.frameTransformedCount >= this.config.loggingIntervalFrameCount ? (this.frameFromSourceCount = 0, this.frameTransformedCount = 0, this.startAt = L, this.reporter.config = this.config, this.reporter.send({ ...this.config.report, variation: "QoS", fps: tt, transformedFps: J, framesTransformed: v, ...e })) : "success"; } } var E2 = /* @__PURE__ */ ((r) => (r.FPS_DROP = "fps_drop", r))(E2 || {}), k2 = /* @__PURE__ */ ((r) => (r.start = "start", r.transform = "transform", r.flush = "flush", r))(k2 || {}), go = /* @__PURE__ */ ((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))(go || {}); const M2 = 500, A2 = 0.8; class B2 extends No { constructor(e, i) { super(), this.reporter_ = new Jo(), this.reporterQos_ = new J2({ loggingIntervalFrameCount: M2, report: { version: gi } }), this.index_ = i, 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 (i) { this.report({ message: er.errors.transformer_start, variation: "Error", error: co(i) }); const m = { eventMetaData: { transformerIndex: this.index_ }, error: i, function: "start" /* start */ }; this.emit("error", m); } } async transform(e, i) { var m, v; if (this.mediaTransformerQosReportStartTimestamp_ === 0 && (this.mediaTransformerQosReportStartTimestamp_ = Date.now()), e instanceof VideoFrame && (this.videoHeight_ = (e == null ? void 0 : e.displayHeight) ?? 0, this.videoWidth_ = (e == null ? void 0 : e.displayWidth) ?? 0), this.reporterQos_.onFrameFromSource(), this.transformer_) if (this.shouldStop_) console.warn("[Pipeline] flush from transform"), e.close(), this.flush(i), i.terminate(); else { try { await ((v = (m = this.transformer_).transform) == null ? void 0 : v.call(m, e, i)), this.reportQos(); } catch (S) { this.report({ message: er.errors.transformer_transform, variation: "Error", error: co(S) }); const L = { eventMetaData: { transformerIndex: this.index_ }, error: S, function: "transform" /* transform */ }; this.emit("error", L); } if (this.trackExpectedRate_ != -1 && this.trackExpectedRate_ * A2 > 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 (i) { this.report({ message: er.errors.transformer_flush, variation: "Error", error: co(i) }); const m = { eventMetaData: { transformerIndex: this.index_ }, error: i, function: "flush" /* flush */ }; this.emit("error", m); } } 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: gi, action: "MediaTransformer", transformerType: this.transformerType_, ...e }); } reportQos(e = !1) { this.reporterQos_.config = { ...this.reporterQos_.config }, this.reporterQos_.onFrameTransformed( { version: gi, action: "MediaTransformer", transformerType: this.transformerType_, videoWidth: this.videoWidth_, videoHeight: this.videoHeight_ }, e ); } } class z2 extends No { constructor(e) { super(), this.transformers_ = [], this.trackExpectedRate_ = -1; for (let i = 0; i < e.length; i++) { let m = new B2(e[i], i); m.on("error", (v) => { this.emit("error", v); }), m.on("warn", (v) => { this.emit("warn", v); }), this.transformers_.push(m); } } setTrackExpectedRate(e) { this.trackExpectedRate_ = e; for (let i of this.transformers_) i.setTrackExpectedRate(this.trackExpectedRate_); } async start(e, i) { if (!this.transformers_ || this.transformers_.length === 0) { console.log("[Pipeline] No transformers."); return; } try { let m = e; for (let v of this.transformers_) e = e.pipeThrough( new TransformStream(v) ); e.pipeTo(i).then(async () => { console.log("[Pipeline] Setup."), await i.abort(), await m.cancel(), this.emit( "pipelineInfo", "pipeline_ended" /* pipeline_ended */ ); }).catch(async (v) => { e.cancel().then(() => { console.log( "[Pipeline] Shutting down streams after abort." ); }).catch((S) => { console.error( "[Pipeline] Error from stream transform:", S ); }), await i.abort(v), await m.cancel(v), this.emit( "pipelineInfo", "pipeline_ended_with_error" /* pipeline_ended_with_error */ ); }); } catch { this.emit( "pipelineInfo", "pipeline_started_with_error" /* pipeline_started_with_error */ ), this.destroy(); return; } this.emit( "pipelineInfo", "pipeline_started" /* pipeline_started */ ), console.log("[Pipeline] Pipeline started."); } async destroy() { console.log("[Pipeline] Destroying Pipeline."); for (let e of this.transformers_) e.stop(); } } class j2 extends No { constructor() { super(), this.reporter_ = new Jo(), this.trackExpectedRate_ = -1, this.report({ variation: "Create" }); } /** * Sets the expected rate of the track per second. * The media processor will use this number for calculating drops in the rate. * This could happen when the transformation will take more time than expected. * This will not cause an error, just warning to the client. * Mostly: * Video: 30 frames per second * Audio: 50 audio data per second for OPUS * In case of increased frame dropping rate a warning will be emitted according to info [here](/docs/intro#errors-and-warnings-listener). * ***This is an optional method.*** * @param trackExpectedRate - number holds the predicted track rate. */ setTrackExpectedRate(e) { this.trackExpectedRate_ = e, this.pipeline_ && this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_); } /** * Starts running the tranformation logic performed by the media processor instance. * When running an instance of this class on a Web worker thread the call for this function should be made by the user. See example [here](/docs/intro#webworker-code). * When running an instance of this class on the application main thread there is no need to call this method given it will be called by the `MediaProcessorConnector` instance. * * @param readable Readable stream associated to the media source being processed. * @param writable Writable stream associated to the resulting media once processed. * * @returns */ transform(e, i) { return this.readable_ = e, this.writable_ = i, this.transformInternal(); } /** * @private */ transformInternal() { return new Promise(async (e, i) => { if (!this.transformers_ || this.transformers_.length === 0) { this.report({ message: er.errors.transformer_none, variation: "Error" }), i("[MediaProcessor] Need to set transformers."); return; } if (!this.readable_) { this.report({ variation: "Error", message: er.errors.readable_null }), i("[MediaProcessor] Readable is null."); return; } if (!this.writable_) { this.report({ variation: "Error", message: er.errors.writable_null }), i("[MediaProcessor] Writable is null."); return; } let m = !1; this.pipeline_ && (m = !0, this.pipeline_.clearListeners(), this.pipeline_.destroy()), this.pipeline_ = new z2(this.transformers_), this.pipeline_.on("warn", (v) => { this.emit("warn", v); }), this.pipeline_.on("error", (v) => { this.emit("error", v); }), this.pipeline_.on("pipelineInfo", (v) => { m && (v === "pipeline_started" ? v = go.pipeline_restarted : v === "pipeline_started_with_error" && (v = go.pipeline_restarted_with_error)), this.emit("pipelineInfo", v); }), this.trackExpectedRate_ != -1 && this.pipeline_.setTrackExpectedRate(this.trackExpectedRate_), this.pipeline_.start(this.readable_, this.writable_).then(() => { e(); }).catch((v) => { i(v); }); }); } /** * Sets an array of transfromer instances that will be hold and ran by the media processor instance. * See example [here](/docs/intro#main-code) * * @param transformers An array of transformer instances. * * @returns */ setTransformers(e) { return this.report({ variation: "Update", message: er.updates.transformer_new }), this.transformers_ = e, this.readable_ && this.writable_ ? this.transformInternal() : Promise.resolve(); } /** * Stops running the tranformation logic performed by the media processor instance. * @returns */ destroy() { return new Promise(async (e) => { this.pipeline_ && this.pipeline_.destroy(), this.report({ variation: "Delete" }), e(); }); } report(e) { this.reporter_.send({ version: gi, action: "MediaProcessor", ...e }); } } class K2 { constructor() { this.processor_ = null, this.generator_ = null; } init(e) { return new Promise((i, m) => { try { this.processor_ = new MediaStreamTrackProcessor(e); } catch (v) { console.log( `[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${v}` ), m(v); } try { e.kind === "audio" ? this.generator_ = new MediaStreamTrackGenerator({ kind: "audio" }) : e.kind === "video" ? this.generator_ = new MediaStreamTrackGenerator({ kind: "video" }) : m("kind not supported"); } catch (v) { console.log( `[InsertableStreamHelper] MediaStreamTrackGenerator failed: ${v}` ), m(v); } i(); }); } getReadable() { return this.processor_.readable; } getWriteable() { return this.generator_.writable; } getProccesorTrack() { return this.generator_; } } class P2 { /** * - When running insertable streams on the main `MediaProcessorInterface` should be instance of `MediaProcessor` class. * - When running insertable streams on WebWorker `MediaProcessorInterface` should be instance of bridge calss created by the user. Example can be found [here](/docs/intro.md#using-the-transformer-on-webworker) * @param vonageMediaProcessor - MediaProcessorInterface. */ constructor(e) { this.insertableStreamHelper_ = new K2(), this.mediaProcessor_ = e; } /** * @private * Sets the media stream track instance to be processed. * * @param track MediaStreamTrack (audio or video) to be processed. * * @returns New track to be used. */ setTrack(e) { return new Promise((i, m) => { this.insertableStreamHelper_.init(e).then(() => { this.mediaProcessor_.transform( this.insertableStreamHelper_.getReadable(), this.insertableStreamHelper_.getWriteable() ).then(() => { i( this.insertableStreamHelper_.getProccesorTrack() ); }).catch((v) => { m(v); }); }).catch((v) => { m(v); }); }); } /** * @private * Stops the media processing being performed. */ destroy() { return new Promise((e, i) => { this.mediaProcessor_ ? this.mediaProcessor_.destroy().then(() => { e(); }).catch((m) => { i(m); }) : i("no processor"); }); } } var Zi = /* @__PURE__ */ ((r) => (r.Low = "Low", r.High = "High", r))(Zi || {}), O2 = /* @__PURE__ */ ((r) => (r.BackgroundBlur = "BackgroundBlur", r.VirtualBackground = "VirtualBackground", r.VideoBackground = "VideoBackground", r.SilhouetteBlur = "SilhouetteBlur", r))(O2 || {}), Zo = /* @__PURE__ */ ((r) => (r.FAST = "FAST", r.PRECISE = "PRECISE", r))(Zo || {}), Wi = /* @__PURE__ */ ((r) => (r.CANVAS = "CANVAS", r.WEBGL = "WEBGL", r))(Wi || {}); const _2 = async () => WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11])); async function T3() { const { wasm: r, insertableStreams: e, simd: i, webgl: m } = await Q2(); if (!r.supported) throw r.message; if (!e.supported) throw e.message; if (!i.supported) throw i.message; if (!m.supported) throw m.message; } async function Q2() { const r = { wasm: { supported: typeof WebAssembly == "object" && typeof WebAssembly.instantiate == "function", message: "Your browser does not support WebAssembly features." }, simd: { supported: await _2(), message: "Your browser does not support simd features." }, insertableStreams: { supported: !0 }, gpu: { message: "Your browser does not support gpu features.", supported: !0 // This is a legacy check from the previous segmentation engine. now we just return true always }, webgl: D2(), performance: _s() }; try { await l2(); } catch (e) { r.insertableStreams.supported = !1, r.insertableStreams.message = e; } return r; } function D2() { if (!OffscreenCanvas) return { supported: !1, message: "Your browser does not support offscreen canvas" }; const e = new OffscreenCanvas(0, 0).getContext("webgl2"); if (e) { const i = e == null ? void 0 : e.getExtension("WEBGL_lose_context"); i && i.loseContext(); } else return { supported: !1, message: "Your browser does not support webgl 2" }; return { supported: !0 }; } function _s() { const e = new OffscreenCanvas(0, 0).getContext("webgl2", { failIfMajorPerformanceCaveat: !0 }); if (e) { const i = e == null ? void 0 : e.getExtension("WEBGL_lose_context"); i && i.loseContext(); } else return { supported: !1, message: "Your hardware does not perform good enough to run the library efficiently" }; return { supported: !0 }; } const Vi = {}; (function() { var r; function e(p) { var W = 0; return function() { return W < p.length ? { done: !1, value: p[W++] } : { done: !0 }; }; } var i = typeof Object.defineProperties == "function" ? Object.defineProperty : function(p, W, V) { return p == Array.prototype || p == Object.prototype || (p[W] = V.value), p; }; function m(p) { p = [typeof globalThis == "object" && globalThis, p, typeof window == "object" && window, typeof self == "object" && self, typeof global == "object" && global]; for (var W = 0; W < p.length; ++W) { var V = p[W]; if (V && V.Math == Math) return V; } throw Error("Cannot find global object"); } var v = m(this); function S(p, W) { if (W) t: { var V = v; p = p.split("."); for (var E = 0; E < p.length - 1; E++) { var P = p[E]; if (!(P in V)) break t; V = V[P]; } p = p[p.length - 1], E = V[p], W = W(E), W != E && W != null && i(V, p, { configurable: !0, writable: !0, value: W }); } } S("Symbol", function(p) { function W(rt) { if (this instanceof W) throw new TypeError("Symbol is not a constructor"); return new V(E + (rt || "") + "_" + P++, rt); } function V(rt, K) { this.g = rt, i(this, "description", { configurable: !0, writable: !0, value: K }); } if (p) return p; V.prototype.toString = function() { return this.g; }; var E = "jscomp_symbol_" + (1e9 * Math.random() >>> 0) + "_", P = 0; return W; }), S("Symbol.iterator", function(p) { if (p) return p; p = Symbol("Symbol.iterator"); for (var W = "Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array".split(" "), V = 0; V < W.length; V++) { var E = v[W[V]]; typeof E == "function" && typeof E.prototype[p] != "function" && i(E.prototype, p, { configurable: !0, writable: !0, value: function() { return L(e(this)); } }); } return p; }); function L(p) { return p = { next: p }, p[Symbol.iterator] = function() { return this; }, p; } function T(p) { var W = typeof Symbol < "u" && Symbol.iterator && p[Symbol.iterator]; return W ? W.call(p) : { next: e(p) }; } function J(p) { if (!(p instanceof Array)) { p = T(p); for (var W, V = []; !(W = p.next()).done; ) V.push(W.value); p = V; } return p; } var tt = typeof Object.create == "function" ? Object.create : function(p) { function W() { } return W.prototype = p, new W(); }, st; if (typeof Object.setPrototypeOf == "function") st = Object.setPrototypeOf; else { var bt; t: { var xt = { a: !0 }, Yt = {}; try { Yt.__proto__ = xt, bt = Yt.a; break t; } catch { } bt = !1; } st = bt ? function(p, W) { if (p.__proto__ = W, p.__proto__ !== W) throw new TypeError(p + " is not extensible"); return p; } : null; } var at = st; function dt(p, W) { if (p.prototype = tt(W.prototype), p.prototype.constructor = p, at) at(p, W); else for (var V in W) if (V != "prototype") if (Object.defineProperties) { var E = Object.getOwnPropertyDescriptor(W, V); E && Object.defineProperty(p, V, E); } else p[V] = W[V]; p.ea = W.prototype; } function Et() { this.l = !1, this.i = null, this.h = void 0, this.g = 1, this.s = this.m = 0, this.j = null; } function Mt(p) { if (p.l) throw new TypeError("Generator is already running"); p.l = !0; } Et.prototype.o = function(p) { this.h = p; }; function oe(p, W) { p.j = { U: W, V: !0 }, p.g = p.m || p.s; } Et.prototype.return = function(p) { this.j = { return: p }, this.g = this.s; }; function D(p, W, V) { return p.g = V, { value: W }; } function dn(p) { this.g = new Et(), this.h = p; } function bn(p, W) { Mt(p.g); var V = p.g.i; return V ? ge(p, "return" in V ? V.return : function(E) { return { value: E, done: !0 }; }, W, p.g.return) : (p.g.return(W), Kt(p)); } function ge(p, W, V, E) { try { var P = W.call(p.g.i, V); if (!(P instanceof Object)) throw new TypeError("Iterator result " + P + " is not an object"); if (!P.done) return p.g.l = !1, P; var rt = P.value; } catch (K) { return p.g.i = null, oe(p.g, K), Kt(p); } return p.g.i = null, E.call(p.g, rt), Kt(p); } function Kt(p) { for (; p.g.g; ) try { var W = p.h(p.g); if (W) return p.g.l = !1, { value: W.value, done: !1 }; } catch (V) { p.g.h = void 0, oe(p.g, V); } if (p.g.l = !1, p.g.j) { if (W = p.g.j, p.g.j = null, W.V) throw W.U; return { value: W.return, done: !0 }; } return { value: void 0, done: !0 }; } function jn(p) { this.next = function(W) { return Mt(p.g), p.g.i ? W = ge(p, p.g.i.next, W, p.g.o) : (p.g.o(W), W = Kt(p)), W; }, this.throw = function(W) { return Mt(p.g), p.g.i ? W = ge(p, p.g.i.throw, W, p.g.o) : (oe(p.g, W), W = Kt(p)), W; }, this.return = function(W) { return bn(p, W); }, this[Symbol.iterator] = function() { return this; }; } function ue(p, W) { return W = new jn(new dn(W)), at && p.prototype && at(W, p.prototype), W; } function yt(p, W) { p instanceof String && (p += ""); var V = 0, E = !1, P = { next: function() { if (!E && V < p.length) { var rt = V++; return { value: W(rt, p[rt]), done: !1 }; } return E = !0, { done: !0, value: void 0 }; } }; return P[Symbol.iterator] = function() { return P; }, P; } var Kn = typeof Object.assign == "function" ? Object.assign : function(p, W) { for (var V = 1; V < arguments.length; V++) { var E = arguments[V]; if (E) for (var P in E) Object.prototype.hasOwnProperty.call(E, P) && (p[P] = E[P]); } return p; }; S("Object.assign", function(p) { return p || Kn; }), S("Promise", function(p) { function W(K) { this.h = 0, this.i = void 0, this.g = [], this.o = !1; var nt = this.j(); try { K(nt.resolve, nt.reject); } catch (ht) { nt.reject(ht); } } function V() { this.g = null; } function E(K) { return K instanceof W ? K : new W(function(nt) { nt(K); }); } if (p) return p; V.prototype.h = function(K) { if (this.g == null) { this.g = []; var nt = this; this.i(function() { nt.l(); }); } this.g.push(K); }; var P = v.setTimeout; V.prototype.i = function(K) { P(K, 0); }, V.prototype.l = function() { for (; this.g && this.g.length; ) { var K = this.g; this.g = []; for (var nt = 0; nt < K.length; ++nt) { var ht = K[nt]; K[nt] = null; try { ht(); } catch (vt) { this.j(vt); } } } this.g = null; }, V.prototype.j = function(K) { this.i(function() { throw K; }); }, W.prototype.j = function() { function K(vt) { return function(Ct) { ht || (ht = !0, vt.call(nt, Ct)); }; } var nt = this, ht = !1; return { resolve: K(this.C), reject: K(this.l) }; }, W.prototype.C = function(K) { if (K === this) this.l(new TypeError("A Promise cannot resolve to itself")); else if (K instanceof W) this.F(K); else { t: switch (typeof K) { case "object": var nt = K != null; break t; case "function": nt = !0; break t; default: nt = !1; } nt ? this.u(K) : this.m(K); } }, W.prototype.u = function(K) { var nt = void 0; try { nt = K.then; } catch (ht) { this.l(ht); return; } typeof nt == "function" ? this.G(nt, K) : this.m(K); }, W.prototype.l = function(K) { this.s(2, K); }, W.prototype.m = function(K) { this.s(1, K); }, W.prototype.s = function(K, nt) { if (this.h != 0) throw Error("Cannot settle(" + K + ", " + nt + "): Promise already settled in state" + this.h); this.h = K, this.i = nt, this.h === 2 && this.D(), this.A(); }, W.prototype.D = function() { var K = this; P(function() { if (K.B()) { var nt = v.console; typeof nt < "u" && nt.error(K.i); } }, 1); }, W.prototype.B = function() { if (this.o) return !1; var K = v.CustomEvent, nt = v.Event, ht = v.dispatchEvent; return typeof ht > "u" ? !0 : (typeof K == "function" ? K = new K("unhandledrejection", { cancelable: !0 }) : typeof nt == "function" ? K = new nt("unhandledrejection", { cancelable: !0 }) : (K = v.document.createEvent("CustomEvent"), K.initCustomEvent("unhandledrejection", !1, !0, K)), K.promise = this, K.reason = this.i, ht(K)); }, W.prototype.A = function() { if (this.g != null) { for (var K = 0; K < this.g.length; ++K) rt.h(this.g[K]); this.g = null; } }; var rt = new V(); return W.prototype.F = function(K) { var nt = this.j(); K.J(nt.resolve, nt.reject); }, W.prototype.G = function(K, nt) { var ht = this.j(); try { K.call(nt, ht.resolve, ht.reject); } catch (vt) { ht.reject(vt); } }, W.prototype.then = function(K, nt) { function ht(jt, Ft) { return typeof jt == "function" ? function(Vt) { try { vt(jt(Vt)); } catch (Ht) { Ct(Ht); } } : Ft; } var vt, Ct, Tt = new W(function(jt, Ft) { vt = jt, Ct = Ft; }); return this.J(ht(K, vt), ht(nt, Ct)), Tt; }, W.prototype.catch = function(K) { return this.then(void 0, K); }, W.prototype.J = function(K, nt) { function ht() { switch (vt.h) { case 1: K(vt.i); break; case 2: nt(vt.i); break; default: throw Error("Unexpected state: " + vt.h); } } var vt = this; this.g == null ? rt.h(ht) : this.g.push(ht), this.o = !0; }, W.resolve = E, W.reject = function(K) { return new W(function(nt, ht) { ht(K); }); }, W.race = function(K) { return new W(function(nt, ht) { for (var vt = T(K), Ct = vt.next(); !Ct.done; Ct = vt.next()) E(Ct.value).J(nt, ht); }); }, W.all = function(K) { var nt = T(K), ht = nt.next(); return ht.done ? E([]) : new W(function(vt, Ct) { function Tt(Vt) { return function(Ht) { jt[Vt] = Ht, Ft--, Ft == 0 && vt(jt); }; } var jt = [], Ft = 0; do jt.push(void 0), Ft++, E(ht.value).J(Tt(jt.length - 1), Ct), ht = nt.next(); while (!ht.done); }); }, W; }), S("Object.is", function(p) { return p || function(W, V) { return W === V ? W !== 0 || 1 / W === 1 / V : W !== W && V !== V; }; }), S("Array.prototype.includes", function(p) { return p || function(W, V) { var E = this; E instanceof String && (E = String(E)); var P = E.length; for (V = V || 0, 0 > V && (V = Math.max(V + P, 0)); V < P; V++) { var rt = E[V]; if (rt === W || Object.is(rt, W)) return !0; } return !1; }; }), S("String.prototype.includes", function(p) { return p || function(W, V) { if (this == null) throw new TypeError("The 'this' value for String.prototype.includes must not be null or undefined"); if (W instanceof RegExp) throw new TypeError("First argument to String.prototype.includes must not be a regular expression"); return this.indexOf(W, V || 0) !== -1; }; }), S("Array.prototype.keys", function(p) { return p || function() { return yt(this, function(W) { return W; }); }; }); var Pn = this || self; function Xe(p, W) { p = p.split("."); var V = Pn; p[0] in V || typeof V.execScript > "u" || V.execScript("var " + p[0]); for (var E; p.length && (E = p.shift()); ) p.length || W === void 0 ? V[E] && V[E] !== Object.prototype[E] ? V = V[E] : V = V[E] = {} : V[E] = W; } function Lt(p, W) { return W = String.fromCharCode.apply(null, W), p == null ? W : p + W; } var Ye, nn = typeof TextDecoder < "u", yn, Yn = typeof TextEncoder < "u"; function Vn(p) { if (Yn) p = (yn || (yn = new TextEncoder())).encode(p); else { var W = void 0; W = W === void 0 ? !1 : W; for (var V = 0, E = new Uint8Array(3 * p.length), P = 0; P < p.length; P++) { var rt = p.charCodeAt(P); if (128 > rt) E[V++] = rt; else { if (2048 > rt) E[V++] = rt >> 6 | 192; else { if (55296 <= rt && 57343 >= rt) { if (56319 >= rt && P < p.length) { var K = p.charCodeAt(++P); if (56320 <= K && 57343 >= K) { rt = 1024 * (rt - 55296) + K - 56320 + 65536, E[V++] = rt >> 18 | 240, E[V++] = rt >> 12 & 63 | 128, E[V++] = rt >> 6 & 63 | 128, E[V++] = rt & 63 | 128; continue; } else P--; } if (W) throw Error("Found an unpaired surrogate"); rt = 65533; } E[V++] = rt >> 12 | 224, E[V++] = rt >> 6 & 63 | 128; } E[V++] = rt & 63 | 128; } } p = E.subarray(0, V); } return p; } var Ln = {}, rn = null; function un(p, W) { W === void 0 && (W = 0), gn(), W = Ln[W]; for (var V = Array(Math.floor(p.length / 3)), E = W[64] || "", P = 0, rt = 0; P < p.length - 2; P += 3) { var K = p[P], nt = p[P + 1], ht = p[P + 2], vt = W[K >> 2]; K = W[(K & 3) << 4 | nt >> 4], nt = W[(nt & 15) << 2 | ht >> 6], ht = W[ht & 63], V[rt++] = vt + K + nt + ht; } switch (vt = 0, ht = E, p.length - P) { case 2: vt = p[P + 1], ht = W[(vt & 15) << 2] || E; case 1: p = p[P], V[rt] = W[p >> 2] + W[(p & 3) << 4 | vt >> 4] + ht + E; } return V.join(""); } function Un(p) { var W = p.length, V = 3 * W / 4; V % 3 ? V = Math.floor(V) : "=.".indexOf(p[W - 1]) != -1 && (V = "=.".indexOf(p[W - 2]) != -1 ? V - 2 : V - 1); var E = new Uint8Array(V), P = 0; return Fn(p, function(rt) { E[P++] = rt; }), E.subarray(0, P); } function Fn(p, W) { function V(ht) { for (; E < p.length; ) { var vt = p.charAt(E++), Ct = rn[vt]; if (Ct != null) return Ct; if (!/^[\s\xa0]*$/.test(vt)) throw Error("Unknown base64 encoding at char: " + vt); } return ht; } gn(); for (var E = 0; ; ) { var P = V(-1), rt = V(0), K = V(64), nt = V(64); if (nt === 64 && P === -1) break; W(P << 2 | rt >> 4), K != 64 && (W(rt << 4 & 240 | K >> 2), nt != 64 && W(K << 6 & 192 | nt)); } } function gn() { if (!rn) { rn = {}; for (var p = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(""), W = ["+/=", "+/", "-_=", "-_.", "-_"], V = 0; 5 > V; V++) { var E = p.concat(W[V].split("")); Ln[V] = E; for (var P = 0; P < E.length; P++) { var rt = E[P]; rn[rt] === void 0 && (rn[rt] = P); } } } } var on = typeof Uint8Array.prototype.slice == "function", Rn; function Zn(p, W, V) { return W === V ? Rn || (Rn = new Uint8Array(0)) : on ? p.slice(W, V) : new Uint8Array(p.subarray(W, V)); } var be = 0, We = 0; function Oe(p, W) { W = W === void 0 ? {} : W, W = W.v === void 0 ? !1 : W.v, this.h = null, this.g = this.i = this.j = 0, this.l = !1, this.v = W, p && pe(this, p); } function pe(p, W) { W = W.constructor === Uint8Array ? W : W.constructor === ArrayBuffer ? new Uint8Array(W) : W.constructor === Array ? new Uint8Array(W) : W.constructor === String ? Un(W) : W instanceof Uint8Array ? new Uint8Array(W.buffer, W.byteOffset, W.byteLength) : new Uint8Array(0), p.h = W, p.j = 0, p.i = p.h.length, p.g = p.j; } Oe.prototype.reset = function() { this.g = this.j; }; function ye(p) { var W = p.h, V = W[p.g], E = V & 127; return 128 > V ? (p.g += 1, E) : (V = W[p.g + 1], E |= (V & 127) << 7, 128 > V ? (p.g += 2, E) : (V = W[p.g + 2], E |= (V & 127) << 14, 128 > V ? (p.g += 3, E) : (V = W[p.g + 3], E |= (V & 127) << 21, 128 > V ? (p.g += 4, E) : (V = W[p.g + 4], E |= (V & 15) << 28, 128 > V ? (p.g += 5, E >>> 0) : (p.g += 5, 128 <= W[p.g++] && 128 <= W[p.g++] && 128 <= W[p.g++] && 128 <= W[p.g++] && p.g++, E))))); } function De(p) { var W = p.h[p.g], V = p.h[p.g + 1], E = p.h[p.g + 2], P = p.h[p.g + 3]; return p.g += 4, V = (W << 0 | V << 8 | E << 16 | P << 24) >>> 0, p = 2 * (V >> 31) + 1, W = V >>> 23 & 255, V &= 8388607, W == 255 ? V ? NaN : 1 / 0 * p : W == 0 ? p * Math.pow(2, -149) * V : p * Math.pow(2, W - 150) * (V + Math.pow(2, 23)); } var Ce = []; function Ot() { this.g = new Uint8Array(64), this.h = 0; } Ot.prototype.push = function(p) { if (!(this.h + 1 < this.g.length)) { var W = this.g; this.g = new Uint8Array(Math.ceil(1 + 2 * this.g.length)), this.g.set(W); } this.g[this.h++] = p; }, Ot.prototype.length = function() { return this.h; }, Ot.prototype.end = function() { var p = this.g, W = this.h; return this.h = 0, Zn(p, 0, W); }; function he(p, W) { for (; 127 < W; ) p.push(W & 127 | 128), W >>>= 7; p.push(W); } function Fe(p) { var W = {}, V = W.N === void 0 ? !1 : W.N; this.o = { v: W.v === void 0 ? !1 : W.v }, this.N = V, W = this.o, Ce.length ? (V = Ce.pop(), W && (V.v = W.v), p && pe(V, p), p = V) : p = new Oe(p, W), this.g = p, this.m = this.g.g, this.h = this.i = this.l = -1, this.j = !1; } Fe.prototype.reset = function() { this.g.reset(), this.h = this.l = -1; }; function Ut(p) { var W = p.g; if ((W = W.g == W.i) || (W = p.j) || (W = p.g, W = W.l || 0 > W.g || W.g > W.i), W) return !1; p.m = p.g.g, W = ye(p.g); var V = W & 7; return V != 0 && V != 5 && V != 1 && V != 2 && V != 3 && V != 4 ? (p.j = !0, !1) : (p.i = W, p.l = W >>> 3, p.h = V, !0); } function Ze(p) { switch (p.h) { case 0: if (p.h != 0) Ze(p); else { for (p = p.g; p.h[p.g] & 128; ) p.g++; p.g++; } break; case 1: p.h != 1 ? Ze(p) : (p = p.g, p.g += 8); break; case 2: if (p.h != 2) Ze(p); else { var W = ye(p.g); p = p.g, p.g += W; } break; case 5: p.h != 5 ? Ze(p) : (p = p.g, p.g += 4); break; case 3: W = p.l; do { if (!Ut(p)) { p.j = !0; break; } if (p.h == 4) { p.l != W && (p.j = !0); break; } Ze(p); } while (!0); break; default: p.j = !0;