在WEBRTC(PEERJS)中的通话中添加新的媒体流
我正在使用PEERJS库进行WEBRTC视频呼叫应用程序,并且每个对等都可以选择与呼叫开始的流有关,因此我有两个流音频和视频流:
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;
}
然后我将它们混合在一起
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()
,并且我有一个呼叫功能来呼叫其他对等式的呼叫功能混合流只能是视频流或仅音频流或两者兼而有之,
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()
})
}
问题是我是否要使用音频流启动调用要添加视频流到呼叫中,我该怎么做,更清楚地不要替换我已经知道的现有流,哪个看起来像这个示例:
// assume videoTrack is come from what ever
const sender = remotePeerConnection.getSenders().find(s=>s.track.kind === "video")
if(sender){
sender.replaceTrack(videoTrack)
}
我尝试使用AddTrack方法添加视频轨道在RemotepeerConnection上,
try{
videoStream = await navigator.mediaDevices.getDisplayMedia({video:true})
}
catch(err){
console.log(err)
}
if(videoStream){
const videoTrack = videoStream.getVideoTracks()[0]
remotePeerConnection.addTrack(videoTrack)
}
但它似乎不起作用,如果不关闭新混合流,我该怎么做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)