question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

replaceTrack only works once

See original GitHub issue

I’m looking at implementing a “switch between front and rear camera” feature.

When user taps a button to switch the camera:

  • I run navigator.mediaDevices.getUserMedia with appropriate constraint
  • when getUserMedia returns a stream I run replaceTrack like so:
navigator.mediaDevices.getUserMedia({
    video: { facingMode: facingMode },
    audio: true
}).then(function(stream) {
    if (p && localStream) {
        p.replaceTrack(localStream.getVideoTracks()[0], stream.getVideoTracks()[0], localStream);
        console.log("Done");
    }

    localStream = stream;
}).catch(() => {})

In the example, p is a Peer instance, and localStream is a local variable that keeps track of the current local stream. The above works, but only once. When I try to switch camera the second time, the replaceTrack doesn’t return (the console.logstatement doesn’t run), and there are no errors logged in console.

Am I using it wrong? Is there an example of an idiomatic use of replaceTrack?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16

github_iconTop GitHub Comments

2reactions
cuu508commented, Apr 30, 2020

Thanks for the suggestion! I had previously tried to use addStream(), but couldn’t get it to work – I have a hunch that’s because of my super-simplistic messaging layer which doesn’t handle bursts of messages well.

But back to the topic, I think it would be great if there was an example of replaceTrack() usage in README, with a note about what can and should go in the third parameter.

0reactions
EdoardoEntusiastacommented, Nov 3, 2022

I had a similar issue and managed to solve this way

First I replace the track peer.replaceTrack(self.user.stream.getVideoTracks()[0], stream.getVideoTracks()[0], self.user.stream); Then I remove the track from my current local stream mystream.removeTrack(self.user.stream.getVideoTracks()[0]) And finally add the new track to my visible stream mystream.addTrack(stream.getVideoTracks()[0])

My error was that I was removing the track from the current local stream before replacing it in the peer, receiving the infamous error: “Cannot replace a track that was never added”

Read more comments on GitHub >

github_iconTop Results From Across the Web

RTCPeerConnection replaceTrack only changing stream for ...
By design replaceTrack replaces the stream on the RTCPeerConnection. This does not affect the local video object. Reset the srcObject on the ...
Read more >
Warm-up with dummy tracks and replaceTrack
It lets you switch out an already-sending track without needing to renegotiate, and still see the change reflected remotely. We're not fully ...
Read more >
webrtc: replaceTrack with an ended track does not work
I'm not sure what is expected when both video tracks are on the same stream (as opposed to two streams, one video track...
Read more >
10 REAPER TIPS YOU SHOULD KNOW! How to ... - YouTube
How to Search & Replace Track Names, Loudness Utility, Cubase-like ... tips and tricks will help make your life easier working in REAPER....
Read more >
Remove, Add, Replace Tracks in case of External Stream
but here I am using only one RTP sender so directly I am taking getSender()[0] ... It's working now by using replaceTrack but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found