zann-voice 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -75,6 +75,8 @@ voice.connect({
75
75
  | onError | function |是 | 监听错误问题 |
76
76
  | onMessage | function |是 | 监听websocket 发来的内容 |
77
77
  | onAuth | function | 是 | 监听到需要用户授权麦克风或者摄像头 |
78
+ | onMicChange | function | 否 | 监听麦克风变化,并返回 MediaStream |
79
+ | onCameraChange | function | 否 | 监听摄像头变化,并返回 MediaStream |
78
80
 
79
81
  媒体模式参数
80
82
  ``` markdown
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,d){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!d)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!d:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?d.call(o,s):d?d.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,_,u,l,w,p,S,W,V,g,y,m,T,M,k,Z,E;class b{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),_.set(this,!1),u.set(this,!1),l.set(this,0),w.set(this,5),p.set(this,""),S.set(this,{urls:""}),W.set(this,void 0),V.set(this,void 0),g.set(this,void 0),y.set(this,void 0),m.set(this,void 0),n(this,p,e,"f"),n(this,S,s,"f"),n(this,w,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:d,onMessage:C,onAuth:v}){if(t(this,_,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,u,!1,"f"),n(this,l,0,"f"),n(this,W,s,"f"),n(this,V,i,"f"),n(this,g,d,"f"),n(this,y,C,"f"),n(this,m,v,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,S,"f")]}),"f"),t(this,a,"f").onicecandidate=f=>{f.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:f.candidate}))},t(this,a,"f").ontrack=f=>t(this,V,"f").call(this,f),t(this,a,"f").onconnectionstatechange=()=>{const f=t(this,a,"f")?.connectionState;console.log("RTC connection state:",f),!t(this,u,"f")&&(f==="disconnected"||f==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",k).call(this))},t(this,h,"m",T).call(this),n(this,_,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopMic(){t(this,c,"f")&&t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopCamera(){t(this,c,"f")&&t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,u,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",Z).call(this),t(this,h,"m",E).call(this),n(this,_,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,_=new WeakMap,u=new WeakMap,l=new WeakMap,w=new WeakMap,p=new WeakMap,S=new WeakMap,W=new WeakMap,V=new WeakMap,g=new WeakMap,y=new WeakMap,m=new WeakMap,h=new WeakSet,T=function o(e=1e3){t(this,p,"f")&&(n(this,r,new WebSocket(t(this,p,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,l,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",k).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",M).call(this,s),t(this,r,"f").onerror=s=>t(this,g,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,u,"f")){if(t(this,l,"f")>=t(this,w,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,w,"f")}`),t(this,g,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,w,"f")}`));return}n(this,l,(s=t(this,l,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,l,"f")}/${t(this,w,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},M=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,W,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,y,"f").call(this,e);break}},k=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},Z=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},E=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")},module.exports=b;
1
+ "use strict";function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,w){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!w)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!w:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?w.call(o,s):w?w.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,g,p,u,_,m,V,y,M,k,T,S,f,l,Z,C,W,E,v;class N{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),g.set(this,!1),p.set(this,!1),u.set(this,0),_.set(this,5),m.set(this,""),V.set(this,{urls:""}),y.set(this,void 0),M.set(this,void 0),k.set(this,void 0),T.set(this,void 0),S.set(this,void 0),f.set(this,void 0),l.set(this,void 0),n(this,m,e,"f"),n(this,V,s,"f"),n(this,_,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:w,onMessage:b,onAuth:O,onMicChange:R,onCameraChange:P}){if(t(this,g,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,p,!1,"f"),n(this,u,0,"f"),n(this,y,s,"f"),n(this,M,i,"f"),n(this,k,w,"f"),n(this,T,b,"f"),n(this,S,O,"f"),n(this,f,R,"f"),n(this,l,P,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,V,"f")]}),"f"),t(this,a,"f").onicecandidate=d=>{d.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:d.candidate}))},t(this,a,"f").ontrack=d=>t(this,M,"f").call(this,d),t(this,a,"f").onconnectionstatechange=()=>{const d=t(this,a,"f")?.connectionState;console.log("RTC connection state:",d),!t(this,p,"f")&&(d==="disconnected"||d==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",W).call(this))},t(this,h,"m",Z).call(this),n(this,g,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",W).call(this),t(this,f,"f")&&t(this,f,"f").call(this,!0,e)}catch(e){t(this,S,"f").call(this,e),t(this,f,"f")&&t(this,f,"f").call(this,!1,null)}}stopMic(){t(this,c,"f")&&(t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)}),t(this,f,"f")&&t(this,f,"f").call(this,!1,null))}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",W).call(this),t(this,l,"f")&&t(this,l,"f").call(this,!0,e)}catch(e){t(this,S,"f").call(this,e),t(this,l,"f")&&t(this,l,"f").call(this,!1,null)}}stopCamera(){t(this,c,"f")&&(t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)}),t(this,l,"f")&&t(this,l,"f").call(this,!1,null))}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,p,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",E).call(this),t(this,h,"m",v).call(this),n(this,g,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,g=new WeakMap,p=new WeakMap,u=new WeakMap,_=new WeakMap,m=new WeakMap,V=new WeakMap,y=new WeakMap,M=new WeakMap,k=new WeakMap,T=new WeakMap,S=new WeakMap,f=new WeakMap,l=new WeakMap,h=new WeakSet,Z=function o(e=1e3){t(this,m,"f")&&(n(this,r,new WebSocket(t(this,m,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,u,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",W).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",C).call(this,s),t(this,r,"f").onerror=s=>t(this,k,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,p,"f")){if(t(this,u,"f")>=t(this,_,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,_,"f")}`),t(this,k,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,_,"f")}`));return}n(this,u,(s=t(this,u,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,u,"f")}/${t(this,_,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},C=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,y,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,T,"f").call(this,e);break}},W=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},E=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},v=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")},module.exports=N;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,d){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!d)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!d:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?d.call(o,s):d?d.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,_,u,l,w,p,S,W,V,g,y,m,T,M,k,Z,E;class b{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),_.set(this,!1),u.set(this,!1),l.set(this,0),w.set(this,5),p.set(this,""),S.set(this,{urls:""}),W.set(this,void 0),V.set(this,void 0),g.set(this,void 0),y.set(this,void 0),m.set(this,void 0),n(this,p,e,"f"),n(this,S,s,"f"),n(this,w,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:d,onMessage:C,onAuth:v}){if(t(this,_,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,u,!1,"f"),n(this,l,0,"f"),n(this,W,s,"f"),n(this,V,i,"f"),n(this,g,d,"f"),n(this,y,C,"f"),n(this,m,v,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,S,"f")]}),"f"),t(this,a,"f").onicecandidate=f=>{f.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:f.candidate}))},t(this,a,"f").ontrack=f=>t(this,V,"f").call(this,f),t(this,a,"f").onconnectionstatechange=()=>{const f=t(this,a,"f")?.connectionState;console.log("RTC connection state:",f),!t(this,u,"f")&&(f==="disconnected"||f==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",k).call(this))},t(this,h,"m",T).call(this),n(this,_,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopMic(){t(this,c,"f")&&t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",k).call(this)}catch(e){t(this,m,"f").call(this,e)}}stopCamera(){t(this,c,"f")&&t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)})}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,u,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",Z).call(this),t(this,h,"m",E).call(this),n(this,_,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,_=new WeakMap,u=new WeakMap,l=new WeakMap,w=new WeakMap,p=new WeakMap,S=new WeakMap,W=new WeakMap,V=new WeakMap,g=new WeakMap,y=new WeakMap,m=new WeakMap,h=new WeakSet,T=function o(e=1e3){t(this,p,"f")&&(n(this,r,new WebSocket(t(this,p,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,l,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",k).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",M).call(this,s),t(this,r,"f").onerror=s=>t(this,g,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,u,"f")){if(t(this,l,"f")>=t(this,w,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,w,"f")}`),t(this,g,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,w,"f")}`));return}n(this,l,(s=t(this,l,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,l,"f")}/${t(this,w,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},M=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,W,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,y,"f").call(this,e);break}},k=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},Z=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},E=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")};export{b as default};
1
+ function t(o,e,s,i){if(s==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?o!==e||!i:!e.has(o))throw new TypeError("Cannot read private member from an object whose class did not declare it");return s==="m"?i:s==="a"?i.call(o):i?i.value:e.get(o)}function n(o,e,s,i,w){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!w)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?o!==e||!w:!e.has(o))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?w.call(o,s):w?w.value=s:e.set(o,s),s}typeof SuppressedError=="function"&&SuppressedError;var h,a,r,c,g,p,u,_,m,V,y,M,k,T,S,f,l,Z,C,W,E,v;class N{constructor({wss:e,TURN:s,maxReconnect:i=5}){h.add(this),a.set(this,null),r.set(this,null),c.set(this,null),g.set(this,!1),p.set(this,!1),u.set(this,0),_.set(this,5),m.set(this,""),V.set(this,{urls:""}),y.set(this,void 0),M.set(this,void 0),k.set(this,void 0),T.set(this,void 0),S.set(this,void 0),f.set(this,void 0),l.set(this,void 0),n(this,m,e,"f"),n(this,V,s,"f"),n(this,_,i,"f")}async connect({media:e={audio:!0,video:!1},onOpen:s,onTrack:i,onError:w,onMessage:b,onAuth:O,onMicChange:R,onCameraChange:P}){if(t(this,g,"f")){console.warn("\u26A0\uFE0F \u5DF2\u7ECF\u5B58\u5728 RTC/WS\uFF0C\u4E0D\u91CD\u590D\u521B\u5EFA");return}n(this,p,!1,"f"),n(this,u,0,"f"),n(this,y,s,"f"),n(this,M,i,"f"),n(this,k,w,"f"),n(this,T,b,"f"),n(this,S,O,"f"),n(this,f,R,"f"),n(this,l,P,"f"),console.log("\u2728 Creating WebRTC + WebSocket..."),n(this,a,new RTCPeerConnection({iceServers:[{urls:"stun:stun.l.google.com:19302"},{urls:"stun:stun1.l.google.com:19302"},t(this,V,"f")]}),"f"),t(this,a,"f").onicecandidate=d=>{d.candidate&&t(this,r,"f")?.readyState===WebSocket.OPEN&&t(this,r,"f").send(JSON.stringify({type:"ice",data:d.candidate}))},t(this,a,"f").ontrack=d=>t(this,M,"f").call(this,d),t(this,a,"f").onconnectionstatechange=()=>{const d=t(this,a,"f")?.connectionState;console.log("RTC connection state:",d),!t(this,p,"f")&&(d==="disconnected"||d==="failed")&&(console.warn("RTC disconnected, retrying offer..."),t(this,h,"m",W).call(this))},t(this,h,"m",Z).call(this),n(this,g,!0,"f"),e.audio&&await this.startMic(),e.video&&await this.startCamera()}async startMic(){if(!t(this,a,"f")){console.warn("\u274C startMic \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getAudioTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({audio:!0});e.getAudioTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getAudioTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",W).call(this),t(this,f,"f")&&t(this,f,"f").call(this,!0,e)}catch(e){t(this,S,"f").call(this,e),t(this,f,"f")&&t(this,f,"f").call(this,!1,null)}}stopMic(){t(this,c,"f")&&(t(this,c,"f").getAudioTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)}),t(this,f,"f")&&t(this,f,"f").call(this,!1,null))}async startCamera(){if(!t(this,a,"f")){console.warn("\u274C startCamera \u9700\u8981\u5148\u8C03\u7528 create()");return}if(!t(this,c,"f")?.getVideoTracks().length)try{const e=await navigator.mediaDevices.getUserMedia({video:!0});e.getVideoTracks().forEach(s=>t(this,a,"f").addTrack(s,e)),t(this,c,"f")||n(this,c,new MediaStream,"f"),e.getVideoTracks().forEach(s=>t(this,c,"f").addTrack(s)),t(this,h,"m",W).call(this),t(this,l,"f")&&t(this,l,"f").call(this,!0,e)}catch(e){t(this,S,"f").call(this,e),t(this,l,"f")&&t(this,l,"f").call(this,!1,null)}}stopCamera(){t(this,c,"f")&&(t(this,c,"f").getVideoTracks().forEach(e=>{e.stop();const s=t(this,a,"f")?.getSenders().find(i=>i.track===e);s&&t(this,a,"f")?.removeTrack(s)}),t(this,l,"f")&&t(this,l,"f").call(this,!1,null))}destroy(){console.log("\u{1F6D1} Destroying ZannVoice..."),n(this,p,!0,"f"),this.stopMic(),this.stopCamera(),t(this,h,"m",E).call(this),t(this,h,"m",v).call(this),n(this,g,!1,"f")}}a=new WeakMap,r=new WeakMap,c=new WeakMap,g=new WeakMap,p=new WeakMap,u=new WeakMap,_=new WeakMap,m=new WeakMap,V=new WeakMap,y=new WeakMap,M=new WeakMap,k=new WeakMap,T=new WeakMap,S=new WeakMap,f=new WeakMap,l=new WeakMap,h=new WeakSet,Z=function o(e=1e3){t(this,m,"f")&&(n(this,r,new WebSocket(t(this,m,"f")),"f"),t(this,r,"f").onopen=()=>{console.log("\u2705 WS connected"),n(this,u,0,"f"),t(this,a,"f")?.connectionState!=="connected"&&t(this,h,"m",W).call(this)},t(this,r,"f").onmessage=s=>t(this,h,"m",C).call(this,s),t(this,r,"f").onerror=s=>t(this,k,"f").call(this,s),t(this,r,"f").onclose=()=>{var s;if(!t(this,p,"f")){if(t(this,u,"f")>=t(this,_,"f")){console.error(`\u274C WS \u8D85\u8FC7\u6700\u5927\u91CD\u8FDE\u6B21\u6570 ${t(this,_,"f")}`),t(this,k,"f")?.call(this,new Error(`WS exceeded max reconnects ${t(this,_,"f")}`));return}n(this,u,(s=t(this,u,"f"),s++,s),"f"),console.warn(`\u{1F504} WS disconnected, retrying in ${e}ms... [${t(this,u,"f")}/${t(this,_,"f")}]`),setTimeout(()=>t(this,h,"m",o).call(this,Math.min(e*2,1e4)),e)}})},C=async function(e){const s=JSON.parse(e.data);switch(s.type){case"answer":await t(this,a,"f")?.setRemoteDescription(s.data),t(this,y,"f").call(this);break;case"ice":await t(this,a,"f")?.addIceCandidate(s.data);break;case"offer":await t(this,a,"f")?.setRemoteDescription(s.data);const i=await t(this,a,"f")?.createAnswer();i&&(await t(this,a,"f")?.setLocalDescription(i),t(this,r,"f")?.send(JSON.stringify({type:"answer",data:i})));break;default:t(this,T,"f").call(this,e);break}},W=async function(){if(!t(this,a,"f")||!t(this,r,"f")||t(this,r,"f").readyState!==WebSocket.OPEN)return;const e=await t(this,a,"f").createOffer();await t(this,a,"f").setLocalDescription(e),t(this,r,"f").send(JSON.stringify({type:"offer",data:e}))},E=function(){t(this,a,"f")?.getSenders().forEach(e=>e.track?.stop()),t(this,a,"f")?.close(),n(this,a,null,"f")},v=function(){t(this,r,"f")&&t(this,r,"f").readyState===WebSocket.OPEN&&t(this,r,"f").close(),n(this,r,null,"f")};export{N as default};
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zann-voice",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.esm.js",
6
6
  "types": "types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -8,6 +8,8 @@ type voice = {
8
8
  onError: (event: any) => void;
9
9
  onMessage: (event: any) => void;
10
10
  onAuth: (event: any) => void;
11
+ onMicChange?: (state: boolean, event: MediaStream | null) => void;
12
+ onCameraChange?: (state: boolean, event: MediaStream | null) => void;
11
13
  };
12
14
  declare class ZannVoice {
13
15
  #private;
@@ -33,9 +35,11 @@ declare class ZannVoice {
33
35
  * @param onError 创建出现了问题
34
36
  * @param onMessage 监听websocket 发来的内容
35
37
  * @param onAuth 当需要授权时候发生
38
+ * @param onMicChange 监听麦克风变化 state 是否开启麦克风
39
+ * @param onCameraChange 监听摄像头变化 state 是否开启麦克风
36
40
  * @returns
37
41
  */
38
- connect({ media, onOpen, onTrack, onError, onMessage, onAuth, }: voice): Promise<void>;
42
+ connect({ media, onOpen, onTrack, onError, onMessage, onAuth, onMicChange, onCameraChange }: voice): Promise<void>;
39
43
  /**
40
44
  * 开启麦克风
41
45
  * @returns