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 
44 namespace Argus { namespace Ext { class IDeFogSettings; } }
45 
46 namespace ArgusSamples
47 {
48 
49 class IObserverForInterface;
50 
51 /**
52  * The dispatcher is called by clients like the command line interface.
53  * It provides functions to set properties like the camera device index or the sensor mode. It
54  * also implements services to open devices, sessions and create requests.
55  * It also maintains an internal capture session which is used when only a single session is
56  * required.
57  */
58 class Dispatcher : public IObserver
59 {
60 public:
61  /**
62  * Get the dispatcher instance.
63  */
64  static Dispatcher &getInstance();
65 
66  /**
67  * Shutdown, free all resources
68  */
69  bool shutdown();
70 
71  /**
72  * Returns whether or not an extension is supported.
73  */
74  bool supportsExtension(const Argus::ExtensionName& extension) const;
75 
76  /**
77  * Get an information string
78  */
79  bool getInfo(std::string &info) const;
80 
81  /**
82  * Get the sensor mode for a given index
83  *
84  * @param sensorModeIndex [in]
85  * @param sensorMode [out]
86  */
87  bool getSensorMode(uint32_t sensorModeIndex, Argus::SensorMode **sensorMode) const;
88 
89  /**
90  * Returns the range of focuser positions of the current device.
91  * The units are focuser steps.
92  */
93  Argus::Range<int32_t> getDeviceFocusPositionRange() const;
94 
95  /**
96  * Get the output size
97  */
98  bool getOutputSize(Argus::Size *size) const;
99 
100  /**
101  * Get the amount of available camera devices
102  */
103  uint32_t getDeviceCount() const;
104 
105  /**
106  * Create a capture session using the device index
107  */
108  bool createSession(Argus::UniqueObj<Argus::CaptureSession> &session, uint32_t deviceIndex);
109 
110  /**
111  * Wait for events from the specific session.
112  *
113  * @param eventQueue [in] event queue to transfer events to
114  * @param timeout [in] maximum time (in nanoseconds) to wait for new events.
115  * @param session [in] capture session (optional, if NULL the internal session is used)
116  */
117  bool waitForEvents(Argus::EventQueue *eventQueue, TimeValue timeout = TimeValue::infinite(),
118  Argus::CaptureSession *session = NULL);
119 
120  /**
121  * Create a request for a session
122  *
123  * @param request [out] created request
124  * @param captureIntent [in] capture intent
125  * @param session [in] capture session (optional, if NULL the internal session is used)
126  */
127  bool createRequest(Argus::UniqueObj<Argus::Request> &request,
128  Argus::CaptureIntent captureIntent, Argus::CaptureSession *session = NULL);
129 
130  /**
131  * Destroy a request
132  *
133  * @param request [out] request to destroy
134  * */
135  bool destroyRequest(Argus::UniqueObj<Argus::Request> &request);
136 
137  /**
138  * create a event queue
139  *
140  * @param eventTypes [in]
141  * @param eventQueue [out] created event queue
142  * @param session [in] capture session (optional, if NULL the internal session is used)
143  */
144  bool createEventQueue(const std::vector<Argus::EventType> &eventTypes,
145  Argus::UniqueObj<Argus::EventQueue> &eventQueue, Argus::CaptureSession *session = NULL);
146 
147 
148  /**
149  * Submits a single capture request for a session
150  *
151  * @param request [in] capture request
152  * @param session [in] capture session (optional, if NULL the internal session is used)
153  */
154  bool capture(Argus::Request *request, Argus::CaptureSession *session = NULL);
155 
156  /**
157  * Start a repeating request for a session
158  *
159  * @param request [in] request
160  * @param session [in] capture session (optional, if NULL the internal session is used)
161  */
162  bool startRepeat(Argus::Request *request, Argus::CaptureSession *session = NULL);
163 
164  /**
165  * Start a repeating burst request for a session
166  *
167  * @param requestList [in] burst request
168  * @param session [in] capture session (optional, if NULL the internal session is used)
169  */
170  bool startRepeatBurst(const std::vector<const Argus::Request*>& requestList,
171  Argus::CaptureSession *session = NULL);
172 
173  /**
174  * Clears the repeating request for a session
175  *
176  * @param session [in] capture session (optional, if NULL the internal session is used)
177  */
178  bool stopRepeat(Argus::CaptureSession *session = NULL);
179 
180  /**
181  * Restart the currently active requests to pick up changed settings.
182  */
183  bool restartActiveRequests();
184 
185  /**
186  * Returns the maximum number of capture requests for a session that can be included in a
187  * burst capture.
188  *
189  * @param session [in] capture session (optional, if NULL the internal session is used)
190  */
191  uint32_t maxBurstRequests(Argus::CaptureSession *session = NULL);
192 
193  /**
194  * Wait until all pending captures for a session are complete
195  *
196  * @param session [in] capture session (optional, if NULL the internal session is used)
197  */
198  bool waitForIdle(Argus::CaptureSession *session = NULL);
199 
200  /**
201  * Create an output stream of an request of a session.
202  *
203  * @param request [in] request
204  * @param stream [out] the created Argus output stream
205  * @param session [in] capture session (optional, if NULL the internal session is used)
206  */
207  bool createOutputStream(Argus::Request *request, Argus::UniqueObj<Argus::OutputStream> &stream,
208  Argus::CaptureSession *session = NULL);
209 
210  /**
211  * Enable an output stream of an request
212  */
213  bool enableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
214 
215  /**
216  * Disable an output stream of an request
217  */
218  bool disableOutputStream(Argus::Request *request, Argus::OutputStream *stream);
219 
220  /**
221  * Output a message if verbose mode is enabled
222  */
223  bool message(const char *msg, ...);
224 
225 private:
226  // the current properties need to be initialized first, some Value<> members below use them
227  // for the validator
228 
229  // current device properties
230  std::vector<Argus::SensorMode*> m_sensorModes; ///< sensor modes
231  Value<Argus::Range<int32_t> > m_deviceFocusPositionRange; ///< device focus position range
232 
233  // current sensor mode properties
234  Value<Argus::Range<Argus::Range<uint64_t> > >
235  m_sensorExposureTimeRange; ///< exposure time range
236  Value<Argus::Range<Argus::Range<float> > >
237  m_sensorAnalogGainRange; ///< analog gain range
238  Value<Argus::Range<float> > m_sensorFrameRateRange; ///< frame rate range
239 
240 public:
241  Value<uint32_t> m_deviceIndex; ///< the device index
242  Value<bool> m_deviceOpen; ///< if set then the device is open
243 
244 
245  Value<bool> m_verbose; ///< if set verbose mode is enabled and messages are printed
246 
247  Value<bool> m_kpi; ///< if set kpi mode is enabled and kpi number are printed
248 
249  // source settings
250  Value<Argus::Range<uint64_t> > m_exposureTimeRange; ///< exposure time range
251  Value<Argus::Range<float> > m_gainRange; ///< gain range
252  Value<uint32_t> m_sensorModeIndex; ///< the sensor mode index
253  Value<float> m_frameRate; ///< in frames per second
254  Value<int32_t> m_focusPosition; ///< focus position
255 
256  // denoise settings
257  Value<Argus::DenoiseMode> m_denoiseMode; ///< denoise mode
258  Value<float> m_denoiseStrength; ///< denoise strength
259 
260  // edge enhance settings
261  Value<Argus::EdgeEnhanceMode> m_edgeEnhanceMode; ///< edge enhancement mode
262  Value<float> m_edgeEnhanceStrength; ///< edge enhancement strength
263 
264  // video stabilization settings
265  Value<Argus::VideoStabilizationMode> m_vstabMode; ///< video stabilization mode
266 
267  // auto control settings
268  Value<Argus::AeAntibandingMode> m_aeAntibandingMode; ///< auto exposure antibanding mode
269  Value<bool> m_aeLock; ///< auto exposure lock
270  Value<bool> m_awbLock; ///< auto white balance lock
271  Value<Argus::AwbMode> m_awbMode; ///< auto white balance mode
272  Value<float> m_exposureCompensation;///< exposure compensation
273 
274  // video settings
275  Value<VideoPipeline::VideoFormat> m_videoFormat; ///< the video format
276  Value<VideoPipeline::VideoFileType> m_videoFileType; ///< the video file type
277  Value<uint32_t> m_videoBitRate; ///< the video bit rate
278 
279  // output settings
280  Value<Argus::Size> m_outputSize; ///< output size
281  Value<std::string> m_outputPath; ///< output path
282 
283  // DeFog extension
284  Value<bool> m_deFogEnable; ///< enable
285  Value<float> m_deFogAmount; ///< amount of fog to be removed. Range 0.0 - 1.0 (none - all)
286  Value<float> m_deFogQuality; ///< quality of the effect. Range 0.0 - 1.0 (low - high)
287 
288 private:
289  Dispatcher();
290  ~Dispatcher();
291 
292  // this is a singleton, hide copy constructor etc.
293  Dispatcher(const Dispatcher&);
295 
296  /**
297  * Initialize the dispatcher
298  */
299  bool initialize();
300 
301  /**
302  * Create the session
303  */
304  bool createSession();
305  /**
306  * Close the session
307  */
308  bool closeSession();
309 
310  /**
311  * Register an IDenoiseSettings observer
312  */
313  bool registerObserver(Argus::IDenoiseSettings *iDenoiseSettings);
314 
315  /**
316  * Register an IEdgeEnhanceSettings observer
317  */
318  bool registerObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings);
319 
320  /**
321  * Register an IVideoStabilizationSettings observer
322  */
323  bool registerObserver(Argus::IVideoStabilizationSettings *iVideoStabilizationSettings);
324 
325  /**
326  * Register an ISourceSettings observer
327  */
328  bool registerObserver(Argus::ISourceSettings *iSourceSettings);
329 
330  /**
331  * Register an IAutoControlSettings observer
332  */
333  bool registerObserver(Argus::IAutoControlSettings *iAutoControlSettings);
334 
335  /**
336  * Register an IDeFogSettings observer
337  */
338  bool registerObserver(Argus::Ext::IDeFogSettings *iDeFogSettings);
339 
340  /**
341  * Unregister an interface which had been registered as an observer.
342  */
343  bool unregisterObserver(Argus::Interface *interface);
344 
345  /**
346  * Callback when the device index changes.
347  */
348  bool onDeviceIndexChanged(const Observed &source);
349 
350  /**
351  * Callback when the sensor mode index changes.
352  */
353  bool onSensorModeIndexChanged(const Observed &source);
354 
355  bool m_initialized; ///< if set the dispatcher is initialized
356 
357  std::list<IObserverForInterface*> m_observers;
358  std::vector<const Argus::Request*> m_activeRequests;
359 
360  Argus::UniqueObj<Argus::CameraProvider> m_cameraProvider; ///< camera provider
361  Argus::ICameraProvider *m_iCameraProvider; ///< camera provider interface
362 
363  std::vector<Argus::CameraDevice*> m_cameraDevices; ///< a list of available devices
364 
365  Argus::UniqueObj<Argus::CaptureSession> m_captureSession; ///< current capture setting
366 };
367 
368 }; // namespace ArgusSamples
369 
370 #endif // DISPATCHER_H