Skip to content

Commit

Permalink
moved verbose bool and COM multihread to .cpp added new function for …
Browse files Browse the repository at this point in the history
…setting the COM state without needing to recompile

small memory leak fix. closes ofTheo#4
  • Loading branch information
ofTheo committed Jul 6, 2013
1 parent 332a254 commit 37b7eee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ obj/
*.obj
*.tlog
*.dep
*.ipch

#codeblocks files
*/*/*.layout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 27 additions & 5 deletions videoInputSrcAndDemos/libs/videoInput/videoInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#define __IDxtJpeg_INTERFACE_DEFINED__
#define __IDxtKey_INTERFACE_DEFINED__
#include <uuids.h>
#include <vector>
#include <Aviriff.h>
#include <Windows.h>

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -127,7 +132,7 @@ class SampleGrabberCallback : public ISampleGrabberCB{
DeleteCriticalSection(&critSection);
CloseHandle(hEvent);
if(bufferSetup){
delete pixels;
delete [] pixels;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions videoInputSrcAndDemos/libs/videoInput/videoInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Thanks to:
#include <math.h>
#include <string.h>
#include <wchar.h>
#include <vector>

//this is for TryEnterCriticalSection
#ifndef _WIN32_WINNT
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 37b7eee

Please sign in to comment.