OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DirectShowImageSource.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 "CheckFirmwareVersion.h"
28 #include "DirectShowToCV.h"
29 #include "ImageSourceFactories.h"
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <iostream>
36 
37 namespace osvr {
38 namespace vbtracker {
39  using directx_camera_server_ptr = std::unique_ptr<directx_camera_server>;
41  public:
42  DirectShowImageSource(directx_camera_server_ptr &&cam)
43  : m_camera(std::move(cam)) {
44  m_camera->read_image_to_memory();
45  if (m_camera->isOpened()) {
46  storeRes();
47  }
48  }
49  virtual ~DirectShowImageSource() {}
50 
51  bool ok() const override {
52  return m_camera && m_gotRes && m_camera->working() &&
53  m_camera->isOpened();
54  }
55  bool grab() override;
56  void retrieveColor(cv::Mat &color,
57  osvr::util::time::TimeValue &timestamp) override;
58  cv::Size resolution() const override;
59 
60  private:
61  void storeRes();
62  directx_camera_server_ptr m_camera;
63  bool m_gotRes = false;
64  cv::Size m_res;
65  };
66 
67  ImageSourcePtr openHDKCameraDirectShow(bool highGain) {
68  auto ret = ImageSourcePtr{};
69  auto cam = getDirectShowHDKCamera(highGain);
70  if (!cam) {
71  return ret;
72  }
73 
74  // Check firmware version
75  {
76  static const auto UPDATER_URL = "osvr.github.io";
77  auto firmwareStatus = checkCameraFirmwareRevision(cam->getPath());
78  switch (firmwareStatus) {
79  case osvr::vbtracker::FirmwareStatus::Good:
80  break;
81  case FirmwareStatus::Future:
82  std::cerr
83  << "\n[Video-based Tracking] Note: Camera firmware "
84  "version detected was newer than recognized latest "
85  "version, assuming OK. You may want to update your "
86  "OSVR server or plugin!\n"
87  << std::endl;
88  break;
89  case osvr::vbtracker::FirmwareStatus::UpgradeRequired:
90  case osvr::vbtracker::FirmwareStatus::UpgradeUseful:
91  std::cerr
92  << "\n[Video-based Tracking] WARNING - Your HDK infrared "
93  "tracking camera was detected to have outdated "
94  "firmware in need of updating, and may not function "
95  "properly. Please visit "
96  << UPDATER_URL << " to get the "
97  "camera firmware updater.\n\n"
98  << std::endl;
101  break;
102  case osvr::vbtracker::FirmwareStatus::Unknown:
103  std::cerr
104  << "\n[Video-based Tracking] Note: Could not detect the "
105  "firmware version on your HDK infrared "
106  "tracking camera. You may need to update it: see "
107  << UPDATER_URL << " to get the camera firmware updater.\n\n"
108  << std::endl;
109  break;
110  default:
111  break;
112  }
113 
114  if (firmwareStatus ==
115  osvr::vbtracker::FirmwareStatus::UpgradeRequired) {
118  std::cerr << "\n[Video-based Tracking] The video tracking "
119  "plugin cannot run with your camera until the "
120  "firmware has been updated.\n\n"
121  << std::endl;
122  return ret;
123  }
124  }
125  ret.reset(new DirectShowImageSource{std::move(cam)});
126  return ret;
127  }
128 
130  return m_camera->read_image_to_memory();
131  }
133  cv::Mat &color, osvr::util::time::TimeValue &timestamp) {
134  color = ::retrieve(*m_camera);
135  timestamp = m_camera->get_buffer_timestamp();
136  }
137  cv::Size DirectShowImageSource::resolution() const { return m_res; }
138 
139  void DirectShowImageSource::storeRes() {
140  int minx, miny, maxx, maxy;
141  m_camera->read_range(minx, maxx, miny, maxy);
142  m_res = cv::Size(maxx - minx + 1, maxy - miny + 1);
143  m_gotRes = true;
144  }
145 } // namespace vbtracker
146 } // namespace osvr
cv::Size resolution() const override
Get resolution of the images from this source.
void retrieveColor(cv::Mat &color, osvr::util::time::TimeValue &timestamp) override
virtual void retrieve(cv::Mat &color, cv::Mat &gray, osvr::util::time::TimeValue &timestamp)
Call after grab() to get the actual image data.
Definition: ImageSource.cpp:37
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81