Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WebRTC RTICE候选答案配置问题_Javascript_Webrtc - Fatal编程技术网

Javascript WebRTC RTICE候选答案配置问题

Javascript WebRTC RTICE候选答案配置问题,javascript,webrtc,Javascript,Webrtc,我目前正在研究如何为我的测试应用程序配置webRTC。我能够让webrtc的用户媒体API部分工作,但ICE配置是导致我出现问题的原因。我试着在这里查看stackoverflow,似乎没有其他人从客户端返回过类似的错误。我正在一个实时服务器上测试它,其中初始通信是通过WSS进行的。我省略了标记和websocket配置,因为它与此无关 let myPeerConnection; /* step 1: get users media stream inputs */ fun

我目前正在研究如何为我的测试应用程序配置webRTC。我能够让webrtc的用户媒体API部分工作,但ICE配置是导致我出现问题的原因。我试着在这里查看stackoverflow,似乎没有其他人从客户端返回过类似的错误。我正在一个实时服务器上测试它,其中初始通信是通过WSS进行的。我省略了标记和websocket配置,因为它与此无关

   let myPeerConnection;


    /* step 1: get users media stream inputs */
    function getUserMediaClient(type = ""){

    let mediaConstraints = {video:true, audio:true};
      createPeerConnection();
      navigator.mediaDevices.getUserMedia(mediaConstraints)
      .then(function(localStream) {
        document.getElementById("myVideo").srcObject = localStream;
        document.getElementById("myVideo").onloadedmetadata = function(){
          document.getElementById("myVideo").play();
        }
        localStream.getTracks().forEach(track => myPeerConnection.addTrack(track, localStream));
      })
      .catch(handleGetUserMediaError);
    }
    getUserMediaClient();


    /* 
      preparation for step 2: 
      get users Peer connection information when the user calls createPeerCandidate() 
      to send the offer or handleVideoOfferMsg() to send the answer 
    */

    function createPeerConnection(){

        myPeerConnection = new RTCPeerConnection(
                                            {
                                                iceServers: [


                                                        {
                                                          urls: [
                                                                  'stun:stun.l.google.com:19302',
                                                                  'stun:stun1.l.google.com:19302'
                                                                ]
                                                        }/*,
                                                        {
                                                          urls: 'turn:192.158.29.39:3478?transport=tcp',
                                                          credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
                                                          username: '28224511:1379330808'
                                                        }*/

                                                ]
                                            }
                                        );

        return myPeerConnection;
    }

    /*
      step 2:
      send information to opposite part through websocket with SDP info 
    */

    function handleNegotiationNeededEvent(myUsername = "", targetUsername = "") {
      myPeerConnection.createOffer().then(function(offer) {
        return myPeerConnection.setLocalDescription(offer);
      })
      .then(function() {
        sendToServer({
          name: myUsername,
          target: targetUsername,
          type: "video-offer",
          sdp: myPeerConnection.localDescription
        });
      })
      .catch(reportError);
    }


    /* 
      step 3:
      "if" answering user accepts - load tracks to stream and respond with SDP config info
    */
    function handleVideoOfferMsg(msg) {

    let mediaConstraints = {video:true, audio:true};
      var localStream = null;

      targetUsername = msg.name;
      let myUsername = document.getElementById("user1").value;
      createPeerConnection();

      var desc = new RTCSessionDescription(msg.sdp);

      myPeerConnection.setRemoteDescription(desc).then(function () {
        return navigator.mediaDevices.getUserMedia(mediaConstraints);
      })
      .then(function(stream) {
        localStream = stream;
        document.getElementById("myVideo").srcObject = localStream;

        localStream.getTracks().forEach(track => myPeerConnection.addTrack(track, localStream));
      })
      .then(function() {
        return myPeerConnection.createAnswer();
      })
      .then(function(answer) {
        return myPeerConnection.setLocalDescription(answer);
      })
      .then(function() {
        var msg = {
          name: myUsername,
          target: targetUsername,
          type: "video-answer",
          sdp: myPeerConnection.localDescription
        };

        sendToServer(msg);
      })
      .catch(handleGetUserMediaError);
    }



    /*
      step 4: 
      when both users have exchanged information - the ice processing can begin 
      the user that initially sent the request can now reply with a communication method
    */
    var candidateData = null;
    function handleICECandidateEvent(event) {
      if ((event.sdp)||(event.candidate)){

        if (event.candidate){
          candidateData = event.candidate;
        } else if (event.sdp) {
          candidateData = event.sdp;
        }

        sendToServer({
          type: "new-ice-candidate",
          target: event.target,
          candidate: candidateData
        });
      }
    }

    ///////////////////// non functional part under ////////////////////////////



    function handleNewICECandidateMsg(msg) {
      candidateData = msg.candidate;

      myPeerConnection.addIceCandidate(new RTCIceCandidate({sdpMLineIndex:1,candidate: candidateData})).catch(e => {
        console.log("Failure during addIceCandidate(): " + JSON.stringify(e));
      });



      ////////////// non functional part above ///////////////



  console.log("MSG: " + JSON.stringify(msg));

}
function handleGetUserMediaError(e){
 //removed for simplicity
}
//wss connection estabilshment from client removed
wss.onmessage = function(e){ 

    if (type == "video-offer" && document.getElementById("user1").value == target){
      // create counteroffer
      handleVideoOfferMsg(data);
    } else if (type == "video-answer"){
      handleICECandidateEvent(data);
    } else if (type == "new-ice-candidate"){
      handleNewICECandidateMsg(data);
    }
    }
大部分代码来自MDN的教程:

问题出在handleNewICECandidateMsg()函数中(我认为是这样)。因为当我输入sdpMLineIndex和sdpMid时,我得到一个空的json字符串,当我只留下候选sdp信息时,它抛出一个类型错误,表示它需要sdpMid或sdpMLineIndex


任何想法,链接。什么都好

你在屠杀候选人。只需执行myPeerConnection.addIceCandidate(msg.candidate)。而且,您从未说过返回了什么错误。最后,看起来您缺少
setRemoteDescription(answer)
来完成协商交换。您正在屠杀候选人。只需执行myPeerConnection.addIceCandidate(msg.candidate)。而且,您从未说过返回了什么错误。最后,您似乎缺少
setRemoteDescription(answer)
来完成协商交换。