webrtc

How to temporarily cancel sending of video track in webRTC?


I have created webRtc connection (javascript) and I want to temporarily cancel sending of video track (audio track must remain) to minimize network traffic. Which actions should I perform to do this: (1) do videoTrack.enabled = false; (videoTrack is MediaStreamTrack) and then do:videoTrack.enabled = true; to restore it or (2) call videoTrack.stop() ? What is the differense between (1) and (2) - which case will minimize network traffic ? How should I restore video sending in the case (2) ?


Solution

  • Setting .enabled to false will send black frames instead of the actual video which reduces bandwidth usage considerably. Try it on https://webrtc.github.io/samples/src/content/peerconnection/bandwidth/ by pasting localStream.getVideoTracks()[0].enabled = false which shows around 12kbps plus overhead for headers.

    It will still send packets for every frame which may be undesirable in some cases. Using RTCRtpSender's replaceTrack(null) or stopping the track reduces that even further.

    Stopping the track has the additional advantage that it will turn off the camera light which is what users expect privacy-wise but you will need to call replaceTrack again after re-aquiring video with another call to getUserMedia.