WebRTC RTCIceCandidate答案配置问题

时间:2020-04-15 00:29:26

标签: javascript webrtc

我目前正在研究如何为测试应用程序配置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的教程:https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Signaling_and_video_calling

问题在handleNewICECandidateMsg()函数内(或者我认为)。因为当我输入sdpMLineIndex和sdpMid时,我得到一个空的json字符串,当我仅将其与候选者sdp信息一起留下时,它会引发typeerror并指出需要sdpMid或sdpMLineIndex。

任何想法,链接。任何事情都值得赞赏!

0 个答案:

没有答案