Also check out the NodeMapInfo example if you haven't already. NodeMapInfo demonstrates how to retrieve node map information and explores retrieving information from all major node types on the camera. The various node type include string, integer, float, boolean, command, enumeration, category, and value types. Only enumeration and integer type retrieval are covered in this example.
Please make sure Microsoft Windows SDK 7.1 is installed prior to building this example. Windows SDK 7.1 is the last version shipped with a set of DirectShow samples, and the DirectShow BaseClasses. DirectShow runtime is now a part of the operating system so no DirectShow redistributables are required.
Note: You may run into problems installing Windows SDK 7.1 if you have a newer version of Visual Studio C++ 2010 Redistributable installed. To resolve the issue, you must uninstall all versions of the Visual C++ 2010 Redistributable before installing the Windows 7 SDK.
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#include <iostream>
#include <dshow.h>
#include "resource.h"
#include "comdef.h"
IVideoWindow*
g_pVW =
nullptr;
IMediaControl*
g_pMC =
nullptr;
IMediaEventEx*
g_pME =
nullptr;
{
HRESULT hr;
IBaseFilter* pSrcFilter = nullptr;
if (FAILED(hr))
{
MsgError(TEXT(
"Failed to get video interfaces! hr=0x%x"), hr);
return hr;
}
if (FAILED(hr))
{
MsgError(TEXT(
"Failed to set capture filter graph! hr=0x%x"), hr);
return hr;
}
if (FAILED(hr))
{
return hr;
}
hr =
g_pGraph->AddFilter(pSrcFilter, L
"Video Capture");
if (FAILED(hr))
{
TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n")
TEXT("If you have a working video capture device, please make sure\r\n")
TEXT("that it is connected and is not being used by another application.\r\n\r\n")
TEXT("The sample will now close."),
hr);
pSrcFilter->Release();
return hr;
}
if (FAILED(hr))
{
pSrcFilter->Release();
return hr;
}
hr =
g_pCapture->RenderStream(&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video, pSrcFilter,
nullptr,
nullptr);
if (FAILED(hr))
{
TEXT("Couldn't render the video capture stream. hr=0x%x\r\n")
TEXT("The capture device may already be in use by another application.\r\n\r\n")
TEXT("The sample will now close."),
hr);
pSrcFilter->Release();
return hr;
}
pSrcFilter->Release();
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't initialize video window! hr=0x%x"), hr);
return hr;
}
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't run the graph! hr=0x%x"), hr);
return hr;
}
return S_OK;
}
{
HRESULT hr = S_OK;
IBaseFilter* pSrc = nullptr;
IMoniker* pMoniker = nullptr;
ICreateDevEnum* pDevEnum = nullptr;
IEnumMoniker* pClassEnum = nullptr;
if (!ppSrcFilter)
{
return E_POINTER;
}
hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC, IID_ICreateDevEnum, (void**)&pDevEnum);
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't create system enumerator! hr=0x%x"), hr);
}
if (SUCCEEDED(hr))
{
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't create class enumerator! hr=0x%x"), hr);
}
}
if (SUCCEEDED(hr))
{
if (pClassEnum == nullptr)
{
MessageBox(
TEXT("No video capture device was detected.\r\n\r\n")
TEXT("This sample requires a video capture device, such as a USB WebCam,\r\n")
TEXT("to be installed and working properly. The sample will now close."),
TEXT("No Video Capture Hardware"),
MB_OK | MB_ICONINFORMATION);
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
while (pClassEnum->Next(1, &pMoniker, nullptr) == S_OK)
{
IPropertyBag* pPropBag = nullptr;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)(&pPropBag));
if (FAILED(hr))
{
continue;
}
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
if (wcscmp(varName.bstrVal, L"PtGrey Camera") == 0)
{
hr = pMoniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&pSrc);
if (FAILED(hr))
{
_com_error err(hr);
LPCTSTR errMsg = err.ErrorMessage();
MsgError(TEXT(
"Couldn't bind moniker to filter object! %s"), err.ErrorMessage());
}
VariantClear(&varName);
break;
}
VariantClear(&varName);
}
}
}
if (SUCCEEDED(hr) && pSrc)
{
*ppSrcFilter = pSrc;
(*ppSrcFilter)->AddRef();
}
if (!pSrc)
{
MessageBox(
TEXT("Unable to detect Spinnaker video capture source.\r\n\r\n") TEXT("Please\r\n")
TEXT(" 1. Ensure DirectShow was chosen as a component during\r\n") TEXT(" installation\r\n")
TEXT(" 2. Run DirectShowRegister_v140_[x32 or x64].bat from \r\n")
TEXT(" <InstallDirectory>\\Spinnaker\\src\\DirectShowScriptFiles"),
TEXT("Spinnaker DirectShow Source Not Found"),
MB_OK | MB_ICONINFORMATION);
hr = E_FAIL;
}
return hr;
}
{
HRESULT hr;
hr = CoCreateInstance(CLSID_FilterGraph,
nullptr, CLSCTX_INPROC, IID_IGraphBuilder, (
void**)&
g_pGraph);
if (FAILED(hr))
{
return hr;
}
hr = CoCreateInstance(
CLSID_CaptureGraphBuilder2,
nullptr, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (
void**)&
g_pCapture);
if (FAILED(hr))
{
return hr;
}
hr =
g_pGraph->QueryInterface(IID_IMediaControl, (LPVOID*)&
g_pMC);
if (FAILED(hr))
{
return hr;
}
hr =
g_pGraph->QueryInterface(IID_IVideoWindow, (LPVOID*)&
g_pVW);
if (FAILED(hr))
{
return hr;
}
hr =
g_pGraph->QueryInterface(IID_IMediaEventEx, (LPVOID*)&
g_pME);
if (FAILED(hr))
{
return hr;
}
return hr;
}
{
{
}
{
}
{
g_pVW->put_Visible(OAFALSE);
}
}
{
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't Query the Capture Interface! hr=0x%x"), hr);
return E_FAIL;
}
if (pDevice == nullptr)
{
MsgError(TEXT(
"Couldn't Query the Capture Device!"));
return E_FAIL;
}
try
{
unsigned int numDevices = 0;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't query number of devices!"));
return hr;
}
unsigned int selectedDeviceIndex = 0;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't query currently selected camera index!"));
return hr;
}
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't query camera information!"));
return hr;
}
std::vector<std::string> nodes;
size_t numNodes = 0;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't query number of available nodes!"));
return hr;
}
for (unsigned int index = 0; index < numNodes; index++)
{
if (SUCCEEDED(hr))
{
nodes.push_back(nodeName);
}
}
int64_t widthValue = 0;
int64_t heightValue = 0;
std::string nodeName = "PixelFormat";
bool isAvailable = false;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't check whether PixelFormat is available!"));
return hr;
}
bool isReadable = false;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't check whether PixelFormat is readable!"));
return hr;
}
if (isReadable)
{
size_t numEntries = 0;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't query number of enumeration entries for PixelFormat!"));
return hr;
}
std::vector<std::string> enumEntries;
for (unsigned int index = 0; index < numNodes; index++)
{
if (SUCCEEDED(hr))
{
enumEntries.push_back(enumEntryName);
}
}
bool isWritable = false;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't check whether PixelFormat is writable!"));
return hr;
}
bool entryExist = false;
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't check whether Mono8 entry is available for PixelFormat!"));
return hr;
}
if (entryExist && isWritable)
{
}
if (FAILED(hr))
{
MsgError(TEXT(
"Couldn't get current values for PixelFormat!"));
return hr;
}
}
nodeName = "Width";
isAvailable = false;
isReadable = false;
if (isReadable)
{
}
nodeName = "Height";
isAvailable = false;
isReadable = false;
if (isReadable)
{
}
std::wstringstream infoString;
infoString << L"Number of cameras found: " << numDevices << std::endl
<< L"Selected Index : " << selectedDeviceIndex << std::endl
<< L"Camera Model : " << model << std::endl
<< L"Camera Sensor : " << sensor << std::endl
<< L"Camera Serial : " << serial << std::endl
<< L"Number of nodes: " << nodes.size() << std::endl
<< L"Current Pixel Format: " << pixelValue << std::endl
<< L"Width : " << widthValue << std::endl
<< L"Height : " << heightValue << std::endl;
MsgInfo(TEXT(
"%s"), infoString.str().c_str());
}
{
std::basic_ostringstream<TCHAR> ex;
MsgError(TEXT(
"Unexpected exception: %s\n\r"), ex.str().c_str());
}
return S_OK;
}
{
HRESULT hr;
if (FAILED(hr))
{
return hr;
}
hr =
g_pVW->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
if (FAILED(hr))
{
return hr;
}
hr =
g_pVW->put_Visible(OATRUE);
if (FAILED(hr))
{
return hr;
}
return hr;
}
{
{
RECT rc;
GetClientRect(
ghApp, &rc);
g_pVW->SetWindowPosition(0, 0, rc.right, rc.bottom);
}
}
{
HRESULT hr = S_OK;
{
return S_OK;
}
if (nShow)
{
{
}
}
else
{
hr =
g_pMC->StopWhenReady();
}
return hr;
}
{
TCHAR szBuffer[1024];
const size_t NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
const int LASTCHAR = NUMCHARS - 1;
va_list pArgs;
va_start(pArgs, szFormat);
(void)StringCchVPrintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
va_end(pArgs);
szBuffer[LASTCHAR] = TEXT('\0');
MessageBox(nullptr, szBuffer, TEXT("SpinSimpleGUI_DirectShow Error"), MB_OK | MB_ICONERROR);
}
{
TCHAR szBuffer[1024];
const size_t NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
const int LASTCHAR = NUMCHARS - 1;
va_list pArgs;
va_start(pArgs, szFormat);
(void)StringCchVPrintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
va_end(pArgs);
szBuffer[LASTCHAR] = TEXT('\0');
MessageBox(nullptr, szBuffer, TEXT("SpinSimpleGUI_DirectShow Info"), MB_OK | MB_ICONINFORMATION);
}
{
LONG evCode;
LONG_PTR evParam1, evParam2;
HRESULT hr = S_OK;
{
return E_POINTER;
}
while (SUCCEEDED(
g_pME->GetEvent(&evCode, &evParam1, &evParam2, 0)))
{
hr =
g_pME->FreeEventParams(evCode, evParam1, evParam2);
}
return hr;
}
LRESULT CALLBACK
WndMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HINSTANCE hInstance = reinterpret_cast<LPCREATESTRUCT>(lParam)->hInstance;
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(
IDI_ICON1));
if (hIcon != nullptr)
{
SendMessage(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(hIcon));
}
break;
}
break;
case WM_SIZE:
break;
case WM_WINDOWPOSCHANGED:
break;
case WM_CLOSE:
ShowWindow(
ghApp, SW_HIDE);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
{
g_pVW->NotifyOwnerMessage((LONG_PTR)hwnd, message, wParam, lParam);
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
int PASCAL
WinMain(HINSTANCE hInstance, HINSTANCE hInstP, LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg = {0};
WNDCLASS wc;
if (FAILED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)))
{
MsgError(TEXT(
"CoInitialize Failed!\r\n"));
exit(1);
}
ZeroMemory(&wc, sizeof wc);
wc.hInstance = hInstance;
wc.lpszMenuName = nullptr;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hIcon = nullptr;
if (!RegisterClass(&wc))
{
MsgError(TEXT(
"RegisterClass Failed! Error=0x%x\r\n"), GetLastError());
CoUninitialize();
exit(1);
}
WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
hInstance,
0);
{
HRESULT hr;
if (FAILED(hr))
{
}
else
{
ShowWindow(
ghApp, nCmdShow);
}
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
return (int)msg.wParam;
}
#define IDI_ICON1
Definition resource.h:5
HRESULT GetInterfaces(void)
Definition SpinSimpleGUI_DirectShow.cpp:292
HRESULT ChangePreviewState(int nShow)
Definition SpinSimpleGUI_DirectShow.cpp:626
IVideoWindow * g_pVW
Definition SpinSimpleGUI_DirectShow.cpp:70
HRESULT CaptureVideo()
Definition SpinSimpleGUI_DirectShow.cpp:77
void MsgError(TCHAR *szFormat,...)
Definition SpinSimpleGUI_DirectShow.cpp:655
void MsgInfo(TCHAR *szFormat,...)
Definition SpinSimpleGUI_DirectShow.cpp:676
void CloseInterfaces(void)
Definition SpinSimpleGUI_DirectShow.cpp:336
PLAYSTATE g_psCurrent
Definition SpinSimpleGUI_DirectShow.cpp:75
HWND ghApp
Definition SpinSimpleGUI_DirectShow.cpp:67
ICaptureGraphBuilder2 * g_pCapture
Definition SpinSimpleGUI_DirectShow.cpp:74
HRESULT SetupVideoWindow(void)
Definition SpinSimpleGUI_DirectShow.cpp:581
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hInstP, LPSTR lpCmdLine, int nCmdShow)
Definition SpinSimpleGUI_DirectShow.cpp:769
LRESULT CALLBACK WndMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition SpinSimpleGUI_DirectShow.cpp:723
IGraphBuilder * g_pGraph
Definition SpinSimpleGUI_DirectShow.cpp:73
void ResizeVideoWindow(void)
Definition SpinSimpleGUI_DirectShow.cpp:613
DWORD g_dwGraphRegister
Definition SpinSimpleGUI_DirectShow.cpp:68
IMediaEventEx * g_pME
Definition SpinSimpleGUI_DirectShow.cpp:72
HRESULT HandleGraphEvent(void)
Definition SpinSimpleGUI_DirectShow.cpp:697
HRESULT FindCaptureDevice(IBaseFilter **ppSrcFilter)
Definition SpinSimpleGUI_DirectShow.cpp:170
HRESULT SetupCamera(IBaseFilter *pSrcFilter)
Definition SpinSimpleGUI_DirectShow.cpp:370
IMediaControl * g_pMC
Definition SpinSimpleGUI_DirectShow.cpp:71
#define SAFE_RELEASE(x)
Definition SpinSimpleGUI_DirectShow.h:51
#define CLASSNAME
Definition SpinSimpleGUI_DirectShow.h:72
#define DEFAULT_VIDEO_HEIGHT
Definition SpinSimpleGUI_DirectShow.h:69
PLAYSTATE
Definition SpinSimpleGUI_DirectShow.h:41
@ Running
Definition SpinSimpleGUI_DirectShow.h:44
@ Stopped
Definition SpinSimpleGUI_DirectShow.h:42
#define APPLICATIONNAME
Definition SpinSimpleGUI_DirectShow.h:71
#define DEFAULT_VIDEO_WIDTH
Definition SpinSimpleGUI_DirectShow.h:68
#define WM_GRAPHNOTIFY
Definition SpinSimpleGUI_DirectShow.h:75
const unsigned int MAX_LENGTH
Definition SpinnakerDirectShow.h:23
Definition SpinnakerDirectShow.h:61
virtual HRESULT NodeIsWritable(const char *nodeName, bool *isWritable)=0
Checks if a node is writable.
virtual HRESULT EnumerationGetEntry(const char *nodeName, char *value, size_t bufferSize)=0
Gets enumeration entry string for the specified enumeration feature.
virtual HRESULT IntegerGetValue(const char *nodeName, int64_t *value)=0
Gets integer node value for the specified feature.
virtual HRESULT NodeIsAvailable(const char *nodeName, bool *isAvailable)=0
Checks if a node is available.
virtual HRESULT GetNumCameras(unsigned int *numCameras)=0
Retrieves the number of cameras available for this Spinnaker DirectShow interface.
virtual HRESULT NodeMapGetNumNodes(size_t *numNodes)=0
Retrieves the number of nodes available in the node map for the currently selected camera.
virtual HRESULT EnumerationGetEntryAtIndex(const char *enumerationName, unsigned int entryIndex, char *enumerationEntryName, size_t bufferSize)=0
Gets enumeration entry string for the specified enumeration feature at the specified index.
virtual HRESULT EnumerationGetNumEntries(const char *enumerationName, size_t *numEntries)=0
Retrieves the number of enumeration entry nodes available for the specified enumeration feature.
virtual HRESULT EnumerationEntryExists(const char *enumerationName, const char *enumerationEntryName, bool *entryExist)=0
Checks if an enumeration entry exists for the specified enumeration feature.
virtual HRESULT GetCameraInfo(unsigned int index, char *model, char *type, char *serial, size_t bufferSize)=0
Retrieves general information about the camera at the specified index.
virtual HRESULT NodeIsReadable(const char *nodeName, bool *isReadable)=0
Checks if a node is readable.
virtual HRESULT NodeMapGetNodeAtIndex(size_t index, char *nodeName, size_t bufferSize)=0
Retrieves the string representation of the node at the specified index.
virtual HRESULT EnumerationSetEntry(const char *nodeName, const char *value)=0
Sets enumeration entry for the specified enumeration feature.
virtual HRESULT GetSelectedCameraIndex(unsigned int *selectedIndex)=0
Retrieves an integer index to the currently selected camera.
Definition SpinnakerDirectShow.h:538
The Exception object represents an error that is returned from the library.
Definition Exception.h:51
virtual const char * what() const
virtual override for what().
static const GUID IID_ISpinnakerInterface
This is the Interface that allows users to get and set device node properties on the camera.
Definition SpinnakerDirectShow.h:53