Simple-peer: addTrack and removeTrack not working

Created on 4 Apr 2020  路  4Comments  路  Source: feross/simple-peer

i have tried to on/off video using addTrack and remove Track but not working

    `   document.getElementById('stopVedio').addEventListener('click', function () {
             peer1.removeTrack(stream.getVideoTracks()[0], stream);
            })
             document.getElementById('startVedio').addEventListener('click', function () {
                 peer1.addTrack(stream.getVideoTracks()[0] stream);
            })`

error : Track has been removed. You should enable/disable tracks that you want to re-add

question

All 4 comments

Indeed, you should better enable/disable tracks by changing enabled field instead of adding/removing them, which in turn requires renegotiation and is more nuanced than you may want it to be.
Adding/removing should also work, but you will need to create a new track for that.

@nazar-pc Is there a workaround which allows adding of previously removed mediaTracks?

For background: I'm attempting a full mesh topology, in which each peer-to-peer stream quality is determined by the distance between the two peers in a 3D environment. The whole project is similar to VR chat or Mozilla Hubs. I had planned to store a high, middle and low quality stream and swap them out for each peer. Enabling / disabling mediaStream tracks doesn't work in this situation because it would affect all peer connections using a given mediaStream.

Thanks!

@AidanNelson sounds like you want to use replaceTrack() API instead of adding/removing it. Also you can clone it and then it would appear as a new distinct track while being the same internally: https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/clone

That seems to work. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

onel picture onel  路  5Comments

olgeorge picture olgeorge  路  5Comments

mullerivan picture mullerivan  路  4Comments

contra picture contra  路  6Comments

measwel picture measwel  路  4Comments