ntv2plugin.cpp 1.49 KB
Newer Older
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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
//
// NTV2 framework includes ksmedia.hpp, which defines the speaker map with the same names...

#include "libvideostitch/audio.hpp"

#include "ntv2plugin.hpp"

#include "ntv2devicescanner.h"
#include "ajastuff/system/process.h"

namespace VideoStitch {

NTV2Device::NTV2Device() : deviceID(DEVICE_ID_NOTFOUND), initialized(false) {}

NTV2Device::~NTV2Device() {
  if (initialized) {
    device.ReleaseStreamForApplication(appSignature, static_cast<uint32_t>(AJAProcess::GetPid()));
    device.SetEveryFrameServices(savedTaskMode);
  }
}

NTV2Device* NTV2Device::getDevice(uint32_t deviceIndex) {
  std::unique_lock<std::mutex> lk(registryMutex);
  if (registry.find(deviceIndex) == registry.end()) {
    NTV2Device* device = new NTV2Device();
    if (AJA_FAILURE(device->init(deviceIndex))) {
      delete device;
      return nullptr;
    }
    registry[deviceIndex] = device;
  }
  return registry[deviceIndex];
}

AJAStatus NTV2Device::init(uint32_t deviceIndex) {
  // Open the device
  if (!CNTV2DeviceScanner::GetDeviceAtIndex(deviceIndex, device)) {
    return AJA_STATUS_OPEN;
  }

  if (!device.AcquireStreamForApplication(appSignature, static_cast<uint32_t>(AJAProcess::GetPid()))) {
    return AJA_STATUS_BUSY;  //  Another app is using the device
  }

  device.GetEveryFrameServices(&savedTaskMode);
  device.SetEveryFrameServices(NTV2_OEM_TASKS);
  deviceID = device.GetDeviceID();
  return AJA_STATUS_SUCCESS;
}

}  // namespace VideoStitch