1 |
- import{d as Z,k as _,s as B,w as ee,o as te,x as ae,X as ie,a6 as oe,t as re,A as se,f as ne,u as ce,a9 as de,a7 as le,P as ue}from"./vue-fae2b924.js";import{h as fe,B as he,i as pe,f as v,d as V,g as H,V as x,b as O,c as z,a as R,A as M,j as me,F as I,D as T,e as L,C as we}from"./canvas-14fb4930.js";import{ba as ge,bu as ye,ac as ve,V as Se}from"./radical-24ede98f.js";import{o as A}from"./index-bad227d6.js";class be extends fe{async _connect(){const o=await fetch(this.url,{...this.options.requestInit,signal:this.abortCtrl.signal});if(!o.body)throw new Error("no body");return o.body}}const $=442,F=444,W=480,j=448,Y=441,De=27,Ce=36,Ee=15,_e=144,Te=145;class Ae extends he{constructor(){super(...arguments),this.videoStreamType=0,this.audioStreamType=0,this.pts=0,this.dts=0,this.tmp8=new Uint8Array(4),this.dv=new DataView(this.tmp8.buffer),this.videoBuffer=[],this.videoBufferSize=0,this.currentPTS=0,this.startTime=0}async pull(){const o=this.tmp8,e=this.dv,t=this.source;switch(await t.read(o),e.getUint32(0)){case $:await t.read(9),await t.read(o.subarray(0,1));const d=e.getUint8(0)&7;await t.read(d);break;case F:await t.read(o.subarray(0,2)),this.decProgramStreamMap(await t.read(e.getUint16(0)));break;case W:if(await t.read(o.subarray(0,2)),this.demuxVideo(await t.read(e.getUint16(0))))return;break;case j:if(await t.read(o.subarray(0,2)),this.demuxAudio(await t.read(e.getUint16(0))))return;break;case Y:return;default:await t.read(o.subarray(0,2));const s=e.getUint16(0);await t.read(s)}return this.pull()}*demux(){const o=this.tmp8,e=this.dv;for(;;){yield o;const t=e.getUint32(0);switch(t.toString(16),t){case $:yield 9,yield o.subarray(0,1),yield e.getUint8(0)&7;break;case F:yield o.subarray(0,2);const s=yield e.getUint16(0);this.decProgramStreamMap(s);break;case W:yield o.subarray(0,2),this.demuxVideo(yield e.getUint16(0));break;case j:yield o.subarray(0,2),this.demuxAudio(yield e.getUint16(0));break;case Y:return;default:yield o.subarray(0,2),yield e.getUint16(0);break}}}demuxVideo(o){var a,d,s;const e=this.parsePESPacket(o),t=this.videoBuffer;if(!this.startTime)if(((a=this.videoDecoderConfig)==null?void 0:a.codec)=="hevc")if((e[4]&126)>>1===32)this.startTime=Date.now(),this.currentPTS=this.pts;else return!1;else if((e[4]&15)==7)this.startTime=Date.now(),this.currentPTS=this.pts;else return!1;if(this.currentPTS==this.pts)return e[4]&15,this.videoBufferSize+=e.length,t.push(e.slice()),!1;if(t.length&&this.currentPTS!=this.pts){let n=0,l="key";return((d=this.videoDecoderConfig)==null?void 0:d.codec)=="hevc"?(t[0][4]&126)>>1!=32&&(l="delta"):(t[0][4]&15)==1&&(l="delta"),(s=this.gotVideo)==null||s.call(this,{type:l,data:t.length==1?t[0]:t.reduce((f,g)=>(f.subarray(n).set(g),n+=g.length,f),new Uint8Array(this.videoBufferSize)),timestamp:(Date.now()-this.startTime)*1e3}),t.length=0,!0}return this.videoBufferSize+=e.length,t.push(e.slice()),this.currentPTS=this.pts,!1}demuxAudio(o){var t,a,d,s;const e=this.parsePESPacket(o);if(((t=this.audioDecoderConfig)==null?void 0:t.codec)=="aac"&&!((a=this.audioDecoderConfig)!=null&&a.description)){const n=pe(e.subarray(7));this.audioDecoderConfig={codec:"aac",description:n.audioSpecificConfig,sampleRate:n.sampleRate,numberOfChannels:n.channel},this.emit(v.AUDIO_ENCODER_CONFIG_CHANGED,this.audioDecoderConfig)}return(s=this.gotAudio)==null||s.call(this,{type:"key",data:((d=this.audioDecoderConfig)==null?void 0:d.codec)=="aac"?e.subarray(7):e,timestamp:this.dts,duration:0}),!0}parsePESPacket(o){if(o.length<4)throw new Error("Short buffer");const e=o[1],t=e>>7==1,a=(e&64)>>6==1,d=o[2];if(o.length<d+3)throw new Error("Short buffer");const s=o.subarray(3,3+d);return t&&s.length>4&&(this.pts=(s[0]&14)<<29|s[1]<<22|(s[2]&254)<<14|s[3]<<7|s[4]>>1,a&&s.length>9?this.dts=(s[5]&14)<<29|s[6]<<22|(s[7]&254)<<14|s[8]<<7|s[9]>>1:this.dts=this.pts),o.subarray(3+d)}decProgramStreamMap(o){const e=new DataView(o.buffer,o.byteOffset,o.byteLength),t=o.length;let a=2;const d=e.getUint16(a);a+=2,a+=d;let s=e.getUint16(a);for(a+=2;s>0&&!(t<=a+1);){const n=o[a];a++;const l=o[a];if(a++,l>=224&&l<=239?(this.videoStreamType=n,this.videoDecoderConfig={codec:{[De]:"avc",[Ce]:"hevc"}[n]||"unknown"},this.emit(v.VIDEO_ENCODER_CONFIG_CHANGED,this.videoDecoderConfig)):l>=192&&l<=223&&(this.audioStreamType=n,this.audioDecoderConfig={codec:{[Ee]:"aac",[_e]:"pcma",[Te]:"pcmu"}[n]||"unknown",numberOfChannels:1,sampleRate:8e3},this.audioDecoderConfig.codec!="aac"&&this.emit(v.AUDIO_ENCODER_CONFIG_CHANGED,this.audioDecoderConfig)),t<=a+1)break;const f=e.getUint16(a);a+=2,a+=f,s-=4+f}}}const Pe={class:"video-container"},ke=["srcObject"],Ue=Z({__name:"jb4",props:{videoShadow:{},streamPath:{},format:{},speed:{},pause:{type:Boolean},startTime:{}},emits:["postion","play"],setup(P,{emit:o}){const e=P,t=o;let a,d=0,s=0;const n=_();let l,f;const g=_(),p=typeof VideoDecoder=="function",S=_();let m=null,b=null,y=null;function N(){return typeof WebAssembly>"u"?!1:WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11]))}let u=k(p?V:N()?H:x),h=U(p?R:M),D=!1;B(()=>{n.value&&(n.value.addEventListener("playing",()=>{t("play",!0)}),n.value.addEventListener("pause",()=>{t("play","pause")}),n.value.addEventListener("ended",()=>{t("play","ended")}),n.value.addEventListener("error",()=>{t("play","error")}))});function k(r){const i=new r;return i.on(O.VideoCodecInfo,c=>{`${c.width}${c.height}`}),i.on(O.VideoFrame,c=>{f?f.write(c):m&&S.value&&(E.value=!1,m.writeVideo(c),c.close())}),i.on(O.Error,c=>{console.error(c),D=!1,u instanceof V&&(u=k(N()?H:x),u.initialize().then(()=>{y&&u.configure({...y})}))}),i}function U(r){const i=new r;return i.on(z.Error,c=>{console.error(c),i instanceof R&&(h=U(M),h.initialize().then(()=>{b&&h.configure({...b})}))}),i.on(z.AudioFrame,c=>{l?l.write(c):m&&(E.value=!1,m.writeAudio(c))}),i}function q(){h==null||h.close(),b=null}function K(){u==null||u.close(),y=null}B(async()=>{if(e.streamPath&&e.format)try{switch(a&&(q(),K(),await a.close()),h=U(p?R:M),u=k(p?V:x),await u.initialize(),await h.initialize(),e.format){case"ps":a=new L(`${A("ps","","ws")}/${e.streamPath}`),C(new Ae(a,T.PUSH));break;case"http-flv":a=new be(`${A("flv","")}/${e.streamPath}`),C(new I(a,T.PUSH));break;case"ws-flv":a=new L(`${A("flv","","ws")}/${e.streamPath}`),C(new I(a,T.PUSH));break;case"webrtc":const r=new RTCPeerConnection;r.ontrack=w=>{w.streams.length!==0&&(g.value=w.streams[0])},r.oniceconnectionstatechange=()=>{r.iceConnectionState},r.onicecandidate=w=>{w.candidate},r.addTransceiver("video",{direction:"recvonly"}),r.addTransceiver("audio",{direction:"recvonly"});const i=await r.createOffer();await r.setLocalDescription(i);const J=await(await fetch(`${A("webrtc","/play")}/${e.streamPath}`,{method:"POST",mode:"cors",cache:"no-cache",credentials:"include",redirect:"follow",referrerPolicy:"no-referrer",headers:{"Content-Type":"application/sdp"},body:i.sdp})).text();await r.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:J})),r.ondatachannel=w=>{const Q=w.channel,G=new me(Q);C(new I(G,T.PUSH)),G.connect()},a=r;return}await a.connect()}catch(r){ge.error({message:"播放失败",description:r instanceof Event?"连接失败":String(r)}),t("play","connect error")}});const X="cmd".split("").map(r=>r.charCodeAt(0));ee(()=>e.pause,r=>{e.format==="ws-flv"&&a.send(new Uint8Array([...X,r?1:2]))});function C(r){if(p&&typeof MediaStreamTrackGenerator=="function"){const i=new MediaStreamTrackGenerator({kind:"audio"});l=i.writable.getWriter(),i.onmute=()=>{t("play","audio mute")};const c=new MediaStreamTrackGenerator({kind:"video"});f=c.writable.getWriter(),c.onmute=()=>{t("play","videomute")},g.value=new MediaStream([i,c])}r.on(v.VIDEO_ENCODER_CONFIG_CHANGED,i=>{y={...i},delete y.description,u.state==="initialized"&&u.configure(y),!p&&S.value&&(m=new we(S.value))}),r.on(v.AUDIO_ENCODER_CONFIG_CHANGED,i=>{b=i;try{h.configure({...i})}catch(c){console.error(c)}}),r.gotVideo=i=>{try{if(u.state!=="configured"){console.warn("Video decoder not configured yet, waiting...");return}if(!D&&i.type!=="key"){console.warn("Waiting for keyframe...");return}i.type==="key"&&(D||(D=!0)),u.decode(i),d=i.timestamp,e.startTime!==void 0&&e.startTime!==null&&t("postion",d-s+e.startTime)}catch(c){console.error(c,i)}},r.gotAudio=i=>{if(h.state!=="configured"){console.warn("Audio decoder not configured yet, waiting...");return}h.decode(i)}}const E=_(!0);return te(()=>{var r;if(!p&&n.value){const i=document.createElement("canvas");i.style.width="100%",i.style.objectFit="cover",i.style.borderRadius="6px",e.videoShadow&&(i.style.boxShadow="4px 4px 8px 4px #dedede"),(r=n.value.parentElement)==null||r.replaceChild(i,n.value),S.value=i}ye(n.value,"canplay",()=>{E.value=!1})}),ae(()=>{a==null||a.close(),f&&f.close(),l&&l.close(),m&&(m=null)}),(r,i)=>(ie(),oe("div",Pe,[re(ne(ce(ve),{class:"loading",size:"large"},null,512),[[se,E.value]]),de("video",{ref_key:"videoEle",ref:n,class:le(["video",{videoShadow:r.videoShadow}]),srcObject:g.value,autoplay:""},null,10,ke),ue(r.$slots,"default",{},void 0,!0)]))}});const Me=Se(Ue,[["__scopeId","data-v-eef4a76a"]]);export{Me as V};
|