Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "CapturePluginDSCapture.h"
00025
00026 #include <sstream>
00027
00028 using namespace std;
00029
00030 namespace alvar {
00031 namespace plugins {
00032
00033 CaptureDSCapture::CaptureDSCapture(const CaptureDevice captureDevice)
00034 : Capture(captureDevice)
00035 , sampler(this)
00036 , m_pDSCapture(NULL)
00037 , m_nBpp(0)
00038 , m_nVideo_x_res(-1)
00039 , m_nVideo_y_res(-1)
00040 , imgBuffer(NULL)
00041 , imgBufferForCallback(NULL)
00042 , mReturnFrame(NULL)
00043 {
00044 InitializeCriticalSection(&crit);
00045 next_event = CreateEvent(NULL, FALSE, FALSE, NULL);
00046 m_pDSCapture = new CDSCapture(true, false);
00047 }
00048
00049 CaptureDSCapture::~CaptureDSCapture()
00050 {
00051 stop();
00052 if (m_pDSCapture) {
00053 m_pDSCapture->Stop();
00054 delete m_pDSCapture;
00055 }
00056 delete imgBuffer;
00057 delete imgBufferForCallback;
00058 DeleteCriticalSection(&crit);
00059 }
00060
00061 bool CaptureDSCapture::start()
00062 {
00063 if (m_pDSCapture) {
00064 HRESULT hr = m_pDSCapture->Init(true, false, mCaptureDevice.id().c_str(), NULL);
00065
00066 if(SUCCEEDED(hr)){
00067
00068 AM_MEDIA_TYPE mt;
00069 ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
00070 mt.majortype = MEDIATYPE_Video;
00071 mt.subtype = MEDIASUBTYPE_RGB24;
00072 hr = m_pDSCapture->SetVideoGrabberFormat(&mt);
00073 }
00074
00075 if(SUCCEEDED(hr))
00076 hr = m_pDSCapture->ShowVideoCaptureFormatDialog();
00077
00078
00079 if(SUCCEEDED(hr))
00080 hr = m_pDSCapture->Connect();
00081
00082 if(SUCCEEDED(hr)){
00083 AM_MEDIA_TYPE mt;
00084 ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
00085 HRESULT hr = m_pDSCapture->GetVideoGrabberFormat(&mt);
00086 if(SUCCEEDED(hr)){
00087
00088 if ((mt.formattype == FORMAT_VideoInfo) &&
00089 (mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
00090 (mt.pbFormat != NULL) )
00091 {
00092 if(mt.subtype == MEDIASUBTYPE_RGB24)
00093 m_nBpp = 24;
00094 else if(mt.subtype == MEDIASUBTYPE_RGB32)
00095 m_nBpp = 32;
00096
00097 VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)mt.pbFormat;
00098
00099 m_nVideo_x_res = pVih->bmiHeader.biWidth;
00100 m_nVideo_y_res = pVih->bmiHeader.biHeight;
00101 }
00102 }
00103 }
00104
00105 if(FAILED(hr)){
00106 return false;
00107 }
00108
00109 m_pDSCapture->AddVideoCallback(&sampler);
00110
00111 buffer_size = (int)(m_nVideo_x_res * m_nVideo_y_res * (m_nBpp/8.0));
00112 imgBuffer = new BYTE[buffer_size];
00113 imgBufferForCallback = new BYTE[buffer_size];
00114 mReturnFrame = cvCreateImageHeader(cvSize(m_nVideo_x_res, m_nVideo_y_res), IPL_DEPTH_8U,m_nBpp / 8);
00115 mReturnFrame->imageData = (char*)imgBuffer;
00116 }
00117 m_pDSCapture->Start();
00118 mIsCapturing = true;
00119 return true;
00120 }
00121
00122 void CaptureDSCapture::stop()
00123 {
00124 if (isCapturing()) {
00125 HRESULT hr = m_pDSCapture->Stop();
00126 mIsCapturing = false;
00127 }
00128 }
00129
00130 IplImage *CaptureDSCapture::captureImage()
00131 {
00132 if (!isCapturing()) {
00133 return NULL;
00134 }
00135
00136 IplImage *ret = NULL;
00137 if (WaitForSingleObject(next_event, 1000) == WAIT_OBJECT_0) {
00138 EnterCriticalSection(&crit);
00139 ret = mReturnFrame;
00140 ret->origin = 1;
00141 memcpy(imgBuffer,imgBufferForCallback,buffer_size);
00142 LeaveCriticalSection(&crit);
00143 }
00144 return ret;
00145 }
00146
00147 bool CaptureDSCapture::showSettingsDialog()
00148 {
00149 HRESULT hr = m_pDSCapture->ShowVideoCaptureFormatDialog();
00150 return SUCCEEDED(hr);
00151 }
00152
00153 string CaptureDSCapture::SerializeId()
00154 {
00155 return "CaptureDSCapture";
00156 }
00157
00158 bool CaptureDSCapture::Serialize(Serialization *serialization)
00159 {
00160 return false;
00161 }
00162
00163 void CaptureDSCapture::OnVideoSample(BYTE* pBuffer, DWORD dwDataLen, REFERENCE_TIME t_start)
00164 {
00165 EnterCriticalSection(&crit);
00166 if(pBuffer){
00167 if (dwDataLen <= buffer_size) {
00168 memcpy(imgBufferForCallback,pBuffer,dwDataLen);
00169 }
00170 SetEvent(next_event);
00171 }
00172 LeaveCriticalSection(&crit);
00173 }
00174
00175
00176 CapturePluginDSCapture::CapturePluginDSCapture(const string &captureType)
00177 : CapturePlugin(captureType)
00178 {
00179 }
00180
00181 CapturePluginDSCapture::~CapturePluginDSCapture()
00182 {
00183 }
00184
00185 CapturePlugin::CaptureDeviceVector CapturePluginDSCapture::enumerateDevices()
00186 {
00187 CaptureDeviceVector devices;
00188 CDSCapture capture;
00189 device_map* vids = capture.GetVideoCaptureDevices();
00190 for (device_map::iterator i = vids->begin(); i != vids->end(); ++i) {
00191 CaptureDevice dev("dscapture", i->first, i->second);
00192 devices.push_back(dev);
00193 }
00194
00195 return devices;
00196 }
00197
00198 Capture *CapturePluginDSCapture::createCapture(const CaptureDevice captureDevice)
00199 {
00200 return new CaptureDSCapture(captureDevice);
00201 }
00202
00203 void registerPlugin(const string &captureType, alvar::CapturePlugin *&capturePlugin)
00204 {
00205 capturePlugin = new CapturePluginDSCapture(captureType);
00206 }
00207
00208 }
00209 }