jb4-f3d7ce01.js 9.8 KB

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