@sanseng/livekit-ws-sdk 0.1.9 → 0.1.11
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/index.cjs.js +3 -3
- package/dist/index.d.ts +57 -2
- package/dist/index.esm.js +3 -3
- package/dist/index.full.umd.js +7 -7
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{Room as Q,RoomEvent as C,DisconnectReason as Y,ParticipantEvent as X,ConnectionState as J}from"livekit-client";var d;(function(o){o.SDK_AUTH_TOKEN_FAILED="SDK_AUTH_TOKEN_FAILED",o.SDK_GET_LIVEKIT_CONFIG_FAILED="SDK_GET_LIVEKIT_CONFIG_FAILED",o.SDK_RECONNECT_FAILED="SDK_RECONNECT_FAILED",o.SDK_DISCONNECT_FAILED="SDK_DISCONNECT_FAILED",o.SDK_NOT_CONNECTED="SDK_NOT_CONNECTED",o.SDK_CONNECT_FAILED="SDK_CONNECT_FAILED",o.SDK_SWITCH_VIDEO_FAILED="SDK_SWITCH_VIDEO_FAILED",o.SDK_ERROR="SDK_ERROR",o.SDK_PRECONNECT_FAILED="SDK_PRECONNECT_FAILED",o.SDK_INTERRUPT_CONVERSATION_FAILED="SDK_INTERRUPT_CONVERSATION_FAILED",o.SDK_INITIALIZATION_FAILED="SDK_INITIALIZATION_FAILED",o.SDK_INVALID_STATE_TRANSITION="SDK_INVALID_STATE_TRANSITION",o.NO_AVATARNAME="NO_AVATARNAME",o.AUDIO_CAPTURE_START_FAILED="AUDIO_CAPTURE_START_FAILED",o.AUDIO_CAPTURE_FAILED="AUDIO_CAPTURE_FAILED",o.AUDIO_INVALID_SAMPLE_RATE="AUDIO_INVALID_SAMPLE_RATE",o.AUDIO_INVALID_CHANNEL="AUDIO_INVALID_CHANNEL",o.AUDIO_INVALID_HEADER_LENGTH="AUDIO_INVALID_HEADER_LENGTH",o.AUDIO_INVALID_TYPE="AUDIO_INVALID_TYPE",o.AUDIO_INVALID_RESERVED="AUDIO_INVALID_RESERVED",o.AUDIO_CONTROLLER_NOT_AVAILABLE="AUDIO_CONTROLLER_NOT_AVAILABLE",o.WS_CONNECT_FAILED="WS_CONNECT_FAILED",o.WS_TEXT_SEND_FAILED="WS_TEXT_SEND_FAILED",o.WS_PARSE_ERROR="WS_PARSE_ERROR",o.WS_NOT_CONNECTED="WS_NOT_CONNECTED",o.WS_CONNECT_ERROR="WS_CONNECT_ERROR",o.LIVEKIT_CONNECT_FAILED="LIVEKIT_CONNECT_FAILED",o.CONVERSATION_CONTROLLER_NOT_AVAILABLE="CONVERSATION_CONTROLLER_NOT_AVAILABLE",o.STATE_MACHINE_INVALID_STATE_TRANSITION="STATE_MACHINE_INVALID_STATE_TRANSITION",o.OBJECT_DISPOSED="OBJECT_DISPOSED"})(d||(d={}));class u extends Error{constructor(e,t="SDK_ERROR",s){super(e),this.name="SDKError",this.code=t,this.cause=s,Object.setPrototypeOf(this,u.prototype)}static fromError(e,t){return e instanceof u?e:e instanceof Error?new u(e.message,t||d.SDK_ERROR,e):typeof e=="string"?new u(e,t||d.SDK_ERROR):new u("Unknown error",t||d.SDK_ERROR,e)}}class l{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new u("Object has been disposed",d.OBJECT_DISPOSED)}}var E;(function(o){o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.NONE=4]="NONE"})(E||(E={}));class i{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=E.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=E.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=E.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=E.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}i._level=E.ERROR;i._prefix="[SDK]";class Z extends l{constructor(){super(...arguments),this._listeners=new Map}on(e,t){this.ensureNotDisposed(),this._listeners.has(e)||this._listeners.set(e,new Set);const s=this._listeners.get(e);return s.add(t),i.debug(`Event listener added for: ${String(e)}`),()=>{s.delete(t),i.debug(`Event listener removed for: ${String(e)}`)}}once(e,t){this.ensureNotDisposed();const s=n=>{t(n),this.off(e,s)};return this.on(e,s)}off(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.delete(t)}emit(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.size>0&&(i.debug(`Emitting event: ${String(e)}`,t),s.forEach(n=>{try{n(t)}catch(r){i.error(`Error in event listener for ${String(e)}:`,r)}}))}removeAllListeners(){this._listeners.clear(),i.debug("All event listeners removed")}listenerCount(e){const t=this._listeners.get(e);return t?t.size:0}onDispose(){this.removeAllListeners()}}var f;(function(o){o.IDLE="idle",o.CONNECTING="connecting",o.CONNECTED="connected",o.PARTIAL="partial",o.DISCONNECTED="disconnected",o.RECONNECTING="reconnecting",o.DISCONNECTING="disconnecting",o.ERROR="error",o.DISPOSED="disposed"})(f||(f={}));function ee(o){switch(o){case"idle":return f.IDLE;case"connecting":return f.CONNECTING;case"connected":return f.CONNECTED;case"partial":return f.PARTIAL;case"disconnecting":return f.DISCONNECTING;case"disconnected":case"error":return f.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return f.RECONNECTING;case"disposed":return f.DISPOSED;default:return f.DISCONNECTED}}class te{constructor(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}reset(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}}const D=3,se=5e3,A=1e3,U=10,et=1e4,ie=3e3,I=3e4,k=24e3,O=16,tt=1,st=4096,R=0,ne="https://api.newportai.com/s2/aigc/api/vih_dispatcher",re="wss://api.newportai.com/s2/livekit-lisbon",oe="wss://api.newportai.com/s2/aigc/api/ws_dispatch/render/v1/stream",ae=6e4;var T;(function(o){o.NO_VIDEO_TRACK="NO_VIDEO_TRACK",o.TRACK_MUTED="TRACK_MUTED",o.TRACK_ENDED="TRACK_ENDED"})(T||(T={}));class ce{constructor(e){this.options=e,this._livekitUrl="",this._token="",this._wsUrl="",this.eventBus=new Z,this.sessionState=new te,e.debug&&i.setLevel(0),i.info("SDK Context initialized")}setLivekitConfig(e,t,s){this._livekitUrl=e||re,this._token=t,this._roomId=s}emitError(e,t){const s=u.fromError(e,t);return this.eventBus.emit("inner:sdk:error",{error:s}),s}setVideoOptions(e){this.options.video=Object.assign({},e,this.options.video||{})}setWsUrl(e){this._wsUrl=e}get wsUrl(){return this._wsUrl}get livekitUrl(){return this._livekitUrl}get token(){return this._token}get roomId(){return this._roomId}getLivekitConfig(){return{livekitUrl:this._livekitUrl,token:this._token}}dispose(){this.eventBus.dispose(),this.sessionState.reset(),i.info("SDK Context disposed")}}function K(o){return{ws:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:o}}}function W(o){return Object.freeze({ws:Object.freeze({...o.ws}),rtc:Object.freeze({...o.rtc}),overall:Object.freeze({...o.overall})})}class B{constructor(){this._transitionMap=null}get transitionMap(){if(!this._transitionMap){const e=new Map;for(const t of this.transitionTable)e.set(t.from,new Set(t.to));this._transitionMap=e}return this._transitionMap}getCurrentState(){return this._currentState}transitionTo(e,t){const s=this._currentState;if(!this.canTransition(s,e)){const n=this.transitionMap.get(s),r=n?Array.from(n).join(", "):"none";throw t?.error??new u(`Invalid state transition: cannot transition from "${s}" to "${e}". Current state: "${s}". Allowed transitions: [${r}]`,d.STATE_MACHINE_INVALID_STATE_TRANSITION)}this._currentState=e}canTransition(e,t){return e===t?!1:this.transitionMap.get(e)?.has(t)??!1}reset(){throw new Error("Method not implemented.")}}class de extends B{constructor(){super(...arguments),this._currentState="idle",this.transitionTable=[{from:"idle",to:["connecting","disposed"]},{from:"connecting",to:["connected","partial","error","idle","disposed"]},{from:"connected",to:["disconnecting","partial","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"partial",to:["connected","disconnecting","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"reconnecting:auto",to:["connected","partial","error","idle","disposed"]},{from:"reconnecting:manual",to:["connected","partial","error","idle","disposed"]},{from:"disconnecting",to:["disconnected","error","disposed"]},{from:"disconnected",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"error",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"disposed",to:[]}]}reset(){this._currentState="idle"}}class he extends l{constructor(e){super(),this._unsubscribes=[],this._context=e,this._fsm=new de,this._snapshot=K("idle")}start(){this.ensureNotDisposed();const e=this._context.eventBus,t=e.on("inner:rtc:connected",()=>{this._snapshot.rtc.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"livekit"}),this._onFactsChanged()}),s=e.on("inner:rtc:disconnected",h=>{this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"livekit",reason:h?.reason}),this._onFactsChanged()}),n=e.on("inner:rtc:video:available",()=>{this._snapshot.rtc.hasVideoTrack=!0}),r=e.on("inner:rtc:video:unavailable",()=>{this._snapshot.rtc.hasVideoTrack=!1}),a=e.on("inner:ws:connected",()=>{this._snapshot.ws.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"ws"}),this._onFactsChanged()}),c=e.on("inner:ws:disconnected",h=>{this._snapshot.ws.connected=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"ws",reason:h?.reason}),this._onFactsChanged()});this._unsubscribes=[t,s,n,r,a,c]}startConnecting(){this._transitionTo("connecting")}startDisconnecting(){this._transitionTo("disconnecting")}completeDisconnecting(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("disconnected")}captureSnapshotAndStartReconnectingManual(){const e=this.getSnapshot();return this._transitionTo("reconnecting:manual"),e}transitionToIdle(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("idle")}transitionToDisposed(){this._transitionTo("disposed")}getSnapshot(){return W(this._snapshot)}getState(){return this._fsm.getCurrentState()}canStartReconnectingManual(){const e=this._fsm.getCurrentState();return e==="connected"||e==="partial"||e==="disconnected"||e==="error"}_onFactsChanged(){const e=this._fsm.getCurrentState();if(e==="connecting"||e==="reconnecting:auto"||e==="reconnecting:manual"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected,s=this._snapshot.rtc.connected||this._snapshot.ws.connected;if(t){this._transitionTo("connected");return}if(s){this._transitionTo("partial");return}}if(e==="connected"||e==="partial"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected;if(!(this._snapshot.rtc.connected||this._snapshot.ws.connected)){this._transitionTo("disconnected");return}if(!t){this._transitionTo("partial");return}if(t&&e==="partial"){this._transitionTo("connected");return}}}_transitionTo(e){this._fsm.canTransition(this._fsm.getCurrentState(),e)&&(this._fsm.transitionTo(e),this._snapshot.overall.state=this._fsm.getCurrentState(),this._syncSessionState())}_syncSessionState(){const e=this._fsm.getCurrentState();this._context.sessionState.status=ee(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class $ extends l{get trackId(){return this._trackId}get participantId(){return this._participantId}get track(){return this._track}constructor(){super(),this._trackId=null,this._participantId=null,this._track=null,this._stream=null,this._videoElement=document.createElement("video"),this._videoElement.autoplay=!0,this._videoElement.playsInline=!0,this._videoElement.muted=!0,this._videoElement.crossOrigin="anonymous",this._videoElement.style.display="none",i.info("Video Source initialized with internal video element")}getInternalElement(){return this._videoElement}setTrack(e,t,s){this.ensureNotDisposed(),this._track&&this._track.stop(),this._track=e,this._trackId=t,this._participantId=s,this._stream||(this._stream=new MediaStream);const n=this._stream.getTracks();for(const r of n)if(r!==e)try{this._stream.removeTrack(r)}catch(a){i.warn("Failed to remove track from stream",a)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(r){i.error("Failed to add track to media stream:",r)}i.info("Video track ready")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){i.warn("Failed to remove track from stream",s)}}this._track&&(this._track.stop(),this._track=null),this._trackId=null,this._participantId=null,i.info("Video track cleared")}get hasTrack(){return this._track!==null&&this._track.readyState==="live"}onDispose(){this.clearTrack(),this._videoElement&&this._videoElement.parentElement&&this._videoElement.parentElement.removeChild(this._videoElement),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),i.info("Video Source disposed")}}class le extends l{constructor(){super(...arguments),this._renderer=null}getRenderer(){return this._renderer}setRenderer(e){this._renderer&&this._renderer.dispose(),this._renderer=e,i.debug("Raw render strategy renderer set")}getProcessors(){return[]}setProcessors(e){i.warn("Raw render strategy does not support processors")}attachToContainer(e){if(!this._renderer){i.warn("No renderer available to attach to container");return}if("getElement"in this._renderer&&typeof this._renderer.getElement=="function"){const t=this._renderer.getElement();t instanceof HTMLVideoElement&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="",i.debug("Video element attached to container in raw mode")))}}onDispose(){this._renderer&&(this._renderer.dispose(),this._renderer=null),i.info("Raw Render Strategy disposed")}}class ue{create(){return new le}}class b extends l{constructor(e,t){if(super(),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,this._resizeObserver=null,this._animationFrameId=null,this._isRendering=!1,this._renderLoop=()=>{if(!(!this._isRendering||!this._videoElement||!this._canvas||!this._ctx)){try{this._syncCanvasSize(),this._ctx.drawImage(this._videoElement,0,0,this._canvas.width,this._canvas.height)}catch(s){i.error("Error rendering from video to canvas:",s)}this._animationFrameId=requestAnimationFrame(this._renderLoop)}},e){if(this._canvas=e,this._ctx=e.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas")}else if(t)this._videoElement=t,this._createCanvasFromVideo();else throw new Error("CanvasRenderer requires either canvas or videoElement");i.info("Canvas Renderer created")}_createCanvasFromVideo(){if(!this._videoElement)return;if(this._parentElement=this._videoElement.parentElement,!this._parentElement)throw new Error("Video element must have a parent element");if(window.getComputedStyle(this._parentElement).position==="static"&&(this._parentElement.style.position="relative"),this._canvas=document.createElement("canvas"),this._ctx=this._canvas.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas");this._canvas.style.position="absolute",this._canvas.style.top="0",this._canvas.style.left="0",this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.pointerEvents="none",this._parentElement.appendChild(this._canvas),this._syncCanvasSize(),this._setupResizeObserver()}_syncCanvasSize(){if(!this._canvas||!this._videoElement||!this._ctx)return;const e=this._videoElement.videoWidth,t=this._videoElement.videoHeight,s=this._videoElement.clientWidth,n=this._videoElement.clientHeight;if(e>0&&t>0&&s>0&&n>0){const r=e/t,a=s/n;let c,h;r>a?(c=s,h=s/r):(c=n*r,h=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${h}px`;const m=(s-c)/2,g=(n-h)/2;this._canvas.style.left=`${m}px`,this._canvas.style.top=`${g}px`,this._canvas.width=e,this._canvas.height=t}}_setupResizeObserver(){!this._videoElement||typeof ResizeObserver>"u"||(this._resizeObserver=new ResizeObserver(()=>{this._syncCanvasSize()}),this._resizeObserver.observe(this._videoElement))}startRenderingFromVideo(){this._isRendering||!this._videoElement||!this._canvas||!this._ctx||(this._isRendering=!0,this._renderLoop())}stopRenderingFromVideo(){this._isRendering=!1,this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}setFitMode(e){this._canvas&&(this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.objectFit=e)}render(e){if(this.ensureNotDisposed(),!(!this._ctx||!this._canvas))try{const t=e.displayWidth,s=e.displayHeight;this._videoElement?this._syncCanvasSize():(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s);const n=e instanceof VideoFrame?e.displayWidth:e.videoWidth,r=e instanceof VideoFrame?e.displayHeight:e.videoHeight;n>0&&r>0&&(this._canvas.width!==n||this._canvas.height!==r)&&(this._canvas.width=n,this._canvas.height=r),this._ctx.clearRect(0,0,this._canvas.width,this._canvas.height),this._ctx.drawImage(e,0,0,this._canvas.width,this._canvas.height)}catch(t){i.error("Error rendering video frame to canvas:",t)}}getCanvas(){return this._canvas}getContext(){return this._ctx}onDispose(){this.stopRenderingFromVideo(),this._resizeObserver&&(this._resizeObserver.disconnect(),this._resizeObserver=null),this._canvas&&this._parentElement&&this._canvas.parentElement===this._parentElement&&this._parentElement.removeChild(this._canvas),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,i.info("Canvas Renderer disposed")}}class H extends l{constructor(e){super(),this._element=null,this._stream=null,this._track=null,this._element=e,i.info("Video Element Renderer created")}setTrack(e){if(this.ensureNotDisposed(),this._track=e,!this._element){i.warn("No video element available to set track");return}this._stream||(this._stream=new MediaStream,this._element.srcObject=this._stream);const t=this._stream.getTracks();for(const s of t)if(s!==e)try{this._stream.removeTrack(s)}catch(n){console.error(n)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(s){i.error("Failed to add track to media stream:",s)}this._element.play().catch(s=>{i.error("Error playing video:",s)}),i.debug("Video track set to element (stream reused)")}setOpacity(e){this._element&&(this._element.style.opacity=String(e))}getElement(){return this._element}setFitMode(e){this._element&&(this._element.style.width="100%",this._element.style.height="100%",this._element.style.objectFit=e)}render(e){i.debug("Render called in direct mode (no-op)")}onDispose(){this._element&&(this._element.srcObject=null),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.info("Video Element Renderer disposed")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){console.error(s)}}this._track=null}}class _e extends l{constructor(){super(...arguments),this._renderer=null,this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,this._videoFrameCallbackId=null,this._animationFrameId=null,this._isProcessing=!1,this._processVideoFrame=()=>{if(!this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2){this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame));return}try{let t=new VideoFrame(e,{timestamp:performance.now()*1e3});for(const s of this._processors){const n=s.process(t);n&&n!==t&&(t instanceof VideoFrame&&t.close(),t=n)}t instanceof ImageData?this._renderImageData(t):this._canvasRenderer.render(t),t instanceof VideoFrame&&t.close()}catch(t){i.error("Error processing video frame:",t)}this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame))},this._processVideoFrameFallback=()=>{if(!this._isProcessing||!this._canvasRenderer||!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2||e.paused){const a=this._canvasRenderer.getContext(),c=this._canvasRenderer.getCanvas();a&&c&&a.clearRect(0,0,c.width,c.height),this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback);return}const t=e.videoWidth,s=e.videoHeight,n=this._canvasRenderer.getCanvas();t>0&&n&&(n.width!==t||n.height!==s)&&(n.width=t,n.height=s);let r=new VideoFrame(e);try{for(const a of this._processors){const c=a.process(r);c&&c!==r&&(r instanceof VideoFrame&&r.close(),r=c)}r instanceof ImageData?this._renderImageData(r):this._canvasRenderer.render(r)}finally{r instanceof VideoFrame&&r.close()}this._isProcessing&&(this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback))}}getRenderer(){return this._renderer}setRenderer(e){if(this._renderer&&this._renderer.dispose(),this._renderer=e,e instanceof H){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new b(void 0,t),this._renderer=this._canvasRenderer,i.debug("Canvas renderer created in setRenderer (processed mode)"))}catch(s){i.warn("Failed to create CanvasRenderer in setRenderer, will retry in attachToContainer:",s)}}this._setupProcessingPipeline()}else this._videoElementRenderer=null,this._stopProcessingPipeline();i.debug("Processed render strategy renderer set")}addProcessor(e,t){t!==void 0&&t>=0&&t<this._processors.length?this._processors.splice(t,0,e):this._processors.push(e),this._setupProcessingPipeline()}getProcessors(){return this._processors}setProcessors(e){this._processors.forEach(t=>t.dispose()),this._processors=e,this._setupProcessingPipeline(),i.debug("Processed render strategy processor set")}attachToContainer(e){if(this._canvasRenderer){const t=this._canvasRenderer.getCanvas();if(t&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),i.debug("Canvas element attached to container in processed mode"))),this._videoElementRenderer){const s=this._videoElementRenderer.getElement();s&&(s.parentElement&&s.parentElement.removeChild(s),s.style.display="none")}}else if(this._videoElementRenderer){const t=this._videoElementRenderer.getElement();if(t){const s=t.parentElement!==null;s||e.appendChild(t);try{this._canvasRenderer=new b(void 0,t),this._renderer=this._canvasRenderer,!s&&t.parentElement===e&&e.removeChild(t),t.style.display="none";const n=this._canvasRenderer.getCanvas();n&&n.parentElement!==e&&e.appendChild(n),this._startProcessingLoop(),i.debug("Canvas created and attached to container in processed mode")}catch(n){i.error("Failed to create CanvasRenderer in attachToContainer:",n),!s&&t.parentElement===e&&e.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="")}}}}_setupProcessingPipeline(){if(!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e){i.warn("Video element not available for processing");return}if(e.parentElement&&e.parentElement.removeChild(e),e.style.display="none",e.readyState<2){const t=()=>{this._setupProcessingPipeline(),e.removeEventListener("loadedmetadata",t)};e.addEventListener("loadedmetadata",t);return}this._canvasRenderer&&(this._startProcessingLoop(),i.info("Processing pipeline setup completed"))}_stopProcessingPipeline(){this._stopProcessingLoop(),this._canvasRenderer&&(this._canvasRenderer.dispose(),this._canvasRenderer=null),this._videoElementRenderer&&(this._renderer=this._videoElementRenderer)}_startProcessingLoop(){this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer||!this._videoElementRenderer.getElement()||(this._isProcessing=!0,"requestVideoFrameCallback"in HTMLVideoElement.prototype?this._processVideoFrame():this._processVideoFrameFallback())}_stopProcessingLoop(){this._isProcessing=!1;const e=this._videoElementRenderer?.getElement();this._videoFrameCallbackId!==null&&e&&"cancelVideoFrameCallback"in e&&(e.cancelVideoFrameCallback(this._videoFrameCallbackId),this._videoFrameCallbackId=null),this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}_renderImageData(e){if(!this._canvasRenderer)return;const t=this._canvasRenderer.getContext(),s=this._canvasRenderer.getCanvas();t&&s&&((s.width!==e.width||s.height!==e.height)&&(s.width=e.width,s.height=e.height),t.clearRect(0,0,s.width,s.height),t.putImageData(e,0,0))}onDispose(){this._stopProcessingPipeline(),this._renderer&&(this._renderer.dispose(),this._renderer=null),this._processors.forEach(e=>e.dispose()),this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,i.info("Processed Render Strategy disposed")}}class pe{create(){return new _e}}class x{static getFactory(e){return e==="processed"?new pe:new ue}}class z extends l{constructor(e,t){super(),this._videoElementRenderer=null,this._containerElement=null,this._source=e;const s=t?.renderMode||"raw";this._containerElement=t?.containerElement||null,this._strategyFactory=x.getFactory(s),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),r=new H(n);this._videoElementRenderer=r,this._strategy.setRenderer(r),this._containerElement&&(this._strategy.attachToContainer(this._containerElement),i.info("Renderer elements attached to container via strategy")),i.info("Video Pipeline created")}setRenderMode(e){this.ensureNotDisposed();const t=x.getFactory(e);if(this._strategyFactory.constructor===t.constructor)return;const s=this._strategy.getRenderer(),n=this._strategy.getProcessors();this._strategy.dispose(),this._strategyFactory=t,this._strategy=this._strategyFactory.create(),s&&this._strategy.setRenderer(s),n.length>0&&this._strategy.setProcessors(n),this._containerElement&&this._strategy.attachToContainer(this._containerElement),i.info(`Video render mode changed to: ${e}`)}setRenderFitMode(e){this.ensureNotDisposed(),this._strategy.getRenderer()?.setFitMode(e),i.info(`Video render fit mode changed to: ${e}`)}setProcessor(e){this.ensureNotDisposed(),this._strategy.setProcessors(e?[e]:[])}setRenderer(e){this.ensureNotDisposed(),this._strategy.setRenderer(e)}get strategy(){return this._strategy}get videoElementRenderer(){return this._videoElementRenderer}onDispose(){if(this._strategy.dispose(),this._videoElementRenderer){const e=this._videoElementRenderer.getElement();e&&e.parentElement&&e.parentElement.removeChild(e),this._videoElementRenderer.dispose(),this._videoElementRenderer=null}i.info("Video Pipeline disposed")}}class me extends l{onDispose(){}}class y extends me{constructor(e){super(),this._program=null,this._texture=null,this._lastProcessedTimestamp=null,this._options=e,this._canvas=document.createElement("canvas");const t=this._canvas.getContext("webgl",{premultipliedAlpha:!1,preserveDrawingBuffer:!0});if(!t)throw new Error("WebGL not supported");this._gl=t,this._initGL(),i.info("WebGL Green Screen Processor initialized")}_initGL(){const e=this._gl,t=`
|
|
1
|
+
import{Room as ie,RoomEvent as C,DisconnectReason as ne,ParticipantEvent as re,ConnectionState as oe}from"livekit-client";var h;(function(r){r.SDK_AUTH_TOKEN_FAILED="SDK_AUTH_TOKEN_FAILED",r.SDK_GET_LIVEKIT_CONFIG_FAILED="SDK_GET_LIVEKIT_CONFIG_FAILED",r.SDK_RECONNECT_FAILED="SDK_RECONNECT_FAILED",r.SDK_DISCONNECT_FAILED="SDK_DISCONNECT_FAILED",r.SDK_NOT_CONNECTED="SDK_NOT_CONNECTED",r.SDK_CONNECT_FAILED="SDK_CONNECT_FAILED",r.SDK_SWITCH_VIDEO_FAILED="SDK_SWITCH_VIDEO_FAILED",r.SDK_ERROR="SDK_ERROR",r.SDK_PRECONNECT_FAILED="SDK_PRECONNECT_FAILED",r.SDK_INTERRUPT_CONVERSATION_FAILED="SDK_INTERRUPT_CONVERSATION_FAILED",r.SDK_INITIALIZATION_FAILED="SDK_INITIALIZATION_FAILED",r.SDK_INVALID_STATE_TRANSITION="SDK_INVALID_STATE_TRANSITION",r.NO_AVATARNAME="NO_AVATARNAME",r.AUDIO_CAPTURE_START_FAILED="AUDIO_CAPTURE_START_FAILED",r.AUDIO_CAPTURE_FAILED="AUDIO_CAPTURE_FAILED",r.AUDIO_INVALID_SAMPLE_RATE="AUDIO_INVALID_SAMPLE_RATE",r.AUDIO_INVALID_CHANNEL="AUDIO_INVALID_CHANNEL",r.AUDIO_INVALID_HEADER_LENGTH="AUDIO_INVALID_HEADER_LENGTH",r.AUDIO_INVALID_TYPE="AUDIO_INVALID_TYPE",r.AUDIO_INVALID_RESERVED="AUDIO_INVALID_RESERVED",r.AUDIO_CONTROLLER_NOT_AVAILABLE="AUDIO_CONTROLLER_NOT_AVAILABLE",r.CAMERA_CONTROLLER_NOT_AVAILABLE="CAMERA_CONTROLLER_NOT_AVAILABLE",r.WS_CONNECT_FAILED="WS_CONNECT_FAILED",r.WS_TEXT_SEND_FAILED="WS_TEXT_SEND_FAILED",r.WS_PARSE_ERROR="WS_PARSE_ERROR",r.WS_NOT_CONNECTED="WS_NOT_CONNECTED",r.WS_CONNECT_ERROR="WS_CONNECT_ERROR",r.LIVEKIT_CONNECT_FAILED="LIVEKIT_CONNECT_FAILED",r.CONVERSATION_CONTROLLER_NOT_AVAILABLE="CONVERSATION_CONTROLLER_NOT_AVAILABLE",r.STATE_MACHINE_INVALID_STATE_TRANSITION="STATE_MACHINE_INVALID_STATE_TRANSITION",r.OBJECT_DISPOSED="OBJECT_DISPOSED"})(h||(h={}));class u extends Error{constructor(e,t="SDK_ERROR",s){super(e),this.name="SDKError",this.code=t,this.cause=s,Object.setPrototypeOf(this,u.prototype)}static fromError(e,t){return e instanceof u?e:e instanceof Error?new u(e.message,t||h.SDK_ERROR,e):typeof e=="string"?new u(e,t||h.SDK_ERROR):new u("Unknown error",t||h.SDK_ERROR,e)}}class l{constructor(){this._disposed=!1}get isDisposed(){return this._disposed}dispose(){this._disposed||(this.onDispose(),this._disposed=!0)}ensureNotDisposed(){if(this._disposed)throw new u("Object has been disposed",h.OBJECT_DISPOSED)}}var E;(function(r){r[r.DEBUG=0]="DEBUG",r[r.INFO=1]="INFO",r[r.WARN=2]="WARN",r[r.ERROR=3]="ERROR",r[r.NONE=4]="NONE"})(E||(E={}));class i{static setLevel(e){this._level=e}static setPrefix(e){this._prefix=e}static debug(e,...t){this._level<=E.DEBUG&&console.debug(`${this._prefix} [DEBUG]`,e,...t)}static info(e,...t){this._level<=E.INFO&&console.info(`${this._prefix} [INFO]`,e,...t)}static warn(e,...t){this._level<=E.WARN&&console.warn(`${this._prefix} [WARN]`,e,...t)}static error(e,...t){this._level<=E.ERROR&&console.error(`${this._prefix} [ERROR]`,e,...t)}}i._level=E.ERROR;i._prefix="[SDK]";class ae extends l{constructor(){super(...arguments),this._listeners=new Map}on(e,t){this.ensureNotDisposed(),this._listeners.has(e)||this._listeners.set(e,new Set);const s=this._listeners.get(e);return s.add(t),i.debug(`Event listener added for: ${String(e)}`),()=>{s.delete(t),i.debug(`Event listener removed for: ${String(e)}`)}}once(e,t){this.ensureNotDisposed();const s=n=>{t(n),this.off(e,s)};return this.on(e,s)}off(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.delete(t)}emit(e,t){this.ensureNotDisposed();const s=this._listeners.get(e);s&&s.size>0&&(i.debug(`Emitting event: ${String(e)}`,t),s.forEach(n=>{try{n(t)}catch(o){i.error(`Error in event listener for ${String(e)}:`,o)}}))}removeAllListeners(){this._listeners.clear(),i.debug("All event listeners removed")}listenerCount(e){const t=this._listeners.get(e);return t?t.size:0}onDispose(){this.removeAllListeners()}}var f;(function(r){r.IDLE="idle",r.CONNECTING="connecting",r.CONNECTED="connected",r.PARTIAL="partial",r.DISCONNECTED="disconnected",r.RECONNECTING="reconnecting",r.DISCONNECTING="disconnecting",r.ERROR="error",r.DISPOSED="disposed"})(f||(f={}));function ce(r){switch(r){case"idle":return f.IDLE;case"connecting":return f.CONNECTING;case"connected":return f.CONNECTED;case"partial":return f.PARTIAL;case"disconnecting":return f.DISCONNECTING;case"disconnected":case"error":return f.DISCONNECTED;case"reconnecting:auto":case"reconnecting:manual":return f.RECONNECTING;case"disposed":return f.DISPOSED;default:return f.DISCONNECTED}}class de{constructor(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}reset(){this.status=f.DISCONNECTED,this.state=f.DISCONNECTED,this.isConnected=!1}}const b=3,he=5e3,R=1e3,G=10,Tt=1e4,le=3e3,x=3e4,L=24e3,P=16,St=1,At=4096,A=0,ue="https://api.newportai.com/s2/aigc/api/vih_dispatcher",_e="wss://api.newportai.com/s2/livekit-lisbon",pe="wss://api.newportai.com/s2/aigc/api/ws_dispatch/render/v1/stream",me=6e4;var T;(function(r){r.NO_VIDEO_TRACK="NO_VIDEO_TRACK",r.TRACK_MUTED="TRACK_MUTED",r.TRACK_ENDED="TRACK_ENDED"})(T||(T={}));const w=.7;class fe{constructor(e){this.options=e,this._livekitUrl="",this._token="",this._wsUrl="",this.eventBus=new ae,this.sessionState=new de,e.debug&&i.setLevel(0),i.info("SDK Context initialized")}setLivekitConfig(e,t,s){this._livekitUrl=e||_e,this._token=t,this._roomId=s}emitError(e,t){const s=u.fromError(e,t);return this.eventBus.emit("inner:sdk:error",{error:s}),s}setVideoOptions(e){this.options.video=Object.assign({},e,this.options.video||{})}setWsUrl(e){this._wsUrl=e}get wsUrl(){return this._wsUrl}get livekitUrl(){return this._livekitUrl}get token(){return this._token}get roomId(){return this._roomId}getLivekitConfig(){return{livekitUrl:this._livekitUrl,token:this._token}}dispose(){this.eventBus.dispose(),this.sessionState.reset(),i.info("SDK Context disposed")}}function j(r){return{ws:{connected:!1},rtc:{connected:!1,hasVideoTrack:!1},overall:{state:r}}}function Q(r){return Object.freeze({ws:Object.freeze({...r.ws}),rtc:Object.freeze({...r.rtc}),overall:Object.freeze({...r.overall})})}class X{constructor(){this._transitionMap=null}get transitionMap(){if(!this._transitionMap){const e=new Map;for(const t of this.transitionTable)e.set(t.from,new Set(t.to));this._transitionMap=e}return this._transitionMap}getCurrentState(){return this._currentState}transitionTo(e,t){const s=this._currentState;if(!this.canTransition(s,e)){const n=this.transitionMap.get(s),o=n?Array.from(n).join(", "):"none";throw t?.error??new u(`Invalid state transition: cannot transition from "${s}" to "${e}". Current state: "${s}". Allowed transitions: [${o}]`,h.STATE_MACHINE_INVALID_STATE_TRANSITION)}this._currentState=e}canTransition(e,t){return e===t?!1:this.transitionMap.get(e)?.has(t)??!1}reset(){throw new Error("Method not implemented.")}}class ge extends X{constructor(){super(...arguments),this._currentState="idle",this.transitionTable=[{from:"idle",to:["connecting","disposed"]},{from:"connecting",to:["connected","partial","error","idle","disposed"]},{from:"connected",to:["disconnecting","partial","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"partial",to:["connected","disconnecting","reconnecting:auto","reconnecting:manual","error","disposed"]},{from:"reconnecting:auto",to:["connected","partial","error","idle","disposed"]},{from:"reconnecting:manual",to:["connected","partial","error","idle","disposed"]},{from:"disconnecting",to:["disconnected","error","disposed"]},{from:"disconnected",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"error",to:["reconnecting:manual","reconnecting:auto","idle","disposed"]},{from:"disposed",to:[]}]}reset(){this._currentState="idle"}}class ve extends l{constructor(e){super(),this._unsubscribes=[],this._context=e,this._fsm=new ge,this._snapshot=j("idle")}start(){this.ensureNotDisposed();const e=this._context.eventBus,t=e.on("inner:rtc:connected",()=>{this._snapshot.rtc.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"livekit"}),this._onFactsChanged()}),s=e.on("inner:rtc:disconnected",d=>{this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"livekit",reason:d?.reason}),this._onFactsChanged()}),n=e.on("inner:rtc:video:available",()=>{this._snapshot.rtc.hasVideoTrack=!0}),o=e.on("inner:rtc:video:unavailable",()=>{this._snapshot.rtc.hasVideoTrack=!1}),a=e.on("inner:ws:connected",()=>{this._snapshot.ws.connected=!0,this._context.eventBus.emit("inner:sdk:connected",{source:"ws"}),this._onFactsChanged()}),c=e.on("inner:ws:disconnected",d=>{this._snapshot.ws.connected=!1,this._context.eventBus.emit("inner:sdk:disconnected",{source:"ws",reason:d?.reason}),this._onFactsChanged()});this._unsubscribes=[t,s,n,o,a,c]}startConnecting(){this._transitionTo("connecting")}startDisconnecting(){this._transitionTo("disconnecting")}completeDisconnecting(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("disconnected")}captureSnapshotAndStartReconnectingManual(){const e=this.getSnapshot();return this._transitionTo("reconnecting:manual"),e}transitionToIdle(){this._snapshot.ws.connected=!1,this._snapshot.rtc.connected=!1,this._snapshot.rtc.hasVideoTrack=!1,this._transitionTo("idle")}transitionToDisposed(){this._transitionTo("disposed")}getSnapshot(){return Q(this._snapshot)}getState(){return this._fsm.getCurrentState()}canStartReconnectingManual(){const e=this._fsm.getCurrentState();return e==="connected"||e==="partial"||e==="disconnected"||e==="error"}_onFactsChanged(){const e=this._fsm.getCurrentState();if(e==="connecting"||e==="reconnecting:auto"||e==="reconnecting:manual"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected,s=this._snapshot.rtc.connected||this._snapshot.ws.connected;if(t){this._transitionTo("connected");return}if(s){this._transitionTo("partial");return}}if(e==="connected"||e==="partial"){const t=this._snapshot.rtc.connected&&this._snapshot.ws.connected;if(!(this._snapshot.rtc.connected||this._snapshot.ws.connected)){this._transitionTo("disconnected");return}if(!t){this._transitionTo("partial");return}if(t&&e==="partial"){this._transitionTo("connected");return}}}_transitionTo(e){this._fsm.canTransition(this._fsm.getCurrentState(),e)&&(this._fsm.transitionTo(e),this._snapshot.overall.state=this._fsm.getCurrentState(),this._syncSessionState())}_syncSessionState(){const e=this._fsm.getCurrentState();this._context.sessionState.status=ce(e),this._context.sessionState.state=this._context.sessionState.status,this._context.sessionState.isConnected=e==="connected"}onDispose(){this._unsubscribes.forEach(e=>e()),this._unsubscribes=[]}}class Y extends l{get trackId(){return this._trackId}get participantId(){return this._participantId}get track(){return this._track}constructor(){super(),this._trackId=null,this._participantId=null,this._track=null,this._stream=null,this._videoElement=document.createElement("video"),this._videoElement.autoplay=!0,this._videoElement.playsInline=!0,this._videoElement.muted=!0,this._videoElement.crossOrigin="anonymous",this._videoElement.style.display="none",i.info("Video Source initialized with internal video element")}getInternalElement(){return this._videoElement}setTrack(e,t,s){this.ensureNotDisposed(),this._track&&this._track.stop(),this._track=e,this._trackId=t,this._participantId=s,this._stream||(this._stream=new MediaStream);const n=this._stream.getTracks();for(const o of n)if(o!==e)try{this._stream.removeTrack(o)}catch(a){i.warn("Failed to remove track from stream",a)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(o){i.error("Failed to add track to media stream:",o)}i.info("Video track ready")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){i.warn("Failed to remove track from stream",s)}}this._track&&(this._track.stop(),this._track=null),this._trackId=null,this._participantId=null,i.info("Video track cleared")}get hasTrack(){return this._track!==null&&this._track.readyState==="live"}onDispose(){this.clearTrack(),this._videoElement&&this._videoElement.parentElement&&this._videoElement.parentElement.removeChild(this._videoElement),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),i.info("Video Source disposed")}}class Ee extends l{constructor(){super(...arguments),this._renderer=null}getRenderer(){return this._renderer}setRenderer(e){this._renderer&&this._renderer.dispose(),this._renderer=e,i.debug("Raw render strategy renderer set")}getProcessors(){return[]}setProcessors(e){i.warn("Raw render strategy does not support processors")}attachToContainer(e){if(!this._renderer){i.warn("No renderer available to attach to container");return}if("getElement"in this._renderer&&typeof this._renderer.getElement=="function"){const t=this._renderer.getElement();t instanceof HTMLVideoElement&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="",i.debug("Video element attached to container in raw mode")))}}onDispose(){this._renderer&&(this._renderer.dispose(),this._renderer=null),i.info("Raw Render Strategy disposed")}}class Ce{create(){return new Ee}}class I extends l{constructor(e,t){if(super(),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,this._resizeObserver=null,this._animationFrameId=null,this._isRendering=!1,this._renderLoop=()=>{if(!(!this._isRendering||!this._videoElement||!this._canvas||!this._ctx)){try{this._syncCanvasSize(),this._ctx.drawImage(this._videoElement,0,0,this._canvas.width,this._canvas.height)}catch(s){i.error("Error rendering from video to canvas:",s)}this._animationFrameId=requestAnimationFrame(this._renderLoop)}},e){if(this._canvas=e,this._ctx=e.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas")}else if(t)this._videoElement=t,this._createCanvasFromVideo();else throw new Error("CanvasRenderer requires either canvas or videoElement");i.info("Canvas Renderer created")}_createCanvasFromVideo(){if(!this._videoElement)return;if(this._parentElement=this._videoElement.parentElement,!this._parentElement)throw new Error("Video element must have a parent element");if(window.getComputedStyle(this._parentElement).position==="static"&&(this._parentElement.style.position="relative"),this._canvas=document.createElement("canvas"),this._ctx=this._canvas.getContext("2d"),!this._ctx)throw new Error("Failed to get 2D context from canvas");this._canvas.style.position="absolute",this._canvas.style.top="0",this._canvas.style.left="0",this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.pointerEvents="none",this._parentElement.appendChild(this._canvas),this._syncCanvasSize(),this._setupResizeObserver()}_syncCanvasSize(){if(!this._canvas||!this._videoElement||!this._ctx)return;const e=this._videoElement.videoWidth,t=this._videoElement.videoHeight,s=this._videoElement.clientWidth,n=this._videoElement.clientHeight;if(e>0&&t>0&&s>0&&n>0){const o=e/t,a=s/n;let c,d;o>a?(c=s,d=s/o):(c=n*o,d=n),this._canvas.style.width=`${c}px`,this._canvas.style.height=`${d}px`;const m=(s-c)/2,g=(n-d)/2;this._canvas.style.left=`${m}px`,this._canvas.style.top=`${g}px`,this._canvas.width=e,this._canvas.height=t}}_setupResizeObserver(){!this._videoElement||typeof ResizeObserver>"u"||(this._resizeObserver=new ResizeObserver(()=>{this._syncCanvasSize()}),this._resizeObserver.observe(this._videoElement))}startRenderingFromVideo(){this._isRendering||!this._videoElement||!this._canvas||!this._ctx||(this._isRendering=!0,this._renderLoop())}stopRenderingFromVideo(){this._isRendering=!1,this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}setFitMode(e){this._canvas&&(this._canvas.style.width="100%",this._canvas.style.height="100%",this._canvas.style.objectFit=e)}render(e){if(this.ensureNotDisposed(),!(!this._ctx||!this._canvas))try{const t=e.displayWidth,s=e.displayHeight;this._videoElement?this._syncCanvasSize():(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s);const n=e instanceof VideoFrame?e.displayWidth:e.videoWidth,o=e instanceof VideoFrame?e.displayHeight:e.videoHeight;n>0&&o>0&&(this._canvas.width!==n||this._canvas.height!==o)&&(this._canvas.width=n,this._canvas.height=o),this._ctx.clearRect(0,0,this._canvas.width,this._canvas.height),this._ctx.drawImage(e,0,0,this._canvas.width,this._canvas.height)}catch(t){i.error("Error rendering video frame to canvas:",t)}}getCanvas(){return this._canvas}getContext(){return this._ctx}onDispose(){this.stopRenderingFromVideo(),this._resizeObserver&&(this._resizeObserver.disconnect(),this._resizeObserver=null),this._canvas&&this._parentElement&&this._canvas.parentElement===this._parentElement&&this._parentElement.removeChild(this._canvas),this._canvas=null,this._ctx=null,this._videoElement=null,this._parentElement=null,i.info("Canvas Renderer disposed")}}class J extends l{constructor(e){super(),this._element=null,this._stream=null,this._track=null,this._element=e,i.info("Video Element Renderer created")}setTrack(e){if(this.ensureNotDisposed(),this._track=e,!this._element){i.warn("No video element available to set track");return}this._stream||(this._stream=new MediaStream,this._element.srcObject=this._stream);const t=this._stream.getTracks();for(const s of t)if(s!==e)try{this._stream.removeTrack(s)}catch(n){console.error(n)}if(!this._stream.getTracks().includes(e))try{this._stream.addTrack(e)}catch(s){i.error("Failed to add track to media stream:",s)}this._element.play().catch(s=>{i.error("Error playing video:",s)}),i.debug("Video track set to element (stream reused)")}setOpacity(e){this._element&&(this._element.style.opacity=String(e))}getElement(){return this._element}setFitMode(e){this._element&&(this._element.style.width="100%",this._element.style.height="100%",this._element.style.objectFit=e)}render(e){i.debug("Render called in direct mode (no-op)")}onDispose(){this._element&&(this._element.srcObject=null),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.info("Video Element Renderer disposed")}clearTrack(){if(this._stream){const e=this._stream.getTracks();for(const t of e)try{this._stream.removeTrack(t)}catch(s){console.error(s)}}this._track=null}}class Te extends l{constructor(){super(...arguments),this._renderer=null,this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,this._videoFrameCallbackId=null,this._animationFrameId=null,this._isProcessing=!1,this._processVideoFrame=()=>{if(!this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2){this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame));return}try{let t=new VideoFrame(e,{timestamp:performance.now()*1e3});for(const s of this._processors){const n=s.process(t);n&&n!==t&&(t instanceof VideoFrame&&t.close(),t=n)}t instanceof ImageData?this._renderImageData(t):this._canvasRenderer.render(t),t instanceof VideoFrame&&t.close()}catch(t){i.error("Error processing video frame:",t)}this._isProcessing&&e&&(this._videoFrameCallbackId=e.requestVideoFrameCallback(this._processVideoFrame))},this._processVideoFrameFallback=()=>{if(!this._isProcessing||!this._canvasRenderer||!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e||e.readyState<2||e.paused){const a=this._canvasRenderer.getContext(),c=this._canvasRenderer.getCanvas();a&&c&&a.clearRect(0,0,c.width,c.height),this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback);return}const t=e.videoWidth,s=e.videoHeight,n=this._canvasRenderer.getCanvas();t>0&&n&&(n.width!==t||n.height!==s)&&(n.width=t,n.height=s);let o=new VideoFrame(e);try{for(const a of this._processors){const c=a.process(o);c&&c!==o&&(o instanceof VideoFrame&&o.close(),o=c)}o instanceof ImageData?this._renderImageData(o):this._canvasRenderer.render(o)}finally{o instanceof VideoFrame&&o.close()}this._isProcessing&&(this._animationFrameId=requestAnimationFrame(this._processVideoFrameFallback))}}getRenderer(){return this._renderer}setRenderer(e){if(this._renderer&&this._renderer.dispose(),this._renderer=e,e instanceof J){if(this._videoElementRenderer=e,!this._canvasRenderer){const t=this._videoElementRenderer.getElement();if(t)try{t.parentElement&&(this._canvasRenderer=new I(void 0,t),this._renderer=this._canvasRenderer,i.debug("Canvas renderer created in setRenderer (processed mode)"))}catch(s){i.warn("Failed to create CanvasRenderer in setRenderer, will retry in attachToContainer:",s)}}this._setupProcessingPipeline()}else this._videoElementRenderer=null,this._stopProcessingPipeline();i.debug("Processed render strategy renderer set")}addProcessor(e,t){t!==void 0&&t>=0&&t<this._processors.length?this._processors.splice(t,0,e):this._processors.push(e),this._setupProcessingPipeline()}getProcessors(){return this._processors}setProcessors(e){this._processors.forEach(t=>t.dispose()),this._processors=e,this._setupProcessingPipeline(),i.debug("Processed render strategy processor set")}attachToContainer(e){if(this._canvasRenderer){const t=this._canvasRenderer.getCanvas();if(t&&(t.parentElement&&t.parentElement!==e&&t.parentElement.removeChild(t),t.parentElement!==e&&(e.appendChild(t),i.debug("Canvas element attached to container in processed mode"))),this._videoElementRenderer){const s=this._videoElementRenderer.getElement();s&&(s.parentElement&&s.parentElement.removeChild(s),s.style.display="none")}}else if(this._videoElementRenderer){const t=this._videoElementRenderer.getElement();if(t){const s=t.parentElement!==null;s||e.appendChild(t);try{this._canvasRenderer=new I(void 0,t),this._renderer=this._canvasRenderer,!s&&t.parentElement===e&&e.removeChild(t),t.style.display="none";const n=this._canvasRenderer.getCanvas();n&&n.parentElement!==e&&e.appendChild(n),this._startProcessingLoop(),i.debug("Canvas created and attached to container in processed mode")}catch(n){i.error("Failed to create CanvasRenderer in attachToContainer:",n),!s&&t.parentElement===e&&e.removeChild(t),t.parentElement!==e&&(e.appendChild(t),t.style.display="")}}}}_setupProcessingPipeline(){if(!this._videoElementRenderer)return;const e=this._videoElementRenderer.getElement();if(!e){i.warn("Video element not available for processing");return}if(e.parentElement&&e.parentElement.removeChild(e),e.style.display="none",e.readyState<2){const t=()=>{this._setupProcessingPipeline(),e.removeEventListener("loadedmetadata",t)};e.addEventListener("loadedmetadata",t);return}this._canvasRenderer&&(this._startProcessingLoop(),i.info("Processing pipeline setup completed"))}_stopProcessingPipeline(){this._stopProcessingLoop(),this._canvasRenderer&&(this._canvasRenderer.dispose(),this._canvasRenderer=null),this._videoElementRenderer&&(this._renderer=this._videoElementRenderer)}_startProcessingLoop(){this._isProcessing||!this._videoElementRenderer||!this._canvasRenderer||!this._videoElementRenderer.getElement()||(this._isProcessing=!0,"requestVideoFrameCallback"in HTMLVideoElement.prototype?this._processVideoFrame():this._processVideoFrameFallback())}_stopProcessingLoop(){this._isProcessing=!1;const e=this._videoElementRenderer?.getElement();this._videoFrameCallbackId!==null&&e&&"cancelVideoFrameCallback"in e&&(e.cancelVideoFrameCallback(this._videoFrameCallbackId),this._videoFrameCallbackId=null),this._animationFrameId!==null&&(cancelAnimationFrame(this._animationFrameId),this._animationFrameId=null)}_renderImageData(e){if(!this._canvasRenderer)return;const t=this._canvasRenderer.getContext(),s=this._canvasRenderer.getCanvas();t&&s&&((s.width!==e.width||s.height!==e.height)&&(s.width=e.width,s.height=e.height),t.clearRect(0,0,s.width,s.height),t.putImageData(e,0,0))}onDispose(){this._stopProcessingPipeline(),this._renderer&&(this._renderer.dispose(),this._renderer=null),this._processors.forEach(e=>e.dispose()),this._processors=[],this._videoElementRenderer=null,this._canvasRenderer=null,i.info("Processed Render Strategy disposed")}}class Se{create(){return new Te}}class F{static getFactory(e){return e==="processed"?new Se:new Ce}}class Z extends l{constructor(e,t){super(),this._videoElementRenderer=null,this._containerElement=null,this._source=e;const s=t?.renderMode||"raw";this._containerElement=t?.containerElement||null,this._strategyFactory=F.getFactory(s),this._strategy=this._strategyFactory.create();const n=this._source.getInternalElement(),o=new J(n);this._videoElementRenderer=o,this._strategy.setRenderer(o),this._containerElement&&(this._strategy.attachToContainer(this._containerElement),i.info("Renderer elements attached to container via strategy")),i.info("Video Pipeline created")}setRenderMode(e){this.ensureNotDisposed();const t=F.getFactory(e);if(this._strategyFactory.constructor===t.constructor)return;const s=this._strategy.getRenderer(),n=this._strategy.getProcessors();this._strategy.dispose(),this._strategyFactory=t,this._strategy=this._strategyFactory.create(),s&&this._strategy.setRenderer(s),n.length>0&&this._strategy.setProcessors(n),this._containerElement&&this._strategy.attachToContainer(this._containerElement),i.info(`Video render mode changed to: ${e}`)}setRenderFitMode(e){this.ensureNotDisposed(),this._strategy.getRenderer()?.setFitMode(e),i.info(`Video render fit mode changed to: ${e}`)}setProcessor(e){this.ensureNotDisposed(),this._strategy.setProcessors(e?[e]:[])}setRenderer(e){this.ensureNotDisposed(),this._strategy.setRenderer(e)}get strategy(){return this._strategy}get videoElementRenderer(){return this._videoElementRenderer}onDispose(){if(this._strategy.dispose(),this._videoElementRenderer){const e=this._videoElementRenderer.getElement();e&&e.parentElement&&e.parentElement.removeChild(e),this._videoElementRenderer.dispose(),this._videoElementRenderer=null}i.info("Video Pipeline disposed")}}class Ae extends l{onDispose(){}}class k extends Ae{constructor(e){super(),this._program=null,this._texture=null,this._lastProcessedTimestamp=null,this._options=e,this._canvas=document.createElement("canvas");const t=this._canvas.getContext("webgl",{premultipliedAlpha:!1,preserveDrawingBuffer:!0});if(!t)throw new Error("WebGL not supported");this._gl=t,this._initGL(),i.info("WebGL Green Screen Processor initialized")}_initGL(){const e=this._gl,t=`
|
|
2
2
|
attribute vec2 aPosition;
|
|
3
3
|
attribute vec2 aTexCoord;
|
|
4
4
|
varying vec2 vTexCoord;
|
|
@@ -52,7 +52,7 @@ import{Room as Q,RoomEvent as C,DisconnectReason as Y,ParticipantEvent as X,Conn
|
|
|
52
52
|
|
|
53
53
|
gl_FragColor = vec4(rgb, alpha * color.a);
|
|
54
54
|
}
|
|
55
|
-
`;this._program=this._createProgram(t,s),this._texture=e.createTexture();const n=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),r=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,r),e.bufferData(e.ARRAY_BUFFER,n,e.STATIC_DRAW);const a=e.getAttribLocation(this._program,"aPosition"),c=e.getAttribLocation(this._program,"aTexCoord");e.enableVertexAttribArray(a),e.vertexAttribPointer(a,2,e.FLOAT,!1,16,0),e.enableVertexAttribArray(c),e.vertexAttribPointer(c,2,e.FLOAT,!1,16,8)}process(e){if(!this._options.enabled)return e;const{displayWidth:t,displayHeight:s,timestamp:n}=e;(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s,this._gl.viewport(0,0,t,s));const r=this._gl;r.useProgram(this._program),r.activeTexture(r.TEXTURE0),r.bindTexture(r.TEXTURE_2D,this._texture),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,e),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_S,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_WRAP_T,r.CLAMP_TO_EDGE),r.texParameteri(r.TEXTURE_2D,r.TEXTURE_MIN_FILTER,r.LINEAR);const a=this._options.chromaKey||[0,255,0];r.uniform3f(r.getUniformLocation(this._program,"uChromaKey"),a[0]/255,a[1]/255,a[2]/255),r.uniform1f(r.getUniformLocation(this._program,"uSimilarity"),this._options.similarity??.3),r.uniform1f(r.getUniformLocation(this._program,"uSmoothness"),this._options.smoothness??.25),r.uniform1f(r.getUniformLocation(this._program,"uSpill"),this._options.despillStrength??1.15),r.drawArrays(r.TRIANGLE_STRIP,0,4);const c=new VideoFrame(this._canvas,{timestamp:n,duration:e.duration??void 0});return e.close(),c}_createProgram(e,t){const s=this._gl,n=(a,c)=>{const h=s.createShader(a);return s.shaderSource(h,c),s.compileShader(h),h},r=s.createProgram();return s.attachShader(r,n(s.VERTEX_SHADER,e)),s.attachShader(r,n(s.FRAGMENT_SHADER,t)),s.linkProgram(r),r}onDispose(){const e=this._gl;this._texture&&e.deleteTexture(this._texture),this._program&&e.deleteProgram(this._program),i.info("WebGL Green Screen Processor disposed")}updateOptions(e){this._options={...this._options,...e}}}class fe extends l{constructor(e,t){if(super(),this._context=e,this._source=new $,this._pipeline=new z(this._source,t),t?.renderMode==="processed"&&t?.greenScreen?.enabled){const s=new y(t.greenScreen);this._pipeline.setProcessor(s),i.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(t?.fitMode||"contain"),i.info("Video Controller created")}setTrack(e,t,s){this.ensureNotDisposed(),this._source.setTrack(e,t,s)}removeTrack(e){this.ensureNotDisposed(),this._source.trackId===e&&this._source.clearTrack()}setRenderMode(e){this.ensureNotDisposed(),this._pipeline.setRenderMode(e),this._context.eventBus.emit("inner:video:render:mode:changed",{mode:e})}setRenderFitMode(e){this.ensureNotDisposed(),this._pipeline.setRenderFitMode(e)}getSource(){return this.ensureNotDisposed(),this._source}onDispose(){this._source.dispose(),this._pipeline.dispose(),i.info("Video Controller disposed")}}class ge extends l{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class ve extends l{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new ge,i.info("Input Audio Pipeline created")}setProcessingStrategy(e){this.ensureNotDisposed(),this._processingStrategy&&this._processingStrategy.dispose(),this._processingStrategy=e,i.debug("Audio processing strategy set")}get processingStrategy(){return this._processingStrategy}addProcessor(e){this.ensureNotDisposed(),this._processors.push(e),i.debug(`Audio processor added. Total: ${this._processors.length}`)}removeProcessor(e){this.ensureNotDisposed();const t=this._processors.indexOf(e);t>=0&&(this._processors.splice(t,1),i.debug(`Audio processor removed. Total: ${this._processors.length}`))}process(e,t){this.ensureNotDisposed();let s=e;try{s=this._processingStrategy.process(s,t)}catch(n){i.error("Error in audio processing strategy:",n)}for(const n of this._processors)try{s=n.process(s,t)}catch(r){i.error("Error in audio processor:",r);continue}this._onProcessedAudio&&this._onProcessedAudio(s,t)}onDispose(){if(this._processingStrategy)try{this._processingStrategy.dispose()}catch(e){i.error("Error disposing audio processing strategy:",e)}this._processors.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing audio processor:",t)}}),this._processors.length=0,this._onProcessedAudio=void 0,i.info("Input Audio Pipeline disposed")}}class Ee extends l{constructor(){super(),this._audioElement=null,this._stream=null,this._track=null,this._volume=1,this._isMuted=!1,this._audioElement=document.createElement("audio"),this._audioElement.autoplay=!0,this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.info("Output Audio Pipeline created")}getAudioElement(){if(!this._audioElement)throw new u("Audio element not found");return this._audioElement}setTrack(e){this.ensureNotDisposed(),this._track&&this._track!==e&&this._track.stop(),this._track=e,this._audioElement&&(this._stream&&(this._stream.getTracks().forEach(t=>t.stop()),this._stream=null),this._stream=new MediaStream([e]),this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.debug("Audio track set to output pipeline"))}removeTrack(){this.ensureNotDisposed(),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.debug("Audio track removed from output pipeline")}setVolume(e){this.ensureNotDisposed(),this._volume=Math.max(0,Math.min(1,e)),this._audioElement&&(this._audioElement.volume=this._volume),i.debug(`Audio output volume set to: ${this._volume}`)}getVolume(){return this._volume}mute(){this.ensureNotDisposed(),this._isMuted=!0,this._audioElement&&(this._audioElement.muted=!0),i.debug("Audio output muted")}unmute(){this.ensureNotDisposed(),this._isMuted=!1,this._audioElement&&(this._audioElement.muted=!1),i.debug("Audio output unmuted")}isMuted(){return this._isMuted}onDispose(){this.removeTrack(),this._audioElement&&(this._audioElement=null),i.info("Output Audio Pipeline disposed")}}const Ce=1,Te=0,Se={0:0,1:1,2:2};function Re(o){return o<0?0:o>4095?4095:Math.floor(o)}function Ae(o){if(o<0)return 0;const e=1048575;return o>e?o&e:Math.floor(o)}function we(o){return o<0?0:o>4095?4095:Math.floor(o)}function De(o){return o<0?0:o>65535?65535:Math.floor(o)}function be(o){if(o!==0&&o!==1&&o!==2)throw new u(`Invalid sample rate encoding: ${o}. Must be 0 (16kHz), 1 (24kHz), or 2 (48kHz)`,d.AUDIO_INVALID_SAMPLE_RATE)}function ye(o){be(o.sampleRate);const e=Re(o.sequenceNumber),t=Ae(o.timestamp),s=we(o.frameSize),n=De(o.payloadLength);if(o.channel!==0&&o.channel!==1)throw new u(`Invalid channel value: ${o.channel}. Must be 0 (mono) or 1 (stereo)`,d.AUDIO_INVALID_CHANNEL);const r=new Uint8Array(9);let a=Ce<<6;a|=o.channel<<5,a|=(o.keyFrame?1:0)<<4,a|=e>>8&15,r[0]=a,r[1]=e&255,r[2]=t>>12&255,r[3]=t>>4&255;const c=Se[o.sampleRate];let h=(t&15)<<4;h|=c<<2,h|=s>>10&3,r[4]=h,r[5]=s>>2&255;let m=(s&3)<<6;return m|=Te&63,r[6]=m,r[7]=n>>8&255,r[8]=n&255,r}class Ne{constructor(e){this.transport=e}sendBinary(e){this.transport.sendBinary(e)}isConnected(){return this.transport.isConnected}}class Ie extends l{constructor(e,t){super(),this._wsSender=null,this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=0,this._isSending=!1,this._audioFrameListener=null,this._audioBuffer=[],this._bufferedSampleCount=0,this._targetSampleRate=24e3,this._bitDepth=16,this._minSamplesToSend=0,this._context=e,this._options=t;const s=t?.input;this._targetSampleRate=s?.sampleRate||24e3,this._bitDepth=s?.bitDepth||16,this._minSamplesToSend=Math.floor(this._targetSampleRate/10),i.info(`Audio Frame Sender created: sampleRate=${this._targetSampleRate}Hz, bitDepth=${this._bitDepth}bit, minSamples=${this._minSamplesToSend}`)}setWebSocketSender(e){this._wsSender=e}setConversationTransport(e){this._wsSender=new Ne(e)}start(){if(this.ensureNotDisposed(),this._isSending){i.warn("Audio frame sending already started");return}this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=Date.now(),this._audioFrameListener=e=>{this._handleAudioFrame(e.data,e.sampleRate)},this._context.eventBus.on("inner:audio:frame:received",this._audioFrameListener),this._isSending=!0,i.info("Audio frame sending started")}stop(){this.ensureNotDisposed(),this._isSending&&(this._flushBuffer(),this._audioFrameListener&&(this._context.eventBus.off("inner:audio:frame:received",this._audioFrameListener),this._audioFrameListener=null),this._isSending=!1,i.info("Audio frame sending stopped"))}_handleAudioFrame(e,t){if(this._isSending)try{Math.abs(t-this._targetSampleRate)>1&&i.warn(`Sample rate mismatch: expected ${this._targetSampleRate}Hz, got ${t}Hz`),this._audioBuffer.push(new Float32Array(e)),this._bufferedSampleCount+=e.length,this._bufferedSampleCount>=this._minSamplesToSend&&this._flushBuffer()}catch(s){i.error("Error handling audio frame:",s)}}_flushBuffer(){if(!(this._audioBuffer.length===0||this._bufferedSampleCount===0))try{const e=this._bufferedSampleCount,t=new Float32Array(e);let s=0;for(const p of this._audioBuffer)t.set(p,s),s+=p.length;this._audioBuffer=[],this._bufferedSampleCount=0;const n=this._convertToPCM(t,this._bitDepth),r=Math.ceil(this._bitDepth/8),a=n.length*r,c=this._getSampleRateEncoding(this._targetSampleRate),h=Math.floor(Date.now()-this._audioStartTime),m={channel:(this._options?.input?.channels||1)===1?0:1,keyFrame:!!this._isKeyFrame,sequenceNumber:this._sequenceNumber,timestamp:h,sampleRate:c,frameSize:t.length,payloadLength:a},g=this._pcmToUint8Array(n,this._bitDepth);if(setTimeout(()=>{try{this._context.eventBus.emit("inner:audio:payload:send",{payload:g,sampleRate:this._targetSampleRate,channels:this._options?.input?.channels||1,sequenceNumber:m.sequenceNumber,timestamp:m.timestamp,keyFrame:m.keyFrame,frameSize:m.frameSize})}catch(p){i.error("Error emitting inner:audio:payload:send event:",p)}},0),this._wsSender&&this._wsSender.isConnected())try{const p=ye(m),v=new Uint8Array(p.length+g.length);v.set(p,0),v.set(g,p.length),this._wsSender.sendBinary(v),i.debug(`Audio frame sent via WS: seq=${m.sequenceNumber}, samples=${t.length}, size=${v.length} bytes`)}catch(p){i.error("Error sending audio frame via WebSocket:",p)}this._isKeyFrame=!1,this._sequenceNumber=(this._sequenceNumber+1)%4096}catch(e){i.error("Error flushing audio buffer:",e)}}_convertToPCM(e,t){if(t===16){const s=new Int16Array(e.length);for(let n=0;n<e.length;n++){const r=Math.max(-1,Math.min(1,e[n]));s[n]=r<0?r*32768:r*32767}return s}else if(t===32){const s=new Int32Array(e.length);for(let n=0;n<e.length;n++){const r=Math.max(-1,Math.min(1,e[n]));s[n]=r*2147483647}return s}else return i.warn(`Unsupported bit depth: ${t}, using 16-bit`),this._convertToPCM(e,16)}_pcmToUint8Array(e,t){const s=Math.ceil(t/8),n=new Uint8Array(e.length*s),r=new DataView(n.buffer);for(let a=0;a<e.length;a++)t===16?r.setInt16(a*2,e[a],!0):t===32&&r.setInt32(a*4,e[a],!0);return n}_getSampleRateEncoding(e){return e===16e3?0:e===24e3?1:e===48e3?2:(i.warn(`Unsupported sample rate: ${e}, defaulting to 48kHz`),1)}onDispose(){this.stop(),i.info("Audio Frame Sender disposed")}}class ke extends l{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(s){i.error("Error in audio data callback:",s)}}}const Oe=`
|
|
55
|
+
`;this._program=this._createProgram(t,s),this._texture=e.createTexture();const n=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),o=e.createBuffer();e.bindBuffer(e.ARRAY_BUFFER,o),e.bufferData(e.ARRAY_BUFFER,n,e.STATIC_DRAW);const a=e.getAttribLocation(this._program,"aPosition"),c=e.getAttribLocation(this._program,"aTexCoord");e.enableVertexAttribArray(a),e.vertexAttribPointer(a,2,e.FLOAT,!1,16,0),e.enableVertexAttribArray(c),e.vertexAttribPointer(c,2,e.FLOAT,!1,16,8)}process(e){if(!this._options.enabled)return e;const{displayWidth:t,displayHeight:s,timestamp:n}=e;(this._canvas.width!==t||this._canvas.height!==s)&&(this._canvas.width=t,this._canvas.height=s,this._gl.viewport(0,0,t,s));const o=this._gl;o.useProgram(this._program),o.activeTexture(o.TEXTURE0),o.bindTexture(o.TEXTURE_2D,this._texture),o.texImage2D(o.TEXTURE_2D,0,o.RGBA,o.RGBA,o.UNSIGNED_BYTE,e),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_S,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_WRAP_T,o.CLAMP_TO_EDGE),o.texParameteri(o.TEXTURE_2D,o.TEXTURE_MIN_FILTER,o.LINEAR);const a=this._options.chromaKey||[0,255,0];o.uniform3f(o.getUniformLocation(this._program,"uChromaKey"),a[0]/255,a[1]/255,a[2]/255),o.uniform1f(o.getUniformLocation(this._program,"uSimilarity"),this._options.similarity??.3),o.uniform1f(o.getUniformLocation(this._program,"uSmoothness"),this._options.smoothness??.25),o.uniform1f(o.getUniformLocation(this._program,"uSpill"),this._options.despillStrength??1.15),o.drawArrays(o.TRIANGLE_STRIP,0,4);const c=new VideoFrame(this._canvas,{timestamp:n,duration:e.duration??void 0});return e.close(),c}_createProgram(e,t){const s=this._gl,n=(a,c)=>{const d=s.createShader(a);return s.shaderSource(d,c),s.compileShader(d),d},o=s.createProgram();return s.attachShader(o,n(s.VERTEX_SHADER,e)),s.attachShader(o,n(s.FRAGMENT_SHADER,t)),s.linkProgram(o),o}onDispose(){const e=this._gl;this._texture&&e.deleteTexture(this._texture),this._program&&e.deleteProgram(this._program),i.info("WebGL Green Screen Processor disposed")}updateOptions(e){this._options={...this._options,...e}}}class we extends l{constructor(e,t){if(super(),this._context=e,this._source=new Y,this._pipeline=new Z(this._source,t),t?.renderMode==="processed"&&t?.greenScreen?.enabled){const s=new k(t.greenScreen);this._pipeline.setProcessor(s),i.info("Green screen processor enabled")}this._pipeline.setRenderFitMode(t?.fitMode||"contain"),i.info("Video Controller created")}setTrack(e,t,s){this.ensureNotDisposed(),this._source.setTrack(e,t,s)}removeTrack(e){this.ensureNotDisposed(),this._source.trackId===e&&this._source.clearTrack()}setRenderMode(e){this.ensureNotDisposed(),this._pipeline.setRenderMode(e),this._context.eventBus.emit("inner:video:render:mode:changed",{mode:e})}setRenderFitMode(e){this.ensureNotDisposed(),this._pipeline.setRenderFitMode(e)}getSource(){return this.ensureNotDisposed(),this._source}onDispose(){this._source.dispose(),this._pipeline.dispose(),i.info("Video Controller disposed")}}class Re extends l{process(e,t){return this.ensureNotDisposed(),e}onDispose(){}}class De extends l{set onProcessedAudio(e){this._onProcessedAudio=e}constructor(e){super(),this._processors=[],this._processingStrategy=new Re,i.info("Input Audio Pipeline created")}setProcessingStrategy(e){this.ensureNotDisposed(),this._processingStrategy&&this._processingStrategy.dispose(),this._processingStrategy=e,i.debug("Audio processing strategy set")}get processingStrategy(){return this._processingStrategy}addProcessor(e){this.ensureNotDisposed(),this._processors.push(e),i.debug(`Audio processor added. Total: ${this._processors.length}`)}removeProcessor(e){this.ensureNotDisposed();const t=this._processors.indexOf(e);t>=0&&(this._processors.splice(t,1),i.debug(`Audio processor removed. Total: ${this._processors.length}`))}process(e,t){this.ensureNotDisposed();let s=e;try{s=this._processingStrategy.process(s,t)}catch(n){i.error("Error in audio processing strategy:",n)}for(const n of this._processors)try{s=n.process(s,t)}catch(o){i.error("Error in audio processor:",o);continue}this._onProcessedAudio&&this._onProcessedAudio(s,t)}onDispose(){if(this._processingStrategy)try{this._processingStrategy.dispose()}catch(e){i.error("Error disposing audio processing strategy:",e)}this._processors.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing audio processor:",t)}}),this._processors.length=0,this._onProcessedAudio=void 0,i.info("Input Audio Pipeline disposed")}}class ye extends l{constructor(){super(),this._audioElement=null,this._stream=null,this._track=null,this._volume=1,this._isMuted=!1,this._audioElement=document.createElement("audio"),this._audioElement.autoplay=!0,this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.info("Output Audio Pipeline created")}getAudioElement(){if(!this._audioElement)throw new u("Audio element not found");return this._audioElement}setTrack(e){this.ensureNotDisposed(),this._track&&this._track!==e&&this._track.stop(),this._track=e,this._audioElement&&(this._stream&&(this._stream.getTracks().forEach(t=>t.stop()),this._stream=null),this._stream=new MediaStream([e]),this._audioElement.volume=this._volume,this._audioElement.muted=this._isMuted,i.debug("Audio track set to output pipeline"))}removeTrack(){this.ensureNotDisposed(),this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._track&&(this._track.stop(),this._track=null),i.debug("Audio track removed from output pipeline")}setVolume(e){this.ensureNotDisposed(),this._volume=Math.max(0,Math.min(1,e)),this._audioElement&&(this._audioElement.volume=this._volume),i.debug(`Audio output volume set to: ${this._volume}`)}getVolume(){return this._volume}mute(){this.ensureNotDisposed(),this._isMuted=!0,this._audioElement&&(this._audioElement.muted=!0),i.debug("Audio output muted")}unmute(){this.ensureNotDisposed(),this._isMuted=!1,this._audioElement&&(this._audioElement.muted=!1),i.debug("Audio output unmuted")}isMuted(){return this._isMuted}onDispose(){this.removeTrack(),this._audioElement&&(this._audioElement=null),i.info("Output Audio Pipeline disposed")}}const be=1,Ne=0,Ie={0:0,1:1,2:2};function ke(r){return r<0?0:r>4095?4095:Math.floor(r)}function Oe(r){if(r<0)return 0;const e=1048575;return r>e?r&e:Math.floor(r)}function xe(r){return r<0?0:r>4095?4095:Math.floor(r)}function Le(r){return r<0?0:r>65535?65535:Math.floor(r)}function Pe(r){if(r!==0&&r!==1&&r!==2)throw new u(`Invalid sample rate encoding: ${r}. Must be 0 (16kHz), 1 (24kHz), or 2 (48kHz)`,h.AUDIO_INVALID_SAMPLE_RATE)}function Fe(r){Pe(r.sampleRate);const e=ke(r.sequenceNumber),t=Oe(r.timestamp),s=xe(r.frameSize),n=Le(r.payloadLength);if(r.channel!==0&&r.channel!==1)throw new u(`Invalid channel value: ${r.channel}. Must be 0 (mono) or 1 (stereo)`,h.AUDIO_INVALID_CHANNEL);const o=new Uint8Array(9);let a=be<<6;a|=r.channel<<5,a|=(r.keyFrame?1:0)<<4,a|=e>>8&15,o[0]=a,o[1]=e&255,o[2]=t>>12&255,o[3]=t>>4&255;const c=Ie[r.sampleRate];let d=(t&15)<<4;d|=c<<2,d|=s>>10&3,o[4]=d,o[5]=s>>2&255;let m=(s&3)<<6;return m|=Ne&63,o[6]=m,o[7]=n>>8&255,o[8]=n&255,o}class Me{constructor(e){this.transport=e}sendBinary(e){this.transport.sendBinary(e)}isConnected(){return this.transport.isConnected}}class Ve extends l{constructor(e,t){super(),this._wsSender=null,this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=0,this._isSending=!1,this._audioFrameListener=null,this._audioBuffer=[],this._bufferedSampleCount=0,this._targetSampleRate=24e3,this._bitDepth=16,this._minSamplesToSend=0,this._context=e,this._options=t;const s=t?.input;this._targetSampleRate=s?.sampleRate||24e3,this._bitDepth=s?.bitDepth||16,this._minSamplesToSend=Math.floor(this._targetSampleRate/10),i.info(`Audio Frame Sender created: sampleRate=${this._targetSampleRate}Hz, bitDepth=${this._bitDepth}bit, minSamples=${this._minSamplesToSend}`)}setWebSocketSender(e){this._wsSender=e}setConversationTransport(e){this._wsSender=new Me(e)}start(){if(this.ensureNotDisposed(),this._isSending){i.warn("Audio frame sending already started");return}this._sequenceNumber=0,this._isKeyFrame=!0,this._audioStartTime=Date.now(),this._audioFrameListener=e=>{this._handleAudioFrame(e.data,e.sampleRate)},this._context.eventBus.on("inner:audio:frame:received",this._audioFrameListener),this._isSending=!0,i.info("Audio frame sending started")}stop(){this.ensureNotDisposed(),this._isSending&&(this._flushBuffer(),this._audioFrameListener&&(this._context.eventBus.off("inner:audio:frame:received",this._audioFrameListener),this._audioFrameListener=null),this._isSending=!1,i.info("Audio frame sending stopped"))}_handleAudioFrame(e,t){if(this._isSending)try{Math.abs(t-this._targetSampleRate)>1&&i.warn(`Sample rate mismatch: expected ${this._targetSampleRate}Hz, got ${t}Hz`),this._audioBuffer.push(new Float32Array(e)),this._bufferedSampleCount+=e.length,this._bufferedSampleCount>=this._minSamplesToSend&&this._flushBuffer()}catch(s){i.error("Error handling audio frame:",s)}}_flushBuffer(){if(!(this._audioBuffer.length===0||this._bufferedSampleCount===0))try{const e=this._bufferedSampleCount,t=new Float32Array(e);let s=0;for(const p of this._audioBuffer)t.set(p,s),s+=p.length;this._audioBuffer=[],this._bufferedSampleCount=0;const n=this._convertToPCM(t,this._bitDepth),o=Math.ceil(this._bitDepth/8),a=n.length*o,c=this._getSampleRateEncoding(this._targetSampleRate),d=Math.floor(Date.now()-this._audioStartTime),m={channel:(this._options?.input?.channels||1)===1?0:1,keyFrame:!!this._isKeyFrame,sequenceNumber:this._sequenceNumber,timestamp:d,sampleRate:c,frameSize:t.length,payloadLength:a},g=this._pcmToUint8Array(n,this._bitDepth);if(setTimeout(()=>{try{this._context.eventBus.emit("inner:audio:payload:send",{payload:g,sampleRate:this._targetSampleRate,channels:this._options?.input?.channels||1,sequenceNumber:m.sequenceNumber,timestamp:m.timestamp,keyFrame:m.keyFrame,frameSize:m.frameSize})}catch(p){i.error("Error emitting inner:audio:payload:send event:",p)}},0),this._wsSender&&this._wsSender.isConnected())try{const p=Fe(m),v=new Uint8Array(p.length+g.length);v.set(p,0),v.set(g,p.length),this._wsSender.sendBinary(v),i.debug(`Audio frame sent via WS: seq=${m.sequenceNumber}, samples=${t.length}, size=${v.length} bytes`)}catch(p){i.error("Error sending audio frame via WebSocket:",p)}this._isKeyFrame=!1,this._sequenceNumber=(this._sequenceNumber+1)%4096}catch(e){i.error("Error flushing audio buffer:",e)}}_convertToPCM(e,t){if(t===16){const s=new Int16Array(e.length);for(let n=0;n<e.length;n++){const o=Math.max(-1,Math.min(1,e[n]));s[n]=o<0?o*32768:o*32767}return s}else if(t===32){const s=new Int32Array(e.length);for(let n=0;n<e.length;n++){const o=Math.max(-1,Math.min(1,e[n]));s[n]=o*2147483647}return s}else return i.warn(`Unsupported bit depth: ${t}, using 16-bit`),this._convertToPCM(e,16)}_pcmToUint8Array(e,t){const s=Math.ceil(t/8),n=new Uint8Array(e.length*s),o=new DataView(n.buffer);for(let a=0;a<e.length;a++)t===16?o.setInt16(a*2,e[a],!0):t===32&&o.setInt32(a*4,e[a],!0);return n}_getSampleRateEncoding(e){return e===16e3?0:e===24e3?1:e===48e3?2:(i.warn(`Unsupported sample rate: ${e}, defaulting to 48kHz`),1)}onDispose(){this.stop(),i.info("Audio Frame Sender disposed")}}class Ue extends l{emitAudioData(e,t){if(this.onAudioData)try{this.onAudioData(e,t)}catch(s){i.error("Error in audio data callback:",s)}}}const Ke=`
|
|
56
56
|
class AudioResamplerProcessor extends AudioWorkletProcessor {
|
|
57
57
|
constructor(options) {
|
|
58
58
|
super();
|
|
@@ -107,4 +107,4 @@ class AudioResamplerProcessor extends AudioWorkletProcessor {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
registerProcessor('audio-resampler', AudioResamplerProcessor);
|
|
110
|
-
`;function xe(){const o=new Blob([Oe],{type:"text/javascript"});return URL.createObjectURL(o)}class Le extends ke{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=k,this._bitDepth=O,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||k,this._bitDepth=t?.bitDepth||O,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const r=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:r});const a=xe();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:h,data:m,sampleRate:g}=c.data;h==="audioData"&&m&&this.emitAudioData(m,g)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${r}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,d.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class Pe{create(e){return new Le(e)}}class Fe extends l{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new ve(t),this._outputPipeline=new Ee,this._frameSender=new Ie(e,t),this._sourceFactory=s||new Pe,this._inputPipeline.onProcessedAudio=(n,r)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:r})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,d.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}var _;(function(o){o.IDLE="idle",o.WAITING="waiting",o.STREAMING="streaming",o.COMPLETED="completed",o.ERROR="error"})(_||(_={}));class Me extends B{constructor(){super(...arguments),this._currentState=_.IDLE,this.transitionTable=[{from:_.IDLE,to:[_.WAITING,_.ERROR]},{from:_.WAITING,to:[_.STREAMING,_.ERROR]},{from:_.STREAMING,to:[_.COMPLETED,_.ERROR]},{from:_.COMPLETED,to:[_.IDLE,_.WAITING,_.ERROR]},{from:_.ERROR,to:[_.IDLE]}]}reset(){this._currentState=_.IDLE}}class L extends l{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new Me,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(_.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(_.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(_.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(_.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==_.IDLE&&t!==_.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==_.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class Ve extends l{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class Ue{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class Ke extends l{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new Ve,this._idService=new Ue,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},r=this._idService.generateSessionId(),a=new L(r,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===_.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new L(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class We extends l{constructor(e,t){super(),this._manager=new Ke(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class Be extends l{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const r=n.mediaStreamTrack;if(r.kind==="video"&&(e=!0,r.readyState==="live"&&(t=!0),r.enabled&&(s=!0),r.readyState==="live"&&r.enabled))return{available:!0}}return e?t?s?{available:!1,reason:T.NO_VIDEO_TRACK}:{available:!1,reason:T.TRACK_MUTED}:{available:!1,reason:T.TRACK_ENDED}:{available:!1,reason:T.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class P extends l{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},se),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class F extends l{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class $e extends l{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new P(e,t),new F(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof P);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof F);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class He extends l{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new Be,this._eventAdapter=new $e(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||D,this._maxDelay=(e.options.reconnect?.delay??U)*A,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const r=new Q({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(A*Math.pow(2,a.retryCount),this._maxDelay):null}});r.prepareConnection(e,t),r.on(C.TrackSubscribed,(a,c,h)=>{this._handleTrackSubscribed(a,c,h)}),r.on(C.TrackUnsubscribed,(a,c,h)=>{this._handleTrackUnsubscribed(a,c,h)}),r.on(C.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),r.on(C.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),r.on(C.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),r.on(C.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==Y.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await r.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||D}),this._eventAdapter.setRoom(r),this._room=r}catch(r){throw u.fromError(r,d.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(X.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const r=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,r,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,r=s,a=n.trackSid,c=r.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===J.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class ze extends l{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 401) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 411) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 410) for uid: ${t}`)):n===400&&(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 412) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class qe extends l{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=D,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??D,this._maxDelay=(e.options.reconnect?.delay??U)*A,this._dispatcher=new ze(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",d.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",d.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,r=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,r)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,d.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(A*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const r=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(r)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${I/A}s). Reconnecting...`),this._ws?.close()},I)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},ie)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",d.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(r){throw this._context.emitError(r,d.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,d.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const w=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],M=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class Ge{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),w.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=r=>{try{t(r)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(w.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const r=s.sanitizer?s.sanitizer(t):t;if(!M.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(r)}catch(h){console.error("PublicEventEmitter listener error",h)}})}handlePublicEvent(e,t){if(w.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const r=this.listeners.get(e);!r||r.size===0||r.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,r=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:r,all:s&&n&&r},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(w.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!M.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class je extends l{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),r={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?r.body=e.data:(r.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return r.signal=c.signal,new Promise((h,m)=>{const g=setTimeout(()=>c.abort(),a);fetch(s,r).then(async p=>{clearTimeout(g);const v={};p.headers.forEach((G,j)=>{v[j]=G});let S;const N=p.headers.get("content-type");N&&N.includes("application/json")?S=await p.json():S=await p.text();const q={requestId:t,status:p.status,headers:v,data:S,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),h(q)}).catch(p=>{clearTimeout(g);const v=p&&p.name==="AbortError",S={requestId:t,status:p&&p.status||void 0,message:v?"Request aborted (timeout)":p&&p.message||"HTTP request failed",error:p,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,p),m(S)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[r,a]of Object.entries(t))s.append(r,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function Qe(o,e="https"){return o.startsWith(e)?o:`${e}://${o}`}class Ye extends l{constructor(e,t){super(),this._context=e,this._httpService=new je(e,t||ne)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===R&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,d.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice}}).then(e=>{if(e.data.code===R){const t=e.data.data;return this._context.setLivekitConfig(Qe(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||oe}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,d.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,d.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===R)return!0;throw this._context.emitError(s.data.message,d.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===R)return!0;throw this._context.emitError(e.data.message,d.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===R).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class Xe extends l{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new ce(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new Ge(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",d.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",d.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<ae?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new Ye(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,d.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,d.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new he(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new fe(this._context,this._context.options.video),this._audioController=new Fe(this._context,this._context.options.audio),this._conversationController=new We(this._context),this._liveKitService=new He(this._context),this._conversationTransport=new qe(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}this._audioController&&this._conversationTransport&&this._audioController.setWebSocketSender({sendBinary:e=>{this._conversationTransport.sendBinary(e)},isConnected:()=>this._conversationTransport.isConnected}),this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,d.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,d.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",d.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,d.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():W(K("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",d.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function it(o){if(!o.avatarName)throw new u("avatarName is required",d.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:o.avatarName}),new Xe(o)}class V extends l{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},r=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",r),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",r),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,r)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Je extends l{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new $;const n={...e,renderMode:"processed"};this._pipeline=new z(this._source,n),this._outputRenderer=new b(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const r=this._outputRenderer.getContext();r&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),r.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class nt extends l{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new V(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Je(this._options,t,s),this._options.greenScreen?.enabled){const r=new y(this._options.greenScreen);this._pipelineRunner.setProcessor(r),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,r=new V(e,t);try{await r.load(),n&&await r.play(),r.onFrame(h=>{this._pipelineRunner.processFrame(h).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=r;try{s.offFrame(),s.stop(),s.dispose()}catch(h){console.warn("Error disposing old video source during switch:",h)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{r.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},r=new y(n);this._pipelineRunner.setProcessor(r),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}export{_ as ConversationStateType,st as DEFAULT_AUDIO_BUFFER_SIZE,tt as DEFAULT_AUDIO_CHANNELS,k as DEFAULT_AUDIO_SAMPLE_RATE,D as DEFAULT_RECONNECT_ATTEMPTS,U as DEFAULT_RECONNECT_DELAY,et as DEFAULT_WS_TIMEOUT,l as Disposable,Z as EventBus,E as LogLevel,i as Logger,Xe as SDKClient,u as SDKError,te as SessionState,f as SessionStatus,nt as StandaloneVideoDebugger,it as createClient};
|
|
110
|
+
`;function Be(){const r=new Blob([Ke],{type:"text/javascript"});return URL.createObjectURL(r)}class We extends Ue{constructor(e){super(),this.options=e,this._stream=null,this._audioContext=null,this._sourceNode=null,this._workletNode=null,this._targetSampleRate=L,this._bitDepth=P,this._channels=1;const t=e?.input;this._targetSampleRate=t?.sampleRate||L,this._bitDepth=t?.bitDepth||P,this._channels=t?.channels||1}async start(){this.ensureNotDisposed();try{const t={audio:this.options?.input?.constraints||{channelCount:this._channels}};this._stream=await navigator.mediaDevices.getUserMedia(t);const o=this._stream.getAudioTracks()[0].getSettings().sampleRate||44100;this._audioContext=new AudioContext({sampleRate:o});const a=Be();await this._audioContext.audioWorklet.addModule(a),this._sourceNode=this._audioContext.createMediaStreamSource(this._stream),this._workletNode=new AudioWorkletNode(this._audioContext,"audio-resampler",{processorOptions:{targetSampleRate:this._targetSampleRate,bitDepth:this._bitDepth,channels:this._channels},numberOfInputs:1,numberOfOutputs:0}),this._workletNode.port.onmessage=c=>{const{type:d,data:m,sampleRate:g}=c.data;d==="audioData"&&m&&this.emitAudioData(m,g)},this._sourceNode.connect(this._workletNode),i.info(`Audio capture started: input=${o}Hz -> output=${this._targetSampleRate}Hz, ${this._bitDepth}bit`)}catch(e){throw u.fromError(e,h.AUDIO_CAPTURE_FAILED)}}stop(){this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._workletNode&&(this._workletNode.port.onmessage=null,this._workletNode.disconnect(),this._workletNode=null),this._sourceNode&&(this._sourceNode.disconnect(),this._sourceNode=null),this._audioContext&&(this._audioContext.close(),this._audioContext=null),i.info("Audio capture stopped")}onDispose(){this.stop()}}class $e{create(e){return new We(e)}}class He extends l{constructor(e,t,s){super(),this._source=null,this._isCapturing=!1,this._currentOutputTrackId=null,this._context=e,this._inputPipeline=new De(t),this._outputPipeline=new ye,this._frameSender=new Ve(e,t),this._sourceFactory=s||new $e,this._inputPipeline.onProcessedAudio=(n,o)=>{this._context.eventBus.emit("inner:audio:frame:received",{data:n,sampleRate:o})},i.info("Audio Controller created")}setWebSocketSender(e){this._frameSender.setWebSocketSender(e)}async startCapture(){if(this.ensureNotDisposed(),this._isCapturing){i.warn("Audio capture already started");return}try{this._source||(this._source=this._sourceFactory.create(this._context.options.audio),this._source.onAudioData=(e,t)=>{this._inputPipeline.process(e,t)}),await this._source.start(),this._isCapturing=!0,this._frameSender.start(),this._context.eventBus.emit("inner:audio:input:started",void 0),i.info("Audio capture started")}catch(e){throw this._context.emitError(e,h.AUDIO_CAPTURE_START_FAILED)}}stopCapture(){this.ensureNotDisposed(),this._isCapturing&&(this._source&&this._source.stop(),this._frameSender.stop(),this._isCapturing=!1,this._context.eventBus.emit("inner:audio:input:stopped",void 0),i.info("Audio capture stopped"))}get isCapturing(){return this._isCapturing}setOutputTrack(e,t,s){this.ensureNotDisposed(),this._outputPipeline.setTrack(e),this._currentOutputTrackId=t,this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.info("Audio output track ready")}removeOutputTrack(e){this.ensureNotDisposed(),this._currentOutputTrackId===e&&(this._outputPipeline.removeTrack(),this._currentOutputTrackId=null,this._context.eventBus.emit("inner:audio:track:removed",{trackId:e}),i.info("Audio output track removed"))}setVolume(e){this.ensureNotDisposed(),this._outputPipeline.setVolume(e),this._context.eventBus.emit("inner:audio:volume:changed",{volume:e}),i.debug(`Audio volume set to: ${e}`)}getVolume(){return this._outputPipeline.getVolume()}mute(){this.ensureNotDisposed(),this._outputPipeline.mute(),this._context.eventBus.emit("inner:audio:muted",void 0),i.debug("Audio muted")}unmute(){this.ensureNotDisposed(),this._outputPipeline.unmute(),this._context.eventBus.emit("inner:audio:unmuted",void 0),i.debug("Audio unmuted")}getOutputElement(){return this._outputPipeline.getAudioElement()}isMuted(){return this._outputPipeline.isMuted()}onDispose(){this.stopCapture(),this._source&&(this._source.dispose(),this._source=null),this._inputPipeline.dispose(),this._outputPipeline.dispose(),this._frameSender.dispose(),i.info("Audio Controller disposed")}}class qe extends l{constructor(){super(...arguments),this._stream=null,this._videoElement=null}async start(){if(this.ensureNotDisposed(),this._stream){i.warn("Camera capture already started");return}try{const e=await navigator.mediaDevices.getUserMedia({video:!0});if(this._stream=e,!this._videoElement){const t=document.createElement("video");t.autoplay=!0,t.muted=!0,t.playsInline=!0,t.style.position="fixed",t.style.opacity="0",t.style.pointerEvents="none",t.style.width="0",t.style.height="0",t.style.zIndex="-1",typeof document<"u"&&document.body&&document.body.appendChild(t),this._videoElement=t}this._videoElement.srcObject=this._stream,this._videoElement.play().catch(t=>{i.error("Failed to start camera video playback",t)})}catch(e){throw u.fromError(e,h.SDK_ERROR)}}stop(){if(this._stream&&(this._stream.getTracks().forEach(e=>e.stop()),this._stream=null),this._videoElement){this._videoElement.srcObject=null;const e=this._videoElement.parentNode;e&&e.removeChild(this._videoElement),this._videoElement=null}i.info("Camera capture stopped")}getStream(){return this.ensureNotDisposed(),this._stream}getVideoElement(){return this.ensureNotDisposed(),this._videoElement}getTrack(){if(this.ensureNotDisposed(),!this._stream)return null;const e=this._stream.getVideoTracks();return e.length>0?e[0]:null}onDispose(){this.stop()}}const D=640,M=640;class ze extends l{constructor(e,t,s){super(),this._videoElement=null,this._canvas=null,this._videoElement=e,this._onFrame=t,this._context=s,this._handleServerCommand=this._onServerCommand.bind(this)}setVideoElement(e){this._videoElement=e}start(){this.ensureNotDisposed(),this._canvas||(typeof OffscreenCanvas<"u"?this._canvas=new OffscreenCanvas(D,M):(this._canvas=document.createElement("canvas"),this._canvas.width=D,this._canvas.height=M)),this._context.eventBus.on("inner:conversation:server:command",this._handleServerCommand)}_onServerCommand({code:e}){e===500&&this._captureFrame()}stop(){this._context.eventBus.off("inner:conversation:server:command",this._handleServerCommand)}_captureFrame(){const e=this._videoElement,t=this._canvas;if(!e||!t||e.readyState<2||e.videoWidth===0||e.videoHeight===0)return;const s=t.getContext("2d");if(!s)return;const n=e.videoWidth,o=e.videoHeight,a=D,c=D*o/n;t.width!==a&&(t.width=a),t.height!==c&&(t.height=c),s.clearRect(0,0,t.width,t.height),s.drawImage(e,0,0,n,o,0,0,a,c),typeof OffscreenCanvas<"u"&&t instanceof OffscreenCanvas&&typeof t.convertToBlob=="function"?t.convertToBlob({type:"image/jpeg",quality:w}).then(d=>{if(d)try{this._onFrame(d,a,c,0,w)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}}).catch(d=>{i.error("ScreenshotScheduler convertToBlob error",d)}):t instanceof HTMLCanvasElement&&t.toBlob(d=>{if(d)try{this._onFrame(d,a,c,0,w)}catch(m){i.error("ScreenshotScheduler onFrame callback error",m)}},"image/jpeg",w)}onDispose(){this.stop(),this._canvas=null,this._videoElement=null}}async function Ge(r,e){const t=await r.arrayBuffer();return{data:new Uint8Array(t),width:e.width,height:e.height,format:e.format,quality:e.quality}}const je=12,Qe=2,Xe=0,V=0,U=4,K=65535,B=4294967295;function Ye(r){const e=Math.floor(r);return e<0?0:e>255?255:e}function N(r){const e=Math.floor(r);return e<0?0:e>K?K:e}function Je(r){const e=Math.floor(r);return e<0?0:e>B?B:e}function Ze(r){const e=Math.floor(r);return e<V?V:e>U?U:e}function et(r){const e=new ArrayBuffer(je),t=new DataView(e),s=Ze(r.format)&15,n=Qe<<6|Xe<<4|s;return t.setUint8(0,n),t.setUint8(1,Ye(r.quality)),t.setUint16(2,N(r.id),!1),t.setUint16(4,N(r.width),!1),t.setUint16(6,N(r.height),!1),t.setUint32(8,Je(r.payloadLength),!1),new Uint8Array(e)}const tt=65535;class st extends l{constructor(){super(...arguments),this._wsSender=null,this._imageId=0}setWebSocketSender(e){this._wsSender=e}send(e,t){if(this.ensureNotDisposed(),!this._wsSender||!this._wsSender.isConnected())return;const s=e.byteLength,n=et({format:t.format,quality:t.quality,id:this._nextId(),width:t.width,height:t.height,payloadLength:s}),o=new Uint8Array(n.byteLength+s);o.set(n,0),o.set(e,n.byteLength),this._wsSender.sendBinary(o)}_nextId(){const e=this._imageId;return this._imageId=this._imageId>=tt?0:this._imageId+1,e}onDispose(){this._wsSender=null}}class it extends l{constructor(e){super(),this._isRunning=!1,this._context=e,this._source=new qe,this._sender=new st;const t=(s,n,o,a,c=w)=>{Ge(s,{width:n,height:o,format:a,quality:c}).then(d=>{this._sender.send(d.data,{width:d.width,height:d.height,format:d.format,quality:d.quality})})};this._scheduler=new ze(null,t,e)}async startCamera(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Camera already running");return}await this._source.start();const e=this._source.getVideoElement();this._scheduler.setVideoElement(e),this._scheduler.start(),this._isRunning=!0,this._context.eventBus.emit("inner:camera:started",void 0),i.info("Camera started")}stopCamera(){this.ensureNotDisposed(),this._isRunning&&(this._scheduler.stop(),this._source.stop(),this._isRunning=!1,this._context.eventBus.emit("inner:camera:stopped",void 0),i.info("Camera stopped"))}isCameraRunning(){return this._isRunning}getStream(){return this.ensureNotDisposed(),this._source.getStream()}getTrack(){return this.ensureNotDisposed(),this._source.getTrack()}attachTo(e){this.ensureNotDisposed();const t=this._source.getStream();t&&(e.srcObject=t,e.play().catch(s=>i.error("attachTo play failed",s)))}setWebSocketSender(e){this._sender.setWebSocketSender(e)}onDispose(){this.stopCamera(),this._source.dispose(),this._scheduler.dispose(),this._sender.dispose()}}var _;(function(r){r.IDLE="idle",r.WAITING="waiting",r.STREAMING="streaming",r.COMPLETED="completed",r.ERROR="error"})(_||(_={}));class nt extends X{constructor(){super(...arguments),this._currentState=_.IDLE,this.transitionTable=[{from:_.IDLE,to:[_.WAITING,_.ERROR]},{from:_.WAITING,to:[_.STREAMING,_.ERROR]},{from:_.STREAMING,to:[_.COMPLETED,_.ERROR]},{from:_.COMPLETED,to:[_.IDLE,_.WAITING,_.ERROR]},{from:_.ERROR,to:[_.IDLE]}]}reset(){this._currentState=_.IDLE}}class W extends l{constructor(e,t){super(),this._currentQuestion=null,this._currentAnswer=null,this._sessionId=e,this._stateMachine=new nt,i.info(`Conversation session created: ${e}`)}get sessionId(){return this._sessionId}get state(){return this._stateMachine.getCurrentState()}_setQuestion(e){this._currentQuestion=e,this._stateMachine.transitionTo(_.WAITING),this._syncContextState()}get currentQuestion(){return this._currentQuestion}_startAnswer(e){this._currentAnswer={questionId:e,text:"",isComplete:!1,timestamp:Date.now()},this._stateMachine.transitionTo(_.STREAMING),this._syncContextState()}_appendAnswerChunk(e){this._currentAnswer&&(this._currentAnswer.text+=e)}_completeAnswer(){this._currentAnswer&&(this._currentAnswer.isComplete=!0,this._currentAnswer.completedAt=Date.now()),this._stateMachine.transitionTo(_.COMPLETED),this._syncContextState()}get currentAnswer(){return this._currentAnswer}_setError(e){this._stateMachine.transitionTo(_.ERROR),this._syncContextState()}_syncContextState(){}handleEvent(e){const t=this._stateMachine.getCurrentState();switch(e.type){case"QUESTION":if(t!==_.IDLE&&t!==_.COMPLETED){i.warn(`Ignoring QUESTION event in invalid state: ${t}`);return}this._setQuestion(e.payload);break;case"ANSWER_START":if(t!==_.WAITING){i.warn(`Ignoring ANSWER_START event in invalid state: ${t}. Expected: WAITING`);return}this._startAnswer(e.payload);break;case"ANSWER_CHUNK":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_CHUNK event in invalid state: ${t}. Expected: STREAMING`);return}this._appendAnswerChunk(e.payload&&e.payload.chunk||""),e.payload&&e.payload.isComplete&&this._completeAnswer();break;case"ANSWER_COMPLETE":if(t!==_.STREAMING){i.warn(`Ignoring ANSWER_COMPLETE event in invalid state: ${t}. Expected: STREAMING`);return}this._completeAnswer();break;case"ERROR":this._setError(e.payload);break;default:i.warn(`Unknown event type: ${e.type}`);break}}reset(){this._stateMachine.reset(),this._currentQuestion=null,this._currentAnswer=null}onDispose(){this.reset(),i.info(`Conversation session disposed: ${this._sessionId}`)}}class rt extends l{constructor(){super(...arguments),this._buffers=new Map}appendChunk(e,t){this.ensureNotDisposed();const n=(this._buffers.get(e)||"")+t;return this._buffers.set(e,n),i.debug(`Message chunk appended for question ${e}, length: ${n.length}`),n}getMessage(e){return this._buffers.get(e)||null}complete(e){const t=this.getMessage(e)||"";return this._buffers.delete(e),i.debug(`Message assembly completed for question ${e}, length: ${t.length}`),t}clear(e){this._buffers.delete(e)}onDispose(){this._buffers.clear(),i.info("Message Assembler disposed")}}class ot{generateSessionId(){return`session_${Date.now()}_${Math.random().toString(36).substring(2,9)}`}generateQuestionId(){return Math.random().toString(36).substring(2,6)}}class at extends l{constructor(e,t){super(),this._transport=null,this._sessions=new Map,this._context=e,this._assembler=new rt,this._idService=new ot,i.info("Conversation Manager created")}setTransport(e){this._transport=e}async sendQuestion(e,t={speed:100,mood:0,vol:50}){this.ensureNotDisposed();const s=this._idService.generateQuestionId(),n={id:s,text:e,timestamp:Date.now(),inputMode:"text"},o=this._idService.generateSessionId(),a=new W(o,this._context);if(a.handleEvent({type:"QUESTION",payload:n}),this._sessions.set(s,a),this._transport)try{this._transport.sendText(s,e,200,t)}catch(c){throw i.error("Error sending question via transport:",c),a.handleEvent({type:"ERROR",payload:"Failed to send question"}),this._context.eventBus.emit("inner:sdk:error",{error:c instanceof Error?c:new Error(String(c))}),c}else i.warn("Transport not set, question not sent");return this._context.eventBus.emit("inner:conversation:question:sent",{questionId:s,text:e}),this._context.eventBus.emit("inner:conversation:answer:waiting",{questionId:s}),i.info(`Question sent: ${s}`),s}handleServerPong(){this.ensureNotDisposed(),this._transport&&this._transport.handlePongReceived()}willDisconnect(){this.ensureNotDisposed(),this._transport?.willDisconnect()}handleTextInput(e){this.sendQuestion(e).catch(t=>{i.error("Error sending question:",t)})}handleAnswerChunk(e,t,s){this.ensureNotDisposed();const n=this._sessions.get(e);if(!n){i.warn(`No active session for answer chunk: ${e}`);return}if(n.state===_.WAITING&&n.handleEvent({type:"ANSWER_START",payload:e}),s){n.handleEvent({type:"ANSWER_COMPLETE"});const a=this._assembler.complete(e);this._context.eventBus.emit("inner:conversation:answer:completed",{questionId:e,fullAnswer:a});try{n.dispose()}catch(c){i.error("Error disposing session:",c)}this._sessions.delete(e)}else n.handleEvent({type:"ANSWER_CHUNK",payload:{uid:e,chunk:t,isComplete:s}}),this._assembler.appendChunk(e,t),this._context.eventBus.emit("inner:conversation:answer:chunk",{questionId:e,chunk:t,isComplete:s})}handleServerInitiatedMessage(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:message",{questionId:e,message:t,type:"text"})}handleAsrTextReceived(e,t){this.ensureNotDisposed();const s=this._idService.generateSessionId(),n=new W(s,this._context);n.handleEvent({type:"QUESTION",payload:{id:e,text:t,timestamp:Date.now(),inputMode:"voice"}}),this._sessions.set(e,n),this._context.eventBus.emit("inner:conversation:asr:received",{questionId:e,text:t})}handleServerCommand(e,t){this.ensureNotDisposed(),this._context.eventBus.emit("inner:conversation:server:command",{code:e,command:t})}onDispose(){for(const e of this._sessions.values())try{e.dispose()}catch(t){i.error("Error disposing session:",t)}this._sessions.clear(),this._assembler.dispose(),i.info("Conversation Manager disposed")}}class ct extends l{constructor(e,t){super(),this._manager=new at(e,t),i.info("Conversation Controller created")}get manager(){return this._manager}setTransport(e){this._manager.setTransport(e)}async sendQuestion(e,t){return this.ensureNotDisposed(),this._manager.sendQuestion(e,t)}onDispose(){this._manager.dispose(),i.info("Conversation Controller disposed")}}class dt extends l{constructor(){super(...arguments),this._tracks=new Map,this._participants=new Map}register(e,t,s){this.ensureNotDisposed(),this._tracks.set(e,t),this._participants.has(s)||this._participants.set(s,new Set),this._participants.get(s).add(e),t.on("ended",()=>{i.debug(`Track ended: ${e}`),this.unregister(e)}),i.debug(`Track registered: ${e} from participant ${s}`)}unregister(e){const t=this._tracks.get(e);if(t){t.stop(),this._tracks.delete(e);for(const[s,n]of this._participants.entries())if(n.has(e)){n.delete(e),n.size===0&&this._participants.delete(s);break}i.debug(`Track unregistered: ${e}`)}}getTrack(e){return this._tracks.get(e)||null}getParticipantTracks(e){const t=this._participants.get(e);return t?Array.from(t):[]}getAllTrackIds(){return Array.from(this._tracks.keys())}getVideoAvailability(){let e=!1,t=!1,s=!1;for(const n of this._tracks.values()){const o=n.mediaStreamTrack;if(o.kind==="video"&&(e=!0,o.readyState==="live"&&(t=!0),o.enabled&&(s=!0),o.readyState==="live"&&o.enabled))return{available:!0}}return e?t?s?{available:!1,reason:T.NO_VIDEO_TRACK}:{available:!1,reason:T.TRACK_MUTED}:{available:!1,reason:T.TRACK_ENDED}:{available:!1,reason:T.NO_VIDEO_TRACK}}clear(){for(const e of this._tracks.values())e.stop();this._tracks.clear(),this._participants.clear(),i.info("All tracks cleared")}onDispose(){this.clear()}}class $ extends l{constructor(e,t){super(),this._videoController=null,this._room=null,this._context=e,this._trackRegistry=t,this._loosTrackTimer=null,i.info("Video Track Handler created")}setRoom(e){this._room=e}setVideoController(e){this._videoController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._trackRegistry.register(t,e,s),this._videoController&&e.attach(this._videoController.getSource().getInternalElement()),e.on("videoPlaybackStarted",()=>{i.info("inner:rtc:video:available"),this._context.eventBus.emit("inner:rtc:video:available",void 0)}),this._context.eventBus.emit("inner:video:track:added",{trackId:t,participantId:s}),i.debug(`Video track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._videoController&&e.detach(this._videoController.getSource().getInternalElement()),e.removeAllListeners(),this._context.eventBus.emit("inner:video:track:removed",{trackId:t,participantId:s}),this._loosTrackTimer&&clearTimeout(this._loosTrackTimer),this._loosTrackTimer=setTimeout(()=>{this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)},he),this._trackRegistry.unregister(t),i.debug(`Video track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="video"}onDispose(){this._loosTrackTimer&&(clearTimeout(this._loosTrackTimer),this._loosTrackTimer=null),i.info("Video Track Handler disposed")}}class H extends l{constructor(e,t){super(),this._audioController=null,this._room=null,this._context=e,this._trackRegistry=t,i.info("Audio Track Handler created")}setRoom(e){this._room=e}setAudioController(e){this._audioController=e}handleTrackSubscribed(e,t,s){this.ensureNotDisposed(),this._trackRegistry.register(t,e,s),this._audioController?e.attach(this._audioController.getOutputElement()):this._context.eventBus.emit("inner:audio:track:added",{trackId:t,participantId:s}),i.debug(`Audio track subscribed: ${t}`)}handleTrackUnsubscribed(e,t,s){this.ensureNotDisposed(),this._audioController?(this._audioController.removeOutputTrack(t),e.detach(this._audioController.getOutputElement())):this._context.eventBus.emit("inner:audio:track:removed",{trackId:t}),this._trackRegistry.unregister(t),i.debug(`Audio track unsubscribed: ${t}`)}supports(e){return e.mediaStreamTrack.kind==="audio"}onDispose(){i.info("Audio Track Handler disposed")}}class ht extends l{constructor(e,t){super(),this._room=null,this._context=e,this._trackRegistry=t,this._handlers=[new $(e,t),new H(e,t)],i.info("LiveKit Event Adapter created")}setRoom(e){this._room=e,this._handlers.forEach(t=>{t.setRoom(e)}),console.log("LiveKit room set:",this._room)}setVideoController(e){const t=this._handlers.find(s=>s instanceof $);t&&t.setVideoController(e)}setAudioController(e){const t=this._handlers.find(s=>s instanceof H);t&&t.setAudioController(e)}handleTrackSubscribed(e,t,s){this.ensureNotDisposed();const n=this._findHandler(e);n?n.handleTrackSubscribed(e,t,s):i.warn(`No handler found for track, trackId: ${t}`)}handleTrackUnsubscribed(e,t){this.ensureNotDisposed();const s=this._trackRegistry.getTrack(e);if(s){const n=this._findHandler(s);n?n.handleTrackUnsubscribed(s,e,t):(this._trackRegistry.unregister(e),i.warn(`No handler found for track, trackId: ${e}`))}}_findHandler(e){return this._handlers.find(t=>t.supports(e))||null}onDispose(){this._handlers.forEach(e=>{try{e.dispose()}catch(t){i.error("Error disposing track handler:",t)}}),this._handlers.length=0,i.info("LiveKit Event Adapter disposed")}}class lt extends l{constructor(e){super(),this._room=null,this._context=e,this._trackRegistry=new dt,this._eventAdapter=new ht(e,this._trackRegistry),this._maxAttempts=e.options.reconnect?.maxAttempts||b,this._maxDelay=(e.options.reconnect?.delay??G)*R,i.info("LiveKit Service created")}async connect(e,t,s,n){if(this.ensureNotDisposed(),this._room&&this._room.isConnected()){i.warn("Already connected to LiveKit room");return}try{i.info(`Connecting to LiveKit room: ${s} at ${e}`);const o=new ie({reconnectPolicy:{nextRetryDelayInMs:a=>a.elapsedMs<this._maxDelay?Math.min(R*Math.pow(2,a.retryCount),this._maxDelay):null}});o.prepareConnection(e,t),o.on(C.TrackSubscribed,(a,c,d)=>{this._handleTrackSubscribed(a,c,d)}),o.on(C.TrackUnsubscribed,(a,c,d)=>{this._handleTrackUnsubscribed(a,c,d)}),o.on(C.TrackMuted,(a,c)=>{this._handleTrackMuted(a,c)}),o.on(C.TrackUnmuted,(a,c)=>{this._handleTrackUnmuted(a,c)}),o.on(C.Connected,()=>{i.info("Connected to LiveKit room"),this._context.eventBus.emit("inner:rtc:connected",void 0)}),o.on(C.Disconnected,a=>{a?i.info(`LiveKit room disconnected: ${a}`):i.info("LiveKit room disconnected"),a!==ne.CLIENT_INITIATED&&this._context.eventBus.emit("inner:rtc:disconnected",{reason:a?String(a):void 0}),this._room=null}),i.info("Connecting to LiveKit room..."),await o.connect(e,t,{autoSubscribe:!0,maxRetries:this._context.options.reconnect?.maxAttempts||b}),this._eventAdapter.setRoom(o),this._room=o}catch(o){throw u.fromError(o,h.LIVEKIT_CONNECT_FAILED)}}_handleTrackSubscribed(e,t,s){try{if(!e.mediaStreamTrack){i.warn("Track does not have MediaStreamTrack");return}e.kind==="audio"&&s.on(re.IsSpeakingChanged,c=>{console.log("\u8F68\u9053 \u8BF4\u8BDD\u72B6\u6001:",c)});const o=t.trackSid,a=s.identity;this._eventAdapter.handleTrackSubscribed(e,o,a)}catch(n){i.error("Error handling track subscribed:",n)}}_handleTrackUnsubscribed(e,t,s){try{const n=t,o=s,a=n.trackSid,c=o.identity;this._trackRegistry.getTrack(a)?.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0),this._eventAdapter.handleTrackUnsubscribed(a,c)}catch(n){i.error("Error handling track unsubscribed:",n)}}_handleTrackMuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:unavailable",void 0)}catch(s){i.error("Error handling track muted:",s)}}_handleTrackUnmuted(e,t){try{e.kind==="video"&&this._context.eventBus.emit("inner:rtc:video:available",void 0)}catch(s){i.error("Error handling track unmuted:",s)}}async disconnect(){if(this._room)try{await this._room.disconnect(),this._room=null,this._context.eventBus.emit("inner:rtc:disconnected",{reason:"Disconnected from LiveKit room"}),i.info("Disconnected from LiveKit room")}catch(e){i.error("Error disconnecting from LiveKit room:",e),this._room=null}}isConnected(){return this._room?this._room.state===oe.Connected:!1}getTrackRegistry(){return this._trackRegistry}get trackRegistry(){return this._trackRegistry}getEventAdapter(){return this._eventAdapter}onDispose(){this.disconnect(),this._trackRegistry.dispose(),this._eventAdapter.dispose(),i.info("LiveKit Service disposed")}}class ut extends l{constructor(e){super(),this._conversationManager=null,this._context=e,i.info("WebSocket Dispatcher created")}setConversationManager(e){this._conversationManager=e,i.debug("Conversation manager set in dispatcher")}dispatch(e){this.ensureNotDisposed();try{if(e.type==="ws"){this._handleWSServerMessage(e);return}if(e.type==="text"){this._handleServerTextMessage(e);return}}catch(t){i.error("Error dispatching message:",t)}}_handleWSServerMessage(e){e.code===101?(this._conversationManager?.handleServerPong(),i.debug("Received pingpong response")):e.code===102&&(i.warn("Server will disconnect"),this._conversationManager?.willDisconnect())}_handleServerTextMessage(e){if(!this._conversationManager){i.warn("Conversation manager not set, text message ignored");return}const{uid:t,payload:s,code:n}=e;n===200?(this._conversationManager.handleAnswerChunk(t,s,!1),i.debug(`Answer received (code 200) for uid: ${t}`)):n===201?(this._conversationManager.handleAnswerChunk(t,"",!0),i.debug(`Answer completed (code 201) for uid: ${t}`)):n===300?(this._conversationManager.handleAsrTextReceived(t,s),i.debug(`ASR text chunk received (code 300) for uid: ${t}`)):n===400?(this._conversationManager.handleServerInitiatedMessage(t,s),i.debug(`Server initiated message received (code 300) for uid: ${t}`)):n===500&&(this._conversationManager.handleServerCommand(n,s),i.debug(`Server command received (code 500) for uid: ${t}`))}onDispose(){i.info("WebSocket Dispatcher disposed")}}class _t extends l{constructor(e){super(),this._ws=null,this._dispatcher=null,this._isConnecting=!1,this._activeTimers=new Set,this._retryCount=0,this._maxRetries=b,this._pingTimer=void 0,this._pongTimer=void 0,this._isExplicitDisconnect=!1,this._context=e,this._maxRetries=e.options.reconnect?.maxAttempts??b,this._maxDelay=(e.options.reconnect?.delay??G)*R,this._dispatcher=new ut(e)}setConversationManager(e){this._dispatcher&&this._dispatcher.setConversationManager(e)}async connect(){if(this.ensureNotDisposed(),this._stopHeartbeat(),!(this._isConnecting||this.isConnected||this._ws?.readyState===WebSocket.OPEN)){this._isConnecting=!0,this._isExplicitDisconnect=!1;try{return this._ws=new WebSocket(this._context.wsUrl),new Promise((e,t)=>{this._ws.onopen=()=>{this._isConnecting=!1,this._retryCount=0,this.sendPing(),this._context.eventBus.emit("inner:ws:connected",void 0),i.info("Conversation transport connected"),e()},this._ws.onerror=s=>{this._isConnecting=!1,i.error("Conversation transport error:",s),this._context.eventBus.emit("inner:sdk:error",{error:new u("WebSocket connection error",h.WS_CONNECT_ERROR,s)}),t(new u("WebSocket connection error",h.WS_CONNECT_ERROR,s))},this._ws.onclose=s=>{const n=s.code,o=s.reason?s.reason:s.wasClean?"Clean close":"Closed by remote";this._handleDisconnect(n,o)},this._ws.onmessage=s=>{this._handleMessage(s.data)}})}catch(e){throw this._isConnecting=!1,this._context.emitError(e,h.WS_CONNECT_FAILED)}}}disconnect(){this._isExplicitDisconnect=!0,this._stopHeartbeat(),this._ws&&(this._ws.close(),this._ws=null),this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client disconnect"}),i.info("Conversation transport disconnected")}_handleDisconnect(e,t){if(this._stopHeartbeat(),this._isConnecting=!1,e!==void 0&&new Set([1e3,1001,1002,1003,1007,1008]).has(e)){this._isExplicitDisconnect=!0;const n=`WebSocket closed (code ${e}): ${t??""}`;this._context.eventBus.emit("inner:ws:disconnected",{reason:n}),i.info(`Conversation transport closed with non-retry code ${e}: ${t??""}`);return}if(!this._isExplicitDisconnect&&this._retryCount<this._maxRetries){this._retryCount++;const n=Math.min(R*Math.pow(2,this._retryCount-1),this._maxDelay);i.warn(`Connection lost. Retry ${this._retryCount} in ${n}ms`);const o=setTimeout(()=>{!this._isExplicitDisconnect&&!this.isDisposed&&this.connect().catch(()=>{})},n);this._registerTimer(o)}else this._context.eventBus.emit("inner:ws:disconnected",{reason:"Closed"})}_registerTimer(e){return this._activeTimers.add(e),e}_clearTimer(e){e&&(clearTimeout(e),this._activeTimers.delete(e))}sendPing(){if(!this.isConnected)return;const e={type:"ws",code:101,payload:"ping"};this._ws?.send(JSON.stringify(e)),clearTimeout(this._pongTimer),this._pongTimer=setTimeout(()=>{i.error(`Pong timeout (${x/R}s). Reconnecting...`),this._ws?.close()},x)}handlePongReceived(){clearTimeout(this._pongTimer),this._clearTimer(this._pingTimer),this._pingTimer=setTimeout(()=>{this.sendPing()},le)}willDisconnect(){if(this.ensureNotDisposed(),this._ws&&this._ws.readyState===WebSocket.OPEN){const t=JSON.stringify({type:"ws",code:102,payload:"will disconnect"});this._ws.send(t),this._isExplicitDisconnect=!0,this._ws.close(),this._ws=null,this._context.eventBus.emit("inner:ws:disconnected",{reason:"Client responsive to server's disconnect message."})}}sendText(e,t,s=200,n){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN)throw new u("WebSocket is not connected",h.WS_NOT_CONNECTED);try{const a=JSON.stringify({type:"text",code:s,uid:e,metadata:n,payload:t});this._ws.send(a),i.debug("Text message sent via transport:",e)}catch(o){throw this._context.emitError(o,h.WS_TEXT_SEND_FAILED)}}_handleMessage(e){try{if(typeof e!="string")return;const t=JSON.parse(e);i.debug("Message received:",t.type),this._dispatcher?this._dispatcher.dispatch(t):i.warn("Dispatcher not set, message ignored")}catch(t){i.error("Error parsing message:",t),this._context.emitError(t,h.WS_PARSE_ERROR)}}sendBinary(e){if(this.ensureNotDisposed(),!this._ws||this._ws.readyState!==WebSocket.OPEN){i.debug("WebSocket not connected, binary data dropped");return}try{this._ws.send(e),i.debug("Binary data sent via transport")}catch(t){i.error("Error sending binary data:",t)}}get isConnected(){return this._ws?.readyState===WebSocket.OPEN}_stopHeartbeat(){this._clearTimer(this._pingTimer),this._clearTimer(this._pongTimer)}onDispose(){this._stopHeartbeat(),this.disconnect(),this._dispatcher&&(this._dispatcher.dispose(),this._dispatcher=null),i.info("Conversation transport disposed")}}const y=["sdk:connected","sdk:disconnected","sdk:error","media:video:available","media:video:unavailable","media:video:trackAdded","media:video:trackRemoved","media:audio:trackAdded","media:audio:trackRemoved","media:audio:captureStarted","media:audio:captureStopped","media:camera:started","media:camera:stopped","media:audio:volumeChanged","media:audio:muted","media:audio:unmuted","conversation:question:sent","conversation:answer:waiting","conversation:server:message","conversation:asr:received","conversation:answer:chunk","conversation:answer:completed"],q=/^[a-z]+:[a-zA-z]+(:[a-zA-Z]+)?$/;class pt{constructor(e){this.inner=e,this.listeners=new Map,this.forwardedVideoTrackIds=new Set,this.wsConnected=!1,this.livekitConnected=!1,this.httpConnected=!1,this.internalMap={"inner:sdk:error":{public:"sdk:error",sanitizer:t=>({message:t?.error?.message,code:t?.error?.code})},"inner:rtc:video:available":{public:"media:video:available"},"inner:rtc:video:unavailable":{public:"media:video:unavailable"},"inner:video:track:added":{public:"media:video:trackAdded"},"inner:video:track:removed":{public:"media:video:trackRemoved"},"inner:audio:track:added":{public:"media:audio:trackAdded"},"inner:audio:track:removed":{public:"media:audio:trackRemoved"},"inner:audio:input:started":{public:"media:audio:captureStarted"},"inner:audio:input:stopped":{public:"media:audio:captureStopped"},"inner:camera:started":{public:"media:camera:started"},"inner:camera:stopped":{public:"media:camera:stopped"},"inner:audio:volume:changed":{public:"media:audio:volumeChanged",sanitizer:t=>({volume:t?.volume})},"inner:audio:muted":{public:"media:audio:muted"},"inner:audio:unmuted":{public:"media:audio:unmuted"},"inner:conversation:question:sent":{public:"conversation:question:sent",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:waiting":{public:"conversation:answer:waiting",sanitizer:t=>({questionId:t?.questionId})},"inner:conversation:server:message":{public:"conversation:server:message",sanitizer:t=>({questionId:t?.questionId,message:String(t?.message||""),type:String(t?.type||"")})},"inner:conversation:asr:received":{public:"conversation:asr:received",sanitizer:t=>({questionId:t?.questionId,text:String(t?.text||"")})},"inner:conversation:answer:chunk":{public:"conversation:answer:chunk",sanitizer:t=>({questionId:t?.questionId,chunk:String(t?.chunk||"")})},"inner:conversation:answer:completed":{public:"conversation:answer:completed",sanitizer:t=>({questionId:t?.questionId,fullAnswer:String(t?.fullAnswer||"")})}},Object.keys(this.internalMap).forEach(t=>{this.inner.on(t,s=>this.handleInternalEvent(t,s))}),this.inner.on("inner:sdk:connected",t=>this.handleConnected(t,!0)),this.inner.on("inner:sdk:disconnected",t=>this.handleConnected(t,!1)),y.forEach(t=>{this.inner.on(t,s=>this.handlePublicEvent(t,s))})}publicAPI(){return{on:this.on.bind(this),off:this.off.bind(this),once:this.once.bind(this)}}on(e,t){const s=String(e);return this.assertAllowed(s),this.listeners.has(s)||this.listeners.set(s,new Set),this.listeners.get(s).add(t),()=>this.off(e,t)}once(e,t){const s=String(e);this.assertAllowed(s);const n=o=>{try{t(o)}finally{this.off(e,n)}};return this.on(e,n)}off(e,t){const s=String(e);this.assertAllowed(s);const n=this.listeners.get(s);n&&(n.delete(t),n.size===0&&this.listeners.delete(s))}handleInternalEvent(e,t){const s=this.internalMap[e];if(!s)return;const n=s.public;if(y.indexOf(n)===-1)return;if(n==="media:video:trackAdded"){const c=t?.trackId||t?.id;if(typeof c=="string"){if(this.forwardedVideoTrackIds.has(c))return;this.forwardedVideoTrackIds.add(c)}}const o=s.sanitizer?s.sanitizer(t):t;if(!q.test(n))return;const a=this.listeners.get(n);!a||a.size===0||a.forEach(c=>{try{c(o)}catch(d){console.error("PublicEventEmitter listener error",d)}})}handlePublicEvent(e,t){if(y.indexOf(e)===-1)return;const s=Object.values(this.internalMap).find(a=>a.public===e),n=s&&s.sanitizer?s.sanitizer(t):t;if(e==="media:video:trackAdded"){const a=t?.trackId||t?.id;if(typeof a=="string"){if(this.forwardedVideoTrackIds.has(a))return;this.forwardedVideoTrackIds.add(a)}}const o=this.listeners.get(e);!o||o.size===0||o.forEach(a=>{try{a(n)}catch(c){console.error("PublicEventEmitter listener error",c)}})}handleConnected(e,t){e.source==="livekit"?this.livekitConnected=t:e.source==="ws"?this.wsConnected=t:e.source==="http"&&(this.httpConnected=t),this.maybeEmitSdkConnected(t?"connected":"disconnected")}maybeEmitSdkConnected(e){const t=e==="connected",s=t?!!this.livekitConnected:!this.livekitConnected,n=t?!!this.wsConnected:!this.wsConnected,o=t?!!this.httpConnected:!this.httpConnected,a={livekit:s,ws:n,http:o,all:s&&n&&o},c=JSON.stringify(a);this.lastConnectedPayloadJson!==c&&(this.lastConnectedPayloadJson=c,this.inner.emit(`sdk:${e}`,a))}assertAllowed(e){if(y.indexOf(e)===-1)throw new Error(`Event "${e}" is not allowed for public consumers`);if(!q.test(e))throw new Error(`Event name "${e}" does not follow namespace:domain[:action] naming`)}}class mt extends l{constructor(e,t,s){super(),this._requestCounter=0,this._context=e,this._baseURL=t||"",this._defaultHeaders=s||{},i.info(`HTTP Service created with baseURL: ${this._baseURL||"none"}`)}setAuthToken(e){this._defaultHeaders.Authorization=`Bearer ${e}`}request(e){this.ensureNotDisposed();const t=e.requestId||`req_${Date.now()}_${++this._requestCounter}`,s=this._buildURL(e.url,e.params),n=this._mergeHeaders(e.headers),o={method:e.method||"GET",headers:n};e.data!==void 0&&(e.method||"GET")!=="GET"&&(typeof e.data=="string"?o.body=e.data:(o.body=JSON.stringify(e.data),!n["Content-Type"]&&!n["content-type"]&&(n["Content-Type"]="application/json")));const a=e.timeout||3e4,c=new AbortController;return o.signal=c.signal,new Promise((d,m)=>{const g=setTimeout(()=>c.abort(),a);fetch(s,o).then(async p=>{clearTimeout(g);const v={};p.headers.forEach((te,se)=>{v[se]=te});let S;const O=p.headers.get("content-type");O&&O.includes("application/json")?S=await p.json():S=await p.text();const ee={requestId:t,status:p.status,headers:v,data:S,config:e};i.debug(`HTTP request succeeded: ${e.method} ${s} [${t}]`),d(ee)}).catch(p=>{clearTimeout(g);const v=p&&p.name==="AbortError",S={requestId:t,status:p&&p.status||void 0,message:v?"Request aborted (timeout)":p&&p.message||"HTTP request failed",error:p,config:e};i.error(`HTTP request failed: ${e.method} ${s} [${t}]`,p),m(S)})})}get(e,t,s){return this.request({...s,url:e,method:"GET",params:t})}post(e,t,s){return this.request({...s,url:e,method:"POST",data:t})}put(e,t,s){return this.request({...s,url:e,method:"PUT",data:t})}delete(e,t){return this.request({...t,url:e,method:"DELETE"})}patch(e,t,s){return this.request({...s,url:e,method:"PATCH",data:t})}_buildURL(e,t){if(e.startsWith("http://")||e.startsWith("https://"))return this._appendParams(e,t);let s=this._baseURL;return s&&!s.endsWith("/")&&!e.startsWith("/")?s+="/":s&&s.endsWith("/")&&e.startsWith("/")&&(s=s.slice(0,-1)),s+=e,this._appendParams(s,t)}_appendParams(e,t){if(!t||Object.keys(t).length===0)return e;const s=new URLSearchParams;for(const[o,a]of Object.entries(t))s.append(o,String(a));const n=e.includes("?")?"&":"?";return`${e}${n}${s.toString()}`}_mergeHeaders(e){const t={...this._defaultHeaders};return e&&Object.assign(t,e),t}onDispose(){i.info("HTTP Service disposed")}}function ft(r,e="https"){return r.startsWith(e)?r:`${e}://${r}`}class gt extends l{constructor(e,t){super(),this._context=e,this._httpService=new mt(e,t||ue)}getAuthToken(){return this.ensureNotDisposed(),this._httpService?.request({url:"/auth/login",method:"GET",data:{}}).then(e=>{e.data.code===A&&this._httpService?.setAuthToken(e.data.data.token)}).catch(e=>{throw this._context.emitError(e,h.SDK_AUTH_TOKEN_FAILED)})}getLivekitConfig(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/start",method:"POST",data:{avatarName:this._context.options.avatarName,voice:this._context.options.avatarVoice||""}}).then(e=>{if(e.data.code===A){const t=e.data.data;return this._context.setLivekitConfig(ft(t.livekitUrl,"wss"),t.roomToken,t.roomId),this._context.setWsUrl(`${this._context.options.wsUrlPrefix||pe}/${t.roomId}/${this._context.options.avatarName}`),this._context.setVideoOptions({renderMode:t.greenScreen.enabled?"processed":"raw",greenScreen:t.greenScreen||{}}),this._context.eventBus.emit("inner:sdk:connected",{source:"http"}),!0}else throw new u(e.data.msg,h.SDK_GET_LIVEKIT_CONFIG_FAILED)}).catch(e=>{throw this._context.eventBus.emit("inner:sdk:error",{error:new u(e.message,h.SDK_GET_LIVEKIT_CONFIG_FAILED)}),u.fromError(e,h.SDK_GET_LIVEKIT_CONFIG_FAILED)})}switchVideo({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._httpService?.request({url:"/avatar/v1/switch-video",method:"POST",data:{roomId:this._context.roomId,onceActions:e,loopActions:t}}).then(s=>{if(s.data.code===A)return!0;throw this._context.emitError(s.data.message,h.SDK_SWITCH_VIDEO_FAILED)})}interruptConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/interrupt",method:"POST",data:{roomId:this._context.roomId}}).then(e=>{if(e.data.code===A)return!0;throw this._context.emitError(e.data.message,h.SDK_INTERRUPT_CONVERSATION_FAILED)}).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}disconnectConversation(){return this.ensureNotDisposed(),this._httpService?.request({url:"/stream/v1/stop",method:"POST",data:{roomId:this._context.roomId}}).then(e=>e.data.code===A).finally(()=>{this._context.eventBus.emit("inner:sdk:disconnected",{source:"http"})})}onDispose(){this._httpService.dispose()}}class vt extends l{constructor(e){super(),this._videoController=null,this._audioController=null,this._conversationController=null,this._cameraController=null,this._liveKitService=null,this._conversationTransport=null,this._connectionCoordinator=null,this._httpController=null,this._preConnectPromise=null,this._lastPreConnectSuccessAt=null,this._context=new fe(e),i.info("SDK Client created")}get events(){return this.ensureNotDisposed(),this._publicEventAPI||(this._publicEmitter=new pt(this._context.eventBus),this._publicEventAPI=this._publicEmitter.publicAPI()),this._publicEventAPI}pushActions({onceActions:e,loopActions:t}){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.switchVideo({onceActions:e,loopActions:t})}interruptConnection(){return this.ensureNotDisposed(),this._ensureConnected(),this._httpController?.interruptConversation()}sendTextQuestion(e){if(this.ensureNotDisposed(),this._ensureConnected(),this._conversationController)return this._conversationController.sendQuestion(e);throw new u("Conversation controller is not available",h.CONVERSATION_CONTROLLER_NOT_AVAILABLE)}setRenderFitMode(e){this.ensureNotDisposed(),this._ensureConnected(),this._videoController&&this._videoController.setRenderFitMode(e)}async startAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.startCapture()}async stopAudioCapture(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);await this._audioController.stopCapture()}setVolume(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.setVolume(e)}getVolume(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController?.getVolume()||1}mute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.mute()}unmute(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);this._audioController.unmute()}get isMuted(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._audioController)throw new u("Audio controller is not available",h.AUDIO_CONTROLLER_NOT_AVAILABLE);return this._audioController.isMuted()}get isAudioCapturing(){return this.ensureNotDisposed(),this._ensureConnected(),this._audioController?this._audioController.isCapturing:!1}async startCamera(){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new u("Camera controller is not available",h.CAMERA_CONTROLLER_NOT_AVAILABLE);await this._cameraController.startCamera()}stopCamera(){this.ensureNotDisposed(),this._ensureConnected(),this._cameraController&&this._cameraController.stopCamera()}getCameraStream(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getStream()??null}getCameraTrack(){return this.ensureNotDisposed(),this._ensureConnected(),this._cameraController?.getTrack()??null}attachCameraTo(e){if(this.ensureNotDisposed(),this._ensureConnected(),!this._cameraController)throw new u("Camera controller is not available",h.CAMERA_CONTROLLER_NOT_AVAILABLE);this._cameraController.attachTo(e)}async preConnect(){if(this.ensureNotDisposed(),this._preConnectPromise)return this._preConnectPromise;const e=Date.now();return this._lastPreConnectSuccessAt!==null&&e-this._lastPreConnectSuccessAt<me?Promise.resolve(!0):(this._preConnectPromise=(async()=>{try{if(i.info("Creating HTTP controller..."),this._httpController=this._httpController??new gt(this._context,this._context.options.http?.baseURL),i.info("Getting auth token..."),await this._httpController.getAuthToken(),i.info("Getting livekit config..."),!!!await this._httpController?.getLivekitConfig())throw new Error("Config missing");return this._lastPreConnectSuccessAt=Date.now(),!0}catch(t){throw this._context.emitError(t,h.SDK_PRECONNECT_FAILED)}finally{this._preConnectPromise=null}})(),this._preConnectPromise)}async connect(){if(this.ensureNotDisposed(),this._connectionCoordinator&&this._context.sessionState.isConnected){i.warn("SDK already initialized");return}try{if(!await this.preConnect())throw new u("PreConnect failed without explicit error",h.SDK_CONNECT_FAILED)}catch(e){throw this._context.emitError(e,h.SDK_CONNECT_FAILED)}try{if(i.info("Initializing SDK..."),this._connectionCoordinator=new ve(this._context),this._connectionCoordinator.start(),this._connectionCoordinator.startConnecting(),this._videoController=new we(this._context,this._context.options.video),this._audioController=new He(this._context,this._context.options.audio),this._cameraController=new it(this._context),this._conversationController=new ct(this._context),this._liveKitService=new lt(this._context),this._conversationTransport=new _t(this._context),this._liveKitService&&this._videoController&&this._audioController){const e=this._liveKitService.getEventAdapter();e&&(typeof e.setVideoController=="function"&&e.setVideoController(this._videoController),typeof e.setAudioController=="function"&&e.setAudioController(this._audioController))}if(this._conversationTransport){const e={sendBinary:t=>{this._conversationTransport.sendBinary(t)},isConnected:()=>this._conversationTransport.isConnected};this._audioController&&this._audioController.setWebSocketSender(e),this._cameraController&&this._cameraController.setWebSocketSender(e)}this._conversationTransport&&this._conversationController&&(this._conversationTransport.setConversationManager(this._conversationController.manager),this._conversationController.setTransport(this._conversationTransport)),await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),this._conversationTransport&&await this._conversationTransport.connect(),i.info("SDK initialized and connected")}catch(e){throw this._connectionCoordinator&&this._connectionCoordinator.transitionToIdle(),await this._cleanup(),this._context.emitError(e,h.SDK_INITIALIZATION_FAILED)}}async disconnect(){if(this.ensureNotDisposed(),!(!this._connectionCoordinator||this._connectionCoordinator.getState()==="idle"||this._connectionCoordinator.getState()==="disposed")){i.info("Disconnecting SDK..."),this._connectionCoordinator.startDisconnecting();try{this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService&&await this._liveKitService.disconnect(),this._audioController&&this._audioController.stopCapture(),this._cameraController&&this._cameraController.stopCamera(),this._httpController&&await this._httpController.disconnectConversation(),this._preConnectPromise=null,this._connectionCoordinator.completeDisconnecting(),i.info("SDK disconnected")}catch(e){throw i.error("Error during disconnect:",e),this._context.emitError(e,h.SDK_DISCONNECT_FAILED)}}}async reconnect(){this.ensureNotDisposed();const e=this._connectionCoordinator;if(!e?.canStartReconnectingManual()){const s=e?e.getSnapshot():this.connectionSnapshot;return i.warn("SDK reconnect not allowed in current state, returning current snapshot"),s}const t=e.captureSnapshotAndStartReconnectingManual();try{if(this._audioController&&this._audioController.stopCapture(),t.rtc.connected&&!t.rtc.hasVideoTrack&&(await this._liveKitService?.disconnect(),await this._conversationTransport?.disconnect()),this._conversationTransport&&!this._conversationTransport.isConnected){if(!await this.preConnect())throw new u("PreConnect failed without explicit error",h.SDK_CONNECT_FAILED);await this._conversationTransport.connect()}return this._liveKitService&&!this._liveKitService.isConnected()&&await this._liveKitService.connect(this._context.livekitUrl,this._context.token,this._context.options.avatarName),t}catch(s){throw i.error("Error during reconnect:",s),this._context.emitError(s,h.SDK_RECONNECT_FAILED)}}get connectionSnapshot(){return this.ensureNotDisposed(),this._connectionCoordinator?this._connectionCoordinator.getSnapshot():Q(j("idle"))}async _cleanup(){if(this._conversationTransport&&this._conversationTransport.disconnect(),this._liveKitService)try{await this._liveKitService.disconnect()}catch(e){i.error("Error disconnecting LiveKit:",e)}this._audioController&&this._audioController.stopCapture(),this._cameraController&&this._cameraController.stopCamera()}get isConnected(){return this.ensureNotDisposed(),this._context.sessionState.isConnected}_ensureConnected(){if(!this._context.sessionState.isConnected)throw new u("SDK is not connected. Call connect() first.",h.SDK_NOT_CONNECTED)}onDispose(){this.disconnect().catch(e=>{i.error("Error during disconnect in dispose:",e)}),this._videoController&&(this._videoController.dispose(),this._videoController=null),this._audioController&&(this._audioController.dispose(),this._audioController=null),this._cameraController&&(this._cameraController.dispose(),this._cameraController=null),this._conversationController&&(this._conversationController.dispose(),this._conversationController=null),this._liveKitService&&(this._liveKitService.dispose(),this._liveKitService=null),this._conversationTransport&&(this._conversationTransport.dispose(),this._conversationTransport=null),this._httpController&&(this._httpController.dispose(),this._httpController=null),this._connectionCoordinator&&(this._connectionCoordinator.transitionToDisposed(),this._connectionCoordinator.dispose(),this._connectionCoordinator=null),this._context.dispose(),i.info("SDK Client disposed")}}function wt(r){if(!r.avatarName)throw new u("avatarName is required",h.NO_AVATARNAME);return i.info("Creating SDK client with options:",{avatarName:r.avatarName}),new vt(r)}class z extends l{constructor(e,t){super(),this._videoElement=null,this._videoUrl=null,this._isPlaying=!1,this._frameCallbackId=null,this._videoUrl=e,t?this._videoElement=t:(this._videoElement=document.createElement("video"),this._videoElement.style.opacity="0",this._videoElement.muted=!0,this._videoElement.playsInline=!0,this._videoElement.loop=!0,document.querySelector("#video-container")?.appendChild(this._videoElement)),i.info("Local Video Debug Source created")}get videoElement(){return this._videoElement}async load(){if(!this._videoElement)throw new Error("Video element or URL not set");return new Promise((e,t)=>{const s=this._videoElement,n=()=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",o),i.info(`Video loaded: ${s.videoWidth}x${s.videoHeight}`),e()},o=a=>{s.removeEventListener("loadedmetadata",n),s.removeEventListener("error",o),t(new Error(`Failed to load video: ${a}`))};s.addEventListener("loadedmetadata",n),s.addEventListener("error",o),s.src=this._videoUrl||"",s.load()})}async play(){if(!this._videoElement)throw new Error("Video element not set");if(!this._isPlaying)try{await this._videoElement.play(),this._videoElement.style.opacity="0",this._videoElement.setAttribute("loop","true"),this._isPlaying=!0,i.info("Video playback started")}catch(e){throw i.error("Failed to play video:",e),e}}pause(){this._videoElement&&this._isPlaying&&(this._videoElement.pause(),this._isPlaying=!1,i.info("Video playback paused"))}stop(){this._videoElement&&(this._videoElement.pause(),this._videoElement.currentTime=0,this._isPlaying=!1,i.info("Video playback stopped"))}get isPlaying(){return this._isPlaying&&this._videoElement!==null&&!this._videoElement.paused}get videoWidth(){return this._videoElement?.videoWidth||0}get videoHeight(){return this._videoElement?.videoHeight||0}async captureFrame(){if(!this._videoElement||this._videoElement.readyState<2)return null;try{if("VideoFrame"in window&&typeof window.VideoFrame=="function")try{return new VideoFrame(this._videoElement,{timestamp:this._videoElement.currentTime*1e6})}catch(s){i.error("",s)}const e=await createImageBitmap(this._videoElement),t=new VideoFrame(e,{timestamp:this._videoElement.currentTime*1e6});return e.close(),t}catch(e){return i.error("Failed to capture video frame:",e),null}}onFrame(e){if(!this._videoElement)return;this.offFrame();const t=this._videoElement;if("requestVideoFrameCallback"in t){const s=(n,o)=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(a=>{a&&(e(a),a.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=t.requestVideoFrameCallback(s))}).catch(a=>{i.error("Error in frame callback:",a)})};this._frameCallbackId=t.requestVideoFrameCallback(s)}else{const s=()=>{!this._isPlaying||!this._videoElement||this.captureFrame().then(n=>{n&&(e(n),n.close()),this._isPlaying&&this._videoElement&&(this._frameCallbackId=requestAnimationFrame(s))}).catch(n=>{i.error("Error in frame callback:",n)})};this._frameCallbackId=requestAnimationFrame(s)}}offFrame(){this._frameCallbackId!==null&&(this._videoElement&&"cancelVideoFrameCallback"in this._videoElement?this._videoElement.cancelVideoFrameCallback(this._frameCallbackId):cancelAnimationFrame(this._frameCallbackId),this._frameCallbackId=null)}onDispose(){this.stop(),this.offFrame(),this._videoElement&&this._videoElement.parentElement===document.body&&document.body.removeChild(this._videoElement),this._videoElement=null,this._videoUrl=null,i.info("Local Video Debug Source disposed")}}class Et extends l{constructor(e,t,s){super(),this._outputCanvas=null,this._outputRenderer=null,this._source=new Y;const n={...e,renderMode:"processed"};this._pipeline=new Z(this._source,n),this._outputRenderer=new I(s,t),this._pipeline.setRenderer(this._outputRenderer),this._outputCanvas=this._outputRenderer.getCanvas(),i.info("Video Pipeline Runner created")}get outputCanvas(){return this._outputCanvas}get processor(){return this._pipeline.strategy.getProcessors()[0]||null}setProcessor(e){this._pipeline.setProcessor(e),i.info("Processor set on pipeline")}async processFrame(e){if(this.ensureNotDisposed(),!this._outputRenderer){i.error("Output renderer not available"),e.close();return}try{const t=this._pipeline.strategy.getProcessors()[0]||null;let s=null;if(t){try{s=t.process(e)}catch(n){i.error("Error processing frame:",n),s=e}if(s===null&&"getImageData"in t){const n=t.getImageData();if(n&&this._outputCanvas&&this._outputRenderer.getContext()){const o=this._outputRenderer.getContext();o&&((this._outputCanvas.width!==n.width||this._outputCanvas.height!==n.height)&&(this._outputCanvas.width=n.width,this._outputCanvas.height=n.height),o.putImageData(n,0,0))}e.close();return}s&&s!==e?(this._outputRenderer.render(s),e.close()):s===e?this._outputRenderer.render(e):e.close()}else this._outputRenderer.render(e)}catch(t){i.error("Error in processFrame:",t),e.close()}}setOutputSize(e,t){this._outputCanvas&&(this._outputCanvas.width=e,this._outputCanvas.height=t,i.info(`Output canvas size set to ${e}x${t}`))}onDispose(){this._pipeline.dispose(),this._outputRenderer&&(this._outputRenderer.dispose(),this._outputRenderer=null),this._outputCanvas&&this._outputCanvas.parentElement===document.body&&document.body.removeChild(this._outputCanvas),this._outputCanvas=null,i.info("Video Pipeline Runner disposed")}}class Rt extends l{constructor(e,t,s,n){if(super(),this._isRunning=!1,this._videoSource=new z(e,t),this._options={renderMode:"processed",containerElement:t,...n},this._pipelineRunner=new Et(this._options,t,s),this._options.greenScreen?.enabled){const o=new k(this._options.greenScreen);this._pipelineRunner.setProcessor(o),i.info("Green screen processor enabled in debugger")}i.info("Standalone Video Debugger created")}async switchSource(e,t){this.ensureNotDisposed();const s=this._videoSource,n=this._isRunning,o=new z(e,t);try{await o.load(),n&&await o.play(),o.onFrame(d=>{this._pipelineRunner.processFrame(d).catch(m=>{i.error("Error processing frame:",m)})}),this._videoSource=o;try{s.offFrame(),s.stop(),s.dispose()}catch(d){console.warn("Error disposing old video source during switch:",d)}const a=this._videoSource.videoWidth,c=this._videoSource.videoHeight;a>0&&c>0&&this._pipelineRunner.setOutputSize(a,c)}catch(a){i.error("Failed to switch video source:",a);try{o.dispose()}catch(c){console.warn("Error disposing new video source after failed switch:",c)}throw a}}get outputCanvas(){return this._pipelineRunner.outputCanvas}get videoSource(){return this._videoSource}get processor(){return this._pipelineRunner.processor}setProcessor(e){this._pipelineRunner.setProcessor(e)}updateGreenScreenOptions(e){const t=this._pipelineRunner.processor;if(t&&"updateOptions"in t&&typeof t.updateOptions=="function")t.updateOptions(e),i.info("Green screen options updated");else{const s=this._options.greenScreen||{enabled:!1},n={...s,...e,enabled:e.enabled!==void 0?e.enabled:s.enabled},o=new k(n);this._pipelineRunner.setProcessor(o),this._options.greenScreen=n,i.info("Green screen processor recreated with new options")}}async initialize(){this.ensureNotDisposed();try{await this._videoSource.load();const e=this._videoSource.videoWidth,t=this._videoSource.videoHeight;e>0&&t>0&&this._pipelineRunner.setOutputSize(e,t),i.info("Video debugger initialized")}catch(e){throw i.error("Failed to initialize video debugger:",e),e}}async start(){if(this.ensureNotDisposed(),this._isRunning){i.warn("Debugger is already running");return}try{this._videoSource.videoWidth===0&&await this.initialize(),await this._videoSource.play(),this._videoSource.onFrame(e=>{this._pipelineRunner.processFrame(e).catch(t=>{i.error("Error processing frame:",t)})}),this._isRunning=!0,i.info("Video debugger started")}catch(e){throw i.error("Failed to start video debugger:",e),e}}stop(){this._isRunning&&(this._videoSource.stop(),this._videoSource.offFrame(),this._isRunning=!1,i.info("Video debugger stopped"))}pause(){this._isRunning&&(this._videoSource.pause(),i.info("Video debugger paused"))}async resume(){this._isRunning&&(await this._videoSource.play(),i.info("Video debugger resumed"))}get isRunning(){return this._isRunning}onDispose(){this.stop(),this._videoSource.dispose(),this._pipelineRunner.dispose(),i.info("Standalone Video Debugger disposed")}}export{_ as ConversationStateType,At as DEFAULT_AUDIO_BUFFER_SIZE,St as DEFAULT_AUDIO_CHANNELS,L as DEFAULT_AUDIO_SAMPLE_RATE,b as DEFAULT_RECONNECT_ATTEMPTS,G as DEFAULT_RECONNECT_DELAY,Tt as DEFAULT_WS_TIMEOUT,l as Disposable,ae as EventBus,E as LogLevel,i as Logger,vt as SDKClient,u as SDKError,de as SessionState,f as SessionStatus,Rt as StandaloneVideoDebugger,wt as createClient};
|