所以我试图通过socket.io
的信令服务器建立webRTC视频连接。我已经过了setLocalDescription
,这让我可以得到冰候选人,我相信从阅读是正确的方法,但我如何添加冰候选人。我看到我必须使用类似下面的内容:myPeerConnection.addIceCandidate(RTCIceCandidate);
但是我是否将其添加到远程和本地对等连接中?我是否将evt.candidates
发送到我的信令服务器并将其添加到那里?如果是这样,怎么样?添加到对等连接的变量不是全局的。我已经在这几天工作了,我想我现在只需要一个指南,比在线教程更好的东西,我已经看了我能做的一切找到。
这是附加到我的html:client.js
的代码var socket = io.connect();
var myPeerConnection;
var remotePeerConnection;
var offer;
var PeerConnectionWindow = (window.RTCPeerConnection || window.mozRTCPeerConnection
|| window.webkitRTCPeerConnection || window.msRTCPeerConnection);
var SessionDescription = (window.RTCSessionDescription || window.mozRTCSessionDescription
|| window.webkitRTCSessionDescription || window.msRTCSessionDescription);
function hasUserMedia() {
//check if the browser supports the WebRTC
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}
if (hasUserMedia()) {
var configuration = {
"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
};
myPeerConnection = new PeerConnectionWindow(configuration);
console.log(myPeerConnection);
remotePeerConnection = new PeerConnectionWindow(configuration);
console.log(remotePeerConnection);
myPeerConnection.createOffer(gotDescription, onError);
myPeerConnection.onicecandidate = function(evt){
if(event.candidate){
socket.emit('ice', JSON.stringify({'ice': evt.candidate}));
remotePeerConnection.addIceCandidate(evt.candidate);
console.log("connected my to remote");
}
};
remotePeerConnection.onicecandidate = function(evt){
if(event.candidate){
socket.emit('ice', JSON.stringify({'ice': evt.candidate}));
myPeerConnection.addIceCandidate(evt.candidate);
console.log("connected my to remote");
}
};
remotePeerConnection.onaddstream = function (evt) {
console.log("on added stream");
var remoteVid = document.getElementById('remote');
remoteVid.src = window.URL.createObjectURL(evt.stream);
}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia
|| navigator.mozGetUserMedia || navigator.msGetUserMedia;
//enabling video and audio channels
navigator.getUserMedia({ video: true, audio: true }, function (stream) {
var video = document.getElementById('local');
// //inserting our stream to the video tag
video.src = window.URL.createObjectURL(stream);
myPeerConnection.addStream(stream);
}, onError);
} else {
alert("WebRTC is not supported");
}
function gotDescription(desc) {
offer = desc
myPeerConnection.setLocalDescription(offer, myPeerConnLocalDescSet, onError);
socket.emit('description', JSON.stringify({'sdp': desc}));
}
function onError(err){
console.log(err);
}
function myPeerConnLocalDescSet(){
remotePeerConnection.setRemoteDescription(offer, remotePeerConnRemoteDescSet,onError);
}
function remotePeerConnRemoteDescSet(){
remotePeerConnection.createAnswer(onAnswerCreated, onError);
}
function onAnswerCreated(description){
answer = description;
remotePeerConnection.setLocalDescription(answer, remotePeerLocalDescSet,onError);
}
function remotePeerLocalDescSet(){
myPeerConnection.setRemoteDescription(answer, myPeerRemoteDescSet,onError);
}
function myPeerRemoteDescSet(){
console.log('did we get it?');
}
这是我的信令服务器:server.js
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.use(express.static(__dirname + '/'));
server.listen(process.env.PORT || 3000, function(){
console.log('listening on *:3000');
});
io.sockets.on('connection', function(socket){
console.log("in the socket on server");
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('description', function(data){
console.log(data);
});
socket.on('ice', function(data){
// Do I attempt to add the candidate here like so?: myPeerConnection.addIceCandidate(new RTCIceCandidate(data));
});
});
更新
所以看起来所有东西都按顺序流动,所以我更新了我的代码。现在我只需要帮助知道如何在两个对等体之间交换SDP信息。我现在拥有的是他们被送到Socket.io
被双方接收和申请......对吗?有人有一些示例代码,说明如何完成它们可以向我显示解释。拜托,谢谢你的帮助!
答案 0 :(得分:1)
所以我发现如何通过一些基本教程与socket.io进行通信,如果有人感兴趣,但最重要的是我发现webrtc的顺序非常重要。在拥有正确的代码并且呼叫之后,对等仍然没有完成,因为 ONADDSTREAM在您创建提议之前已经在本地远程和ADDSTREAM上被调用
对于那些在这么简单的事情上浪费了几个小时的人来说。
答案 1 :(得分:0)
首先实现总是很棘手,ICE候选接口(添加和打开)确实非常混乱。您可以查看该图表,以了解何时调用哪个事件以及应该使用哪个API。您可以看到,当本地创建候选项(onIceCandidate事件)时,需要通过信令将其传递到远程端,并使用addIceCandidate API添加。由于它是对称的,onIceCandidate总是提供本地候选者,addIceCandidate总是将远程候选者作为参数。
http://www.slideshare.net/alexpiwi5/overviewpeerconnectionlifetime