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

前端之家收集整理的这篇文章主要介绍了javascript – WebRTC适用于Chrome,但不适用于Firefox前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在相关问题上阅读了其他几个问题,但没有人回答我的问题.我有一个奇怪的问题,我可以使用WebRTC从chrome到firefox的音频聊天,但不能使用firefox到chrome.

基本上,当用户希望进行音频聊天时,他/她单击按钮#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",'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后,在配置中尝试使用’可选’:[{‘DtlsSrtpKeyAgreement’:’true’}]
>尝试在每个请求中创建新的RTCPeerConnection()
>尝试使用本机浏览器功能而不是adapter.js.
>探索Web Audio API而不是getUserMedia()

解决方法

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

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

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

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

原文链接:https://www.f2er.com/js/158367.html

猜你在找的JavaScript相关文章