2016-09-07 4 views
1

私はピア接続のためにwebrtcを実装していますが、動作しません。私はこのコードをデバッグするとき、私はそれが来ていないことを見た。webrtc configration return null値

theirConnection.onaddstream = function (e) { 
     theirVideo.src = window.URL.createObjectURL(e.stream); 
    }; 

ブロック。私が間違って行ったことを教えてください。私は自分のローカルシステムと1つのWebカメラでのみこれを使用しています。

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

あなたは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>

を、常にエラーをチェック。 A createOfferエラーは.addStream(stream)を忘れてしまったと言いました。

promise APIは、Firefoxではネイティブで、Chromeではadapter.jsから利用できます。