WebRTC适用于Chrome,但不适用于Firefox

ara*_*ao6 7 javascript html5 webrtc

我在相关问题上阅读了其他几个问题,但没有人回答我的问题.我有一个奇怪的问题,我可以使用WebRTC从chrome到firefox的音频聊天,但不能使用firefox到chrome.

基本上,当用户希望音频聊天时,他/她点击一个按钮#audioChatBtn,它采用getUserMedia()设置一个流.问题是,#audioChatBtn从Firefox 点击不会触发onaddstreamChrome上的回调,但点击Chrome onaddstream上的按钮会触发Firefox.因此,我可以从Chrome到Firefox进行音频聊天,但不是相反.我一直试图弄清楚这几个小时,但我希望也许有人在这里有答案.

相关来源:

var configuration = {
    'iceServers': [
        { url: 'stun:stun.l.google.com:19302' },
        { url: 'stun:stun1.l.google.com:19302' },
        { url: 'stun:stun2.l.google.com:19302' },
        { url: 'stun:stun3.l.google.com:19302' },
        { url: 'stun:stun4.l.google.com:19302' }
    ]
};
var pc = RTCPeerConnection(configuration);
var myStream = null;
var currentAudioIndex = 0; // Number of created channels
var myAudioEnabled = false;

// send any ice candidates to the other peer
pc.onicecandidate = function (evt) {
    if (evt.candidate)
        $(document).trigger("persistState", { mode: 'rtc', 'candidate': evt.candidate });
};

// let the 'negotiationneeded' event trigger offer generation
pc.onnegotiationneeded = function () {
    pc.createOffer(localDescCreated, logError);
}

// once remote stream arrives, play it in the audio element
pc.onaddstream = function (evt) {
    console.log('creating and binding audio');

    var idx = (currentAudioIndex++);
    var audioElement = $('#audio' + idx);

    if (audioElement.length == 0) {
        var audio = $('<audio id="audio' + idx + '" autoplay>');
        $('body').append(audio);
        audioElement = $('#audio' + idx);
    }

    var audioObject = audioElement[0];
    attachMediaStream(audioObject, evt.stream);
};

function localDescCreated(desc) {
    pc.setLocalDescription(desc, function () {
        $(document).trigger("persistState", { mode: 'rtc', 'sdp': pc.localDescription });
    }, logError);
}

function logError(e) {
    bootbox.alert("Audio chat could not be started.");
}

function hasGetUserMedia() {
    return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
              navigator.mozGetUserMedia || navigator.msGetUserMedia);
}

server.onPersist = function(msg) {
    if (msg.mode == "rtc") {
        if (msg.sdp)
            pc.setRemoteDescription(new RTCSessionDescription(msg.sdp), function () {
                // if we received an offer, we need to answer
                if (pc.remoteDescription.type == 'offer')
                    pc.createAnswer(localDescCreated, logError);
            }, logError);
        else
            pc.addIceCandidate(new RTCIceCandidate(msg.candidate));
    }
}



// On click, start audio chat from this user.
$('#audioChatBtn').click(function() {
    if (!hasGetUserMedia()) {
        bootbox.alert('Audio conferencing is not supported by your browser. (Currently only supported by Chrome, Firefox, and Opera web browsers.)');
        return;
    }

    if (myAudioEnabled) {
        myStream.stop();
        displayAlert('Streaming closed', 'Audio chat is off');
        $('#audioChatBtn').removeClass('btn-success').addClass('btn-primary');

    } else {
        getUserMedia({ video: false, audio: true }, function (localMediaStream) {
            myStream = localMediaStream;
            pc.addStream(localMediaStream);
            displayAlert('Streaming...', 'Audio chat is enabled');
            $('#audioChatBtn').removeClass('btn-primary').addClass('btn-success');
        }, logError);
    }

    myAudioEnabled = !myAudioEnabled;
});
Run Code Online (Sandbox Code Playgroud)

我试过的

  • 'optional': [{ 'DtlsSrtpKeyAgreement': 'true' }]阅读此问题后尝试在配置中使用
  • 试图在每个请求中创建一个新的RTCPeerConnection()
  • 尝试使用本机浏览器功能而不是adapter.js.
  • 探索Web Audio API而不是 getUserMedia()

jes*_*sup 5

Firefox目前不支持onnegotiationneeded,因为我们目前不支持重新协商现有连接.所有addStream/addTrack和一个createDataChannel(如果你想使用它们)都需要 createOffer()或createAnswer 之前完成.您可以 createDataChannel()在连接之后,如果你createOffer之前创建的.

在连接后添加流将不起作用.

(恼人的)替代方案是创建一组新的PeerConnections来替换旧的PeerConnections(使用旧对中的DataChannel作为信号通道以降低延迟)

解决这个问题在我们的优先级列表中很重要,但需要更多版本.