Module 126208

2,088 characters · in no known chunk
Dependencies
Required by (1)
Lazily required by (0)none
Lazily requires (0)none
Source Raw
function(e,t,n){"use strict";n.d(t,{A:()=>l});var r=n(143236),i=n(647457),s=n(228272),a=n(731854);let o={voiceActivityDetection:!0,offerToReceiveAudio:!0,offerToReceiveVideo:!1,iceRestart:!1};class l extends r.EventEmitter{userId;sinkId;input;pc1;pc2;senders=[];outputs={};audioContext;constructor(e,t,n,r=""){super(),this.userId=r,this.sinkId=n,this.pc1=new RTCPeerConnection,this.pc1.onicecandidate=e=>{null!=e.candidate&&this.pc2.addIceCandidate(e.candidate)},this.pc2=new RTCPeerConnection,this.pc2.onicecandidate=e=>{null!=e.candidate&&this.pc1.addIceCandidate(e.candidate)},this.pc2.ontrack=this.handleTrack,this.input=new i.A(e),this.input.setSource(t),this.input.on("stream",this.handleStream),this.input.enable(),this.audioContext=e}stop(){for(let e of(this.pc1.close(),this.pc2.close(),this.input.destroy(),Object.keys(this.outputs)))this.destroyOutput(e)}setAudioSource(e){this.input.setSource(e)}setAudioSink(e){for(let t of(this.sinkId=e,Object.keys(this.outputs)))this.outputs[t].setSinkId(e)}setNoiseCancellation(e){this.input.noiseCancellation=e}createOutput(e,t){let n=this.outputs[e];null==n&&((n=new s.A(this.userId,this.audioContext)).mute=!1,n.volume=100,n.setSpeakingFlags(a.ME.VOICE),n.setSinkId(this.sinkId),this.outputs[e]=n),n.addTrack(t),n.play()}destroyOutput(e,t){let n=this.outputs[e];null!=n&&(null==t||0===n.removeTrack(t))&&(n.destroy(),delete this.outputs[e])}handshake(){let e=e=>{let t=e.sdp.split("\n");for(let e=0;e<t.length;e++){let n=t[e];if(/^a=mid:/.test(n))break}return e.sdp=t.join("\n"),e};this.pc1.createOffer(o).then(t=>{this.pc1.setLocalDescription(e(t)).then(()=>{this.pc2.setRemoteDescription(t).then(()=>{this.pc2.createAnswer().then(e=>{this.pc2.setLocalDescription(e).then(()=>{this.pc1.setRemoteDescription(e)})})})})})}handleStream=()=>{let e=this.input.getDelayedStream();this.senders.forEach(e=>this.pc1.removeTrack(e)),this.senders=[...e.getAudioTracks().map(t=>this.pc1.addTrack(t,e))],this.handshake()};handleTrack=e=>{e.streams[0].getTracks().forEach(e=>{this.createOutput(e.id,e),e.onmute=()=>{this.destroyOutput(e.id,e)}})}}}