Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRTC Cannot create peer connection

Tags:

webrtc

I can't create peer connection on webrtc app, after change webkitPeerConnection to webkitPeerConnection00

Why onSignalingMessage not calling in

 pc = new webkitPeerConnection00(stun, onSignalingMessage);

Here is code

$(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] + '人</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(){
        }
    });
}

});

like image 458
Tima Ospanov Avatar asked Aug 23 '12 21:08

Tima Ospanov


1 Answers

webkitPeerConnection00 is now using JSEP ---- so it passes ice-candidates instead of signalling message:

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

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

Please have a look the code of this project: https://webrtc-experiment.appspot.com/

like image 129
Muaz Khan Avatar answered Oct 21 '22 09:10

Muaz Khan