我正在为peerconnection实现webrtc但是没有工作。当我调试这段代码然后我看到它没有进入
theirConnection.onaddstream = function (e) {
theirVideo.src = window.URL.createObjectURL(e.stream);
};
块。请让我知道我做错了什么?我在本地系统上使用此功能,仅使用一个网络摄像头。
function startPeerConnection(stream) {
var configuration = {
"iceServers": [{ "url": "stun:127.0.0.1:8080" }]
};
yourConnection = new mozRTCPeerConnection(configuration);
theirConnection = new mozRTCPeerConnection(configuration);
// Setup stream listening
theirConnection.onaddstream = function (e) {
theirVideo.src = window.URL.createObjectURL(e.stream);
};
// Setup ice handling
yourConnection.onicecandidate = function (event) {
if (event.candidate) {
theirConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
}
};
theirConnection.onicecandidate = function (event) {
if (event.candidate) {
yourConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
}
};
// Begin the offer
yourConnection.createOffer(function (offer) {
yourConnection.setLocalDescription(offer);
theirConnection.setRemoteDescription(offer);
theirConnection.createAnswer(function (offer) {
theirConnection.setLocalDescription(offer);
yourConnection.setRemoteDescription(offer);
});
});
}
答案 0 :(得分:0)
你正在调用promise方法。问题实际上是你没有检查错误。
具体来说:您寻求的legacy callback methods至少需要两个参数:成功回调,和失败回调。
如果没有失败的回调,WebIDL名称重载会调用newer promise methods,而不会回调并返回一个承诺。
在任何地方传递所需的错误回调,例如:
yourConnection.createOffer(offer => { ... }, err => console.log(err));
或者更好,完全避免遗留方法:
function startPeerConnection(stream) {
var config = { iceServers: [{ urls: "stun:127.0.0.1:8080" }] };
yours = new RTCPeerConnection(config);
theirs = new RTCPeerConnection(config);
theirs.onaddstream = e => theirVideo.srcObject = e.stream;
yours.onicecandidate = e => theirs.addIceCandidate(e.candidate);
theirs.onicecandidate = e => yours.addIceCandidate(e.candidate);
yours.addStream(stream); // <-- don't forget
return yours.createOffer()
.then(offer => Promise.all([yours.setLocalDescription(offer),
theirs.setRemoteDescription(offer)]))
.then(() => theirs.createAnswer())
.then(answer => Promise.all([theirs.setLocalDescription(answer),
yours.setRemoteDescription(answer)]));
}
navigator.mediaDevices.getUserMedia({video: true, audio: true})
.then(stream => startPeerConnection(stream))
.catch(e => console.log(e));
<video id="theirVideo" height="120" width="160" autoplay></video>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
始终检查错误。 createOffer
错误告诉我您忘记了.addStream(stream)
。
promise API可以在Firefox中本地使用,也可以通过Chrome中的adapter.js获得。