UNPKG

trtc-sdk-v5

Version:
3 lines 51.8 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).AudioPlayer=t()}(this,function(){"use strict";function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var i=0,r=Array(t);i<t;i++)r[i]=e[i];return r}function t(e,t,i,r,n,s,a){try{var o=e[s](a),l=o.value}catch(e){return void i(e)}o.done?t(l):Promise.resolve(l).then(r,n)}function i(e){return function(){var i=this,r=arguments;return new Promise(function(n,s){var a=e.apply(i,r);function o(e){t(a,n,s,o,l,"next",e)}function l(e){t(a,n,s,o,l,"throw",e)}o(void 0)})}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t,i){return t&&function(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,u(r.key),r)}}(e.prototype,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function s(e,t){var i="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!i){if(Array.isArray(e)||(i=h(e))||t){i&&(e=i);var r=0,n=function(){};return{s:n,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,a=!0,o=!1;return{s:function(){i=i.call(e)},n:function(){var e=i.next();return a=e.done,e},e:function(e){o=!0,s=e},f:function(){try{a||null==i.return||i.return()}finally{if(o)throw s}}}}function a(){ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e,t,i="function"==typeof Symbol?Symbol:{},r=i.iterator||"@@iterator",n=i.toStringTag||"@@toStringTag";function s(i,r,n,s){var a=r&&r.prototype instanceof u?r:u,c=Object.create(a.prototype);return o(c,"_invoke",function(i,r,n){var s,a,o,u=0,c=n||[],h=!1,p={p:0,n:0,v:e,a:_,f:_.bind(e,4),d:function(t,i){return s=t,a=0,o=e,p.n=i,l}};function _(i,r){for(a=i,o=r,t=0;!h&&u&&!n&&t<c.length;t++){var n,s=c[t],_=p.p,d=s[2];i>3?(n=d===r)&&(o=s[(a=s[4])?5:(a=3,3)],s[4]=s[5]=e):s[0]<=_&&((n=i<2&&_<s[1])?(a=0,p.v=r,p.n=s[1]):_<d&&(n=i<3||s[0]>r||r>d)&&(s[4]=i,s[5]=r,p.n=d,a=0))}if(n||i>1)return l;throw h=!0,r}return function(n,c,d){if(u>1)throw TypeError("Generator is already running");for(h&&1===c&&_(c,d),a=c,o=d;(t=a<2?e:o)||!h;){s||(a?a<3?(a>1&&(p.n=-1),_(a,o)):p.n=o:p.v=o);try{if(u=2,s){if(a||(n="next"),t=s[n]){if(!(t=t.call(s,o)))throw TypeError("iterator result is not an object");if(!t.done)return t;o=t.value,a<2&&(a=0)}else 1===a&&(t=s.return)&&t.call(s),a<2&&(o=TypeError("The iterator does not provide a '"+n+"' method"),a=1);s=e}else if((t=(h=p.n<0)?o:i.call(r,p))!==l)break}catch(t){s=e,a=1,o=t}finally{u=1}}return{value:t,done:h}}}(i,n,s),!0),c}var l={};function u(){}function c(){}function h(){}t=Object.getPrototypeOf;var p=[][r]?t(t([][r]())):(o(t={},r,function(){return this}),t),_=h.prototype=u.prototype=Object.create(p);function d(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,o(e,n,"GeneratorFunction")),e.prototype=Object.create(_),e}return c.prototype=h,o(_,"constructor",h),o(h,"constructor",c),c.displayName="GeneratorFunction",o(h,n,"GeneratorFunction"),o(_),o(_,n,"Generator"),o(_,r,function(){return this}),o(_,"toString",function(){return"[object Generator]"}),(a=function(){return{w:s,m:d}})()}function o(e,t,i,r){var n=Object.defineProperty;try{n({},"",{})}catch(e){n=0}o=function(e,t,i,r){function s(t,i){o(e,t,function(e){return this._invoke(t,i,e)})}t?n?n(e,t,{value:i,enumerable:!r,configurable:!r,writable:!r}):e[t]=i:(s("next",0),s("throw",1),s("return",2))},o(e,t,i,r)}function l(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var i=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=i){var r,n,s,a,o=[],l=!0,u=!1;try{if(s=(i=i.call(e)).next,0===t);else for(;!(l=(r=s.call(i)).done)&&(o.push(r.value),o.length!==t);l=!0);}catch(e){u=!0,n=e}finally{try{if(!l&&null!=i.return&&(a=i.return(),Object(a)!==a))return}finally{if(u)throw n}}return o}}(e,t)||h(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var i=e[Symbol.toPrimitive];if(void 0!==i){var r=i.call(e,t);if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e,"string");return"symbol"==typeof t?t:t+""}function c(e){return c="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},c(e)}function h(t,i){if(t){if("string"==typeof t)return e(t,i);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(t,i):void 0}}var p=Object.defineProperty,_=Object.getOwnPropertySymbols,d=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable,m=function(e,t,i){return t in e?p(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i},f=function(e,t){for(var i in t||(t={}))d.call(t,i)&&m(e,i,t[i]);if(_){var r,n=s(_(t));try{for(n.s();!(r=n.n()).done;){i=r.value;y.call(t,i)&&m(e,i,t[i])}}catch(e){n.e(e)}finally{n.f()}}return e},P=function(e,t){var i={};for(var r in e)d.call(e,r)&&t.indexOf(r)<0&&(i[r]=e[r]);if(null!=e&&_){var n,a=s(_(e));try{for(a.s();!(n=a.n()).done;){r=n.value;t.indexOf(r)<0&&y.call(e,r)&&(i[r]=e[r])}}catch(e){a.e(e)}finally{a.f()}}return i},v=function(e,t,i){return m(e,"symbol"!==c(t)?t+"":t,i)},b=48e3;var k=null,g=function(){return n(function e(t,i){r(this,e),v(this,"_ctx"),v(this,"_options"),v(this,"_workletNode",null),v(this,"_pcmInitialized",!1),v(this,"_pcmState","idle"),v(this,"_pendingInputs",[]),v(this,"_processQueue",Promise.resolve()),v(this,"_playedSamples",0),v(this,"_totalInputSamples",0),v(this,"_isPlayEnd",!1),v(this,"_playEndStartTime",0),v(this,"_resampleState",{lastSample:0,nextInputIndex:0}),v(this,"_channels",1),v(this,"_mediaDestination",null),v(this,"_audioElement",null),this._ctx=t,this._options=f({},i),void 0===this._options.realtime&&(this._options.realtime=!0),this._channels=this._options.channels&&this._options.channels>0?this._options.channels:1},[{key:"currentTime",get:function(){return Math.round(this._playedSamples/b*100)/100}},{key:"duration",get:function(){return Math.round(this._totalInputSamples/b*100)/100}},{key:"isStop",get:function(){return"stopped"===this._pcmState||"idle"===this._pcmState}},{key:"isPause",get:function(){return"paused"===this._pcmState}},{key:"isPlayEnd",get:function(){return this._isPlayEnd}},{key:"state",get:function(){return this._pcmState}},{key:"getWorkletNode",value:function(){return this._workletNode}},{key:"updateOptions",value:function(e){void 0!==e.realtime&&(this._options.realtime=e.realtime,this._workletNode&&this._workletNode.port.postMessage({name:"set-mode",realtime:!!e.realtime})),void 0!==e.channels&&e.channels>0&&(this._channels=e.channels),e.onInputError&&(this._options.onInputError=e.onInputError),e.onTimeUpdate&&(this._options.onTimeUpdate=e.onTimeUpdate),e.onEnded&&(this._options.onEnded=e.onEnded),e.onDurationChange&&(this._options.onDurationChange=e.onDurationChange)}},{key:"init",value:(t=i(a().m(function e(){var t,i=this;return a().w(function(e){for(;;)switch(e.n){case 0:if("suspended"!==(t=this._ctx._audioContext).state){e.n=1;break}return e.n=1,t.resume();case 1:return k||(k=t.audioWorklet.addModule(URL.createObjectURL(new Blob(['class PCMPlayerWorklet extends AudioWorkletProcessor{constructor(){super(),this.capacity=480000,this.ringBuf=new Float32Array(this.capacity),this.writePos=0,this.readPos=0,this.buffered=0,this.isStop=!1,this.isPause=!1,this.playedSamples=0,this.totalSamples=0,this.msgCounter=0,this.isRealtime=!0,this.dataStart=0,this.port.onmessage=e=>{var t=e.data;switch(t.name){case"input":{var p=t.pcm,l=p.length;if(this.buffered+l>this.capacity){if(this.isRealtime){var c=Math.max(this.capacity*2,this.buffered+l),n=new Float32Array(c);for(var i=0;i<this.buffered;i++)n[i]=this.ringBuf[(this.readPos+i)%this.capacity];this.ringBuf=n,this.readPos=0,this.writePos=this.buffered,this.dataStart=0,this.capacity=c}else{var c=Math.max(this.capacity*2,this.totalSamples+l),n=new Float32Array(c);for(var i=0;i<this.totalSamples;i++)n[i]=this.ringBuf[(this.dataStart+i)%this.capacity];this.readPos=this.playedSamples,this.writePos=this.totalSamples,this.dataStart=0,this.ringBuf=n,this.capacity=c}}for(i=0;i<l;i++)this.ringBuf[this.writePos]=p[i],this.writePos=(this.writePos+1)%this.capacity;this.buffered+=l,this.totalSamples+=l;break}case"clear":{var s=this.buffered;this.readPos=0,this.writePos=0,this.buffered=0,this.playedSamples=0,this.totalSamples=0,this.dataStart=0,this.port.postMessage({type:"cleared",length:s});break}case"pause":this.isPause=!0;break;case"resume":this.isPause=!1;break;case"discard":this.readPos=0,this.writePos=0,this.buffered=0,this.playedSamples=0,this.totalSamples=0,this.dataStart=0;break;case"reset-playback":this.readPos=this.dataStart,this.playedSamples=0,this.buffered=this.totalSamples;break;case"set-mode":this.isRealtime=!!t.realtime;break;case"stop":this.isStop=!0}}}process(e,t){if(this.isStop)return!1;var s=t[0];if(!s||!s[0])return!0;var o=s[0];if(this.isPause)return o.fill(0),!0;var a=o.length;if(this.buffered>=a){for(var i=0;i<a;i++)o[i]=this.ringBuf[this.readPos],this.readPos=(this.readPos+1)%this.capacity;return this.buffered-=a,this.playedSamples+=a,this.msgCounter++,this.msgCounter>=8&&(this.msgCounter=0,this.port.postMessage({type:"played",playedSamples:this.playedSamples,bufferLength:this.buffered})),!0}if(this.buffered>0){var n=this.buffered;for(i=0;i<n;i++)o[i]=this.ringBuf[this.readPos],this.readPos=(this.readPos+1)%this.capacity;for(i=n;i<a;i++)o[i]=0;return this.buffered=0,this.playedSamples+=n,this.port.postMessage({type:"played",playedSamples:this.playedSamples,bufferLength:0}),!0}return o.fill(0),this.port.postMessage({type:"starving",playedSamples:this.playedSamples,bufferLength:0}),!0}}registerProcessor("pcm-player",PCMPlayerWorklet);'],{type:"application/javascript"})))),e.n=2,k;case 2:this._workletNode=new AudioWorkletNode(t,"pcm-player",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[1]}),this._workletNode.port.postMessage({name:"set-mode",realtime:!!this._options.realtime}),this._workletNode.port.postMessage({name:"pause"}),this._workletNode.port.onmessage=function(e){var t,r,n,s,a,o,l=e.data;switch(l.type){case"played":i._playedSamples=l.playedSamples,i._isPlayEnd=!1,i._playEndStartTime=0,null==(r=(t=i._options).onTimeUpdate)||r.call(t,i.currentTime,i.duration);break;case"starving":i._playedSamples=l.playedSamples,i._handlePlayEnd();break;case"cleared":i._totalInputSamples-=l.length,i._totalInputSamples<0&&(i._totalInputSamples=0),null==(s=(n=i._options).onDurationChange)||s.call(n,i.duration),null==(o=(a=i._options).onTimeUpdate)||o.call(a,i.currentTime,i.duration)}},this._pcmInitialized=!0,this._pcmState="idle",this._resetResampleState(),this._ctx._log.info("PCM engine initialized");case 3:return e.a(2)}},e,this)})),function(){return t.apply(this,arguments)})},{key:"input",value:function(e,t){var r,n,s,o,l,u=this;if("destroyed"===this._pcmState)return this._ctx._log.warn("player is destroyed"),void(null==(n=(r=this._options).onInputError)||n.call(r,"player is destroyed",0));if(!t||t<=0)return this._ctx._log.warn("invalid sampleRate"),void(null==(o=(s=this._options).onInputError)||o.call(s,"invalid sampleRate",0));if(e instanceof Int16Array){l=new Float32Array(e.length);for(var c=0;c<e.length;c++)l[c]=e[c]/32767}else l=e;this._channels>1&&(l=this._downmixToMono(l,this._channels)),this._processQueue=this._processQueue.then(i(a().m(function e(){return a().w(function(e){for(;;)switch(e.n){case 0:if("destroyed"!==u._pcmState){e.n=1;break}return e.a(2);case 1:if(u._workletNode){e.n=2;break}return u._pendingInputs.push({pcm:l,sampleRate:t}),e.a(2);case 2:u._sendToWorklet(l,t);case 3:return e.a(2)}},e)}))).catch(function(e){var t,i;u._ctx._log.error("input error:",e),null==(i=(t=u._options).onInputError)||i.call(t,e instanceof Error?e.message:String(e),0)})}},{key:"clearInput",value:function(){var e,t,i,r;this._workletNode&&(this._workletNode.port.postMessage({name:"clear"}),this._resetResampleState(),this._pendingInputs=[],this._playedSamples=0,this._totalInputSamples=0,null==(t=(e=this._options).onDurationChange)||t.call(e,0),null==(r=(i=this._options).onTimeUpdate)||r.call(i,0,0))}},{key:"start",value:(e=i(a().m(function e(){var t,r,n,o=this;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:if(this._pcmInitialized){e.n=1;break}return this._ctx._log.warn("PCM not initialized"),e.a(2);case 1:if("playing"!==this._pcmState){e.n=2;break}return this._ctx._log.debug("PCM already playing"),e.a(2);case 2:if("suspended"!==(t=this._ctx._audioContext).state){e.n=3;break}return e.n=3,t.resume();case 3:if("stopped"!==this._pcmState&&"paused"!==this._pcmState){e.n=4;break}this._workletNode&&("stopped"===this._pcmState&&this._workletNode.port.postMessage({name:"reset-playback"}),this._workletNode.port.postMessage({name:"resume"})),"stopped"===this._pcmState&&(this._playedSamples=0),e.n=12;break;case 4:if(this._workletNode){e.n=5;break}return this._ctx._log.warn("worklet not available"),e.a(2);case 5:if(!this._ctx._core.environment.IS_IOS){e.n=10;break}return this._mediaDestination=t.createMediaStreamDestination(),this._workletNode.connect(this._mediaDestination),(r=new Audio).srcObject=this._mediaDestination.stream,r.setAttribute("autoplay","autoplay"),r.setAttribute("playsinline","playsinline"),this._audioElement=r,e.p=6,e.n=7,r.play();case 7:e.n=9;break;case 8:e.p=8,n=e.v,this._ctx._log.warn("iOS audio element play failed:",n);case 9:e.n=11;break;case 10:this._workletNode.connect(t.destination);case 11:this._workletNode.port.postMessage({name:"resume"});case 12:this._pcmState="playing",this._isPlayEnd=!1,this._playEndStartTime=0,this._resetResampleState(),this._processQueue=this._processQueue.then(i(a().m(function e(){var t,i,r;return a().w(function(e){for(;;)switch(e.n){case 0:t=s(o._pendingInputs);try{for(t.s();!(i=t.n()).done;)r=i.value,o._sendToWorklet(r.pcm,r.sampleRate)}catch(e){t.e(e)}finally{t.f()}o._pendingInputs=[];case 1:return e.a(2)}},e)}))),this._ctx._log.info("PCM playback started");case 13:return e.a(2)}},e,this,[[6,8]])})),function(){return e.apply(this,arguments)})},{key:"pause",value:function(){var e,t,i,r;"playing"===this._pcmState&&(this._pcmState="paused",this._workletNode&&(this._workletNode.port.postMessage({name:"pause"}),this._options.realtime&&(this._workletNode.port.postMessage({name:"discard"}),this._pendingInputs=[],this._totalInputSamples=0,this._playedSamples=0,null==(t=(e=this._options).onDurationChange)||t.call(e,0))),null==(r=(i=this._options).onTimeUpdate)||r.call(i,this.currentTime,this.duration))}},{key:"resume",value:function(){var e,t,i=this;"paused"===this._pcmState&&(this._pcmState="playing",this._workletNode&&this._workletNode.port.postMessage({name:"resume"}),"suspended"===this._ctx._audioContext.state&&this._ctx._audioContext.resume().catch(function(e){return i._ctx._log.warn("AudioContext resume failed:",e)}),null==(t=(e=this._options).onTimeUpdate)||t.call(e,this.currentTime,this.duration))}},{key:"stop",value:function(){var e,t,i,r,n,s;this._pcmInitialized&&("playing"!==this._pcmState&&"paused"!==this._pcmState||(this._workletNode&&(this._workletNode.port.postMessage({name:"pause"}),this._options.realtime?(this._workletNode.port.postMessage({name:"discard"}),this._totalInputSamples=0,this._playedSamples=0,null==(t=(e=this._options).onDurationChange)||t.call(e,0)):this._playedSamples=0),this._pcmState="stopped",this._pendingInputs=[],this._processQueue=Promise.resolve(),this._resetResampleState(),this._isPlayEnd=!0,this._playEndStartTime=0,null==(r=(i=this._options).onEnded)||r.call(i),null==(s=(n=this._options).onTimeUpdate)||s.call(n,this.currentTime,this.duration),this._ctx._log.info("PCM playback stopped")))}},{key:"destroy",value:function(){var e,t;null==(t=(e=this._options).onDurationChange)||t.call(e,0),this._pendingInputs=[],this._processQueue=Promise.resolve(),this._resetResampleState(),this._playedSamples=0,this._totalInputSamples=0,this._workletNode&&(this._workletNode.port.postMessage({name:"stop"}),this._workletNode.disconnect(),this._workletNode.port.onmessage=null,this._workletNode=null),this._audioElement&&(this._audioElement.pause(),this._audioElement.srcObject=null,this._audioElement=null),this._mediaDestination&&(this._mediaDestination.disconnect(),this._mediaDestination=null),this._isPlayEnd=!0,this._playEndStartTime=0,this._pcmState="destroyed",this._pcmInitialized=!1}},{key:"_resetResampleState",value:function(){this._resampleState={lastSample:0,nextInputIndex:0}}},{key:"_downmixToMono",value:function(e,t){for(var i=Math.floor(e.length/t),r=new Float32Array(i),n=0;n<i;n++){for(var s=0,a=0;a<t;a++)s+=e[n*t+a];r[n]=s/t}return r}},{key:"_resampleStream",value:function(e,t,i){if(t===i)return this._resampleState.lastSample=e[e.length-1],this._resampleState.nextInputIndex=0,e;for(var r=t/i,n=[],s=this._resampleState.nextInputIndex,a=e.length;;){var o=Math.floor(s);if(o>=a-1)break;var l=s-o,u=void 0,c=void 0;-1===o?(u=this._resampleState.lastSample,c=e[0]):(u=e[o],c=e[o+1]),n.push(u+(c-u)*l),s+=r}return this._resampleState.lastSample=e[a-1],this._resampleState.nextInputIndex=s-a,new Float32Array(n)}},{key:"_sendToWorklet",value:function(e,t){var i,r,n,s,a,o;if(this._workletNode&&"destroyed"!==this._pcmState&&("paused"!==this._pcmState||!this._options.realtime)){var l=this._resampleStream(e,t,b);if(this._options.realtime){var u="object"===c(this._options.realtime)?this._options.realtime:{},h=(u.maxDelay||300)/1e3*b;this._totalInputSamples-this._playedSamples>h&&u.discardAll&&(this._workletNode.port.postMessage({name:"discard"}),this._totalInputSamples=this._playedSamples,null==(r=(i=this._options).onDurationChange)||r.call(i,this.duration))}this._totalInputSamples+=l.length,this._isPlayEnd=!1,this._playEndStartTime=0,this._workletNode.port.postMessage({name:"input",pcm:l}),null==(s=(n=this._options).onDurationChange)||s.call(n,this.duration),null==(o=(a=this._options).onTimeUpdate)||o.call(a,this.currentTime,this.duration)}}},{key:"_handlePlayEnd",value:function(){var e,t,i,r;"paused"===this._pcmState||this._isPlayEnd||(this._playEndStartTime?Date.now()-this._playEndStartTime>500&&(this._playEndStartTime=0,this._isPlayEnd=!0,null==(t=(e=this._options).onEnded)||t.call(e),null==(r=(i=this._options).onTimeUpdate)||r.call(i,this.currentTime,this.duration)):this._playEndStartTime=Date.now())}}]);var e,t}(),E=function(e){return e[e.INVALID_PARAMETER=4096]="INVALID_PARAMETER",e[e.INVALID_OPERATION=4097]="INVALID_OPERATION",e[e.NOT_SUPPORTED=4098]="NOT_SUPPORTED",e[e.DEVICE_NOT_FOUND=4099]="DEVICE_NOT_FOUND",e[e.INITIALIZE_FAILED=4100]="INITIALIZE_FAILED",e[e.SIGNAL_CHANNEL_SETUP_FAILED=16385]="SIGNAL_CHANNEL_SETUP_FAILED",e[e.SIGNAL_CHANNEL_ERROR=16386]="SIGNAL_CHANNEL_ERROR",e[e.ICE_TRANSPORT_ERROR=16387]="ICE_TRANSPORT_ERROR",e[e.JOIN_ROOM_FAILED=16388]="JOIN_ROOM_FAILED",e[e.CREATE_OFFER_FAILED=16389]="CREATE_OFFER_FAILED",e[e.SIGNAL_CHANNEL_RECONNECTION_FAILED=16390]="SIGNAL_CHANNEL_RECONNECTION_FAILED",e[e.UPLINK_RECONNECTION_FAILED=16391]="UPLINK_RECONNECTION_FAILED",e[e.DOWNLINK_RECONNECTION_FAILED=16392]="DOWNLINK_RECONNECTION_FAILED",e[e.REMOTE_STREAM_NOT_EXIST=16400]="REMOTE_STREAM_NOT_EXIST",e[e.CLIENT_BANNED=16448]="CLIENT_BANNED",e[e.SERVER_TIMEOUT=16449]="SERVER_TIMEOUT",e[e.SUBSCRIPTION_TIMEOUT=16450]="SUBSCRIPTION_TIMEOUT",e[e.PLAY_NOT_ALLOWED=16451]="PLAY_NOT_ALLOWED",e[e.DEVICE_AUTO_RECOVER_FAILED=16452]="DEVICE_AUTO_RECOVER_FAILED",e[e.START_PUBLISH_CDN_FAILED=16453]="START_PUBLISH_CDN_FAILED",e[e.STOP_PUBLISH_CDN_FAILED=16454]="STOP_PUBLISH_CDN_FAILED",e[e.START_MIX_TRANSCODE_FAILED=16455]="START_MIX_TRANSCODE_FAILED",e[e.STOP_MIX_TRANSCODE_FAILED=16456]="STOP_MIX_TRANSCODE_FAILED",e[e.NOT_SUPPORTED_H264=16457]="NOT_SUPPORTED_H264",e[e.SWITCH_ROLE_FAILED=16458]="SWITCH_ROLE_FAILED",e[e.API_CALL_TIMEOUT=16459]="API_CALL_TIMEOUT",e[e.SCHEDULE_FAILED=16460]="SCHEDULE_FAILED",e[e.API_CALL_ABORTED=16461]="API_CALL_ABORTED",e[e.SPC_INITIALIZED_FAILED=16462]="SPC_INITIALIZED_FAILED",e[e.VIDEO_MANAGER_ERROR=16463]="VIDEO_MANAGER_ERROR",e[e.SWITCH_ROOM_FAILED=16464]="SWITCH_ROOM_FAILED",e[e.VIDEO_ENCODE_FAILED=16465]="VIDEO_ENCODE_FAILED",e[e.AUDIO_ENCODE_FAILED=16466]="AUDIO_ENCODE_FAILED",e[e.WHIP_REQUEST_FAILED=16467]="WHIP_REQUEST_FAILED",e[e.UNKNOWN=65535]="UNKNOWN",e}(E||{}),S=E;function T(e){var t;return"NotAllowedError"===e.name||(e.code===S.PLAY_NOT_ALLOWED||!!(null==(t=e.message)?void 0:t.includes("NotAllowedError")))}var I=function(){return n(function e(t,i){r(this,e),v(this,"_ctx"),v(this,"_options"),v(this,"_urlPlayer",null),v(this,"_urlPublisher",null),v(this,"_audioObjectURL",null),v(this,"_urlInitSettings",{}),v(this,"_urlEventsBound",!1),v(this,"_destroyed",!1),v(this,"_localGainNode",null),v(this,"_localSourceNode",null),v(this,"_isLocalPipelineActive",!1),v(this,"_localPipelineFailed",!1),this._ctx=t,this._options=f({},i)},[{key:"volume",get:function(){var e,t;return this._isLocalPipelineActive&&this._localGainNode?this._localGainNode.gain.value:(null==(e=this._urlPlayer)?void 0:e.element)?this._urlPlayer.element.volume:null!=(t=this._options.volume)?t:1},set:function(e){this._options.volume=e,this._setVolume(e)}},{key:"loop",get:function(){var e,t;return(null==(e=this._urlPlayer)?void 0:e.element)?this._urlPlayer.element.loop:null!=(t=this._options.loop)&&t},set:function(e){this._options.loop=e,this._setLoop(e)}},{key:"playbackRate",get:function(){var e,t;return(null==(e=this._urlPlayer)?void 0:e.element)?this._urlPlayer.element.playbackRate:null!=(t=this._options.playbackRate)?t:1},set:function(e){this._options.playbackRate=e,this._setPlaybackRate(e)}},{key:"currentTime",get:function(){return this._urlPlayer?Math.round(this._urlPlayer.currentTime/10)/100:0}},{key:"duration",get:function(){if(this._urlPlayer){var e=this._urlPlayer.duration;return Number.isFinite(e)?Math.round(e/10)/100:0}return 0}},{key:"isStop",get:function(){var e;return!(null==(e=this._urlPlayer)?void 0:e.element)||this._urlPlayer.element.paused&&0===this._urlPlayer.element.currentTime}},{key:"isPause",get:function(){var e;return!!(null==(e=this._urlPlayer)?void 0:e.element)&&this._urlPlayer.element.paused}},{key:"isPlayEnd",get:function(){var e;return!!(null==(e=this._urlPlayer)?void 0:e.element)&&this._urlPlayer.element.ended}},{key:"getPublisherElement",value:function(){var e;return(null==(e=this._urlPublisher)?void 0:e.element)||null}},{key:"getPublisher",value:function(){return this._urlPublisher}},{key:"updateOptions",value:function(e){void 0!==e.volume&&this._setVolume(e.volume),void 0!==e.loop&&this._setLoop(e.loop),void 0!==e.playbackRate&&this._setPlaybackRate(e.playbackRate),void 0!==e.seekFrom&&this.seek(e.seekFrom),e.operation&&this._setOperation(e.operation),e.onDurationChange&&(this._options.onDurationChange=e.onDurationChange,this._bindDurationChange(e.onDurationChange)),e.onTimeUpdate&&(this._options.onTimeUpdate=e.onTimeUpdate,this._bindTimeUpdate(e.onTimeUpdate)),e.onEnded&&(this._options.onEnded=e.onEnded,this._bindEnded(e.onEnded))}},{key:"init",value:(u=i(a().m(function e(){var t,i,r,n,s,o,l,u;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:if(t=this._options,i=t.url,r=t.loop,n=t.volume,s=t.playbackRate,o=t.publish,i){e.n=1;break}return e.a(2);case 1:if(l=i,!o){e.n=5;break}return e.p=2,e.n=3,this._preloadURL(i);case 3:l=e.v,e.n=5;break;case 4:e.p=4,u=e.v,this._ctx._log.warn("preload failed, falling back to original URL: ".concat(u)),l=i;case 5:this._urlPlayer=new this._ctx._core.AudioPlayer({id:"".concat(this._ctx._id,"-player"),url:l,track:null,container:null,muted:!1,log:this._ctx._log,enableTimeupdateEvent:!0}),o&&(this._urlPublisher=new this._ctx._core.AudioPlayer({id:"".concat(this._ctx._id,"-publisher"),url:l,track:null,container:null,muted:!1,log:this._ctx._log,enableTimeupdateEvent:!1})),this._urlInitSettings={loop:r,volume:n,playbackRate:s},this._destroyed=!1,this._ctx._log.info("URL engine initialized, publish=".concat(o));case 6:return e.a(2)}},e,this,[[2,4]])})),function(){return u.apply(this,arguments)})},{key:"start",value:(l=i(a().m(function e(){var t,i;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:if(this._urlPlayer){e.n=1;break}return e.a(2);case 1:return e.p=1,t=[this._urlPlayer.play()],this._urlPublisher&&t.push(this._urlPublisher.play()),e.n=2,Promise.all(t);case 2:this._ctx._core.environment.isRealIOS()&&this._urlPlayer.element&&this._setupLocalPipeline(),this._applyURLSettings(),this._bindURLEvents(),this._ctx._log.info("URL playback started"),e.n=4;break;case 3:return e.p=3,i=e.v,e.n=4,this._handleAutoPlayFailed(i);case 4:return e.a(2)}},e,this,[[1,3]])})),function(){return l.apply(this,arguments)})},{key:"pause",value:function(){var e,t;null==(e=this._urlPlayer)||e.pause(),null==(t=this._urlPublisher)||t.pause()}},{key:"resume",value:function(){var e,t;null==(e=this._urlPlayer)||e.play(),null==(t=this._urlPublisher)||t.play()}},{key:"stop",value:function(){this._urlPlayer&&(this._urlPlayer.pause(),this._urlPlayer.currentTime=0,this._urlPublisher&&(this._urlPublisher.pause(),this._urlPublisher.currentTime=0),this._ctx._log.info("URL playback stopped"))}},{key:"seek",value:function(e){if(this._urlPlayer){var t=1e3*e;t<0||t>this._urlPlayer.duration||(this._urlPlayer.currentTime=t,this._urlPublisher&&(this._urlPublisher.currentTime=t))}}},{key:"createPublisher",value:(o=i(a().m(function e(t){var i,r,n;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:if(!this._urlPublisher){e.n=1;break}return e.a(2,this._urlPublisher.element);case 1:if(this._urlPlayer){e.n=2;break}return e.a(2,null);case 2:if(i=t||this._audioObjectURL||this._options.url||"",t||this._audioObjectURL||!this._options.url){e.n=6;break}return e.p=3,e.n=4,this._preloadURL(this._options.url);case 4:i=e.v,e.n=6;break;case 5:e.p=5,r=e.v,this._ctx._log.warn("preload for publish toggle failed, using original URL: ".concat(r)),i=this._options.url;case 6:if(this._urlPublisher=new this._ctx._core.AudioPlayer({id:"".concat(this._ctx._id,"-publisher"),url:i,track:null,container:null,muted:!1,log:this._ctx._log,enableTimeupdateEvent:!1}),!this._urlPlayer.element||this._urlPlayer.element.paused){e.n=10;break}return e.p=7,e.n=8,this._urlPublisher.play();case 8:this._urlPublisher.element&&this._urlPlayer.element&&(this._urlPublisher.element.loop=this._urlPlayer.element.loop,this._urlPublisher.element.playbackRate=this._urlPlayer.element.playbackRate,this._urlPublisher.element.currentTime=this._urlPlayer.element.currentTime),e.n=10;break;case 9:e.p=9,n=e.v,this._ctx._log.warn("publisher play failed during publish toggle: ".concat(n));case 10:return e.a(2,this._urlPublisher.element)}},e,this,[[7,9],[3,5]])})),function(e){return o.apply(this,arguments)})},{key:"destroyPublisher",value:function(){this._urlPublisher&&(this._urlPublisher.stop(),this._urlPublisher=null)}},{key:"destroy",value:function(){this._destroyLocalPipeline(),this._urlPlayer&&(this._urlPlayer.stop(),this._urlPlayer=null),this._urlPublisher&&(this._urlPublisher.stop(),this._urlPublisher=null),this._audioObjectURL&&(URL.revokeObjectURL(this._audioObjectURL),this._audioObjectURL=null),this._urlEventsBound=!1,this._destroyed=!0}},{key:"_applyURLSettings",value:function(){var e=this._urlInitSettings,t=e.loop,i=e.volume,r=e.playbackRate;void 0!==t&&this._setLoop(t),void 0!==i&&this._setVolume(i),void 0!==r&&this._setPlaybackRate(r),this._urlInitSettings={}}},{key:"_bindURLEvents",value:function(){this._urlEventsBound||(this._urlEventsBound=!0,this._options.onDurationChange&&this._bindDurationChange(this._options.onDurationChange),this._options.onTimeUpdate&&this._bindTimeUpdate(this._options.onTimeUpdate),this._options.onEnded&&this._bindEnded(this._options.onEnded),this._bindErrorHandlers())}},{key:"_handleAutoPlayFailed",value:(s=i(a().m(function e(t){var r,n,s,o=this;return a().w(function(e){for(;;)switch(e.n){case 0:if("NotSupportedError"!==t.name){e.n=4;break}return this._ctx._log.warn("play failed, try to reload source. error: ".concat(t)),e.n=1,this._reloadURL();case 1:return e.n=2,null==(r=this._urlPlayer)?void 0:r.play();case 2:if(!this._urlPublisher){e.n=3;break}return e.n=3,this._urlPublisher.play();case 3:e.n=6;break;case 4:if(!T(t)){e.n=5;break}this._ctx._log.warn("autoplay blocked by browser policy, user interaction required"),(null==(n=this._ctx._core.room)?void 0:n.enableAutoPlayDialog)?this._ctx._core.showAutoPlayDialog():(s=function(){var e=i(a().m(function e(){var t,i,r;return a().w(function(e){for(;;)switch(e.n){case 0:if(!o._destroyed){e.n=1;break}return e.a(2);case 1:return r=[],o._urlPlayer&&r.push(o._urlPlayer.play()),o._urlPublisher&&r.push(o._urlPublisher.play()),e.n=2,Promise.all(r);case 2:null==(i=(t=o._options).onAutoPlayResume)||i.call(t);case 3:return e.a(2)}},e)}));return function(){return e.apply(this,arguments)}}(),this._ctx._core.trtc.emit("autoplay-failed",{userId:"",mediaType:"audio",resume:s})),e.n=6;break;case 5:throw t;case 6:return e.a(2)}},e,this)})),function(e){return s.apply(this,arguments)})},{key:"_preloadURL",value:(t=i(a().m(function e(t){var i,r,n,s,o,l;return a().w(function(e){for(;;)switch(e.p=e.n){case 0:return this._ctx._log.info("preloading URL resource: ".concat(t)),e.p=1,e.n=2,this._ctx._core.fileDownloader.download(t,{retries:3,type:"blob"});case 2:if((n=e.v)&&0!==n.size){e.n=3;break}throw new Error("Downloaded resource is empty (size=0), url: ".concat(t));case 3:return this._audioObjectURL&&URL.revokeObjectURL(this._audioObjectURL),this._audioObjectURL=URL.createObjectURL(n),this._ctx._log.info("preload complete, blobUrl created, size=".concat(n.size)),e.a(2,this._audioObjectURL);case 4:if(e.p=4,!((l=e.v)instanceof TypeError&&(null==(i=l.message)?void 0:i.includes("Failed to fetch")))){e.n=5;break}throw this._ctx._log.error("CORS error detected when loading audio resource: ".concat(t,". Please check the server's CORS configuration (Access-Control-Allow-Origin header).")),new Error("CORS error: Failed to fetch audio resource. Ensure the server allows cross-origin requests for: ".concat(t));case 5:if(!(s=null==(r=l.message)?void 0:r.match(/(\d{3})/))){e.n=6;break}if(!((o=parseInt(s[1]))>=400&&o<600)){e.n=6;break}throw this._ctx._log.error("HTTP ".concat(o," error when loading audio resource: ").concat(t)),new Error("HTTP ".concat(o,": Failed to load audio resource: ").concat(t));case 6:throw this._ctx._log.error("Failed to preload audio resource after retries: ".concat(t,", error: ").concat(l.message||l)),l;case 7:return e.a(2)}},e,this,[[1,4]])})),function(e){return t.apply(this,arguments)})},{key:"_reloadURL",value:(e=i(a().m(function e(){var t,i,r,n;return a().w(function(e){for(;;)switch(e.n){case 0:if(r=this._options.url){e.n=1;break}return e.a(2);case 1:return e.n=2,this._preloadURL(r);case 2:n=e.v,(null==(t=this._urlPlayer)?void 0:t.element)&&(this._urlPlayer.element.src=n),(null==(i=this._urlPublisher)?void 0:i.element)&&(this._urlPublisher.element.src=n);case 3:return e.a(2)}},e,this)})),function(){return e.apply(this,arguments)})},{key:"_setVolume",value:function(e){this._urlPlayer&&(this._ctx._core.environment.isRealIOS()?this._localPipelineFailed?this._urlPlayer.setVolume(e):(!this._isLocalPipelineActive&&this._urlPlayer.element&&this._setupLocalPipeline(),this._localGainNode&&(this._localGainNode.gain.value=e)):this._urlPlayer.setVolume(e))}},{key:"_setLoop",value:function(e){this._urlPlayer&&this._urlPlayer.setLoop(e),this._urlPublisher&&this._urlPublisher.setLoop(e)}},{key:"_setPlaybackRate",value:function(e){var t,i;(null==(t=this._urlPlayer)?void 0:t.element)&&(this._urlPlayer.element.playbackRate=e),(null==(i=this._urlPublisher)?void 0:i.element)&&(this._urlPublisher.element.playbackRate=e)}},{key:"_setOperation",value:function(e){"pause"===e&&this.pause(),"resume"===e&&this.resume(),"stop"===e&&this.stop()}},{key:"_bindDurationChange",value:function(e){var t,i=this;(null==(t=this._urlPlayer)?void 0:t.element)&&(this._urlPlayer.element.ondurationchange=function(){return e(i.duration)})}},{key:"_bindTimeUpdate",value:function(e){var t,i=this;(null==(t=this._urlPlayer)?void 0:t.element)&&(this._urlPlayer.element.ontimeupdate=function(){return e(i.currentTime,i.duration)})}},{key:"_bindEnded",value:function(e){var t;(null==(t=this._urlPlayer)?void 0:t.element)&&(this._urlPlayer.element.onended=e)}},{key:"_setupLocalPipeline",value:function(){var e,t,i,r=this;if(!this._localPipelineFailed){var n=this._ctx._audioContext;if(this._isLocalPipelineActive)"suspended"===n.state&&n.resume().catch(function(e){r._ctx._log.warn("AudioContext resume failed: ".concat(e))});else{var s=null==(e=this._urlPlayer)?void 0:e.element;if(s)try{"suspended"===n.state&&n.resume().catch(function(e){r._ctx._log.warn("AudioContext resume failed during local pipeline setup: ".concat(e))});var a=this._ctx._core.createAudioNode(s);this._localSourceNode=a;var o=n.createGain();o.gain.value=null!=(t=this._options.volume)?t:1,this._localGainNode=o,a.connect(o),o.connect(n.destination),this._isLocalPipelineActive=!0,this._ctx._log.info("iOS local playback pipeline established")}catch(e){this._localPipelineFailed=!0,this._ctx._log.warn("Failed to setup iOS local playback pipeline, falling back to element.volume: ".concat(e.message||e)),this._urlPlayer&&this._urlPlayer.setVolume(null!=(i=this._options.volume)?i:1)}}}}},{key:"_destroyLocalPipeline",value:function(){if(this._localGainNode){try{this._localGainNode.disconnect()}catch(e){}this._localGainNode=null}if(this._localSourceNode){try{this._localSourceNode.disconnect()}catch(e){}this._localSourceNode=null}this._isLocalPipelineActive=!1,this._localPipelineFailed=!1}},{key:"_bindErrorHandlers",value:function(){var e,t,i=this;(null==(e=this._urlPlayer)?void 0:e.element)&&(this._urlPlayer.element.onerror=function(){var e,t,r=null==(t=null==(e=i._urlPlayer)?void 0:e.element)?void 0:t.error;(null==r?void 0:r.code)===MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED?(i._ctx._log.warn("player source not supported, triggering reload"),i._reloadURL().catch(function(e){return i._ctx._log.error("reload after player error failed:",e)})):r&&i._ctx._log.error("player media error: code=".concat(r.code,", message=").concat(r.message))}),(null==(t=this._urlPublisher)?void 0:t.element)&&(this._urlPublisher.element.onerror=function(){var e,t,r=null==(t=null==(e=i._urlPublisher)?void 0:e.element)?void 0:t.error;(null==r?void 0:r.code)===MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED?(i._ctx._log.warn("publisher source not supported, triggering reload"),i._reloadURL().catch(function(e){return i._ctx._log.warn("reload after publisher error failed:",e)})):r&&i._ctx._log.warn("publisher media error (non-fatal): code=".concat(r.code,", message=").concat(r.message))})}}]);var e,t,s,o,l,u}(),w=function(){return n(function e(t,i){r(this,e),v(this,"_ctx"),v(this,"_options"),v(this,"_trackSource",null),v(this,"_trackPlaying",!1),this._ctx=t,this._options=f({},i)},[{key:"isPlaying",get:function(){return this._trackPlaying}},{key:"isStop",get:function(){return!this._trackPlaying}},{key:"isPause",get:function(){return!!this._options.track&&!this._options.track.enabled}},{key:"track",get:function(){return this._options.track||null}},{key:"init",value:function(){this._ctx._log.info("Track engine initialized")}},{key:"start",value:(e=i(a().m(function e(){var t,i;return a().w(function(e){for(;;)switch(e.n){case 0:if(t=this._options.track){e.n=1;break}return e.a(2);case 1:if(!this._trackPlaying){e.n=2;break}return this._ctx._log.warn("Track already playing"),e.a(2);case 2:if("suspended"!==(i=this._ctx._audioContext).state){e.n=3;break}return e.n=3,i.resume();case 3:this._trackSource=i.createMediaStreamSource(new MediaStream([t])),this._trackSource.connect(i.destination),this._trackPlaying=!0,this._ctx._log.info("Track playback started");case 4:return e.a(2)}},e,this)})),function(){return e.apply(this,arguments)})},{key:"pause",value:function(){this._options.track&&(this._options.track.enabled=!1)}},{key:"resume",value:function(){this._options.track&&(this._options.track.enabled=!0)}},{key:"stop",value:function(){this._trackPlaying&&(this._trackSource&&(this._trackSource.disconnect(),this._trackSource=null),this._trackPlaying=!1,this._ctx._log.info("Track playback stopped"))}},{key:"destroy",value:function(){this.stop(),this._trackSource=null}}]);var e}(),R="add",A="remove",N=function(){return n(function e(t,i,n,s,a,o){r(this,e),v(this,"_id"),v(this,"_sourceType"),v(this,"_log"),v(this,"_core"),v(this,"_audioContext"),v(this,"_options"),v(this,"_destroyed",!1),v(this,"_pcmPlayer",null),v(this,"_urlPlayer",null),v(this,"_trackPlayer",null),v(this,"_mixPipeline",null),v(this,"_publish",!1),this._id=t,this._sourceType=i,this._audioContext=n,this._core=s,this._log=a,this._options=f({},o),this._publish=o.publish||!1},[{key:"id",get:function(){return this._id}},{key:"sourceType",get:function(){return this._sourceType}},{key:"volume",get:function(){var e;return"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.volume:null!=(e=this._options.volume)?e:1},set:function(e){"url"===this._sourceType&&(this._options.volume=e,this._urlPlayer&&(this._urlPlayer.volume=e),this._mixPipeline&&this._mixPipeline.setVolume(e))}},{key:"loop",get:function(){var e;return"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.loop:null!=(e=this._options.loop)&&e},set:function(e){"url"===this._sourceType&&(this._options.loop=e,this._urlPlayer&&(this._urlPlayer.loop=e))}},{key:"playbackRate",get:function(){var e;return"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.playbackRate:null!=(e=this._options.playbackRate)?e:1},set:function(e){"url"===this._sourceType&&(this._options.playbackRate=e,this._urlPlayer&&(this._urlPlayer.playbackRate=e))}},{key:"publish",get:function(){return this._publish},set:function(e){e!==this._publish&&this.updateOptions({publish:e})}},{key:"currentTime",get:function(){return"pcm"===this._sourceType&&this._pcmPlayer?this._pcmPlayer.currentTime:"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.currentTime:0}},{key:"duration",get:function(){return"pcm"===this._sourceType&&this._pcmPlayer?this._pcmPlayer.duration:"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.duration:0}},{key:"isStop",get:function(){return"pcm"===this._sourceType&&this._pcmPlayer?this._pcmPlayer.isStop:"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.isStop:"track"!==this._sourceType||!this._trackPlayer||this._trackPlayer.isStop}},{key:"isPause",get:function(){return"pcm"===this._sourceType&&this._pcmPlayer?this._pcmPlayer.isPause:"url"===this._sourceType&&this._urlPlayer?this._urlPlayer.isPause:!("track"!==this._sourceType||!this._trackPlayer)&&this._trackPlayer.isPause}},{key:"isPlayEnd",get:function(){return"pcm"===this._sourceType&&this._pcmPlayer?this._pcmPlayer.isPlayEnd:!("url"!==this._sourceType||!this._urlPlayer)&&this._urlPlayer.isPlayEnd}},{key:"input",value:function(e,t){var i,r,n;if("pcm"===this._sourceType)return this._destroyed?(this._log.warn("player is destroyed"),void(null==(r=(i=this._options).onInputError)||r.call(i,"player is destroyed",0))):void(null==(n=this._pcmPlayer)||n.input(e,t));this._log.warn("input() is only available for PCM source type")}},{key:"clearInput",value:function(){var e;"pcm"===this._sourceType&&(this._destroyed||null==(e=this._pcmPlayer)||e.clearInput())}},{key:"start",value:(s=i(a().m(function e(){var t,i,r,n,s;return a().w(function(e){for(;;)switch(e.n){case 0:if(!this._destroyed){e.n=1;break}return this._log.warn("player is destroyed"),e.a(2);case 1:s=this._sourceType,e.n="pcm"===s?2:"url"===s?4:"track"===s?6:8;break;case 2:return e.n=3,null==(t=this._pcmPlayer)?void 0:t.start();case 3:case 7:return this._publish&&this._connectMixPipeline(),e.a(3,8);case 4:return e.n=5,null==(i=this._urlPlayer)?void 0:i.start();case 5:return this._publish&&(null==(r=this._urlPlayer)?void 0:r.getPublisherElement())&&(this._connectMixPipeline(),this._mixPipeline&&void 0!==this._options.volume&&this._mixPipeline.setVolume(this._options.volume)),e.a(3,8);case 6:return e.n=7,null==(n=this._trackPlayer)?void 0:n.start();case 8:return e.a(2)}},e,this)})),function(){return s.apply(this,arguments)})},{key:"pause",value:function(){var e,t,i;switch(this._sourceType){case"pcm":null==(e=this._pcmPlayer)||e.pause();break;case"url":null==(t=this._urlPlayer)||t.pause();break;case"track":null==(i=this._trackPlayer)||i.pause()}}},{key:"resume",value:function(){var e,t,i,r,n;switch(this._sourceType){case"pcm":if(null==(e=this._pcmPlayer)||e.resume(),this._publish&&!this._mixPipeline)(null==(t=this._pcmPlayer)?void 0:t.getWorkletNode())&&this._connectMixPipeline();break;case"url":null==(i=this._urlPlayer)||i.resume(),this._publish&&!this._mixPipeline&&(null==(r=this._urlPlayer)?void 0:r.getPublisherElement())&&(this._connectMixPipeline(),this._mixPipeline&&void 0!==this._options.volume&&this._mixPipeline.setVolume(this._options.volume));break;case"track":null==(n=this._trackPlayer)||n.resume(),this._publish&&!this._mixPipeline&&this._connectMixPipeline()}}},{key:"stop",value:function(){var e,t,i;switch(this._sourceType){case"pcm":null==(e=this._pcmPlayer)||e.stop(),this._publish&&this._disconnectMixPipeline();break;case"url":null==(t=this._urlPlayer)||t.stop(),this._disconnectMixPipeline();break;case"track":null==(i=this._trackPlayer)||i.stop(),this._disconnectMixPipeline()}}},{key:"seek",value:function(e){var t;"url"===this._sourceType&&(null==(t=this._urlPlayer)||t.seek(e))}},{key:"init",value:(t=i(a().m(function e(){var t,i,r=this;return a().w(function(e){for(;;)switch(e.n){case 0:this._log.info("init: sourceType=".concat(this._sourceType,", id=").concat(this._id,", publish=").concat(this._publish)),i=this._sourceType,e.n="pcm"===i?1:"url"===i?3:"track"===i?5:6;break;case 1:return this._pcmPlayer=new g(this,{channels:this._options.channels,realtime:null==(t=this._options.realtime)||t,onInputError:this._options.onInputError,onDurationChange:this._options.onDurationChange,onTimeUpdate:this._options.onTimeUpdate,onEnded:this._options.onEnded}),e.n=2,this._pcmPlayer.init();case 2:return e.a(3,6);case 3:return this._urlPlayer=new I(this,{url:this._options.url||"",publish:this._publish,loop:this._options.loop,volume:this._options.volume,playbackRate:this._options.playbackRate,onDurationChange:this._options.onDurationChange,onTimeUpdate:this._options.onTimeUpdate,onEnded:this._options.onEnded,onAutoPlayResume:function(){var e;r._publish&&(null==(e=r._urlPlayer)?void 0:e.getPublisher())&&r._connectMixPipeline()}}),e.n=4,this._urlPlayer.init();case 4:return e.a(3,6);case 5:return this._trackPlayer=new w(this,{track:this._options.track,onEnded:this._options.onEnded}),this._trackPlayer.init(),e.a(3,6);case 6:this._destroyed=!1;case 7:return e.a(2)}},e,this)})),function(){return t.apply(this,arguments)})},{key:"updateOptions",value:(e=i(a().m(function e(t){var i,r;return a().w(function(e){for(;;)switch(e.n){case 0:if(this._log.info("update: ".concat(JSON.stringify(t))),void 0===t.publish||t.publish===this._publish){e.n=5;break}if(this._publish,this._publish=t.publish,!this._publish){e.n=4;break}if("url"!==this._sourceType||!this._urlPlayer){e.n=2;break}return e.n=1,this._urlPlayer.createPublisher();case 1:this._urlPlayer.isStop||this._urlPlayer.isPause||this._connectMixPipeline(),e.n=3;break;case 2:"url"!==this._sourceType&&this._connectMixPipeline();case 3:e.n=5;break;case 4:this._disconnectMixPipeline(),"url"===this._sourceType&&this._urlPlayer&&this._urlPlayer.destroyPublisher();case 5:"url"===this._sourceType&&this._urlPlayer&&(i={},void 0!==t.volume&&(i.volume=t.volume),void 0!==t.loop&&(i.loop=t.loop),void 0!==t.playbackRate&&(i.playbackRate=t.playbackRate),void 0!==t.seekFrom&&(i.seekFrom=t.seekFrom),t.operation&&(i.operation=t.operation),t.onDurationChange&&(i.onDurationChange=t.onDurationChange),t.onTimeUpdate&&(i.onTimeUpdate=t.onTimeUpdate),t.onEnded&&(i.onEnded=t.onEnded),Object.keys(i).length>0&&this._urlPlayer.updateOptions(i),void 0!==t.volume&&this._mixPipeline&&this._mixPipeline.setVolume(t.volume)),"pcm"===this._sourceType&&this._pcmPlayer&&(r={},void 0!==t.realtime&&(r.realtime=t.realtime),void 0!==t.channels&&(r.channels=t.channels),t.onInputError&&(r.onInputError=t.onInputError),t.onTimeUpdate&&(r.onTimeUpdate=t.onTimeUpdate),t.onEnded&&(r.onEnded=t.onEnded),t.onDurationChange&&(r.onDurationChange=t.onDurationChange),Object.keys(r).length>0&&this._pcmPlayer.updateOptions(r));case 6:return e.a(2)}},e,this)})),function(t){return e.apply(this,arguments)})},{key:"destroy",value:function(){var e,t,i;if(!this._destroyed){switch(this._log.info("destroy: id=".concat(this._id)),this._disconnectMixPipeline(),this._sourceType){case"pcm":null==(e=this._pcmPlayer)||e.destroy(),this._pcmPlayer=null;break;case"url":null==(t=this._urlPlayer)||t.destroy(),this._urlPlayer=null;break;case"track":null==(i=this._trackPlayer)||i.destroy(),this._trackPlayer=null}this._destroyed=!0}}},{key:"_connectMixPipeline",value:function(){var e;if(!this._mixPipeline)try{var t=this._core.room;if(!(null==t?void 0:t.audioManager))return void this._log.warn("cannot connect mix pipeline: room or audioManager not available");this._core.validateSourceNode();var i=t.audioManager;if(this._mixPipeline=new this._core.MixAudioPipeline(i),"url"===this._sourceType&&this._urlPlayer){var r=this._urlPlayer.getPublisherElement();r?this._mixPipeline.replaceSource(r):this._log.warn("publisher element not available, mix pipeline source not connected")}else if("track"===this._sourceType&&(null==(e=this._trackPlayer)?void 0:e.track))this._mixPipeline.replaceSource(this._trackPlayer.track);else if("pcm"===this._sourceType&&this._pcmPlayer){var n=this._pcmPlayer.getWorkletNode();n&&this._mixPipeline.source.replaceNode(n)}this._mixPipeline.connect(),this._mixPipeline.source.node&&t.audioManager.updateAudioReference({type:R,audioReference:this._mixPipeline.source.node,refId:"".concat("aup","-").concat(this._id)}),this._log.info("mix pipeline connected")}catch(e){this._log.error("failed to connect mix pipeline:",e)}}},{key:"_disconnectMixPipeline",value:function(){if(this._mixPipeline){try{var e=this._core.room;(null==e?void 0:e.audioManager)&&this._mixPipeline.source.node&&e.audioManager.updateAudioReference({type:A,audioReference:this._mixPipeline.source.node,refId:"".concat("aup","-").concat(this._id)})}catch(e){this._log.warn("failed to remove audio reference:",e)}this._mixPipeline.disconnect(),this._mixPipeline=null,this._log.info("mix pipeline disconnected")}}}]);var e,t,s}(),O=0,x=function(){function e(t){r(this,e),this.core=t,v(this,"_core"),v(this,"_log"),v(this,"_audioContext"),v(this,"_instances",new Map),this._core=t,this._log=t.log.createChild({id:"aup"}),this._audioContext=t.audioContext,this._log.info("plugin registered")}return n(e,[{key:"getName",value:function(){return e.Name}},{key:"getAlias",value:function(){return"aup-"}},{key:"getGroup",value:function(e){return(null==e?void 0:e.id)||"*"}},{key:"getValidateRule",value:function(e){switch(e){case"start":return t=this._core,{type:"object",required:!0,properties:{id:{type:"string",required:!0},sourceType:{type:"string",required:!0,values:["pcm","url","track"]},url:{type:"string"},loop:{type:"boolean"},volume:{type:"number"},playbackRate:{type:"number"},publish:{type:"boolean"},realtime:{type:["boolean","object"],properties:{maxDelay:{type:"number"},discardAll:{type:"boolean"}}},onInputError:{type:"function"},onDurationChange:{type:"function"},onTimeUpdate:{type:"function"},onEnded:{type:"function"}},validate:function(e,i,r){var n=t.errorModule,s=n.RtcError,a=n.ErrorCode,o=e.sourceType,l=e.url,u=e.track;if("url"===o){if(!l)throw new s({code:a.INVALID_PARAMETER,message:"".concat(r,": url is required when sourceType is 'url'.")});if("*"!==l&&!function(e){var t,i=e.split("?")[0],r=null==(t=i.split(".").pop())?void 0:t.toLowerCase(),n=!!r&&["mp3","ogg","wav","flac"].includes(r),s=i.startsWith("blob"),a=i.startsWith("data");return n||s||a}(l))throw new s({code:a.INVALID_PARAMETER,message:"".concat(r,": audio url is invalid, supported formats: mp3, ogg, wav, flac, blob, data.")})}if("track"===o&&!u)throw new s({code:a.INVALID_PARAMETER,message:"".concat(r,": track is required when sourceType is 'track'.")})}};case"update":return this._core,{type:"object",required:!0,properties:{id:{type:"string",required:!0},publish:{type:"boolean"},realtime:{type:["boolean","object"],properties:{maxDelay:{type:"number"},discardAll:{type:"boolean"}}},loop:{type:"boolean"},volume:{type:"number"},playbackRate:{type:"number"},seekFrom:{type:"number"},operation:{type:"string",values:["pause","resume","stop"]},onInputError:{type:"function"},onDurationChange:{type:"function"},onTimeUpdate:{type:"function"},onEnded:{type:"function"}}};case"stop":return this._core,{type:"object",required:!0,properties:{id:{type:"string",required:!0}}}}var t}},{key:"start",value:(u=i(a().m(function e(t){var i,r,n,s,o,l;return a().w(function(e){for(;;)switch(e.n){case 0:return i=t.id,r=t.sourceType,this._instances.has(i)&&(this._log.warn("instance with id=".concat(i," already exists, destroying old instance")),this._instances.get(i).destroy(),this._instances.delete(i)),n="aup-".concat(++O),s=this._core.log.createChild({id:n}),l=new N(i,o=r,this._audioContext,this._core,s,t),e.n=1,l.init();case 1:return this.