我感觉自己被 WebRTC 困住了。我只是尝试虚拟的点对点连接,但远程连接没有收到任何内容。问题是 ontrack 函数没有被触发,我不知道为什么?我怎样才能让它发挥作用?
它不适用于 Chromium 和 Firefox
var localVideo = document.querySelector('#local'),
remoteVideo = document.querySelector('#remote'),
localConnection,remoteConnection;
if (hasUserMedia()){
navigator.getUserMedia({video: true, audio:false},function(stream){
localVideo.srcObject = stream;
if (hasRTCPeerConnection()){
startPeerConnection(stream);
} else {
alert("WebRTC not supported!");
}
},function(error){
alert("Camera capture failed!")
});
} else {
alert("WebRTC not supported!");
}
function startPeerConnection(stream){
var configuration ={
offerToReceiveAudio: true,
offerToReceiveVideo: true
}
localConnection = new RTCPeerConnection(configuration);
remoteConnection = new RTCPeerConnection(configuration);
stream.getTracks().forEach(
function(track) {
localConnection.addTrack(
track,
stream
);
}
);
remoteConnection.ontrack = function(e){
remoteVideo.srcObject = e.streams[0];
};
localConnection.onicecandidate = function(event){
if (event.candidate){
remoteConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
}
};
remoteConnection.onicecandidate = function(event){
if (event.candidate){
localConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
}
};
localConnection.createOffer(function(offer){
localConnection.setLocalDescription(offer);
remoteConnection.setRemoteDescription(offer);
remoteConnection.createAnswer(function(offer){
remoteConnection.setLocalDescription(offer);
localConnection.setRemoteDescription(offer);
});
});
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我已经对您的代码做了一些更正,现在它可以工作了。此外,我还用承诺更改了回调。
var localVideo = document.querySelector('#local'),
remoteVideo = document.querySelector('#remote'),
localConnection, remoteConnection;
navigator.getUserMedia({video: true, audio: false}, function (stream) {
localVideo.srcObject = stream;
startPeerConnection(stream);
}, function (error) {
alert("Camera capture failed!")
});
function startPeerConnection(stream) {
var configuration = {
offerToReceiveAudio: true,
offerToReceiveVideo: true
}
localConnection = new RTCPeerConnection({configuration: configuration, iceServers: []});
remoteConnection = new RTCPeerConnection(configuration);
stream.getTracks().forEach(
function (track) {
localConnection.addTrack(
track,
stream
);
}
);
remoteConnection.ontrack = function (e) {
remoteVideo.srcObject = e.streams[0];
};
// Set up the ICE candidates for the two peers
localConnection.onicecandidate = e => !e.candidate
|| remoteConnection.addIceCandidate(e.candidate)
.catch(e => {
console.error(e)
});
remoteConnection.onicecandidate = e => !e.candidate
|| localConnection.addIceCandidate(e.candidate)
.catch(e => {
console.error(e)
});
localConnection.createOffer()
.then(offer => localConnection.setLocalDescription(offer))
.then(() => remoteConnection.setRemoteDescription(localConnection.localDescription))
.then(() => remoteConnection.createAnswer())
.then(answer => remoteConnection.setLocalDescription(answer))
.then(() => localConnection.setRemoteDescription(remoteConnection.localDescription))
.catch(e => {
console.error(e)
});
}
Run Code Online (Sandbox Code Playgroud)