livecam.js
function GstLaunch() {

  const gst_launch_executable = 'gst-launch-1.0';
  const gst_launch_versionarg = '--version';

  const SpawnSync = require('child_process').spawnSync;
  const Spawn = require('child_process').spawn;
  const Assert = require('assert');
  const Path = require('path');
  const OS = require('os');
  const FS = require('fs');

  /**
   * @fn getPath
   * @brief Returns path to gst-launch or undefined on error
   */
  var getPath = function() {
    var detected_path = undefined;

    if (OS.platform() == 'win32') {
      // On Windows, GStreamer MSI installer defines the following
      // environment variables.
      const detected_path_x64 = process.env.GSTREAMER_1_0_ROOT_X86_64;
      const detected_path_x32 = process.env.GSTREAMER_1_0_ROOT_X86;
      if (detected_path_x64 || detected_path_x32) {
        // If both variables are present, favor the architecture
        // of GStreamer which is the same as Node.js runtime.
        if (detected_path_x64 && detected_path_x32) {
          if (process.arch == 'x64')
            detected_path = detected_path_x64;
          else if (process.arch == 'x32')
            detected_path = detected_path_x32;
        } else {
          detected_path = detected_path_x64 || detected_path_x32;
        }
      }

      if (detected_path) {
        detected_path = Path.join(
          detected_path,
          'bin',
          (gst_launch_executable + '.exe'));
        try {
          FS.accessSync(detected_path, FS.F_OK);
        } catch (e) {
          detected_path = undefined;
        }
      } else {
        // Look for GStreamer on PATH
        var path_dirs = process.env.PATH.split(';');
        for (var index = 0; index < path_dirs.length; ++index) {
          try {
            var base = Path.normalize(path_dirs[index]);
            var bin = Path.join(
              base,
              (gst_launch_executable + '.exe'));
            FS.accessSync(bin, FS.F_OK);
            detected_path = bin;
          } catch (e) { /* no-op */ }
        }
      }
    } else if (OS.platform() == 'linux') {
      // Look for GStreamer on PATH
      var path_dirs = process.env.PATH.split(':');
      for (var index = 0; index < path_dirs.length; ++index) {
        try {
          var base = Path.normalize(path_dirs[index]);
          var bin = Path.join(
            base,
            gst_launch_executable);
          FS.accessSync(bin, FS.F_OK);
          detected_path = bin;
        } catch (e) { /* no-op */ }
      }
    } else if (OS.platform() == 'darwin') {
      try {
        var bin = '/usr/local/bin/gst-launch-1.0'
        FS.accessSync(bin, FS.F_OK);
        detected_path = bin;
      } catch (e) { /* no-op */ }
    }

    return detected_path;
  }

  /**
   * @fn getVersion
   * @brief Returns version string of GStreamer on this machine by
   * invoking the gst-launch executable or 'undefined' on failure.
   */
  var getVersion = function() {
    var version_str = undefined;
    try {
      var gst_launch_path = getPath();
      Assert.ok(typeof(gst_launch_path), 'string');

      var output = SpawnSync(
          gst_launch_path, [gst_launch_versionarg], {
            'timeout': 1000
          })
        .stdout;

      if (output && output.toString().includes('GStreamer')) {
        version_str = output
          .toString()
          .match(/GStreamer\s+.+/g)[0]
          .replace(/GStreamer\s+/, '');
      }
    } catch (ex) {
      version_str = undefined;
    }

    return version_str;
  }

  /*!
   * @fn isAvailable
   * @brief Answers true if gst-launch executable is available
   */
  var isAvailable = function() {
    return getVersion() != undefined;
  }

  /*!
   * @fn spawnPipeline
   * @brief Spawns a GStreamer pipeline using gst-launch
   * @return A Node <child-process> of the launched pipeline
   * @see To construct a correct pipeline arg, consult the link below:
   * https://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-programs.html
   * @usage spawnPipeline('videotestsrc ! autovideosink')
   */
  var spawnPipeline = function(pipeline) {
    Assert.ok(typeof(pipeline), 'string');
    Assert.ok(isAvailable(), "gst-launch is not available.");

    var gst_launch_path = getPath();
    Assert.ok(typeof(gst_launch_path), 'string');

    return Spawn(gst_launch_path, pipeline.split(' '));
  }

  return {
    'getPath': getPath,
    'getVersion': getVersion,
    'isAvailable': isAvailable,
    'spawnPipeline': spawnPipeline
  }

}
function GstLaunch() {

  const gst_launch_executable = 'gst-launch-1.0';
  const gst_launch_versionarg = '--version';

  const SpawnSync = require('child_process').spawnSync;
  const Spawn = require('child_process').spawn;
  const Assert = require('assert');
  const Path = require('path');
  const OS = require('os');
  const FS = require('fs');

  /**
   * @fn getPath
   * @brief Returns path to gst-launch or undefined on error
   */
  var getPath = function() {
    var detected_path = undefined;

    if (OS.platform() == 'win32') {
      // On Windows, GStreamer MSI installer defines the following
      // environment variables.
      const detected_path_x64 = process.env.GSTREAMER_1_0_ROOT_X86_64;
      const detected_path_x32 = process.env.GSTREAMER_1_0_ROOT_X86;
      if (detected_path_x64 || detected_path_x32) {
        // If both variables are present, favor the architecture
        // of GStreamer which is the same as Node.js runtime.
        if (detected_path_x64 && detected_path_x32) {
          if (process.arch == 'x64')
            detected_path = detected_path_x64;
          else if (process.arch == 'x32')
            detected_path = detected_path_x32;
        } else {
          detected_path = detected_path_x64 || detected_path_x32;
        }
      }

      if (detected_path) {
        detected_path = Path.join(
          detected_path,
          'bin',
          (gst_launch_executable + '.exe'));
        try {
          FS.accessSync(detected_path, FS.F_OK);
        } catch (e) {
          detected_path = undefined;
        }
      } else {
        // Look for GStreamer on PATH
        var path_dirs = process.env.PATH.split(';');
        for (var index = 0; index < path_dirs.length; ++index) {
          try {
            var base = Path.normalize(path_dirs[index]);
            var bin = Path.join(
              base,
              (gst_launch_executable + '.exe'));
            FS.accessSync(bin, FS.F_OK);
            detected_path = bin;
          } catch (e) { /* no-op */ }
        }
      }
    } else if (OS.platform() == 'linux') {
      // Look for GStreamer on PATH
      var path_dirs = process.env.PATH.split(':');
      for (var index = 0; index < path_dirs.length; ++index) {
        try {
          var base = Path.normalize(path_dirs[index]);
          var bin = Path.join(
            base,
            gst_launch_executable);
          FS.accessSync(bin, FS.F_OK);
          detected_path = bin;
        } catch (e) { /* no-op */ }
      }
    } else if (OS.platform() == 'darwin') {
      try {
        var bin = '/usr/local/bin/gst-launch-1.0'
        FS.accessSync(bin, FS.F_OK);
        detected_path = bin;
      } catch (e) { /* no-op */ }
    }

    return detected_path;
  }

  /**
   * @fn getVersion
   * @brief Returns version string of GStreamer on this machine by
   * invoking the gst-launch executable or 'undefined' on failure.
   */
  var getVersion = function() {
    var version_str = undefined;
    try {
      var gst_launch_path = getPath();
      Assert.ok(typeof(gst_launch_path), 'string');

      var output = SpawnSync(
          gst_launch_path, [gst_launch_versionarg], {
            'timeout': 1000
          })
        .stdout;

      if (output && output.toString().includes('GStreamer')) {
        version_str = output
          .toString()
          .match(/GStreamer\s+.+/g)[0]
          .replace(/GStreamer\s+/, '');
      }
    } catch (ex) {
      version_str = undefined;
    }

    return version_str;
  }

  /*!
   * @fn isAvailable
   * @brief Answers true if gst-launch executable is available
   */
  var isAvailable = function() {
    return getVersion() != undefined;
  }

  /*!
   * @fn spawnPipeline
   * @brief Spawns a GStreamer pipeline using gst-launch
   * @return A Node <child-process> of the launched pipeline
   * @see To construct a correct pipeline arg, consult the link below:
   * https://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-programs.html
   * @usage spawnPipeline('videotestsrc ! autovideosink')
   */
  var spawnPipeline = function(pipeline) {
    Assert.ok(typeof(pipeline), 'string');
    Assert.ok(isAvailable(), "gst-launch is not available.");

    var gst_launch_path = getPath();
    Assert.ok(typeof(gst_launch_path), 'string');

    return Spawn(gst_launch_path, pipeline.split(' '));
  }

  return {
    'getPath': getPath,
    'getVersion': getVersion,
    'isAvailable': isAvailable,
    'spawnPipeline': spawnPipeline
  }

}
function GstLaunch ( ) { const gst_launch_executable = 'gst-launch-1.0' ; const gst_launch_versionarg = '--version' ; const SpawnSync = require ( 'child_process' ) . spawnSync ; const Spawn = require ( 'child_process' ) . spawn ; const Assert = require ( 'assert' ) ; const Path = require ( 'path' ) ; const OS = require ( 'os' ) ; const FS = require ( 'fs' ) ; /**
   * @fn getPath
   * @brief Returns path to gst-launch or undefined on error
   */ var getPath = function ( ) { var detected_path = undefined ; if ( OS . platform ( ) == 'win32' ) { // On Windows, GStreamer MSI installer defines the following // environment variables. const detected_path_x64 = process . env . GSTREAMER_1_0_ROOT_X86_64 ; const detected_path_x32 = process . env . GSTREAMER_1_0_ROOT_X86 ; if ( detected_path_x64 || detected_path_x32 ) { // If both variables are present, favor the architecture // of GStreamer which is the same as Node.js runtime. if ( detected_path_x64 && detected_path_x32 ) { if ( process . arch == 'x64' ) detected_path = detected_path_x64 ; else if ( process . arch == 'x32' ) detected_path = detected_path_x32 ; } else { detected_path = detected_path_x64 || detected_path_x32 ; } } if ( detected_path ) { detected_path = Path . join ( detected_path , 'bin' , ( gst_launch_executable + '.exe' ) ) ; try { FS . accessSync ( detected_path , FS . F_OK ) ; } catch ( e ) { detected_path = undefined ; } } else { // Look for GStreamer on PATH var path_dirs = process . env . PATH . split ( ';' ) ; for ( var index = 0 ; index < path_dirs . length ; ++ index ) { try { var base = Path . normalize ( path_dirs [ index ] ) ; var bin = Path . join ( base , ( gst_launch_executable + '.exe' ) ) ; FS . accessSync ( bin , FS . F_OK ) ; detected_path = bin ; } catch ( e ) { /* no-op */ } } } } else if ( OS . platform ( ) == 'linux' ) { // Look for GStreamer on PATH var path_dirs = process . env . PATH . split ( ':' ) ; for ( var index = 0 ; index < path_dirs . length ; ++ index ) { try { var base = Path . normalize ( path_dirs [ index ] ) ; var bin = Path . join ( base , gst_launch_executable ) ; FS . accessSync ( bin , FS . F_OK ) ; detected_path = bin ; } catch ( e ) { /* no-op */ } } } else if ( OS . platform ( ) == 'darwin' ) { try { var bin = '/usr/local/bin/gst-launch-1.0' FS . accessSync ( bin , FS . F_OK ) ; detected_path = bin ; } catch ( e ) { /* no-op */ } } return detected_path ; } /**
   * @fn getVersion
   * @brief Returns version string of GStreamer on this machine by
   * invoking the gst-launch executable or 'undefined' on failure.
   */ var getVersion = function ( ) { var version_str = undefined ; try { var gst_launch_path = getPath ( ) ; Assert . ok ( typeof ( gst_launch_path ) , 'string' ) ; var output = SpawnSync ( gst_launch_path , [ gst_launch_versionarg ] , { 'timeout' : 1000 } ) . stdout ; if ( output && output . toString ( ) . includes ( 'GStreamer' ) ) { version_str = output . toString ( ) . match ( / GStreamer\s+.+ / g ) [ 0 ] . replace ( / GStreamer\s+ / , '' ) ; } } catch ( ex ) { version_str = undefined ; } return version_str ; } /*!
   * @fn isAvailable
   * @brief Answers true if gst-launch executable is available
   */ var isAvailable = function ( ) { return getVersion ( ) != undefined ; } /*!
   * @fn spawnPipeline
   * @brief Spawns a GStreamer pipeline using gst-launch
   * @return A Node <child-process> of the launched pipeline
   * @see To construct a correct pipeline arg, consult the link below:
   * https://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-programs.html
   * @usage spawnPipeline('videotestsrc ! autovideosink')
   */ var spawnPipeline = function ( pipeline ) { Assert . ok ( typeof ( pipeline ) , 'string' ) ; Assert . ok ( isAvailable ( ) , "gst-launch is not available." ) ; var gst_launch_path = getPath ( ) ; Assert . ok ( typeof ( gst_launch_path ) , 'string' ) ; return Spawn ( gst_launch_path , pipeline . split ( ' ' ) ) ; } return { 'getPath' : getPath , 'getVersion' : getVersion , 'isAvailable' : isAvailable , 'spawnPipeline' : spawnPipeline } }
/*!
@class GstLaunch
@brief Class that encapsulates "gst-launch" executable.
@note Refer to README.md to see how to install GStreamer
/ * !