Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

RDark
Code Block
languagejs
themeRDark
//join room
room.join();
Code Block
languagejs
theme

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.

RDark
Code Block
languagejs
themeRDark
Code Block
languagejs
themeRDark
Code Block
languagejs
themeRDark
Code Block
languagejs
themeRDark
Code Block
languagejs
theme
const 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
languagejs
themeRDark
const addTrackToPeerConnection = function(pc, stream, track, encodings) {
    pc.addTransceiver(track, {
        direction: "sendonly",
        streams: [stream],
        sendEncodings: encodings ? encodings : [] //passing encoding types for video simulcast tracks
    });
}