OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
SampleGrabberWrapper.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include "SampleGrabberWrapper.h"
28 
29 // Library/third-party includes
30 // - none
31 
32 // Standard includes
33 #include <stdexcept>
34 #include <dshow.h>
35 #include "qedit_wrapper.h"
36 
37 extern "C" const CLSID CLSID_SampleGrabber;
39  comutils::Ptr<ISampleGrabber> grabber;
40 };
41 
42 SampleGrabberWrapper::SampleGrabberWrapper()
43  : impl_(new Impl), sampleExchange_(std::make_shared<MediaSampleExchange>()),
44  callback_(new directx_samplegrabber_callback(sampleExchange_)) {
45 
46 #ifdef DEBUG
47  printf("directx_camera_server::open_and_find_parameters(): Before "
48  "CoCreateInstance SampleGrabber\n");
49 #endif
50  // Create the sample grabber
51  CoCreateInstance(CLSID_SampleGrabber, nullptr, CLSCTX_INPROC_SERVER,
52  IID_IBaseFilter, AttachPtr(sampleGrabber_));
53  if (!sampleGrabber_) {
54  throw std::runtime_error("Could not create Sample Grabber filter - "
55  "possibly missing DirectShow/DirectX 8.1+?");
56  }
57  // Get its sample grabber interface
58  sampleGrabber_->QueryInterface(IID_ISampleGrabber,
59  AttachPtr(impl_->grabber));
60 
61  // Do the setup...
62 
63  // Ask for video media producers that produce 8-bit RGB
64  AM_MEDIA_TYPE mt = {0};
65  mt.majortype = MEDIATYPE_Video; // Ask for video media producers
66  mt.subtype = MEDIASUBTYPE_RGB24; // Ask for 8 bit RGB
67  impl_->grabber->SetMediaType(&mt);
68 
69  impl_->grabber->SetCallback(callback_.get(),
70  0 /* to call the SampleCB method */);
71  // Set the grabber do not do one-shot mode because that would cause
72  // it to stop the stream after a single frame is captured.
73  impl_->grabber->SetOneShot(FALSE);
74 
75  // Set the grabber to not do buffering mode, because we've not
76  // implemented the handler for buffered callbacks.
77  impl_->grabber->SetBufferSamples(FALSE);
78 }
79 
80 SampleGrabberWrapper::~SampleGrabberWrapper() { shutdown(); }
81 
83  BOOST_ASSERT(impl_ && impl_->grabber);
84  auto hr = impl_->grabber->GetConnectedMediaType(&mt);
85  if (FAILED(hr)) {
86  throw std::runtime_error("Could not get connected media type - is "
87  "there nothing connected on the input pin of "
88  "the filter?");
89  }
90 }
91 
93  // Clear the callback
94  BOOST_ASSERT(impl_ && impl_->grabber);
95  impl_->grabber->SetCallback(nullptr, 0 /* to call the SampleCB method */);
96  // Tell the callback object to finish up.
97  if (callback_) {
98  callback_->shutdown();
99  // callback_.reset();
100  }
101 }
STL namespace.
void shutdown()
Gets the callback to finish up all its samples.
void getConnectedMediaType(AM_MEDIA_TYPE &mt)
Forwards call on to SampleGrabber.