<commit_before>'use strict';

// https://codelabs.developers.google.com/codelabs/webrtc-web/#4
var lv = document.getElementById("local");

navigator.webkitGetUserMedia({
    video:true,audio:false
},function(stream){
    lv.src = window.URL.createObjectURL(stream);
    console.log("local stream " + lv.src);

    rtcCall(stream);
}, function(error){
    console.error(error);
});

function rtcCall(localStream) {
    // local peer connection setup.
    var pcLocal = new window.webkitRTCPeerConnection(null);
    pcLocal.onicecandidate = function(e) {
        e.candidate && console.log("pcLocal candidate: " + e.candidate.candidate);
        e.candidate && pcRemote.addIceCandidate(new window.RTCIceCandidate(e.candidate));
    };

    // remote peer connection setup.
    var pcRemote = new window.webkitRTCPeerConnection(null);
    pcRemote.onicecandidate = function(e) {
        e.candidate && console.log("pcRemote candidate: " + e.candidate.candidate);
        e.candidate && pcLocal.addIceCandidate(new window.RTCIceCandidate(e.candidate));
    };

    pcRemote.onaddstream = function(e) {
        var rv = document.getElementById("remote");
        rv.src = window.URL.createObjectURL(e.stream);
        console.log("remote stream " + rv.src);
    };

    // trigger connection between local and remote peer.
    pcLocal.addStream(localStream);
    pcLocal.createOffer(function(desc){
        pcLocal.setLocalDescription(desc); // trigger pcLocal.onicecandidate().
        pcRemote.setRemoteDescription(desc);
        console.log("Offer from pcLocal: " + desc.sdp.length + " size sdp");

        fireRemote(desc);
    }, function(error){
        console.error(error);
    });

    // fire remote when got local peer event.
    var fireRemote = function(desc){
        // Since the 'remote' side has no media stream we need
        // to pass in the right constraints in order for it to
        // accept the incoming offer of audio and video.
        pcRemote.createAnswer(function(desc){
            pcRemote.setLocalDescription(desc); // trigger pcRemote.onicecandiate().
            pcLocal.setRemoteDescription(desc);
            console.log("Answer from pcRemote: " + desc.sdp.length + " size sdp");
        }, function(error){
            console.error(error);
        });
    };
}<commit_msg>Refine the peer connection tutorial<commit_after>'use strict';

// https://codelabs.developers.google.com/codelabs/webrtc-web/#4
var lv = document.getElementById("local");

navigator.webkitGetUserMedia({
    video:true,audio:false
},function(stream){
    lv.src = window.URL.createObjectURL(stream);
    console.log("[navigator.webkitGetUserMedia] lv.src=localStream " + lv.src);

    rtcCall(stream);
}, function(error){
    console.error(error);
});

function copyObject(obj) {
    var cp = {};
    for (var key in obj) {
        cp[key] = obj[key];
    }
    return cp;
}

function rtcCall(localStream) {
    // local peer connection setup.
    var pcLocal = new window.webkitRTCPeerConnection(null);
    pcLocal.onicecandidate = function(e) {
        e.candidate && console.log("[pcLocal.onicecandidate] " + e.candidate.candidate);
        e.candidate && pcRemote.addIceCandidate(new window.RTCIceCandidate(e.candidate));
    };

    // remote peer connection setup.
    var pcRemote = new window.webkitRTCPeerConnection(null);
    pcRemote.onicecandidate = function(e) {
        e.candidate && console.log("[pcRemote.onicecandidate] " + e.candidate.candidate);
        e.candidate && pcLocal.addIceCandidate(new window.RTCIceCandidate(e.candidate));
    };

    pcRemote.onaddstream = function(e) {
        var rv = document.getElementById("remote");
        rv.src = window.URL.createObjectURL(e.stream);
        console.log("[pcRemote.onaddstream] rv.src=remoteStream " + rv.src);
    };

    // trigger connection between local and remote peer.
    pcLocal.addStream(localStream);
    console.log("[pcLocal.addStream] add localStream to peer connection");

    pcLocal.createOffer(function(offer){
        pcLocal.setLocalDescription(offer); // trigger pcLocal.onicecandidate().
        console.log("[pcLocal.createOffer] Request with offer " + offer.sdp.length + "B sdp as bellow:");
        console.log(offer);

        transmitOffer(offer);
    }, function(error){
        console.error(error);
    });

    // Local transmit offer to remote.
    var transmitOffer = function(offer) {
        offer = copyObject(offer); // To demonstrate offer transmit over network in JSON.
        setTimeout(onRemoteGotOffer, 200, offer);
    };
    var onRemoteGotOffer = function(offer) {
        pcRemote.setRemoteDescription(offer);
        console.log("[onRemoteGotOffer] Got offer " + offer.sdp.length + "B sdp as bellow:");
        console.log(offer);

        // Since the 'remote' side has no media stream we need
        // to pass in the right constraints in order for it to
        // accept the incoming offer of audio and video.
        pcRemote.createAnswer(function(answer){
            pcRemote.setLocalDescription(answer); // trigger pcRemote.onicecandiate().
            console.log("[pcRemote.createAnswer] Response offer=" + offer.sdp.length + "B answer " + answer.sdp.length + "B sdp as bellow:");
            console.log(answer);

            transmitAnswer(answer);
        }, function(error){
            console.error(error);
        });
    };

    // Remote transmit answer to local.
    var transmitAnswer = function(answer) {
        answer = copyObject(answer); // To demonstrate answer transmit over network in JSON.
        setTimeout(onLocalGotAnswer, 200, answer);
    };
    var onLocalGotAnswer = function(answer) {
        pcLocal.setRemoteDescription(answer);
        console.log("[pcRemote.createAnswer] Got answer " + answer.sdp.length + "B sdp as bellow:");
        console.log(answer);
    };
}
{'fraction_non_alphanumeric': 0.1266689489900719, 'fraction_numerical': 0.0013693940431359123, 'mean_word_length': 3.035220994475138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4404408', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1694, 'n_words': 362}