Moved audio sharing to new network model

This commit is contained in:
Mitchell McCaffrey
2020-12-12 16:30:55 +11:00
parent 5e244617ae
commit bdba2ecc13
2 changed files with 50 additions and 24 deletions

View File

@@ -108,24 +108,11 @@ class Session extends EventEmitter {
}
}
/**
* Send data to all connected peers
*
* @param {string} id - the id of the event to send
* @param {object} data
* @param {string} channel
*/
send(id, data, channel) {
for (let peer of Object.values(this.peers)) {
peer.connection.send({ id, data }, channel);
}
}
/**
* Send data to a single peer
*
* @param {string} sessionId - the socket id of the player to send to
* @param {string} eventId - the id of the event to send
* @param {string} sessionId - The socket id of the player to send to
* @param {string} eventId - The id of the event to send
* @param {object} data
* @param {string} channel
*/
@@ -140,6 +127,37 @@ class Session extends EventEmitter {
}
}
/**
* Start streaming to a peer
*
* @param {string} sessionId - The socket id of the player to stream to
* @param {MediaStreamTrack} track
* @param {MediaStream} stream
*/
startStreamTo(sessionId, track, stream) {
if (!(sessionId in this.peers)) {
this._addPeer(sessionId, true);
this.peers[sessionId].connection.once("connect", () => {
this.peers[sessionId].connection.addTrack(track, stream);
});
} else {
this.peers[sessionId].connection.addTrack(track, stream);
}
}
/**
* End streaming to a peer
*
* @param {string} sessionId - The socket id of the player to stream to
* @param {MediaStreamTrack} track
* @param {MediaStream} stream
*/
endStreamTo(sessionId, track, stream) {
if (sessionId in this.peers) {
this.peers[sessionId].connection.removeTrack(track, stream);
}
}
/**
* Join a party
*