<commit_before>isCaller = false;
mySid = "ivmos1";
iceCandidates = [];

error = function (error) {
    console.log(error);
}

init = function (onSuccess) {
    pc = new webkitRTCPeerConnection({
        "iceServers": [{
            "url": "stun:stun.l.google.com:19302"
        }]
    });

    var v = document.getElementById('v');

    pc.onaddstream = function (event) {
        console.log("onaddstream");
        console.log(event);
        v.src = URL.createObjectURL(event.stream);
        v.play();
    };

    pc.onicecandidate = function (event) {
        if (event.candidate) {
            Api.sendIceCandidate(mySid, otherSid, event.candidate.candidate);
        }
    };

    navigator.webkitGetUserMedia({
        video: true,
        audio: true
    },
    onSuccess,
    error);
}

/* Init a call */
call = function (otherId) {
    otherSid = otherId;
    isCaller = true;
    init(

    function (localMediaStream) {
        pc.addStream(localMediaStream);
        pc.createOffer(function (offer) {
            pc.setLocalDescription(
            offer,

            function () {
                Api.sendOffer(mySid, otherSid, offer.sdp);
            });
        });
    });
}

receiveOffer = function (offerSdp, otherId) {
    otherSid = otherId;
    isCaller = false;
    init(

    function (localMediaStream) {
        pc.addStream(localMediaStream);
        pc.setRemoteDescription(new RTCSessionDescription({
            type: "offer",
            sdp: offerSdp
        }),

        function () {
            pc.createAnswer(function (answer) {
                pc.setLocalDescription(answer);
                Api.sendAnswer(mySid, otherSid, answer.sdp);
            },
            error, {
                mandatory: {
                    OfferToReceiveAudio: true,
                    OfferToReceiveVideo: true
                }
            });

            // Sending pending candidates
            for (var i = 0; i < iceCandidates.length; i++) {
                addIceCandidate(iceCandidates[i]);
            }

        },
        error);
    });
}

receiveAnswer = function (answerSdp) {
    pc.setRemoteDescription(new RTCSessionDescription({
        type: "answer",
        sdp: answerSdp
    }));
}

addIceCandidate = function (candidateSdp) {
    if (pc && pc.remoteDescription) {
        pc.addIceCandidate(new RTCIceCandidate({
            candidate: candidateSdp
        }));
    } else {
        iceCandidates.push(candidateSdp);
    }
}

setInterval(function () {
    Api.getMessages(mySid, function (data) {
        console.log(data);
        try {
            data = JSON.parse(data);
        } catch (e) {
            return;
        }

        for (var i = 0; i < data.length; i++) {
            switch (data[i].type) {
                case "offer":
                    receiveOffer(data[i].request, data[i].from);
                    break;
                case "answer":
                    receiveAnswer(data[i].request);
                    break;
                case "candidate":
                    addIceCandidate(data[i].request);
                    break;

            };
        }

    });
}, 4000);

// Poll participants
setInterval(function () {
    Api.getParticipants(mySid, function (data) {
        data = JSON.parse(data);
        for (var i = 0; i < data.length; i++) {
            if ($("#otherId option[value='" + data[i] + "']").length == 0) {
                $('#otherId').append('<option value="' + data[i] + '">' + data[i] + '</a>');
            }
        }
    });
}, 4000);


Api.startSession(mySid);

$('#call').click(function () {
    call($('#otherId').val());
});<commit_msg>jsApp: mySid unhardcoded<commit_after>isCaller = false;
iceCandidates = [];
mySid = null;

error = function (error) {
    console.log(error);
}

init = function (onSuccess) {
    pc = new webkitRTCPeerConnection({
        "iceServers": [{
            "url": "stun:stun.l.google.com:19302"
        }]
    });

    var v = document.getElementById('v');

    pc.onaddstream = function (event) {
        console.log("onaddstream");
        console.log(event);
        v.src = URL.createObjectURL(event.stream);
        v.play();
    };

    pc.onicecandidate = function (event) {
        if (event.candidate) {
            Api.sendIceCandidate(mySid, otherSid, event.candidate.candidate);
        }
    };

    navigator.webkitGetUserMedia({
        video: true,
        audio: true
    },
    onSuccess,
    error);
}

/* Init a call */
call = function (otherId) {
    otherSid = otherId;
    isCaller = true;
    init(

    function (localMediaStream) {
        pc.addStream(localMediaStream);
        pc.createOffer(function (offer) {
            pc.setLocalDescription(
            offer,

            function () {
                Api.sendOffer(mySid, otherSid, offer.sdp);
            });
        });
    });
}

receiveOffer = function (offerSdp, otherId) {
    otherSid = otherId;
    isCaller = false;
    init(

    function (localMediaStream) {
        pc.addStream(localMediaStream);
        pc.setRemoteDescription(new RTCSessionDescription({
            type: "offer",
            sdp: offerSdp
        }),

        function () {
            pc.createAnswer(function (answer) {
                pc.setLocalDescription(answer);
                Api.sendAnswer(mySid, otherSid, answer.sdp);
            },
            error, {
                mandatory: {
                    OfferToReceiveAudio: true,
                    OfferToReceiveVideo: true
                }
            });

            // Sending pending candidates
            for (var i = 0; i < iceCandidates.length; i++) {
                addIceCandidate(iceCandidates[i]);
            }

        },
        error);
    });
}

receiveAnswer = function (answerSdp) {
    pc.setRemoteDescription(new RTCSessionDescription({
        type: "answer",
        sdp: answerSdp
    }));
}

addIceCandidate = function (candidateSdp) {
    if (pc && pc.remoteDescription) {
        pc.addIceCandidate(new RTCIceCandidate({
            candidate: candidateSdp
        }));
    } else {
        iceCandidates.push(candidateSdp);
    }
}

setInterval(function () {
    if (!mySid) {
        return;
    }
    Api.getMessages(mySid, function (data) {
        console.log(data);
        try {
            data = JSON.parse(data);
        } catch (e) {
            return;
        }

        for (var i = 0; i < data.length; i++) {
            switch (data[i].type) {
                case "offer":
                    receiveOffer(data[i].request, data[i].from);
                    break;
                case "answer":
                    receiveAnswer(data[i].request);
                    break;
                case "candidate":
                    addIceCandidate(data[i].request);
                    break;

            };
        }

    });
}, 4000);

// Poll participants
setInterval(function () {
    Api.getParticipants(mySid, function (data) {
        data = JSON.parse(data);
        for (var i = 0; i < data.length; i++) {
            if ($("#otherId option[value='" + data[i] + "']").length == 0) {
                $('#otherId').append('<option value="' + data[i] + '">' + data[i] + '</a>');
            }
        }
    });
}, 4000);

$('#sessionId').change(function(data) {
    mySid = $('#sessionId').val();
    Api.startSession(mySid);
});

$('#call').click(function () {
    call($('#otherId').val());
});
{'fraction_non_alphanumeric': 0.13952861952861953, 'fraction_numerical': 0.0047138047138047135, 'mean_word_length': 1.681834597327555, 'pattern_counts': {'":': 10, '<': 13, '<?xml version=': 0, '>': 7, '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': '4777151', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2193, 'n_words': 396}