<commit_before>// Global Namespace
var Camera = Camera || function(minWidth, minHeight, video, canvas, image, scale) {
        this.video = video;

        this.scale = scale;

        this.canvas = canvas;

        this.image = image;

        this.resolution = {
            "mandatory": {
                "minWidth": minWidth,
                "minHeight": minHeight
            }
        };

        // Setting up the mediaStream so that when the user closes the video, the stream ends
        this.mediaStream = null;

        // Video rotation
        this.videoRotation = 0;

        // Video rotated recently?
        this.rotated = 0;
    };

Camera.prototype.startCamera = function(success, error) {
    var self = this;

    navigator.getUserMedia(
        {
            audio: false,
            video: self.resolution
        },
        function successCallBack(stream) {
            var retryCount = 0;
            var retryLimit = 50;

            self.mediaStream = stream;

            self.video.src = (window.URL && window.URL.createObjectURL(stream));

            self.video.play();

            self.video.onplaying = function(e) {
                var videoWidth = this.videoWidth;
                var videoHeight = this.videoHeight;
                self.video.width = videoWidth * self.scale;
                self.video.height = videoHeight * self.scale;

                if (!videoWidth || !videoHeight) {
                    if (retryCount < retryLimit) {
                        retryCount++;
                        window.setTimeout(function () {
                            video.pause();
                            video.play();
                        }, 100);
                    }

                } else if (videoWidth && videoHeight) {
                    self.image.height = self.video.videoHeight;
                    self.image.width = self.video.videoWidth;

                    self.canvas.width = videoWidth;
                    self.canvas.height = videoHeight;
                } else {
                    console.log("An error has occurred: Can't retrieve video width and height");
                }
            };

            success();
        }, function errorCallback(error) {
            console.log("An error occurred: " + error.code);

            error();
        }
    );
};

Camera.prototype.stopCamera = function(success, error) {
    this.mediaStream.stop();
    this.mediaStream = null;
    this.video.pause();

    if ((this.mediaStream == null) && (this.video.paused)) {
        success();
    } else {
        console.log("Error: Could not pause/stop camera");
        error();
    }
};

Camera.prototype.takePicture = function(callback) {
    var self = this;

    // Get the frame from the video
    var ctx = self.canvas.getContext('2d');

    if (self.rotated == 1) {
        // Base on the rotation
        switch (self.videoRotation) {
            case -90:
            case 270:
                ctx.translate(0, self.canvas.height);
                self.image.height = self.video.videoWidth;
                self.image.width = self.video.videoHeight;
                break;
            case 90:
            case -270:
                ctx.translate(self.canvas.width, 0);
                self.image.height = self.video.videoWidth;
                self.image.width = self.video.videoHeight;
                break;
            case 180:
            case -180:
                ctx.translate(self.canvas.width, self.canvas.height);
                self.image.height = self.video.videoHeight;
                self.image.width = self.video.videoWidth;
                break;
            case 0:
                self.image.height = self.video.videoHeight;
                self.image.width = self.video.videoWidth;
                break;
            default:
                console.log('Error: Can not translate ctx');
                break;
        }

        self.rotated = 0;
        ctx.rotate(self.videoRotation * (Math.PI/180));
    }

    // Draw the image to the canvas
    ctx.drawImage(self.video, 0, 0);

    // Set the source
    self.image.src = self.canvas.toDataURL('image/png', 1.0);

    callback();
};

Camera.prototype.rotateLeft = function() {
    var self = this;
    self.videoRotation -= 90;

    if (self.videoRotation < -270) {
        self.videoRotation = 0;
    }

    // Switch base on video rotation
    switch (self.videoRotation) {
        case 90:
        case -90:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 180:
        case -180:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        case 270:
        case -270:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 0:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        default:
            console.log('Error: Could not rotate canvas');
            break;
    }

    self.rotated = 1;
    self.video.style.transform = ' rotate(' + this.videoRotation + 'deg)';
};

Camera.prototype.rotateRight = function() {
    var self = this;
    self.videoRotation += 90;

    if (self.videoRotation > 270) {
        self.videoRotation = 0;
    }

    // Switch base on video rotation
    switch (self.videoRotation) {
        case 90:
        case -90:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 180:
        case -180:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        case 270:
        case -270:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 0:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        default:
            console.log('Error: Could not rotate canvas');
            break;
    }

    self.rotated = 1;
    self.video.style.transform = ' rotate(' + this.videoRotation + 'deg)';
};<commit_msg>Moved success callback in startCamera() to the inside when the video is actually play and included the error callback when the video can not retrieve video.videoWidth and video.videoHeight<commit_after>// Global Namespace
var Camera = Camera || function(minWidth, minHeight, video, canvas, image, scale) {
        this.video = video;

        this.scale = scale;

        this.canvas = canvas;

        this.image = image;

        this.resolution = {
            "mandatory": {
                "minWidth": minWidth,
                "minHeight": minHeight
            }
        };

        // Setting up the mediaStream so that when the user closes the video, the stream ends
        this.mediaStream = null;

        // Video rotation
        this.videoRotation = 0;

        // Video rotated recently?
        this.rotated = 0;
    };

Camera.prototype.startCamera = function(success, error) {
    var self = this;

    navigator.getUserMedia(
        {
            audio: false,
            video: self.resolution
        },
        function successCallBack(stream) {
            var retryCount = 0;
            var retryLimit = 50;

            self.mediaStream = stream;

            self.video.src = (window.URL && window.URL.createObjectURL(stream));

            self.video.play();

            self.video.onplaying = function(e) {
                var videoWidth = this.videoWidth;
                var videoHeight = this.videoHeight;
                self.video.width = videoWidth * self.scale;
                self.video.height = videoHeight * self.scale;

                if (!videoWidth || !videoHeight) {
                    if (retryCount < retryLimit) {
                        retryCount++;
                        window.setTimeout(function () {
                            video.pause();
                            video.play();
                        }, 100);
                    }

                } else if (videoWidth && videoHeight) {
                    self.image.height = self.video.videoHeight;
                    self.image.width = self.video.videoWidth;

                    self.canvas.width = videoWidth;
                    self.canvas.height = videoHeight;

                    success();
                } else {
                    console.log("An error has occurred: Can't retrieve video width and height");

                    error();
                }
            };
        }, function errorCallback(error) {
            console.log("An error occurred: " + error.code);

            error();
        }
    );
};

Camera.prototype.stopCamera = function(success, error) {
    this.mediaStream.stop();
    this.mediaStream = null;
    this.video.pause();

    if ((this.mediaStream == null) && (this.video.paused)) {
        success();
    } else {
        console.log("Error: Could not pause/stop camera");
        error();
    }
};

Camera.prototype.takePicture = function(callback) {
    var self = this;

    // Get the frame from the video
    var ctx = self.canvas.getContext('2d');

    if (self.rotated == 1) {
        // Base on the rotation
        switch (self.videoRotation) {
            case -90:
            case 270:
                ctx.translate(0, self.canvas.height);
                self.image.height = self.video.videoWidth;
                self.image.width = self.video.videoHeight;
                break;
            case 90:
            case -270:
                ctx.translate(self.canvas.width, 0);
                self.image.height = self.video.videoWidth;
                self.image.width = self.video.videoHeight;
                break;
            case 180:
            case -180:
                ctx.translate(self.canvas.width, self.canvas.height);
                self.image.height = self.video.videoHeight;
                self.image.width = self.video.videoWidth;
                break;
            case 0:
                self.image.height = self.video.videoHeight;
                self.image.width = self.video.videoWidth;
                break;
            default:
                console.log('Error: Can not translate ctx');
                break;
        }

        self.rotated = 0;
        ctx.rotate(self.videoRotation * (Math.PI/180));
    }

    // Draw the image to the canvas
    ctx.drawImage(self.video, 0, 0);

    // Set the source
    self.image.src = self.canvas.toDataURL('image/png', 1.0);

    callback();
};

Camera.prototype.rotateLeft = function() {
    var self = this;
    self.videoRotation -= 90;

    if (self.videoRotation < -270) {
        self.videoRotation = 0;
    }

    // Switch base on video rotation
    switch (self.videoRotation) {
        case 90:
        case -90:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 180:
        case -180:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        case 270:
        case -270:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 0:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        default:
            console.log('Error: Could not rotate canvas');
            break;
    }

    self.rotated = 1;
    self.video.style.transform = ' rotate(' + this.videoRotation + 'deg)';
};

Camera.prototype.rotateRight = function() {
    var self = this;
    self.videoRotation += 90;

    if (self.videoRotation > 270) {
        self.videoRotation = 0;
    }

    // Switch base on video rotation
    switch (self.videoRotation) {
        case 90:
        case -90:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 180:
        case -180:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        case 270:
        case -270:
            self.canvas.height = self.video.videoWidth;
            self.canvas.width = self.video.videoHeight;

            self.video.height = self.video.videoWidth * self.scale;
            break;
        case 0:
            self.canvas.height = self.video.videoHeight;
            self.canvas.width = self.video.videoWidth;

            self.video.height = self.video.videoHeight * self.scale;
            break;
        default:
            console.log('Error: Could not rotate canvas');
            break;
    }

    self.rotated = 1;
    self.video.style.transform = ' rotate(' + this.videoRotation + 'deg)';
};
{'fraction_non_alphanumeric': 0.10729248029539161, 'fraction_numerical': 0.012071291628204217, 'mean_word_length': 1.5069419722321111, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 5, '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': '1126893', 'n_tokens_mistral': 3972, 'n_tokens_neox': 3658, 'n_words': 824}