<commit_before>
var App = (function(w, d, n, $) {

    var peerCon,
        $header = $('#ice-header'),
        $main = $('#main'),
        $c2aDiv = $main.find('.c2a'),
        $c2aForm = $('#c2a-form'),
        $c2aToggle = $('#c2a-toggle'),
        $hdrToggle = $('#hdr-toggle'),
        actions = {},
        $mainVideo = $('#main-video'),
        $overlayVideo = $('#overlay-video');
        firstPlayed = false;

    var constraints = {
        video: true,    // ask for Camera
        audio: true     // ask for Microphone
    }

    // Defining a common method by checking vendor prefixes
    n.getMedia = ( 
        n.getUserMedia ||       // default
        n.webkitGetUserMedia || // Chrome and Safari
        n.mozGetUserMedia ||    // Firefox
        n.msGetUserMedia);      // IE 9, 10

    w.RTCPeerConnection = w.webkitRTCPeerConnection || w.mozRTCPeerConnection;

    var socket = new WebSocket('ws://' + window.location.host + window.location.pathname);

    socket.onmessage = function(message) {
        var msg = JSON.parse(message.data);

        switch(msg.type) {
            case 'uuid' :
            socket.id = msg.id;
            break;

        case 'received_offer' : 
            console.log('received offer', msg.data);
            peerCon.setRemoteDescription(new RTCSessionDescription(msg.data));
            peerCon.createAnswer(function(description) {
                console.log('sending answer');
                peerCon.setLocalDescription(description); 
                socket.send(
                    JSON.stringify({
                        type: 'received_answer', 
                        data: description
                    })
                );
            }, null, mediaConstraints);
            break;

        case 'received_answer' :
            console.log('received answer');
            if(!connected) {
                peerCon.setRemoteDescription(new RTCSessionDescription(msg.data));
                connected = true;
            }
            break;

        case 'received_candidate' :
            console.log('received candidate');
            var candidate = new RTCIceCandidate({
                sdpMLineIndex: msg.data.label,
                candidate: msg.data.candidate
            });
            peerCon.addIceCandidate(candidate);
            break;
        }
    };

    var 
        // the famous Google STUN server for signaling
        configuration = {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]},
        connected = false,
        mediaConstraints = {
            'mandatory': {
                'OfferToReceiveAudio':true, 
                'OfferToReceiveVideo':true
            }
        };

    // For peer
    // Doesn't run in stable Mozilla
    peerCon = new w.RTCPeerConnection(configuration);

    peerCon.onicecandidate = function(e) {
        if(e.candidate) {
            socket.send(
                JSON.stringify({
                    type: 'received_candidate',
                    data: {
                        label: e.candidate.sdpMLineIndex,
                        id: e.candidate.sdpMid,
                        candidate: e.candidate.candidate
                    }
                })
            );
        }
    };

    peerCon.onaddstream = function(e) {
        console.log('remote start video stream');
        setVideoSrc($overlayVideo[0], e.stream);
        $overlayVideo[0].play();
        $overlayVideo.removeClass('overlay');
        $mainVideo.addClass('overlay');
    };

    // Privates

    function setVideoSrc(el, s) {
        if (n.mozGetUserMedia) { 
            el.mozSrcObject = s; // only for FF
        } else {
            var vendorURL = w.URL || w.webkitURL;
            el.src = vendorURL.createObjectURL(s);
        }
    }

    function playMainVideo() {

        // gets local video stream and renders to vid1
        n.getMedia( constraints, function(localMediaStream) {
            peerCon.addStream(localMediaStream);

            setVideoSrc($mainVideo[0], localMediaStream);

            $mainVideo[0].play(); // not doing this doesn't work in some browsers
            $mainVideo[0].title = 'Double click on video to launch full screen!';

            // initCall is set in views/index and is based on if there is another person in the room to connect to
            if(initCall)
                start();
        }, mainVideoError);
    }

    function start() {
      // this initializes the peer connection
        peerCon.createOffer(function(description) {
            peerCon.setLocalDescription(description);
            socket.send(
                JSON.stringify({
                    type: 'received_offer',
                    data: description
                })
            );
        }, null, mediaConstraints);
    }

    function closePeer() {
        socket.send(
            JSON.stringify({
                type: 'close'
            })
        );

        peerCon.close();
        peerCon = null;

        $mainVideo.removeClass('overlay');
        $overlayVideo.addClass('overlay');
        firstPlayed = false;
    };

    // window.onload = function() {<commit_msg>Check with gh-branch<commit_after>
var App = (function(w, d, n, $) {

    var peerCon,
        $header = $('#ice-header'),
        $main = $('#main'),
        $c2aDiv = $main.find('.c2a'),
        $c2aForm = $('#c2a-form'),
        $c2aToggle = $('#c2a-toggle'),
        $hdrToggle = $('#hdr-toggle'),
        actions = {},
        $mainVideo = $('#main-video'),
        $overlayVideo = $('#overlay-video');
        firstPlayed = false;

    var constraints = {
        video: true,    // ask for Camera
        audio: true     // ask for Microphone
    }

    // Defining a common method by checking vendor prefixes
    n.getMedia = ( 
        n.getUserMedia ||       // default
        n.webkitGetUserMedia || // Chrome and Safari
        n.mozGetUserMedia ||    // Firefox
        n.msGetUserMedia);      // IE 9, 10

    w.RTCPeerConnection = w.webkitRTCPeerConnection || w.mozRTCPeerConnection;

    var socket = new WebSocket('ws://' + window.location.host + window.location.pathname);

    socket.onmessage = function(message) {
        var msg = JSON.parse(message.data);

        switch(msg.type) {
            case 'uuid' :
            socket.id = msg.id;
            break;

        case 'offered' : 
            console.log('Received offer', msg.data);
            peerCon.setRemoteDescription(new RTCSessionDescription(msg.data));
            peerCon.createAnswer(function(description) {
                console.log('sending answer');
                peerCon.setLocalDescription(description); 
                socket.send(
                    JSON.stringify({
                        type: 'answered', 
                        data: description
                    })
                );
            }, null, mediaConstraints);
            break;

        case 'answered' :
            console.log('Received answer');
            if(!connected) {
                peerCon.setRemoteDescription(new RTCSessionDescription(msg.data));
                connected = true;
            }
            break;

        case 'candidate' :
            console.log('Received ICE request');
            var candidate = new RTCIceCandidate({
                sdpMLineIndex: msg.data.label,
                candidate: msg.data.candidate
            });
            peerCon.addIceCandidate(candidate);
            break;
        }
    };

    var 
        // the famous Google STUN server for signaling
        configuration = {"iceServers": [{"url": "stun:stun.l.google.com:19302"}]},
        connected = false,
        mediaConstraints = {
            'mandatory': {
                'OfferToReceiveAudio':true, 
                'OfferToReceiveVideo':true
            }
        };

    // For peer
    // Doesn't run in stable Mozilla
    peerCon = new w.RTCPeerConnection(configuration);

    peerCon.onicecandidate = function(e) {
        if(e.candidate) {
            socket.send(
                JSON.stringify({
                    type: 'candidate',
                    data: {
                        label: e.candidate.sdpMLineIndex,
                        id: e.candidate.sdpMid,
                        candidate: e.candidate.candidate
                    }
                })
            );
        }
    };

    peerCon.onaddstream = function(e) {
        console.log('remote start video stream');
        setVideoSrc($overlayVideo[0], e.stream);
        $overlayVideo[0].play();
        $overlayVideo.removeClass('overlay');
        $mainVideo.addClass('overlay');
    };

    // Privates

    function setVideoSrc(el, s) {
        if (n.mozGetUserMedia) { 
            el.mozSrcObject = s; // only for FF
        } else {
            var vendorURL = w.URL || w.webkitURL;
            el.src = vendorURL.createObjectURL(s);
        }
    }

    function playMainVideo() {

        // gets local video stream and renders to vid1
        n.getMedia( constraints, function(localMediaStream) {
            peerCon.addStream(localMediaStream);

            setVideoSrc($mainVideo[0], localMediaStream);

            $mainVideo[0].play(); // not doing this doesn't work in some browsers
            $mainVideo[0].title = 'Double click on video to launch full screen!';

            // initCall is set in views/index and is based on if there is another person in the room to connect to
            if(initCall)
                start();
        }, mainVideoError);
    }

    function start() {
      // this initializes the peer connection
        peerCon.createOffer(function(description) {
            peerCon.setLocalDescription(description);
            socket.send(
                JSON.stringify({
                    type: 'offered',
                    data: description
                })
            );
        }, null, mediaConstraints);
    }

    function closePeer() {
        socket.send(
            JSON.stringify({
                type: 'close'
            })
        );

        peerCon.close();
        peerCon = null;

        $mainVideo.removeClass('overlay');
        $overlayVideo.addClass('overlay');
        firstPlayed = false;
    };

    // window.onload = function() {
{'fraction_non_alphanumeric': 0.11406509627602385, 'fraction_numerical': 0.003909686247678624, 'mean_word_length': 1.5383279583229967, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1725079', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2625, 'n_words': 571}