@vonage/media-processor 3.0.0-alpha.2 → 3.0.1-alpha.0
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.
- package/dist/media-processor.es.js +312 -306
- package/dist/media-processor.min.js +2 -2
- package/dist/media-processor.static.js +312 -306
- package/dist/media-processor.umd.js +2 -2
- package/dist/types/lib/main.d.ts +0 -2
- package/dist/types/lib/src/utils/getMediaStreamTrackGenerator.d.ts +7 -0
- package/dist/types/lib/src/utils/getMediaStreamTrackProcessor.d.ts +5 -0
- package/package.json +1 -1
- package/dist/types/lib/src/utils/MediaStreamTrackGenerator.polyfill.d.ts +0 -0
- package/dist/types/lib/src/utils/MediaStreamTrackProcessor.polyfill.d.ts +0 -0
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const{keys:_a}=Object;const{defineProperty:_b}=Object;
|
|
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}={}){v(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)||P(this,I,{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){v(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)||P(this,x,{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 H(this,e)}async emit(e,t){if(y(e),b(e)&&!C)throw TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');this.logIfDebugEnabled('emit',e,t),_x(this,e,t);const s=_(this,e)??new Set,i=u.get(this),n=[...s],o=b(e)?[]:[...i];await j,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)&&!C)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 j;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}={}){v(e),this.logIfDebugEnabled('subscribeAny',void 0,void 0),u.get(this).add(e),P(this,I,{listener:e});let s=()=>(this.offAny(e),t?.removeEventListener('abort',s));return t?.addEventListener('abort',s,{once:!0}),t?.aborted&&s(),s}anyEvent(){return H(this)}offAny(e){v(e),this.logIfDebugEnabled('unsubscribeAny',void 0,void 0),P(this,x,{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),O(t)){let s=_(this,t);s?.clear();let i=E(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(O(s)){t+=u.get(this).size+(_(this,s)?.size??0)+(E(this,s)?.size??0)+(E(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=Q(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:I,writable:!1,enumerable:!0,configurable:!1});_b(h,'listenerRemoved',{value:x,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(!M&&(M=typeof crypto<'u'&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!M))throw Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');return M(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 z(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 Z(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=>Z(a))),new Promise(a=>i?.set(n,a))}function k(r,e){const{id:t,type:s}=r,i=_c(e)?e:[e];postMessage({id:t,type:s,result:e},i.filter(n=>Z(n)))}function K(){return typeof WorkerGlobalScope<'u'&&self instanceof WorkerGlobalScope}async function aO(){if(K())k({type:g.GLOBALS_SYNC},p);else{let r=[];for(const e in B){const{worker:t,resolvers:s}=B[e].workerContext;t&&r.push(aN(t,g.GLOBALS_SYNC,'',[p],s))}await Promise.all(r)}}function L(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]=[],L(r[t],e[t])):typeof r[t]=='object'?(e[t]={},L(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}`;k(r,await e.instance[t](...s??[]))}let aQ=z('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}`;L(s,p),k(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??[])),k(r,s)}function aT(r){if(!r.args)throw 'Missing globals while syncing';L(r.args[0],p),k(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}}}K()&&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 D(){return aX()}class V{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=D())!=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=D().proxyUrl)!=null?e:aF;return t+=(t.at(-1)==='/'?'':'/')+aE,t}getHeaders(){return{'Content-Type':'application/json'}}buildReport(e){let t=D();return{guid:this.uuid,...e,applicationId:t.appId,timestamp:Date.now(),proxyUrl:t.proxyUrl,source:t.sourceType}}}const A='3.0.0-alpha.2',W=class W{};W.updates={transformer_new:'New transformer',transformer_null:'Null transformer'},W.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=W;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 $(r){return bB(r).message}class Ae{constructor(e,t){this.config=e,this.frameTransformedCount=0,this.frameFromSourceCount=0,this.startAt=0,this.reporter=new V(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 Oe extends h{constructor(e,t){super(),this.reporter_=new V,this.reporterQos_=new Ae({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:$(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:$(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:$(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 Le extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new Oe(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 V,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 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=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(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{this.processor_=new MediaStreamTrackProcessor(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{e.kind==='audio'?this.generator_=new MediaStreamTrackGenerator({kind:'audio'}):e.kind==='video'?this.generator_=new MediaStreamTrackGenerator({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 We)&&await X()){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()){let t=new Fe;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,ze as MediaProcessorConnector,U as PipelineInfoData,aD as VonageSourceType,bC as WarningType,D as getVonageMetadata,_f as isSupported,aZ as setVonageMetadata};
|
|
1
|
+
const{keys:_a}=Object;const{defineProperty:_b}=Object;async function _d(){return new Promise(async (r,e)=>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 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,A=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=U('globals',{}),g=(r=>(r.INIT='INIT',r.FORWARD='FORWARD',r.TERMINATE='TERMINATE',r.GLOBALS_SYNC='GLOBALS_SYNC',r))(g||{}),q=U('workerized',{}),[aW,aX]=aV('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:A,s=d.get(r);if(s.has(t))return s.get(t)}function K(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(A)){let i=Promise.all([e,t]);for(const n of s.get(A))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(A,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),K(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 N=Object.defineProperty,aA=(r,e,t)=>e in r?N(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,aB=(r,e,t)=>(aA(r,`${e}`,t),t),aC=(r=>(r.automation='automation',r.test='test',r.vbc='vbc',r.video='video',r.voice='voice',r))(aC||{});const aD='hlg.tokbox.com/prod/logging/vcp_webrtc',aE='https://',aF=1e4;let aG=new Uint8Array(16);function aH(){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(aG)}let c=[];for(let r=0;r<256;++r)c.push((r+256).toString(16).slice(1));function aI(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 aJ=typeof crypto<'u'&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),Q={randomUUID:aJ};function aK(r,e,t){if(Q.randomUUID&&!r)return Q.randomUUID();r=r||{};let s=r.random||(r.rng||aH)();return s[6]=s[6]&15|64,s[8]=s[8]&63|128,aI(s)}function U(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 aL=0;function aM(r,e,t,s,i){let n=aL++;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 aN(){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(aM(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 aO(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 aP=U('registeredWorkers',{});function aQ(r,e){if(!r.args)throw 'Missing className while initializing worker';const[t,s]=r.args,i=aP[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 aR(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 aS(r){if(!r.args)throw 'Missing globals while syncing';C(r.args[0],p),S(r,{})}function aT(){let r={};onmessage=e=>{let t=e.data;switch(t.type) {case g.INIT:aQ(t,r);break;case g.FORWARD:aO(t,r);break;case g.TERMINATE:aR(t,r);break;case g.GLOBALS_SYNC:aS(t);break}}}X()&&aT();function aU(r,e){return p[r]||(p[r]=e),[()=>p[r],async t=>(p[r]=t,await aN())]}function aV(r,e){return aU(r,e)}function aY(r){aX(r)}function L(){return aW()}class z{constructor(e,t){aB(this,'uuid'),this.config=e,this.uuid=t??aK()}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(),aF)),'success'}getUrl(){var e,t=(e=L().proxyUrl)!=null?e:aE;return t+=(t.at(-1)==='/'?'':'/')+aD,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 P='3.0.1-alpha.0',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 aZ(r){return typeof r=='object'&&r!==null&&'message' in r&&typeof r.message=='string'}function bA(r){if(aZ(r))return r;try{return Error(JSON.stringify(r))}catch{return Error(`${r}`)}}function G(r){return bA(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 bB=(r=>(r.FPS_DROP='fps_drop',r))(bB||{}),bC=(r=>(r.start='start',r.transform='transform',r.flush='flush',r))(bC||{}),$=(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))($||{});const bD=500,bE=0.8;class xe extends h{constructor(e,t){super(),this.reporter_=new z,this.reporterQos_=new Pe({loggingIntervalFrameCount:bD,report:{version:P}}),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_*bE>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:P,action:'MediaTransformer',transformerType:this.transformerType_,...e})}reportQos(e=!1){this.reporterQos_.config={...this.reporterQos_.config},this.reporterQos_.onFrameTransformed({version:P,action:'MediaTransformer',transformerType:this.transformerType_,videoWidth:this.videoWidth_,videoHeight:this.videoHeight_},e)}}class Ce extends h{constructor(e){super(),this.transformers_=[],this.trackExpectedRate_=-1;for(let t=0;t<e.length;t++){let s=new xe(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 Oe 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 Ce(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=$.pipeline_restarted:i==='pipeline_started_with_error'&&(i=$.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:P,action:'MediaProcessor',...e})}isTrack(e){return 'kind' in e}}class We{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 bF(){return self.MediaStreamTrackGenerator||We}class Le{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 bG(){return self.MediaStreamTrackProcessor||Le}class $e{constructor(){this.processor_=null,this.generator_=null}init(e){return new Promise((t,s)=>{try{let i=bG();this.processor_=new i(e)}catch(i){console.log(`[InsertableStreamHelper] MediaStreamTrackProcessor failed: ${i}`),s(i)}try{let i=bF();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 je{constructor(e){this.mediaProcessor_=e}async setTrack(e){if(!(this.mediaProcessor_ instanceof Oe)&&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{bC as ErrorFunction,Oe as MediaProcessor,je as MediaProcessorConnector,$ as PipelineInfoData,aC as VonageSourceType,bB as WarningType,L as getVonageMetadata,_d as isSupported,aY as setVonageMetadata};
|