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 step positions of the current device.
102  * The units are aperture motor steps.
103  */
104  Argus::Range<int32_t> getDeviceApertureMotorStepRange() const;
105 
106  /**
107  * Returns the range of aperture step positions 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_deviceApertureMotorStepRange; ///< device aperture motor step range
250  Value<Argus::Range<float> > m_deviceApertureMotorSpeedRange; ///< device aperture motor speed range
251  Value<Argus::Range<float> > m_deviceExposureCompensationRange; ///< exposure compensation range
252  Value<Argus::Range<Argus::Range<float> > >
253  m_deviceIspDigitalGainRange; ///< device isp digital gain range
254 
255  // current sensor mode properties
256  Value<Argus::Range<Argus::Range<uint64_t> > >
257  m_sensorExposureTimeRange; ///< exposure time range
258  Value<Argus::Range<Argus::Range<float> > >
259  m_sensorAnalogGainRange; ///< analog gain range
260  Value<Argus::Range<float> > m_sensorFrameRateRange; ///< frame rate range
261 
262 public:
263  Value<uint32_t> m_deviceIndex; ///< the device index
264  Value<bool> m_deviceOpen; ///< if set then the device is open
265  Value<bool> m_sensorModeValid;
266 
267 
268  Value<bool> m_verbose; ///< if set verbose mode is enabled and messages are printed
269 
270  Value<bool> m_kpi; ///< if set kpi mode is enabled and kpi number are printed
271 
272  // source settings
273  Value<Argus::Range<uint64_t> > m_exposureTimeRange; ///< exposure time range
274  Value<Argus::Range<float> > m_gainRange; ///< gain range
275  Value<uint32_t> m_sensorModeIndex; ///< the sensor mode index
276  Value<float> m_frameRate; ///< in frames per second
277  Value<int32_t> m_focusPosition; ///< focus position
278  Value<int32_t> m_apertureMotorStep; ///< aperture motor step position
279  Value<float> m_apertureMotorSpeed; ///< aperture motor steps/second
280  Value<Argus::PixelFormat> m_captureYuvFormat; ///< NV12 vs. P016 YUV color depth
281 
282  // denoise settings
283  Value<Argus::DenoiseMode> m_denoiseMode; ///< denoise mode
284  Value<float> m_denoiseStrength; ///< denoise strength
285 
286  // edge enhance settings
287  Value<Argus::EdgeEnhanceMode> m_edgeEnhanceMode; ///< edge enhancement mode
288  Value<float> m_edgeEnhanceStrength; ///< edge enhancement strength
289 
290  // auto control settings
291  Value<Argus::AeAntibandingMode> m_aeAntibandingMode; ///< auto exposure antibanding mode
292  Value<bool> m_aeLock; ///< auto exposure lock
293  Value<bool> m_awbLock; ///< auto white balance lock
294  Value<Argus::AwbMode> m_awbMode; ///< auto white balance mode
295  Value<float> m_exposureCompensation;///< exposure compensation
296  Value<Argus::Range<float> > m_ispDigitalGainRange;///< ISP digital Gain Range
297 
298  // still settings
299  Value<StillFileType> m_stillFileType; ///< the still image file format
300 
301  // video settings
302  Value<VideoPipeline::VideoFormat> m_videoFormat; ///< the video format
303  Value<VideoPipeline::VideoFileType> m_videoFileType; ///< the video file type
304  Value<uint32_t> m_videoBitRate; ///< the video bit rate
305 
306  // output settings
307  Value<Argus::Size2D<uint32_t> > m_outputSize; ///< output size
308  Value<std::string> m_outputPath; ///< output path
309 
310  // DeFog extension
311  Value<bool> m_deFogEnable; ///< enable
312  Value<float> m_deFogAmount; ///< amount of fog to be removed. Range 0.0 - 1.0 (none - all)
313  Value<float> m_deFogQuality; ///< quality of the effect. Range 0.0 - 1.0 (low - high)
314 
315 private:
316  Dispatcher();
317  ~Dispatcher();
318 
319  // this is a singleton, hide copy constructor etc.
320  Dispatcher(const Dispatcher&);
322 
323  /**
324  * Initialize the dispatcher
325  */
326  bool initialize();
327 
328  /**
329  * Create the internal session
330  */
331  bool createSession();
332 
333  /**
334  * Close the internal session
335  */
336  bool closeSession();
337 
338  /**
339  * Start tracking a session
340  */
341  bool track(Argus::CaptureSession *session);
342 
343  /**
344  * No longer track a session
345  */
346  bool untrack(Argus::CaptureSession *session);
347 
348  /**
349  * Start tracking a request
350  */
351  bool track(Argus::Request *request);
352 
353  /**
354  * No longer track a request
355  */
356  bool untrack(Argus::Request *request);
357 
358  /**
359  * Register an IDenoiseSettings observer
360  */
361  bool registerObserver(Argus::IDenoiseSettings *iDenoiseSettings);
362 
363  /**
364  * Register an IEdgeEnhanceSettings observer
365  */
366  bool registerObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings);
367 
368  /**
369  * Register an ISourceSettings observer
370  */
371  bool registerObserver(Argus::ISourceSettings *iSourceSettings);
372 
373  /**
374  * Register an IAutoControlSettings observer
375  */
376  bool registerObserver(Argus::IAutoControlSettings *iAutoControlSettings);
377 
378  /**
379  * Register an IDeFogSettings observer
380  */
381  bool registerObserver(Argus::Ext::IDeFogSettings *iDeFogSettings);
382 
383  /**
384  * Unregister an interface which had been registered as an observer.
385  */
386  bool unregisterObserver(Argus::Interface *interface);
387 
388  /**
389  * Callback when the device index changes.
390  */
391  bool onDeviceIndexChanged(const Observed &source);
392 
393  /**
394  * Callback when the sensor mode index changes.
395  */
396  bool onSensorModeIndexChanged(const Observed &source);
397 
398  bool m_initialized; ///< if set the dispatcher is initialized
399 
400  std::list<IObserverForInterface*> m_observers;
401 
402  Argus::UniqueObj<Argus::CameraProvider> m_cameraProvider; ///< camera provider
403  Argus::ICameraProvider *m_iCameraProvider; ///< camera provider interface
404 
405  std::vector<Argus::CameraDevice*> m_cameraDevices; ///< a list of available devices
406 
408 
410  {
411  public:
412  ActiveSession(Argus::CaptureSession *session)
413  : m_session(session)
414  {
415  }
416 
417  Argus::CaptureSession *m_session;
418  std::vector<const Argus::Request*> m_requests;
419  };
420  typedef std::list<ActiveSession> ActiveSessionList;
422 };
423 
424 }; // namespace ArgusSamples
425 
426 #endif // DISPATCHER_H