CapturingImageProvider.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Core
19  * @author Kai Welke (kai dot welke at kit dot edu)
20  * @date 2012
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "CapturingImageProvider.h"
26 
30 
31 #include <string>
32 #include <thread>
33 
34 using namespace armarx;
35 namespace visionx
36 {
37  // ================================================================== //
38  // == ImageProvider ice interface =================================== //
39  // ================================================================== //
40  void CapturingImageProvider::startCapture(float framesPerSecond,
41  const Ice::Current& ctx)
42  {
43  std::unique_lock lock(captureMutex);
44  onStartCapture(framesPerSecond);
45 
46  captureEnabled = true;
47  frameRate = framesPerSecond;
48 
49  ARMARX_INFO << "Starting image capture with " << frameRate << "fps";
50  }
51 
52 
53  void CapturingImageProvider::stopCapture(const Ice::Current& ctx)
54  {
55  std::unique_lock lock(captureMutex);
56  captureEnabled = false;
57  onStopCapture();
58  }
59 
60  // ================================================================== //
61  // == Component implementation =============================== //
62  // ================================================================== //
63  void CapturingImageProvider::onInitImageProvider()
64  {
65  // init members
66  frameRate = hasProperty("FPS") ? getProperty<float>("FPS").getValue() : 30;
67  captureEnabled = false;
68 
69  // default sync mode
70  setImageSyncMode(eFpsSynchronization);
71 
72  // call setup of image provider implementation to setup image size
73  onInitCapturingImageProvider();
74 
75  // capature task
76  captureTask = new RunningTask<CapturingImageProvider>(this, &CapturingImageProvider::capture);
77  ARMARX_INFO << "started capturing thread";
78  }
79 
80 
81  void CapturingImageProvider::onConnectImageProvider()
82  {
83  onStartCapturingImageProvider();
84 
85  captureTask->start();
86 
87  // TODO: hack start capturing
88  startCapture(frameRate);
89  }
90 
91 
92  void CapturingImageProvider::onExitImageProvider()
93  {
94  // TODO: hack stop capturing
95  stopCapture();
96 
97  if (captureTask)
98  {
99  captureTask->stop();
100  }
101 
102  onExitCapturingImageProvider();
103  }
104 
105 
106  void CapturingImageProvider::capture()
107  {
108  ARMARX_INFO << "Starting Image Provider: " << getName();
109 
110  // main loop of component
111  std::chrono::milliseconds td(1);
112 
113  while (!captureTask->isStopped() && !isExiting() && sharedMemoryProvider)
114  {
115  auto tmpSharedMemoryProvider = sharedMemoryProvider;
116  if (captureEnabled && tmpSharedMemoryProvider)
117  {
118 
119  MetaInfoSizeBasePtr info = tmpSharedMemoryProvider->getMetaInfo();
121  long oldTimestamp = info ? info->timeProvided : 0;
122  bool succeeded = capture(imageBuffers);
123 
124  if (succeeded)
125  {
126  MetaInfoSizeBasePtr info = tmpSharedMemoryProvider->getMetaInfo();
128  IceUtil::Time image_timestamp = armarx::TimeUtil::GetTime();
129  if (!info || info->timeProvided == oldTimestamp)
130  {
131  ARMARX_WARNING << deactivateSpam(10000000) << "The image provider did not set a timestamp - measuring the timestamp now. The ImageProvider implementation should call updateTimestamp()!";
132  updateTimestamp(image_timestamp.toMicroSeconds());
133  }
134  else if (info)
135  {
136  image_timestamp = IceUtil::Time::microSeconds(info->timeProvided);
137  }
138  auto imageProcessorProxy = this->imageProcessorProxy;
139  recordImages(image_timestamp);
140  ARMARX_CHECK_EXPRESSION(imageProcessorProxy);
141  imageProcessorProxy->reportImageAvailable(getName());
142  }
143 
144  if (imageSyncMode == eFpsSynchronization)
145  {
146  fpsCounter.assureFPS(frameRate);
147  setMetaInfo("fps", new Variant(fpsCounter.getFPS()));
148  setMetaInfo("minCycleTimeMs", new Variant(fpsCounter.getMinCycleTimeMS()));
149  setMetaInfo("maxCycleTimeMs", new Variant(fpsCounter.getMaxCycleTimeMS()));
150  auto dimension = getImageFormat().dimension;
151  setMetaInfo("resolution", new Variant(std::to_string(dimension.width) + "x" + std::to_string(dimension.height)));
152  }
153  }
154  else
155  {
156  std::this_thread::sleep_for(td);
157  }
158  }
159 
160  ARMARX_INFO << "Stopping ImageProvider";
161  }
162 
163  // ================================================================== //
164  // == Utility methods for CapturingImageProviders =================== //
165  // ================================================================== //
166  void CapturingImageProvider::setImageSyncMode(ImageSyncMode imageSyncMode)
167  {
168  this->imageSyncMode = imageSyncMode;
169  }
170 
171  ImageSyncMode CapturingImageProvider::getImageSyncMode()
172  {
173  return imageSyncMode;
174  }
175 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
ExpressionException.h
CapturingImageProvider.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
Variant.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28