I am making a WebRTC video call application using peerjs library and every peer can chose what stream to start the call with, so I have two streams audio and video stream:
const peer = new Peer()
let remotePeerConnection = null
let audioStream;
let videoStream;
try{
audioStream = await navigator.mediaDevices.getUserMedia({audio:true})
}
catch(err){
audioStream = null;
}
try{
videoStream = await navigator.mediaDevices.getUserMedia({video:true})
}
catch(err){
videoStream = null;
}
And then I mix them together
let mixedStream = new MediaStream()
if(audioStream){
mixedStream.addTrack(audioStream.getAudioTracks()[0])
}
if(videoStream){
mixedStream.addTrack(videoStream.getVideoTracks()[0])
}
const myVideo = document.getElementById("my-video")
myVideo.srcObject = mixedStream
myVideo.play()
and I have a call function to call other peer with the mixed stream which can be only video stream or only audio stream or both,
const call = (peerID) => {
const call = peer.call(peerID,mixedStream)
remotePeerConnection = call.peerConnection
call.on("stream",remoteStream=>{
const remoteVideo = document.getElementById("remote-video")
remoteVideo .srcObject = remoteStream
remoteVideo .play()
})
}
the question is if I want to start the call with only audio stream so the mixed stream only has an audio tracks added at the beginning, and then if I want to add video stream to the call How could I do that , to be more clear not replacing an existing stream which I already know, Which kinda looks like this example :
// assume videoTrack is come from what ever
const sender = remotePeerConnection.getSenders().find(s=>s.track.kind === "video")
if(sender){
sender.replaceTrack(videoTrack)
}
I tried adding the video track using addTrack method on the remotePeerConnection
try{
videoStream = await navigator.mediaDevices.getDisplayMedia({video:true})
}
catch(err){
console.log(err)
}
if(videoStream){
const videoTrack = videoStream.getVideoTracks()[0]
remotePeerConnection.addTrack(videoTrack)
}
but it does not seem to work, how could I do that without closing the call and re-call with a new mixed stream.
