我在相關問題上閱讀了其他幾個問題,但沒有人回答我的問題。我有一個奇怪的問題,我可以使用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
- 在配置上採用
'optional': [{ 'DtlsSrtpKeyAgreement': 'true' }]
嘗試使用原生瀏覽器功能而不是adapter.js。 - 探索網絡音頻API而不是
getUserMedia()
親愛的downvoter,您提供的密切原因表明,問題「必須包括期望的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼」。我的問題包括所有這些內容。 – arao6 2014-09-23 03:14:56
沒有主題的問題,我想實現webRTC,但是有可能使user1聯繫user2,如果兩個都有他們的ID而不是加載。顯示所有連接的用戶到服務器? – Awena 2014-09-23 03:18:27
也許[peer.js](http://peerjs.com/)會更容易使用,然後重新發明輪子? – 2014-09-23 03:26:23