Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Dispatcher.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef DISPATCHER_H
30 #define DISPATCHER_H
31 
32 #include <stdint.h>
33 #include <string>
34 #include <list>
35 
36 #include <Argus/Argus.h>
37 
38 #include "Value.h"
39 #include "Error.h"
40 #include "VideoPipeline.h"
41 #include "IObserver.h"
42 #include "Util.h"
43 #include "TrackedUniqueObject.h"
44 #include "UniquePointer.h"
45 
46 namespace Argus { namespace Ext { class IDeFogSettings; } }
47 
48 namespace ArgusSamples
49 {
50 
51 class IObserverForInterface;
52 
53 /**
54  * The dispatcher is called by clients like the command line interface.
55  * It provides functions to set properties like the camera device index or the sensor mode. It
56  * also implements services to open devices, sessions and create requests.
57  * It also maintains an internal capture session which is used when only a single session is
58  * required.
59  */
61  : public IObserver
62  , public Tracker<Argus::CaptureSession>
63  , public Tracker<Argus::Request>
64 {
65 public:
66  /**
67  * Get the dispatcher instance.
68  */
69  static Dispatcher &getInstance();
70 
71  /**
72  * Shutdown, free all resources
73  */
74  bool shutdown();
75 
76  /**
77  * Returns whether or not an extension is supported.
78  */
79  bool supportsExtension(const Argus::ExtensionName& extension) const;
80 
81  /**
82  * Get an information string
83  */
84  bool getInfo(std::string &info) const;
85 
86  /**
87  * Get the sensor mode for a given index
88  *
89  * @param sensorModeIndex [in]
90  * @param sensorMode [out]
91  */
92  bool getSensorMode(uint32_t sensorModeIndex, Argus::SensorMode **sensorMode) const;
93 
94  /**
95  * Returns the range of focuser positions of the current device.
96  * The units are focuser steps.
97  */
98  Argus::Range<int32_t> getDeviceFocusPositionRange() const;
99 
100  /**
101  * Get the output size
102  */
103  bool getOutputSize(Argus::Size2D<uint32_t> *size) const;
104 
105  /**
106  * Get the amount of available camera devices
107  */
108  uint32_t getDeviceCount() const;
109 
110  /**
111  * Create a capture session using the device index
112  */
113  bool createSession(TrackedUniqueObj<Argus::CaptureSession> &session, uint32_t deviceIndex);
114 
115  /**
116  * Wait for events from the specific session.
117  *
118  * @param eventQueue [in] event queue to transfer events to
119  * @param timeout [in] maximum time (in nanoseconds) to wait for new events.
120  * @param session [in] capture session (optional, if NULL the internal session is used)
121  */
122  bool waitForEvents(Argus::EventQueue *eventQueue, TimeValue timeout = TimeValue::infinite(),
123  Argus::CaptureSession *session = NULL);
124 
125  /**
126  * Dump runtime information for a session.
127  */
128  bool dumpSessionInfo() const;
129 
130  /**
131  * Create a request for a session
132  *
133  * @param request [out] created request
134  * @param captureIntent [in] capture intent
135  * @param session [in] capture session (optional, if NULL the internal session is used)
136  */
138  Argus::CaptureIntent captureIntent, Argus::CaptureSession *session = NULL);
139 
140  /**
141  * create a event queue
142  *
143  * @param eventTypes [in]
144  * @param eventQueue [out] created event queue
145  * @param session [in] capture session (optional, if NULL the internal session is used)
146  */
147  bool createEventQueue(const std::vector<Argus::EventType> &eventTypes,
148  Argus::UniqueObj<Argus::EventQueue> &eventQueue, Argus::CaptureSession *session = NULL);
149 
150 
151  /**
152  * Submits a single capture request for a session
153  *
154  * @param request [in] capture request
155  * @param session [in] capture session (optional, if NULL the internal session is used)
156  */
157  bool capture(Argus::Request *request, Argus::CaptureSession *session = NULL);
158 
159  /**
160  * Start a repeating request for a session
161  *
162  * @param request [in] request
163  * @param session [in] capture session (optional, if NULL the internal session is used)
164  */
165  bool startRepeat(Argus::Request *request, Argus::CaptureSession *session = NULL);
166 
167  /**
168  * Start a repeating burst request for a session
169  *
170  * @param requestList [in] burst request
171  * @param session [in] capture session (optional, if NULL the internal session is used)
172  */
173  bool startRepeatBurst(const std::vector<const Argus::Request*>& requestList,
174  Argus::CaptureSession *session = NULL);
175 
176  /**
177  * Clears the repeating request for a session
178  *
179  * @param session [in] capture session (optional, if NULL the internal session is used)
180  */
181  bool stopRepeat(Argus::CaptureSession *session = NULL);
182 
183  /**
184  * Restart the currently active requests to pick up changed settings.
185  */
186  bool restartActiveRequests();
187 
188  /**
189  * Returns the maximum number of capture requests for a session that can be included in a
190  * burst capture.
191  *
192  * @param session [in] capture session (optional, if NULL the internal session is used)
193  */
194  uint32_t maxBurstRequests(Argus::CaptureSession *session = NULL);
195 
196  /**
197  * Wait until all pending captures for a session are complete
198  *
199  * @param session [in] capture session (optional, if NULL the internal session is used)
200  */
201  bool waitForIdle(Argus::CaptureSession *session = NULL);
202 
203  /**
204  * Create an output stream of an request of a session.
205  *
206  * @param request [in] request
207  * @param stream [out] the created Argus output stream
208  * @param session [in] capture session (optional, if NULL the internal session is used)
209  */
210  bool createOutputStream(Argus::Request *request, Argus::UniqueObj<Argus::OutputStream> &stream,
211  Argus::CaptureSession *session = NULL);
212 
213  /**
214  * Enable an output stream of an request
215  */
216  bool enableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
217 
218  /**
219  * Disable an output stream of an request
220  */
221  bool disableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
222 
223  /**
224  * Output a message if verbose mode is enabled
225  */
226  bool message(const char *msg, ...);
227 
228 private:
229  // the current properties need to be initialized first, some Value<> members below use them
230  // for the validator
231 
232  // current device properties
233  std::vector<Argus::SensorMode*> m_sensorModes; ///< sensor modes
234  Value<Argus::Range<int32_t> > m_deviceFocusPositionRange; ///< device focus position range
235  Value<Argus::Range<Argus::Range<float> > >
236  m_deviceIspDigitalGainRange; ///< device isp digital gain range
237 
238  // current sensor mode properties
239  Value<Argus::Range<Argus::Range<uint64_t> > >
240  m_sensorExposureTimeRange; ///< exposure time range
241  Value<Argus::Range<Argus::Range<float> > >
242  m_sensorAnalogGainRange; ///< analog gain range
243  Value<Argus::Range<float> > m_sensorFrameRateRange; ///< frame rate range
244 
245 public:
246  Value<uint32_t> m_deviceIndex; ///< the device index
247  Value<bool> m_deviceOpen; ///< if set then the device is open
248 
249 
250  Value<bool> m_verbose; ///< if set verbose mode is enabled and messages are printed
251 
252  Value<bool> m_kpi; ///< if set kpi mode is enabled and kpi number are printed
253 
254  // source settings
255  Value<Argus::Range<uint64_t> > m_exposureTimeRange; ///< exposure time range
256  Value<Argus::Range<float> > m_gainRange; ///< gain range
257  Value<uint32_t> m_sensorModeIndex; ///< the sensor mode index
258  Value<float> m_frameRate; ///< in frames per second
259  Value<int32_t> m_focusPosition; ///< focus position
260 
261  // denoise settings
262  Value<Argus::DenoiseMode> m_denoiseMode; ///< denoise mode
263  Value<float> m_denoiseStrength; ///< denoise strength
264 
265  // edge enhance settings
266  Value<Argus::EdgeEnhanceMode> m_edgeEnhanceMode; ///< edge enhancement mode
267  Value<float> m_edgeEnhanceStrength; ///< edge enhancement strength
268 
269  // video stabilization settings
270  Value<Argus::VideoStabilizationMode> m_vstabMode; ///< video stabilization mode
271 
272  // auto control settings
273  Value<Argus::AeAntibandingMode> m_aeAntibandingMode; ///< auto exposure antibanding mode
274  Value<bool> m_aeLock; ///< auto exposure lock
275  Value<bool> m_awbLock; ///< auto white balance lock
276  Value<Argus::AwbMode> m_awbMode; ///< auto white balance mode
277  Value<float> m_exposureCompensation;///< exposure compensation
278  Value<Argus::Range<float> > m_ispDigitalGainRange;///< ISP digital Gain Range
279 
280  // video settings
281  Value<VideoPipeline::VideoFormat> m_videoFormat; ///< the video format
282  Value<VideoPipeline::VideoFileType> m_videoFileType; ///< the video file type
283  Value<uint32_t> m_videoBitRate; ///< the video bit rate
284 
285  // output settings
286  Value<Argus::Size2D<uint32_t> > m_outputSize; ///< output size
287  Value<std::string> m_outputPath; ///< output path
288 
289  // DeFog extension
290  Value<bool> m_deFogEnable; ///< enable
291  Value<float> m_deFogAmount; ///< amount of fog to be removed. Range 0.0 - 1.0 (none - all)
292  Value<float> m_deFogQuality; ///< quality of the effect. Range 0.0 - 1.0 (low - high)
293 
294 private:
295  Dispatcher();
296  ~Dispatcher();
297 
298  // this is a singleton, hide copy constructor etc.
299  Dispatcher(const Dispatcher&);
301 
302  /**
303  * Initialize the dispatcher
304  */
305  bool initialize();
306 
307  /**
308  * Create the internal session
309  */
310  bool createSession();
311 
312  /**
313  * Close the internal session
314  */
315  bool closeSession();
316 
317  /**
318  * Start tracking a session
319  */
320  bool track(Argus::CaptureSession *session);
321 
322  /**
323  * No longer track a session
324  */
325  bool untrack(Argus::CaptureSession *session);
326 
327  /**
328  * Start tracking a request
329  */
330  bool track(Argus::Request *request);
331 
332  /**
333  * No longer track a request
334  */
335  bool untrack(Argus::Request *request);
336 
337  /**
338  * Register an IDenoiseSettings observer
339  */
340  bool registerObserver(Argus::IDenoiseSettings *iDenoiseSettings);
341 
342  /**
343  * Register an IEdgeEnhanceSettings observer
344  */
345  bool registerObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings);
346 
347  /**
348  * Register an IVideoStabilizationSettings observer
349  */
350  bool registerObserver(Argus::IVideoStabilizationSettings *iVideoStabilizationSettings);
351 
352  /**
353  * Register an ISourceSettings observer
354  */
355  bool registerObserver(Argus::ISourceSettings *iSourceSettings);
356 
357  /**
358  * Register an IAutoControlSettings observer
359  */
360  bool registerObserver(Argus::IAutoControlSettings *iAutoControlSettings);
361 
362  /**
363  * Register an IDeFogSettings observer
364  */
365  bool registerObserver(Argus::Ext::IDeFogSettings *iDeFogSettings);
366 
367  /**
368  * Unregister an interface which had been registered as an observer.
369  */
370  bool unregisterObserver(Argus::Interface *interface);
371 
372  /**
373  * Callback when the device index changes.
374  */
375  bool onDeviceIndexChanged(const Observed &source);
376 
377  /**
378  * Callback when the sensor mode index changes.
379  */
380  bool onSensorModeIndexChanged(const Observed &source);
381 
382  bool m_initialized; ///< if set the dispatcher is initialized
383 
384  std::list<IObserverForInterface*> m_observers;
385 
386  Argus::UniqueObj<Argus::CameraProvider> m_cameraProvider; ///< camera provider
387  Argus::ICameraProvider *m_iCameraProvider; ///< camera provider interface
388 
389  std::vector<Argus::CameraDevice*> m_cameraDevices; ///< a list of available devices
390 
392 
394  {
395  public:
396  ActiveSession(Argus::CaptureSession *session)
397  : m_session(session)
398  {
399  }
400 
401  Argus::CaptureSession *m_session;
402  std::vector<const Argus::Request*> m_requests;
403  };
404  typedef std::list<ActiveSession> ActiveSessionList;
406 };
407 
408 }; // namespace ArgusSamples
409 
410 #endif // DISPATCHER_H