replaceTrack only works once
See original GitHub issueI’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.log
statement 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:
- Created 3 years ago
- Comments:16
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.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 streammystream.removeTrack(self.user.stream.getVideoTracks()[0])
And finally add the new track to my visible streammystream.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”