All files / lib video.js

94.92% Statements 56/59
88.89% Branches 16/18
100% Functions 6/6
94.64% Lines 53/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                              1x   1x 1x 1x 1x   1x 15x   15x 15x   15x 15x   15x 15x 15x                           1x 7x 7x 7x 7x 7x 7x 7x 7x   7x   3x 2x 2x                       4x   1x   1x     1x                     3x   2x 2x 2x                 1x                     1x 5x 4x 1x   3x   3x               1x 6x   5x 5x 3x 3x 3x   5x 5x 3x       1x  
"use strict";
/**
 * Creates a new instance of VideoRecorder.
 *
 * @class
 * @name VideoRecorder
 * @classdesc contains methods to record video for tests
 * @arg {object} [opts] - Options.
 * @arg {object} [opts._process] - Injected `process` module.
 * @arg {object} [opts._which] - Injected `which` module.
 * @arg {object} [opts._spawn] - Injected `spawn` module.
 * @property {boolean} [isRunning=false] - flag is video recorder running or no
 * @property {?string} [filePath=null] - path video file
 */
 
var path = require("path");
 
var spawn = require("cross-spawn");
var which = require("which");
var U = require("glace-utils");
var LOG = U.logger;
 
var VideoRecorder = function (opts) {
    opts = U.defVal(opts, {});
 
    this.isRunning = false;
    this.filePath = null;
 
    this._process = null;
    this._recordOpts = null;
 
    this.__process = U.defVal(opts._process, process);
    this.__which = U.defVal(opts._which, which);
    this.__spawn = U.defVal(opts._spawn, spawn);
};
/**
 * Configures video recorder.
 *
 * @method
 * @arg {object} [opts] - recorder configuration
 * @arg {string} [opts.path] - video file path
 * @arg {number} [opts.fps=30] - video framerate
 * @arg {number} [opts.width=1024] - video width
 * @arg {number} [opts.height=768] - video height
 * @arg {number} [opts.x=0] - `X`-offset on display
 * @arg {number} [opts.y=0] - `Y`-offset on display
 */
VideoRecorder.prototype.configure = function (opts) {
    opts = U.defVal(opts, {});
    opts.fps = U.defVal(opts.fps, 30);
    opts.width = U.defVal(opts.width, 1024);
    opts.height = U.defVal(opts.height, 768);
    opts.x = U.defVal(opts.x, 0);
    opts.y = U.defVal(opts.y, 0);
    opts.size = opts.width + "x" + opts.height;
    this.filePath = U.defVal(opts.path, path.resolve(U.cwd, "out.avi"));
 
    if (this.__process.platform === "win32") {
 
        this.__which.sync("ffmpeg");
        this._recordCmd = "ffmpeg";
        this._recordOpts = [ "-y",
            "-loglevel", "quiet",
            "-video_size", opts.size,
            "-offset_x", opts.x,
            "-offset_y", opts.y,
            "-draw_mouse", 0,
            "-framerate", opts.fps,
            "-f", "gdigrab",
            "-i", "desktop",
            "-vcodec", "libx264",
            this.filePath ];
 
    } else if (this.__process.platform === "linux") {
 
        Iif (this.__which.sync("avconv", { nothrow: true })) {
            this._recordCmd = "avconv";
        } else Iif (this.__which.sync("ffmpeg", { nothrow: true })) {
            this._recordCmd = "ffmpeg";
        } else {
            throw new Error("not found: avconv or ffmpeg");
        };
        this._recordOpts = [ "-y",
            "-loglevel", "quiet",
            "-f", "x11grab",
            "-r", opts.fps,
            "-s", opts.size,
            "-i", `${process.env.DISPLAY}+${opts.x},${opts.y}`,
            "-codec", "libx264",
            this.filePath ];
 
    } else if (this.__process.platform === "darwin") {
 
        this.__which.sync("ffmpeg");
        this._recordCmd = "ffmpeg";
        this._recordOpts = [ "-y",
            "-loglevel", "quiet",
            "-f", "avfoundation",
            "-r", opts.fps,
            "-i", "1:",
            "-vsync", 2,
            this.filePath ];
 
    } else {
        throw new Error("Video capture isn't supported " +
                        `on platform '${this.__process.platform}'`);
    };
};
/**
 * Starts video recorder.
 *
 * @method
 * @throws {Error} if video recorder is started already
 * @throws {Error} if video recorder isn't configured yet
 */
VideoRecorder.prototype.start = function () {
    if (this.isRunning) return;
    if (!this._recordOpts)
        throw new Error("Video recorder isn't configured yet");
 
    this._process = this.__spawn(this._recordCmd, this._recordOpts,
        { killSignal: "SIGINT" });
    this.isRunning = true;
};
/**
 * Stops video recorder.
 *
 * @method
 * @throws {Error} if video recorder isn't started yet
 */
VideoRecorder.prototype.stop = function () {
    if (!this.isRunning) return;
 
    return new Promise((resolve, reject) => {
        this._process.once("exit", (code, signal) => {
            LOG.debug(`${this._recordCmd} was stopped with code ${code} and signal ${signal}`);
            this.isRunning = false;
            resolve();
        });
        this._process.once("error", reject);
        var result = this._process.kill("SIGINT");
        if (!result) reject(new Error(`Oops! Can't kill ${this._recordCmd}`));
    });
};
 
module.exports = VideoRecorder;