我正在创建一对一的 webrtc 视频聊天室,但此代码不起作用,我想知道为什么
function hasUserMedia(){
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
return !!navigator.getUserMedia;
}
function hasRTCPeerConnection() {
window.RTCPeerConnection = window.RTCPeerConnection ||
window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
return !!window.RTCPeerConnection;
}
function startPeerConnection(stream) {
var configuration = {
"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
};
yourConnection = new RTCPeerConnection(configuration);
theirConnection = new webkitRTCPeerConnection(configuration);
yourConnection.addStream(stream);
theirConnection.onaddstream = function (e) {
theirVideo.src = window.URL.createObjectURL(e.stream);
};
yourConnection.onicecandidate = function (event) {
if (event.candidate){
theirConnection.addIceCandidate(newRTCIceCandidate(event.candidate));
}
};
theirConnection.onicecandidate = function (event) {
if (event.candidate) {
yourConnection.addIceCandidate(new
RTCIceCandidate(event.candidate));
}
};
yourConnection.createOffer(function (offer) {
yourConnection.setLocalDescription(offer);
theirConnection.setRemoteDescription(offer);
theirConnection.createAnswer(function (offer) {
theirConnection.setLocalDescription(offer);
yourConnection.setRemoteDescription(offer);
});
});
}
var yourVideo = document.querySelector("#face_cam_vid"),
theirVideo = document.querySelector("#thevid"),
yourConnection, theirConnection;
if (hasUserMedia()) {
navigator.getUserMedia({ video: true, audio: true }, function(stream)
{
yourVideo.src = window.URL.createObjectURL(stream);
if (hasRTCPeerConnection()) {
startPeerConnection(stream);
} else {
alert("Sorry, your browser does not support WebRTC.");
}
}, function (error) {
console.log(error);
}
);
} else {
alert("Sorry, your browser does not support WebRTC.");
}
并且这段代码给了我一个类似这样的错误,当你看到视频没有显示时,我试图创建 div(视频标签所在的位置)但它无论如何都不起作用
如果你能帮助我,我会很高兴这是我的 html
<!DOCTYPE html>
<html>
<head>
<title>
Video Call
</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="vidd.css" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="/videof.js"></script>
<script>var width = Math.max(window.screen.width, window.innerWidth);
if(width <= 414){
var faceCam = document.getElementById("face_cam");
faceCam.style.width = "15%";
}
function smaller(){
if(width <= 414){
var size = document.getElementById("face_cam").style.width;
if(size == "15%"){
faceCam.style.width = "3%";
faceCam.style.height = "3%";
faceCam.style.borderRadius = "0px"
}
else if(size == "3%"){
faceCam.style.width = "15%";
faceCam.style.height = "30%";
faceCam.style.borderRadius = "10px"
}
}
else{
var size = document.getElementById("face_cam").style.width;
if(size == "30%"){
faceCam.style.width = "3%";
faceCam.style.height = "3%";
faceCam.style.borderRadius = "0px"
}
else if(size == "3%"){
faceCam.style.width = "30%";
faceCam.style.height = "30%";
faceCam.style.borderRadius = "10px";
}
}
}
var width = Math.max(window.screen.width, window.innerWidth);
function smaller(){
var size = document.getElementById("face_cam").style.height;
if (size == "30%"){
var frame = document.getElementById("face_cam");
frame.style.height = "3%";
frame.style.width = "4%";
frame.borderRadius = "0px";
}
else{
var frame = document.getElementById("face_cam");
frame.style.height = "30%";
frame.style.width = "30%";
}
}
function BACKT(){
window.location.href = "http://localhost:8000/"
}
</script>
</head>
<body>
<div class="test_vc_field">
<video id="thevid" autoplay></video>
<div id="face_cam" onclick="smaller()" style="height: 30%; width: 30%">
<video id="face_cam_vid" autoplay></video>
</div>
</div>
<div class="nav">
<button class="next">შემდეგი</button>
<img src="next.png" class="next_icon">
<button class="off" id="off">გათიშვა</button>
<img src="shutdown.png" class="shd_icon">
<button class="goto_main" id="WTfu" onclick="BACKT();">მთავარი
გვერდი</button>
<img src="home.png" class="home_icon" onclick="main()">
</div>
</body>
</html>
#thevid id vid 其中第二个用户显示 face_cam_vid 是我显示的视频