WebRTC:一对一音频呼叫无法在不同的机器

时间:2015-05-12 09:03:24

标签: node.js webrtc

我正在尝试使用webRTC实现一对一的音频呼叫(使用websockets进行信令)。但是当我在一个系统中使用多个chrome选项卡(localhost)进行尝试时,它可以正常工作。当我尝试从另一台机器上打开我的服务器时,它会进行初始握手,但是呼叫并没有发生。

但是当我尝试将标记更改为并将约束更改为视频约束时。即使我们尝试从其他机器访问(即视频通话工作),它仍然有效。

我最初认为这是因为如果防火墙但是当视频通话工作时我感到困惑。

这是我的代码:

// Constraints to get audio stream only
$scope.constraints = {
    audio: {
            mandatory: {
                googEchoCancellation: true
            },
            optional: []
        },
    video:false
};

navigator.getUserMedia = navigator.getUserMedia ||
    navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

// success Callback of getUserMedia(), stream variable is the audio stream.
$scope.successCallback = function (stream) {
    if (window.URL) {
        myVideo.src = window.URL.createObjectURL(stream); // converting media stream to Blob URL.
    } else {
        myVideo.src = stream;
    }
    //attachMediaStream(audioTag, stream);
    localStream = stream;
    if (initiator)
        maybeStart();
    else
        doAnswer();
};

// failure Callback of getUserMedia()
$scope.failureCallback = function (error) {
    console.log('navigator.getUserMedia Failed: ', error);
};
var initiator, started = false;

 $("#call").click(function () {
    socket.emit("message", undefined);
    initiator = true;
    navigator.getUserMedia($scope.constraints, $scope.successCallback, $scope.failureCallback);
});

var channelReady = false;
socket.on('message', function (data) {
    channelReady = true;
    if (data) {
        if (data.type === 'offer') {
            if (!initiator) {
                $("#acceptCall").show();
                  $("#acceptCall").click(function(){
                      if (!initiator && !started) {
                    var pc_config = {
                        iceServers: [
                     { url: "stun:stun.l.google.com:19302" },
                      { url: "turn:numb.viagenie.ca", credential: "drfunk", username: "toadums@hotmail.com"}
                     ]
                    };
                    pc = new webkitRTCPeerConnection(pc_config);
                    pc.onicecandidate = onIceCandidate;
                    pc.onaddstream = onRemoteStreamAdded;
                }
                pc.setRemoteDescription(new RTCSessionDescription(data));
                      $scope.acceptCall();
                  });

            }
        } else if (data.type === 'answer' && started) {
            pc.onaddstream = onRemoteStreamAdded;
            pc.setRemoteDescription(new RTCSessionDescription(data));

        } else if (data.type === 'candidate' && started) {
            var candidate = new RTCIceCandidate({
                sdpMLineIndex: data.label,
                candidate: data.candidate
            });
            pc.addIceCandidate(candidate);
        } else if (data.type === 'bye' && started) {
            console.log("Bye");
        }
    }
});

 function onRemoteStreamAdded(event) {
    othersVideo.src = URL.createObjectURL(event.stream); 
 };
  var sdpConstraints = {
   'mandatory': {
     'OfferToReceiveAudio': true,
     'OfferToReceiveVideo': false
   }
 };
function doAnswer() {
    pc.addStream(localStream);
    pc.createAnswer(gotDescription,null,sdpConstraints);
}
function gotDescription(desc) {
        pc.setLocalDescription(desc);
        socket.send(desc);
    }

function maybeStart() {
    if (!started && localStream && channelReady)
        createPeerConnection();
        pc.addStream(localStream);
        started = true;
        if (initiator)
            doCall();
    }

$scope.acceptCall = function () {
    navigator.getUserMedia($scope.constraints, $scope.successCallback, $scope.failureCallback);
}

function createPeerConnection() {
    var pc_config = {
         iceServers: [
         { url: "stun:stun.l.google.com:19302" },
         { url: "turn:numb.viagenie.ca", credential: "drfunk", username: "toadums@hotmail.com"}
         ]
    };

    pc = new webkitRTCPeerConnection(pc_config);
    pc.onicecandidate = onIceCandidate;
    console.log("Created RTCPeerConnnection with config:\n" + "  \"" +
        JSON.stringify(pc_config) + "\".");
};


function doCall() {
    $scope.caller = true;
    pc.createOffer(setLocalAndSendMessage,null,sdpConstraints);
};

function setLocalAndSendMessage(sessionDescription) {
    pc.setLocalDescription(sessionDescription);
    socket.send(sessionDescription);
}

function onIceCandidate(event) {
    if (event.candidate) {
        socket.emit('message', {
            type: 'candidate',
            label: event.candidate.sdpMLineIndex,
            id: event.candidate.sdpMid,
            candidate: event.candidate.candidate
        });
    } else {
        console.log("End of candidates.");
    }
}

1 个答案:

答案 0 :(得分:0)

如果未定义navigator.mediaDevices,则因为仅在安全上下文(https)中起作用

请参阅:

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia