canopy/src/app/channel/media/webRTCHandler.js

103 lines
No EOL
3.3 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
/**
* Object to relay WebRTC Negotiation data for a single channel
*/
class webRTCHandler{
/**
* Instantiates a new object to relay WebRTC Negotiation data for a single channel
* @param {channelManager} server - Parent server object
* @param {activeChannel} channel - Parent Channel object
*/
constructor(server, channel){
/**
* Parent Server Object
*/
this.server = server
/**
* Parent Channel Object
*/
this.channel = channel;
/**
* Declare class property to hold socked it of the current device/browser-tab we're streaming from
*/
this.currentStreamer;
}
/**
* Defines server-side socket.io listeners for newly connected sockets
* @param {Socket} socket - Newly connected socket to define listeners against
*/
defineListeners(socket){
//Queueing Functions
socket.on("relayRTCData", (data) => {this.relayRTCData(socket, data)});
socket.on("disconnect", (data) => {this.handleStreamerDisconnect(socket, data)});
}
handleStreamerDisconnect(socket){
//If the disconnecting socket is the current streamer
if(socket.id == this.currentStreamer){
//Tell the queue to stop current stream
this.channel.queue.stop();
}
}
startStream(socket){
this.currentStreamer = socket.id;
}
endStream(){
this.currentStreamer = null;
}
relayRTCData(socket, data){
//Set the recipient to the current streamer by default, assuming that this isn't coming from the streaming socket
let recipientID = this.currentStreamer;
//Create object to hold data being relayed to client
let webRTCData = {
type: data.type,
body: data.body
};
//if data is being relayed from streaming socket to a viewer
if(socket.id == this.currentStreamer){
//Change recipient to reflect streaming socket's request
recipientID = data.recipient;
//Otherwise if the data is being relayed from a viewer to the streaming socket
}else{
//Add viewers socket id
webRTCData.source = {
user: socket.user.user,
socket: socket.id
};
}
//Get recipient socket
const recipient = this.server.io.sockets.sockets.get(recipientID);
//If we have a recipient
if(recipient != null){
//Relay RTC Data
recipient.emit("RTCData", webRTCData);
}
}
}
module.exports = webRTCHandler;