From 37b7eee092c7a1eae2effff6ad2939955c38c9c8 Mon Sep 17 00:00:00 2001 From: Theodore Watson Date: Sat, 6 Jul 2013 17:09:52 -0400 Subject: [PATCH] moved verbose bool and COM multihread to .cpp added new function for setting the COM state without needing to recompile small memory leak fix. closes #4 --- .gitignore | 1 + .../src/triangleApp.cpp | 3 ++ .../libs/videoInput/videoInput.cpp | 32 ++++++++++++++++--- .../libs/videoInput/videoInput.h | 18 +++++------ 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 77390a7..e52505e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ obj/ *.obj *.tlog *.dep +*.ipch #codeblocks files */*/*.layout diff --git a/videoInputSrcAndDemos/VS-videoInputDemoWithSrc/src/triangleApp.cpp b/videoInputSrcAndDemos/VS-videoInputDemoWithSrc/src/triangleApp.cpp index 06a8ae7..a3a36fb 100644 --- a/videoInputSrcAndDemos/VS-videoInputDemoWithSrc/src/triangleApp.cpp +++ b/videoInputSrcAndDemos/VS-videoInputDemoWithSrc/src/triangleApp.cpp @@ -11,6 +11,9 @@ triangleApp::triangleApp(){ void triangleApp::init(){ + //videoInput::setVerbose(false); + //videoInput::setComMultiThreaded(true); + //optional static function to list devices //for silent listDevices use listDevices(true); int numDevices = videoInput::listDevices(); diff --git a/videoInputSrcAndDemos/libs/videoInput/videoInput.cpp b/videoInputSrcAndDemos/libs/videoInput/videoInput.cpp index b617afd..d7ff361 100755 --- a/videoInputSrcAndDemos/libs/videoInput/videoInput.cpp +++ b/videoInputSrcAndDemos/libs/videoInput/videoInput.cpp @@ -19,7 +19,6 @@ #define __IDxtJpeg_INTERFACE_DEFINED__ #define __IDxtKey_INTERFACE_DEFINED__ #include -#include #include #include @@ -75,6 +74,12 @@ EXTERN_C const CLSID CLSID_SampleGrabber; EXTERN_C const IID IID_ISampleGrabber; EXTERN_C const CLSID CLSID_NullRenderer; +//use videoInput::setVerbose to change +static bool verbose = true; + +//use videoInput::setComMultiThreaded to change +static bool VI_COM_MULTI_THREADED = false; + /////////////////////////// HANDY FUNCTIONS ///////////////////////////// void MyFreeMediaType(AM_MEDIA_TYPE& mt){ @@ -127,7 +132,7 @@ class SampleGrabberCallback : public ISampleGrabberCB{ DeleteCriticalSection(&critSection); CloseHandle(hEvent); if(bufferSetup){ - delete pixels; + delete [] pixels; } } @@ -610,6 +615,23 @@ void videoInput::setVerbose(bool _verbose){ verbose = _verbose; } +// ---------------------------------------------------------------------- +// static - new in 2013, allow for multithreaded use of VI without recompile. +// +// ---------------------------------------------------------------------- +void videoInput::setComMultiThreaded(bool bMulti){ + if( bMulti != VI_COM_MULTI_THREADED ){ + VI_COM_MULTI_THREADED = bMulti; + + //we should only need one call to comUnInit - but as its reference counting its better to be safe. + int limit = 100; + while(!comUnInit() && limit > 0){ + limit--; + } + comInit(); + } +} + // ---------------------------------------------------------------------- // change to use callback or regular capture // callback tells you when a new frame has arrived @@ -1429,11 +1451,11 @@ bool videoInput::comInit(){ // Initialize the COM library. //CoInitializeEx so videoInput can run in another thread - #ifdef VI_COM_MULTI_THREADED + if( VI_COM_MULTI_THREADED ){ hr = CoInitializeEx(NULL,COINIT_MULTITHREADED); - #else + }else{ hr = CoInitialize(NULL); - #endif + } //this is the only case where there might be a problem //if another library has started com as single threaded //and we need it multi-threaded - send warning but don't fail diff --git a/videoInputSrcAndDemos/libs/videoInput/videoInput.h b/videoInputSrcAndDemos/libs/videoInput/videoInput.h index c57f723..b14007d 100755 --- a/videoInputSrcAndDemos/libs/videoInput/videoInput.h +++ b/videoInputSrcAndDemos/libs/videoInput/videoInput.h @@ -44,6 +44,7 @@ Thanks to: #include #include #include +#include //this is for TryEnterCriticalSection #ifndef _WIN32_WINNT @@ -112,20 +113,12 @@ Thanks to: ////////////////////////////////////// VARS AND DEFS ////////////////////////////////// -//STUFF YOU CAN CHANGE - -//change for verbose debug info -static bool verbose = true; - -//if you need VI to use multi threaded com -//#define VI_COM_MULTI_THREADED - //STUFF YOU DON'T CHANGE //videoInput defines #define VI_VERSION 0.1995 #define VI_MAX_CAMERAS 20 -#define VI_NUM_TYPES 19 //DON'T TOUCH (I'm not touching you ;-) +#define VI_NUM_TYPES 19 //DON'T TOUCH #define VI_NUM_FORMATS 18 //DON'T TOUCH //defines for setPhyCon - tuner is not as well supported as composite and s-video @@ -268,10 +261,15 @@ class videoInput{ //turns off console messages - default is to print messages static void setVerbose(bool _verbose); + //this allows for multithreaded use of VI ( default is single threaded ). + //call this before any videoInput calls. + //note if your app has other COM calls then you should set VIs COM usage to match the other COM mode + static void setComMultiThreaded(bool bMulti); + //Functions in rough order they should be used. static int listDevices(bool silent = false); - static int getDeviceIDFromName(char * name); // added by gameover + static int getDeviceIDFromName(char * name); //needs to be called after listDevices - otherwise returns NULL static char * getDeviceName(int deviceID);