CapturePluginDSCapture.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ALVAR, A Library for Virtual and Augmented Reality.
3  *
4  * Copyright 2007-2012 VTT Technical Research Centre of Finland
5  *
6  * Contact: VTT Augmented Reality Team <alvar.info@vtt.fi>
7  * <http://www.vtt.fi/multimedia/alvar.html>
8  *
9  * ALVAR is free software; you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ALVAR; if not, see
21  * <http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>.
22  */
23 
24 #include "CapturePluginDSCapture.h"
25 
26 #include <sstream>
27 
28 using namespace std;
29 
30 namespace alvar {
31 namespace plugins {
32 
33 CaptureDSCapture::CaptureDSCapture(const CaptureDevice captureDevice)
34  : Capture(captureDevice)
35  , sampler(this)
36  , m_pDSCapture(NULL)
37  , m_nBpp(0)
38  , m_nVideo_x_res(-1)
39  , m_nVideo_y_res(-1)
40  , imgBuffer(NULL)
41  , imgBufferForCallback(NULL)
42  , mReturnFrame(NULL)
43 {
44  InitializeCriticalSection(&crit);
45  next_event = CreateEvent(NULL, FALSE, FALSE, NULL);
46  m_pDSCapture = new CDSCapture(true, false);
47 }
48 
50 {
51  stop();
52  if (m_pDSCapture) {
53  m_pDSCapture->Stop();
54  delete m_pDSCapture;
55  }
56  delete imgBuffer;
57  delete imgBufferForCallback;
58  DeleteCriticalSection(&crit);
59 }
60 
62 {
63  if (m_pDSCapture) {
64  HRESULT hr = m_pDSCapture->Init(true, false, mCaptureDevice.id().c_str(), NULL);
65 
66  if(SUCCEEDED(hr)){
67  // Set video grabber media type
68  AM_MEDIA_TYPE mt;
69  ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
70  mt.majortype = MEDIATYPE_Video;
71  mt.subtype = MEDIASUBTYPE_RGB24;
72  hr = m_pDSCapture->SetVideoGrabberFormat(&mt);
73  }
74 
75  if(SUCCEEDED(hr))
76  hr = m_pDSCapture->ShowVideoCaptureFormatDialog();
77 
78  // We must connect filters before we can get connected media type from grabber(s)
79  if(SUCCEEDED(hr))
80  hr = m_pDSCapture->Connect();
81 
82  if(SUCCEEDED(hr)){
83  AM_MEDIA_TYPE mt;
84  ZeroMemory(&mt, sizeof(AM_MEDIA_TYPE));
85  HRESULT hr = m_pDSCapture->GetVideoGrabberFormat(&mt);
86  if(SUCCEEDED(hr)){
87  // Examine the format block.
88  if ((mt.formattype == FORMAT_VideoInfo) &&
89  (mt.cbFormat >= sizeof(VIDEOINFOHEADER)) &&
90  (mt.pbFormat != NULL) )
91  {
92  if(mt.subtype == MEDIASUBTYPE_RGB24)
93  m_nBpp = 24;
94  else if(mt.subtype == MEDIASUBTYPE_RGB32)
95  m_nBpp = 32;
96 
97  VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)mt.pbFormat;
98  //cout << "Video capture: "<<pVih->bmiHeader.biWidth<<"x"<<pVih->bmiHeader.biHeight<<" "<<pVih->bmiHeader.biBitCount<<" bpp "<<fps<<" fps";
99  m_nVideo_x_res = pVih->bmiHeader.biWidth;
100  m_nVideo_y_res = pVih->bmiHeader.biHeight;
101  }
102  }
103  }
104 
105  if(FAILED(hr)){
106  return false;
107  }
108 
109  m_pDSCapture->AddVideoCallback(&sampler);
110 
111  buffer_size = (int)(m_nVideo_x_res * m_nVideo_y_res * (m_nBpp/8.0));
112  imgBuffer = new BYTE[buffer_size];
114  mReturnFrame = cvCreateImageHeader(cvSize(m_nVideo_x_res, m_nVideo_y_res), IPL_DEPTH_8U,m_nBpp / 8);
115  mReturnFrame->imageData = (char*)imgBuffer;
116  }
117  m_pDSCapture->Start();
118  mIsCapturing = true;
119  return true;
120 }
121 
123 {
124  if (isCapturing()) {
125  HRESULT hr = m_pDSCapture->Stop();
126  mIsCapturing = false;
127  }
128 }
129 
131 {
132  if (!isCapturing()) {
133  return NULL;
134  }
135 
136  IplImage *ret = NULL;
137  if (WaitForSingleObject(next_event, 1000) == WAIT_OBJECT_0) {
138  EnterCriticalSection(&crit);
139  ret = mReturnFrame;
140  ret->origin = 1;
142  LeaveCriticalSection(&crit);
143  }
144  return ret;
145 }
146 
148 {
149  HRESULT hr = m_pDSCapture->ShowVideoCaptureFormatDialog();
150  return SUCCEEDED(hr);
151 }
152 
154 {
155  return "CaptureDSCapture";
156 }
157 
159 {
160  return false;
161 }
162 
163 void CaptureDSCapture::OnVideoSample(BYTE* pBuffer, DWORD dwDataLen, REFERENCE_TIME t_start)
164 {
165  EnterCriticalSection(&crit);
166  if(pBuffer){
167  if (dwDataLen <= buffer_size) {
168  memcpy(imgBufferForCallback,pBuffer,dwDataLen);
169  }
170  SetEvent(next_event);
171  }
172  LeaveCriticalSection(&crit);
173 }
174 
175 
177  : CapturePlugin(captureType)
178 {
179 }
180 
182 {
183 }
184 
186 {
187  CaptureDeviceVector devices;
188  CDSCapture capture;
189  device_map* vids = capture.GetVideoCaptureDevices();
190  for (device_map::iterator i = vids->begin(); i != vids->end(); ++i) {
191  CaptureDevice dev("dscapture", i->first, i->second);
192  devices.push_back(dev);
193  }
194 
195  return devices;
196 }
197 
199 {
200  return new CaptureDSCapture(captureDevice);
201 }
202 
203 void registerPlugin(const string &captureType, alvar::CapturePlugin *&capturePlugin)
204 {
205  capturePlugin = new CapturePluginDSCapture(captureType);
206 }
207 
208 } // namespace plugins
209 } // namespace alvar
Capture * createCapture(const CaptureDevice captureDevice)
Create Capture class. Transfers onwership to the caller.
Main ALVAR namespace.
Definition: Alvar.h:174
std::string SerializeId()
The identification of the class for serialization.
void OnVideoSample(BYTE *pBuffer, DWORD dwDataLen, REFERENCE_TIME t_start)
unsigned char BYTE
IplImage * captureImage()
Capture one image from the camera.
CaptureDevice mCaptureDevice
Definition: Capture.h:180
CaptureDeviceVector enumerateDevices()
Enumerate capture devices currently available.
CapturePluginDSCapture(const std::string &captureType)
Constructor.
CaptureDevice holder for camera information.
Definition: CaptureDevice.h:44
void registerPlugin(const std::string &captureType, alvar::CapturePlugin *&capturePlugin)
CapturePlugin interface that plugins must implement.
Definition: CapturePlugin.h:44
Implementation of Capture interface for DSCapture plugin.
std::string id() const
The id of the camera.
This file implements a capture plugin based on DSCapture.
bool isCapturing()
Test if the camera was properly initialized.
Definition: Capture.h:85
void stop()
Stops the camera capture.
Capture interface that plugins must implement.
Definition: Capture.h:46
Class for serializing class content to/from file or std::iostream.
Definition: Util.h:352
alvar::plugins::CaptureDSCapture::VideoSampler sampler
bool start()
Starts the camera capture.
bool mIsCapturing
Definition: Capture.h:183
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
Definition: CapturePlugin.h:60
bool showSettingsDialog()
Show the settings dialog of the camera.
bool Serialize(Serialization *serialization)
Performs serialization of the class members and configuration.


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:03