vendredi 8 mai 2015

Sending video captured with the computer camera from a website to a server

I am trying to have a computer or an android device record video from the camera and send it to a server that will store it. The system has to be web-based and needs to send clips of video continuously.

Here is the code in javascript:

var camera = (function() {
    var options;
    var video, canvas, context;
    var renderTimer;

    function initVideoStream() {
        video = document.createElement("video");
        video.setAttribute('width', options.width);
        video.setAttribute('height', options.height);

        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
        window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;

        if (navigator.getUserMedia) {
            navigator.getUserMedia({
                video: true
            }, function(stream) {
                options.onSuccess();

                if (video.mozSrcObject !== undefined) { // hack for Firefox < 19
                    video.mozSrcObject = stream;
                } else {
                    video.src = (window.URL && window.URL.createObjectURL(stream)) || stream;
                }

                initCanvas();
            }, options.onError);
        } else {
            options.onNotSupported();
        }
    }

    function initCanvas() {
        canvas = document.getElementById("canvas");//options.targetCanvas || document.createElement("canvas");
        canvas.setAttribute('width', options.width);
        canvas.setAttribute('height', options.height);

        context = canvas.getContext('2d');

        // mirror video
        if (options.mirror) {
            context.translate(canvas.width, 0);
            context.scale(-1, 1);
        }

        startCapture();
    }

    function startCapture() {
        video.play();

        renderTimer = setInterval(function() {
            try {
                context.drawImage(video, 0, 0, video.width, video.height);
                options.onFrame(canvas);
            } catch (e) {
                // TODO
            }
        }, Math.round(1000 / options.fps));
    }

    function stopCapture() {
        pauseCapture();

        if (video.mozSrcObject !== undefined) {
            video.mozSrcObject = null;
        } else {
            video.src = "";
        }
    }

    function pauseCapture() {
        if (renderTimer) clearInterval(renderTimer);
        video.pause();
    }

    return {
        init: function(captureOptions) {
            var doNothing = function(){};

            options = captureOptions || {};

            options.fps = options.fps || 30;
            options.width = options.width || 640;
            options.height = options.height || 480;
            options.mirror = options.mirror || false;
            options.targetCanvas = options.targetCanvas || null; // TODO: is the element actually a <canvas> ?

            initVideoStream();
        },

        start: startCapture,

        pause: pauseCapture,

        stop: stopCapture
    };
})();

var imgSender = (function() {
    function imgsFromCanvas(canvas, options) {
        var context = canvas.getContext("2d");
        var canvasWidth = canvas.width; //# pixels horizontally
        var canvasHeight = canvas.height; //# pixels vertically
        var imageData = context.getImageData(0, 0, canvasWidth, canvasHeight); //Vector of all pixels
        options.callback(canvasWidth, canvasHeight, imageData.data);
    }

    return {
        fromCanvas: function(canvas, options) {
            options = options || {};
            options.callback = options.callback || doNothing;
            return imgsFromCanvas(canvas, options);
        }
    };
})();

var FPS = 30; //fps
var minVideoTime = 5; //Seconds that every video clip sent will take
var arrayImgs = []; //Array with all the images that will be sent to the server
var videoTime = 0; //Number of seconds of video stored in videoTime
(function() {
    var capturing = false;

    camera.init({
        width: 160,
        height: 120,
        fps: FPS,
        mirror: true,

        onFrame: function(canvas) {
            imgSender.fromCanvas(canvas, {
                callback: function(w,h,image) {
                    arrayImgs.push(image);
                    videoTime += 1/FPS;
                    if (minVideoTime <= videoTime) {
                        sendToPhp(w,h,videoTime*FPS);
                        arrayImgs = [];
                        videoTime = 0;

                        function sendToPhp(width,height,numFrames) {
                            $.ajax({
                                type: "POST",
                                url: "sendToServer.php",
                                data: {
                                    arrayImgs: arrayImgs,
                                    width: w,
                                    height: h,
                                    nframes: numFrames
                                },

                                async: true, // If set to non-async, browser shows page as "Loading.."
                                cache: false,
                                timeout:500, // Timeout in ms (0.5s)

                                success: function(answer){
                                    console.log("SUCCESS: ", answer);
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown){
                                    console.log("ERROR: ", textStatus, " , ", errorThrown);                             }
                            });
                        }
                    }
                }
            });
        },

        onSuccess: function() {
            document.getElementById("info").style.display = "none";

            capturing = true;
            document.getElementById("pause").style.display = "block";
            document.getElementById("pause").onclick = function() {
                if (capturing) {
                    camera.pause();
                } else {
                    camera.start();
                }
                capturing = !capturing;
            };
        },

        onError: function(error) {
            // TODO: log error
        },

        onNotSupported: function() {
            document.getElementById("info").style.display = "none";
            document.getElementById("notSupported").style.display = "block";
        }
    });
})();

As you can see, I am using getUserMedia to get the video, and I am sampling the images in an array until I have 5 seconds of video, then I send that to a php file that will send it to the server.

My problem: there is A LOT of lag in all the ajax requests, and I also have size problems, the ajax requests don't accept so much data at the same time, so I have to reduce the fps and the length of the videos sent.

Is there any better way to do this? By now, I lose more than 50% of the video! Maybe through WebRTC? Help please

Thank you!

Aucun commentaire:

Enregistrer un commentaire