CapturePluginDSCapture.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
00003  *
00004  * Copyright 2007-2012 VTT Technical Research Centre of Finland
00005  *
00006  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
00007  *          <http://www.vtt.fi/multimedia/alvar.html>
00008  *
00009  * ALVAR is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU Lesser General Public License as published by the Free
00011  * Software Foundation; either version 2.1 of the License, or (at your option)
00012  * any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful, but WITHOUT
00015  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00017  * for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with ALVAR; if not, see
00021  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
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                 // Set video grabber media type
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         // We must connect filters before we can get connected media type from grabber(s)
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                         // Examine the format block.
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                                 //cout << "Video capture: "<<pVih->bmiHeader.biWidth<<"x"<<pVih->bmiHeader.biHeight<<" "<<pVih->bmiHeader.biBitCount<<" bpp "<<fps<<" fps";
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 } // namespace plugins
00209 } // namespace alvar


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Thu Feb 16 2017 03:23:02