eaglercraft-1.5/javascript/eagswebrtc.js

269 lines
7.8 KiB
JavaScript
Raw Normal View History

2022-07-16 22:02:55 -07:00
"use strict";
/*
This is the backend for voice channels in eaglercraft, it links with TeaVM EaglerAdapter at runtime
2022-07-17 17:57:05 -07:00
Copyright 2022 Calder Young & ayunami2000. All rights reserved.
2022-07-16 22:02:55 -07:00
Based on code written by ayunami2000
*/
window.initializeVoiceClient = (() => {
const READYSTATE_NONE = 0;
const READYSTATE_ABORTED = -1;
const READYSTATE_DEVICE_INITIALIZED = 1;
const TASKSTATE_NONE = -1;
const TASKSTATE_LOADING = 0;
const TASKSTATE_COMPLETE = 1;
const TASKSTATE_FAILED = 2;
class EaglercraftVoicePeer {
2022-07-17 17:57:05 -07:00
constructor(client, peerId, peerConnection, offer) {
2022-07-16 22:02:55 -07:00
this.client = client;
this.peerId = peerId;
this.peerConnection = peerConnection;
2022-07-17 17:57:05 -07:00
this.stream = null;
2022-07-16 22:02:55 -07:00
const self = this;
this.peerConnection.addEventListener("icecandidate", (evt) => {
if(evt.candidate) {
2022-07-17 17:57:05 -07:00
self.client.iceCandidateHandler(self.peerId, JSON.stringify({ sdpMLineIndex: evt.candidate.sdpMLineIndex, candidate: evt.candidate.candidate }));
2022-07-16 22:02:55 -07:00
}
});
this.peerConnection.addEventListener("track", (evt) => {
2022-07-17 17:57:05 -07:00
self.rawStream = evt.streams[0];
const aud = new Audio();
aud.autoplay = true;
aud.muted = true;
aud.onended = function() {
aud.remove();
};
aud.srcObject = self.rawStream;
self.client.peerTrackHandler(self.peerId, self.rawStream);
2022-07-16 22:02:55 -07:00
});
2022-07-17 17:57:05 -07:00
this.peerConnection.addStream(this.client.localMediaStream.stream);
if (offer) {
this.peerConnection.createOffer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.client.descriptionHandler(self.peerId, JSON.stringify(selfDesc));
}, (err) => {
console.error("Failed to set local description for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
});
2022-07-16 22:02:55 -07:00
}, (err) => {
2022-07-17 17:57:05 -07:00
console.error("Failed to set create offer for \"" + self.peerId + "\"! " + err);
2022-07-16 22:02:55 -07:00
self.client.signalDisconnect(self.peerId);
});
2022-07-17 17:57:05 -07:00
}
2022-07-16 22:02:55 -07:00
this.peerConnection.addEventListener("connectionstatechange", (evt) => {
if(evt.connectionState === 'disconnected') {
self.client.signalDisconnect(self.peerId);
}
});
}
disconnect() {
this.peerConnection.close();
}
2022-07-17 17:57:05 -07:00
mute(muted) {
this.rawStream.getAudioTracks()[0].enabled = !muted;
}
2022-07-16 22:02:55 -07:00
setRemoteDescription(descJSON) {
const self = this;
2022-07-17 17:57:05 -07:00
try {
const remoteDesc = JSON.parse(descJSON);
this.peerConnection.setRemoteDescription(remoteDesc, () => {
if(remoteDesc.type == 'offer') {
self.peerConnection.createAnswer((desc) => {
const selfDesc = desc;
self.peerConnection.setLocalDescription(selfDesc, () => {
self.client.descriptionHandler(self.peerId, JSON.stringify(selfDesc));
}, (err) => {
console.error("Failed to set local description for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
});
2022-07-16 22:02:55 -07:00
}, (err) => {
2022-07-17 17:57:05 -07:00
console.error("Failed to create answer for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
2022-07-16 22:02:55 -07:00
});
2022-07-17 17:57:05 -07:00
}
}, (err) => {
console.error("Failed to set remote description for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
});
} catch (err) {
console.error("Failed to parse remote description for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
}
2022-07-16 22:02:55 -07:00
}
addICECandidate(candidate) {
2022-07-17 17:57:05 -07:00
try {
this.peerConnection.addIceCandidate(new RTCIceCandidate(JSON.parse(candidate)));
} catch (err) {
console.error("Failed to parse ice candidate for \"" + self.peerId + "\"! " + err);
self.client.signalDisconnect(self.peerId);
}
2022-07-16 22:02:55 -07:00
}
}
class EaglercraftVoiceClient {
constructor() {
this.ICEServers = [];
this.hasInit = false;
this.peerList = new Map();
this.readyState = READYSTATE_NONE;
this.taskState = TASKSTATE_NONE;
this.iceCandidateHandler = null;
this.descriptionHandler = null;
this.peerTrackHandler = null;
this.peerDisconnectHandler = null;
2022-07-18 08:07:59 -07:00
this.microphoneVolumeAudioContext = null;
2022-07-16 22:02:55 -07:00
}
voiceClientSupported() {
return typeof window.RTCPeerConnection !== "undefined" && typeof navigator.mediaDevices !== "undefined" &&
typeof navigator.mediaDevices.getUserMedia !== "undefined";
}
setICEServers(urls) {
this.ICEServers.length = 0;
2022-07-17 23:49:05 -07:00
for(var i = 0; i < urls.length; ++i) {
var etr = urls[i].split(";");
if(etr.length == 1) {
this.ICEServers.push({ urls: etr[0] });
}else if(etr.length == 3) {
this.ICEServers.push({ urls: etr[0], username: etr[1], credential: etr[2] });
2022-07-17 17:57:05 -07:00
}
2022-07-16 22:02:55 -07:00
}
}
setICECandidateHandler(cb) {
this.iceCandidateHandler = cb;
}
setDescriptionHandler(cb) {
this.descriptionHandler = cb;
}
setPeerTrackHandler(cb) {
this.peerTrackHandler = cb;
}
setPeerDisconnectHandler(cb) {
this.peerDisconnectHandler = cb;
}
activateVoice(tk) {
2022-07-17 17:57:05 -07:00
if(this.hasInit) this.localRawMediaStream.getAudioTracks()[0].enabled = tk;
2022-07-16 22:02:55 -07:00
}
2022-07-17 17:57:05 -07:00
initializeDevices() {
2022-07-16 22:02:55 -07:00
if(!this.hasInit) {
this.taskState = TASKSTATE_LOADING;
const self = this;
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then((stream) => {
2022-07-18 08:07:59 -07:00
self.microphoneVolumeAudioContext = new AudioContext();
2022-07-16 22:02:55 -07:00
self.localRawMediaStream = stream;
self.localRawMediaStream.getAudioTracks()[0].enabled = false;
self.localMediaStream = self.microphoneVolumeAudioContext.createMediaStreamDestination();
self.localMediaStreamGain = self.microphoneVolumeAudioContext.createGain();
var localStreamIn = self.microphoneVolumeAudioContext.createMediaStreamSource(stream);
localStreamIn.connect(self.localMediaStreamGain);
self.localMediaStreamGain.connect(self.localMediaStream);
2022-07-17 17:57:05 -07:00
self.localMediaStreamGain.gain.value = 1.0;
2022-07-16 22:02:55 -07:00
self.readyState = READYSTATE_DEVICE_INITIALIZED;
self.taskState = TASKSTATE_COMPLETE;
this.hasInit = true;
2022-07-17 17:57:05 -07:00
}).catch((err) => {
console.error(err);
2022-07-16 22:02:55 -07:00
self.readyState = READYSTATE_ABORTED;
self.taskState = TASKSTATE_FAILED;
});
}else {
self.readyState = READYSTATE_DEVICE_INITIALIZED;
self.taskState = TASKSTATE_COMPLETE;
}
}
setMicVolume(val) {
2022-07-17 17:57:05 -07:00
if(this.hasInit) {
if(val > 0.5) val = 0.5 + (val - 0.5) * 2.0;
if(val > 1.5) val = 1.5;
if(val < 0.0) val = 0.0;
this.localMediaStreamGain.gain.value = val * 2.0;
}
2022-07-16 22:02:55 -07:00
}
getTaskState() {
return this.taskState;
}
getReadyState() {
return this.readyState;
}
2022-07-17 17:57:05 -07:00
signalConnect(peerId, offer) {
if (!this.hasInit) initializeDevices();
2022-07-16 22:02:55 -07:00
const peerConnection = new RTCPeerConnection({ iceServers: this.ICEServers, optional: [ { DtlsSrtpKeyAgreement: true } ] });
2022-07-17 17:57:05 -07:00
const peerInstance = new EaglercraftVoicePeer(this, peerId, peerConnection, offer);
2022-07-16 22:02:55 -07:00
this.peerList.set(peerId, peerInstance);
}
signalDescription(peerId, descJSON) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.setRemoteDescription(descJSON);
}
}
2022-07-17 17:57:05 -07:00
signalDisconnect(peerId, quiet) {
2022-07-16 22:02:55 -07:00
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
this.peerList.delete(thePeer);
try {
thePeer.disconnect();
}catch(e) {}
2022-07-17 17:57:05 -07:00
this.peerDisconnectHandler(peerId, quiet);
}
}
mutePeer(peerId, muted) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.mute(muted);
2022-07-16 22:02:55 -07:00
}
}
signalICECandidate(peerId, candidate) {
var thePeer = this.peerList.get(peerId);
if((typeof thePeer !== "undefined") && thePeer !== null) {
thePeer.addICECandidate(candidate);
}
}
}
window.constructVoiceClient = () => new EaglercraftVoiceClient();
});
window.startVoiceClient = () => {
if(typeof window.constructVoiceClient !== "function") {
window.initializeVoiceClient();
}
return window.constructVoiceClient();
};