Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RTCPeerConnection.removeTrack() to remove video or audio or both?

I'm studying WebRTC and try to figure how it works.

I modified this sample on WebRTC.github.io to make getUserMedia source of leftVideo and streaming to rightVideo.It works.

And I want to add some feature, like when I press pause on leftVideo(My browser is Chrome 69)

I change apart of Call()

...
stream.getTracks().forEach(track => {
    pc1Senders.push(pc1.addTrack(track, stream));
});
...

And add function on leftVideo

leftVideo.onpause = () => {
  pc1Senders.map(sender => pc1.removeTrack(sender));
}

I don't want to close the connection, I just want to turn off only video or audio.

But after I pause leftVideo, the rightVideo still gets track. Am I doing wrong here, or maybe other place?

Thanks for your helping.

like image 875
iamshaojin Avatar asked Dec 14 '22 14:12

iamshaojin


1 Answers

First, you need to get the stream of the peer. You can mute/hide the stream using the enabled attribute of the MediaStreamTrack. Use the below code snippet toggle media.

/* stream: MediaStream, type:trackType('audio'/'video') */
toggleTrack(stream,type) {
    stream.getTracks().forEach((track) => {
        if (track.kind === type) {
            track.enabled = !track.enabled;
        }
    });
}
like image 166
Fan Jin Avatar answered Dec 16 '22 03:12

Fan Jin