stream-labs / obs-studio-node

libOBS (OBS Studio) for Node.Js, Electron and similar tools
GNU General Public License v2.0
614 stars 99 forks source link

Question: Is obs-studio-node actually communiating with a running OBS instance? #1379

Closed nospam2k closed 9 months ago

nospam2k commented 10 months ago

I sorry for my question, but I can't seem to understand from playing with the code. I setup an example to get an audio device and the windows audio device was returned. My confusion is I renamed the OBS directory and it still worked. I'm not sure where initResult is coming from. Whatever it's doing, it seems it is not actually using OBS. Is it just working with config files? What is osn.NodeObs.OBS_API_initAPI actually doing? My end goal that led me here is I'm wanting to figure out how to listen for a change in the gain filter of an audio device by an end user. Here is my code:

const path = require('path');
const { Subject } = require('rxjs');
const { first } = require('rxjs/operators');
const osn = require("obs-studio-node");
const { v4: uuid } = require('uuid');

console.log('Starting app');

function initOBS() {
  console.debug('Initializing OBS...');

  osn.NodeObs.IPC.host('gain-controller');
  osn.NodeObs.SetWorkingDirectory(path.join(__dirname, 'node_modules', 'obs-studio-node'));
  const obsDataPath = path.join(__dirname, 'osn-data');
  const initResult = osn.NodeObs.OBS_API_initAPI('en-US', obsDataPath, '1.0.0');

  if (initResult !== 0) {
    const errorReasons = {
      '-2': 'DirectX could not be found on your system. Please install the latest version of DirectX for your machine here <https://www.microsoft.com/en-us/download/details.aspx?id=35?> and try again.',
      '-5': 'Failed to initialize OBS. Your video drivers may be out of date, or Streamlabs OBS may not be supported on your system.',
    }

    const errorMessage = errorReasons[initResult.toString()] ||
        `An unknown error #${initResult} was encountered while initializing OBS.`;

    console.error('OBS init failure', errorMessage);

    shutdown();

    throw Error(errorMessage);
  }

  console.debug('OBS initialized');
  console.log(getAudioDevices('wasapi_output_capture', 'desktop-audio'));
}

function getAudioDevices(type, subtype) {
  const dummyDevice = osn.InputFactory.create(type, subtype, { device_id: 'does_not_exist' });
  const devices = dummyDevice.properties.get('device_id').details.items.map(({ name, value }) => {
    return { device_id: value, name,};
  });
  dummyDevice.release();
  return devices;
};

function shutdown() {
  if (!obsInitialized) {
    console.debug('OBS is already shut down!');
    return false;
  }

  console.debug('Shutting down OBS...');

  try {
    osn.NodeObs.OBS_service_removeCallback();
    osn.NodeObs.IPC.disconnect();
    obsInitialized = false;
  } catch(e) {
    throw Error('Exception when shutting down OBS process' + e);
  }

  console.debug('OBS shutdown successfully');

  return true;
}

initOBS();
summeroff commented 9 months ago

You're correct; this package doesn't interact with the OBS Studio app. Instead this package includes a custom-built obs64.exe, which runs in the background without an UI( as UI part be done in Electron). The line osn.NodeObs.IPC.host('gain-controller'); launches this background process. Then, the osn.NodeObs.OBS_API_initAPI call sets up the necessary communication between the Node module and the obs64.exe process.

nospam2k commented 9 months ago

ah ok.