Firefox无法创建优惠:无法创建没有本地曲目,没有offerToReceiveAudio / Video,也没有DataChannel的优惠

时间:2016-03-05 18:18:40

标签: javascript firefox webrtc

我遇到了Firefox的问题。此代码无可挑剔地在Chrome中有效。

function create_offer() {
    pc = new peer_connection({iceServers: [
        {urls: "stun:stun.l.google.com:19302"}
    ]});
    //pc.createDataChannel('channel');

    pc.createOffer(
        create_offer_success,
        create_offer_error,
        setSdpConstraints()
    );

    pc.onaddstream = on_add_stream;
    pc.onicecandidate = on_ice_candidate;
}

function create_offer_success(desc) {
    pc.setLocalDescription(desc);
}

function create_offer_error(error) {
    console.error("create_offer_error() error:", error);
}

function on_ice_candidate(event){
    if (event.candidate) {
        ice.push(event.candidate);
    } else {
        return send('/offer', {
            'id': id,
            'desc': pc.localDescription,
            'ice': ice
        })
        .then(function(data) {
            return pc.setRemoteDescription(new session_description(data.desc), function() {
                for(var i = 0, l = data.ice.length; i < l; i++) {
                    pc.addIceCandidate(new ice_candidate(data.ice[i]));
                }
                return data;
             }, function(err) {
                console.error(err);
            });
        })
        .catch(function(err) {
            console.error('on_ice_candidate() catch: ', err);
        });
    }
}

function on_add_stream(event) {
    console.log(event);
    stream = event.stream;
    var vid = document.getElementById("stream");
    if(isFirefox) {
        vid.mozSrcObject = stream;
    } else {
        vid.src = URL.createObjectURL(stream)
    }
}

function setSdpConstraints() {
    return !!navigator.mozGetUserMedia && firefoxVersion > 34 ?
    {
        OfferToReceiveAudio: true,
        OfferToReceiveVideo: true
    } :
    {
        optional: [],
        mandatory: {
            OfferToReceiveAudio: true,
            OfferToReceiveVideo: true
        }
    };
}

Firefox显示错误:无法创建没有本地曲目,没有offerToReceiveAudio / Video,也没有DataChannel的优惠。 Firefox版本是44。 我做错了什么?

2 个答案:

答案 0 :(得分:4)

您为Firefox设置了错误的约束:

{
    OfferToReceiveAudio: true,
    OfferToReceiveVideo: true
}

您需要设置的内容(Chrome也支持此功能,因为Chrome 38)应该是:

{
    offerToReceiveAudio: 1,
    offerToReceiveVideo: 1
}

注意案例差异。

答案 1 :(得分:2)

Philipp Hancke的答案是正确的,mediacontraints有一个案例问题。

要简化约束配置,您可以合并Chrome&amp; Firefox有这样的限制:

var mediaConstraints = {
    mandatory: {
        'OfferToReceiveAudio': true,
        'OfferToReceiveVideo': true
    },
    'offerToReceiveAudio': true,
    'offerToReceiveVideo': true    
};

这样您就可以在Chrome和Firefox上使用相同的代码创建优惠:

if (typeof RTCPeerConnection === "undefined") {
	RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
}
	
var pcConfig = {"iceServers": [] };
var pc = new RTCPeerConnection(pcConfig);

var mediaConstraints = {
    mandatory: {
        'OfferToReceiveAudio': true,
        'OfferToReceiveVideo': true
    },
    'offerToReceiveAudio': true,
    'offerToReceiveVideo': true    
};
pc.createOffer(function(session) { alert("Create offer ok"); }
             , function(error)   { alert("Create offer error:" + error);  }
             , mediaConstraints);