eaglercraft/stable-download/web/eagswebrtc.js
LAX1DUDE b65f67def9 22w29a ADDED VOICE CHAT LOLOLOL :> (update bungee)
bungee now also supports 1.8 protocolsupport servers
2022-07-21 22:11:31 -07:00

1 line
No EOL
5.9 KiB
JavaScript

"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.readyState=i;else{const e=this;navigator.mediaDevices.getUserMedia({audio:!0,video:!1}).then(t=>{e.microphoneVolumeAudioContext=new AudioContext,e.localRawMediaStream=t,e.localRawMediaStream.getAudioTracks()[0].enabled=!1,e.localMediaStream=e.microphoneVolumeAudioContext.createMediaStreamDestination(),e.localMediaStreamGain=e.microphoneVolumeAudioContext.createGain(),e.microphoneVolumeAudioContext.createMediaStreamSource(t).connect(e.localMediaStreamGain),e.localMediaStreamGain.connect(e.localMediaStream),e.localMediaStreamGain.gain.value=1,e.readyState=i,this.hasInit=!0}).catch(i=>{console.error(i),e.readyState=t})}}setMicVolume(e){this.hasInit&&(e>.5&&(e=.5+2*(e-.5)),e>1.5&&(e=1.5),e<0&&(e=0),this.localMediaStreamGain.gain.value=2*e)}resetPeerStates(){this.peerState=this.peerStateConnect=this.peerStateInitial=this.peerStateDesc=this.peerStateIce=a}getPeerState(){return this.peerState}getPeerStateConnect(){return this.peerStateConnect}getPeerStateInitial(){return this.peerStateInitial}getPeerStateDesc(){return this.peerStateDesc}getPeerStateIce(){return this.peerStateIce}getReadyState(){return this.readyState}signalConnect(e,t){this.hasInit||initializeDevices();try{const i=new RTCPeerConnection({iceServers:this.ICEServers,optional:[{DtlsSrtpKeyAgreement:!0}]}),s=new c(this,e,i,t);this.peerList.set(e,s),this.peerStateConnect!=r&&(this.peerStateConnect=r)}catch(e){this.peerStateConnect==a&&(this.peerStateConnect=n)}}signalDescription(e,t){var i=this.peerList.get(e);null!=i&&i.setRemoteDescription(t)}signalDisconnect(e,t){var i=this.peerList.get(e);if(null!=i){this.peerList.delete(i);try{i.disconnect()}catch(e){}this.peerDisconnectHandler(e,t)}}mutePeer(e,t){var i=this.peerList.get(e);null!=i&&i.mute(t)}signalICECandidate(e,t){var i=this.peerList.get(e);null!=i&&i.addICECandidate(t)}})}),window.startVoiceClient=(()=>("function"!=typeof window.constructVoiceClient&&window.initializeVoiceClient(),window.constructVoiceClient()));