@ceeblue/webrtc-client
Version:
Ceeblue WebRTC Client
1 lines • 66.1 kB
JavaScript
function t(t,e,s,i){return new(s||(s=Promise))((function(r,n){function o(t){try{h(i.next(t))}catch(t){n(t)}}function a(t){try{h(i.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(o,a)}h((i=i.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const e=new TextDecoder;function s(t,e,s,i){return new(s||(s=Promise))((function(r,n){function o(t){try{h(i.next(t))}catch(t){n(t)}}function a(t){try{h(i.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(o,a)}h((i=i.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const i=new TextDecoder,r=new TextEncoder,n=performance,o=()=>{};function a(){return Math.floor(n.now())}function h(t=("undefined"==typeof location?void 0:location)){if(!t)return{};try{t=new URL(t).searchParams}catch(e){"string"==typeof t&&(t.startsWith("?")&&(t=t.substring(1)),t=new URLSearchParams(t))}return c(t,{withType:!0,noEmptyString:!0})}function c(t,e){e=Object.assign({withType:!1,noEmptyString:!1},e);const s={};if(!t)return s;for(const[i,r]of l(t)){if(t=r,e.withType&&null!=t&&t.substring)if(t){const e=Number(t);if(isNaN(e))switch(t.toLowerCase()){case"true":t=!0;break;case"false":t=!1;break;case"null":t=null;break;case"undefined":t=void 0}else t=e}else e.noEmptyString&&(t=!0);s[i]?(Array.isArray(s[i])||(s[i]=new Array(s[i])),s[i].push(t)):s[i]=t}return s}function l(t){return t?("function"==typeof t.entries&&(t=t.entries()),"function"==typeof t[Symbol.iterator]?t:function*(){for(const e in t)yield[e.toString(),t[e]]}()):function*(){}()}function u(t){return r.encode(t)}function d(t){const e=t.lastIndexOf(".");return e>=0&&e>t.lastIndexOf("/")?t.substring(e):""}function m(t){return t.substring(t.lastIndexOf("/")+1)}function _(t){const e=t.lastIndexOf("."),s=t.lastIndexOf("/")+1;return e>=0&&e>=s?t.substring(s,e):t.substring(s)}function p(t){const e=[];for(let s=0;s<t.length;++s)e.push(t.charCodeAt(s));return e}function g(t,e=" "){const s=p(e);let i=0;for(;i<t.length&&s.includes(t.charCodeAt(i));)++i;return t.substring(i)}var f,v=Object.freeze({__proto__:null,EMPTY_FUNCTION:o,equal:function(t,e){if(Object(t)!==t)return Object(e)!==e&&t===e;if(t[Symbol.iterator]){if(!e[Symbol.iterator])return!1;if(t.length!==e.length)return!1;for(let s=0;s!==t.length;++s)if(t[s]!==e[s])return!1;return!0}return t===e},fetch:function(t,e){return s(this,void 0,void 0,(function*(){const s=yield self.fetch(t,e);if(s.status>=300){let t;throw s.body&&(t=yield s.text()),(t||s.statusText||s.status).toString()}return s}))},getBaseFile:_,getExtension:d,getFile:m,iterableEntries:l,objectFrom:c,options:h,safePromise:function(t,e){let s;return Promise.race([e instanceof Promise?e:new Promise(e),new Promise(((e,i)=>s=setTimeout((()=>i("timed out in "+t+"ms")),t)))]).finally((()=>clearTimeout(s)))},sleep:function(t){return new Promise((e=>{setTimeout(e,t)}))},stringify:function t(e,s={}){if(s=Object.assign({space:" ",decimal:2,recursion:1,noBin:!1},s),null==e)return String(e);const r=e.error||e.message;if(r&&(e=r),e.toFixed)return e.toFixed(Number(s.decimal)||0);if(null!=e.byteLength&&(null==e?void 0:e[Symbol.iterator]))return s.noBin?"["+e.byteLength+"#bytes]":i.decode(e);if("boolean"==typeof e||e.substring||!s.recursion)return String(e);const n=s.space||"";if(Array.isArray(e)){let i="";for(const r of e)i+=(i?",":"[")+n,i+=t(r,Object.assign(Object.assign({},s),{recursion:s.recursion-1}));return i+=n+"]"}let o="{";for(const i in e)o.length>1&&(o+=","),o+=n+i+":",o+=t(e[i],Object.assign(Object.assign({},s),{recursion:s.recursion-1}))+n;return o+"}"},time:a,timeOrigin:function(){return Math.floor(n.now()+n.timeOrigin)},toBin:u,trim:function(t,e=" "){const s=p(e);let i=0;for(;i<t.length&&s.includes(t.charCodeAt(i));)++i;let r=t.length;for(;r>0&&s.includes(t.charCodeAt(r-1));)--r;return t.substring(i,r)},trimEnd:function(t,e=" "){const s=p(e);let i=t.length;for(;i>0&&s.includes(t.charCodeAt(i-1));)--i;return t.substring(0,i)},trimStart:g});!function(t){t.ERROR="error",t.WARN="warn",t.INFO="info",t.DEBUG="debug"}(f||(f={}));let y=0;setInterval((()=>{console.assert(0===y,y.toFixed(),"calls to log was useless")}),1e4);const b=h()["!cb-override-log-level"],w=new Array(128);w[101]=w[69]=1,w[119]=w[87]=2,w[105]=w[73]=3,w[100]=w[68]=4;class S{get error(){return this._bind(f.ERROR)}get warn(){return this._bind(f.WARN)}get info(){return this._bind(f.INFO)}get debug(){return this._bind(f.DEBUG)}constructor(t,...e){e.length||e.push(void 0),this._args=e,this._log=t,++y}_onLog(t,e){var s,i;const r=null!==(i=null!==(s=null!=b?b:t.level)&&void 0!==s?s:k.level)&&void 0!==i?i:f.INFO;return!1!==r&&(!(!0!==r&&w[e.charCodeAt(0)]>w[r.charCodeAt(0)])&&(t.on&&t.on(e,this._args),!!this._args.length))}_bind(t){return this._done||(this._done=!0,--y),this._onLog(k,t)&&(this._log===k||this._onLog(this._log,t))?console[t].bind(console,...this._args):o}}class T{constructor(){this.log=(...t)=>new S(this.log,...t)}}const k=(...t)=>new S(k,...t);class x{onBytes(t){}get interval(){return this._interval}set interval(t){this._interval=t,this.updateSamples()}constructor(t=1e3){this._interval=t,this.clear()}value(){return Math.round(this.exact())}exact(){this.updateSamples();const t=a()-this._time;return t?this._bytes/t*1e3:0}addBytes(t){var e;const s=a(),i=this.updateSamples(s)[this._samples.length-1];if(s>(null!==(e=null==i?void 0:i.time)&&void 0!==e?e:this._time))this._samples.push({bytes:t,time:s,clip:!1});else{if(!i)return this;i.bytes+=t}return this._bytes+=t,this.onBytes(t),this}clear(){return this._bytes=0,this._time=a(),this._samples=[],this._clip=!1,this}clip(){if(this._clip){this._clip=!1;let t=0;for(const e of this._samples)if(this._bytes-=e.bytes,++t,this._time=e.time,e.clip)break;this._samples.splice(0,t)}const t=this._samples[this._samples.length-1];return t&&(t.clip=!0,this._clip=!0),this}updateSamples(t=a()){const e=t-this._interval;let s,i=0;for(;this._time<e&&(s=this._samples[i]);){if(this._bytes-=s.bytes,s.clip&&(this._clip=s.clip=!1),s.time>e){s.bytes*=(s.time-e)/(s.time-this._time),this._time=e,this._bytes+=s.bytes;break}++i,this._time=s.time}return this._samples.splice(0,i),this._samples}}class C{static fixProtocol(t,e){const s=e.indexOf("://");return s>=0&&(s>2&&"s"===e.charAt(s-1).toLowerCase()?(t.length<=2||!t.endsWith("s"))&&(t+="s"):t.length>2&&t.endsWith("s")&&(t=t.slice(0,-1)),e=e.substring(s+3)),t+"://"+e}get host(){return this._host}get domain(){return this._domain}get port(){return this._port}toString(){return this._address}valueOf(){return this._address}constructor(t,e){this._address=t;let s=t.indexOf("/");s>=0&&(47===t.charCodeAt(s+1)?s>0?58===t.charCodeAt(s-1)&&(t=t.substring(s+2)):t=t.substring(2):s||(t=t.substring(1))),s=t.indexOf("/"),s>=0&&(t=t.substring(0,s)),this._host=t,this._domain=t,this._port=e,s=this._host.lastIndexOf(":");if(s>this._host.lastIndexOf("]")){const e=parseInt(t.substring(s+1));e>=0&&e<=65535&&(this._port=e,this._domain=t.substring(0,s))}}}var I;function R(t,e){switch(t){case I.HESP:e.mediaExt="mp4";break;case I.WEBRTC:e.mediaExt="rtp";break;case I.WRTS:try{const t=d(m(new URL(e.endPoint).pathname));t&&".json"!==t.toLowerCase()&&(e.mediaExt=t)}catch(t){}e.mediaExt||(e.mediaExt="rts");break;case I.META:e.mediaExt="js";break;case I.DATA:e.mediaExt="json";break;default:e.mediaExt="",console.warn("Unknown params type "+t)}e.mediaExt=g(e.mediaExt,".")}!function(t){t.HESP="HESP",t.WRTS="WebRTS",t.WEBRTC="WebRTC",t.META="Meta",t.DATA="Data"}(I||(I={}));var M=Object.freeze({__proto__:null,get Type(){return I},buildURL:function(t,e,s="wss"){var i;R(t,e);const r=new URL(C.fixProtocol(s,e.endPoint));if(r.pathname.length<=1)switch(t){case I.HESP:r.pathname="/hesp/"+e.streamName+"/index.json";break;case I.WEBRTC:r.pathname="/webrtc/"+e.streamName;break;case I.WRTS:r.pathname="/wrts/"+e.streamName+"."+e.mediaExt;break;case I.META:r.pathname="/json_"+e.streamName+".js";break;case I.DATA:r.pathname="/"+e.streamName+".json";break;default:console.warn("Unknown url type "+t)}else if(!e.streamName){const t=r.pathname.split("/");e.streamName=_(t[2]||t[1]||t[0])}e.accessToken&&r.searchParams.set("id",e.accessToken);for(const[t,s]of null!==(i=e.query)&&void 0!==i?i:[])r.searchParams.set(t,s);return r},defineMediaExt:R});class E extends T{constructor(){super(),this._events=new Map;let t=Object.getPrototypeOf(this);for(;t&&t!==Object.prototype;){for(const e of Object.getOwnPropertyNames(t)){if(e.length<3||!e.startsWith("on"))continue;let s=t[e];if(s instanceof Function){const t=new Set;this._events.set(e.substring(2).toLowerCase(),t);const i=(...e)=>{const i=s?s.call(this,...e):void 0;for(const s of t)s(...e);return i};Object.defineProperties(this,{[e]:{get:()=>i,set:t=>{s=t}}})}}t=Object.getPrototypeOf(t)}}on(t,e,s){var i;if("function"!=typeof e)throw Error("event callback must be a function");const r=this._event(t);r.add(e),null===(i=null==s?void 0:s.signal)||void 0===i||i.addEventListener("abort",(()=>r.delete(e)),{once:!0})}once(t,e,s){var i;if("function"!=typeof e)throw Error("event callback must be a function");const r=this._event(t);r.add(((...t)=>{r.delete(e),e(...t)})),null===(i=null==s?void 0:s.signal)||void 0===i||i.addEventListener("abort",(()=>r.delete(e)),{once:!0})}off(t,e){if(!e)throw Error("event to unsubscribe cannot be null");return this._event(t).delete(e)}_event(t){const e=this._events.get(t.toLowerCase());if(!e)throw Error("No event on"+t+" on class "+this.constructor.name);return e}}class P{[Symbol.iterator](){return this._map[Symbol.iterator]()}get size(){return this._map.size}constructor(t){this._initValue=t,this._map=new Map}get(t){let e=this.find(t);return void 0===e&&this._map.set(t,e=this._initValue()),e}find(t){return this._map.get(t)}has(t){return this._map.has(t)}clear(){this._map.clear()}delete(t){return this._map.delete(t)}set(t,e){return this._map.set(t,e),e}forEach(t,e){this._map.forEach(t,e)}}class B{get size(){return this._queue.length}get capacity(){return this._capacity}set capacity(t){this._capacity=t,null!=t&&this._queue.length>t&&this._queue.splice(0,this._queue.length-t)}get front(){return this._queue[0]}get back(){return this._queue[this._queue.length-1]}[Symbol.iterator](){return this._queue[Symbol.iterator]()}constructor(t){this._capacity=t,this._queue=new Array}push(t){return null!=this._capacity&&this._queue.push(t)>this._capacity&&this.pop(),this}pop(){return this._queue.shift()}clear(){return this._queue.length=0,this}}class O extends B{get minimum(){return this._min}get maximum(){return this._max}get average(){return null==this._average&&(this._average=this.size?this._sum/this.size:0),this._average}constructor(t){super(t),this._sum=0,this._min=0,this._max=0}push(t){return t>this._max?this._max=t:t<this._min&&(this._min=t),this._average=void 0,this._sum+=t,super.push(t),this}pop(){const t=super.pop();return t===this._max?this._max=Math.max(0,...this):t===this._min&&(this._min=Math.min(0,...this)),this._average=void 0,this._sum-=t||0,t}clear(){return this._min=this._max=this._sum=0,super.clear(),this}}const N={fromString(t){if(Array.isArray(t))return t;const e=new Array;let s,i=e;for(let r of t.toString().split("\n")){if(r=r.trim(),!r)continue;let t=r[0];const n=r.substring(r.indexOf("=")+1).trim();switch(t.toLowerCase()){case"a":if(!n)continue;t=this.addAttribute(i,n),e===i&&"fingerprint"===t.toLowerCase()&&(s=i.fingerprint);break;case"m":e.length&&s&&!e[e.length-1].fingerprint&&(i.fingerprint=s),e.push(i={m:n});break;default:i[t]=n}}return e.length&&s&&!e[e.length-1].fingerprint&&(i.fingerprint=s),e},toString(t){if("string"==typeof t)return t;const e=[];let s="v"in t?"v="+t.v+"\n":"";"o"in t&&(s+="o="+t.o+"\n"),"s"in t&&(s+="s="+t.s+"\n");const i=t;for(const r of Object.keys(t)){if("v"===r||"o"===r||"s"===r)continue;const t=i[r];if(null==t)continue;const n=parseInt(r);if(!isNaN(n)){e[n]=t;continue}const o=Array.isArray(t)&&t.length||1;for(let e=0;e<o;++e){const i=Array.isArray(t)&&t.length?t[e]:t;r.length>1?(s+="a="+r,i&&(s+=":")):s+=r+"=",s+=i+"\n"}}for(const t of e)s+=this.toString(t);return s},addAttribute(t,e){var s;const i=N.parseAttribute(e),r=null!==(s=i.value)&&void 0!==s?s:"",n=t,o=n[i.key];return o?Array.isArray(o)?o.push(r):r!==o&&(n[i.key]=[o,r]):n[i.key]=r,i.key},removeAttribute(t,e){const s=N.parseAttribute(e),i=t;if(void 0===s.value)return delete i[e],e;const r=i[e];if(Array.isArray(s.value)){const t=r.findIndex((t=>t===s.value));t>=0&&r.splice(t,1)}else r===s.value&&delete i[e];return s.key},parseAttribute(t){const e=t.indexOf(":");return{key:(e>=0?t.substring(0,e):t).trim(),value:e>=0?t.substring(e+1).trim():void 0}}};Object.freeze(N);class A extends E{onOpen(){}onMessage(t){}onClose(t){t?this.log("onClose",t).error():this.log("onClose").info()}get binaryType(){return"arraybuffer"}get recvByteRate(){return this._recvByteRate.value()}get sendByteRate(){return this._sendByteRate.value()}get url(){var t,e;return null!==(e=null===(t=this._ws)||void 0===t?void 0:t.url)&&void 0!==e?e:""}get extensions(){var t,e;return null!==(e=null===(t=this._ws)||void 0===t?void 0:t.extensions)&&void 0!==e?e:""}get protocol(){var t,e;return null!==(e=null===(t=this._ws)||void 0===t?void 0:t.protocol)&&void 0!==e?e:""}get opened(){return this._opened}get readyState(){return this._ws?this._ws.readyState:3}get closed(){return this._closed}get bufferedAmount(){var t;return this._queueingBytes+((null===(t=this._ws)||void 0===t?void 0:t.bufferedAmount)||0)}get queueing(){return this._queueing}constructor(t,e){super(),this._queueing=[],this._queueingBytes=0,this._opened=!1,this._closed=!0,this._recvByteRate=new x,this._sendByteRate=new x,t&&this.open(t,e)}open(t,e){this._closed=!1;const s=this._ws=new WebSocket(t,e);return s.binaryType=this.binaryType,s.onmessage=t=>{var e;this._recvByteRate.addBytes(null!==(e=t.data.byteLength)&&void 0!==e?e:t.data.length),this.onMessage(t.data)},s.onclose=e=>{this._opened?1e3===e.code||1005===e.code?this.close({type:"WebSocketReliableError",name:"Server shutdown",url:t.toString()}):this.close({type:"WebSocketReliableError",name:"Socket disconnection",url:t.toString(),reason:String(e.reason||e.code)}):this.close({type:"WebSocketReliableError",name:"Connection failed",url:t.toString(),reason:String(e.reason||e.code)})},s.onopen=t=>{this._opened=!0,this.flush(),this.onOpen()},this}send(t,e=!1){if(this._closed)throw Error("Open socket before to send data");return e||!this._opened?(this._queueing.push(t),this._queueingBytes+="string"==typeof t?t.length:t.byteLength):this._send(t),this}flush(){if(this._ws)for(const t of this._queueing)this._send(t);this._queueing.length=0,this._queueingBytes=0}close(t){this._ws&&!this._closed&&(this._closed=!0,this._ws.onopen=this._ws.onclose=this._ws.onmessage=null,this._ws.close(),this._queueing.length=0,this._queueingBytes=0,this.onClose(t),this._opened=!1)}_send(t){this._ws&&(this._sendByteRate.addBytes("string"==typeof t?t.length:t.byteLength),this._ws.send(t))}}function z(t,e,s=32,i=.2){const r=e/s;let n="",o=r/2;const a=t.getImageData(0,Math.round(o),e,1).data,h=new Uint32Array(a.buffer),c=255*i,l=255*(1-i);for(;o<h.length;){const t=16777215&h[Math.round(o)],e=.299*(t>>16&255)+.587*(t>>8&255)+.114*(255&t);if(e<c)n+="1";else{if(!(e>l))return;n+="0"}o+=r}const u=parseInt(n.slice(0,5),2),d=parseInt(n.slice(5,10),2),m=parseInt(n.slice(10,16),2),_=parseInt(n.slice(16,22),2),p=parseInt(n.slice(22,32),2),g=new Date;return new Date(Date.UTC(g.getUTCFullYear(),g.getUTCMonth(),u,d,m,_,p))}var F=Object.freeze({__proto__:null,decodeTimestamp:z,encodeTimestamp:function(t,e,s=32,i=new Date){const r=Math.floor(e/s),n=i.getUTCDate(),o=i.getUTCHours(),a=i.getUTCMinutes(),h=i.getUTCSeconds(),c=i.getUTCMilliseconds(),l=n.toString(2).padStart(5,"0")+o.toString(2).padStart(5,"0")+a.toString(2).padStart(6,"0")+h.toString(2).padStart(6,"0")+c.toString(2).padStart(10,"0");for(let e=0;e<l.length;e++){const i=e%s*r,n=Math.floor(e/s)*r;t.fillStyle="1"===l[e]?"black":"white",t.fillRect(i,n,r,r)}},getLatency:function(t,e,s,i=new Date,r=32,n=.2){if(e.width=t.videoWidth,e.height=Math.floor(e.width/r),!e.width||!e.height)return 0;s.drawImage(t,0,0,e.width,e.height,0,0,e.width,e.height);const o=z(s,e.width,r,n);return null==o?0:i.getTime()-o.getTime()}});var D,U=Object.freeze({__proto__:null,BinaryReader:class{constructor(t){this._data="buffer"in t?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):new Uint8Array(t),this._size=this._data.byteLength,this._position=0,this._view=new DataView(this._data.buffer,this._data.byteOffset,this._size)}data(){return this._data}size(){return this._size}available(){return this._size-this._position}value(t=this._position){return this._data[t]}position(){return this._position}reset(t=0){this._position=Math.max(0,t>this._size?this._size:t)}shrink(t){const e=this._size-this._position;return t>e?e:(this._size=this._position+t,t)}next(t=1){const e=this._size-this._position;return t>e&&(t=e),this._position=Math.max(0,this._position+t),t}read8(){return 1===this.next(1)?this._view.getUint8(this._position-1):0}read16(){return 2===this.next(2)?this._view.getUint16(this._position-2):0}read24(){return 3===this.next(3)?this._view.getUint16(this._position-3)<<8|255&this._view.getUint8(this._position-1):0}read32(){return 4===this.next(4)?this._view.getUint32(this._position-4):0}read64(){return 8!==this.next(8)?0:4294967296*this._view.getUint32(this._position-8)+this._view.getUint32(this._position-4)}readFloat(){return 4===this.next(4)?this._view.getFloat32(this._position-4):0}readDouble(){return 8===this.next(8)?this._view.getFloat64(this._position-8):0}read7Bit(t=5){let e=0,s=1;for(;this.available();){const t=this.read8();if(e+=(127&t)*s,!(128&t))break;s*=128}return e}readString(){let t=this._position;for(;t<this._size&&this._data[t];)++t;const s=this.read(t-this._position);return this.next(),e.decode(s)}readHex(t){let e="";for(;t-- >0;)e+=("0"+this.read8().toString(16)).slice(-2);return e}read(t=this.available()){if(this.available()<t)return new Uint8Array(t);const e=this._position;return this._data.subarray(e,Math.max(e,this._position+=t))}},BinaryWriter:class{get view(){return this._view||(this._view=new DataView(this._data.buffer,this._data.byteOffset,this._data.byteLength)),this._view}get capacity(){return this._data.byteLength}constructor(t=64,e=0,s){"number"==typeof t?(this._data=new Uint8Array(t),this._size=0):"buffer"in t?(this._data=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),this._size=t.byteLength):(this._isConst=!0,null==s&&(s=t.byteLength),this._data=new Uint8Array(t,e,s),this._size=0)}data(){return new Uint8Array(this._data.buffer,this._data.byteOffset,this._size)}size(){return this._size||0}next(t=1){return this.reserve(this._size+=t)}clear(t=0){return this.reserve(this._size=t)}write(t){var e;let s;return s="string"==typeof t?u(t):t instanceof ArrayBuffer?new Uint8Array(t):"buffer"in t?new Uint8Array(t.buffer,null!==(e=t.byteOffset)&&void 0!==e?e:0,t.byteLength):t,this.reserve(this._size+s.length),this._data.set(s,this._size),this._size+=s.length,this}write8(t){return t>255&&(t=255),this.reserve(this._size+1),this._data[this._size++]=t,this}write16(t){return t>65535&&(t=65535),this.reserve(this._size+2),this.view.setUint16(this._size,t),this._size+=2,this}write24(t){return t>16777215&&(t=16777215),this.reserve(this._size+3),this.view.setUint16(this._size,t>>8),this.view.setUint8(this._size+=2,255&t),++this._size,this}write32(t){return t>4294967295&&(t=4294967295),this.reserve(this._size+4),this.view.setUint32(this._size,t),this._size+=4,this}write64(t){return this.write32(t/4294967296),this.write32(4294967295&t)}writeFloat(t){return this.reserve(this._size+4),this.view.setFloat32(this._size,t),this._size+=4,this}writeDouble(t){return this.reserve(this._size+8),this.view.setFloat64(this._size,t),this._size+=8,this}write7Bit(t){let e=127&t;for(;t=Math.floor(t/128);)this.write8(128|e),e=127&t;return this.write8(e)}writeString(t){return this.write(u(t)).write8(0)}writeHex(t){for(let e=0;e<t.length;e+=2)this.write8(parseInt(t.substring(e,e+2),16));return this}reserve(t){if(!this._data)throw Error("buffer not writable");if(t<=this._data.byteLength)return this;if(this._isConst)throw Error("writing exceeds maximum "+this._data.byteLength+" bytes limit");--t,t|=t>>1,t|=t>>2,t|=t>>4,t|=t>>8,t|=t>>16,++t;const e=new Uint8Array(t);return e.set(this._data),this._data=e,this._view=void 0,this}},BitReader:class extends T{constructor(t){super(),this._data="buffer"in t?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):new Uint8Array(t),this._size=this._data.byteLength,this._position=0,this._bit=0}data(){return this._data}size(){return this._size}available(){return 8*(this._size-this._position)-this._bit}next(t=1){let e=0;for(;this._position!==this._size&&t--;)++e,8==++this._bit&&(this._bit=0,++this._position);return e}read(t=1){let e=0;for(;this._position!==this._size&&t--;)e<<=1,this._data[this._position]&128>>this._bit++&&(e|=1),8===this._bit&&(this._bit=0,++this._position);return e}read8(){return this.read(8)}read16(){return this.read(16)}read24(){return this.read(24)}read32(){return this.read(32)}readExpGolomb(){let t=0;for(;!this.read();){if(!this.available())return 0;++t}const e=this.read(t);return t>15?(this.log("Exponential-Golomb code exceeding unsigned 16 bits").warn(),0):e+(1<<t)-1}},ByteRate:x,Connect:M,EpochTime:F,EventEmitter:E,FixMap:P,Log:S,get LogLevel(){return f},Loggable:T,NetAddress:C,Numbers:O,Queue:B,SDP:N,UIMetrics:class{get graphMargin(){return this._graphMargin}set graphMargin(t){this._graphMargin=t}get textMargin(){return this._textMargin}set textMargin(t){this._textMargin=t}get lineHeight(){return this._lineHeight}set lineHeight(t){this._lineHeight=t}get labelWidth(){return this._labelWidth}set labelWidth(t){this._labelWidth=t}get legendFontSize(){return this._legendFontSize}set legendFontSize(t){this._legendFontSize=t}get stepSize(){return this._stepSize}set stepSize(t){this._stepSize=t}get averageDisplayWidth(){return this._legendFontSize/2*7}get displayableCount(){const t=this._ui.clientWidth-this.averageDisplayWidth;return Math.ceil((t-this._labelWidth)/this._stepSize)}constructor(t){this._ui=t,this._lineHeight=40,this._labelWidth=170,this._graphMargin=5,this._textMargin=5,this._legendFontSize=13,this._stepSize=10,this._ranges={}}reset(){this._ranges={}}display(t){if(null!=this._html)return;this._html="";const e=this.averageDisplayWidth,s=e/2,i=this._ui.clientWidth-e,r=this._lineHeight-2*this._graphMargin,n=Math.round(this._lineHeight/2),o=Math.round(this._lineHeight/2+this._textMargin),a=this._labelWidth-2*this._textMargin;for(const[e,h]of t){const t=Math.ceil((i-this._labelWidth)/this._stepSize),c=h.slice(Math.max(0,h.length-t));if(!c.length)continue;let l=this._labelWidth+c.length*this._stepSize;this._html+='<svg class="list-group-item p-0" style="height: '+this._lineHeight+'px" xmlns="http://www.w3.org/2000/svg">',this._html+='<text x="'+this._textMargin+'" y="'+o+'">'+e+"</text>",this._html+='<text x="'+a+'" y="'+o+'" text-anchor="end">'+c[c.length-1].toString()+"</text>",this._html+='<path fill="none" d="M'+this._labelWidth+" "+n,this._html+="H"+(i+s),this._html+='" stroke-width="1" stroke="lightgray" stroke-dasharray="10,10"/>',this._html+='<path fill="none" stroke-width="1" stroke="brown" d="M';let u=Number.POSITIVE_INFINITY,d=Number.NEGATIVE_INFINITY;for(let t=0;t<c.length;++t){const e=parseFloat(c[t].toString());e<u&&(u=e),e>d&&(d=e)}let m=this._ranges[e];m||(this._ranges[e]=m={min:Number.POSITIVE_INFINITY,max:Number.NEGATIVE_INFINITY}),m.min=Math.min(m.min,u),m.max=Math.max(m.max,d);const _=m.max-m.min;let p="",g="";for(let t=0;t<c.length;++t){l-=this._stepSize;const e=parseFloat(c[t].toString()),s=n+(_?Math.round((.5-(e-m.min)/_)*r):0);this._html+=l+" "+s+" ",e===u?g=g||this._drawCircle(l,s,e):e===d&&(p=p||this._drawCircle(l,s,e))}this._html+='" />';const f=Math.round((d-u)/2);this._html+='<text text-anchor="middle" font-size="'+this._legendFontSize+'" y="'+o+'">',this._html+='<tspan x="'+(i+s)+'" dy="-0.5em">'+(u!==d?"≈":"=")+(u+f)+"</tspan>",this._html+='<tspan x="'+(i+s)+'" dy="1em">±'+f+"</tspan>",this._html+="</text>",this._html+=p+g,this._html+="</svg>"}requestAnimationFrame((()=>{null!=this._html&&(this._ui.innerHTML=this._html,this._html=void 0)}))}_drawCircle(t,e,s){let i='<circle cx="'+t+'" cy="'+e+'" r="2" fill="green" />';const r=.7*this._legendFontSize;return e<Math.round(this._lineHeight/2)?e+=this.textMargin+r:e-=this.textMargin,i+='<text font-style="italic" font-size="'+this._legendFontSize+'" x="'+(t-this._legendFontSize)+'" y="'+e+'">'+s+"</text>",i}},Util:v,VERSION:"5.0.0",WebSocketReliable:A,log:k});function $(t,e,s){for(let i=0;i<e.length;++i){const r=e[i];s.has(r.codec.toLowerCase())||(t.delete(r.idx),e.splice(i--,1))}}!function(t){t.AUDIO="audio",t.VIDEO="video",t.DATA="data"}(D||(D={}));class L{constructor(){this.type="",this.width=0,this.height=0,this.sources=new Map,this.tracks=new Map,this.audios=[],this.videos=[],this.datas=[]}subset(t){const e=Object.assign({},this);if(t){$(e.tracks,e.audios,t),$(e.tracks,e.videos,t);for(const[,t]of e.tracks){for(;t.up&&!e.tracks.has(t.up.idx);)t.up=t.up.up;for(;t.down&&!e.tracks.has(t.down.idx);)t.down=t.down.down}}return e}}const W=(t,e)=>e.maxbps-t.maxbps;var j;!function(t){t.UNKNOWN="",t.ONLINE="Stream is online",t.OFFLINE="Stream is offline",t.INITIALIZING="Stream is initializing",t.BOOTING="Stream is booting",t.WAITING="Stream is waiting for data"}(j||(j={}));class V extends E{onState(t){this.log("onState",t).info()}onClose(t){this.log("onClose").info()}onMetadata(t){this.log(v.stringify(t)).info()}get url(){return this._ws.url}get streamState(){return this._streamState}get connectParams(){return this._connectParams}get metadata(){return this._metadata}get closed(){return this._ws.closed}constructor(t){super();const e=new Map;for(const t of Object.values(j))e.set(t,t);e.set("Stream status is unknown?!",j.UNKNOWN),this._connectParams=t,this._streamState=j.UNKNOWN,this._ws=new A(M.buildURL(M.Type.META,t)),this._ws.onClose=t=>this.close(t),this._ws.onMessage=s=>{var i;try{const r=JSON.parse(s);if(r.error){const s=e.get(r.error);return void(s?this.onState(this._streamState=s):this.close({type:"StreamMetadataError",name:r.error,stream:t.streamName}))}this._metadata=new L,this._metadata.type=r.type,this._metadata.width=r.width,this._metadata.height=r.height,this._metadata.sources.clear();for(const t of r.source||[])this._metadata.sources.set(t.hrn,t);const n=[];if(this._metadata.tracks.clear(),null===(i=r.meta)||void 0===i?void 0:i.tracks)for(const[t,e]of v.iterableEntries(r.meta.tracks)){switch(e.name=t,e.type=e.type.toLowerCase(),e.type){case"audio":this._metadata.audios.push(e);continue;case"video":this._metadata.videos.push(e);continue;case"meta":e.type=D.DATA,this._metadata.datas.push(e);break;default:this.log(`Unknown track type ${e.type}`).warn()}n.push(e)}this._addSortedTrack(this._metadata.audios,this._metadata.tracks),this._addSortedTrack(this._metadata.videos,this._metadata.tracks);for(const t of n)this._metadata.tracks.set(t.idx,t);this.onState(this._streamState=j.ONLINE)}catch(t){return void this.log(v.stringify(t)).error()}this.onMetadata(this._metadata)}}close(t){this._ws.onClose!==v.EMPTY_FUNCTION&&(this._ws.onClose=v.EMPTY_FUNCTION,this._ws.close(),this._metadata=new L,this.onClose(t))}_addSortedTrack(t,e){t.sort(W);for(let s=0;s<t.length;++s){const i=t[s];e.set(i.idx,i),s&&(i.up=t[s-1],t[s-1].down=i)}}}function q(t){return"send"in t}var H={},Y={exports:{}},G=Y.exports={v:[{name:"version",reg:/^(\d*)$/}],o:[{name:"origin",reg:/^(\S*) (\d*) (\d*) (\S*) IP(\d) (\S*)/,names:["username","sessionId","sessionVersion","netType","ipVer","address"],format:"%s %s %d %s IP%d %s"}],s:[{name:"name"}],i:[{name:"description"}],u:[{name:"uri"}],e:[{name:"email"}],p:[{name:"phone"}],z:[{name:"timezones"}],r:[{name:"repeats"}],t:[{name:"timing",reg:/^(\d*) (\d*)/,names:["start","stop"],format:"%d %d"}],c:[{name:"connection",reg:/^IN IP(\d) (\S*)/,names:["version","ip"],format:"IN IP%d %s"}],b:[{push:"bandwidth",reg:/^(TIAS|AS|CT|RR|RS):(\d*)/,names:["type","limit"],format:"%s:%s"}],m:[{reg:/^(\w*) (\d*) ([\w/]*)(?: (.*))?/,names:["type","port","protocol","payloads"],format:"%s %d %s %s"}],a:[{push:"rtp",reg:/^rtpmap:(\d*) ([\w\-.]*)(?:\s*\/(\d*)(?:\s*\/(\S*))?)?/,names:["payload","codec","rate","encoding"],format:function(t){return t.encoding?"rtpmap:%d %s/%s/%s":t.rate?"rtpmap:%d %s/%s":"rtpmap:%d %s"}},{push:"fmtp",reg:/^fmtp:(\d*) ([\S| ]*)/,names:["payload","config"],format:"fmtp:%d %s"},{name:"control",reg:/^control:(.*)/,format:"control:%s"},{name:"rtcp",reg:/^rtcp:(\d*)(?: (\S*) IP(\d) (\S*))?/,names:["port","netType","ipVer","address"],format:function(t){return null!=t.address?"rtcp:%d %s IP%d %s":"rtcp:%d"}},{push:"rtcpFbTrrInt",reg:/^rtcp-fb:(\*|\d*) trr-int (\d*)/,names:["payload","value"],format:"rtcp-fb:%s trr-int %d"},{push:"rtcpFb",reg:/^rtcp-fb:(\*|\d*) ([\w-_]*)(?: ([\w-_]*))?/,names:["payload","type","subtype"],format:function(t){return null!=t.subtype?"rtcp-fb:%s %s %s":"rtcp-fb:%s %s"}},{push:"ext",reg:/^extmap:(\d+)(?:\/(\w+))?(?: (urn:ietf:params:rtp-hdrext:encrypt))? (\S*)(?: (\S*))?/,names:["value","direction","encrypt-uri","uri","config"],format:function(t){return"extmap:%d"+(t.direction?"/%s":"%v")+(t["encrypt-uri"]?" %s":"%v")+" %s"+(t.config?" %s":"")}},{name:"extmapAllowMixed",reg:/^(extmap-allow-mixed)/},{push:"crypto",reg:/^crypto:(\d*) ([\w_]*) (\S*)(?: (\S*))?/,names:["id","suite","config","sessionConfig"],format:function(t){return null!=t.sessionConfig?"crypto:%d %s %s %s":"crypto:%d %s %s"}},{name:"setup",reg:/^setup:(\w*)/,format:"setup:%s"},{name:"connectionType",reg:/^connection:(new|existing)/,format:"connection:%s"},{name:"mid",reg:/^mid:([^\s]*)/,format:"mid:%s"},{name:"msid",reg:/^msid:(.*)/,format:"msid:%s"},{name:"ptime",reg:/^ptime:(\d*(?:\.\d*)*)/,format:"ptime:%d"},{name:"maxptime",reg:/^maxptime:(\d*(?:\.\d*)*)/,format:"maxptime:%d"},{name:"direction",reg:/^(sendrecv|recvonly|sendonly|inactive)/},{name:"icelite",reg:/^(ice-lite)/},{name:"iceUfrag",reg:/^ice-ufrag:(\S*)/,format:"ice-ufrag:%s"},{name:"icePwd",reg:/^ice-pwd:(\S*)/,format:"ice-pwd:%s"},{name:"fingerprint",reg:/^fingerprint:(\S*) (\S*)/,names:["type","hash"],format:"fingerprint:%s %s"},{push:"candidates",reg:/^candidate:(\S*) (\d*) (\S*) (\d*) (\S*) (\d*) typ (\S*)(?: raddr (\S*) rport (\d*))?(?: tcptype (\S*))?(?: generation (\d*))?(?: network-id (\d*))?(?: network-cost (\d*))?/,names:["foundation","component","transport","priority","ip","port","type","raddr","rport","tcptype","generation","network-id","network-cost"],format:function(t){var e="candidate:%s %d %s %d %s %d typ %s";return e+=null!=t.raddr?" raddr %s rport %d":"%v%v",e+=null!=t.tcptype?" tcptype %s":"%v",null!=t.generation&&(e+=" generation %d"),e+=null!=t["network-id"]?" network-id %d":"%v",e+=null!=t["network-cost"]?" network-cost %d":"%v"}},{name:"endOfCandidates",reg:/^(end-of-candidates)/},{name:"remoteCandidates",reg:/^remote-candidates:(.*)/,format:"remote-candidates:%s"},{name:"iceOptions",reg:/^ice-options:(\S*)/,format:"ice-options:%s"},{push:"ssrcs",reg:/^ssrc:(\d*) ([\w_-]*)(?::(.*))?/,names:["id","attribute","value"],format:function(t){var e="ssrc:%d";return null!=t.attribute&&(e+=" %s",null!=t.value&&(e+=":%s")),e}},{push:"ssrcGroups",reg:/^ssrc-group:([\x21\x23\x24\x25\x26\x27\x2A\x2B\x2D\x2E\w]*) (.*)/,names:["semantics","ssrcs"],format:"ssrc-group:%s %s"},{name:"msidSemantic",reg:/^msid-semantic:\s?(\w*) (\S*)/,names:["semantic","token"],format:"msid-semantic: %s %s"},{push:"groups",reg:/^group:(\w*) (.*)/,names:["type","mids"],format:"group:%s %s"},{name:"rtcpMux",reg:/^(rtcp-mux)/},{name:"rtcpRsize",reg:/^(rtcp-rsize)/},{name:"sctpmap",reg:/^sctpmap:([\w_/]*) (\S*)(?: (\S*))?/,names:["sctpmapNumber","app","maxMessageSize"],format:function(t){return null!=t.maxMessageSize?"sctpmap:%s %s %s":"sctpmap:%s %s"}},{name:"xGoogleFlag",reg:/^x-google-flag:([^\s]*)/,format:"x-google-flag:%s"},{push:"rids",reg:/^rid:([\d\w]+) (\w+)(?: ([\S| ]*))?/,names:["id","direction","params"],format:function(t){return t.params?"rid:%s %s %s":"rid:%s %s"}},{push:"imageattrs",reg:new RegExp("^imageattr:(\\d+|\\*)[\\s\\t]+(send|recv)[\\s\\t]+(\\*|\\[\\S+\\](?:[\\s\\t]+\\[\\S+\\])*)(?:[\\s\\t]+(recv|send)[\\s\\t]+(\\*|\\[\\S+\\](?:[\\s\\t]+\\[\\S+\\])*))?"),names:["pt","dir1","attrs1","dir2","attrs2"],format:function(t){return"imageattr:%s %s %s"+(t.dir2?" %s %s":"")}},{name:"simulcast",reg:new RegExp("^simulcast:(send|recv) ([a-zA-Z0-9\\-_~;,]+)(?:\\s?(send|recv) ([a-zA-Z0-9\\-_~;,]+))?$"),names:["dir1","list1","dir2","list2"],format:function(t){return"simulcast:%s %s"+(t.dir2?" %s %s":"")}},{name:"simulcast_03",reg:/^simulcast:[\s\t]+([\S+\s\t]+)$/,names:["value"],format:"simulcast: %s"},{name:"framerate",reg:/^framerate:(\d+(?:$|\.\d+))/,format:"framerate:%s"},{name:"sourceFilter",reg:/^source-filter: *(excl|incl) (\S*) (IP4|IP6|\*) (\S*) (.*)/,names:["filterMode","netType","addressTypes","destAddress","srcList"],format:"source-filter: %s %s %s %s %s"},{name:"bundleOnly",reg:/^(bundle-only)/},{name:"label",reg:/^label:(.+)/,format:"label:%s"},{name:"sctpPort",reg:/^sctp-port:(\d+)$/,format:"sctp-port:%s"},{name:"maxMessageSize",reg:/^max-message-size:(\d+)$/,format:"max-message-size:%s"},{push:"tsRefClocks",reg:/^ts-refclk:([^\s=]*)(?:=(\S*))?/,names:["clksrc","clksrcExt"],format:function(t){return"ts-refclk:%s"+(null!=t.clksrcExt?"=%s":"")}},{name:"mediaClk",reg:/^mediaclk:(?:id=(\S*))? *([^\s=]*)(?:=(\S*))?(?: *rate=(\d+)\/(\d+))?/,names:["id","mediaClockName","mediaClockValue","rateNumerator","rateDenominator"],format:function(t){var e="mediaclk:";return e+=null!=t.id?"id=%s %s":"%v%s",e+=null!=t.mediaClockValue?"=%s":"",e+=null!=t.rateNumerator?" rate=%s":"",e+=null!=t.rateDenominator?"/%s":""}},{name:"keywords",reg:/^keywds:(.+)$/,format:"keywds:%s"},{name:"content",reg:/^content:(.+)/,format:"content:%s"},{name:"bfcpFloorCtrl",reg:/^floorctrl:(c-only|s-only|c-s)/,format:"floorctrl:%s"},{name:"bfcpConfId",reg:/^confid:(\d+)/,format:"confid:%s"},{name:"bfcpUserId",reg:/^userid:(\d+)/,format:"userid:%s"},{name:"bfcpFloorId",reg:/^floorid:(.+) (?:m-stream|mstrm):(.+)/,names:["id","mStream"],format:"floorid:%s mstrm:%s"},{push:"invalid",names:["value"]}]};Object.keys(G).forEach((function(t){G[t].forEach((function(t){t.reg||(t.reg=/(.*)/),t.format||(t.format="%s")}))}));var J=Y.exports;!function(t){var e=function(t){return String(Number(t))===t?Number(t):t},s=function(t,s,i){var r=t.name&&t.names;t.push&&!s[t.push]?s[t.push]=[]:r&&!s[t.name]&&(s[t.name]={});var n=t.push?{}:r?s[t.name]:s;!function(t,s,i,r){if(r&&!i)s[r]=e(t[1]);else for(var n=0;n<i.length;n+=1)null!=t[n+1]&&(s[i[n]]=e(t[n+1]))}(i.match(t.reg),n,t.names,t.name),t.push&&s[t.push].push(n)},i=J,r=RegExp.prototype.test.bind(/^([a-z])=(.*)/);t.parse=function(t){var e={},n=[],o=e;return t.split(/(\r\n|\r|\n)/).filter(r).forEach((function(t){var e=t[0],r=t.slice(2);"m"===e&&(n.push({rtp:[],fmtp:[]}),o=n[n.length-1]);for(var a=0;a<(i[e]||[]).length;a+=1){var h=i[e][a];if(h.reg.test(r))return s(h,o,r)}})),e.media=n,e};var n=function(t,s){var i=s.split(/=(.+)/,2);return 2===i.length?t[i[0]]=e(i[1]):1===i.length&&s.length>1&&(t[i[0]]=void 0),t};t.parseParams=function(t){return t.split(/;\s?/).reduce(n,{})},t.parseFmtpConfig=t.parseParams,t.parsePayloads=function(t){return t.toString().split(" ").map(Number)},t.parseRemoteCandidates=function(t){for(var s=[],i=t.split(" ").map(e),r=0;r<i.length;r+=3)s.push({component:i[r],ip:i[r+1],port:i[r+2]});return s},t.parseImageAttributes=function(t){return t.split(" ").map((function(t){return t.substring(1,t.length-1).split(",").reduce(n,{})}))},t.parseSimulcastStreamList=function(t){return t.split(";").map((function(t){return t.split(",").map((function(t){var s,i=!1;return"~"!==t[0]?s=e(t):(s=e(t.substring(1,t.length)),i=!0),{scid:s,paused:i}}))}))}}(H);var K=J,Z=/%[sdv%]/g,X=function(t){var e=1,s=arguments,i=s.length;return t.replace(Z,(function(t){if(e>=i)return t;var r=s[e];switch(e+=1,t){case"%%":return"%";case"%s":return String(r);case"%d":return Number(r);case"%v":return""}}))},Q=function(t,e,s){var i=[t+"="+(e.format instanceof Function?e.format(e.push?s:s[e.name]):e.format)];if(e.names)for(var r=0;r<e.names.length;r+=1){var n=e.names[r];e.name?i.push(s[e.name][n]):i.push(s[e.names[r]])}else i.push(s[e.name]);return X.apply(null,i)},tt=["v","o","s","i","u","e","p","c","b","t","r","z","a"],et=["i","c","b","a"],st=function(t,e){e=e||{},null==t.version&&(t.version=0),null==t.name&&(t.name=" "),t.media.forEach((function(t){null==t.payloads&&(t.payloads="")}));var s=e.outerOrder||tt,i=e.innerOrder||et,r=[];return s.forEach((function(e){K[e].forEach((function(s){s.name in t&&null!=t[s.name]?r.push(Q(e,s,t)):s.push in t&&null!=t[s.push]&&t[s.push].forEach((function(t){r.push(Q(e,s,t))}))}))})),t.media.forEach((function(t){r.push(Q("m",K.m[0],t)),i.forEach((function(e){K[e].forEach((function(s){s.name in t&&null!=t[s.name]?r.push(Q(e,s,t)):s.push in t&&null!=t[s.push]&&t[s.push].forEach((function(t){r.push(Q(e,s,t))}))}))}))})),r.join("\r\n")+"\r\n"},it=H.parse,rt=H.parseParams;const nt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent);class ot extends E{onOpen(t){this.log("onOpen").info()}onClose(t){t?this.log("onClose",t).error():this.log("onClose").info()}get opened(){return!(!this._peerConnection||this._peerConnection.ontrack!==v.EMPTY_FUNCTION)}get closed(){return this._closed}get stream(){return this._stream}get streamName(){return this._streamName}get codecs(){return this._codecs}constructor(t,e){super(),this._closed=!1,this._streamName=t.streamName,this._endPoint=t.endPoint,this._stream=e,this._connectionInfosTime=0,this._codecs=new Set}connectionInfos(e=1e3){return t(this,void 0,void 0,(function*(){if(!this._peerConnection)return Promise.reject("Not connected");if(!this._connectionInfos||v.time()-e>this._connectionInfosTime){const t=yield this._peerConnection.getStats(null);this._connectionInfos={inputs:{},outputs:{}};const e=new Map,s=new Map;for(const i of t.values())switch(i.type){case"track":e.set(i.id,i);break;case"outbound-rtp":s.set(i.trackId,i),this._connectionInfos.outputs["audio"===(i.kind||i.mediaType)?"audio":"video"]=i;break;case"inbound-rtp":s.set(i.trackId,i),this._connectionInfos.inputs["audio"===(i.kind||i.mediaType)?"audio":"video"]=i;break;case"candidate-pair":if(null!=i.selected){if(!i.selected)continue}else if(null!=i.nominated&&!i.nominated)continue;this._connectionInfos.candidate=i}for(const[t,i]of e){const e=s.get(t);e&&Object.assign(e,Object.assign(Object.assign({},i),e))}this._connectionInfosTime=v.time()}return this._connectionInfos}))}close(t){if(this._closed)return;this._closed=!0,this._clearPeerConnectionIdleTimeout();const e=this._peerConnection;e&&(this._peerConnection=void 0,e.getReceivers().forEach((t=>t.track&&t.track.stop())),e.getSenders().forEach((t=>t.track&&t.track.stop())),e.close()),this._stream&&this._stream.getTracks().forEach((t=>t.stop())),this.onClose(t)}_open(t){if(!t){const e=new C(this._endPoint,443).domain;t={urls:["turn:"+e+":3478?transport=tcp","turn:"+e+":3478"],username:"ceeblue",credential:"ceeblue"}}try{this._peerConnection=new RTCPeerConnection({iceServers:[t]})}catch(t){return void this.close({type:"ConnectorError",name:"RTCPeerConnection failed",detail:v.stringify(t)})}if(this._peerConnection.onconnectionstatechange=t=>{const e=t.target;if(e){const t=null==e?void 0:e.connectionState;switch(this.log(`Peer connection state: ${t}`).info(),t){case"connected":case"connecting":this._clearPeerConnectionIdleTimeout();break;case"disconnected":case"failed":this.log(`Peer connection state: ${t}`).warn(),this._startPeerConnectionIdleTimeout();break;case"closed":this.log(`Peer connection state: ${t}`).warn(),this.close()}}},this._stream)for(const t of this._stream.getTracks())this._peerConnection.addTrack(t);else this._peerConnection.ontrack=t=>{this._stream=t.streams[0],this._tryToOpen()};if(nt)if(this._stream){const t=this._peerConnection.getTransceivers();for(const e of t)"audio"!==e.receiver.track.kind&&"video"!==e.receiver.track.kind||(e.direction="sendonly")}else this._peerConnection.addTransceiver("audio",{direction:"recvonly"}),this._peerConnection.addTransceiver("video",{direction:"recvonly"});let e;this._peerConnection.createOffer({offerToReceiveAudio:!this._stream,offerToReceiveVideo:!this._stream}).then((t=>{if(this._peerConnection)return t.sdp=e=t.sdp?function(t){const e=it(t);for(const t of e.media)if("audio"===t.type){const e=[];for(const s of t.rtp)"opus"===s.codec&&e.push(s.payload);if(!e.length)continue;for(const s of t.fmtp)if(e.includes(s.payload)){const t=rt(s.config);t.stereo=1,s.config="";for(const e in t)s.config+=(s.config?";":"")+e+"="+t[e]}}return st(e)}(t.sdp):"",this.log(`Offer\r\n${e}`).debug(),this._peerConnection.setLocalDescription(t)})).then((t=>{if(this._peerConnection)return e?this._sip(e):Promise.reject("invalid empty sdp offer")})).then((t=>{if(t&&this._peerConnection)return this.log(`Answer\r\n${t}`).debug(),this.updateCodecs(t),this._peerConnection.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:t}))})).then((()=>this._tryToOpen())).catch((t=>this.close({type:"ConnectorError",name:"SIP failed",detail:v.stringify(t)})))}updateCodecs(t){const e=it(t);for(const t of e.media)for(const e of t.rtp)this._codecs.add(e.codec.toLowerCase())}_tryToOpen(){this._stream&&this._peerConnection&&this._peerConnection.ontrack!==v.EMPTY_FUNCTION&&(this._peerConnection.ontrack=v.EMPTY_FUNCTION,this.onOpen(this._stream))}_startPeerConnectionIdleTimeout(){this._peerConnectionIdleTimeout||(this._peerConnectionIdleTimeout=setTimeout((()=>{this.log("Peer connection idle timeout!").error(),this.close({type:"ConnectorError",name:"Connection idle"})}),15e3))}_clearPeerConnectionIdleTimeout(){this._peerConnectionIdleTimeout&&(clearTimeout(this._peerConnectionIdleTimeout),this._peerConnectionIdleTimeout=void 0)}}const at=3e4;class ht extends ot{onRTPProps(t){}onMediaReport(t){}onVideoBitrate(t,e){this.log(`onVideoBitrate ${v.stringify({video_bitrate:t,video_bitrate_constraint:e})}`).info()}onPlaying(t){this.log(`onPlaying ${v.stringify(t)}`).debug()}constructor(t,e){super(t,e),this._ws=new A(M.buildURL(M.Type.WEBRTC,t,"wss")),this._ws.onClose=t=>this.close(t),this._ws.onOpen=()=>{this._startReportWatchdog(),this._open(t.iceServer)},this._ws.onMessage=t=>{try{this._eventHandler(JSON.parse(t))}catch(t){this.log(`Invalid signaling message, ${v.stringify(t)}`).warn()}}}setRTPProps(t,e){this.send("rtp_props",{nack:t,drop:e})}setVideoBitrate(t){this.send("video_bitrate",{video_bitrate:t})}setTracks(t){this.send("tracks",t)}send(t,e){try{this.log(`Command ${t} ${v.stringify(e)}`).info(),this._ws.send(JSON.stringify(Object.assign({type:t},e)))}catch(t){this.log(v.stringify(t)).error()}}close(t){this._ws.onClose!==v.EMPTY_FUNCTION&&(this._ws.onClose=v.EMPTY_FUNCTION,this._ws.close(),this._clearReportWatchdog(),this._promise&&(this._promise(Error("closing")),this._promise=void 0),super.close(t))}_sip(t){return new Promise(((e,s)=>{this._promise=t=>{t instanceof Error?s(t):e(t)},this._ws.send(JSON.stringify({type:"offer_sdp",offer_sdp:t}))}))}_eventHandler(t){var e,s;switch(this.log(`EventHandler ${v.stringify(t,{recursion:2})}`).debug(),t.type){case"on_answer_sdp":if(!0!==t.result)return void this.close({type:"ConnectorError",name:"Access denied"});this._promise&&this._promise(t.answer_sdp);break;case"on_error":if(!this.opened)return void this.close({type:"ConnectorError",name:"Connection failed",detail:v.stringify(t)});this.log(v.stringify(t)).warn();break;case"on_video_bitrate":this.onVideoBitrate(t.video_bitrate,t.video_bitrate_constraint);break;case"on_stop":return this.log("on_stop").info(),void this.close();case"on_track_drop":{const i=null!==(e=t.mediatype)&&void 0!==e?e:"?",r=null!==(s=t.track)&&void 0!==s?s:"?";this.log(`${i} track #${r} dropped`).warn();break}case"on_rtp_props":this.onRTPProps(t);break;case"on_media_receive":this._reportReceivedTimestamp=v.time(),t.stats.loss_perc&&!t.stats.loss_num&&(t.stats.loss_perc=0),this.onMediaReport(t);break;case"on_seek":break;case"on_time":this._reportReceivedTimestamp=v.time(),this.onPlaying(t);break;default:this.log(`Unhandled event: ${t.type}`).warn()}}_startReportWatchdog(){this._reportReceivedTimestamp=v.time(),this._reportWatchdogInterval=setInterval((()=>{const t=this._reportReceivedTimestamp?v.time()-this._reportReceivedTimestamp:at;t>=1e4&&this.log(`No updates received for the last ${(t/1e3).toFixed(1)}s`).warn(),t>=at&&this.close({type:"ConnectorError",name:"Connection idle"})}),5e3)}_clearReportWatchdog(){this._reportWatchdogInterval&&(clearInterval(this._reportWatchdogInterval),this._reportWatchdogInterval=void 0)}}class ct extends ot{constructor(t,e){super(t,e),this._url=M.buildURL(M.Type.WEBRTC,t,"https"),this._fetch=new AbortController,setTimeout((()=>{this._open(t.iceServer)}),0)}close(t){this._fetch.abort(),super.close(t)}_sip(e){return t(this,void 0,void 0,(function*(){const t=yield fetch(this._url,{method:"POST",body:e,headers:{"Content-Type":"application/sdp"},signal:this._fetch.signal});return t.ok?t.text():Promise.reject(`HTTP ${t.status} ${t.statusText} status`)}))}}class lt extends E{onClose(t){this.log("onClose").info()}onData(t,e,s){this.log(`Data received on track ${t} at ${e}: ${v.stringify(s)}`).info()}get url(){return this._url}get tracks(){return[...this._tracks]}set tracks(t){this._tracks=[...t],this._sendTracks()}get closed(){return!this._ws||this._ws.closed}constructor(t){super(),this._url=M.buildURL(M.Type.DATA,t).toString(),this._tracks=Array(),this._ws=new A,this._ws.onOpen=()=>this._sendTracks(),this._ws.onClose=t=>this.onClose(t),this._ws.onMessage=t=>{let e;try{if(e=JSON.parse(t),e.error)throw Error(e.error)}catch(t){return void this.log(v.stringify(t)).error()}"time"in e&&"track"in e&&"data"in e?this.onData(e.track,e.time,e.data):"on_time"!==e.type&&this.log(`Internal JSON: ${v.stringify(e)}`).debug()}}close(t){this._ws.close(t)}_sendTracks(){this._tracks.length?this._ws.closed?this._ws.open(this._url):this._ws.opened&&this._ws.send(JSON.stringify({type:"tracks",tracks:this._tracks.join(",")})):this.close()}}class ut extends T{get upDelay(){return this._upDelay}get learningUpStep(){return this._learningUpStep}get maximumUpDelay(){return this._maximumUpDelay}constructor(t){super();const e=Object.assign({learningUpStep:1400,maximumUpDelay:28e3},t);this._learningUpStep=e.learningUpStep,this._maximumUpDelay=e.maximumUpDelay,this._upDelay=0,this._testTime=0}reset(){this._upDelay=0,this._mTrack=void 0}compute(t,e,s){var i;const r=null!==(i=e.video)&&void 0!==i?i:e.audio;if(null==r)return this._mTrack=void 0,!1;const n=v.time();if(!(this._mTrack&&this._mTrack.idx===r||(this._appreciationTime=void 0,this._testTime=n,this._mTrack=t.tracks.get(r),this._mTrack)))return this.log(`Can't find track ${r} absent from metadata`).error(),!1;const o=r===e.video?s.video:s.audio;if(!o)return this.log(`Can't compute ${this._mTrack.type} track ${this._mTrack.idx} without statistics`).error(),!1;const a=this._downBitrate(n-this._testTime,this._mTrack,o);if(a)this._appreciationTime=void 0;else{this._appreciationTime||(this._appreciationTime=n);const t=n-this._appreciationTime;if(!this._upBitrate(t,this._mTrack,o)||t<this._upDelay)return!1}let h=this.updateTrack(e.audio,t,a);if(h)e.audio=h.idx;else{if(h=this.updateTrack(e.video,t,a),!h)return!1;e.video=h.idx}return a&&(this._upDelay=Math.min(this._upDelay+this._learningUpStep,this._maximumUpDelay)),this.log(`${a?"DOWN":"UP"} from ${this._mTrack.type} track ${this._mTrack.idx} (${this._mTrack.maxbps}bps) to ${h.type} track ${h.idx} (${h.maxbps}bps)`).info(),!0}updateTrack(t,e,s){if(null==t)return;const i=s?"down":"up",r=e.tracks.get(t);if(r)return r[i];this.log(`Can't find track ${t} from metadata`).error()}}class dt extends ut{constructor(t){super(t),this._keyFramesDecoded=0,this._lost=0,this._nackCount=0}_downBitrate(t,e,s){const i=s.packetsLost,r=s.nackCount;if(null==i)return this.log(`No packetsLost information in ${v.stringify(s)}`).warn(),!1;const n=t>0&&i>this._lost&&(!r||r>this._nackCount);return this._lost=i,this._nackCount=r||0,n}_upBitrate(t,e,s){if(e.type===D.AUDIO)return!0;if(t>1e4)return!0;const i=s.keyFramesDecoded;if(null==i)return!1;if(t){if(i>this._keyFramesDecoded){if(!this._keyFramesDecoded)return!0;this._keyFramesDecoded=0}}else this._keyFramesDecoded=i;return!1}}const mt=2e3;class _t extends E{onStart(t){this.log("onStart").info()}onStop(t){t?this.log("onStop",t).error():this.log("onStop").info()}onState(t){this.log("onState",t).info()}onPlaying(t){this.log(`onPlaying ${v.stringify(t)}`).debug()}onMetadata(t){this.log(v.stringify(t)).info()}onData(t,e,s){this.log(`Data received on track ${e} at ${t} : ${v.stringify(s)}`).info()}get streamName(){return this._connector?this._connector.streamName:""}get stream(){return this._connector&&this._connector.stream}get running(){return!!this._connector}get controller(){return this._controller}get connector(){return this._connector}get streamState(){var t;return(null===(t=this._streamMetadata)||void 0===t?void 0:t.streamState)||j.UNKNOWN}get metadata(){return this._metadata}get playingInfos(){return this._playingInfos}get audioTrack(){return this._audioTrack}set audioTrack(t){if(this._audioTrackFixed=null!=t,this._audioTrackFixed&&this._audioTrack!==t&&(this._audioTrack=t,this._connector)){if(!this._controller)throw Error("Cannot set audioTrack without start a controllable session");this._controller.setTracks({audio:t})}}get videoTrack(){return this._videoTrack}set videoTrack(t){if(this._videoTrackFixed=null!=t,this._videoTrackFixed&&this._videoTrack!==t&&(this._videoTrack=t,this._connector)){if(!this._controller)throw Error("Cannot set videoTrack without start a controllable session");this._controller.setTracks({video:t})}}get data