...
Code Block |
---|
|
//join room
room.join();
|
Code Block |
---|
language | js |
---|
theme | RDark
subscribeTrackToEndedEvent function
This is a helper function that subscribes new local track to "ended" event. Once event fired we remove track from peer connection and kickoff renegotiation.
Code Block |
---|
|
Code Block |
---|
|
Code Block |
---|
|
Code Block |
---|
|
Code Block |
---|
|
language | js |
---|
theme | RDarkconst subscribeTrackToEndedEvent = function(room, track, pc) {
track.addEventListener("ended", function() {
//track ended, see if we need to cleanup
let negotiate = false;
for (const sender of pc.getSenders()) {
if (sender.track === track) {
pc.removeTrack(sender);
//track found, set renegotiation flag
negotiate = true;
break;
}
}
if (negotiate) {
//kickoff renegotiation
room.updateState();
}
});
}; |
addTrackToPeerConnection function
This is a helper function which adds new local track to peer connection.
Code Block |
---|
|
const addTrackToPeerConnection = function(pc, stream, track, encodings) {
pc.addTransceiver(track, {
direction: "sendonly",
streams: [stream],
sendEncodings: encodings ? encodings : [] //passing encoding types for video simulcast tracks
});
} |