5

将 webkitPeerConnection 更改为 webkitPeerConnection00 后,我无法在 webrtc 应用程序上创建对等连接

为什么 onSignalingMessage 不调用

 pc = new webkitPeerConnection00(stun, onSignalingMessage);

这是代码

$(function(){
var localInfo = {
    video:  $("#video_local").get(0),
    video_remote:  $("#video_remote").get(0),
    stream: null,
    initiator: false,
    peerCon: null,
    started: false,
    uid: render_param.uid,
    readySDP: null
};
wsSend({"act": "list", "join": "1"}, function(data){
    updateRoomDisplay(data.room_list);
    setInterval(function(){
        wsSend({"act": "check"}, function(data){
            $.each(data.ret, function(i, json_str){
                var msg = JSON.parse(json_str)
                if (msg.type == "sdp"){
                    receiveSDP(msg.sdp);
                } else if (msg.type == "room_info"){
                    updateRoomDisplay(msg.room_list);
                }

            });
        });
    }, 3000);
});
function updateRoomDisplay(room_list){
    console.log("list receive", room_list);
    var container = $("#room_container");
    container.empty();
    $.each(room_list, function(i, room_persons){
        if (i > 0){
            var class_add = "";
            if (room_persons == 2){
                class_add = " room_full";
            }
            $('<div class="room' + class_add + '" data-no="' + i + '"> Room' + (100 + i) + 
                "<br/>(" + room_persons + "人)</div>").appendTo(container);
        }
    });
    container.append($('<div style="clear: both">ロビー'+ room_list[0] + '人&lt;/div>'));
}
function receiveSDP(sdp){
    if (sdp == 'BYE') {
        console.log('Session terminated.');
        localInfo.video_remote.src = null;
        localInfo.video_remote.style.opacity = 0;
        localInfo.initiator = false;
        localInfo.started = false;
    } else {
        if (sdp.indexOf("\"ERROR\"", 0) == -1) {
            if (!localInfo.initiator && !localInfo.started) {
                maybeStart();
            }
            if (localInfo.peerCon){
                console.log("processSignalingMessage1");
                localInfo.peerCon.processSignalingMessage(sdp);
            } else {
                console.log("sdp ready");
                localInfo.readySDP = sdp;
            }
        } else {
            console.log("ERROR ***************");
        }
    }
}
$(window).unload(function(){
    $.ajax({
        type: "POST",
        url: "/ajax",
        async: false,
        data: {"act": "leave", "uid": localInfo.uid},
        dataType: "json"
    });
});


$("#room_container").on("click", ".room", function(){
    var no = parseInt($(this).attr("data-no"));
    try {
        navigator.webkitGetUserMedia({audio:true, video:true}, onGUMSuccess, onGUMError);
        console.log("Requested access to local media with new syntax.");
    } catch (e) {
        try {
            navigator.webkitGetUserMedia("video,audio", onGUMSuccess, onGUMError);
            console.log("Requested access to local media with old syntax.");
        } catch (e) {
            alert("webkitGetUserMedia() failed. Is the MediaStream flag enabled in about:flags?");
            console.log("webkitGetUserMedia failed with exception: " + e.message);
        }
    }
    function onGUMSuccess(stream){
        localInfo.stream = stream;
        localInfo.video.src = window.webkitURL ?  window.webkitURL.createObjectURL(stream) : stream;

        wsSend({act: "move", room_fr: 0, room_to: no}, function(data){
            console.log("move_ok receive");
            console.log(data.room_list);
            localInfo.room = data.room_to;
            if (data.count == 2){
                localInfo.initiator = true;
                maybeStart();
            } else {
                localInfo.initiator = false;
            }
        });
        $("#video_container").slideDown();
        $("#room_container").fadeOut();

    }
    function onGUMError(error){
        console.error('An error occurred: [CODE ' + error.code + ']');
        return;
    }
});
$("#dis").click(function(){
    localInfo.stream.stop();
    localInfo.stream = null;
    localInfo.started = false;
    $(localInfo.video).removeAttr("src");
    $(localInfo.video_remote).removeAttr("src");
    if (localInfo.peerCon){
        localInfo.peerCon.close();
        localInfo.peerCon = null;
    }
    wsSend({act: "move", room_fr: localInfo.room, room_to: 0}, function(data){
        console.log("move_ok receive");
        console.log(data.room_list);
        localInfo.room = data.room_to;
    });
    $("#video_container").slideUp();
    $("#room_container").fadeIn();

});

function maybeStart() {
    if (!localInfo.started && localInfo.stream) {
        console.log("Creating PeerConnection.");
        createPeerConnection();
        console.log("Adding local stream.");
        localInfo.peerCon.addStream(localInfo.stream);
        localInfo.started = true;
    }
}


function createPeerConnection(){
    var pc;
    var stun = "STUN stun.l.google.com:19302";

    if (localInfo.peerCon == null){
        console.log("createPeerConnection")
        try {
            pc = new webkitDeprecatedPeerConnection(stun, onSignalingMessage);
        } catch (e){
            try {
                pc = new webkitPeerConnection00(stun, onSignalingMessage);
                console.log("Created webkitPeerConnnection with config.");
            } catch (e) {
                console.log("Failed to create webkitPeerConnection, exception: " + e.message);
                alert("Cannot create PeerConnection object; Is the 'PeerConnection' flag enabled in about:flags?");
                return;
            }
        }
        localInfo.peerCon = pc;
    } else {
        console.log("use peerConnection")
        pc = localInfo.peerCon;
    }

    $(pc).bind("connecting", function(){
        console.log("onSessionConnecting...");
    });
    $(pc).bind("open", function(){
        console.log("onSessionOpened...");
    });

    pc.onaddstream = function(){
        console.log("onRemoteStreamAdded...");
        var url = webkitURL.createObjectURL(event.stream);
        localInfo.video_remote.src = url;

    };
    $(pc).bind("removestream", function(){
        console.log("onRemoteStreamRemoved...");
    });

    if (localInfo.readySDP){
        console.log("processSignalingMessage2");
        localInfo.peerCon.processSignalingMessage(localInfo.readySDP);
    }
}

function onSignalingMessage(msg) {
    console.log("receive signaling message");
    wsSend({"act": "sdp", "room_no": localInfo.room, "sdp": msg});
}

function wsSend(obj, callback){
    if (!callback){
        callback = function(res){
            console.log(res);
        };
    }
    obj = obj || {};
    obj["uid"] = localInfo.uid;
    $.ajax({
        type: "POST",
        url: "/ajax",
        data: obj,
        dataType: "json",
        success: callback,
        error: function(){
        }
    });
}

});

4

1 回答 1

7

webkitPeerConnection00现在正在使用JSEP ---- 所以它通过ice-candidates而不是信令消息

window.PeerConnection = window.webkitPeerConnection00;
var peer = new PeerConnection(stun, onGettingIceCandidates);

function onGettingIceCandidates(candidate, isLastCandidate)
{
    // candidate.toSdp() ------ and ------ candidate.label
}

请看一下这个项目的代码: https ://webrtc-experiment.appspot.com/

于 2012-08-24T02:18:28.560 回答