gpt4 book ai didi

javascript - Webrtc 如何在收到回复后设置视频 Remote

转载 作者:行者123 更新时间:2023-11-30 14:07:52 24 4
gpt4 key购买 nike

大家好,我是新手,我几乎完成了所有工作,但只是对我的发起人说了一些我不知道在收到答案后接收远程视频流的方式,有人可以帮助我吗?

这是我的客户

$(document).ready(() => {

const configuration = {
iceServers: [{ url: 'stun:stun2.1.google.com:19302' }]
}

var peerConection = null;
// var btnCall = $('body #call');
var list = $('#mylist');
var TitlePrint = $('#titleUser');
var localVideo = document.getElementById('local');
var remoteVideo = document.getElementById('remote');
var userid = null;

var socket = io();


socket.on('connect', () => {
userid = socket.id
TitlePrint.text(userid);
});



socket.on('users', data => {
var users = [];
list.empty();
for (let index = 0; index < data.user.length; index++) {
if (data.user[index] != userid) {
users.push(`<button id="call" class="list-group-item list-group-item-action" data-ids="${data.user[index]}">${data.user[index]}</button>`);
}
}
if (users.length != 0) {

list.html(users);
} else {
list.html(`<div class="list-group-item"> Any users connected! </div>`);
}
});


$('body').on('click', '#call', function () {
let toId = $(this).attr('data-ids');
socket.emit('initiator', { initiatorid: userid, receiverid: toId });
});


socket.on('initiator', data => {

peerConection = createRTC(socket);

if (data.initiatorid === userid) {
console.log('this is the initiator');
initiateSignaling(socket, peerConection, data.receiverid, data.initiatorid);
} else {
console.log('this is the receiver');
prepareToReceiveOffer(socket, peerConection, data.initiatorid, data.receiverid);
}
});


// =============== HELPERS =====================//

function createRTC(socket) {
console.log('createRTC')
var peerConection = new RTCPeerConnection(configuration);
peerConection.onicecandidate = (e) => {
if (e.candidate) {
console.log('emit candidate')
socket.emit('send-candidate', e.candidate);
}
}

socket.on('receiver-candidate', (candidate) => {
peerConection.addIceCandidate(candidate);
});

return peerConection;
}

function initiateSignaling(socket, peerConection, targetID, from) {
navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then((stream) => {
stream.getTracks().forEach(function (track) {
peerConection.addTrack(track, stream);
});
localVideo.srcObject = stream;
peerConection.createOffer().then(function (offer) {
return peerConection.setLocalDescription(offer);
})
.then(function () {
socket.emit('send-offer', {
from: from,
target: targetID,
type: "send-offer",
sdp: peerConection.localDescription
});
})
.catch(function (reason) {
console.log('error on create offer', reason);
});

})

socket.on('receiver-answer', (answer) => {
console.log(answer);
peerConection.setRemoteDescription(answer.sdp);
peerConection.ontrack = function (event) {
remoteVideo.srcObject = event.streams[0];
};
});


}



function prepareToReceiveOffer(socket, peerConection, targetID, from) {
socket.on('receiver-offer', (offer) => {
console.log(offer);
peerConection.setRemoteDescription(offer.sdp);
peerConection.createAnswer().then(function (answer) {
return peerConection.setLocalDescription(answer);
})
.then(function () {
socket.emit('send-answer', {
from: from,
target: targetID,
type: "send-answer",
sdp: peerConection.localDescription
});
});

peerConection.ontrack = function (event) {
remoteVideo.srcObject = event.streams[0];
};
navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then((stream) => {
localVideo.srcObject = stream;
})
});
}

});

我只是使用 socket.io 我正在处理报价并回答然后在我的套接字服务器上我只是像这样设置

socket.on('initiator', (init) => {
console.log(init);
io.to('video').emit('initiator', init);
});

socket.on('send-offer', offer => {
console.log('sending offer', offer);
socket.broadcast.emit('receiver-offer', offer);
});

socket.on('send-answer', answer => {
console.log('sending answer', answer);
socket.broadcast.emit('receiver-answer', answer);
});

socket.on('send-candidate', candidate => {
console.log(candidate);
socket.broadcast.emit('receiver-candidate',candidate);
});

我在我的接收器上从发起者那里获得了远程视频,但在发起者中却没有,我不知道我错过了什么来获得远程视频,谢谢大家

最佳答案

发起者调用 addTrack(),但接收者不调用,所以这只是以一种方式发送媒体。

prepareToReceiveOffer 中,您调用 getUserMedia() 但永远不会将生成的轨道添加到对等连接。如果您想要双向调用,它需要调用 addTrack() 作为提议/答案协商的一部分。

一定要在 setRemoteDescription 之后调用 getUserMedia()not miss ICE candidates :

function prepareToReceiveOffer(socket, peerConection, targetID, from) {
socket.on('receiver-offer', (offer) => {
console.log(offer);
peerConection.setRemoteDescription(offer.sdp)
.then(() => navigator.mediaDevices.getUserMedia({video: true, audio: false}))
.then(stream => {
localVideo.srcObject = stream;
for (const track of stream.getTracks()) {
peerConection.addTrack(track, stream);
}
return peerConection.createAnswer();
})
.then(function (answer) {
return peerConection.setLocalDescription(answer);
})
.then(function () {
socket.emit('send-answer', {
from: from,
target: targetID,
type: "send-answer",
sdp: peerConection.localDescription
});
})
.catch(err => console.log(err.message));

peerConection.ontrack = function (event) {
remoteVideo.srcObject = event.streams[0];
};
});
}

关于javascript - Webrtc 如何在收到回复后设置视频 Remote ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55069345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com