@ikonai/sdk 1.0.79 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app-messaging.d.ts +1 -1
- package/assets/audio-capture-worker-D-0X2Dy8.js +1 -0
- package/assets/audio-playback-worker-DivBZPfS.js +1 -0
- package/assets/libopus-CnBCihDi.js +1 -0
- package/assets/protocol-worker-BH93CVck.js +1 -0
- package/assets/src-BKYD07QQ.js +1 -0
- package/assets/src-DOUWsCWT.js +1 -0
- package/assets/video-capture-worker-BQGOFbZt.js +1 -0
- package/assets/video-playback-worker-tC5nUVnw.js +1 -0
- package/assets/viseme-worker-Cd-JqTVM.js +1 -0
- package/capabilities-BtJblfdG.js +108 -0
- package/channel/channel-manager.d.ts +1 -1
- package/channel/channel.d.ts +1 -1
- package/client/endpoint-selector.d.ts +1 -1
- package/client/ikon-client-config.d.ts +13 -11
- package/client/ikon-client.d.ts +98 -4
- package/connection/authenticator.d.ts +3 -3
- package/connection/urls.d.ts +7 -18
- package/errors/index.d.ts +22 -13
- package/functions/browser-functions.d.ts +1 -1
- package/functions/function-registry.d.ts +1 -1
- package/functions/types.d.ts +2 -2
- package/ikon-media-zqob_QeE.js +3160 -0
- package/index.d.ts +8 -8
- package/index.js +8119 -12502
- package/media/ikon-audio-capture.d.ts +15 -3
- package/media/ikon-audio-playback.d.ts +37 -0
- package/media/ikon-video-playback.d.ts +24 -0
- package/media/media-session-helper.d.ts +8 -0
- package/media/video-constants.d.ts +11 -1
- package/media/viseme-tracker.d.ts +10 -1
- package/package.json +1 -1
- package/storage.d.ts +13 -0
- package/transport/transport.d.ts +1 -1
- package/transport/web-socket-transport.d.ts +1 -1
- package/transport/web-transport-transport.d.ts +1 -1
- package/utils/inspect-mode.d.ts +4 -0
- package/utils/log-sender.d.ts +1 -1
- package/utils/platform.d.ts +4 -0
- package/utils/query-params.d.ts +8 -4
- package/vite.config.d.ts +2 -0
- package/webrtc/webrtc-signaling.d.ts +11 -2
- package/worker/audio-playback-worker.d.ts +14 -1
- package/worker/video-capture-worker.d.ts +2 -2
- package/worker/video-playback-worker.d.ts +14 -2
- package/worker/viseme-worker.d.ts +10 -2
- package/assets/audio-capture-worker-Brmt2Z7a.js +0 -1279
- package/assets/audio-playback-worker-DDtsnLoq.js +0 -1473
- package/assets/index-Ca9sTcGt.js +0 -119
- package/assets/index-D_vz6N3U.js +0 -267
- package/assets/libopus-ChbDZJKA.js +0 -139
- package/assets/protocol-worker-BoZcMF0x.js +0 -1674
- package/assets/video-capture-worker-DxgvFTcw.js +0 -1126
- package/assets/video-playback-worker-Ca4XVGqt.js +0 -1005
- package/assets/viseme-worker-DVWM4pRh.js +0 -831
package/app-messaging.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ProtocolMessage } from '
|
|
1
|
+
import { ProtocolMessage } from '../shared/protocol/src/index.ts';
|
|
2
2
|
import { IkonClient } from './client/ikon-client';
|
|
3
3
|
/**
|
|
4
4
|
* Describes one app-local Teleport message type, tying together the opcode and
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return e}function t(t,n,r,i,a){let o=a?.trackId??0,s=a?.sequenceId??0,c=a?.flags??0,l=a?.targetIds??[],u=a?.payloadType??8,d=27+l.length*4,f=d+n.length,p=new Uint8Array(f),m=new DataView(p.buffer);m.setUint32(0,f,!0),m.setUint32(4,t>>>0,!0),m.setUint32(8,i>>>0,!0),m.setUint32(12,o>>>0,!0),m.setUint32(16,s>>>0,!0),m.setUint32(20,l.length>>>0,!0),m.setUint8(24,r&255),m.setUint8(25,u&255),m.setUint8(26,c&255);let h=27;for(let e=0;e<l.length;e++)m.setUint32(h,l[e]>>>0,!0),h+=4;return p.set(n,d),e(p)}let n=function(e){return e[e.Null=1]=`Null`,e[e.Bool=2]=`Bool`,e[e.Int32=3]=`Int32`,e[e.Int64=4]=`Int64`,e[e.UInt32=5]=`UInt32`,e[e.UInt64=6]=`UInt64`,e[e.Float32=7]=`Float32`,e[e.Float64=8]=`Float64`,e[e.Array=9]=`Array`,e[e.Dict=10]=`Dict`,e[e.Object=11]=`Object`,e[e.String=12]=`String`,e[e.Binary=13]=`Binary`,e[e.Guid=14]=`Guid`,e}({});const r=new TextEncoder;new TextDecoder(`utf-8`,{fatal:!0});var i=class e{buffer=new l;cached;constructor(e=1){this.buffer.writeByte(161),this.buffer.writeVarUInt(e>>>0)}writeInt32Field(e,t){this.writeFixedField(e,3,()=>this.buffer.writeInt32(t|0))}writeUInt32Field(e,t){this.writeFixedField(e,5,()=>this.buffer.writeUInt32(t>>>0))}writeInt64Field(e,t){this.writeFixedField(e,4,()=>this.buffer.writeBigInt64(t))}writeUInt64Field(e,t){this.writeFixedField(e,6,()=>this.buffer.writeBigUInt64(t))}writeFloat32Field(e,t){this.writeFixedField(e,7,()=>this.buffer.writeFloat32(t))}writeFloat64Field(e,t){this.writeFixedField(e,8,()=>this.buffer.writeFloat64(t))}writeBoolField(e,t){this.writeFixedField(e,2,()=>this.buffer.writeByte(+!!t))}writeGuidField(e,t){let n=t instanceof c?t.asBytes():t;if(n.length!==16)throw Error(`Guid payload must be 16 bytes`);this.writeFixedField(e,14,()=>this.buffer.writeBytes(n))}writeStringField(e,t){let n=r.encode(t??``);this.writeVariableField(e,12,n)}writeBinaryField(e,t){this.writeVariableField(e,13,t)}writeObjectField(t,n,r){let i=new e(n);r(i);let a=i.finish();this.writeVariableField(t,11,a)}writeArrayField(e,t,n){let r=new a(t);n(r);let i=r.finish();this.writeVariableField(e,9,i)}writeDictionaryField(e,t,n,r){let i=new o(t,n);r(i);let a=i.finish();this.writeVariableField(e,10,a)}finish(){return this.cached||=(this.buffer.writeByte(162),this.buffer.toUint8Array()),this.cached}writeFixedField(e,t,n){this.writeFieldHeader(e,t,0),n()}writeVariableField(e,t,n){this.writeFieldHeader(e,t,n.length),this.buffer.writeBytes(n)}writeFieldHeader(e,t,n){this.buffer.writeUInt32(e>>>0),this.buffer.writeByte(u(t)),d(t)&&this.buffer.writeVarUInt(n>>>0)}},a=class e{elementType;payload=new l;count=0;constructor(e){this.elementType=e}writeInt32(e){this.ensureElementType(3),this.count++,this.payload.writeInt32(e|0)}writeUInt32(e){this.ensureElementType(5),this.count++,this.payload.writeUInt32(e>>>0)}writeInt64(e){this.ensureElementType(4),this.count++,this.payload.writeBigInt64(e)}writeUInt64(e){this.ensureElementType(6),this.count++,this.payload.writeBigUInt64(e)}writeFloat32(e){this.ensureElementType(7),this.count++,this.payload.writeFloat32(e)}writeFloat64(e){this.ensureElementType(8),this.count++,this.payload.writeFloat64(e)}writeBool(e){this.ensureElementType(2),this.count++,this.payload.writeByte(+!!e)}writeGuid(e){this.ensureElementType(14),this.count++;let t=e instanceof c?e.asBytes():e;if(t.length!==16)throw Error(`Guid payload must be 16 bytes`);this.payload.writeBytes(t)}writeString(e){this.ensureElementType(12),this.count++;let t=r.encode(e??``);this.payload.writeVarUInt(t.length),this.payload.writeBytes(t)}writeBinary(e){this.ensureElementType(13),this.count++,this.payload.writeVarUInt(e.length),this.payload.writeBytes(e)}writeObject(e,t){this.ensureElementType(11),this.count++;let n=new i(e);t(n);let r=n.finish();this.payload.writeVarUInt(r.length),this.payload.writeBytes(r)}writeArray(t,n){this.ensureElementType(9),this.count++;let r=new e(t);n(r);let i=r.finish();this.payload.writeBytes(i)}writeDictionary(e,t,n){this.ensureElementType(10),this.count++;let r=new o(e,t);n(r);let i=r.finish();this.payload.writeBytes(i)}finish(){let e=new l;return e.writeByte(u(this.elementType)),e.writeVarUInt(this.count),e.writeBytes(this.payload.toUint8Array()),e.toUint8Array()}ensureElementType(e){if(this.elementType!==e)throw Error(`Array element type is ${n[this.elementType]}, expected ${n[e]}`)}},o=class{keyType;valueType;payload=new l;count=0;entryOpen=!1;constructor(e,t){this.keyType=e,this.valueType=t,f(e)}beginEntry(){if(this.entryOpen)throw Error(`Previous dictionary entry not completed`);return this.count++,this.entryOpen=!0,new s(this.keyType,this.valueType,this.payload,()=>{this.entryOpen=!1})}finish(){if(this.entryOpen)throw Error(`Dictionary entry not completed`);let e=new l;return e.writeByte(u(this.keyType)),e.writeByte(u(this.valueType)),e.writeVarUInt(this.count),e.writeBytes(this.payload.toUint8Array()),e.toUint8Array()}},s=class{keyType;valueType;payload;onComplete;keyWritten=!1;valueWritten=!1;completed=!1;constructor(e,t,n,r){this.keyType=e,this.valueType=t,this.payload=n,this.onComplete=r}writeKeyInt32(e){this.ensureKeyType(3),this.payload.writeInt32(e|0),this.keyWritten=!0}writeKeyUInt32(e){this.ensureKeyType(5),this.payload.writeUInt32(e>>>0),this.keyWritten=!0}writeKeyInt64(e){this.ensureKeyType(4),this.payload.writeBigInt64(e),this.keyWritten=!0}writeKeyUInt64(e){this.ensureKeyType(6),this.payload.writeBigUInt64(e),this.keyWritten=!0}writeKeyFloat32(e){this.ensureKeyType(7),this.payload.writeFloat32(e),this.keyWritten=!0}writeKeyFloat64(e){this.ensureKeyType(8),this.payload.writeFloat64(e),this.keyWritten=!0}writeKeyBool(e){this.ensureKeyType(2),this.payload.writeByte(+!!e),this.keyWritten=!0}writeKeyGuid(e){this.ensureKeyType(14);let t=e instanceof c?e.asBytes():e;if(t.length!==16)throw Error(`Guid payload must be 16 bytes`);this.payload.writeBytes(t),this.keyWritten=!0}writeKeyString(e){this.ensureKeyType(12);let t=r.encode(e??``);this.payload.writeVarUInt(t.length),this.payload.writeBytes(t),this.keyWritten=!0}writeKeyBinary(e){this.ensureKeyType(13),this.payload.writeVarUInt(e.length),this.payload.writeBytes(e),this.keyWritten=!0}writeValueInt32(e){this.ensureValueType(3),this.payload.writeInt32(e|0),this.valueWritten=!0}writeValueUInt32(e){this.ensureValueType(5),this.payload.writeUInt32(e>>>0),this.valueWritten=!0}writeValueInt64(e){this.ensureValueType(4),this.payload.writeBigInt64(e),this.valueWritten=!0}writeValueUInt64(e){this.ensureValueType(6),this.payload.writeBigUInt64(e),this.valueWritten=!0}writeValueFloat32(e){this.ensureValueType(7),this.payload.writeFloat32(e),this.valueWritten=!0}writeValueFloat64(e){this.ensureValueType(8),this.payload.writeFloat64(e),this.valueWritten=!0}writeValueBool(e){this.ensureValueType(2),this.payload.writeByte(+!!e),this.valueWritten=!0}writeValueGuid(e){this.ensureValueType(14);let t=e instanceof c?e.asBytes():e;if(t.length!==16)throw Error(`Guid payload must be 16 bytes`);this.payload.writeBytes(t),this.valueWritten=!0}writeNullValue(){this.ensureValueType(1),this.valueWritten=!0}writeValueBinary(e){this.ensureValueType(13),this.payload.writeVarUInt(e.length),this.payload.writeBytes(e),this.valueWritten=!0}writeValueString(e){this.ensureValueType(12);let t=r.encode(e??``);this.payload.writeVarUInt(t.length),this.payload.writeBytes(t),this.valueWritten=!0}writeValueObject(e,t){this.ensureValueType(11);let n=new i(e);t(n);let r=n.finish();this.payload.writeVarUInt(r.length),this.payload.writeBytes(r),this.valueWritten=!0}writeValueArray(e,t){this.ensureValueType(9);let n=new a(e);t(n);let r=n.finish();this.payload.writeBytes(r),this.valueWritten=!0}writeValueDictionary(e,t,n){this.ensureValueType(10);let r=new o(e,t);n(r);let i=r.finish();this.payload.writeBytes(i),this.valueWritten=!0}complete(){if(!this.completed){if(!this.keyWritten||!this.valueWritten)throw Error(`Dictionary entry must write both key and value`);this.completed=!0,this.onComplete()}}ensureKeyType(e){if(this.keyType!==e)throw Error(`Dictionary key type is ${n[this.keyType]}, expected ${n[e]}`)}ensureValueType(e){if(this.valueType!==e)throw Error(`Dictionary value type is ${n[this.valueType]}, expected ${n[e]}`)}},c=class e{bytes;constructor(e){this.bytes=e}static fromString(t){if(!t)throw Error(`Guid string is empty`);let n=t.replace(/-/g,``);if(n.length!==32)throw Error(`Guid string must be 32 hex characters`);let r=new Uint8Array(16),i=e.parseHexSlice(n,0,8),a=e.parseHexSlice(n,8,4),o=e.parseHexSlice(n,12,4);e.writeUInt32LE(r,0,i),e.writeUInt16LE(r,4,a),e.writeUInt16LE(r,6,o);for(let t=0;t<8;t++)r[8+t]=e.parseHexSlice(n,16+t*2,2);return new e(r)}static fromBytes(t){if(t.length!==16)throw Error(`Guid byte array must be 16 bytes`);return new e(Uint8Array.from(t))}static createZero(){return new e(new Uint8Array(16))}static createRandom(){let t=new Uint8Array(16),n=globalThis.crypto;if(n?.getRandomValues)n.getRandomValues(t);else for(let e=0;e<t.length;e++)t[e]=Math.floor(Math.random()*256);return t[6]=t[6]&15|64,t[8]=t[8]&63|128,new e(t)}toString(){let t=this.bytes;return[e.toHex(e.readUInt32LE(t,0),8),e.toHex(e.readUInt16LE(t,4),4),e.toHex(e.readUInt16LE(t,6),4),p(t.subarray(8,10)),p(t.subarray(10,16))].join(`-`)}asBytes(){return this.bytes.slice()}static parseHexSlice(e,t,n){let r=e.substr(t,n),i=Number.parseInt(r,16);if(Number.isNaN(i))throw Error(`Guid string contains invalid characters`);return i>>>0}static writeUInt32LE(e,t,n){let r=n>>>0;e[t]=r&255,e[t+1]=r>>>8&255,e[t+2]=r>>>16&255,e[t+3]=r>>>24&255}static writeUInt16LE(e,t,n){let r=n&65535;e[t]=r&255,e[t+1]=r>>>8&255}static readUInt32LE(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0}static readUInt16LE(e,t){return(e[t]|e[t+1]<<8)&65535}static toHex(e,t){return(e>>>0).toString(16).padStart(t,`0`)}},l=class e{static INITIAL_CAPACITY=256;buffer;dataView;length=0;constructor(){this.buffer=new Uint8Array(e.INITIAL_CAPACITY),this.dataView=new DataView(this.buffer.buffer)}ensureCapacity(e){let t=this.length+e;if(t<=this.buffer.length)return;let n=this.buffer.length;for(;n<t;)n*=2;let r=new Uint8Array(n);r.set(this.buffer.subarray(0,this.length)),this.buffer=r,this.dataView=new DataView(this.buffer.buffer)}writeByte(e){this.ensureCapacity(1),this.buffer[this.length++]=e&255}writeBytes(e){this.ensureCapacity(e.length),this.buffer.set(e,this.length),this.length+=e.length}writeUInt32(e){this.ensureCapacity(4),this.dataView.setUint32(this.length,e>>>0,!0),this.length+=4}writeInt32(e){this.ensureCapacity(4),this.dataView.setInt32(this.length,e|0,!0),this.length+=4}writeFloat32(e){this.ensureCapacity(4),this.dataView.setFloat32(this.length,e,!0),this.length+=4}writeFloat64(e){this.ensureCapacity(8),this.dataView.setFloat64(this.length,e,!0),this.length+=8}writeBigInt64(e){this.ensureCapacity(8),this.dataView.setBigInt64(this.length,e,!0),this.length+=8}writeBigUInt64(e){this.ensureCapacity(8),this.dataView.setBigUint64(this.length,e,!0),this.length+=8}writeVarUInt(e){let t=e>>>0;for(;t>=128;)this.ensureCapacity(1),this.buffer[this.length++]=t&127|128,t>>>=7;this.ensureCapacity(1),this.buffer[this.length++]=t&127}toUint8Array(){return this.buffer.slice(0,this.length)}};function u(e,t=0){if(t&240)throw Error(`Teleport flags must fit into 4 bits`);return(e&15)<<4|t&15}function d(e){return e===12||e===13||e===9||e===11||e===10}function f(e){if(e===9||e===11||e===10||e===1)throw Error(`Dictionary keys must be primitive Teleport types`)}function p(e){return Array.from(e).map(e=>e.toString(16).padStart(2,`0`)).join(``)}let m=function(e){return e[e.Unknown=0]=`Unknown`,e[e.Opus=1]=`Opus`,e[e.Mp3=2]=`Mp3`,e[e.RawPcm16=3]=`RawPcm16`,e}({});function h(e){let t={};return ee(t),e&&Object.assign(t,e),t}function ee(e){return e.Samples=new Uint8Array,e.Epoch=0,e.Sequence=0,e.FrameSizeInInterleavedSamples=0,e.TimeStampInInterleavedSamples=BigInt(0),e.IsFirst=!1,e.IsLast=!1,e.AverageVolume=0,e.AudioEventEstimatedDuration=0,e}function g(e){let t=new i(1);return _(e,t),t.finish()}function _(e,t){t.writeFloat32Field(205938238,e.AverageVolume),t.writeBoolField(1594075008,e.IsFirst),t.writeUInt64Field(2026534360,e.TimeStampInInterleavedSamples),t.writeBinaryField(3409001585,e.Samples),t.writeFloat32Field(3466650495,e.AudioEventEstimatedDuration),t.writeUInt32Field(4007456593,e.FrameSizeInInterleavedSamples>>>0),t.writeBoolField(4017217601,e.IsLast),t.writeUInt32Field(4065417427,e.Sequence>>>0),t.writeUInt32Field(4086360332,e.Epoch>>>0)}function v(e,n,r){return t(4194309,g(e),1,n,r)}function y(e){let t={};return b(t),e&&Object.assign(t,e),t}function b(e){return e.StreamId=``,e.Description=``,e.SourceType=``,e.Codec=m.Unknown,e.CodecDetails=``,e.SampleRate=0,e.Channels=0,e.ShapeSets=void 0,e.CorrelationId=void 0,e}function x(e){let t=new i(6);return S(e,t),t.finish()}function S(e,t){t.writeStringField(161083277,e.StreamId);let r=e.ShapeSets;r!=null&&t.writeArrayField(265814330,n.Object,e=>{for(let t of r)e.writeObject(6,e=>{w(t,e)})}),t.writeStringField(1368629611,e.Description),t.writeInt32Field(2431514951,e.Channels|0);let i=e.CorrelationId;i!=null&&t.writeStringField(2514959030,i),t.writeStringField(2745379226,e.CodecDetails),t.writeStringField(3282782683,e.SourceType),t.writeInt32Field(3284746250,e.Codec),t.writeInt32Field(4101844078,e.SampleRate|0)}function C(e,n,r){return t(4194305,x(e),6,n,r)}function w(e,t){t.writeStringField(1107713536,e.Name),t.writeUInt32Field(1154362099,e.SetId>>>0),t.writeArrayField(1185721362,n.String,t=>{for(let n of e.ShapeNames)t.writeString(n)})}function T(e){let t={};return e&&Object.assign(t,e),t}function E(e){return new i(1).finish()}function D(e,n,r){return t(4194306,E(e),1,n,r)}var O=class{static getStorageForCapacity(e,t){let n=Int32Array.BYTES_PER_ELEMENT*2,r=t.BYTES_PER_ELEMENT*e;return new SharedArrayBuffer(n+r)}readWrite;storage;buf;constructor(e,t){this.buf=e,this.readWrite=new Int32Array(e,0,2);let n=Int32Array.BYTES_PER_ELEMENT*2,r=(e.byteLength-n)/Float32Array.BYTES_PER_ELEMENT;this.storage=new t(e,n,r)}get capacity(){return this.storage.length}flush(){Atomics.store(this.readWrite,0,0),Atomics.store(this.readWrite,1,0)}availableRead(){let e=Atomics.load(this.readWrite,0),t=Atomics.load(this.readWrite,1);return t>=e?t-e:this.capacity-(e-t)}availableWrite(){return this.capacity-this.availableRead()-1}push(e){let t=Math.min(this.availableWrite(),e.length);if(t<=0)return 0;let n=Atomics.load(this.readWrite,1),r=Math.min(t,this.capacity-n);this.storage.set(e.subarray(0,r),n);let i=t-r;return i>0&&this.storage.set(e.subarray(r,r+i),0),n=(n+t)%this.capacity,Atomics.store(this.readWrite,1,n),Atomics.notify(this.readWrite,1),t}pop(e){let t=Math.min(this.availableRead(),e.length);if(t<=0)return 0;let n=Atomics.load(this.readWrite,0),r=Math.min(t,this.capacity-n);e.set(this.storage.subarray(n,n+r),0);let i=t-r;return i>0&&e.set(this.storage.subarray(0,i),r),n=(n+t)%this.capacity,Atomics.store(this.readWrite,0,n),t}skip(e){let t=Math.min(this.availableRead(),e);if(t<=0)return 0;let n=Atomics.load(this.readWrite,0);return n=(n+t)%this.capacity,Atomics.store(this.readWrite,0,n),t}};const k=new Map,te={0:`DEBUG`,1:`INFO`,2:`WARN`,3:`ERROR`,4:`NONE`};function ne(e,t){return`[${e}] ${t}`}function A(e,t,n,r){let i={timestamp:new Date().toISOString(),level:e,levelName:te[e],component:t,message:n,args:r};for(let[t,n]of k)if(e>=(n?.minLevel??2))try{t(i)}catch{}if(1<=e){let i=ne(t,n);switch(e){case 0:console.debug(i,...r);break;case 1:console.info(i,...r);break;case 2:console.warn(i,...r);break;case 3:console.error(i,...r);break;case 4:break}}}function j(e){return{debug(t,...n){A(0,e,t,n)},info(t,...n){A(1,e,t,n)},warn(t,...n){A(2,e,t,n)},error(t,...n){A(3,e,t,n)}}}const M=8e3,N=j(`AudioCaptureWorker`);var P=class{encoder=null;isReady=!1;sampleRate;channels;bitrate;frameSize;onOutput;onError;timestampUs=0;frameDurationUs;constructor(e){this.sampleRate=e.sampleRate,this.channels=e.channels,this.bitrate=e.bitrate,this.frameSize=e.frameSize,this.frameDurationUs=Math.floor(this.frameSize*1e6/this.sampleRate),this.onOutput=e.onOutput,this.onError=e.onError}async initialize(){try{let{OpusEncoder:e,OPUS_APPLICATION_VOIP:t,getOpusModule:n}=await import(`./src-BKYD07QQ.js`);await n(),this.encoder=new e({sampleRate:this.sampleRate,channels:this.channels,bitrate:this.bitrate,application:t,complexity:5}),await this.encoder.ready,this.isReady=!0}catch(e){this.onError(e instanceof Error?e:Error(String(e)))}}encode(e){if(!(!this.isReady||!this.encoder))try{let t;if(e instanceof Float32Array)t=e;else{let n=e.numberOfFrames,r=e.numberOfChannels;t=new Float32Array(n*r),e.copyTo(t,{planeIndex:0,format:`f32`}),e.close()}let n=this.encoder.encodeAndCopy(t);this.onOutput(n,this.timestampUs,this.frameDurationUs),this.timestampUs+=this.frameDurationUs}catch(e){this.onError(e instanceof Error?e:Error(String(e)))}}async flush(){}close(){try{this.encoder?.destroy()}catch{}this.encoder=null,this.isReady=!1}getBitrate(){return this.bitrate}};const F=e=>self.postMessage(e);let I=null;function L(e,t){let n=e instanceof Error?e:Error(String(e));F({type:`error`,captureId:t,error:{name:n.name,message:n.message,stack:n.stack}})}function R(e){return e.byteOffset===0&&e.byteLength===e.buffer.byteLength?e.buffer:e.slice().buffer}function z(e){if(!I){N.warn(`Audio capture message dropped: sendPort not available`);return}let t=R(e);I.postMessage({type:`send`,message:t},[t])}function B(e){if(e.length===0)return 0;let t=0;for(let n=0;n<e.length;n++)t+=Math.abs(e[n]??0);return t/e.length}const V=new Map,H=new Map;let U=null;const W=Atomics;function re(){return typeof Atomics<`u`&&typeof W.waitAsync==`function`}async function G(e){if(!e.ringBuffer||!e.ringTemp)return;let t=e.ringBuffer,n=e.ringTemp;for(;!e.stopped&&e.transport===`sab`;){try{let r=t.pop(n);r>0&&X(e,n.subarray(0,r))}catch(t){L(t,e.captureId)}try{let e=Atomics.load(t.readWrite,1),n=Atomics.waitAsync(t.readWrite,1,e,100);n.async&&await n.value}catch{await new Promise(e=>setTimeout(e,10))}}}function K(){for(let e of V.values())if(!(e.transport!==`sab`||!e.ringBuffer||!e.ringTemp||e.stopped))try{let t=e.ringBuffer.pop(e.ringTemp);t>0&&X(e,e.ringTemp.subarray(0,t))}catch(t){L(t,e.captureId)}}function q(e){if(re()){if(!H.has(e.captureId)){let t=G(e);H.set(e.captureId,t),t.catch(t=>L(t,e.captureId)).finally(()=>{H.delete(e.captureId)})}}else U===null&&(U=self.setInterval(K,50))}function J(e){if(H.delete(e),U!==null){let e=!1;for(let t of V.values())if(t.transport===`sab`&&!t.stopped){e=!0;break}if(!e){try{self.clearInterval(U)}catch{}U=null}}}function Y(e){if(e.stopped||e.isReconfiguring)return;if(!e.inSegment){e.pendingReadOffset=0,e.pendingWriteOffset=0,e.pendingSamples=0;return}let t=e.frameSize,n=e.frameBuffer,r=e.pendingBuffer.length;for(;e.pendingSamples>=t;){let i=e.pendingReadOffset;if(i+t<=r)n.set(e.pendingBuffer.subarray(i,i+t));else{let a=r-i;n.set(e.pendingBuffer.subarray(i,r)),n.set(e.pendingBuffer.subarray(0,t-a),a)}e.pendingReadOffset=(i+t)%r,e.pendingSamples-=t,e.lastVolume=B(n);try{if(e.useWasmEncoder)e.encoder.encode(n);else{let r=new AudioData({format:`f32`,sampleRate:e.sampleRate,numberOfFrames:t,numberOfChannels:e.channels,timestamp:e.timestampUs,data:n.buffer});e.encoder.encode(r),r.close()}}catch(t){L(t,e.captureId)}e.timestampUs+=e.frameDurationUs}}function X(e,t){if(e.stopped)return;let n=t.length;if(n===0)return;let r=e.pendingSamples+n;if(r>e.pendingBuffer.length){let t=Math.max(r,e.pendingBuffer.length*2),n=new Float32Array(t);if(e.pendingSamples>0){let t=e.pendingBuffer.length;if(e.pendingReadOffset+e.pendingSamples<=t)n.set(e.pendingBuffer.subarray(e.pendingReadOffset,e.pendingReadOffset+e.pendingSamples));else{let r=t-e.pendingReadOffset;n.set(e.pendingBuffer.subarray(e.pendingReadOffset,t)),n.set(e.pendingBuffer.subarray(0,e.pendingSamples-r),r)}}e.pendingBuffer=n,e.pendingReadOffset=0,e.pendingWriteOffset=e.pendingSamples}let i=e.pendingBuffer.length,a=Math.min(n,i-e.pendingWriteOffset);e.pendingBuffer.set(t.subarray(0,a),e.pendingWriteOffset),a<n&&e.pendingBuffer.set(t.subarray(a),0),e.pendingWriteOffset=(e.pendingWriteOffset+n)%i,e.pendingSamples+=n,Y(e)}function Z(e,t,n,r,i=!0){let a=V.get(e.captureId);if(!a||a.stopped||!a.inSegment&&!a.segmentEndPending)return;let o=t.byteLength;a.encoderOutputBuffer.length<o&&(a.encoderOutputBuffer=new Uint8Array(Math.max(o,a.encoderOutputBuffer.length*2))),a.encoderOutputBuffer.set(t);let s=a.encoderOutputBuffer.subarray(0,o),c=a.segmentStartPending;c&&(a.segmentStartPending=!1,a.epoch++,a.sequence=0);let l=a.segmentEndPending;l&&(a.segmentEndPending=!1,a.inSegment=!1);let u=a.frameSize*a.channels,d=BigInt(Math.floor(n/1e6*a.sampleRate*a.channels));z(v(h({Samples:s,Epoch:a.epoch,Sequence:a.sequence,FrameSizeInInterleavedSamples:u,TimeStampInInterleavedSamples:d,IsFirst:c,IsLast:l,AverageVolume:a.lastVolume,AudioEventEstimatedDuration:0}),a.senderId,{trackId:a.trackId})),a.sequence++}function ie(e){let t=Math.max(M,Math.floor(e.options?.bitrate??32e3)),n=new AudioEncoder({output:t=>{let n=V.get(e.captureId);if(!n)return;let r=t.byteLength;n.encoderOutputBuffer.length<r&&(n.encoderOutputBuffer=new Uint8Array(Math.max(r,n.encoderOutputBuffer.length*2))),t.copyTo(n.encoderOutputBuffer),Z(n,n.encoderOutputBuffer.subarray(0,r),t.timestamp??n.timestampUs,t.duration??n.frameDurationUs,t.type===`key`)},error:t=>{L(t,e.captureId)}});return n.configure({codec:`opus`,numberOfChannels:e.channels,sampleRate:e.sampleRate,bitrate:t,bitrateMode:`constant`,opus:{application:`lowdelay`,signal:`voice`}}),n}function ae(e){let t=Math.max(M,Math.floor(e.options?.bitrate??32e3));return new P({sampleRate:e.sampleRate,channels:e.channels,bitrate:t,frameSize:e.frameSize,onOutput:(t,n,r)=>{let i=V.get(e.captureId);i&&Z(i,t,n,r,!0)},onError:t=>{L(t,e.captureId)}})}async function Q(){if(typeof AudioEncoder>`u`)return!1;try{return(await AudioEncoder.isConfigSupported({codec:`opus`,numberOfChannels:1,sampleRate:48e3})).supported===!0}catch{return!1}}async function oe(e){if(!I)throw Error(`Audio capture worker missing send port`);if(V.has(e.captureId)){let t=V.get(e.captureId);F({type:`started`,captureId:e.captureId,sampleRate:t.sampleRate,channels:t.channels});return}let t=Math.max(8e3,Math.floor(e.sampleRate)),n=Math.max(1,Math.floor(e.channels)),r=Math.max(1,Math.floor(t*20/1e3)),i=Math.max(1,Math.floor(r*1e6/t)),a=r*4,o={captureId:e.captureId,streamId:e.streamId,sourceType:e.sourceType,senderId:e.senderId,trackId:e.trackId,sampleRate:t,channels:n,frameSize:r,frameDurationUs:i,timestampUs:0,sentFirst:!1,stopped:!1,transport:e.transport,ringBuffer:null,ringTemp:null,pendingBuffer:new Float32Array(a),pendingReadOffset:0,pendingWriteOffset:0,pendingSamples:0,frameBuffer:new Float32Array(r),encoderOutputBuffer:new Uint8Array(1024),inSegment:!1,segmentStartPending:!1,segmentEndPending:!1,epoch:0,sequence:0,lastVolume:0,options:e.options,correlationId:e.correlationId},s=(e.options?.preferWebCodecs??!0)&&await Q(),c,l=!1,u=Math.max(M,Math.floor(e.options?.bitrate??32e3));if(s)c=ie(o),N.debug(`Audio capture started: encoder=WebCodecs, sampleRate=${t}Hz, channels=${n}, bitrate=${u}bps`);else{let e=ae(o);await e.initialize(),c=e,l=!0,N.debug(`Audio capture started: encoder=WASM Opus, sampleRate=${t}Hz, channels=${n}, bitrate=${u}bps`)}let d={...o,encoder:c,useWasmEncoder:l,isReconfiguring:!1};if(e.transport===`sab`){if(!e.sharedArrayBuffer)throw Error(`Missing SharedArrayBuffer for sab transport`);d.ringBuffer=new O(e.sharedArrayBuffer,Float32Array),d.ringTemp=new Float32Array(Math.max(d.frameSize*4,4096)),q(d)}V.set(e.captureId,d),z(C(y({StreamId:d.streamId,Description:`mic:${d.streamId}`,SourceType:d.sourceType,Codec:m.Opus,CodecDetails:``,SampleRate:d.sampleRate,Channels:d.channels,CorrelationId:d.correlationId}),d.senderId,{trackId:d.trackId})),F({type:`started`,captureId:e.captureId,sampleRate:t,channels:n})}async function $(e){let t=V.get(e);if(t){t.stopped=!0,V.delete(e);try{Y(t)}catch{}try{await t.encoder.flush()}catch{}try{t.encoder.close()}catch{}z(D(T({}),t.senderId,{trackId:t.trackId})),F({type:`stopped`,captureId:t.captureId}),J(e)}}function se(e){let t=V.get(e);!t||t.stopped||(t.inSegment=!0,t.segmentStartPending=!0)}function ce(e){let t=V.get(e);if(!(!t||t.stopped||!t.inSegment)&&(t.segmentEndPending=!0,Y(t),t.segmentEndPending&&!t.stopped)){let e=t.frameSize-t.pendingSamples;e>0&&X(t,new Float32Array(e))}}async function le(e,t){let n=V.get(e);if(!n||n.stopped)return;let r=n.options?.bitrate??32e3,i=t.bitrate??32e3;if(r===i){n.options=t;return}n.isReconfiguring=!0,n.options=t;let a=Math.max(M,Math.floor(i)),o;if(n.useWasmEncoder){let t=new P({sampleRate:n.sampleRate,channels:n.channels,bitrate:a,frameSize:n.frameSize,onOutput:(e,t,r)=>{let i=V.get(n.captureId);!i||i.isReconfiguring||Z(i,e,t,r,!0)},onError:e=>{L(e,n.captureId)}});try{await t.initialize()}catch(t){n.isReconfiguring=!1,L(t,e);return}o=t}else{let e=new AudioEncoder({output:e=>{let t=V.get(n.captureId);if(!t||t.isReconfiguring)return;let r=e.byteLength;t.encoderOutputBuffer.length<r&&(t.encoderOutputBuffer=new Uint8Array(Math.max(r,t.encoderOutputBuffer.length*2))),e.copyTo(t.encoderOutputBuffer),Z(t,t.encoderOutputBuffer.subarray(0,r),e.timestamp??t.timestampUs,e.duration??t.frameDurationUs,e.type===`key`)},error:e=>{L(e,n.captureId)}});e.configure({codec:`opus`,numberOfChannels:n.channels,sampleRate:n.sampleRate,bitrate:a,bitrateMode:`constant`,opus:{application:`lowdelay`,signal:`voice`}}),o=e}let s=n.encoder;try{await s.flush()}catch{}try{s.close()}catch{}n.encoder=o,n.isReconfiguring=!1,N.debug(`Audio capture bitrate updated: ${a}bps`)}async function ue(){let e=Array.from(V.keys()).map(e=>$(e).catch(()=>{}));if(await Promise.all(e),I){try{I.close()}catch{}I=null}}self.addEventListener(`message`,e=>{let t=e.data;if(t.type===`attachSendPort`){if(I)try{I.close()}catch{}I=t.port;try{I.start?.()}catch{}return}if(t.type===`start`){oe(t).catch(e=>{L(e,t.captureId)});return}if(t.type===`pcm`){let e=V.get(t.captureId);if(!e||e.stopped)return;X(e,t.pcm);return}if(t.type===`stop`){$(t.captureId).catch(e=>{N.warn(`Failed to stop audio capture: ${e}`),L(e,t.captureId)});return}if(t.type===`startSegment`){se(t.captureId);return}if(t.type===`endSegment`){ce(t.captureId);return}if(t.type===`updateOptions`){le(t.captureId,t.options).catch(e=>{N.warn(`Failed to update audio capture options: ${e}`),L(e,t.captureId)});return}t.type===`dispose`&&ue().catch(e=>{N.warn(`Failed to dispose audio capture worker: ${e}`)})});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e={None:0,SendBackToSender:1,Delayable:2,SendToUser:4,Compressed:8,Unreliable:16};function t(e){return e}function n(e){if(e.length<8)throw Error(`Protocol message too short`);return(e[4]|e[5]<<8|e[6]<<16|e[7]<<24)>>>0}function r(e){let t=S(e),n=new DataView(t.buffer,t.byteOffset,t.byteLength);if(t.length<27)throw Error(`Protocol payload too short`);let r=n.getUint32(0,!0),i=n.getUint32(4,!0),a=n.getUint32(8,!0),o=n.getUint32(12,!0),s=n.getUint32(16,!0),c=n.getUint32(20,!0),l=n.getUint8(24),u=n.getUint8(25),d=n.getUint8(26);if(27+c*4>t.length)throw Error(`Protocol header exceeds payload length`);let f=[],p=27;for(let e=0;e<c;e++)f.push(n.getUint32(p,!0)),p+=4;return{length:r,opcode:i,senderId:a,trackId:o,sequenceId:s,targetIds:f,payloadVersion:l,payloadType:u,flags:d}}async function i(t,n,i){let o=S(t),s=r(o);if(n!==void 0&&s.opcode!==n)throw Error(`Unexpected opcode ${s.opcode}`);if(s.payloadType!==8)throw Error(`Unexpected payload type ${s.payloadType}`);let c=27+s.targetIds.length*4,l=o.subarray(c,s.length);return(s.flags&e.Compressed)!==0&&(l=await a(l)),l}async function a(e){if(typeof DecompressionStream>`u`)throw Error(`DecompressionStream not supported`);let t=new DecompressionStream(`gzip`),n=t.writable.getWriter(),r=new Uint8Array(e);n.write(r).then(()=>n.close()).catch(()=>void 0);let i=t.readable.getReader(),a=[],o=0;for(;;){let{done:e,value:t}=await i.read();if(e)break;a.push(t),o+=t.length}let s=new Uint8Array(o),c=0;for(let e=0;e<a.length;e++)s.set(a[e],c),c+=a[e].length;return s}let o=function(e){return e[e.Null=1]=`Null`,e[e.Bool=2]=`Bool`,e[e.Int32=3]=`Int32`,e[e.Int64=4]=`Int64`,e[e.UInt32=5]=`UInt32`,e[e.UInt64=6]=`UInt64`,e[e.Float32=7]=`Float32`,e[e.Float64=8]=`Float64`,e[e.Array=9]=`Array`,e[e.Dict=10]=`Dict`,e[e.Object=11]=`Object`,e[e.String=12]=`String`,e[e.Binary=13]=`Binary`,e[e.Guid=14]=`Guid`,e}({});new TextEncoder;const s=new TextDecoder(`utf-8`,{fatal:!0});var c=class e{buffer;version;offset;end;constructor(e,t,n){this.buffer=e,this.version=t,this.offset=n,this.end=e.length-1}static create(t){let n=S(t);if(n.length<2)throw Error(`Teleport payload too short`);if(n[0]!==161||n[n.length-1]!==162)throw Error(`Teleport object missing markers`);let r={offset:1},i=m(n,r,`InvalidLength`);return new e(n,i,r.offset)}next(){if(this.offset>=this.end)return null;if(this.offset+5>this.buffer.length)throw Error(`Teleport object truncated`);let e=re(this.buffer,this.offset);this.offset+=4;let t=this.buffer[this.offset++],n=t>>4&15;if(t&15)throw Error(`Teleport field flags must be zero`);let r=h(n),i;if(r>=0)_(this.buffer,this.offset,r),i=this.buffer.subarray(this.offset,this.offset+r),this.offset+=r;else{let e={offset:this.offset},t=m(this.buffer,e,`InvalidLength`);_(this.buffer,e.offset,t),i=this.buffer.subarray(e.offset,e.offset+t),this.offset=e.offset+t}return new u(e,n,i)}},l=class{type;payload;constructor(e,t){this.type=e,this.payload=t}asInt32(){return this.ensureType(3),new DataView(this.payload.buffer,this.payload.byteOffset,4).getInt32(0,!0)}asUInt32(){return this.ensureType(5),new DataView(this.payload.buffer,this.payload.byteOffset,4).getUint32(0,!0)}asInt64(){return this.ensureType(4),new DataView(this.payload.buffer,this.payload.byteOffset,8).getBigInt64(0,!0)}asUInt64(){return this.ensureType(6),new DataView(this.payload.buffer,this.payload.byteOffset,8).getBigUint64(0,!0)}asFloat32(){return this.ensureType(7),new DataView(this.payload.buffer,this.payload.byteOffset,4).getFloat32(0,!0)}asFloat64(){return this.ensureType(8),new DataView(this.payload.buffer,this.payload.byteOffset,8).getFloat64(0,!0)}asBool(){return this.ensureType(2),this.payload.length>0&&this.payload[0]!==0}asBinary(){return this.ensureType(13),this.payload}asUtf8(){return this.ensureType(12),this.payload}asString(){return this.ensureType(12),s.decode(this.payload)}asGuid(){return this.ensureType(14),te.fromBytes(this.payload)}asObject(){return this.ensureType(11),c.create(this.payload)}asArray(){return this.ensureType(9),d.create(this.payload)}asDictionary(){return this.ensureType(10),p.create(this.payload)}ensureType(e){if(this.type!==e)throw Error(`Teleport value has type ${o[this.type]}, expected ${o[e]}`)}},u=class extends l{fieldId;constructor(e,t,n){super(t,n),this.fieldId=e}get isNull(){return this.type===1}},d=class e{payload;elementType;count;offset;index=0;constructor(e){if(this.payload=e,e.length===0)throw Error(`Array payload too short`);let t=e[0];if(this.elementType=t>>4&15,t&15)throw Error(`Array flags must be zero`);let n={offset:1};this.count=m(e,n,`ArrayMalformed`),this.offset=n.offset}static create(t){return new e(S(t))}next(){if(this.index>=this.count){if(this.offset!==this.payload.length)throw Error(`Array payload contains trailing data`);return null}let e=this.readValue();return this.index++,e}readValue(){switch(this.elementType){case 3:case 5:case 7:case 8:case 2:case 4:case 6:case 14:{let e=h(this.elementType);_(this.payload,this.offset,e);let t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new f(this.elementType,t)}case 12:case 13:{let e={offset:this.offset},t=m(this.payload,e,`ArrayMalformed`);_(this.payload,e.offset,t);let n=this.payload.subarray(e.offset,e.offset+t);return this.offset=e.offset+t,new f(this.elementType,n)}case 11:{let e={offset:this.offset},t=m(this.payload,e,`ArrayMalformed`);_(this.payload,e.offset,t);let n=this.payload.subarray(e.offset,e.offset+t);return this.offset=e.offset+t,new f(11,n)}case 9:{let e=v(this.payload,this.offset),t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new f(9,t)}case 10:{let e=y(this.payload,this.offset),t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new f(10,t)}default:throw Error(`Unsupported array element type ${o[this.elementType]}`)}}},f=class extends l{constructor(e,t){super(e,t)}},p=class e{payload;keyType;valueType;count;offset;index=0;constructor(e){if(this.payload=e,e.length<2)throw Error(`Dictionary payload too short`);if(this.keyType=e[0]>>4&15,this.valueType=e[1]>>4&15,e[0]&15||e[1]&15)throw Error(`Dictionary key/value flags must be zero`);g(this.keyType);let t={offset:2};this.count=m(e,t,`DictMalformed`),this.offset=t.offset}static create(t){return new e(S(t))}next(){if(this.index>=this.count){if(this.offset!==this.payload.length)throw Error(`Dictionary payload contains trailing data`);return null}let e=this.readKey(),t=this.readValue();return this.index++,new ee(e,t)}readKey(){let e=h(this.keyType);if(e>=0){_(this.payload,this.offset,e);let t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new l(this.keyType,t)}if(this.keyType===12||this.keyType===13){let e={offset:this.offset},t=m(this.payload,e,`DictMalformed`);_(this.payload,e.offset,t);let n=this.payload.subarray(e.offset,e.offset+t);return this.offset=e.offset+t,new l(this.keyType,n)}throw Error(`Unsupported dictionary key type`)}readValue(){switch(this.valueType){case 12:case 13:{let e={offset:this.offset},t=m(this.payload,e,`DictMalformed`);_(this.payload,e.offset,t);let n=this.payload.subarray(e.offset,e.offset+t);return this.offset=e.offset+t,new l(this.valueType,n)}case 11:{let e={offset:this.offset},t=m(this.payload,e,`DictMalformed`);_(this.payload,e.offset,t);let n=this.payload.subarray(e.offset,e.offset+t);return this.offset=e.offset+t,new l(11,n)}case 9:{let e=v(this.payload,this.offset),t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new l(9,t)}case 10:{let e=y(this.payload,this.offset),t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new l(10,t)}case 3:case 5:case 7:case 8:case 2:case 4:case 6:case 14:case 1:{let e=h(this.valueType);_(this.payload,this.offset,e);let t=this.payload.subarray(this.offset,this.offset+e);return this.offset+=e,new l(this.valueType,t)}default:throw Error(`Unsupported dictionary value type ${o[this.valueType]}`)}}},ee=class{key;value;constructor(e,t){this.key=e,this.value=t}},te=class e{bytes;constructor(e){this.bytes=e}static fromString(t){if(!t)throw Error(`Guid string is empty`);let n=t.replace(/-/g,``);if(n.length!==32)throw Error(`Guid string must be 32 hex characters`);let r=new Uint8Array(16),i=e.parseHexSlice(n,0,8),a=e.parseHexSlice(n,8,4),o=e.parseHexSlice(n,12,4);e.writeUInt32LE(r,0,i),e.writeUInt16LE(r,4,a),e.writeUInt16LE(r,6,o);for(let t=0;t<8;t++)r[8+t]=e.parseHexSlice(n,16+t*2,2);return new e(r)}static fromBytes(t){if(t.length!==16)throw Error(`Guid byte array must be 16 bytes`);return new e(Uint8Array.from(t))}static createZero(){return new e(new Uint8Array(16))}static createRandom(){let t=new Uint8Array(16),n=globalThis.crypto;if(n?.getRandomValues)n.getRandomValues(t);else for(let e=0;e<t.length;e++)t[e]=Math.floor(Math.random()*256);return t[6]=t[6]&15|64,t[8]=t[8]&63|128,new e(t)}toString(){let t=this.bytes;return[e.toHex(e.readUInt32LE(t,0),8),e.toHex(e.readUInt16LE(t,4),4),e.toHex(e.readUInt16LE(t,6),4),x(t.subarray(8,10)),x(t.subarray(10,16))].join(`-`)}asBytes(){return this.bytes.slice()}static parseHexSlice(e,t,n){let r=e.substr(t,n),i=Number.parseInt(r,16);if(Number.isNaN(i))throw Error(`Guid string contains invalid characters`);return i>>>0}static writeUInt32LE(e,t,n){let r=n>>>0;e[t]=r&255,e[t+1]=r>>>8&255,e[t+2]=r>>>16&255,e[t+3]=r>>>24&255}static writeUInt16LE(e,t,n){let r=n&65535;e[t]=r&255,e[t+1]=r>>>8&255}static readUInt32LE(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0}static readUInt16LE(e,t){return(e[t]|e[t+1]<<8)&65535}static toHex(e,t){return(e>>>0).toString(16).padStart(t,`0`)}};function m(e,t,n){if(t.offset>=e.length)throw Error(n);let r=0,i=0,a=0;for(;t.offset<e.length;){let n=e[t.offset++];if(a++,r|=(n&127)<<i,!(n&128)){if(a!==ne(r))throw Error(`Teleport VarUInt is not canonical`);return r>>>0}if(i+=7,i>=35)throw Error(`Teleport VarUInt exceeds 32-bit range`)}throw Error(n)}function ne(e){return e<128?1:e<16384?2:e<2097152?3:e<268435456?4:5}function h(e){switch(e){case 3:case 5:case 7:return 4;case 4:case 6:case 8:return 8;case 14:return 16;case 2:return 1;case 1:return 0;default:return-1}}function g(e){if(e===9||e===11||e===10||e===1)throw Error(`Dictionary keys must be primitive Teleport types`)}function _(e,t,n){if(t<0||n<0||t+n>e.length)throw Error(`Teleport payload exceeds bounds`)}function re(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16|e[t+3]<<24)>>>0}function v(e,t){if(t>=e.length)throw Error(`Array payload exceeds bounds`);let n=e[t],r=n>>4&15;if(n&15)throw Error(`Array flags must be zero`);let i={offset:t+1},a=m(e,i,`ArrayMalformed`),o=h(r);if(o>=0){let n=o*a;return _(e,i.offset,n),i.offset+n-t}let s=i.offset;for(let t=0;t<a;t++)s=b(r,e,s,`ArrayMalformed`);return s-t}function y(e,t){if(t+2>e.length)throw Error(`Dictionary payload too short`);let n=e[t]>>4&15,r=e[t+1]>>4&15;if(e[t]&15||e[t+1]&15)throw Error(`Dictionary key/value flags must be zero`);g(n);let i={offset:t+2},a=m(e,i,`DictMalformed`),o=i.offset;for(let t=0;t<a;t++)o=b(n,e,o,`DictMalformed`),o=b(r,e,o,`DictMalformed`);return o-t}function b(e,t,n,r){let i=h(e);if(i>=0)return _(t,n,i),n+i;switch(e){case 12:case 13:{let e={offset:n},i=m(t,e,r);return _(t,e.offset,i),e.offset+i}case 11:{let e={offset:n},i=m(t,e,r);return _(t,e.offset,i),e.offset+i}case 9:return n+v(t,n);case 10:return n+y(t,n);default:throw Error(`Unsupported Teleport type ${o[e]}`)}}function x(e){return Array.from(e).map(e=>e.toString(16).padStart(2,`0`)).join(``)}function S(e){return e instanceof Uint8Array?e:new Uint8Array(e)}let C=function(e){return e[e.Unknown=0]=`Unknown`,e[e.Opus=1]=`Opus`,e[e.Mp3=2]=`Mp3`,e[e.RawPcm16=3]=`RawPcm16`,e}({}),w=function(e){return e[e.NONE=0]=`NONE`,e[e.CONSTANT_GROUP_BITS=15]=`CONSTANT_GROUP_BITS`,e[e.CONSTANT_GROUP_OFFSET=16]=`CONSTANT_GROUP_OFFSET`,e[e.GROUP_CORE=65536]=`GROUP_CORE`,e[e.CORE_AUTH_RESPONSE=65537]=`CORE_AUTH_RESPONSE`,e[e.CORE_AUTH_TICKET=65538]=`CORE_AUTH_TICKET`,e[e.CORE_GLOBAL_STATE=65539]=`CORE_GLOBAL_STATE`,e[e.CORE_ON_SERVER_STATUS_PING=65540]=`CORE_ON_SERVER_STATUS_PING`,e[e.CORE_ON_USER_JOINED=65541]=`CORE_ON_USER_JOINED`,e[e.CORE_ON_USER_LEFT=65542]=`CORE_ON_USER_LEFT`,e[e.CORE_ON_CLIENT_JOINED=65543]=`CORE_ON_CLIENT_JOINED`,e[e.CORE_ON_CLIENT_LEFT=65544]=`CORE_ON_CLIENT_LEFT`,e[e.CORE_ON_SERVER_STARTED=65545]=`CORE_ON_SERVER_STARTED`,e[e.CORE_ON_SERVER_STOPPED=65546]=`CORE_ON_SERVER_STOPPED`,e[e.CORE_ON_SERVER_STOPPING=65547]=`CORE_ON_SERVER_STOPPING`,e[e.CORE_ON_CLIENT_READY=65548]=`CORE_ON_CLIENT_READY`,e[e.CORE_CLIENT_READY=65549]=`CORE_CLIENT_READY`,e[e.CORE_SERVER_INIT=65550]=`CORE_SERVER_INIT`,e[e.CORE_ON_PLUGIN_RELOADED=65551]=`CORE_ON_PLUGIN_RELOADED`,e[e.CORE_DYNAMIC_CONFIG=65555]=`CORE_DYNAMIC_CONFIG`,e[e.CORE_PROXY_RPC_AUTH_TICKET=65556]=`CORE_PROXY_RPC_AUTH_TICKET`,e[e.CORE_UPDATE_CLIENT_CONTEXT=65558]=`CORE_UPDATE_CLIENT_CONTEXT`,e[e.CORE_BACKGROUND_WORK_ACTIVE=65559]=`CORE_BACKGROUND_WORK_ACTIVE`,e[e.CORE_RESET_IDLE=65560]=`CORE_RESET_IDLE`,e[e.CORE_CLIENT_DISCONNECTING=65561]=`CORE_CLIENT_DISCONNECTING`,e[e.CORE_ON_APP_READY=65562]=`CORE_ON_APP_READY`,e[e.CORE_ON_FRONTEND_RELOADED=65563]=`CORE_ON_FRONTEND_RELOADED`,e[e.CORE_ON_USER_DATA_ERASED=65564]=`CORE_ON_USER_DATA_ERASED`,e[e.CORE_WEBRTC_OFFER=65566]=`CORE_WEBRTC_OFFER`,e[e.CORE_WEBRTC_ANSWER=65567]=`CORE_WEBRTC_ANSWER`,e[e.CORE_WEBRTC_ICE_CANDIDATE=65568]=`CORE_WEBRTC_ICE_CANDIDATE`,e[e.CORE_WEBRTC_READY=65569]=`CORE_WEBRTC_READY`,e[e.CORE_WEBRTC_AUDIO_SEGMENT=65570]=`CORE_WEBRTC_AUDIO_SEGMENT`,e[e.CORE_WEBRTC_TRACK_MAP=65571]=`CORE_WEBRTC_TRACK_MAP`,e[e.CORE_WEBRTC_VIDEO_CAPTURE=65572]=`CORE_WEBRTC_VIDEO_CAPTURE`,e[e.CORE_WEBRTC_ICE_SERVERS_REQUEST=65573]=`CORE_WEBRTC_ICE_SERVERS_REQUEST`,e[e.CORE_WEBRTC_ICE_SERVERS_RESPONSE=65574]=`CORE_WEBRTC_ICE_SERVERS_RESPONSE`,e[e.CORE_WEBRTC_CLOSE=65575]=`CORE_WEBRTC_CLOSE`,e[e.CORE_RELAY_AGENT_AUTH=65576]=`CORE_RELAY_AGENT_AUTH`,e[e.CORE_RELAY_AGENT_AUTH_RESULT=65577]=`CORE_RELAY_AGENT_AUTH_RESULT`,e[e.CORE_RELAY_HEARTBEAT=65578]=`CORE_RELAY_HEARTBEAT`,e[e.CORE_RELAY_TCP_CONNECTION_OPENED=65579]=`CORE_RELAY_TCP_CONNECTION_OPENED`,e[e.CORE_RELAY_TCP_CONNECTION_CLOSED=65580]=`CORE_RELAY_TCP_CONNECTION_CLOSED`,e[e.CORE_RELAY_TCP_DATA=65581]=`CORE_RELAY_TCP_DATA`,e[e.CORE_RELAY_UDP_DATA=65582]=`CORE_RELAY_UDP_DATA`,e[e.CORE_RELAY_ADD_TUNNEL=65583]=`CORE_RELAY_ADD_TUNNEL`,e[e.CORE_RELAY_TUNNEL_ADDED=65584]=`CORE_RELAY_TUNNEL_ADDED`,e[e.CORE_RELAY_REMOVE_TUNNEL=65585]=`CORE_RELAY_REMOVE_TUNNEL`,e[e.CORE_IKON_SERVER_ENDPOINT_HOST_INFO=65586]=`CORE_IKON_SERVER_ENDPOINT_HOST_INFO`,e[e.CORE_CLIENT_INITIALIZATION=65587]=`CORE_CLIENT_INITIALIZATION`,e[e.CORE_CLIENT_LIFECYCLE_BATCH=65588]=`CORE_CLIENT_LIFECYCLE_BATCH`,e[e.CORE_APP_CONFIG=65589]=`CORE_APP_CONFIG`,e[e.GROUP_KEEPALIVE=131072]=`GROUP_KEEPALIVE`,e[e.KEEPALIVE_REQUEST=131073]=`KEEPALIVE_REQUEST`,e[e.KEEPALIVE_RESPONSE=131074]=`KEEPALIVE_RESPONSE`,e[e.GROUP_EVENTS=262144]=`GROUP_EVENTS`,e[e.EVENTS_PROFILE_UPDATE=262145]=`EVENTS_PROFILE_UPDATE`,e[e.EVENTS_SPEECH_PLAYBACK_COMPLETE=262147]=`EVENTS_SPEECH_PLAYBACK_COMPLETE`,e[e.GROUP_ANALYTICS=524288]=`GROUP_ANALYTICS`,e[e.ANALYTICS_LOGS=524289]=`ANALYTICS_LOGS`,e[e.ANALYTICS_EVENTS=524290]=`ANALYTICS_EVENTS`,e[e.ANALYTICS_USAGES=524291]=`ANALYTICS_USAGES`,e[e.ANALYTICS_USAGE=524292]=`ANALYTICS_USAGE`,e[e.ANALYTICS_SPECIAL_LOG=524293]=`ANALYTICS_SPECIAL_LOG`,e[e.ANALYTICS_PROCESSING_UPDATE=524294]=`ANALYTICS_PROCESSING_UPDATE`,e[e.ANALYTICS_REACTIVE_PROCESSING_UPDATE=524295]=`ANALYTICS_REACTIVE_PROCESSING_UPDATE`,e[e.ANALYTICS_IKON_PROXY_SERVER_STATS=524296]=`ANALYTICS_IKON_PROXY_SERVER_STATS`,e[e.ANALYTICS_IKON_RELAY_SERVER_STATS=524297]=`ANALYTICS_IKON_RELAY_SERVER_STATS`,e[e.ANALYTICS_IKON_TURN_SERVER_STATS=524298]=`ANALYTICS_IKON_TURN_SERVER_STATS`,e[e.ANALYTICS_IKON_HOST_SERVER_STATS=524299]=`ANALYTICS_IKON_HOST_SERVER_STATS`,e[e.GROUP_ACTIONS=1048576]=`GROUP_ACTIONS`,e[e.ACTION_CALL=1048577]=`ACTION_CALL`,e[e.ACTION_ACTIVE=1048578]=`ACTION_ACTIVE`,e[e.ACTION_TEXT_OUTPUT=1048579]=`ACTION_TEXT_OUTPUT`,e[e.ACTION_TEXT_OUTPUT_DELTA=1048580]=`ACTION_TEXT_OUTPUT_DELTA`,e[e.ACTION_TEXT_OUTPUT_DELTA_FULL=1048581]=`ACTION_TEXT_OUTPUT_DELTA_FULL`,e[e.ACTION_SET_STATE=1048582]=`ACTION_SET_STATE`,e[e.ACTION_TAP=1048583]=`ACTION_TAP`,e[e.ACTION_PAN=1048584]=`ACTION_PAN`,e[e.ACTION_ZOOM=1048585]=`ACTION_ZOOM`,e[e.ACTION_FILE_UPLOAD_BEGIN=1048586]=`ACTION_FILE_UPLOAD_BEGIN`,e[e.ACTION_FILE_UPLOAD_DATA=1048587]=`ACTION_FILE_UPLOAD_DATA`,e[e.ACTION_FILE_UPLOAD_ACK=1048588]=`ACTION_FILE_UPLOAD_ACK`,e[e.ACTION_FILE_UPLOAD_END=1048589]=`ACTION_FILE_UPLOAD_END`,e[e.ACTION_FILE_UPLOAD_RESULT=1048590]=`ACTION_FILE_UPLOAD_RESULT`,e[e.ACTION_OPEN_EXTERNAL_URL=1048592]=`ACTION_OPEN_EXTERNAL_URL`,e[e.ACTION_FUNCTION_REGISTER=1048600]=`ACTION_FUNCTION_REGISTER`,e[e.ACTION_FUNCTION_CALL=1048601]=`ACTION_FUNCTION_CALL`,e[e.ACTION_FUNCTION_RESULT=1048602]=`ACTION_FUNCTION_RESULT`,e[e.ACTION_GENERATE_ANSWER=1048603]=`ACTION_GENERATE_ANSWER`,e[e.ACTION_REGENERATE_ANSWER=1048604]=`ACTION_REGENERATE_ANSWER`,e[e.ACTION_CLEAR_STATE=1048606]=`ACTION_CLEAR_STATE`,e[e.ACTION_CLASSIFICATION_RESULT=1048609]=`ACTION_CLASSIFICATION_RESULT`,e[e.ACTION_AUDIO_STOP=1048610]=`ACTION_AUDIO_STOP`,e[e.ACTION_CALL_TEXT=1048611]=`ACTION_CALL_TEXT`,e[e.ACTION_CANCEL_GENERATION=1048613]=`ACTION_CANCEL_GENERATION`,e[e.ACTION_SPEECH_RECOGNIZED=1048615]=`ACTION_SPEECH_RECOGNIZED`,e[e.ACTION_CALL_RESULT=1048616]=`ACTION_CALL_RESULT`,e[e.ACTION_DOWNLOAD=1048618]=`ACTION_DOWNLOAD`,e[e.ACTION_PLAY_SOUND=1048621]=`ACTION_PLAY_SOUND`,e[e.ACTION_STOP_SOUND=1048623]=`ACTION_STOP_SOUND`,e[e.ACTION_START_RECORDING=1048624]=`ACTION_START_RECORDING`,e[e.ACTION_STOP_RECORDING=1048625]=`ACTION_STOP_RECORDING`,e[e.ACTION_FUNCTION_ENUMERATION_ITEM=1048627]=`ACTION_FUNCTION_ENUMERATION_ITEM`,e[e.ACTION_FUNCTION_ENUMERATION_END=1048628]=`ACTION_FUNCTION_ENUMERATION_END`,e[e.ACTION_FUNCTION_CANCEL=1048629]=`ACTION_FUNCTION_CANCEL`,e[e.ACTION_FUNCTION_DISPOSE=1048630]=`ACTION_FUNCTION_DISPOSE`,e[e.ACTION_FUNCTION_ERROR=1048631]=`ACTION_FUNCTION_ERROR`,e[e.ACTION_FUNCTION_ACK=1048632]=`ACTION_FUNCTION_ACK`,e[e.ACTION_FUNCTION_AWAITING_APPROVAL=1048633]=`ACTION_FUNCTION_AWAITING_APPROVAL`,e[e.ACTION_FUNCTION_APPROVAL_REQUIRED=1048634]=`ACTION_FUNCTION_APPROVAL_REQUIRED`,e[e.ACTION_FUNCTION_APPROVAL_RESPONSE=1048635]=`ACTION_FUNCTION_APPROVAL_RESPONSE`,e[e.UI_UPDATE_ACK=1048636]=`UI_UPDATE_ACK`,e[e.ACTION_CALL2=1048637]=`ACTION_CALL2`,e[e.ACTION_FUNCTION_REGISTER_BATCH=1048638]=`ACTION_FUNCTION_REGISTER_BATCH`,e[e.ACTION_FILE_UPLOAD_CALLBACK=1048640]=`ACTION_FILE_UPLOAD_CALLBACK`,e[e.ACTION_CUSTOM_USER_MESSAGE=1048641]=`ACTION_CUSTOM_USER_MESSAGE`,e[e.ACTION_URL_CHANGED=1048642]=`ACTION_URL_CHANGED`,e[e.ACTION_FILE_UPLOAD_PRE_START2=1048643]=`ACTION_FILE_UPLOAD_PRE_START2`,e[e.ACTION_FILE_UPLOAD_PRE_START_RESPONSE2=1048644]=`ACTION_FILE_UPLOAD_PRE_START_RESPONSE2`,e[e.ACTION_FILE_UPLOAD_START2=1048645]=`ACTION_FILE_UPLOAD_START2`,e[e.ACTION_FILE_UPLOAD_START_RESPONSE2=1048646]=`ACTION_FILE_UPLOAD_START_RESPONSE2`,e[e.ACTION_FILE_UPLOAD_DATA2=1048647]=`ACTION_FILE_UPLOAD_DATA2`,e[e.ACTION_FILE_UPLOAD_ACK2=1048648]=`ACTION_FILE_UPLOAD_ACK2`,e[e.ACTION_FILE_UPLOAD_END2=1048649]=`ACTION_FILE_UPLOAD_END2`,e[e.ACTION_FILE_UPLOAD_COMPLETE2=1048650]=`ACTION_FILE_UPLOAD_COMPLETE2`,e[e.ACTION_FUNCTION_ENUMERATION_ITEM_BATCH=1048651]=`ACTION_FUNCTION_ENUMERATION_ITEM_BATCH`,e[e.ACTION_CALL_ACK=1048652]=`ACTION_CALL_ACK`,e[e.ACTION_TRIGGER_CRON=1048653]=`ACTION_TRIGGER_CRON`,e[e.ACTION_RESULT=1048654]=`ACTION_RESULT`,e[e.UI_RESYNC_REQUEST=1048655]=`UI_RESYNC_REQUEST`,e[e.ACTION_USER_DATA_ERASURE=1048656]=`ACTION_USER_DATA_ERASURE`,e[e.GROUP_UI=2097152]=`GROUP_UI`,e[e.UI_STREAM_BEGIN=2097153]=`UI_STREAM_BEGIN`,e[e.UI_STREAM_END=2097154]=`UI_STREAM_END`,e[e.UI_STYLES=2097188]=`UI_STYLES`,e[e.UI_UPDATE=2097190]=`UI_UPDATE`,e[e.UI_STYLES_BATCH=2097192]=`UI_STYLES_BATCH`,e[e.UI_STYLES_DELETE=2097193]=`UI_STYLES_DELETE`,e[e.GROUP_COMMON=4128768]=`GROUP_COMMON`,e[e.GROUP_AUDIO=4194304]=`GROUP_AUDIO`,e[e.AUDIO_STREAM_BEGIN=4194305]=`AUDIO_STREAM_BEGIN`,e[e.AUDIO_STREAM_END=4194306]=`AUDIO_STREAM_END`,e[e.AUDIO_FRAME=4194307]=`AUDIO_FRAME`,e[e.AUDIO_FRAME_VOLUME=4194308]=`AUDIO_FRAME_VOLUME`,e[e.AUDIO_FRAME2=4194309]=`AUDIO_FRAME2`,e[e.AUDIO_SHAPE_FRAME=4194310]=`AUDIO_SHAPE_FRAME`,e[e.AUDIO_PLAYBACK_REPORT=4194311]=`AUDIO_PLAYBACK_REPORT`,e[e.GROUP_VIDEO=8388608]=`GROUP_VIDEO`,e[e.VIDEO_STREAM_BEGIN=8388609]=`VIDEO_STREAM_BEGIN`,e[e.VIDEO_STREAM_END=8388610]=`VIDEO_STREAM_END`,e[e.VIDEO_FRAME=8388611]=`VIDEO_FRAME`,e[e.VIDEO_REQUEST_IDR_FRAME=8388612]=`VIDEO_REQUEST_IDR_FRAME`,e[e.VIDEO_INVALIDATE_FRAME=8388613]=`VIDEO_INVALIDATE_FRAME`,e[e.GROUP_ALL=16711680]=`GROUP_ALL`,e[e.GROUP_APP_LOCAL=1073741824]=`GROUP_APP_LOCAL`,e[e.CONSTANT_GROUP_MASK=2147418112]=`CONSTANT_GROUP_MASK`,e}({});function ie(e){let t={};return T(t),e&&Object.assign(t,e),t}function T(e){return e.Samples=new Uint8Array,e.Epoch=0,e.Sequence=0,e.FrameSizeInInterleavedSamples=0,e.TimeStampInInterleavedSamples=BigInt(0),e.IsFirst=!1,e.IsLast=!1,e.AverageVolume=0,e.AudioEventEstimatedDuration=0,e}function ae(e,t){return oe(c.create(e),t)}function oe(e,t){let n=t??ie();return T(n),se(e,n),n}function se(e,t){let n;for(;(n=e.next())!==null;)switch(n.fieldId){case 205938238:if(n.isNull)throw Error();t.AverageVolume=n.asFloat32();break;case 1594075008:if(n.isNull)throw Error();t.IsFirst=n.asBool();break;case 2026534360:if(n.isNull)throw Error();t.TimeStampInInterleavedSamples=n.asUInt64();break;case 3409001585:if(n.isNull)throw Error();t.Samples=n.asBinary();break;case 3466650495:if(n.isNull)throw Error();t.AudioEventEstimatedDuration=n.asFloat32();break;case 4007456593:if(n.isNull)throw Error();t.FrameSizeInInterleavedSamples=n.asUInt32();break;case 4017217601:if(n.isNull)throw Error();t.IsLast=n.asBool();break;case 4065417427:if(n.isNull)throw Error();t.Sequence=n.asUInt32();break;case 4086360332:if(n.isNull)throw Error();t.Epoch=n.asUInt32();break;default:break}}async function ce(e,t){return ae(await i(e,4194309,1),t)}function le(e){let t={};return E(t),e&&Object.assign(t,e),t}function E(e){return e.StreamId=``,e.Description=``,e.SourceType=``,e.Codec=C.Unknown,e.CodecDetails=``,e.SampleRate=0,e.Channels=0,e.ShapeSets=void 0,e.CorrelationId=void 0,e}function ue(e,t){return de(c.create(e),t)}function de(e,t){let n=t??le();return E(n),fe(e,n),n}function fe(e,t){let n;for(;(n=e.next())!==null;)switch(n.fieldId){case 161083277:if(n.isNull)throw Error();t.StreamId=n.asString();break;case 265814330:if(n.isNull){t.ShapeSets=void 0;break}t.ShapeSets=me(n.asArray());break;case 1368629611:if(n.isNull)throw Error();t.Description=n.asString();break;case 2431514951:if(n.isNull)throw Error();t.Channels=n.asInt32();break;case 2514959030:if(n.isNull){t.CorrelationId=void 0;break}t.CorrelationId=n.asString();break;case 2745379226:if(n.isNull)throw Error();t.CodecDetails=n.asString();break;case 3282782683:if(n.isNull)throw Error();t.SourceType=n.asString();break;case 3284746250:if(n.isNull)throw Error();t.Codec=n.asInt32();break;case 4101844078:if(n.isNull)throw Error();t.SampleRate=n.asInt32();break;default:break}}async function pe(e,t){return ue(await i(e,4194305,6),t)}function me(e){let t=[],n;for(;(n=e.next())!==null;)t.push(ge(n.asObject()));return t}function he(e){let t={};return D(t),e&&Object.assign(t,e),t}function D(e){return e.SetId=0,e.Name=``,e.ShapeNames=[],e}function ge(e,t){let n=t??he();return D(n),_e(e,n),n}function _e(e,t){let n;for(;(n=e.next())!==null;)switch(n.fieldId){case 1107713536:if(n.isNull)throw Error();t.Name=n.asString();break;case 1154362099:if(n.isNull)throw Error();t.SetId=n.asUInt32();break;case 1185721362:if(n.isNull)throw Error();t.ShapeNames=ve(n.asArray());break;default:break}}function ve(e){let t=[],n;for(;(n=e.next())!==null;)t.push(n.asString());return t}var O=class{static getStorageForCapacity(e,t){let n=Int32Array.BYTES_PER_ELEMENT*2,r=t.BYTES_PER_ELEMENT*e;return new SharedArrayBuffer(n+r)}readWrite;storage;buf;constructor(e,t){this.buf=e,this.readWrite=new Int32Array(e,0,2);let n=Int32Array.BYTES_PER_ELEMENT*2,r=(e.byteLength-n)/Float32Array.BYTES_PER_ELEMENT;this.storage=new t(e,n,r)}get capacity(){return this.storage.length}flush(){Atomics.store(this.readWrite,0,0),Atomics.store(this.readWrite,1,0)}availableRead(){let e=Atomics.load(this.readWrite,0),t=Atomics.load(this.readWrite,1);return t>=e?t-e:this.capacity-(e-t)}availableWrite(){return this.capacity-this.availableRead()-1}push(e){let t=Math.min(this.availableWrite(),e.length);if(t<=0)return 0;let n=Atomics.load(this.readWrite,1),r=Math.min(t,this.capacity-n);this.storage.set(e.subarray(0,r),n);let i=t-r;return i>0&&this.storage.set(e.subarray(r,r+i),0),n=(n+t)%this.capacity,Atomics.store(this.readWrite,1,n),Atomics.notify(this.readWrite,1),t}pop(e){let t=Math.min(this.availableRead(),e.length);if(t<=0)return 0;let n=Atomics.load(this.readWrite,0),r=Math.min(t,this.capacity-n);e.set(this.storage.subarray(n,n+r),0);let i=t-r;return i>0&&e.set(this.storage.subarray(0,i),r),n=(n+t)%this.capacity,Atomics.store(this.readWrite,0,n),t}skip(e){let t=Math.min(this.availableRead(),e);if(t<=0)return 0;let n=Atomics.load(this.readWrite,0);return n=(n+t)%this.capacity,Atomics.store(this.readWrite,0,n),t}};const ye=new Map,be={0:`DEBUG`,1:`INFO`,2:`WARN`,3:`ERROR`,4:`NONE`};function xe(e,t){return`[${e}] ${t}`}function k(e,t,n,r){let i={timestamp:new Date().toISOString(),level:e,levelName:be[e],component:t,message:n,args:r};for(let[t,n]of ye)if(e>=(n?.minLevel??2))try{t(i)}catch{}if(1<=e){let i=xe(t,n);switch(e){case 0:console.debug(i,...r);break;case 1:console.info(i,...r);break;case 2:console.warn(i,...r);break;case 3:console.error(i,...r);break;case 4:break}}}function A(e){return{debug(t,...n){k(0,e,t,n)},info(t,...n){k(1,e,t,n)},warn(t,...n){k(2,e,t,n)},error(t,...n){k(3,e,t,n)}}}const j=A(`Capabilities`),M=globalThis;function Se(){if(typeof SharedArrayBuffer>`u`)return j.debug(`SharedArrayBuffer not supported: SharedArrayBuffer is undefined`),!1;if(typeof Atomics>`u`)return j.debug(`SharedArrayBuffer not supported: Atomics is undefined`),!1;if(M.crossOriginIsolated===!1)return j.debug(`SharedArrayBuffer not supported: crossOriginIsolated is false (missing COOP/COEP headers)`),!1;try{new SharedArrayBuffer(1)}catch(e){return j.debug(`SharedArrayBuffer not supported: construction failed - ${e instanceof Error?e.message:String(e)}`),!1}return!0}const N=48e3,P=A(`AudioPlaybackWorker`),F=(e,t)=>{self.postMessage(e,t??[])},Ce=Se();let I=`pcm`,L=!0,R=!1,z=N,B=2,V=null;function H(e,t){let n=e instanceof Error?e:Error(String(e));F({type:`error`,error:{name:n.name,message:n.message,stack:n.stack},streamId:t})}function U(e){let t=r(e);return`${t.senderId}_${t.trackId}`}var we=class{mode;ring=null;sab=void 0;streamId;outputSampleRate;outputChannels;closed=!1;pcmBatchBuffer=null;pcmBatchOffset=0;lastPcmFlushTimeMs=0;constructor(e){this.streamId=e,this.mode=`pcm`,this.outputSampleRate=z,this.outputChannels=B,this.rebind(I)}close(){this.closed=!0,this.pcmBatchBuffer=null,this.pcmBatchOffset=0,this.ring=null,this.sab=void 0}setOutputFormat(e,t){this.outputSampleRate=e,this.outputChannels=t}rebind(e){if(!this.closed){if(this.mode=e,e===`sab`&&Ce){let e=Math.floor(1500*this.outputSampleRate*this.outputChannels/1e3),t=O.getStorageForCapacity(e,Float32Array),n=new O(t,Float32Array);n.flush(),this.sab=t,this.ring=n;return}this.sab=void 0,this.ring=null}}flushPendingPcm(e){if(this.closed||this.mode!==`pcm`||this.pcmBatchOffset<=0)return;let t=performance.now(),n=Math.max(1,Math.round(this.outputSampleRate*this.outputChannels*80/1e3));if(!(e||this.pcmBatchOffset>=n||this.lastPcmFlushTimeMs>0&&t-this.lastPcmFlushTimeMs>=50))return;let r=new Float32Array(this.pcmBatchOffset);r.set(this.pcmBatchBuffer.subarray(0,this.pcmBatchOffset)),this.pcmBatchOffset=0,this.lastPcmFlushTimeMs=t,F({type:`pcm`,streamId:this.streamId,pcm:r},[r.buffer])}drainPcm(){this.flushPendingPcm(!0)}writePcm(e){if(this.closed)return;if(this.mode===`sab`&&this.ring){let t=this.ring,n=this.outputChannels,r=e,i=Math.floor(Math.max(0,t.capacity-1)/n)*n;r.length>i&&(r=r.subarray(r.length-i));let a=Math.floor(t.availableWrite()/n)*n;if(a<=0)return;r.length>a&&(r=r.subarray(r.length-a)),t.push(r);return}let t=Math.max(1,Math.round(this.outputSampleRate*this.outputChannels*80/1e3)),n=this.pcmBatchOffset+e.length;if(!this.pcmBatchBuffer||this.pcmBatchBuffer.length<n){let e=Math.max(n,t*2),r=new Float32Array(e);this.pcmBatchBuffer&&this.pcmBatchOffset>0&&r.set(this.pcmBatchBuffer.subarray(0,this.pcmBatchOffset)),this.pcmBatchBuffer=r}this.pcmBatchBuffer.set(e,this.pcmBatchOffset),this.pcmBatchOffset+=e.length,this.lastPcmFlushTimeMs===0&&(this.lastPcmFlushTimeMs=performance.now()),this.flushPendingPcm(!1)}getSharedArrayBuffer(){return this.sab}flush(){this.closed||(this.pcmBatchOffset=0,this.lastPcmFlushTimeMs=0,this.ring?.flush())}};function Te(e){switch(e){case C.Opus:return`opus`;case C.Mp3:return`mp3`;default:return null}}let W=null;function Ee(e){let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=Math.floor(e.byteLength/2);(!W||W.length<n)&&(W=new Float32Array(n));for(let e=0;e<n;e++)W[e]=t.getInt16(e*2,!0)/32768;return W.subarray(0,n)}var G=class{inputSampleRate;inputChannels;sink;_resampleBuffer=null;_remixBuffer=null;_interleavedBuffer=null;constructor(e){this.inputSampleRate=e.inputSampleRate,this.inputChannels=e.inputChannels,this.sink=e.sink}getResampleBuffer(e){return(!this._resampleBuffer||this._resampleBuffer.length<e)&&(this._resampleBuffer=new Float32Array(e)),this._resampleBuffer}getRemixBuffer(e){return(!this._remixBuffer||this._remixBuffer.length<e)&&(this._remixBuffer=new Float32Array(e)),this._remixBuffer}getInterleavedBuffer(e){return(!this._interleavedBuffer||this._interleavedBuffer.length<e)&&(this._interleavedBuffer=new Float32Array(e)),this._interleavedBuffer}reset(){}writeConvertedPcm(e,t,n){let r=this.convertPcmBuffer(e,t,n);r.length>0&&this.sink.writePcm(r)}convertAudioData(e){let t=e.numberOfFrames,n=e.numberOfChannels,r=e.format,i=typeof r==`string`&&r.includes(`planar`),a=()=>{let r=Array(n);for(let i=0;i<n;i++){let n=new Float32Array(t);e.copyTo(n,{planeIndex:i,format:`f32-planar`}),r[i]=n}return Le(r,t)},o=()=>{let r=new Float32Array(t*n);return e.copyTo(r,{planeIndex:0,format:`f32`}),r},s;if(i)try{s=a()}catch(e){P.debug(`Failed to read planar audio data, falling back to interleaved copy: ${e}`),s=o()}else s=o();return s}convertPcmBuffer(e,t,n){if(e.length===0)return e;let r=e,i=e.length,a=t;if(n!==z){let e=i/a,t=Math.max(1,Math.round(e*z/n))*a,o=this.getResampleBuffer(t);i=ze(r,o,a,n,z),r=o}if(a!==B){let e=i/a*B,t=this.getRemixBuffer(e);i=Be(r.subarray(0,i),t,a,B),r=t}return r.subarray(0,i)}},De=class extends G{feedChunk(e){e.isFirst&&this.reset();let t=Ee(e.data);this.writeConvertedPcm(t,this.inputChannels,this.inputSampleRate),e.isLast&&this.reset()}close(){}},Oe=class extends G{decoder;codec;constructor(e){super({inputChannels:e.inputChannels,inputSampleRate:e.inputSampleRate,sink:e.sink}),this.codec=e.codec,this.decoder=this.createDecoder()}createDecoder(){let e=new AudioDecoder({output:e=>{try{let t=this.convertAudioData(e);this.writeConvertedPcm(t,e.numberOfChannels,e.sampleRate)}catch(e){H(e)}finally{try{e.close()}catch{}}},error:e=>{H(e)}});return e.configure({codec:this.codec,numberOfChannels:this.inputChannels,sampleRate:this.inputSampleRate}),e}feedChunk(e){try{if(e.isFirst&&this.reset(),this.decoder.state===`closed`&&(this.decoder=this.createDecoder()),this.decoder.state!==`configured`)return;this.decoder.decode(new EncodedAudioChunk({type:`key`,timestamp:e.timestampUs,duration:e.durationUs,data:e.data})),e.isLast&&this.decoder.flush().catch(()=>{})}catch(e){H(e);try{this.reset()}catch{}}}reset(){super.reset();try{this.decoder.reset(),this.decoder.configure({codec:this.codec,numberOfChannels:this.inputChannels,sampleRate:this.inputSampleRate})}catch(e){H(e)}}close(){try{this.decoder.close()}catch{}}},ke=class extends G{decoder=null;isReady=!1;decoderSampleRate=48e3;pendingChunks=[];async initialise(e,t){let{OpusDecoder:n,getOpusModule:r}=await import(`./src-DOUWsCWT.js`);await r();let i=[8e3,12e3,16e3,24e3,48e3].reduce((e,n)=>Math.abs(n-t)<Math.abs(e-t)?n:e);this.decoderSampleRate=i,this.decoder=new n({channels:e,sampleRate:this.decoderSampleRate}),await this.decoder.ready,this.isReady=!0,this.flushPendingChunks()}flushPendingChunks(){if(!this.isReady||!this.decoder){this.pendingChunks.length=0;return}let e=this.pendingChunks,t=e.length;if(t!==0){for(let n=0;n<t;n++)this.decodeChunk(e[n]);e.length=0}}reset(){super.reset();try{this.decoder?.reset()}catch{}}decodeChunk(e){if(!(!this.isReady||!this.decoder))try{if(e.data.byteLength===0)return;let t=this.decoder.decodeFrame(e.data),n=t.channelData,r=t.samplesDecoded,i=n.length;if(!i||!r)return;let a=r*i,o=this.getInterleavedBuffer(a);if(i===2){let e=n[0],t=n[1];for(let n=0,i=0;n<r;n++)o[i++]=e[n],o[i++]=t[n]}else if(i===1)o.set(n[0].subarray(0,r));else{let e=0;for(let t=0;t<r;t++)for(let r=0;r<i;r++)o[e++]=n[r][t]}this.writeConvertedPcm(o.subarray(0,a),i,this.decoderSampleRate),e.isLast&&this.reset()}catch(e){H(e),this.reset()}}feedChunk(e){if(!this.isReady||!this.decoder){this.pendingChunks.push(e);return}e.isFirst&&this.reset(),this.decodeChunk(e)}close(){try{this.decoder?.destroy()}catch{}this.decoder=null,this.pendingChunks.length=0}};const K=new Map;async function Ae(e){if(e.closed||e.decoder)return;if(e.codec===C.RawPcm16){e.decoder=new De({inputChannels:e.inputChannels,inputSampleRate:e.inputSampleRate,sink:e.sink});return}if(e.codec!==C.Opus&&e.codec!==C.Mp3)throw Error(`Unsupported audio codec: ${C[e.codec]??e.codec}`);let t=Te(e.codec);if(L&&typeof AudioDecoder<`u`&&t!==null)try{if((await AudioDecoder.isConfigSupported({codec:t,numberOfChannels:e.inputChannels,sampleRate:e.inputSampleRate})).supported){let n=new Oe({codec:t,inputChannels:e.inputChannels,inputSampleRate:e.inputSampleRate,sink:e.sink});if(e.closed){n.close();return}e.decoder=n;return}}catch(e){P.warn(`WebCodecs AudioDecoder failed, falling back to WASM: ${e instanceof Error?e.message:String(e)}`)}if(e.codec===C.Opus){let t=new ke({inputChannels:e.inputChannels,inputSampleRate:e.inputSampleRate,sink:e.sink});if(await t.initialise(e.inputChannels,e.inputSampleRate),e.closed){t.close();return}e.decoder=t;return}throw Error(`No available decoder for codec: ${C[e.codec]??e.codec}`)}function je(e){if(e.closed){e.pendingFrames.splice(0);try{e.decoder?.close()}catch{}e.decoder=null;return}if(!e.decoder)return;let t=e.pendingFrames.splice(0);for(let n of t)q(e,n)}function q(e,t){if(e.closed||!e.decoder)return;let n=t.Epoch,r=Number(t.TimeStampInInterleavedSamples),i=t.FrameSizeInInterleavedSamples;if(e.currentEpoch!==n&&(e.currentEpoch=n,e.epochSet=!1,e.lastArrivalTimeMs=0,e.jitterMs=0,e.sink.flush(),e.decoder.reset()),!e.epochSet){e.epochSet=!0;let t=e.inputSampleRate*e.inputChannels;e.expectedIntervalMs=i*1e3/t}let a=performance.now();if(e.lastArrivalTimeMs>0){let t=a-e.lastArrivalTimeMs,n=Math.abs(t-e.expectedIntervalMs);e.jitterMs+=(n-e.jitterMs)/10}e.lastArrivalTimeMs=a;let o=e.inputSampleRate*e.inputChannels,s=z*B,c=Math.round(r*s/o),l=Math.round(i*s/o),u=Math.round(r*1e6/o),d=Math.round(i*1e6/o);F({type:`frame`,streamId:e.streamId,epoch:n,timestampInterleavedSamples:c,frameSizeInterleavedSamples:l,isFirst:t.IsFirst,isLast:t.IsLast,jitterMs:e.jitterMs}),e.decoder.feedChunk({data:t.Samples,isFirst:t.IsFirst,isLast:t.IsLast,timestampUs:u,durationUs:d})}async function Me(e){J(U(e),await pe(e))}function J(e,t){let n=K.get(e);if(n){if(!n.closed&&n.codec===t.Codec&&n.inputSampleRate===t.SampleRate&&n.inputChannels===t.Channels)return;n.closed=!0,n.pendingFrames.splice(0);try{n.decoder?.close()}catch{}try{n.sink.drainPcm(),n.sink.close()}catch{}K.delete(e)}let r=new we(e),i={streamId:e,codec:t.Codec,inputSampleRate:t.SampleRate,inputChannels:t.Channels,sink:r,decoder:null,pendingFrames:[],initTask:null,decoderInitAttempts:0,currentEpoch:-1,epochSet:!1,closed:!1,lastArrivalTimeMs:0,jitterMs:0,expectedIntervalMs:20};K.set(e,i),X.delete(e),F({type:`streamBegin`,streamId:e,sharedArrayBuffer:r.getSharedArrayBuffer()})}function Y(){for(let e of K.values())F({type:`streamBegin`,streamId:e.streamId,sharedArrayBuffer:e.sink.getSharedArrayBuffer()})}function Ne(e){let t=U(e),n=K.get(t);if(n){n.closed=!0,n.pendingFrames.splice(0);try{n.sink.drainPcm(),n.sink.close()}catch{}try{n.decoder?.close()}catch{}n.decoder=null,K.delete(t),F({type:`streamEnd`,streamId:t})}}const X=new Set;async function Pe(e){let t=U(e),n=K.get(t);if(!n){X.has(t)||(X.add(t),P.warn(`Dropping audio frames for unknown stream ${t.substring(0,8)} — no AUDIO_STREAM_BEGIN received`));return}if(n.closed)return;let r=await ce(e);if(!R){if(!n.decoder){if(n.pendingFrames.length>=64){let e=n.pendingFrames.length-64+1;n.pendingFrames.splice(0,e),P.warn(`Dropped ${e} frames waiting for decoder, streamId=${t.substring(0,8)}`)}n.pendingFrames.push(r),!n.initTask&&n.decoderInitAttempts<3&&(n.initTask=Ae(n).then(()=>je(n)).catch(e=>{n.initTask=null,n.decoderInitAttempts+=1,n.closed||(P.warn(`Decoder init failed (attempt ${n.decoderInitAttempts}/3), streamId=${t.substring(0,8)}`),n.decoderInitAttempts>=3&&H(e,t))}));return}q(n,r)}}function Fe(e){switch(n(t(e))){case w.AUDIO_STREAM_BEGIN:Me(e);return;case w.AUDIO_STREAM_END:Ne(e);return;case w.AUDIO_FRAME2:Pe(e);return;default:return}}function Z(){if(V){try{V.close()}catch{}V=null}}function Ie(){Z();for(let e of K.values()){try{e.sink.drainPcm(),e.sink.close()}catch{}try{e.decoder?.close()}catch{}}K.clear()}self.addEventListener(`message`,e=>{let t=e.data;if(t.type===`configure`){I=t.transport,L=t.preferWebCodecs??!0,R=t.webRtcEnabled??!1,z=Number.isFinite(t.outputSampleRate)&&t.outputSampleRate>0?t.outputSampleRate:N,B=Number.isFinite(t.outputChannels)&&t.outputChannels>0?t.outputChannels:2;return}if(t.type===`attachProtocolPort`){Z(),V=t.port,V.addEventListener(`message`,e=>{let t=e.data;t?.type===`protocol`&&t.message instanceof ArrayBuffer&&Fe(new Uint8Array(t.message))}),V.start?.();return}if(t.type===`rebind`){I=t.transport,typeof t.outputSampleRate==`number`&&Number.isFinite(t.outputSampleRate)&&t.outputSampleRate>0&&(z=t.outputSampleRate),typeof t.outputChannels==`number`&&Number.isFinite(t.outputChannels)&&t.outputChannels>0&&(B=t.outputChannels);for(let e of K.values())e.sink.setOutputFormat(z,B),e.sink.rebind(I);Y();return}if(t.type===`setWebRtcEnabled`){R!==t.enabled&&P.info(`WebRTC media ${t.enabled?`active — leaving decode to the RTP stack`:`inactive — decoding protocol audio frames`}`),R=t.enabled;return}if(t.type===`syncStreams`){for(let e of t.streams)J(`${e.senderId}_${e.trackId}`,e.info);return}t.type===`dispose`&&Ie()});let Q=null;function Le(e,t){let n=e.length;if(n===1)return e[0];let r=t*n;if((!Q||Q.length<r)&&(Q=new Float32Array(r)),n===2){let n=e[0],r=e[1];for(let e=0,i=0;e<t;e++)Q[i++]=n[e],Q[i++]=r[e]}else{let r=0;for(let i=0;i<t;i++)for(let t=0;t<n;t++)Q[r++]=e[t][i]}return Q.subarray(0,r)}function $(e){if(e===0)return 1;let t=Math.PI*e;return Math.sin(t)/t}function Re(e,t){return e===0?1:Math.abs(e)>=t?0:$(e)*$(e/t)}function ze(e,t,n,r,i){if(r===i)return t.set(e),e.length;let a=e.length/n,o=Math.max(1,Math.round(a*i/r)),s=r/i,c=s>1?s:1,l=Math.ceil(3*c);for(let r=0;r<n;r++)for(let i=0;i<o;i++){let o=i*s,u=Math.floor(o),d=0,f=0;for(let t=-l+1;t<=l;t++){let i=u+t;if(i<0||i>=a)continue;let s=Re((o-i)/c,3);if(s!==0){let t=e[i*n+r]??0;d+=t*s,f+=s}}t[i*n+r]=f>0?d/f:0}return o*n}function Be(e,t,n,r){let i=e.length/n;if(n===r)return t.set(e),e.length;if(n===1&&r===2){for(let n=0;n<i;n++){let r=e[n]??0;t[n*2]=r,t[n*2+1]=r}return i*2}if(n===2&&r===1){for(let n=0;n<i;n++)t[n]=((e[n*2]??0)+(e[n*2+1]??0))*.5;return i}let a=Math.min(n,r);for(let o=0;o<i;o++)for(let i=0;i<a;i++)t[o*r+i]=e[o*n+i]??0;return i*r}
|