2014-09-23 105 views
7

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

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

我已經閱讀this question
  • 嘗試創建一個新的RTCPeerConnection()每個請求
  • 試過後試圖
    • 在配置上採用'optional': [{ 'DtlsSrtpKeyAgreement': 'true' }]嘗試使用原生瀏覽器功能而不是adapter.js
    • 探索網絡音頻API而不是getUserMedia()
    +3

    親愛的downvoter,您提供的密切原因表明,問題「必須包括期望的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼」。我的問題包括所有這些內容。 – arao6 2014-09-23 03:14:56

    +0

    沒有主題的問題,我想實現webRTC,但是有可能使user1聯繫user2,如果兩個都有他們的ID而不是加載。顯示所有連接的用戶到服務器? – Awena 2014-09-23 03:18:27

    +0

    也許[peer.js](http://peerjs.com/)會更容易使用,然後重新發明輪子? – 2014-09-23 03:26:23

    回答

    5

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

    在連接後添加數據流將不起作用。

    安(煩人)另一種方法是創建一組新PeerConnections來替換舊的(在舊對作爲一信令信道使用DataChannel較低等待時間)

    解決這是高我們的優先級列表上,但會多發幾個版本。

    +2

    相關錯誤:https://bugzilla.mozilla.org/show_bug.cgi?id=1071643 – ptman 2014-11-20 11:12:15

    +0

    顯然在ff 38中修復:https://hacks.mozilla.org/ 2015/03/webrtc-in-firefox-38-multistream-and-renegotiation/ – ptman 2015-05-25 13:46:53

    +0

    是的,它落在Fx38中,現在是Firefox版本 – jesup 2015-06-11 12:01:50

    1

    大量的調試後,我才明白,錯誤無關我的代碼,但與Firefox的執行的WebRTC要做。 Firefox不會觸發onnegotiationneeded回撥,所以我必須使用超時(並且希望在啓動該功能之前將流信息傳遞到遠程客戶端)。顯然,這是一個Firefox的錯誤,我會報告它,希望他們修復下一個版本的錯誤。

     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); 
    
    相關問題