我正在研究WebRTC,但我觉得我不了解全貌.我特别关注这个演示项目:https://github.com/oney/RCTWebRTCDemo/blob/master/main.js
我无法理解如何匹配2个客户端,以便客户端A可以看到客户端B的视频流,反之亦然.
这是在演示中:
function getLocalStream(isFront, callback) {
MediaStreamTrack.getSources(sourceInfos => {
console.log(sourceInfos);
let videoSourceId;
for (const i = 0; i < sourceInfos.length; i++) {
const sourceInfo = sourceInfos[i];
if(sourceInfo.kind == "video" && sourceInfo.facing == (isFront ? "front" : "back")) {
videoSourceId = sourceInfo.id;
}
}
getUserMedia({
audio: true,
video: {
mandatory: {
minWidth: 500, // Provide your own width, height and frame rate here
minHeight: 300,
minFrameRate: 30
},
facingMode: (isFront ? "user" : "environment"),
optional: [{ sourceId: sourceInfos.id }]
}
}, function (stream) {
console.log('dddd', stream);
callback(stream);
}, logError);
});
}
Run Code Online (Sandbox Code Playgroud)
然后就像这样使用:
socket.on('connect', function(data) {
console.log('connect');
getLocalStream(true, function(stream) {
localStream = stream;
container.setState({selfViewSrc: stream.toURL()});
container.setState({status: 'ready', info: 'Please enter or create room ID'});
});
});
Run Code Online (Sandbox Code Playgroud)
问题:
究竟在MediaStreamTrack.getSources做什么?这是因为设备可以有多个视频源(例如3个网络摄像头)?
不getUserMedia只是打开客户端的相机?在上面的代码中,客户只是查看自己的视频?
我想知道如何将客户端A的某种URL传递给客户端B,以便客户端B流式传输来自客户端A的视频.我该怎么做?我想象的是这样的:
我如何使用WebRTC库(您可以假设创建了用于房间匹配的后端服务器)
您正在寻找的过程称为JSEP(JavaScript 会话建立协议),它可以分为我在下面描述的 3 个步骤。一旦两个客户端都在房间里并且可以通过 WebSockets 通信,这些步骤就开始了,我将使用ws一个虚构的 WebSocket API 来进行客户端和服务器以及其他客户端之间的通信:
1. 邀请
在此步骤中,一个指定的调用者创建并提供并通过服务器将其发送到另一个客户端(被调用者):
// This is only in Chrome
var pc = new webkitRTCPeerConnection({iceServers:[{url:"stun:stun.l.google.com:19302"}]}, {optional: [{RtpDataChannels: true}]});
// Someone must be chosen to be the caller
// (it can be either latest person who joins the room or the people in it)
ws.on('joined', function() {
var offer = pc.createOffer(function (offer) {
pc.setLocalDescription(offer);
ws.send('offer', offer);
});
});
// The callee receives offer and returns an answer
ws.on('offer', function (offer) {
pc.setRemoteDescription(new RTCSessionDescription(offer));
pc.createAnswer(function(answer) {
pc.setLocalDescription(answer);
ws.send('answer', answer);
}, err => console.log('error'), {});
});
// The caller receives the answer
ws.on('answer', function (answer) {
pc.setRemoteDescription(new RTCSessionDescription(answer));
});
Run Code Online (Sandbox Code Playgroud)
现在双方已经交换了 SDP 数据包并准备相互连接。
2. 谈判(ICE)
ICE 候选由每一方创建以找到相互连接的方法,它们几乎是可以找到的 IP 地址:本地主机、局域网地址 (192.168.xx) 和外部公共 IP 地址 (ISP)。它们由 PC 对象自动生成。
// Both processing them on each end:
ws.on('ICE', candidate => pc.addIceCandidate(new RTCIceCandidate(data)));
// Both sending them:
pc.onicecandidate = candidate => ws.send('ICE', candidate);
Run Code Online (Sandbox Code Playgroud)
ICE 协商后,除非您尝试通过连接两端的防火墙连接客户端,否则连接会建立,p2p 通信是 NAT 穿越,但在某些情况下不起作用。
3. 数据流
// Once the connection is established we can start to transfer video,
// audio or data
navigator.getUserMedia(function (stream) {
pc.addStream(stream);
}, err => console.log('Error getting User Media'));
Run Code Online (Sandbox Code Playgroud)
在进行呼叫之前拥有流并在更早的步骤中添加它是一个不错的选择,在为调用者创建要约之前以及在接收到被调用者的调用之后,因此您不必处理重新协商。几年前这很痛苦,但现在在 WebRTC 中可能会更好地实现。
随意检查我在 GitHub 中的 WebRTC 项目,我在其中为许多参与者在房间中创建了 p2p 连接,它在GitHub 中并有一个现场演示。
| 归档时间: |
|
| 查看次数: |
1019 次 |
| 最近记录: |