Skip to content

Commit

Permalink
publish
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjwang committed Mar 3, 2022
1 parent 5a62b4a commit 41d0196
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
44 changes: 27 additions & 17 deletions Source/VI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,35 @@ const size_t String::size() const { return m_size; }
std::unordered_map<int, uint32_t> GlobalDeviceUsage;
std::mutex GlobalDeviceUsageMutex;

videoInput GlobalVideoInput;
std::shared_ptr<videoInput> GlobalVideoInput = nullptr;

videoInput& GetGlobalVideoInput() {
if (!GlobalVideoInput) {
GlobalVideoInput = std::make_shared<videoInput>();
}
return *GlobalVideoInput;
}

struct CameraInfo {
int deviceID;
bool bSuccess;
};

void Camera::setVerbose(bool verbose) { videoInput::setVerbose(verbose); }
void Camera::setComMultiThreaded(bool bMulti) { videoInput::setComMultiThreaded(bMulti); }

Camera::Camera(int deviceID) {
int numDevices = GlobalVideoInput.listDevices();
GlobalVideoInput.setUseCallback(true);
int numDevices = GetGlobalVideoInput().listDevices();
GetGlobalVideoInput().setUseCallback(true);
bool success = false;
if (deviceID >= 0 && deviceID < numDevices) {
std::lock_guard<std::mutex> lk(GlobalDeviceUsageMutex);
if (GlobalDeviceUsage.find(deviceID) == GlobalDeviceUsage.end()) {
success = GlobalVideoInput.setupDevice(deviceID);
success = GetGlobalVideoInput().setupDevice(deviceID);
GlobalDeviceUsage.emplace(deviceID, 0);
} else {
GlobalDeviceUsage[deviceID]++;
success = GlobalVideoInput.isDeviceSetup(deviceID);
success = GetGlobalVideoInput().isDeviceSetup(deviceID);
}
} else {
deviceID = -1;
Expand All @@ -82,17 +92,17 @@ Camera::Camera(int deviceID) {
}

Camera::Camera(int deviceID, int width, int height, int fps) {
int numDevices = GlobalVideoInput.listDevices();
GlobalVideoInput.setUseCallback(true);
int numDevices = GetGlobalVideoInput().listDevices();
GetGlobalVideoInput().setUseCallback(true);
bool success = false;
if (deviceID >= 0 && deviceID < numDevices) {
std::lock_guard<std::mutex> lk(GlobalDeviceUsageMutex);
if (GlobalDeviceUsage.find(deviceID) == GlobalDeviceUsage.end()) {
success = GlobalVideoInput.setupDevice(deviceID, width, height);
success = GetGlobalVideoInput().setupDevice(deviceID, width, height);
GlobalDeviceUsage.emplace(deviceID, 1);
} else {
GlobalDeviceUsage[deviceID]++;
success = GlobalVideoInput.isDeviceSetup(deviceID);
success = GetGlobalVideoInput().isDeviceSetup(deviceID);
}
} else {
deviceID = -1;
Expand All @@ -102,7 +112,7 @@ Camera::Camera(int deviceID, int width, int height, int fps) {
info->deviceID = deviceID;
info->bSuccess = success;
if (success && fps) {
GlobalVideoInput.setIdealFramerate(deviceID, fps);
GetGlobalVideoInput().setIdealFramerate(deviceID, fps);
}
m_handle = info;
}
Expand All @@ -117,7 +127,7 @@ Camera::~Camera() {
usage->second--;
if (usage->second == 0) {
GlobalDeviceUsage.erase(usage);
GlobalVideoInput.stopDevice(deviceID);
GetGlobalVideoInput().stopDevice(deviceID);
}
}
}
Expand Down Expand Up @@ -145,7 +155,7 @@ bool Camera::isFrameNew() {
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.isFrameNew(deviceID);
return GetGlobalVideoInput().isFrameNew(deviceID);
} else {
return false;
}
Expand All @@ -157,7 +167,7 @@ bool Camera::isDeviceSetup() {
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.isDeviceSetup(deviceID);
return GetGlobalVideoInput().isDeviceSetup(deviceID);
} else {
return false;
}
Expand All @@ -170,7 +180,7 @@ bool Camera::getPixels(unsigned char* pixels, bool rgb, bool flipX, bool flipY)
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.getPixels(deviceID, pixels, rgb, flipX, !flipY);
return GetGlobalVideoInput().getPixels(deviceID, pixels, rgb, flipX, !flipY);
} else {
return false;
}
Expand All @@ -184,7 +194,7 @@ void Camera::showSettingsWindow() {
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.showSettingsWindow(deviceID);
return GetGlobalVideoInput().showSettingsWindow(deviceID);
} else {
return;
}
Expand All @@ -197,7 +207,7 @@ int Camera::getWidth() {
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.getWidth(deviceID);
return GetGlobalVideoInput().getWidth(deviceID);
} else {
return 0;
}
Expand All @@ -209,7 +219,7 @@ int Camera::getHeight() {
}
int deviceID = ((CameraInfo*)(m_handle))->deviceID;
if (deviceID >= 0) {
return GlobalVideoInput.getHeight(deviceID);
return GetGlobalVideoInput().getHeight(deviceID);
} else {
return 0;
}
Expand Down
5 changes: 5 additions & 0 deletions Source/VI.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class VI_PORT Camera {

static LinkedList<String> getDeviceList();

#if defined(WIN32)
static void setVerbose(bool verbose);
static void setComMultiThreaded(bool bMulti);
#endif

// Tells you when a new frame has arrived - you should call this if you have
// specified setAutoReconnectOnFreeze to true
bool isFrameNew();
Expand Down

0 comments on commit 41d0196

Please sign in to comment.