我在这里关注这个例子:https://www.w3.org/TR/webrtc/#simple-peer-to-peer-example
我修改了代码,因为我只需要单向流式传输:
var configuration = null; //{ "iceServers": [{ "urls": "stuns:stun.example.org" }] };
var peerConnection;
var outboundPeerStream = null;
var outboundPeerStreamSessionId = null;
var createPeerConnection = function () {
if (peerConnection)
return;
peerConnection = new RTCPeerConnection(configuration);
// send any ice candidates to the other peer
peerConnection.onicecandidate = function (event) {
signalrModule.sendClientNotification(JSON.stringify({ "candidate": event.candidate }));
};
// let the "negotiationneeded" event trigger offer generation
peerConnection.onnegotiationneeded = peerStreamingModule.sendOfferToPeers;
// once remote track arrives, show it in the remote video element
peerConnection.ontrack = function (event) {
var inboundPeerStream = event.streams[0];
remoteStreamHelper.pushStreamToDom(inboundPeerStream, foo);
}
}
// this gets called either on negotiationNeeded and every 30s to ensure all peers have the offer from the stream originator
peerStreamingModule.sendOfferToPeers = function () {
peerConnection.createOffer().then(function (offer) {
return peerConnection.setLocalDescription(offer);
}).then(function () {
// send the offer to the other peer
signalrModule.sendClientNotification(JSON.stringify({ "desc": peerConnection.localDescription}));
}).catch(logger.internalLog);
};
// this gets called by the stream originator when the stream is available to initiate streaming to peers
peerStreamingModule.initializeWithStream = function (outboundStream, sessionId) {
outboundPeerStream = outboundStream;
outboundPeerStreamSessionId = sessionId;
createPeerConnection();
peerConnection.addStream(outboundStream);
//peerStreamingModule.sendOfferToPeers(); I don't think I need this...
}
peerStreamingModule.handleP2PEvent = function (notification) {
if (!peerConnection)
createPeerConnection();
if (notification.desc) {
var desc = notification.desc;
// if we get an offer, we need to reply with an answer
if (desc.type == "offer") {
peerConnection.setRemoteDescription(desc).then(function () {
return peerConnection.createAnswer();
}).then(function (answer) {
return peerConnection.setLocalDescription(answer);
}).then(function () {
signalrModule.sendClientNotification(JSON.stringify({ "desc": peerConnection.localDescription, "sessionId": sessionManager.thisSession().deviceSessionId() }), app.username());
}).catch(logger.internalLog);
} else if (desc.type == "answer") {
peerConnection.setRemoteDescription(desc).catch(logger.internalLog);
} else {
logger.internalLog("Unsupported SDP type. Your code may differ here.");
}
} else
pc.addIceCandidate(notification.candidate).catch(logger.internalLog);
}
这似乎有效,但我有两个部分困扰:
1)WebRTC - Failed to set remote answer sdp: Called in wrong state: STATE_INPROGRESS
- 这会不时出现在我的日志中 - 我是否在上面做错了导致这种情况?
2)我是否正确实施了sendOfferToPeers
和initializeWithStream
?我担心来自发起人的间隔触发的sendOfferToPeers
不是规范的使用方式;我的目标是确保所有同行最终收到报价,无论他们何时加入,或者他们是否面临连接问题而导致原始报价/谈判失败。
答案 0 :(得分:5)
//这可以在negotiationNeeded和每30秒调用一次,以确保所有对等方都有提供
您无法向多个同行发送相同的优惠。它是点对点,而不是点对点。一对多要求每个参与者至少需要一个连接,并且可能是要扩展的媒体服务器。
此外,SDP不适用于discovery。提议/应答交换仅在两个端点之间进行脆弱的state-machine协商,以建立单个连接。
您应该在建立WebRTC连接之前解决谁与谁建立联系。