livecam.js
function GstLiveCamServer(config) {

  const Assert = require('assert');
  const OS = require('os');

  Assert.ok(['win32', 'linux', 'darwin'].indexOf(OS.platform()) > -1,
    "livecam module supports Windows, Linux, and Mac OSX for broadcasting.");

  config = config || {};
  Assert.ok(typeof(config), 'object');

  const fake = config.fake || false;
  const width = config.width || 800;
  const height = config.height || 600;
  const framerate = config.framerate || 30;
  const grayscale = config.grayscale || false;
  const deviceIndex = config.deviceIndex || -1;

  Assert.ok(typeof(fake), 'boolean');
  Assert.ok(typeof(width), 'number');
  Assert.ok(typeof(height), 'number');
  Assert.ok(typeof(framerate), 'number');
  Assert.ok(typeof(grayscale), 'boolean');

  var gst_multipart_boundary = '--videoboundary';
  var gst_video_src = '';

  if (!fake) {
    if (OS.platform() == 'win32')
      gst_video_src = 'ksvideosrc device-index=' + deviceIndex + ' ! decodebin';
    else if (OS.platform() == 'linux')
      gst_video_src = 'v4l2src ! decodebin';
    else if (OS.platform() == 'darwin')
      gst_video_src = 'avfvideosrc device-index=' + deviceIndex;
    else
      Assert.ok(false, 'unsupported platform')
  } else {
    gst_video_src = 'videotestsrc';
  }

  if (width > 0 || height > 0) {
    gst_video_src += ' ! videoscale ! video/x-raw,width=' + parseInt(width) + ',height=' + parseInt(height);
  }

  if (framerate > 0) {
    gst_video_src += ' ! videorate ! video/x-raw,framerate=' + parseInt(framerate) + '/1';
  }

  if (grayscale) {
    gst_video_src += ' ! videobalance saturation=0.0 ! videoconvert';
  }

  /*!
   * @fn start
   * @brief Starts a GStreamer pipeline that broadcasts the default
   * webcam over the given TCP address and port.
   * @return A Node <child-process> of the launched pipeline
   */
  var start = function(tcp_addr, tcp_port) {
    Assert.ok(typeof(tcp_addr), 'string');
    Assert.ok(typeof(tcp_port), 'number');

    const cam_pipeline = gst_video_src + ' ! jpegenc ! multipartmux  boundary="' +
      gst_multipart_boundary + '" ! tcpserversink host=' + tcp_addr + ' port=' + tcp_port;

    var gst_launch = new GstLaunch();

    if (gst_launch.isAvailable()) {
      console.log('GstLaunch found: ' + gst_launch.getPath());
      console.log('GStreamer version: ' + gst_launch.getVersion());
      console.log('GStreamer pipeline: ' + cam_pipeline);

      return gst_launch.spawnPipeline(cam_pipeline);
    } else {
      throw new Error('GstLaunch not found.');
    }
  }

  return {
    'start': start
  }

}
function GstLiveCamServer(config) {

  const Assert = require('assert');
  const OS = require('os');

  Assert.ok(['win32', 'linux', 'darwin'].indexOf(OS.platform()) > -1,
    "livecam module supports Windows, Linux, and Mac OSX for broadcasting.");

  config = config || {};
  Assert.ok(typeof(config), 'object');

  const fake = config.fake || false;
  const width = config.width || 800;
  const height = config.height || 600;
  const framerate = config.framerate || 30;
  const grayscale = config.grayscale || false;
  const deviceIndex = config.deviceIndex || -1;

  Assert.ok(typeof(fake), 'boolean');
  Assert.ok(typeof(width), 'number');
  Assert.ok(typeof(height), 'number');
  Assert.ok(typeof(framerate), 'number');
  Assert.ok(typeof(grayscale), 'boolean');

  var gst_multipart_boundary = '--videoboundary';
  var gst_video_src = '';

  if (!fake) {
    if (OS.platform() == 'win32')
      gst_video_src = 'ksvideosrc device-index=' + deviceIndex + ' ! decodebin';
    else if (OS.platform() == 'linux')
      gst_video_src = 'v4l2src ! decodebin';
    else if (OS.platform() == 'darwin')
      gst_video_src = 'avfvideosrc device-index=' + deviceIndex;
    else
      Assert.ok(false, 'unsupported platform')
  } else {
    gst_video_src = 'videotestsrc';
  }

  if (width > 0 || height > 0) {
    gst_video_src += ' ! videoscale ! video/x-raw,width=' + parseInt(width) + ',height=' + parseInt(height);
  }

  if (framerate > 0) {
    gst_video_src += ' ! videorate ! video/x-raw,framerate=' + parseInt(framerate) + '/1';
  }

  if (grayscale) {
    gst_video_src += ' ! videobalance saturation=0.0 ! videoconvert';
  }

  /*!
   * @fn start
   * @brief Starts a GStreamer pipeline that broadcasts the default
   * webcam over the given TCP address and port.
   * @return A Node <child-process> of the launched pipeline
   */
  var start = function(tcp_addr, tcp_port) {
    Assert.ok(typeof(tcp_addr), 'string');
    Assert.ok(typeof(tcp_port), 'number');

    const cam_pipeline = gst_video_src + ' ! jpegenc ! multipartmux  boundary="' +
      gst_multipart_boundary + '" ! tcpserversink host=' + tcp_addr + ' port=' + tcp_port;

    var gst_launch = new GstLaunch();

    if (gst_launch.isAvailable()) {
      console.log('GstLaunch found: ' + gst_launch.getPath());
      console.log('GStreamer version: ' + gst_launch.getVersion());
      console.log('GStreamer pipeline: ' + cam_pipeline);

      return gst_launch.spawnPipeline(cam_pipeline);
    } else {
      throw new Error('GstLaunch not found.');
    }
  }

  return {
    'start': start
  }

}
/*!
@class GstLiveCamServer
@brief Encapsulates a GStreamer pipeline to broadcast default webcam.