2016-11-14 51 views
1

問題があります。私はWebRTCを学び、本を買ったかったのです。問題は、指定されたコードが動作しないことです。私は間違いをしたと思ったが、その本のコードを試したところ、同じ問題がある。WebRTC黒画面

2つのビデオhtml要素間にビデオ通信を作成したいと思います。私は既に廃止予定の機能を置き換えました。 現在のところ、自分自身(「あなた」のもの)と「自分のもの」の黒い画面が表示されています。私は私の両方の要素を私に表示されるはずです。

スクリーンショット:エラーがどこにあるか私にはわからないWebRTC black screen

。おそらく誰かが私を助けることができましたか?

ありがとうございました!

main.js

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

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

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

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

答えて

0

あなたはおそらく(これまでのところ唯一のFirefoxでサポートされています)オントラックで非推奨onaddstreamを置き換えます。 ontracksにはe.streamプロパティはありません。e.streams [0]

1

古い本の古いコードです。古い例を抜き出すために、私はいくつかのヒントを提供することができます:

  • 接頭辞はmozです。
  • 落下window.msRTCPeerConnection。それは決して存在しませんでした。
  • あなたのhasUserMedia()関数は古いnavigator.getUserMediaをポリフィリングしますが、メインコードは新しいnavigator.mediaDevices.getUserMediaを使用します。後者が存在するかどうかだけを確認してください。
  • 使用pc.ontrack = e => video.srcObject = e.streams[0];、またはあなたは、これがChromeで仕事をしたい場合は、adapter.js歳以上pc.onaddtream = e => video.srcObject = e.stream;
  • を使用し、あなたのcreateOffercreateAnswer呼び出しに失敗-コールバックを追加、または彼らwon't work

getUserMediaの新しいプロミス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>