【问题标题】:WebRTC black screenWebRTC黑屏
【发布时间】:2021-01-19 04:46:03
【问题描述】:

我有问题。我想学习WebRTC并买了一本书。问题是,给定的代码不起作用。我以为我犯了一个错误,但我尝试了书中给出的代码,并且出现了同样的问题。

我想在两个视频 html 元素之间创建视频通信。我已经替换了一些不推荐使用的功能。 目前,我只看到自己(在“你的”中)和“他们的”中的黑屏。我应该看到我两个元素:

截图:WebRTC black screen

我不知道错误在哪里。也许有人可以帮助我?

谢谢!

ma​​in.js

// Check whether the user has access to the getUserMedia API
function hasUserMedia() {
	
	navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
	
	return !!navigator.getUserMedia;
	
}

// Check whether the user has access to the RTCPeerConnection API
function hasRTCPeerConnection() {
	
	window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.msRTCPeerConnection;
	
	return !!window.RTCPeerConnection;
}

var yourVideo = document.querySelector('#yours'),
	theirVideo = document.querySelector('#theirs'),
	yourConnection, theirConnection;
	
if(hasUserMedia()) {
	navigator.mediaDevices.getUserMedia({video: true, audio: false})
	.then(
		function(mediaStream) {
			
			// Giving the stream to the html object
			yourVideo.srcObject = mediaStream;
			
			if(hasRTCPeerConnection()) {
				startPeerConnection(mediaStream);
			} // End if(hasRTCPeerConnection())
			else {
				alert('Sorry, your browser does not support WebRTC.');
			} // End else if
			
		} // End function(mediaStream)
	) // End getUserMedia().then()
	.catch(
		function(err) {
			alert('Sorry, we failed to capture your camera, please try again.');
			console.log(err.name + ': ' + err.message);
		} // End function(err)
	) // End getUserMedia().catch()
} // End hasUserMedia()
else {
	alert('Sorry, your browser does not support WebRTC.');
} // End Else if(hasUserMedia())
	
function startPeerConnection(mediaStream) {
	var configuration = {
		// Uncomment this code to add custom iceServers
		"iceServers": [{"urls": "stun:stun.1und1.de"}, {"urls": "stun:stun.gmx.net"}, {"urls": "stun:stun1.l.google.com:19305"}, {"urls": "stun:stun2.l.google.com:19305"}]
	};
	
	yourConnection = new mozRTCPeerConnection(configuration);
	theirConncetion = new mozRTCPeerConnection(configuration);
	
	// Setup stream listening
	yourConnection.addStream(mediaStream);
	theirConncetion.ontrack = function (e) {
		theirVideo.srcObject = e.stream;
	};
	
	// Setup ice handling
	yourConnection.onicecandidate = function (event){
		if(event.candidate){
			theirConncetion.addIceCandidate(new RTCIceCandidate(event.candidate));
		}
	};
	
	theirConncetion.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);
				}
			);
		}
	);
}

**index.html**
<!DOCTYPE html>
<html>
	<head>
		<meta char="utf-8" />
		<title>Chapter 3</title>
		<style>
			body {
				background-color: #3D6DF2;
				margin-top: 15px;
			}
			video {
				background: black;
				border: 1px solid gray;
			}
			#container {
				position: relative;
				display: block;
				margin: 0 auto;
				width: 500px;
				height: 500px;
			}
			#yours {
				width: 150px;
				height: 150px;
				position: absolute;
				top: 15px;
				right: 15px;
			}
			#theirs {
				width: 500px;
				height: 500px;
			}
		</style>
	</head>
	<body>
		<video id="yours" autoplay></video>
		<video id="theirs" autoplay></video>
		<script src="main.js"></script>
	</body>
</html>

【问题讨论】:

    标签: screen webrtc


    【解决方案1】:

    旧书中的旧代码。为了消除旧的例子,请允许我提供一些提示:

    • 删除 moz 前缀。
    • 删除window.msRTCPeerConnection。它从未存在过。
    • 您的hasUserMedia() 函数填充了旧的navigator.getUserMedia,但您的主代码使用了较新的navigator.mediaDevices.getUserMedia。只需检查后者是否存在。
    • 使用pc.ontrack = e =&gt; video.srcObject = e.streams[0];,或者如果你想让它在Chrome中工作,使用adapter.js或更旧的pc.onaddtream = e =&gt; video.srcObject = e.stream;
    • 将失败回调添加到您的 createOffercreateAnswer 调用中,或者它们是 won't work

    我看到你对getUserMedia 使用了较新的promise-API,但对RTCPeerConnection 没有。 Try:

    var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();
    
    navigator.mediaDevices.getUserMedia({video: true, audio: true})
      .then(stream => pc1.addStream(video1.srcObject = stream))
      .catch(e => console.log(e));
    
    pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
    pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
    
    pc2.ontrack = e => video2.srcObject = e.streams[0];
    pc1.oniceconnectionstatechange = e => console.log(pc1.iceConnectionState);
    pc1.onnegotiationneeded = e =>
      pc1.createOffer().then(d => pc1.setLocalDescription(d))
      .then(() => pc2.setRemoteDescription(pc1.localDescription))
      .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
      .then(() => pc1.setRemoteDescription(pc2.localDescription))
      .catch(e => console.log(e));
    <video id="video1" width="160" height="120" autoplay muted></video>
    <video id="video2" width="160" height="120" autoplay></video><br>
    <div id="div"></div>
    <script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>

    【讨论】:

      【解决方案2】:

      您可能将已弃用的 onaddstream 替换为 ontrack(目前仅在 Firefox 中支持)。 但是 ontracks 没有 e.stream 属性,请尝试 e.streams[0]

      【讨论】:

        【解决方案3】:

        解决方案很简单。我在这里找到了答案,但我不得不在谷歌上更加努力地搜索。 那里描述了解决方案: onaddstream method is not executed after RTCPeerconnection object is instantiated

        【讨论】:

          猜你喜欢
          • 2021-06-30
          • 1970-01-01
          • 1970-01-01
          • 2020-05-30
          • 1970-01-01
          • 2016-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多