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-2019, 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  * Returns the range of aperture positions of the current device.
102  * The units are aperture positions.
103  */
104  Argus::Range<int32_t> getDeviceAperturePositionRange() const;
105 
106  /**
107  * Returns the range of aperture motor positions per second of the current device.
108  * The units are aperture motor steps/second.
109  */
110  Argus::Range<float> getDeviceApertureMotorSpeedRange() const;
111 
112  /**
113  * Get the output size
114  */
115  bool getOutputSize(Argus::Size2D<uint32_t> *size) const;
116 
117  /**
118  * Get the amount of available camera devices
119  */
120  uint32_t getDeviceCount() const;
121 
122  /**
123  * Create a capture session using the device index
124  */
125  bool createSession(TrackedUniqueObj<Argus::CaptureSession> &session, uint32_t deviceIndex);
126 
127  /**
128  * Wait for events from the specific session.
129  *
130  * @param eventQueue [in] event queue to transfer events to
131  * @param timeout [in] maximum time (in nanoseconds) to wait for new events.
132  * @param session [in] capture session (optional, if NULL the internal session is used)
133  */
134  bool waitForEvents(Argus::EventQueue *eventQueue, TimeValue timeout = TimeValue::infinite(),
135  Argus::CaptureSession *session = NULL);
136 
137  /**
138  * Dump runtime information for a session.
139  */
140  bool dumpSessionInfo() const;
141 
142  /**
143  * Create a request for a session
144  *
145  * @param request [out] created request
146  * @param captureIntent [in] capture intent
147  * @param session [in] capture session (optional, if NULL the internal session is used)
148  */
150  Argus::CaptureIntent captureIntent, Argus::CaptureSession *session = NULL);
151 
152  /**
153  * create a event queue
154  *
155  * @param eventTypes [in]
156  * @param eventQueue [out] created event queue
157  * @param session [in] capture session (optional, if NULL the internal session is used)
158  */
159  bool createEventQueue(const std::vector<Argus::EventType> &eventTypes,
160  Argus::UniqueObj<Argus::EventQueue> &eventQueue, Argus::CaptureSession *session = NULL);
161 
162 
163  /**
164  * Submits a single capture request for a session
165  *
166  * @param request [in] capture request
167  * @param session [in] capture session (optional, if NULL the internal session is used)
168  */
169  bool capture(Argus::Request *request, Argus::CaptureSession *session = NULL);
170 
171  /**
172  * Start a repeating request for a session
173  *
174  * @param request [in] request
175  * @param session [in] capture session (optional, if NULL the internal session is used)
176  */
177  bool startRepeat(Argus::Request *request, Argus::CaptureSession *session = NULL);
178 
179  /**
180  * Start a repeating burst request for a session
181  *
182  * @param requestList [in] burst request
183  * @param session [in] capture session (optional, if NULL the internal session is used)
184  */
185  bool startRepeatBurst(const std::vector<const Argus::Request*>& requestList,
186  Argus::CaptureSession *session = NULL);
187 
188  /**
189  * Clears the repeating request for a session
190  *
191  * @param session [in] capture session (optional, if NULL the internal session is used)
192  */
193  bool stopRepeat(Argus::CaptureSession *session = NULL);
194 
195  /**
196  * Restart the currently active requests to pick up changed settings.
197  */
198  bool restartActiveRequests();
199 
200  /**
201  * Returns the maximum number of capture requests for a session that can be included in a
202  * burst capture.
203  *
204  * @param session [in] capture session (optional, if NULL the internal session is used)
205  */
206  uint32_t maxBurstRequests(Argus::CaptureSession *session = NULL);
207 
208  /**
209  * Wait until all pending captures for a session are complete
210  *
211  * @param session [in] capture session (optional, if NULL the internal session is used)
212  */
213  bool waitForIdle(Argus::CaptureSession *session = NULL);
214 
215  /**
216  * Create an output stream of an request of a session.
217  *
218  * @param request [in] request
219  * @param enableMetadata [in] whether or not to enable meatdata for the stream
220  * @param stream [out] the created Argus output stream
221  * @param session [in] capture session (optional, if NULL the internal session is used)
222  */
223  bool createOutputStream(Argus::Request *request, bool enableMetadata,
224  Argus::UniqueObj<Argus::OutputStream> &stream,
225  Argus::CaptureSession *session = NULL);
226 
227  /**
228  * Enable an output stream of an request
229  */
230  bool enableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
231 
232  /**
233  * Disable an output stream of an request
234  */
235  bool disableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
236 
237  /**
238  * Output a message if verbose mode is enabled
239  */
240  bool message(const char *msg, ...);
241 
242 private:
243  // the current properties need to be initialized first, some Value<> members below use them
244  // for the validator
245 
246  // current device properties
247  std::vector<Argus::SensorMode*> m_sensorModes; ///< sensor modes
248  Value<Argus::Range<int32_t> > m_deviceFocusPositionRange; ///< device focus position range
249  Value<Argus::Range<int32_t> > m_deviceAperturePositionRange; ///< device aperture position range
250  std::vector<float> m_deviceApertureFnums; ///< device aperture fnum available values
251  Value<Argus::Range<float> > m_deviceApertureMotorSpeedRange; ///< device aperture motor speed range
252  Value<Argus::Range<float> > m_deviceExposureCompensationRange; ///< exposure compensation range
253  Value<Argus::Range<Argus::Range<float> > >
254  m_deviceIspDigitalGainRange; ///< device isp digital gain range
255 
256  // current sensor mode properties
257  Value<Argus::Range<Argus::Range<uint64_t> > >
258  m_sensorExposureTimeRange; ///< exposure time range
259  Value<Argus::Range<Argus::Range<float> > >
260  m_sensorAnalogGainRange; ///< analog gain range
261  Value<Argus::Range<float> >
262  m_sensorFrameRateRange; ///< frame rate range
263  Value<Argus::Range<Argus::Range<float> > >
264  m_sensorFrameRateRangeLimits; ///< frame rate range
265 
266 public:
267  Value<uint32_t> m_deviceIndex; ///< the device index
268  Value<bool> m_deviceOpen; ///< if set then the device is open
269  Value<bool> m_sensorModeValid;
270 
271  Value<bool> m_verbose; ///< if set verbose mode is enabled and messages are printed
272  Value<bool> m_kpi; ///< if set kpi mode is enabled and kpi number are printed
273 
274  // source settings
275  Value<Argus::Range<uint64_t> > m_exposureTimeRange; ///< exposure time range
276  Value<Argus::Range<float> > m_gainRange; ///< gain range
277  Value<uint32_t> m_sensorModeIndex; ///< the sensor mode index
278  Value<float> m_frameRate; ///< in frames per second
279  Value<Argus::Range<float> > m_frameRateRange; ///< range in frames per second
280  Value<int32_t> m_focusPosition; ///< focus position
281  Value<int32_t> m_aperturePosition; ///< aperture position
282  Value<float> m_apertureFnum; ///< aperture position
283  Value<float> m_apertureMotorSpeed; ///< aperture motor steps/second
284  Value<Argus::PixelFormat> m_captureYuvFormat; ///< NV12 vs. P016 YUV color depth
285 
286  // denoise settings
287  Value<Argus::DenoiseMode> m_denoiseMode; ///< denoise mode
288  Value<float> m_denoiseStrength; ///< denoise strength
289 
290  // edge enhance settings
291  Value<Argus::EdgeEnhanceMode> m_edgeEnhanceMode; ///< edge enhancement mode
292  Value<float> m_edgeEnhanceStrength; ///< edge enhancement strength
293 
294  // auto control settings
295  Value<Argus::AeAntibandingMode> m_aeAntibandingMode; ///< auto exposure antibanding mode
296  Value<bool> m_aeLock; ///< auto exposure lock
297  Value<bool> m_awbLock; ///< auto white balance lock
298  Value<Argus::AwbMode> m_awbMode; ///< auto white balance mode
299  Value<float> m_exposureCompensation;///< exposure compensation
300  Value<Argus::Range<float> > m_ispDigitalGainRange;///< ISP digital Gain Range
301  Value<Argus::Range<uint32_t> > m_acRegionHorizontal; ///< auto control region horizontal
302  Value<Argus::Range<uint32_t> > m_acRegionVertical; ///< auto control region vertical
303 
304  // still settings
305  Value<StillFileType> m_stillFileType; ///< the still image file format
306 
307  // video settings
308  Value<VideoPipeline::VideoFormat> m_videoFormat; ///< the video format
309  Value<VideoPipeline::VideoFileType> m_videoFileType; ///< the video file type
310  Value<uint32_t> m_videoBitRate; ///< the video bit rate
311  Value<VideoPipeline::VideoControlRateMode> m_videoControlRate; ///< bitrate control types
312  Value<bool> m_videoTwoPassCBREnable; ///< enable TwoPassCBR for encoding bit rate
313 
314  // output settings
315  Value<Argus::Size2D<uint32_t> > m_outputSize; ///< output size
316  Value<std::string> m_outputPath; ///< output path
317 
318  // DeFog extension
319  Value<bool> m_deFogEnable; ///< enable
320  Value<float> m_deFogAmount; ///< amount of fog to be removed. Range 0.0 - 1.0 (none - all)
321  Value<float> m_deFogQuality; ///< quality of the effect. Range 0.0 - 1.0 (low - high)
322 
323 private:
324  Dispatcher();
325  ~Dispatcher();
326 
327  // this is a singleton, hide copy constructor etc.
328  Dispatcher(const Dispatcher&);
330 
331  /**
332  * Initialize the dispatcher
333  */
334  bool initialize();
335 
336  /**
337  * Create the internal session
338  */
339  bool createSession();
340 
341  /**
342  * Close the internal session
343  */
344  bool closeSession();
345 
346  /**
347  * Start tracking a session
348  */
349  bool track(Argus::CaptureSession *session);
350 
351  /**
352  * No longer track a session
353  */
354  bool untrack(Argus::CaptureSession *session);
355 
356  /**
357  * Start tracking a request
358  */
359  bool track(Argus::Request *request);
360 
361  /**
362  * No longer track a request
363  */
364  bool untrack(Argus::Request *request);
365 
366  /**
367  * Register an IDenoiseSettings observer
368  */
369  bool registerObserver(Argus::IDenoiseSettings *iDenoiseSettings);
370 
371  /**
372  * Register an IEdgeEnhanceSettings observer
373  */
374  bool registerObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings);
375 
376  /**
377  * Register an ISourceSettings observer
378  */
379  bool registerObserver(Argus::ISourceSettings *iSourceSettings);
380 
381  /**
382  * Register an IAutoControlSettings observer
383  */
384  bool registerObserver(Argus::IAutoControlSettings *iAutoControlSettings);
385 
386  /**
387  * Register an IDeFogSettings observer
388  */
389  bool registerObserver(Argus::Ext::IDeFogSettings *iDeFogSettings);
390 
391  /**
392  * Unregister an interface which had been registered as an observer.
393  */
394  bool unregisterObserver(Argus::Interface *interface);
395 
396  /**
397  * Callback when the device index changes.
398  */
399  bool onDeviceIndexChanged(const Observed &source);
400 
401  /**
402  * Callback when the sensor mode index changes.
403  */
404  bool onSensorModeIndexChanged(const Observed &source);
405 
406  bool m_initialized; ///< if set the dispatcher is initialized
407 
408  std::list<IObserverForInterface*> m_observers;
409 
410  Argus::UniqueObj<Argus::CameraProvider> m_cameraProvider; ///< camera provider
411  Argus::ICameraProvider *m_iCameraProvider; ///< camera provider interface
412 
413  std::vector<Argus::CameraDevice*> m_cameraDevices; ///< a list of available devices
414 
416 
418  {
419  public:
420  ActiveSession(Argus::CaptureSession *session)
421  : m_session(session)
422  {
423  }
424 
425  Argus::CaptureSession *m_session;
426  std::vector<const Argus::Request*> m_requests;
427  };
428  typedef std::list<ActiveSession> ActiveSessionList;
430 };
431 
432 }; // namespace ArgusSamples
433 
434 #endif // DISPATCHER_H