What's the difference of 'addStream' and 'addTrack' of 'RTCPeerConnection'?

328 views
Skip to first unread message

天堂的回响

unread,
Apr 9, 2021, 8:30:43 PM4/9/21
to discuss-webrtc
I want to replace the audio track of an RTCRtpSender, so I use 'replaceTrack' method of RTCRtpSender, but I found that this method only works if I use 'addTrack' of RTCPeerConnection to add local audio track to it firstly, if I use 'addStream' to add the whole stream got from 'getUserMedia', then 'replaceTrack' will not work, and it cause audio track stopped(Remote peer will not get any audio anymore).My code is like this:
1.Use 'addStream':
navigator.mediaDevices.getUserMedia({audio:true}).then(function(oldStream){
    pc.addStream(oldStream);
    oldStream.getAudioTracks()[0].stop();
    navigator.mediaDevices.getUserMedia({audio:true}).then(function(newStream){
        var newAudioTrack = newStream.getAudioTracks()[0];
        var sender = pc.getSenders().find(function (s) {
            return s.track.kind == newAudioTrack.kind;
        });
        sender.replaceTrack(newAudioTrack);
    });
});

2.Use 'addTrack':
navigator.mediaDevices.getUserMedia({audio:true}).then(function(oldStream){
    pc.addTrack(oldStream.getAudioTracks()[0], oldStream);
    oldStream.getAudioTracks()[0].stop();
    navigator.mediaDevices.getUserMedia({audio:true}).then(function(newStream){
        var newAudioTrack = newStream.getAudioTracks()[0];
        var sender = pc.getSenders().find(function (s) {
            return s.track.kind == newAudioTrack.kind;
        });
        sender.replaceTrack(newAudioTrack);
    });
});

Sean DuBois

unread,
Apr 9, 2021, 10:12:36 PM4/9/21
to 'Neil Young' via discuss-webrtc
You could do addTransceiver('audio') and do replaceTrack on the RTCRTPSender that would be created from that.

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/7a6595ae-3700-4ffc-9725-da9be2e97a0cn%40googlegroups.com.

天堂的回响

unread,
Apr 10, 2021, 6:00:41 AM4/10/21
to discuss-webrtc
addTransceiver('audio') is redundent for after addStream called the transceiver is created automadically.This extra operation make negotiation failed :(

THANKS for your advice!

Reply all
Reply to author
Forward
0 new messages