Skip to content

Commit

Permalink
Added USB hotplug callback to get more events related to USB device c…
Browse files Browse the repository at this point in the history
…onnectivity, WiP #73
  • Loading branch information
vmdocua committed Feb 9, 2024
1 parent d7e9a3c commit 7004a74
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
32 changes: 31 additions & 1 deletion Capture/capturelib/include/CaptureApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CAPTURE_CAPTUREAPP_H

#include "CaptureLib.h"
#include "CaptureThreading.h"
#include "yaml-cpp/yaml.h"

namespace reprostim {
Expand Down Expand Up @@ -42,7 +43,17 @@ namespace reprostim {


class CaptureApp {
private:
_DECLARE_CLASS_WITH_SYNC();

std::set<std::string> m_disconnDevs;

inline void disconnDevAdd(const std::string& devPath);
inline bool disconnDevContains(const std::string& devPath) const;
inline void disconnDevRemove(const std::string& devPath);

protected:

// setup data
std::string appName;
bool audioEnabled;
Expand All @@ -62,21 +73,40 @@ namespace reprostim {
MWCAP_VIDEO_SIGNAL_STATUS vssPrev; // previous video signal status
VideoDevice targetDev;
std::string targetBusInfo;
std::string targetMwDevPath;
std::string targetVideoDevPath;
std::string targetAudioDevPath;


static void usbHotplugCallback(MWUSBHOT_PLUG_EVETN event, const char *pszDevicePath, void* pParam);

public:
CaptureApp();
virtual bool loadConfig(AppConfig& cfg, const std::string& pathConfig);
virtual void onCaptureStart();
virtual void onCaptureStop(const std::string& message);
virtual bool onLoadConfig(AppConfig& cfg, const std::string& pathConfig, YAML::Node doc);
virtual void onUsbDevArrived(const std::string& devPath);
virtual void onUsbDevLeft(const std::string& devPath);
virtual int parseOpts(AppOpts& opts, int argc, char* argv[]);
void printVersion();
int run(int argc, char* argv[]);
};

// inline methods
inline void CaptureApp::disconnDevAdd(const std::string& devPath) {
_SYNC();
m_disconnDevs.insert(devPath);
}

inline bool CaptureApp::disconnDevContains(const std::string& devPath) const {
_SYNC();
return m_disconnDevs.find(devPath)!=m_disconnDevs.end();
}

inline void CaptureApp::disconnDevRemove(const std::string& devPath) {
_SYNC();
m_disconnDevs.erase(devPath);
}

}
#endif //CAPTURE_CAPTUREAPP_H
17 changes: 17 additions & 0 deletions Capture/capturelib/include/CaptureThreading.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
#include <iostream>
#include <atomic>
#include <thread>
#include <mutex>
#include "CaptureLib.h"

//////////////////////////////////////////////////////////////////////////
// Macros

#ifndef _SYNC_LOCK
#define _SYNC_LOCK(mutex_) std::lock_guard<std::mutex> _sync_lock(mutex_)
#endif //_SYNC_LOCK

#ifndef _DECLARE_CLASS_WITH_SYNC
#define _DECLARE_CLASS_WITH_SYNC() mutable std::mutex _this_mutex_
#endif //_DECLARE_CLASS_WITH_SYNC

#ifndef _SYNC
#define _SYNC() _SYNC_LOCK(_this_mutex_)
#endif //_SYNC


namespace reprostim {

//////////////////////////////////////////////////////////////////////////
Expand Down
58 changes: 55 additions & 3 deletions Capture/capturelib/src/CaptureApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ namespace reprostim {
return true;
}

void CaptureApp::onUsbDevArrived(const std::string& devPath) {
_INFO("Connected USB device: " << devPath);
disconnDevRemove(devPath);
}

void CaptureApp::onUsbDevLeft(const std::string& devPath) {
_INFO("Disconnected USB device: " << devPath);
disconnDevAdd(devPath);
}

int CaptureApp::parseOpts(AppOpts& opts, int argc, char* argv[]) {
_INFO("TODO: parseOpts");
return EX_OK;
Expand Down Expand Up @@ -167,8 +177,22 @@ namespace reprostim {

_VERBOSE("MWCapture SDK version: " << mwcSdkVersion());

// register USB hotplug callback if any
bool hasHotplug = true;
if (MWUSBRegisterHotPlug(CaptureApp::usbHotplugCallback, this) != MW_SUCCEEDED) {
_ERROR("Failed register USB device hot plug callback");
hasHotplug = false;
}

do {
SLEEP_SEC(1);

if( !targetMwDevPath.empty() && disconnDevContains(targetMwDevPath) ) {
onCaptureStop("Target USB device instance " + targetMwDevPath + " disconnected");
targetMwDevPath = "";
continue;
}

HCHANNEL hChannel = NULL;
if( !findTargetVideoDevice(opts.verbose,
cfg.has_device_serial_number?cfg.device_serial_number:"",
Expand All @@ -186,8 +210,13 @@ namespace reprostim {
_VERBOSE("Found target device: " << vdToString(targetDev));

char wPath[256] = {0};
mr = MWGetDevicePath(targetDev.channelIndex, wPath);
_VERBOSE("Device path: " << wPath);
if( MWGetDevicePath(targetDev.channelIndex, wPath)==MW_SUCCEEDED ) {
targetMwDevPath = wPath;
_VERBOSE("Magewell device instance path: " << wPath);
} else {
_ERROR("ERROR[006]: Failed MWGetDevicePath");
targetMwDevPath = "";
}

// TODO: check res
hChannel = MWOpenChannelByPath(wPath);
Expand Down Expand Up @@ -222,8 +251,11 @@ namespace reprostim {
if( !cfg.ffm_opts.has_v_dev || !cfg.has_device_serial_number ) {
_INFO(" <> Found Video Device ===> "
<< targetVideoDevPath << ", S/N: " << targetDev.serial
<< ", busInfo: " << targetBusInfo
<< ", " << targetDev.name);
_INFO(" <> "
<< "USB bus info : " << targetBusInfo);
_INFO(" <> "
<< "Instance device path : " << targetMwDevPath);
}

if( audioEnabled ) {
Expand Down Expand Up @@ -268,6 +300,11 @@ namespace reprostim {

onCaptureStop("Program terminated");

if( hasHotplug ) {
MWUSBUnRegisterHotPlug();
hasHotplug = false;
}

if( fInit )
MWCaptureExitInstance();

Expand All @@ -280,4 +317,19 @@ namespace reprostim {
return EX_OK;
}

void CaptureApp::usbHotplugCallback(MWUSBHOT_PLUG_EVETN event, const char *pszDevicePath, void* pParam) {
if( pParam==NULL ) return;
CaptureApp* pApp = reinterpret_cast<CaptureApp*>(pParam);
bool verbose = pApp->verbose;
switch(event) {
case USBHOT_PLUG_EVENT_DEVICE_ARRIVED:
pApp->onUsbDevArrived(pszDevicePath);
break;
case USBHOT_PLUG_EVENT_DEVICE_LEFT:
pApp->onUsbDevLeft(pszDevicePath);
break;
default:
_VERBOSE("Unknown USB hotplug event: " << event << ", " << pszDevicePath);
}
}
}
2 changes: 1 addition & 1 deletion Capture/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0.32
1.1.0.36

0 comments on commit 7004a74

Please sign in to comment.