import{determineVideoPacketType as g}from"./codec-data.js";import{customAudioDecoders as u,customVideoDecoders as p}from"./custom-coder.js";import{EncodedPacketSink as h}from"./media-sink.js";import{assert as s}from"./misc.js";import{EncodedPacket as d}from"./packet.js";export class InputTrack{constructor(e){this._backing=e}isVideoTrack(){return this instanceof InputVideoTrack}isAudioTrack(){return this instanceof InputAudioTrack}get id(){return this._backing.getId()}get languageCode(){return this._backing.getLanguageCode()}get timeResolution(){return this._backing.getTimeResolution()}getFirstTimestamp(){return this._backing.getFirstTimestamp()}computeDuration(){return this._backing.computeDuration()}async computePacketStats(e=1/0){const r=new h(this);let t=1/0,i=-1/0,n=0,c=0;for await(const o of r.packets(void 0,void 0,{metadataOnly:!0})){if(n>=e&&o.timestamp>=i)break;t=Math.min(t,o.timestamp),i=Math.max(i,o.timestamp+o.duration),n++,c+=o.byteLength}return{packetCount:n,averagePacketRate:n?Number((n/(i-t)).toPrecision(16)):0,averageBitrate:n?Number((8*c/(i-t)).toPrecision(16)):0}}}export class InputVideoTrack extends InputTrack{constructor(e){super(e),this._backing=e}get type(){return"video"}get codec(){return this._backing.getCodec()}get codedWidth(){return this._backing.getCodedWidth()}get codedHeight(){return this._backing.getCodedHeight()}get rotation(){return this._backing.getRotation()}get displayWidth(){return this._backing.getRotation()%180===0?this._backing.getCodedWidth():this._backing.getCodedHeight()}get displayHeight(){return this._backing.getRotation()%180===0?this._backing.getCodedHeight():this._backing.getCodedWidth()}getColorSpace(){return this._backing.getColorSpace()}async hasHighDynamicRange(){const e=await this._backing.getColorSpace();return e.primaries==="bt2020"||e.primaries==="smpte432"||e.transfer==="pg"||e.transfer==="hlg"||e.matrix==="bt2020-ncl"}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{const e=await this._backing.getDecoderConfig();if(!e)return!1;const r=this._backing.getCodec();return s(r!==null),p.some(i=>i.supports(r,e))?!0:typeof VideoDecoder>"u"?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error("Error during decodability check:",e),!1}}async determinePacketType(e){if(!(e instanceof d))throw new TypeError("packet must be an EncodedPacket.");if(e.isMetadataOnly)throw new TypeError("packet must not be metadata-only to determine its type.");return this.codec===null?null:g(this,e)}}export class InputAudioTrack extends InputTrack{constructor(e){super(e),this._backing=e}get type(){return"audio"}get codec(){return this._backing.getCodec()}get numberOfChannels(){return this._backing.getNumberOfChannels()}get sampleRate(){return this._backing.getSampleRate()}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{const e=await this._backing.getDecoderConfig();if(!e)return!1;const r=this._backing.getCodec();return s(r!==null),u.some(t=>t.supports(r,e))||e.codec.startsWith("pcm-")?!0:typeof AudioDecoder>"u"?!1:(await AudioDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error("Error during decodability check:",e),!1}}async determinePacketType(e){if(!(e instanceof d))throw new TypeError("packet must be an EncodedPacket.");return this.codec===null?null:"key"}}
