/**
 * AgoraWebSDK_N-v4.23.0-0-g98fcf352-dirty Copyright AgoraInc.
 */

import{RemoteDataChannel as e,LocalDataChannel as t}from"@agora-js/media";import{logger as n}from"@agora-js/report";import{getParameter as r}from"@agora-js/shared";import{inflate as a,deflate as s}from"pako";var i=function(e){return e[e.ONE_BYTE=0]="ONE_BYTE",e[e.TWO_BYTE=1]="TWO_BYTE",e}(i||{});class o{constructor(){this._sequence=0,this._startTime=Date.now(),this.isUseOneByte=!0}get startTime(){const e=Date.now()-this._startTime;return e<Math.pow(2,16)?e:(this._startTime+=Math.pow(2,16),this.startTime)}get sequence(){return this._sequence<Math.pow(2,32)?this._sequence++:(this._sequence-=Math.pow(2,32),this.sequence)}serialize(e){const t={commonPacketHeader:{length:0,reserved:0,extension:0,sequence:this.sequence},commonStreamHeader:this.startTime,extension:void 0,payload:e};if(e.byteLength>128){const n=new Uint8Array(4);n.set([1,0,0,0]);const r={id:0,length:4,data:n.buffer},a={profile:this.isUseOneByte?0:1,length:this.isUseOneByte?5:6,datas:[r]};t.commonPacketHeader.extension=1,t.extension=a,t.payload=this.compress(e),t.commonPacketHeader.length=8+(t.extension.length+2)+t.payload.byteLength}else t.commonPacketHeader.length=8+t.payload.byteLength;r("SHOW_DATASTREAM2_LOG")&&n.debug("send data header: ".concat(JSON.stringify(t.commonPacketHeader)));const a=new ArrayBuffer(t.commonPacketHeader.length),s=new Uint8Array(a),i=new DataView(a);let o=0;if(i.setUint16(o,t.commonPacketHeader.extension<<15|t.commonPacketHeader.reserved<<14|t.commonPacketHeader.length,!0),o+=2,i.setUint32(o,t.commonPacketHeader.sequence,!0),o+=4,i.setUint16(o,t.commonStreamHeader,!0),o+=2,t.extension){const e=this.serializeExtension(t.extension);s.set(new Uint8Array(e),o),o+=e.byteLength}if(s.set(new Uint8Array(t.payload),o),o+=t.payload.byteLength,o!==t.commonPacketHeader.length)throw Error("serialize error!");return a}deserialize(e){if(e.byteLength<4)return new ArrayBuffer(0);const t=new DataView(e);let a=0;const s=t.getUint16(a,!0);a+=2;const i={length:16383&s,reserved:(16384&s)>>14,extension:(32768&s)>>15,sequence:t.getUint16(a+2,!0)<<16|t.getUint16(a,!0)};let o,c;if(a+=4,r("SHOW_DATASTREAM2_LOG")&&n.debug("receive data header: ".concat(JSON.stringify(i))),t.getUint16(a,!0),a+=2,i.extension){c=this.deserializeExtension(e.slice(a)),a+=2+c.length,o=e.slice(a);let t=!1;if(c.datas.length>0){const e=c.datas.find((e=>0===e.id));if(e){t=1==(1&new DataView(e.data).getUint32(0,!0))}}o=t?this.decompress(o):o}else o=e.slice(8);return o}serializeExtension(e){const{profile:t,length:n,datas:r}=e,a=new ArrayBuffer(n+2),s=new Uint8Array(a),i=new DataView(a);let o=0;if(i.setUint8(o++,t),i.setUint8(o++,n),r.forEach((e=>{t?(i.setUint8(o++,e.id),i.setUint8(o++,e.length),s.set(new Uint8Array(e.data),o),o+=e.data.byteLength):(i.setUint8(o++,e.id|e.length<<4),s.set(new Uint8Array(e.data),o),o+=e.data.byteLength)})),o!==n+2)throw Error("serialize extension error, is ".concat(o,"!==").concat(n+2));return a}deserializeExtension(e){const t=new DataView(e);let n=0;const r=t.getUint8(n);n++;const a=t.getUint8(n);n++;const s=r===i.TWO_BYTE,o=[],c=new DataView(e,2);let h=0;for(;h<a;){let e=0,t=0,n=new ArrayBuffer(0);s?(e=c.getUint8(h),h++,t=c.getUint8(h),h++):(e=15&c.getUint8(h),t=c.getUint8(h)>>4,h++),t>0&&(n=c.buffer.slice(h+2,h+2+t),h+=n.byteLength),o.push({id:e,length:t,data:n})}if(h!==a)throw Error("parse error");return{profile:r,length:a,datas:o}}decompress(e){return a(new Uint8Array(e))}compress(e){return s(new Uint8Array(e))}}const c={name:"DataStream",create:(n,r)=>{const a=r?new e(n):new t(n);return a.useDataStream(new o),a}};export{c as DataStreamService};
