usePlay-78e60e4f.js 2.7 KB

1
  1. import{W as S,a as v,D as A,F as E,b as k,V as L,A as N,c as g,d as y,e as C}from"./audio_decoder_hard-ce29f1e5.js";import{r as R}from"./vue-18ee037d.js";import"./dayjs-4778c158.js";import{aw as T}from"./index-421a14fe.js";function z(){const a=R([]);if(location.protocol==="http:"||location.protocol==="https:")return{webrtc:!1,streamList:a,changeStreamList:e=>{a.length=0,a.push(...e)},delItem:e=>{a[e].path="",delete a[e].item,delete a[e].stream},closeAll:()=>{a.length=0},updateItem:async(e,t)=>{let o,n;typeof e!="string"?(o=e,n=`${e.ID}/${e.DeviceID}`):n=e,a[t].path=n,a[t].item=o}};let w;const l=new Map;let r,i;const D=async()=>{const e=a.filter(t=>t.path);for(const t of e)if(l.has(t.path))t.stream||(t.stream=l.get(t.path));else{const o=new S(t.path);r||await b(),r.addStream(o),t.stream=o,l.set(t.path,t.stream)}if(e.length){const t=new Set(e.map(n=>n.path)),o=await i.createOffer();await i.setLocalDescription(o),w.send(JSON.stringify({type:"subscribe",offer:o.sdp,streamList:[...t]}))}else r&&(r.close(),r=null,l.clear())},b=async()=>new Promise(e=>{r=new v(T("webrtc","/batch")),i=r.webrtc,w=i.createDataChannel("signal"),w.onmessage=async t=>{const o=JSON.parse(t.data);switch(o.type){case"answer":i.setRemoteDescription(new RTCSessionDescription(o));break;case"remove":r.deleteStream(o.streamPath),l.delete(o.streamPath);break;case"offer":await i.setRemoteDescription(new RTCSessionDescription(o));const n=await i.createAnswer();await i.setLocalDescription(n),w.send(JSON.stringify(n))}},w.onopen=e,i.ondatachannel=async t=>{const o=t.channel,n=l.get(o.label);if(n){let u,h;const p=new A(o),f=new E(p,k.PUSH);f.gotVideo=s=>d.decode(s),f.gotAudio=s=>m.decode(s);const d=new L,m=new N;await d.initialize(),await m.initialize(),f.on(g.VIDEO_ENCODER_CONFIG_CHANGED,s=>{d.configure(s)}),f.on(g.AUDIO_ENCODER_CONFIG_CHANGED,s=>{m.configure(s)}),d.on(y.Error,s=>{console.error(s),d.initialize()}),d.on(y.VideoFrame,s=>{if(!u){const c=new MediaStreamTrackGenerator({kind:"video"});n.videoTrack=c,u=c.writable.getWriter()}u.write(s).catch(c=>{console.error(c),d.close(),p.close()})}),m.on(C.Error,s=>{console.error(s),m.initialize()}),m.on(C.AudioFrame,s=>{if(!h){const c=new MediaStreamTrackGenerator({kind:"audio"});n.audioTrack=c,h=c.writable.getWriter()}h.write(s).catch(c=>{console.error(c),m.close(),p.close()})}),p.connect()}},r.connect()});return{webrtc:!0,changeStreamList:e=>{a.length=0,a.push(...e),D()},streamList:a,updateItem:async(e,t)=>{let o,n;typeof e!="string"?(o=e,n=`${e.ID}/${e.DeviceID}`):n=e,a[t].path=n,a[t].item=o,a[t].stream=l.get(n),await D()},delItem:async e=>{a[e].path="",delete a[e].item,delete a[e].stream,await D()},closeAll:()=>{r==null||r.close(),r=null,a.length=0,l.clear()}}}export{z as u};