eaglercraft-1.5/stable-download/web/eagswebrtc.js

1 line
5.9 KiB
JavaScript
Raw Normal View History

"use strict";window.initializeVoiceClient=(()=>{const e=0,t=-1,i=1,n=0,r=1,a=2;class c{constructor(e,t,i,c){this.client=e,this.peerId=t,this.peerConnection=i,this.stream=null;const s=this;this.peerConnection.addEventListener("icecandidate",e=>{e.candidate&&s.client.iceCandidateHandler(s.peerId,JSON.stringify({sdpMLineIndex:e.candidate.sdpMLineIndex,candidate:e.candidate.candidate}))}),this.peerConnection.addEventListener("track",e=>{s.rawStream=e.streams[0];const t=new Audio;t.autoplay=!0,t.muted=!0,t.onended=function(){t.remove()},t.srcObject=s.rawStream,s.client.peerTrackHandler(s.peerId,s.rawStream)}),this.peerConnection.addStream(this.client.localMediaStream.stream),c&&this.peerConnection.createOffer(e=>{const t=e;s.peerConnection.setLocalDescription(t,()=>{s.client.descriptionHandler(s.peerId,JSON.stringify(t)),s.client.peerStateInitial!=r&&(s.client.peerStateInitial=r)},e=>{console.error('Failed to set local description for "'+s.peerId+'"! '+e),s.client.peerStateInitial==a&&(s.client.peerStateInitial=n),s.client.signalDisconnect(s.peerId)})},e=>{console.error('Failed to set create offer for "'+s.peerId+'"! '+e),s.client.peerStateInitial==a&&(s.client.peerStateInitial=n),s.client.signalDisconnect(s.peerId)}),this.peerConnection.addEventListener("connectionstatechange",e=>{"disconnected"===e.connectionState?s.client.signalDisconnect(s.peerId):"connected"===e.connectionState?s.client.peerState!=r&&(s.client.peerState=r):"failed"===e.connectionState&&(s.client.peerState==a&&(s.client.peerState=n),s.client.signalDisconnect(s.peerId))})}disconnect(){this.peerConnection.close()}mute(e){this.rawStream.getAudioTracks()[0].enabled=!e}setRemoteDescription(e){const t=this;try{const i=JSON.parse(e);this.peerConnection.setRemoteDescription(i,()=>{"offer"==i.type&&t.peerConnection.createAnswer(e=>{const i=e;t.peerConnection.setLocalDescription(i,()=>{t.client.descriptionHandler(t.peerId,JSON.stringify(i)),t.client.peerStateDesc!=r&&(t.client.peerStateDesc=r)},e=>{console.error('Failed to set local description for "'+t.peerId+'"! '+e),t.client.peerStateDesc==a&&(t.client.peerStateDesc=n),t.client.signalDisconnect(t.peerId)})},e=>{console.error('Failed to create answer for "'+t.peerId+'"! '+e),t.client.peerStateDesc==a&&(t.client.peerStateDesc=n),t.client.signalDisconnect(t.peerId)})},e=>{console.error('Failed to set remote description for "'+t.peerId+'"! '+e),t.client.peerStateDesc==a&&(t.client.peerStateDesc=n),t.client.signalDisconnect(t.peerId)})}catch(e){console.error('Failed to parse remote description for "'+t.peerId+'"! '+e),t.client.peerStateDesc==a&&(t.client.peerStateDesc=n),t.client.signalDisconnect(t.peerId)}}addICECandidate(e){try{this.peerConnection.addIceCandidate(new RTCIceCandidate(JSON.parse(e))),this.client.peerStateIce!=r&&(this.client.peerStateIce=r)}catch(e){console.error('Failed to parse ice candidate for "'+this.peerId+'"! '+e),this.client.peerStateIce==a&&(this.client.peerStateIce=n),this.client.signalDisconnect(this.peerId)}}}window.constructVoiceClient=(()=>new class{constructor(){this.ICEServers=[],this.hasInit=!1,this.peerList=new Map,this.readyState=e,this.peerState=a,this.peerStateConnect=a,this.peerStateInitial=a,this.peerStateDesc=a,this.peerStateIce=a,this.iceCandidateHandler=null,this.descriptionHandler=null,this.peerTrackHandler=null,this.peerDisconnectHandler=null,this.microphoneVolumeAudioContext=null}voiceClientSupported(){return void 0!==window.RTCPeerConnection&&void 0!==navigator.mediaDevices&&void 0!==navigator.mediaDevices.getUserMedia}setICEServers(e){this.ICEServers.length=0;for(var t=0;t<e.length;++t){var i=e[t].split(";");1==i.length?this.ICEServers.push({urls:i[0]}):3==i.length&&this.ICEServers.push({urls:i[0],username:i[1],credential:i[2]})}}setICECandidateHandler(e){this.iceCandidateHandler=e}setDescriptionHandler(e){this.descriptionHandler=e}setPeerTrackHandler(e){this.peerTrackHandler=e}setPeerDisconnectHandler(e){this.peerDisconnectHandler=e}activateVoice(e){this.hasInit&&(this.localRawMediaStream.getAudioTracks()[0].enabled=e)}initializeDevices(){if(this.hasInit)this.readySta