我正在将 Simple-peer 用于简单的呼叫应用程序,我最初仅使用音频开始呼叫:
function callPeer(id) {
if (id !== "" && id !== currentUser.id) {
navigator.mediaDevices
.getUserMedia({ video: false, audio: true })
.then((stream) => {
setStream(stream);
setCallingFriend(true);
setCallerNew(id);
if (userVideo.current) {
userVideo.current.srcObject = stream;
}
const peer = new Peer({
initiator: true,
trickle: false,
config: {
iceServers: [
{
urls: "stun:numb.viagenie.ca",
username: "sultan1640@gmail.com",
credential: "98376683",
},
{
urls: "turn:numb.viagenie.ca",
username: "sultan1640@gmail.com",
credential: "98376683",
},
],
},
stream: stream,
});
....
....
现在开始音频通话后,我想将视频轨道添加到我尝试过的当前通话中:
const videoCall = () => {
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then((strm) => {
userVideo.current.srcObject = strm;
setStream(strm);
myPeer.current.addTrack(strm.getVideoTracks()[0], stream);
})
.catch((err) => console.log("Cant add video: " + err));
};
在接收方我正在使用:
peer.on("track", (track, stream) => {
console.log("A track has been added.");
});
但由于某种原因,只有在我第一次开始音频通话时才会调用这个在轨道上,但是在将视频轨道添加到对等方时它不会被调用。