peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.5k stars 1.43k forks source link

How to switch from video/audio streaming to screen/audio streaming and back ? #853

Open matheuscamarques opened 3 years ago

matheuscamarques commented 3 years ago

How to switch from video/audio streaming to screen/audio streaming and back ?

matheuscamarques commented 3 years ago

any old issue didn't help me much

haf-decent commented 2 years ago

If you're still looking for an answer, I was able to swap audio/video tracks at will using a function like this:

const peers = []; // list of connected peers

function replaceStream(newStream) {
    const [ audioTrack = null ] = newStream.getAudioTracks();
    const [ videoTrack = null ] = newStream.getVideoTracks();
    peers.forEach(peer => {
        const [ audioSender, videoSender ] = peer.call.peerConnection.getSenders();
        audioSender.replaceTrack(audioTrack);
        videoSender.replaceTrack(videoTrack);
    });
}

where peer.call is the MediaConnection created when calling/answering a remote peer (i.e. myPeer.on('call', call => console.log(call)) or const call = myPeer.call(remoteId, ...)). Hope that helps.