Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
leonsal opened this issue May 19, 2020 · 4 comments
Closed

Comments

@leonsal
Copy link

leonsal commented May 19, 2020

Hi,

During a call between two peers streaming video/audio, if one of them
wants to share his screen, he could use getDisplayMedia() to get
a new stream with screen/audio. How to send this new stream to
the peer instead of the current video/audio stream ?
Is it necessary to close the current peer connection and create a
new one with new stream ?

Regards.

@Florrr
Copy link

Florrr commented May 20, 2020

Its not necessary to create a new connection - you could just replace the tracks of the stream:

function replaceStream(peerConnection, mediaStream) {
        for(sender of peerConnection.getSenders()){
            if(sender.track.kind == "audio") {
                if(mediaStream.getAudioTracks().length > 0){
                    sender.replaceTrack(mediaStream.getAudioTracks()[0]);
                }
            }
            if(sender.track.kind == "video") {
                if(mediaStream.getVideoTracks().length > 0){
                    sender.replaceTrack(mediaStream.getVideoTracks()[0]);
                }
            }
        }
}

EDIT:
Also: Thats not a peerjs-related question. You could just search for "WebRTC change video stream in the middle of communication" for an appropriate answer

@leonsal
Copy link
Author

leonsal commented May 20, 2020

It worked OK! Thanks!

@leonsal leonsal closed this as completed May 20, 2020
@muhammadsaqib95
Copy link

It works fine but let me explain for the floks who are trying this with PeerJS.
function replaceStream() { navigator.mediaDevices.getDisplayMedia(device).then((stream) => { callSate.peerConnection.getSenders().forEach((sender) => { if(sender.track.kind === "audio" && stream.getAudioTracks().length > 0){ sender.replaceTrack(stream.getAudioTracks()[0]); } if (sender.track.kind === "video" && stream.getVideoTracks().length > 0) { sender.replaceTrack(stream.getVideoTracks()[0]); } }); videoRef.current.srcObject = stream; videoRef.current.play(); }); }

  1. Here callSate is state/variable holds the value of call object which is returned when we make call or answer call of peer.
  2. you can change getDisplayMedia to getUserMedia according to your need.
  3. videoRef holds the reference to video displayed on frontend.

@Ritesh560
Copy link

can @muhammadsaqib95 or anyone tell me how we can handle the case when the call has only audio track previously and we need to add our screen recording video track to the call stream.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants