mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-18 00:55:40 +00:00
Inline start/stop webrtc methods
This commit is contained in:
parent
440102fc1b
commit
4c699d634b
1 changed files with 72 additions and 76 deletions
|
|
@ -22,80 +22,6 @@ let inputReady = false;
|
|||
|
||||
let onData;
|
||||
|
||||
const start = (iceservers) => {
|
||||
log.debug('[rtc] <- ICE servers', iceservers);
|
||||
const servers = iceservers || [];
|
||||
connection = new RTCPeerConnection({iceServers: servers});
|
||||
mediaStream = new MediaStream();
|
||||
|
||||
connection.ondatachannel = e => {
|
||||
log.debug('[rtc] ondatachannel', e.channel.label)
|
||||
e.channel.binaryType = "arraybuffer";
|
||||
|
||||
if (e.channel.label === 'keyboard') {
|
||||
keyboardChannel = e.channel
|
||||
return
|
||||
}
|
||||
|
||||
if (e.channel.label === 'mouse') {
|
||||
mouseChannel = e.channel
|
||||
return
|
||||
}
|
||||
|
||||
dataChannel = e.channel;
|
||||
dataChannel.onopen = () => {
|
||||
log.debug('[rtc] the input channel has been opened');
|
||||
inputReady = true;
|
||||
pub(WEBRTC_CONNECTION_READY)
|
||||
};
|
||||
if (onData) {
|
||||
dataChannel.onmessage = onData;
|
||||
}
|
||||
dataChannel.onclose = () => {
|
||||
inputReady = false
|
||||
log.debug('[rtc] the input channel has been closed')
|
||||
}
|
||||
}
|
||||
connection.oniceconnectionstatechange = ice.onIceConnectionStateChange;
|
||||
connection.onicegatheringstatechange = ice.onIceStateChange;
|
||||
connection.onicecandidate = ice.onIceCandidate;
|
||||
connection.onicecandidateerror = ice.onIceCandidateError;
|
||||
connection.onconnectionstatechange = _ => {
|
||||
console.debug(`[rtc] connection state -> ${connection.connectionState}`)
|
||||
}
|
||||
connection.ontrack = event => {
|
||||
mediaStream.addTrack(event.track);
|
||||
}
|
||||
};
|
||||
|
||||
const stop = () => {
|
||||
if (mediaStream) {
|
||||
mediaStream.getTracks().forEach(t => {
|
||||
t.stop();
|
||||
mediaStream.removeTrack(t);
|
||||
});
|
||||
mediaStream = null;
|
||||
}
|
||||
if (connection) {
|
||||
connection.close();
|
||||
connection = null;
|
||||
}
|
||||
if (dataChannel) {
|
||||
dataChannel.close()
|
||||
dataChannel = null
|
||||
}
|
||||
if (keyboardChannel) {
|
||||
keyboardChannel?.close()
|
||||
keyboardChannel = null
|
||||
}
|
||||
if (mouseChannel) {
|
||||
mouseChannel?.close()
|
||||
mouseChannel = null
|
||||
}
|
||||
candidates = [];
|
||||
log.info('[rtc] WebRTC has been closed');
|
||||
}
|
||||
|
||||
const ice = (() => {
|
||||
const ICE_TIMEOUT = 3000;
|
||||
let timeForIceGathering;
|
||||
|
|
@ -155,7 +81,51 @@ const ice = (() => {
|
|||
* WebRTC connection module.
|
||||
*/
|
||||
export const webrtc = {
|
||||
start,
|
||||
start: (iceservers) => {
|
||||
log.debug('[rtc] <- ICE servers', iceservers);
|
||||
const servers = iceservers || [];
|
||||
connection = new RTCPeerConnection({iceServers: servers});
|
||||
mediaStream = new MediaStream();
|
||||
|
||||
connection.ondatachannel = e => {
|
||||
log.debug('[rtc] ondatachannel', e.channel.label)
|
||||
e.channel.binaryType = "arraybuffer";
|
||||
|
||||
if (e.channel.label === 'keyboard') {
|
||||
keyboardChannel = e.channel
|
||||
return
|
||||
}
|
||||
|
||||
if (e.channel.label === 'mouse') {
|
||||
mouseChannel = e.channel
|
||||
return
|
||||
}
|
||||
|
||||
dataChannel = e.channel;
|
||||
dataChannel.onopen = () => {
|
||||
log.debug('[rtc] the input channel has been opened');
|
||||
inputReady = true;
|
||||
pub(WEBRTC_CONNECTION_READY)
|
||||
};
|
||||
if (onData) {
|
||||
dataChannel.onmessage = onData;
|
||||
}
|
||||
dataChannel.onclose = () => {
|
||||
inputReady = false
|
||||
log.debug('[rtc] the input channel has been closed')
|
||||
}
|
||||
}
|
||||
connection.oniceconnectionstatechange = ice.onIceConnectionStateChange;
|
||||
connection.onicegatheringstatechange = ice.onIceStateChange;
|
||||
connection.onicecandidate = ice.onIceCandidate;
|
||||
connection.onicecandidateerror = ice.onIceCandidateError;
|
||||
connection.onconnectionstatechange = _ => {
|
||||
console.debug(`[rtc] connection state -> ${connection.connectionState}`)
|
||||
}
|
||||
connection.ontrack = event => {
|
||||
mediaStream.addTrack(event.track);
|
||||
}
|
||||
},
|
||||
setRemoteDescription: async (data, media) => {
|
||||
log.debug('[rtc] remote SDP', data)
|
||||
const decodedSDP = JSON.parse(atob(data))
|
||||
|
|
@ -214,7 +184,33 @@ export const webrtc = {
|
|||
if (!connected) return Promise.resolve();
|
||||
return await connection.getStats()
|
||||
},
|
||||
stop,
|
||||
stop: () => {
|
||||
if (mediaStream) {
|
||||
mediaStream.getTracks().forEach(t => {
|
||||
t.stop();
|
||||
mediaStream.removeTrack(t);
|
||||
});
|
||||
mediaStream = null;
|
||||
}
|
||||
if (connection) {
|
||||
connection.close();
|
||||
connection = null;
|
||||
}
|
||||
if (dataChannel) {
|
||||
dataChannel.close()
|
||||
dataChannel = null
|
||||
}
|
||||
if (keyboardChannel) {
|
||||
keyboardChannel?.close()
|
||||
keyboardChannel = null
|
||||
}
|
||||
if (mouseChannel) {
|
||||
mouseChannel?.close()
|
||||
mouseChannel = null
|
||||
}
|
||||
candidates = [];
|
||||
log.info('[rtc] WebRTC has been closed');
|
||||
},
|
||||
set onData(fn) {
|
||||
onData = fn
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue