All files camera.js

94.62% Statements 88/93
100% Branches 34/34
76.92% Functions 10/13
95.6% Lines 87/91
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228      1x 1x     1x 1x 1x     1x   1x   1x 15x     1x 1x     1x 13x     1x 1x             15x   15x 15x 15x 15x 15x 15x 15x 15x                 15x 6x     15x 1x 1x       15x 1x   14x 1x         15x 1x       15x 1x       15x 1x       15x 1x         15x 14x 1x   13x       15x 1x     15x               15x       18x                 5x 5x 5x         15x   15x     2x         1x         1x                       15x 15x 15x   15x       22x   22x 18x     22x 15x 15x       15x   15x 15x       15x   22x       7x   7x 7x 5x 5x 5x     7x       5x 5x 5x                   1x 1x 1x         1x  
'use strict';
 
// Dependencies: Built-in
const cp = require('child_process');
const Stream = require('stream');
 
// Dependencies: Third Party
const got = require('got');
const ip = require('ip');
const MjpegConsumer = require('tessel-mjpeg-consumer');
 
// Dependencies: Internal
const CaptureStream = require('./capture-stream');
 
const priv = new WeakMap();
 
const isValidVideoDevice = device => {
  return device && /\/dev\/video\d/.test(device);
};
 
const isValidRemoteStream = url => {
  return url && url.endsWith('?action=stream');
};
 
const videoUrl = port => {
  return `http://${ip.address()}:${port}/?action=stream`;
};
 
try {
  cp.spawnSync('kill -9 $(pgrep "mjpg_streamer")');
} catch (error) {}
 
 
class Camera extends Stream {
 
  constructor(options) {
    super();
 
    let device = '/dev/video0';
    let dimensions = '800x600';
    let fps = 30;
    let port = 8080;
    let quality = 100;
    let timeout = 10000;
    let url = ''; //  use ip.address()
    let state = {
      frame: null,
      mjpg: null,
      process: null,
      remote: null,
      stream: null,
    };
 
    /* istanbul ignore else */
    if (typeof options === 'undefined') {
      options = {};
    }
 
    if (typeof options === 'string') {
      url = options;
      options = {};
    }
 
    // -r
    if (options.dimensions) {
      dimensions = options.dimensions;
    } else {
      if (options.width && options.height) {
        dimensions = `${options.width}x${options.height}`;
      }
    }
 
    // -q
    if (options.fps) {
      fps = options.fps;
    }
 
    // -q
    if (options.quality) {
      quality = options.quality;
    }
 
    // XXXX
    if (options.port) {
      port = options.port;
    }
 
    // /dev/video0|1
    if (isValidVideoDevice(options.device)) {
      device = options.device;
    }
 
    // No string url provided
    /* istanbul ignore else */
    if (!url) {
      if (options.url && isValidRemoteStream(options.url)) {
        url = options.url;
      } else {
        url = videoUrl(port);
      }
    }
 
    if (options.timeout) {
      timeout = options.timeout;
    }
 
    state.mjpg = {
      fps,
      device,
      dimensions,
      quality,
      port,
    };
 
    state.remote = {
      url,
      timeout,
      start() {
        state.process = cp.spawn('mjpg_streamer', [
          '-i',
          `/usr/lib/input_uvc.so -n -q ${quality} -r ${dimensions} -f ${fps} -d ${device} `,
          '-o',
          `/usr/lib/output_http.so -p ${port}`,
        ]);
      },
      stop() {
        /* istanbul ignore else */
        if (state.process) {
          state.process.kill('SIGTERM');
          state.process = null;
        }
      }
    };
 
    priv.set(this, state);
 
    Object.defineProperties(this, {
      dimensions: {
        get() {
          return dimensions;
        }
      },
      frame: {
        get() {
          return state.frame;
        }
      },
      url: {
        get() {
          return url;
        }
      }
    });
 
    // state.process.stdout.on("data", (buffer) => {
    //   console.log(buffer.toString());
    // });
    // state.process.stderr.on("data", (buffer) => {
    //   console.log(buffer.toString());
    // });
 
    this.highWaterMark = 0;
    this.readable = true;
    this.writable = true;
 
    this.stream();
  }
 
  stream() {
    let state = priv.get(this);
 
    if (!state.process) {
      state.remote.start();
    }
 
    if (!state.stream) {
      state.stream = got.stream(state.remote.url);
      state.stream.on('error', () => {
        state.stream = null;
        this.stream();
      });
      let incoming = state.stream.pipe(new MjpegConsumer());
 
      incoming.on('data', frame => state.frame = frame);
      incoming.on('error', () => {
        state.stream = null;
        this.stream();
      });
      incoming.pipe(this);
    }
    return this;
  }
 
  capture() {
    let cs = new CaptureStream(this);
 
    this.stream();
    this.once('data', data => {
      cs.push(data);
      cs.push(null);
      cs.on('end', () => this.stop());
    });
 
    return cs;
  }
 
  stop() {
    priv.get(this).remote.stop();
    this.emit('stop');
    return this;
  }
 
  // start() {
  //   priv.get(this).remote.start();
  //   return this;
  // }
 
  write(chunk) {
    /* istanbul ignore else */
    if (chunk) {
      this.emit('data', chunk);
      this.emit('frame', chunk);
    }
  }
}
 
module.exports = Camera;