簡體   English   中英

WebRTC適用於Chrome,但不適用於Firefox

[英]WebRTC works in Chrome but not Firefox

我在相關問題上閱讀了其他幾個問題,但沒有人回答我的問題。 我有一個奇怪的問題,我可以使用WebRTC從chrome到firefox的音頻聊天,但不能使用firefox到chrome。

基本上,當用戶希望進行音頻聊天時,他/她單擊按鈕#audioChatBtn ,該按鈕使用getUserMedia()來設置流。 問題是,點擊#audioChatBtn從Firefox不火的onaddstream在Chrome回調,但點擊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;
});

我試過的

  • 在閱讀完這個問題后,在配置中嘗試使用'optional': [{ 'DtlsSrtpKeyAgreement': 'true' }]
  • 試圖在每個請求中創建一個新的RTCPeerConnection()
  • 嘗試使用本機瀏覽器功能而不是adapter.js
  • 探索Web Audio API而不是getUserMedia()

Firefox目前不支持onnegotiationneeded,因為我們目前不支持重新協商現有連接。 所有addStream / addTrack和一個createDataChannel(如果你想使用它們)都需要 createOffer()或createAnswer 之前完成。 可以 createDataChannel()在連接之后,如果你createOffer之前創建的。

在連接后添加流將不起作用。

(煩人的)替代方案是創建一組新的PeerConnections來替換舊的PeerConnections(使用舊對中的DataChannel作為信號通道以降低延遲)

解決這個問題在我們的優先級列表中很重要,但需要更多版本。

經過大量調試后,我意識到這個bug與我的代碼無關,而是與Firefox的WebRTC實現有關。 Firefox不會觸發onnegotiationneeded回調,因此我必須使用超時(並希望在函數觸發之前將流信息中繼到遠程客戶端)。 顯然,這是一個firefox錯誤,我會報告它,希望他們在下一個版本中修復bug。

        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');

            // Need this for Firefox
            if (webrtcDetectedBrowser == 'firefox')
                setTimeout(pc.onnegotiationneeded, 5000);

        }, logError);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM