@wetspace/wetrtc 3.0.1 → 3.0.2
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 +7 -5
- package/es/index.js +1 -1
- package/lib/index.js +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -16,12 +16,14 @@
|
|
|
16
16
|
|
|
17
17
|
## 2. 源码地址
|
|
18
18
|
|
|
19
|
-
[
|
|
19
|
+
[地址链接](https://gitee.com/wetspace/wetrtc)
|
|
20
20
|
|
|
21
|
-
## 3.
|
|
21
|
+
## 3. Chrome 对 RTC 的测试指标显示地址
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
**`chrome://webrtc-internals/`**:该地址就是当你开启了 WebRTC 的音视频传输,它就能显示一些通讯中的基本信息,可用于某些功能的调试
|
|
24
24
|
|
|
25
|
-
## 测试体验工具(音视频通讯)
|
|
25
|
+
## 4. 测试体验工具(音视频通讯)
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
[安装包地址(windows)](https://pan.baidu.com/s/10tniytcLmS1w7JFooT3Jsg?pwd=pov4)
|
|
28
|
+
|
|
29
|
+
<!-- 下载地址:[windows 版本]() -->
|
package/es/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useState,useRef,useEffect,useCallback}from"react";function __awaiter(e,o,a,c){return new(a=a||Promise)(function(n,t){function r(e){try{s(c.next(e))}catch(e){t(e)}}function i(e){try{s(c.throw(e))}catch(e){t(e)}}function s(e){var t;e.done?n(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(r,i)}s((c=c.apply(e,o||[])).next())})}const getUseAbleDevices=()=>__awaiter(void 0,void 0,void 0,function*(){return navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices?yield navigator.mediaDevices.enumerateDevices():(console.error("navigator.mediaDevices.enumerateDevices"),null)}),getMediaStream=(e="display",r)=>{const i="display"===e?"getDisplayMedia":"getUserMedia";return n=>__awaiter(void 0,void 0,void 0,function*(){let e=null;if(navigator.mediaDevices[i]){var t=Object.assign({video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,enableBackground:!1,suppressLocalAudioPlayback:!0}},n);try{e=yield navigator.mediaDevices[i](t)}catch(e){r&&r({name:e.name||"Error",message:e.message||"内部错误"})}}else r&&r({name:"CompatibleError",message:"不支持navigator.mediaDevices"});return e})},stopStreamTrack=e=>{e&&e.getTracks().forEach(e=>{e.stop()})},concatStream=e=>{let t=[];return e.forEach(e=>{t=[...t,...e.getTracks()]}),new MediaStream(t)},filterStream=(e,t)=>{let n=[];return e.forEach(e=>{n=[...n,...e.getTracks().filter(e=>t?e.kind===t:e)]}),new MediaStream(n)},getRtcPeerTracks=(e,t)=>{const n={receivers(){const t=[];return e.getReceivers().forEach(e=>{e.track&&t.push(e.track)}),t},senders(){const t=[];return e.getSenders().forEach(e=>{e.track&&t.push(e.track)}),t}};return t?n[t]():[...n.receivers(),...n.senders()]},listenerEvent=(e,t)=>(e.addEventListener("track",t.track),e.addEventListener("icecandidate",t.icecandidate),e.addEventListener("connectionstatechange",t.connectionstatechange),()=>{e.removeEventListener("track",t.track),e.removeEventListener("icecandidate",t.icecandidate),e.removeEventListener("connectionstatechange",t.connectionstatechange)}),SaveFileToLocal=(e,t)=>{e=URL.createObjectURL(e);const n=document.createElement("a");n.href=e,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.download=t,n.click(),URL.revokeObjectURL(e),document.body.removeChild(n)},streamToRecord=(e,t={audioBitsPerSecond:128e3,videoBitsPerSecond:25e5,mimeType:"video/webm"})=>{const n=t.mimeType?t.mimeType.split("/"):["webm"];let r=(new Date).getTime().toString();const i=MediaRecorder.isTypeSupported(t.mimeType||"");if(!i)throw new Error(t.mimeType+"类型不支持");const s=new MediaRecorder(e,t);s.addEventListener("dataavailable",e=>{SaveFileToLocal(e.data,r+"."+n[(null===n||void 0===n?void 0:n.length)-1])});const o=()=>{i&&s.stop()};return{start:()=>{i&&s.start()},pause:()=>{i&&s.pause()},save:e=>{i&&(r=e||(new Date).getTime().toString(),o())},stop:o,resume:()=>{i&&s.resume()}}},DEFAULT_WetOption={transDirect:"sendrecv",configuration:{iceServers:[{urls:"stun:stun.l.google.com:19302"}]},constraints:{video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,suppressLocalAudioPlayback:!0}}};class WetRTC{constructor(e){this.state="new",this.isUseAbles={audiooutput:!1,audioinput:!1,videoinput:!1},this.streamControl={close:e=>{const t={remote:()=>{stopStreamTrack(this.streamControl.remoteStreams),this.streamControl.remoteStreams=null},local:()=>{stopStreamTrack(this.streamControl.localStreams),this.streamControl.localStreams=null}};e?t[e]():(t.local(),t.remote())}},this.runByt={time:null,close:()=>{this.runByt.time&&clearInterval(this.runByt.time)},run:()=>{const i=this.EventMap.get("byts");let s=null;i&&0<i.length&&(null!==i&&void 0!==i&&i.forEach(e=>{e(0)}),this.runByt.time=setInterval(()=>{var e;null!==(e=this.RTCPeerIns)&&void 0!==e&&e.getStats().then(e=>{const t={bytesReceived:0};if(e.forEach(e=>{"transport"===e.type&&(t.bytesReceived+=e.bytesReceived)}),s){const n=s.bytesReceived,r=t.bytesReceived;null!==i&&void 0!==i&&i.forEach(e=>{e(Number(((r-n)/1024).toFixed(2)))}),s=Object.assign({},t)}else s=Object.assign({},t)})},1e3))}},this.EventMap=new Map([]),this.RTCPeerIns=null,this.removeListenFn=()=>{},this.WetOption=Object.assign({},DEFAULT_WetOption),this.WetOption=Object.assign(Object.assign({},this.WetOption),e)}runError(t){const e=this.EventMap.get("error");null!==e&&void 0!==e&&e.forEach(e=>{e(t)})}getUseAbleDevices(){return __awaiter(this,void 0,void 0,function*(){return yield getUseAbleDevices()})}addEventListener(e,t){const n=this.EventMap.get(e);n?n.find(e=>e===t)||(n.push(t),this.EventMap.set(e,n)):this.EventMap.set(e,[t])}removeEventListener(e,t){const n=this.EventMap.get(e);var r;n&&(r=n.filter(e=>e!==t),this.EventMap.set(e,r))}captureStream(){return __awaiter(this,void 0,void 0,function*(){const e=yield getUseAbleDevices();if(e){const n=e.map(e=>e.kind);this.isUseAbles.audioinput=n.includes("audioinput"),this.isUseAbles.videoinput=n.includes("videoinput"),this.isUseAbles.audiooutput=n.includes("audiooutput")}else this.isUseAbles.audioinput=!0,this.isUseAbles.videoinput=!0,this.isUseAbles.audiooutput=!0;var t;return this.isUseAbles.audioinput||this.isUseAbles.videoinput?((t=yield getMediaStream("user",e=>{this.runError(e)})({video:!!this.isUseAbles.videoinput&&this.WetOption.constraints.video,audio:!!this.isUseAbles.audioinput&&this.WetOption.constraints.audio}))&&(this.streamControl.localStreams=t),t):(this.runError({name:"StreamError",message:"音频输入设备,视频输入设备均不可用"}),null)})}connect(o){var a;return __awaiter(this,void 0,void 0,function*(){if(["sendrecv","sendonly"].includes(this.WetOption.transDirect)&&(!this.streamControl.localStreams&&!(yield this.captureStream())))return null;let n="new",r=this;Object.defineProperty(this,"state",{get(){return n},set(e){"connected"===(n=e)?r.runByt.run():["disconnected","closed","failed"].includes(e)&&r.runByt.close();const t=r.EventMap.get("state");null!==t&&void 0!==t&&t.forEach(e=>{e(n)})}});var e;if(this.state="new",this.RTCPeerIns=new RTCPeerConnection(this.WetOption.configuration),this.removeListenFn=listenerEvent(this.RTCPeerIns,{track:e=>{e.streams&&e.streams[0]&&(this.streamControl.remoteStreams=e.streams[0])},icecandidate:t=>{if(t.candidate){const e=r.EventMap.get("ice");null!==e&&void 0!==e&&e.forEach(e=>{e(t.candidate)})}},connectionstatechange:e=>{e=e.target;this.state=e.connectionState}}),["sendrecv","sendonly"].includes(this.WetOption.transDirect)){const i=null===(a=this.streamControl)||void 0===a?void 0:a.localStreams;null!==i&&void 0!==i&&i.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,i)})}else try{this.RTCPeerIns.addTransceiver("video",{direction:"recvonly"}),this.RTCPeerIns.addTransceiver("audio",{direction:"recvonly"})}catch(e){const t=document.createElement("canvas"),s=t.captureStream();null!==s&&void 0!==s&&s.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,s)})}return o?(yield this.setRemoteSdp(o),e=yield this.RTCPeerIns.createAnswer(),yield this.RTCPeerIns.setLocalDescription(e),e):(e=yield this.RTCPeerIns.createOffer(),yield this.RTCPeerIns.setLocalDescription(e),e)})}closePeerInsTracks(){if(this.RTCPeerIns){const e={recvonly:()=>getRtcPeerTracks(this.RTCPeerIns,"receivers"),sendonly:()=>getRtcPeerTracks(this.RTCPeerIns,"senders"),sendrecv:()=>getRtcPeerTracks(this.RTCPeerIns)},t=e[this.WetOption.transDirect]();t.forEach(e=>{null!=e&&e.stop()})}}disconnect(){this.state="closed",this.removeListenFn(),this.closePeerInsTracks()}close(){this.disconnect(),this.streamControl.close()}setRemoteSdp(e){var t;return __awaiter(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失sdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.setRemoteDescription(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}addIceCandidate(e){var t;return __awaiter(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失IceSdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.addIceCandidate(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}playRemote(t,n){return __awaiter(this,void 0,void 0,function*(){var e;this.RTCPeerIns&&(e=this.RTCPeerIns.getReceivers().map(e=>e.track),e=new MediaStream(e),null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=filterStream([e],n)))})}playLocal(t,n){return __awaiter(this,void 0,void 0,function*(){this.streamControl.localStreams||(yield this.captureStream());var e=this.streamControl.localStreams;null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=filterStream([e],n))})}}let getRandomValues;const rnds8=new Uint8Array(16);function rng(){if(!getRandomValues&&!(getRandomValues="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return getRandomValues(rnds8)}const byteToHex=[];for(let e=0;e<256;++e)byteToHex.push((e+256).toString(16).slice(1));function unsafeStringify(e,t=0){return(byteToHex[e[t+0]]+byteToHex[e[t+1]]+byteToHex[e[t+2]]+byteToHex[e[t+3]]+"-"+byteToHex[e[t+4]]+byteToHex[e[t+5]]+"-"+byteToHex[e[t+6]]+byteToHex[e[t+7]]+"-"+byteToHex[e[t+8]]+byteToHex[e[t+9]]+"-"+byteToHex[e[t+10]]+byteToHex[e[t+11]]+byteToHex[e[t+12]]+byteToHex[e[t+13]]+byteToHex[e[t+14]]+byteToHex[e[t+15]]).toLowerCase()}const randomUUID="undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);var native={randomUUID:randomUUID};function v4(e,t,n){if(native.randomUUID&&!t&&!e)return native.randomUUID();const r=(e=e||{}).random||(e.rng||rng)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return unsafeStringify(r)}function useWetRTC(e){const{connectionConfig:t,name:n,isLisenByts:r}=e,[u,l]=useState(0),i=useRef(""),s=useRef(e.senderSdp),o=useRef(e.senderIce),a=useRef(e.onError),c=(s.current=e.senderSdp,o.current=e.senderIce,a.current=e.onError,useRef(null)),[v,m]=useState({audioinput:!1,audiooutput:!1,videoinput:!1}),p=useRef({audioinput:!1,audiooutput:!1,videoinput:!1}),[d,h]=useState("new");useEffect(()=>{c.current=new WetRTC(t),c.current.addEventListener("error",e=>{a.current&&a.current(e)}),c.current.addEventListener("state",e=>{h(e)}),c.current.addEventListener("ice",e=>{o.current&&o.current(e)}),c.current.getUseAbleDevices().then(e=>{const t={audioinput:!0,audiooutput:!0,videoinput:!0};if(e){const n=e.map(e=>e.kind);t.audioinput=n.includes("audioinput"),t.audiooutput=n.includes("audiooutput"),t.videoinput=n.includes("videoinput"),p.current=Object.assign({},t)}m(()=>t)})},[]),useEffect(()=>{var e;"connected"===d&&r&&null!==(e=c.current)&&void 0!==e&&e.addEventListener("byts",e=>{l(e)})},[d,r]);return{connect:useCallback(t=>__awaiter(this,void 0,void 0,function*(){var e;n?i.current="object"==typeof n?n.current:n:i.current=i.current||v4(),c.current&&i.current&&((e=yield c.current.connect(t))&&s.current(e))}),[n&&"object"==typeof n?n.current:n]),disconnect:()=>{var e;null!==(e=c.current)&&void 0!==e&&e.close()},play:(e,t,n)=>{"local"===e?null!==(e=c.current)&&void 0!==e&&e.playLocal(t,n):null!==(e=c.current)&&void 0!==e&&e.playRemote(t,n)},answerAction:e=>{var t;null!==(t=c.current)&&void 0!==t&&t.setRemoteSdp(e)},byts:u,connectState:d,useAblesState:v}}export{concatStream,WetRTC as default,filterStream,getMediaStream,getRtcPeerTracks,getUseAbleDevices,stopStreamTrack,streamToRecord,useWetRTC};
|
|
1
|
+
import{useState,useRef,useEffect,useCallback}from"react";function __awaiter(e,o,a,c){return new(a=a||Promise)(function(n,t){function r(e){try{s(c.next(e))}catch(e){t(e)}}function i(e){try{s(c.throw(e))}catch(e){t(e)}}function s(e){var t;e.done?n(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(r,i)}s((c=c.apply(e,o||[])).next())})}const getUseAbleDevices=()=>__awaiter(void 0,void 0,void 0,function*(){return navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices?yield navigator.mediaDevices.enumerateDevices():(console.error("navigator.mediaDevices.enumerateDevices"),null)}),getMediaStream=(e="display",r)=>{const i="display"===e?"getDisplayMedia":"getUserMedia";return n=>__awaiter(void 0,void 0,void 0,function*(){let e=null;if(navigator.mediaDevices[i]){var t=Object.assign({video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,enableBackground:!1,suppressLocalAudioPlayback:!0}},n);try{e=yield navigator.mediaDevices[i](t)}catch(e){r&&r({name:e.name||"Error",message:e.message||"内部错误"})}}else r&&r({name:"CompatibleError",message:"不支持navigator.mediaDevices"});return e})},stopStreamTrack=e=>{e&&e.getTracks().forEach(e=>{e.stop()})},concatStream=e=>{let t=[];return e.forEach(e=>{t=[...t,...e.getTracks()]}),new MediaStream(t)},filterStream=(e,t)=>{let n=[];return e.forEach(e=>{n=[...n,...e.getTracks().filter(e=>t?e.kind===t:e)]}),new MediaStream(n)},getRtcPeerTracks=(e,t)=>{const n={receivers(){const t=[];return e.getReceivers().forEach(e=>{e.track&&t.push(e.track)}),t},senders(){const t=[];return e.getSenders().forEach(e=>{e.track&&t.push(e.track)}),t}};return t?n[t]():[...n.receivers(),...n.senders()]},listenerEvent=(e,t)=>(e.addEventListener("track",t.track),e.addEventListener("icecandidate",t.icecandidate),e.addEventListener("connectionstatechange",t.connectionstatechange),()=>{e.removeEventListener("track",t.track),e.removeEventListener("icecandidate",t.icecandidate),e.removeEventListener("connectionstatechange",t.connectionstatechange)}),SaveFileToLocal=(e,t)=>{e=URL.createObjectURL(e);const n=document.createElement("a");n.href=e,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.download=t,n.click(),URL.revokeObjectURL(e),document.body.removeChild(n)},streamToRecord=(e,t={audioBitsPerSecond:128e3,videoBitsPerSecond:25e5,mimeType:"video/webm"})=>{const n=t.mimeType?t.mimeType.split("/"):["webm"];let r=(new Date).getTime().toString();const i=MediaRecorder.isTypeSupported(t.mimeType||"");if(!i)throw new Error(t.mimeType+"类型不支持");const s=new MediaRecorder(e,t);s.addEventListener("dataavailable",e=>{SaveFileToLocal(e.data,r+"."+n[(null===n||void 0===n?void 0:n.length)-1])});const o=()=>{i&&s.stop()};return{start:()=>{i&&s.start()},pause:()=>{i&&s.pause()},save:e=>{i&&(r=e||(new Date).getTime().toString(),o())},stop:o,resume:()=>{i&&s.resume()}}},DEFAULT_WetOption={transDirect:"sendrecv",configuration:{iceServers:[{urls:"stun:stun.l.google.com:19302"}]},constraints:{video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,suppressLocalAudioPlayback:!0}}};class WetRTC{constructor(e){this.state="new",this.isUseAbles={audiooutput:!1,audioinput:!1,videoinput:!1},this.streamControl={close:e=>{const t={remote:()=>{stopStreamTrack(this.streamControl.remoteStreams),this.streamControl.remoteStreams=null},local:()=>{stopStreamTrack(this.streamControl.localStreams),this.streamControl.localStreams=null}};e?t[e]():(t.local(),t.remote())}},this.runByt={time:null,close:()=>{this.runByt.time&&clearInterval(this.runByt.time)},run:()=>{const i=this.EventMap.get("byts");let s=null;i&&0<i.length&&(null!==i&&void 0!==i&&i.forEach(e=>{e(0)}),this.runByt.time=setInterval(()=>{var e;null!==(e=this.RTCPeerIns)&&void 0!==e&&e.getStats().then(e=>{const t={bytesReceived:0};if(e.forEach(e=>{"transport"===e.type&&(t.bytesReceived+=e.bytesReceived)}),s){const n=s.bytesReceived,r=t.bytesReceived;null!==i&&void 0!==i&&i.forEach(e=>{e(Number(((r-n)/1024).toFixed(2)))}),s=Object.assign({},t)}else s=Object.assign({},t)})},1e3))}},this.EventMap=new Map([]),this.RTCPeerIns=null,this.removeListenFn=()=>{},this.WetOption=Object.assign({},DEFAULT_WetOption),this.WetOption=Object.assign(Object.assign({},this.WetOption),e)}runError(t){const e=this.EventMap.get("error");null!==e&&void 0!==e&&e.forEach(e=>{e(t)})}getUseAbleDevices(){return __awaiter(this,void 0,void 0,function*(){return yield getUseAbleDevices()})}addEventListener(e,t){const n=this.EventMap.get(e);n?n.find(e=>e===t)||(n.push(t),this.EventMap.set(e,n)):this.EventMap.set(e,[t])}removeEventListener(e,t){const n=this.EventMap.get(e);var r;n&&(r=n.filter(e=>e!==t),this.EventMap.set(e,r))}captureStream(){return __awaiter(this,void 0,void 0,function*(){const e=yield getUseAbleDevices();if(e){const n=e.map(e=>e.kind);this.isUseAbles.audioinput=n.includes("audioinput"),this.isUseAbles.videoinput=n.includes("videoinput"),this.isUseAbles.audiooutput=n.includes("audiooutput")}else this.isUseAbles.audioinput=!0,this.isUseAbles.videoinput=!0,this.isUseAbles.audiooutput=!0;var t;return this.isUseAbles.audioinput||this.isUseAbles.videoinput?((t=yield getMediaStream("user",e=>{this.runError(e)})({video:!!this.isUseAbles.videoinput&&this.WetOption.constraints.video,audio:!!this.isUseAbles.audioinput&&this.WetOption.constraints.audio}))&&(this.streamControl.localStreams=t),t):(this.runError({name:"StreamError",message:"音频输入设备,视频输入设备均不可用"}),null)})}connect(o){var a;return __awaiter(this,void 0,void 0,function*(){if(["sendrecv","sendonly"].includes(this.WetOption.transDirect)&&(!this.streamControl.localStreams&&!(yield this.captureStream())))return null;let n="new",r=this;Object.defineProperty(this,"state",{get(){return n},set(e){"connected"===(n=e)?r.runByt.run():["disconnected","closed","failed"].includes(e)&&r.runByt.close();const t=r.EventMap.get("state");null!==t&&void 0!==t&&t.forEach(e=>{e(n)})}});var e;if(this.state="new",this.RTCPeerIns=new RTCPeerConnection(this.WetOption.configuration),this.removeListenFn=listenerEvent(this.RTCPeerIns,{track:e=>{e.streams&&e.streams[0]&&(this.streamControl.remoteStreams=e.streams[0])},icecandidate:t=>{if(t.candidate){const e=r.EventMap.get("ice");null!==e&&void 0!==e&&e.forEach(e=>{e(t.candidate)})}},connectionstatechange:e=>{e=e.target;this.state=e.connectionState}}),["sendrecv","sendonly"].includes(this.WetOption.transDirect)){const i=null===(a=this.streamControl)||void 0===a?void 0:a.localStreams;null!==i&&void 0!==i&&i.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,i)})}else try{this.RTCPeerIns.addTransceiver("video",{direction:"recvonly"}),this.RTCPeerIns.addTransceiver("audio",{direction:"recvonly"})}catch(e){const t=document.createElement("canvas"),s=t.captureStream();null!==s&&void 0!==s&&s.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,s)})}return o?(yield this.setRemoteSdp(o),e=yield this.RTCPeerIns.createAnswer(),yield this.RTCPeerIns.setLocalDescription(e),e):(e=yield this.RTCPeerIns.createOffer(),yield this.RTCPeerIns.setLocalDescription(e),e)})}closePeerInsTracks(){if(this.RTCPeerIns){const e={recvonly:()=>getRtcPeerTracks(this.RTCPeerIns,"receivers"),sendonly:()=>getRtcPeerTracks(this.RTCPeerIns,"senders"),sendrecv:()=>getRtcPeerTracks(this.RTCPeerIns)},t=e[this.WetOption.transDirect]();t.forEach(e=>{null!=e&&e.stop()})}}disconnect(){this.state="closed",this.removeListenFn(),this.closePeerInsTracks(),this.RTCPeerIns=null}close(){this.disconnect(),this.streamControl.close()}setRemoteSdp(e){var t;return __awaiter(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失sdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.setRemoteDescription(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}addIceCandidate(n){return __awaiter(this,void 0,void 0,function*(){if(!n)return this.runError({name:"OperationError",message:"缺失IceSdp信息"}),void(this.state="failed");const e=()=>__awaiter(this,void 0,void 0,function*(){var e;try{yield null===(e=this.RTCPeerIns)||void 0===e?void 0:e.addIceCandidate(n)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}});if(this.RTCPeerIns)e();else{const t=setInterval(()=>__awaiter(this,void 0,void 0,function*(){e(),clearInterval(t)}),1e3)}})}playRemote(t,n){return __awaiter(this,void 0,void 0,function*(){var e;this.RTCPeerIns&&(e=this.RTCPeerIns.getReceivers().map(e=>e.track),e=new MediaStream(e),null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=filterStream([e],n)))})}playLocal(t,n){return __awaiter(this,void 0,void 0,function*(){this.streamControl.localStreams||(yield this.captureStream());var e=this.streamControl.localStreams;null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=filterStream([e],n))})}}let getRandomValues;const rnds8=new Uint8Array(16);function rng(){if(!getRandomValues&&!(getRandomValues="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return getRandomValues(rnds8)}const byteToHex=[];for(let e=0;e<256;++e)byteToHex.push((e+256).toString(16).slice(1));function unsafeStringify(e,t=0){return(byteToHex[e[t+0]]+byteToHex[e[t+1]]+byteToHex[e[t+2]]+byteToHex[e[t+3]]+"-"+byteToHex[e[t+4]]+byteToHex[e[t+5]]+"-"+byteToHex[e[t+6]]+byteToHex[e[t+7]]+"-"+byteToHex[e[t+8]]+byteToHex[e[t+9]]+"-"+byteToHex[e[t+10]]+byteToHex[e[t+11]]+byteToHex[e[t+12]]+byteToHex[e[t+13]]+byteToHex[e[t+14]]+byteToHex[e[t+15]]).toLowerCase()}const randomUUID="undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);var native={randomUUID:randomUUID};function v4(e,t,n){if(native.randomUUID&&!t&&!e)return native.randomUUID();const r=(e=e||{}).random||(e.rng||rng)();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return unsafeStringify(r)}function useWetRTC(e){const{connectionConfig:t,name:n,isLisenByts:r}=e,[u,l]=useState(0),i=useRef(""),s=useRef(e.senderSdp),o=useRef(e.senderIce),a=useRef(e.onError),c=(s.current=e.senderSdp,o.current=e.senderIce,a.current=e.onError,useRef(null)),[v,m]=useState({audioinput:!1,audiooutput:!1,videoinput:!1}),h=useRef({audioinput:!1,audiooutput:!1,videoinput:!1}),[d,p]=useState("new");useEffect(()=>{c.current=new WetRTC(t),c.current.addEventListener("error",e=>{a.current&&a.current(e)}),c.current.addEventListener("state",e=>{p(e)}),c.current.addEventListener("ice",e=>{o.current&&o.current(e)}),c.current.getUseAbleDevices().then(e=>{const t={audioinput:!0,audiooutput:!0,videoinput:!0};if(e){const n=e.map(e=>e.kind);t.audioinput=n.includes("audioinput"),t.audiooutput=n.includes("audiooutput"),t.videoinput=n.includes("videoinput"),h.current=Object.assign({},t)}m(()=>t)})},[]),useEffect(()=>{var e;"connected"===d&&r&&null!==(e=c.current)&&void 0!==e&&e.addEventListener("byts",e=>{l(e)})},[d,r]);return{connect:useCallback(t=>__awaiter(this,void 0,void 0,function*(){var e;n?i.current="object"==typeof n?n.current:n:i.current=i.current||v4(),c.current&&i.current&&((e=yield c.current.connect(t))&&s.current(e))}),[n&&"object"==typeof n?n.current:n]),disconnect:()=>{var e;null!==(e=c.current)&&void 0!==e&&e.close()},play:(e,t,n)=>{"local"===e?null!==(e=c.current)&&void 0!==e&&e.playLocal(t,n):null!==(e=c.current)&&void 0!==e&&e.playRemote(t,n)},answerAction:e=>{var t;null!==(t=c.current)&&void 0!==t&&t.setRemoteSdp(e)},byts:u,connectState:d,useAblesState:v}}export{concatStream,WetRTC as default,filterStream,getMediaStream,getRtcPeerTracks,getUseAbleDevices,stopStreamTrack,streamToRecord,useWetRTC};
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).index={},e.react)}(this,function(e,f){"use strict";function y(e,o,a,c){return new(a=a||Promise)(function(n,t){function r(e){try{s(c.next(e))}catch(e){t(e)}}function i(e){try{s(c.throw(e))}catch(e){t(e)}}function s(e){var t;e.done?n(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(r,i)}s((c=c.apply(e,o||[])).next())})}const r=()=>y(void 0,void 0,void 0,function*(){return navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices?yield navigator.mediaDevices.enumerateDevices():(console.error("navigator.mediaDevices.enumerateDevices"),null)}),i=(e="display",r)=>{const i="display"===e?"getDisplayMedia":"getUserMedia";return n=>y(void 0,void 0,void 0,function*(){let e=null;if(navigator.mediaDevices[i]){var t=Object.assign({video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,enableBackground:!1,suppressLocalAudioPlayback:!0}},n);try{e=yield navigator.mediaDevices[i](t)}catch(e){r&&r({name:e.name||"Error",message:e.message||"内部错误"})}}else r&&r({name:"CompatibleError",message:"不支持navigator.mediaDevices"});return e})},n=e=>{e&&e.getTracks().forEach(e=>{e.stop()})};const s=(e,t)=>{let n=[];return e.forEach(e=>{n=[...n,...e.getTracks().filter(e=>t?e.kind===t:e)]}),new MediaStream(n)},o=(e,t)=>{const n={receivers(){const t=[];return e.getReceivers().forEach(e=>{e.track&&t.push(e.track)}),t},senders(){const t=[];return e.getSenders().forEach(e=>{e.track&&t.push(e.track)}),t}};return t?n[t]():[...n.receivers(),...n.senders()]};const t={transDirect:"sendrecv",configuration:{iceServers:[{urls:"stun:stun.l.google.com:19302"}]},constraints:{video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,suppressLocalAudioPlayback:!0}}};class g{constructor(e){this.state="new",this.isUseAbles={audiooutput:!1,audioinput:!1,videoinput:!1},this.streamControl={close:e=>{const t={remote:()=>{n(this.streamControl.remoteStreams),this.streamControl.remoteStreams=null},local:()=>{n(this.streamControl.localStreams),this.streamControl.localStreams=null}};e?t[e]():(t.local(),t.remote())}},this.runByt={time:null,close:()=>{this.runByt.time&&clearInterval(this.runByt.time)},run:()=>{const i=this.EventMap.get("byts");let s=null;i&&0<i.length&&(null!==i&&void 0!==i&&i.forEach(e=>{e(0)}),this.runByt.time=setInterval(()=>{var e;null!==(e=this.RTCPeerIns)&&void 0!==e&&e.getStats().then(e=>{const t={bytesReceived:0};if(e.forEach(e=>{"transport"===e.type&&(t.bytesReceived+=e.bytesReceived)}),s){const n=s.bytesReceived,r=t.bytesReceived;null!==i&&void 0!==i&&i.forEach(e=>{e(Number(((r-n)/1024).toFixed(2)))}),s=Object.assign({},t)}else s=Object.assign({},t)})},1e3))}},this.EventMap=new Map([]),this.RTCPeerIns=null,this.removeListenFn=()=>{},this.WetOption=Object.assign({},t),this.WetOption=Object.assign(Object.assign({},this.WetOption),e)}runError(t){const e=this.EventMap.get("error");null!==e&&void 0!==e&&e.forEach(e=>{e(t)})}getUseAbleDevices(){return y(this,void 0,void 0,function*(){return yield r()})}addEventListener(e,t){const n=this.EventMap.get(e);n?n.find(e=>e===t)||(n.push(t),this.EventMap.set(e,n)):this.EventMap.set(e,[t])}removeEventListener(e,t){const n=this.EventMap.get(e);var r;n&&(r=n.filter(e=>e!==t),this.EventMap.set(e,r))}captureStream(){return y(this,void 0,void 0,function*(){const e=yield r();if(e){const n=e.map(e=>e.kind);this.isUseAbles.audioinput=n.includes("audioinput"),this.isUseAbles.videoinput=n.includes("videoinput"),this.isUseAbles.audiooutput=n.includes("audiooutput")}else this.isUseAbles.audioinput=!0,this.isUseAbles.videoinput=!0,this.isUseAbles.audiooutput=!0;var t;return this.isUseAbles.audioinput||this.isUseAbles.videoinput?((t=yield i("user",e=>{this.runError(e)})({video:!!this.isUseAbles.videoinput&&this.WetOption.constraints.video,audio:!!this.isUseAbles.audioinput&&this.WetOption.constraints.audio}))&&(this.streamControl.localStreams=t),t):(this.runError({name:"StreamError",message:"音频输入设备,视频输入设备均不可用"}),null)})}connect(c){var d;return y(this,void 0,void 0,function*(){if(["sendrecv","sendonly"].includes(this.WetOption.transDirect)&&(!this.streamControl.localStreams&&!(yield this.captureStream())))return null;let n="new",r=this;Object.defineProperty(this,"state",{get(){return n},set(e){"connected"===(n=e)?r.runByt.run():["disconnected","closed","failed"].includes(e)&&r.runByt.close();const t=r.EventMap.get("state");null!==t&&void 0!==t&&t.forEach(e=>{e(n)})}});var e,t,i;if(this.state="new",this.RTCPeerIns=new RTCPeerConnection(this.WetOption.configuration),this.removeListenFn=(e=this.RTCPeerIns,t={track:e=>{e.streams&&e.streams[0]&&(this.streamControl.remoteStreams=e.streams[0])},icecandidate:t=>{if(t.candidate){const e=r.EventMap.get("ice");null!==e&&void 0!==e&&e.forEach(e=>{e(t.candidate)})}},connectionstatechange:e=>{e=e.target;this.state=e.connectionState}},e.addEventListener("track",t.track),e.addEventListener("icecandidate",t.icecandidate),e.addEventListener("connectionstatechange",t.connectionstatechange),()=>{e.removeEventListener("track",t.track),e.removeEventListener("icecandidate",t.icecandidate),e.removeEventListener("connectionstatechange",t.connectionstatechange)}),["sendrecv","sendonly"].includes(this.WetOption.transDirect)){const s=null===(d=this.streamControl)||void 0===d?void 0:d.localStreams;null!==s&&void 0!==s&&s.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,s)})}else try{this.RTCPeerIns.addTransceiver("video",{direction:"recvonly"}),this.RTCPeerIns.addTransceiver("audio",{direction:"recvonly"})}catch(e){const o=document.createElement("canvas"),a=o.captureStream();null!==a&&void 0!==a&&a.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,a)})}return c?(yield this.setRemoteSdp(c),i=yield this.RTCPeerIns.createAnswer(),yield this.RTCPeerIns.setLocalDescription(i),i):(i=yield this.RTCPeerIns.createOffer(),yield this.RTCPeerIns.setLocalDescription(i),i)})}closePeerInsTracks(){if(this.RTCPeerIns){const e={recvonly:()=>o(this.RTCPeerIns,"receivers"),sendonly:()=>o(this.RTCPeerIns,"senders"),sendrecv:()=>o(this.RTCPeerIns)},t=e[this.WetOption.transDirect]();t.forEach(e=>{null!=e&&e.stop()})}}disconnect(){this.state="closed",this.removeListenFn(),this.closePeerInsTracks()}close(){this.disconnect(),this.streamControl.close()}setRemoteSdp(e){var t;return y(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失sdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.setRemoteDescription(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}addIceCandidate(e){var t;return y(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失IceSdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.addIceCandidate(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}playRemote(t,n){return y(this,void 0,void 0,function*(){var e;this.RTCPeerIns&&(e=this.RTCPeerIns.getReceivers().map(e=>e.track),e=new MediaStream(e),null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=s([e],n)))})}playLocal(t,n){return y(this,void 0,void 0,function*(){this.streamControl.localStreams||(yield this.captureStream());var e=this.streamControl.localStreams;null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=s([e],n))})}}let a;const d=new Uint8Array(16);const c=[];for(let e=0;e<256;++e)c.push((e+256).toString(16).slice(1));var u={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function b(e,t,n){if(u.randomUUID&&!t&&!e)return u.randomUUID();const r=(e=e||{}).random||(e.rng||function(){if(!a&&!(a="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return a(d)})();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return e=r,i=0,(c[e[i+0]]+c[e[i+1]]+c[e[i+2]]+c[e[i+3]]+"-"+c[e[i+4]]+c[e[i+5]]+"-"+c[e[i+6]]+c[e[i+7]]+"-"+c[e[i+8]]+c[e[i+9]]+"-"+c[e[i+10]]+c[e[i+11]]+c[e[i+12]]+c[e[i+13]]+c[e[i+14]]+c[e[i+15]]).toLowerCase();var i}e.concatStream=e=>{let t=[];return e.forEach(e=>{t=[...t,...e.getTracks()]}),new MediaStream(t)},e.default=g,e.filterStream=s,e.getMediaStream=i,e.getRtcPeerTracks=o,e.getUseAbleDevices=r,e.stopStreamTrack=n,e.streamToRecord=(e,t={audioBitsPerSecond:128e3,videoBitsPerSecond:25e5,mimeType:"video/webm"})=>{const r=t.mimeType?t.mimeType.split("/"):["webm"];let i=(new Date).getTime().toString();const n=MediaRecorder.isTypeSupported(t.mimeType||"");if(!n)throw new Error(t.mimeType+"类型不支持");const s=new MediaRecorder(e,t);s.addEventListener("dataavailable",e=>{{e=e.data;var t=i+"."+r[(null===r||void 0===r?void 0:r.length)-1];e=URL.createObjectURL(e);const n=document.createElement("a");return n.href=e,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.download=t,n.click(),URL.revokeObjectURL(e),void document.body.removeChild(n)}});const o=()=>{n&&s.stop()};return{start:()=>{n&&s.start()},pause:()=>{n&&s.pause()},save:e=>{n&&(i=e||(new Date).getTime().toString(),o())},stop:o,resume:()=>{n&&s.resume()}}},e.useWetRTC=function(e){const{connectionConfig:t,name:n,isLisenByts:r}=e,[u,l]=f.useState(0),i=f.useRef(""),s=f.useRef(e.senderSdp),o=f.useRef(e.senderIce),a=f.useRef(e.onError),c=(s.current=e.senderSdp,o.current=e.senderIce,a.current=e.onError,f.useRef(null)),[v,p]=f.useState({audioinput:!1,audiooutput:!1,videoinput:!1}),h=f.useRef({audioinput:!1,audiooutput:!1,videoinput:!1}),[d,m]=f.useState("new");return f.useEffect(()=>{c.current=new g(t),c.current.addEventListener("error",e=>{a.current&&a.current(e)}),c.current.addEventListener("state",e=>{m(e)}),c.current.addEventListener("ice",e=>{o.current&&o.current(e)}),c.current.getUseAbleDevices().then(e=>{const t={audioinput:!0,audiooutput:!0,videoinput:!0};if(e){const n=e.map(e=>e.kind);t.audioinput=n.includes("audioinput"),t.audiooutput=n.includes("audiooutput"),t.videoinput=n.includes("videoinput"),h.current=Object.assign({},t)}p(()=>t)})},[]),f.useEffect(()=>{var e;"connected"===d&&r&&null!==(e=c.current)&&void 0!==e&&e.addEventListener("byts",e=>{l(e)})},[d,r]),{connect:f.useCallback(t=>y(this,void 0,void 0,function*(){var e;n?i.current="object"==typeof n?n.current:n:i.current=i.current||b(),c.current&&i.current&&((e=yield c.current.connect(t))&&s.current(e))}),[n&&"object"==typeof n?n.current:n]),disconnect:()=>{var e;null!==(e=c.current)&&void 0!==e&&e.close()},play:(e,t,n)=>{"local"===e?null!==(e=c.current)&&void 0!==e&&e.playLocal(t,n):null!==(e=c.current)&&void 0!==e&&e.playRemote(t,n)},answerAction:e=>{var t;null!==(t=c.current)&&void 0!==t&&t.setRemoteSdp(e)},byts:u,connectState:d,useAblesState:v}},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).index={},e.react)}(this,function(e,f){"use strict";function y(e,o,a,c){return new(a=a||Promise)(function(n,t){function r(e){try{s(c.next(e))}catch(e){t(e)}}function i(e){try{s(c.throw(e))}catch(e){t(e)}}function s(e){var t;e.done?n(e.value):((t=e.value)instanceof a?t:new a(function(e){e(t)})).then(r,i)}s((c=c.apply(e,o||[])).next())})}const r=()=>y(void 0,void 0,void 0,function*(){return navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices?yield navigator.mediaDevices.enumerateDevices():(console.error("navigator.mediaDevices.enumerateDevices"),null)}),i=(e="display",r)=>{const i="display"===e?"getDisplayMedia":"getUserMedia";return n=>y(void 0,void 0,void 0,function*(){let e=null;if(navigator.mediaDevices[i]){var t=Object.assign({video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,enableBackground:!1,suppressLocalAudioPlayback:!0}},n);try{e=yield navigator.mediaDevices[i](t)}catch(e){r&&r({name:e.name||"Error",message:e.message||"内部错误"})}}else r&&r({name:"CompatibleError",message:"不支持navigator.mediaDevices"});return e})},n=e=>{e&&e.getTracks().forEach(e=>{e.stop()})};const s=(e,t)=>{let n=[];return e.forEach(e=>{n=[...n,...e.getTracks().filter(e=>t?e.kind===t:e)]}),new MediaStream(n)},o=(e,t)=>{const n={receivers(){const t=[];return e.getReceivers().forEach(e=>{e.track&&t.push(e.track)}),t},senders(){const t=[];return e.getSenders().forEach(e=>{e.track&&t.push(e.track)}),t}};return t?n[t]():[...n.receivers(),...n.senders()]};const t={transDirect:"sendrecv",configuration:{iceServers:[{urls:"stun:stun.l.google.com:19302"}]},constraints:{video:!0,audio:{noiseSuppression:!0,echoCancellation:!0,suppressLocalAudioPlayback:!0}}};class g{constructor(e){this.state="new",this.isUseAbles={audiooutput:!1,audioinput:!1,videoinput:!1},this.streamControl={close:e=>{const t={remote:()=>{n(this.streamControl.remoteStreams),this.streamControl.remoteStreams=null},local:()=>{n(this.streamControl.localStreams),this.streamControl.localStreams=null}};e?t[e]():(t.local(),t.remote())}},this.runByt={time:null,close:()=>{this.runByt.time&&clearInterval(this.runByt.time)},run:()=>{const i=this.EventMap.get("byts");let s=null;i&&0<i.length&&(null!==i&&void 0!==i&&i.forEach(e=>{e(0)}),this.runByt.time=setInterval(()=>{var e;null!==(e=this.RTCPeerIns)&&void 0!==e&&e.getStats().then(e=>{const t={bytesReceived:0};if(e.forEach(e=>{"transport"===e.type&&(t.bytesReceived+=e.bytesReceived)}),s){const n=s.bytesReceived,r=t.bytesReceived;null!==i&&void 0!==i&&i.forEach(e=>{e(Number(((r-n)/1024).toFixed(2)))}),s=Object.assign({},t)}else s=Object.assign({},t)})},1e3))}},this.EventMap=new Map([]),this.RTCPeerIns=null,this.removeListenFn=()=>{},this.WetOption=Object.assign({},t),this.WetOption=Object.assign(Object.assign({},this.WetOption),e)}runError(t){const e=this.EventMap.get("error");null!==e&&void 0!==e&&e.forEach(e=>{e(t)})}getUseAbleDevices(){return y(this,void 0,void 0,function*(){return yield r()})}addEventListener(e,t){const n=this.EventMap.get(e);n?n.find(e=>e===t)||(n.push(t),this.EventMap.set(e,n)):this.EventMap.set(e,[t])}removeEventListener(e,t){const n=this.EventMap.get(e);var r;n&&(r=n.filter(e=>e!==t),this.EventMap.set(e,r))}captureStream(){return y(this,void 0,void 0,function*(){const e=yield r();if(e){const n=e.map(e=>e.kind);this.isUseAbles.audioinput=n.includes("audioinput"),this.isUseAbles.videoinput=n.includes("videoinput"),this.isUseAbles.audiooutput=n.includes("audiooutput")}else this.isUseAbles.audioinput=!0,this.isUseAbles.videoinput=!0,this.isUseAbles.audiooutput=!0;var t;return this.isUseAbles.audioinput||this.isUseAbles.videoinput?((t=yield i("user",e=>{this.runError(e)})({video:!!this.isUseAbles.videoinput&&this.WetOption.constraints.video,audio:!!this.isUseAbles.audioinput&&this.WetOption.constraints.audio}))&&(this.streamControl.localStreams=t),t):(this.runError({name:"StreamError",message:"音频输入设备,视频输入设备均不可用"}),null)})}connect(c){var d;return y(this,void 0,void 0,function*(){if(["sendrecv","sendonly"].includes(this.WetOption.transDirect)&&(!this.streamControl.localStreams&&!(yield this.captureStream())))return null;let n="new",r=this;Object.defineProperty(this,"state",{get(){return n},set(e){"connected"===(n=e)?r.runByt.run():["disconnected","closed","failed"].includes(e)&&r.runByt.close();const t=r.EventMap.get("state");null!==t&&void 0!==t&&t.forEach(e=>{e(n)})}});var e,t,i;if(this.state="new",this.RTCPeerIns=new RTCPeerConnection(this.WetOption.configuration),this.removeListenFn=(e=this.RTCPeerIns,t={track:e=>{e.streams&&e.streams[0]&&(this.streamControl.remoteStreams=e.streams[0])},icecandidate:t=>{if(t.candidate){const e=r.EventMap.get("ice");null!==e&&void 0!==e&&e.forEach(e=>{e(t.candidate)})}},connectionstatechange:e=>{e=e.target;this.state=e.connectionState}},e.addEventListener("track",t.track),e.addEventListener("icecandidate",t.icecandidate),e.addEventListener("connectionstatechange",t.connectionstatechange),()=>{e.removeEventListener("track",t.track),e.removeEventListener("icecandidate",t.icecandidate),e.removeEventListener("connectionstatechange",t.connectionstatechange)}),["sendrecv","sendonly"].includes(this.WetOption.transDirect)){const s=null===(d=this.streamControl)||void 0===d?void 0:d.localStreams;null!==s&&void 0!==s&&s.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,s)})}else try{this.RTCPeerIns.addTransceiver("video",{direction:"recvonly"}),this.RTCPeerIns.addTransceiver("audio",{direction:"recvonly"})}catch(e){const o=document.createElement("canvas"),a=o.captureStream();null!==a&&void 0!==a&&a.getTracks().forEach(e=>{var t;null!==(t=this.RTCPeerIns)&&void 0!==t&&t.addTrack(e,a)})}return c?(yield this.setRemoteSdp(c),i=yield this.RTCPeerIns.createAnswer(),yield this.RTCPeerIns.setLocalDescription(i),i):(i=yield this.RTCPeerIns.createOffer(),yield this.RTCPeerIns.setLocalDescription(i),i)})}closePeerInsTracks(){if(this.RTCPeerIns){const e={recvonly:()=>o(this.RTCPeerIns,"receivers"),sendonly:()=>o(this.RTCPeerIns,"senders"),sendrecv:()=>o(this.RTCPeerIns)},t=e[this.WetOption.transDirect]();t.forEach(e=>{null!=e&&e.stop()})}}disconnect(){this.state="closed",this.removeListenFn(),this.closePeerInsTracks(),this.RTCPeerIns=null}close(){this.disconnect(),this.streamControl.close()}setRemoteSdp(e){var t;return y(this,void 0,void 0,function*(){if(!e)return this.runError({name:"OperationError",message:"缺失sdp信息"}),void(this.state="failed");try{yield null===(t=this.RTCPeerIns)||void 0===t?void 0:t.setRemoteDescription(e)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}})}addIceCandidate(n){return y(this,void 0,void 0,function*(){if(!n)return this.runError({name:"OperationError",message:"缺失IceSdp信息"}),void(this.state="failed");const e=()=>y(this,void 0,void 0,function*(){var e;try{yield null===(e=this.RTCPeerIns)||void 0===e?void 0:e.addIceCandidate(n)}catch(e){this.state="failed",this.runError({name:e.name||"Error",message:e.message||"内部错误"})}});if(this.RTCPeerIns)e();else{const t=setInterval(()=>y(this,void 0,void 0,function*(){e(),clearInterval(t)}),1e3)}})}playRemote(t,n){return y(this,void 0,void 0,function*(){var e;this.RTCPeerIns&&(e=this.RTCPeerIns.getReceivers().map(e=>e.track),e=new MediaStream(e),null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=s([e],n)))})}playLocal(t,n){return y(this,void 0,void 0,function*(){this.streamControl.localStreams||(yield this.captureStream());var e=this.streamControl.localStreams;null!=t&&t.setAttribute("autoplay",""),null!=t&&t.setAttribute("playsinline",""),t&&(t.srcObject=s([e],n))})}}let a;const d=new Uint8Array(16);const c=[];for(let e=0;e<256;++e)c.push((e+256).toString(16).slice(1));var u={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function b(e,t,n){if(u.randomUUID&&!t&&!e)return u.randomUUID();const r=(e=e||{}).random||(e.rng||function(){if(!a&&!(a="undefined"!=typeof crypto&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto)))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return a(d)})();if(r[6]=15&r[6]|64,r[8]=63&r[8]|128,t){n=n||0;for(let e=0;e<16;++e)t[n+e]=r[e];return t}return e=r,i=0,(c[e[i+0]]+c[e[i+1]]+c[e[i+2]]+c[e[i+3]]+"-"+c[e[i+4]]+c[e[i+5]]+"-"+c[e[i+6]]+c[e[i+7]]+"-"+c[e[i+8]]+c[e[i+9]]+"-"+c[e[i+10]]+c[e[i+11]]+c[e[i+12]]+c[e[i+13]]+c[e[i+14]]+c[e[i+15]]).toLowerCase();var i}e.concatStream=e=>{let t=[];return e.forEach(e=>{t=[...t,...e.getTracks()]}),new MediaStream(t)},e.default=g,e.filterStream=s,e.getMediaStream=i,e.getRtcPeerTracks=o,e.getUseAbleDevices=r,e.stopStreamTrack=n,e.streamToRecord=(e,t={audioBitsPerSecond:128e3,videoBitsPerSecond:25e5,mimeType:"video/webm"})=>{const r=t.mimeType?t.mimeType.split("/"):["webm"];let i=(new Date).getTime().toString();const n=MediaRecorder.isTypeSupported(t.mimeType||"");if(!n)throw new Error(t.mimeType+"类型不支持");const s=new MediaRecorder(e,t);s.addEventListener("dataavailable",e=>{{e=e.data;var t=i+"."+r[(null===r||void 0===r?void 0:r.length)-1];e=URL.createObjectURL(e);const n=document.createElement("a");return n.href=e,n.target="_blank",n.style.display="none",document.body.appendChild(n),n.download=t,n.click(),URL.revokeObjectURL(e),void document.body.removeChild(n)}});const o=()=>{n&&s.stop()};return{start:()=>{n&&s.start()},pause:()=>{n&&s.pause()},save:e=>{n&&(i=e||(new Date).getTime().toString(),o())},stop:o,resume:()=>{n&&s.resume()}}},e.useWetRTC=function(e){const{connectionConfig:t,name:n,isLisenByts:r}=e,[u,l]=f.useState(0),i=f.useRef(""),s=f.useRef(e.senderSdp),o=f.useRef(e.senderIce),a=f.useRef(e.onError),c=(s.current=e.senderSdp,o.current=e.senderIce,a.current=e.onError,f.useRef(null)),[v,h]=f.useState({audioinput:!1,audiooutput:!1,videoinput:!1}),p=f.useRef({audioinput:!1,audiooutput:!1,videoinput:!1}),[d,m]=f.useState("new");return f.useEffect(()=>{c.current=new g(t),c.current.addEventListener("error",e=>{a.current&&a.current(e)}),c.current.addEventListener("state",e=>{m(e)}),c.current.addEventListener("ice",e=>{o.current&&o.current(e)}),c.current.getUseAbleDevices().then(e=>{const t={audioinput:!0,audiooutput:!0,videoinput:!0};if(e){const n=e.map(e=>e.kind);t.audioinput=n.includes("audioinput"),t.audiooutput=n.includes("audiooutput"),t.videoinput=n.includes("videoinput"),p.current=Object.assign({},t)}h(()=>t)})},[]),f.useEffect(()=>{var e;"connected"===d&&r&&null!==(e=c.current)&&void 0!==e&&e.addEventListener("byts",e=>{l(e)})},[d,r]),{connect:f.useCallback(t=>y(this,void 0,void 0,function*(){var e;n?i.current="object"==typeof n?n.current:n:i.current=i.current||b(),c.current&&i.current&&((e=yield c.current.connect(t))&&s.current(e))}),[n&&"object"==typeof n?n.current:n]),disconnect:()=>{var e;null!==(e=c.current)&&void 0!==e&&e.close()},play:(e,t,n)=>{"local"===e?null!==(e=c.current)&&void 0!==e&&e.playLocal(t,n):null!==(e=c.current)&&void 0!==e&&e.playRemote(t,n)},answerAction:e=>{var t;null!==(t=c.current)&&void 0!==t&&t.setRemoteSdp(e)},byts:u,connectState:d,useAblesState:v}},Object.defineProperty(e,"__esModule",{value:!0})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wetspace/wetrtc",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "wetrtc",
|
|
5
5
|
"author": "tangjie <981955667@qq.com>",
|
|
6
6
|
"license": "ISC",
|
|
@@ -38,6 +38,5 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"rollup": "^2.67.3"
|
|
41
|
-
}
|
|
42
|
-
"dependencies": {}
|
|
41
|
+
}
|
|
43
42
|
}
|