Argus Camera Sample Argus Camera Sample

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Dispatcher.cpp
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 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdarg.h>
32 #include <assert.h>
33
34 #include <sstream>
35 #include <limits>
36
37 #include "Dispatcher.h"
38 #include "InitOnce.h"
39 #include "UniquePointer.h"
40 #include "Error.h"
41 #include "Util.h"
42 #include "Renderer.h"
43 #include "Validator.h"
44 #include <Argus/Ext/BayerSharpnessMap.h>
45 #include <Argus/Ext/DebugCaptureMetadata.h>
46 #include <Argus/Ext/DebugCaptureSession.h>
47 #include <Argus/Ext/DeFog.h>
48 #include <Argus/Ext/FaceDetect.h>
49 #include <Argus/Ext/SensorPrivateMetadata.h>
50
51 namespace ArgusSamples
52 {
53
54 /**
55 * An observer for an Argus interface.
56 */
57 class IObserverForInterface : public IObserver
58 {
59 public:
60 virtual ~IObserverForInterface() { };
61
62 /**
63 * Check if this is the observer for the given interface.
64 *
65 * @param interface [in]
66 */
67 virtual bool isInterface(Argus::Interface *interface) const = 0;
68 };
69
70 /**
71 * Denoise settings observer. Update Argus denoise settings when values change.
72 */
73 class DenoiseSettingsObserver : public IObserverForInterface
74 {
75 public:
76 DenoiseSettingsObserver(Argus::IDenoiseSettings *iDenoiseSettings)
77 : m_iDenoiseSettings(iDenoiseSettings)
78 {
79 Dispatcher &dispatcher = Dispatcher::getInstance();
80
81 PROPAGATE_ERROR_CONTINUE(dispatcher.m_denoiseMode.registerObserver(this,
82 static_cast<IObserver::CallbackFunction>(
83 &DenoiseSettingsObserver::onDenoiseModeChanged)));
84 PROPAGATE_ERROR_CONTINUE(dispatcher.m_denoiseStrength.registerObserver(this,
85 static_cast<IObserver::CallbackFunction>(
86 &DenoiseSettingsObserver::onDenoiseStrengthChanged)));
87 }
88
89 virtual ~DenoiseSettingsObserver()
90 {
91 Dispatcher &dispatcher = Dispatcher::getInstance();
92
93 PROPAGATE_ERROR_CONTINUE(dispatcher.m_denoiseMode.unregisterObserver(this,
94 static_cast<IObserver::CallbackFunction>(
95 &DenoiseSettingsObserver::onDenoiseModeChanged)));
96 PROPAGATE_ERROR_CONTINUE(dispatcher.m_denoiseStrength.unregisterObserver(this,
97 static_cast<IObserver::CallbackFunction>(
98 &DenoiseSettingsObserver::onDenoiseStrengthChanged)));
99 }
100
101 virtual bool isInterface(Argus::Interface *interface) const
102 {
103 return (interface == m_iDenoiseSettings);
104 }
105
106 private:
107 bool onDenoiseModeChanged(const Observed &source)
108 {
109 Dispatcher &dispatcher = Dispatcher::getInstance();
110
111 assert(&source == &dispatcher.m_denoiseMode);
112
113 if (m_iDenoiseSettings->setDenoiseMode(dispatcher.m_denoiseMode.get()) != Argus::STATUS_OK)
114 ORIGINATE_ERROR("Failed to set the denoising mode");
115
116 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
117
118 return true;
119 }
120
121 bool onDenoiseStrengthChanged(const Observed &source)
122 {
123 Dispatcher &dispatcher = Dispatcher::getInstance();
124
125 assert(&source == &dispatcher.m_denoiseStrength);
126
127 if (m_iDenoiseSettings->setDenoiseStrength(dispatcher.m_denoiseStrength.get()) !=
128 Argus::STATUS_OK)
129 {
130 ORIGINATE_ERROR("Failed to set the denoise strength");
131 }
132
133 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
134
135 return true;
136 }
137
138 Argus::IDenoiseSettings *m_iDenoiseSettings;
139 };
140
141 /**
142 * Edge enhancement settings observer. Update Argus edge enhance settings when values change.
143 */
144 class EdgeEnhanceSettingsObserver : public IObserverForInterface
145 {
146 public:
147 EdgeEnhanceSettingsObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings)
148 : m_iEdgeEnhanceSettings(iEdgeEnhanceSettings)
149 {
150 Dispatcher &dispatcher = Dispatcher::getInstance();
151
152 PROPAGATE_ERROR_CONTINUE(dispatcher.m_edgeEnhanceMode.registerObserver(this,
153 static_cast<IObserver::CallbackFunction>(
154 &EdgeEnhanceSettingsObserver::onEdgeEnhanceModeChanged)));
155 PROPAGATE_ERROR_CONTINUE(dispatcher.m_edgeEnhanceStrength.registerObserver(this,
156 static_cast<IObserver::CallbackFunction>(
157 &EdgeEnhanceSettingsObserver::onEdgeEnhanceStrengthChanged)));
158 }
159
160 virtual ~EdgeEnhanceSettingsObserver()
161 {
162 Dispatcher &dispatcher = Dispatcher::getInstance();
163
164 PROPAGATE_ERROR_CONTINUE(dispatcher.m_edgeEnhanceMode.unregisterObserver(this,
165 static_cast<IObserver::CallbackFunction>(
166 &EdgeEnhanceSettingsObserver::onEdgeEnhanceModeChanged)));
167 PROPAGATE_ERROR_CONTINUE(dispatcher.m_edgeEnhanceStrength.unregisterObserver(this,
168 static_cast<IObserver::CallbackFunction>(
169 &EdgeEnhanceSettingsObserver::onEdgeEnhanceStrengthChanged)));
170 }
171
172 virtual bool isInterface(Argus::Interface *interface) const
173 {
174 return (interface == m_iEdgeEnhanceSettings);
175 }
176
177 private:
178 bool onEdgeEnhanceModeChanged(const Observed &source)
179 {
180 Dispatcher &dispatcher = Dispatcher::getInstance();
181
182 assert(&source == &dispatcher.m_edgeEnhanceMode);
183
184 if (m_iEdgeEnhanceSettings->setEdgeEnhanceMode(dispatcher.m_edgeEnhanceMode.get())
185 != Argus::STATUS_OK)
186 {
187 ORIGINATE_ERROR("Failed to set the edge enhancement mode");
188 }
189
190 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
191
192 return true;
193 }
194
195 bool onEdgeEnhanceStrengthChanged(const Observed &source)
196 {
197 Dispatcher &dispatcher = Dispatcher::getInstance();
198
199 assert(&source == &dispatcher.m_edgeEnhanceStrength);
200
201 if (m_iEdgeEnhanceSettings->setEdgeEnhanceStrength(dispatcher.m_edgeEnhanceStrength.get())
202 != Argus::STATUS_OK)
203 {
204 ORIGINATE_ERROR("Failed to set the edge enhancement strength");
205 }
206
207 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
208
209 return true;
210 }
211
212 Argus::IEdgeEnhanceSettings *m_iEdgeEnhanceSettings;
213 };
214
215 /**
216 * Video stabilization settings observer. Update Argus video stabilization settings when changed.
217 */
218 class VideoStabilizationSettingsObserver : public IObserverForInterface
219 {
220 public:
221 VideoStabilizationSettingsObserver(Argus::IVideoStabilizationSettings *iVStabSettings)
222 : m_iVideoStabilizationSettings(iVStabSettings)
223 {
224 Dispatcher &dispatcher = Dispatcher::getInstance();
225
226 PROPAGATE_ERROR_CONTINUE(dispatcher.m_vstabMode.registerObserver(this,
227 static_cast<IObserver::CallbackFunction>(
228 &VideoStabilizationSettingsObserver::onVideoStabilizationModeChanged)));
229 }
230
231 virtual ~VideoStabilizationSettingsObserver()
232 {
233 Dispatcher &dispatcher = Dispatcher::getInstance();
234
235 PROPAGATE_ERROR_CONTINUE(dispatcher.m_vstabMode.unregisterObserver(this,
236 static_cast<IObserver::CallbackFunction>(
237 &VideoStabilizationSettingsObserver::onVideoStabilizationModeChanged)));
238 }
239
240 virtual bool isInterface(Argus::Interface *interface) const
241 {
242 return (interface == m_iVideoStabilizationSettings);
243 }
244
245 private:
246 bool onVideoStabilizationModeChanged(const Observed &source)
247 {
248 Dispatcher &dispatcher = Dispatcher::getInstance();
249
250 assert(&source == &dispatcher.m_vstabMode);
251
252 if (m_iVideoStabilizationSettings->setVideoStabilizationMode(dispatcher.m_vstabMode.get())
253 != Argus::STATUS_OK)
254 {
255 ORIGINATE_ERROR("Failed to set the video stabilization mode");
256 }
257
258 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
259
260 return true;
261 }
262
263 Argus::IVideoStabilizationSettings *m_iVideoStabilizationSettings;
264 };
265
266 /**
267 * Source settings observer. Update Argus source settings if values which are set through the
268 * source settings change.
269 */
270 class SourceSettingsObserver : public IObserverForInterface
271 {
272 public:
273 SourceSettingsObserver(Argus::ISourceSettings *iSourceSettings)
274 : m_iSourceSettings(iSourceSettings)
275 {
276 Dispatcher &dispatcher = Dispatcher::getInstance();
277
278 PROPAGATE_ERROR_CONTINUE(dispatcher.m_exposureTimeRange.registerObserver(this,
279 static_cast<IObserver::CallbackFunction>(
280 &SourceSettingsObserver::onExposureTimeRangeChanged)));
281 PROPAGATE_ERROR_CONTINUE(dispatcher.m_gainRange.registerObserver(this,
282 static_cast<IObserver::CallbackFunction>(
283 &SourceSettingsObserver::onGainRangeChanged)));
284 PROPAGATE_ERROR_CONTINUE(dispatcher.m_sensorModeIndex.registerObserver(this,
285 static_cast<IObserver::CallbackFunction>(
286 &SourceSettingsObserver::onSensorModeChanged)));
287 PROPAGATE_ERROR_CONTINUE(dispatcher.m_frameRate.registerObserver(this,
288 static_cast<IObserver::CallbackFunction>(
289 &SourceSettingsObserver::onFrameRateChanged)));
290 PROPAGATE_ERROR_CONTINUE(dispatcher.m_focusPosition.registerObserver(this,
291 static_cast<IObserver::CallbackFunction>(
292 &SourceSettingsObserver::onFocusPositionChanged)));
293 }
294
295 virtual ~SourceSettingsObserver()
296 {
297 Dispatcher &dispatcher = Dispatcher::getInstance();
298
299 PROPAGATE_ERROR_CONTINUE(dispatcher.m_exposureTimeRange.unregisterObserver(this,
300 static_cast<IObserver::CallbackFunction>(
301 &SourceSettingsObserver::onExposureTimeRangeChanged)));
302 PROPAGATE_ERROR_CONTINUE(dispatcher.m_gainRange.unregisterObserver(this,
303 static_cast<IObserver::CallbackFunction>(
304 &SourceSettingsObserver::onGainRangeChanged)));
305 PROPAGATE_ERROR_CONTINUE(dispatcher.m_sensorModeIndex.unregisterObserver(this,
306 static_cast<IObserver::CallbackFunction>(
307 &SourceSettingsObserver::onSensorModeChanged)));
308 PROPAGATE_ERROR_CONTINUE(dispatcher.m_frameRate.unregisterObserver(this,
309 static_cast<IObserver::CallbackFunction>(
310 &SourceSettingsObserver::onFrameRateChanged)));
311 PROPAGATE_ERROR_CONTINUE(dispatcher.m_focusPosition.unregisterObserver(this,
312 static_cast<IObserver::CallbackFunction>(
313 &SourceSettingsObserver::onFocusPositionChanged)));
314 }
315
316 virtual bool isInterface(Argus::Interface *interface) const
317 {
318 return (interface == m_iSourceSettings);
319 }
320
321 private:
322 bool onExposureTimeRangeChanged(const Observed &source)
323 {
324 Dispatcher &dispatcher = Dispatcher::getInstance();
325
326 assert(&source == &dispatcher.m_exposureTimeRange);
327
328 if (m_iSourceSettings->setExposureTimeRange(dispatcher.m_exposureTimeRange.get()) !=
329 Argus::STATUS_OK)
330 {
331 ORIGINATE_ERROR("Failed to set exposure time range");
332 }
333
334 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
335
336 return true;
337 }
338
339 bool onGainRangeChanged(const Observed &source)
340 {
341 Dispatcher &dispatcher = Dispatcher::getInstance();
342
343 assert(&source == &dispatcher.m_gainRange);
344
345 if (m_iSourceSettings->setGainRange(dispatcher.m_gainRange.get()) != Argus::STATUS_OK)
346 ORIGINATE_ERROR("Failed to set gain range");
347
348 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
349
350 return true;
351 }
352
353 bool onSensorModeChanged(const Observed &source)
354 {
355 Dispatcher &dispatcher = Dispatcher::getInstance();
356
357 assert(&source == &dispatcher.m_sensorModeIndex);
358
359 Argus::SensorMode *sensorMode = NULL;
360 PROPAGATE_ERROR(dispatcher.getSensorMode(dispatcher.m_sensorModeIndex.get(), &sensorMode));
361
362 if (m_iSourceSettings->setSensorMode(sensorMode) != Argus::STATUS_OK)
363 ORIGINATE_ERROR("Failed to set sensor mode");
364
365 PROPAGATE_ERROR(dispatcher.restartActiveRequests());
366
367 return true;
368 }
369
370 bool onFocusPositionChanged(const Observed &source)
371 {
372 Dispatcher &dispatcher = Dispatcher::getInstance();
373
374 assert(&source == &dispatcher.m_focusPosition);
375
376 if (m_iSourceSettings->setFocusPosition(dispatcher.m_focusPosition.get()) !=
377 Argus::STATUS_OK)
378 {
379 ORIGINATE_ERROR("Failed to set focus position");
380 }
381
382 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
383
384 return true;
385 }
386
387 bool onFrameRateChanged(const Observed &source)
388 {
389 Dispatcher &dispatcher = Dispatcher::getInstance();
390
391 assert(&source == &dispatcher.m_frameRate);
392
393 Argus::Range<uint64_t> frameDurationRangeNs(0);
394
395 if (dispatcher.m_frameRate.get() == 0.0f)
396 {
397 // a frame rate of zero means VFR, get the sensor frame duration and apply it to
398 // the source
399 Argus::SensorMode *sensorMode = NULL;
400 PROPAGATE_ERROR(dispatcher.getSensorMode(dispatcher.m_sensorModeIndex.get(),
401 &sensorMode));
402
403 Argus::ISensorMode *iSensorMode =
404 Argus::interface_cast<Argus::ISensorMode>(sensorMode);
405
406 frameDurationRangeNs = iSensorMode->getFrameDurationRange();
407 }
408 else
409 {
410 // frame rate is frames per second, frameduration is in nanoseconds
411 frameDurationRangeNs =
412 TimeValue::fromCycelsPerSec(dispatcher.m_frameRate.get()).toNSec();
413 }
414
415 if (m_iSourceSettings->setFrameDurationRange(frameDurationRangeNs) != Argus::STATUS_OK)
416 ORIGINATE_ERROR("Failed to set frame duration range");
417
418 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
419
420 return true;
421 }
422
423 Argus::ISourceSettings *m_iSourceSettings;
424 };
425
426 /**
427 * Auto control settings observer. Update Argus auto control settings if values which are set
428 * through the auto control settings change.
429 */
430 class AutoControlSettingsObserver : public IObserverForInterface
431 {
432 public:
433 AutoControlSettingsObserver(Argus::IAutoControlSettings *iAutoControlSettings)
434 : m_iAutoControlSettings(iAutoControlSettings)
435 {
436 Dispatcher &dispatcher = Dispatcher::getInstance();
437
438 PROPAGATE_ERROR_CONTINUE(dispatcher.m_aeAntibandingMode.registerObserver(this,
439 static_cast<IObserver::CallbackFunction>(
440 &AutoControlSettingsObserver::onAeAntibandingModeChanged)));
441 PROPAGATE_ERROR_CONTINUE(dispatcher.m_aeLock.registerObserver(this,
442 static_cast<IObserver::CallbackFunction>(
443 &AutoControlSettingsObserver::onAeLockChanged)));
444 PROPAGATE_ERROR_CONTINUE(dispatcher.m_awbLock.registerObserver(this,
445 static_cast<IObserver::CallbackFunction>(
446 &AutoControlSettingsObserver::onAwbLockChanged)));
447 PROPAGATE_ERROR_CONTINUE(dispatcher.m_awbMode.registerObserver(this,
448 static_cast<IObserver::CallbackFunction>(
449 &AutoControlSettingsObserver::onAwbModeChanged)));
450 PROPAGATE_ERROR_CONTINUE(dispatcher.m_exposureCompensation.registerObserver(this,
451 static_cast<IObserver::CallbackFunction>(
452 &AutoControlSettingsObserver::onExposureCompensationChanged)));
453 }
454
455 virtual ~AutoControlSettingsObserver()
456 {
457 Dispatcher &dispatcher = Dispatcher::getInstance();
458
459 PROPAGATE_ERROR_CONTINUE(dispatcher.m_aeAntibandingMode.unregisterObserver(this,
460 static_cast<IObserver::CallbackFunction>(
461 &AutoControlSettingsObserver::onAeAntibandingModeChanged)));
462 PROPAGATE_ERROR_CONTINUE(dispatcher.m_aeLock.unregisterObserver(this,
463 static_cast<IObserver::CallbackFunction>(
464 &AutoControlSettingsObserver::onAeLockChanged)));
465 PROPAGATE_ERROR_CONTINUE(dispatcher.m_awbLock.unregisterObserver(this,
466 static_cast<IObserver::CallbackFunction>(
467 &AutoControlSettingsObserver::onAwbLockChanged)));
468 PROPAGATE_ERROR_CONTINUE(dispatcher.m_awbMode.unregisterObserver(this,
469 static_cast<IObserver::CallbackFunction>(
470 &AutoControlSettingsObserver::onAwbModeChanged)));
471 PROPAGATE_ERROR_CONTINUE(dispatcher.m_exposureCompensation.unregisterObserver(this,
472 static_cast<IObserver::CallbackFunction>(
473 &AutoControlSettingsObserver::onExposureCompensationChanged)));
474 }
475
476 virtual bool isInterface(Argus::Interface *interface) const
477 {
478 return (interface == m_iAutoControlSettings);
479 }
480
481 private:
482 bool onAeAntibandingModeChanged(const Observed &source)
483 {
484 Dispatcher &dispatcher = Dispatcher::getInstance();
485
486 assert(&source == &dispatcher.m_aeAntibandingMode);
487
488 if (m_iAutoControlSettings->setAeAntibandingMode(dispatcher.m_aeAntibandingMode.get()) !=
489 Argus::STATUS_OK)
490 {
491 ORIGINATE_ERROR("Failed to set the AE antibanding mode");
492 }
493
494 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
495
496 return true;
497 }
498
499 bool onAeLockChanged(const Observed &source)
500 {
501 Dispatcher &dispatcher = Dispatcher::getInstance();
502
503 assert(&source == &dispatcher.m_aeLock);
504
505 if (m_iAutoControlSettings->setAeLock(dispatcher.m_aeLock.get()) != Argus::STATUS_OK)
506 ORIGINATE_ERROR("Failed to set the AE lock");
507
508 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
509
510 return true;
511 }
512
513 bool onAwbLockChanged(const Observed &source)
514 {
515 Dispatcher &dispatcher = Dispatcher::getInstance();
516
517 assert(&source == &dispatcher.m_awbLock);
518
519 if (m_iAutoControlSettings->setAwbLock(dispatcher.m_awbLock.get()) != Argus::STATUS_OK)
520 ORIGINATE_ERROR("Failed to set the AWB lock");
521
522 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
523
524 return true;
525 }
526
527 bool onAwbModeChanged(const Observed &source)
528 {
529 Dispatcher &dispatcher = Dispatcher::getInstance();
530
531 assert(&source == &dispatcher.m_awbMode);
532
533 if (m_iAutoControlSettings->setAwbMode(dispatcher.m_awbMode.get()) != Argus::STATUS_OK)
534 ORIGINATE_ERROR("Failed to set the AWB mode");
535
536 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
537
538 return true;
539 }
540
541 bool onExposureCompensationChanged(const Observed &source)
542 {
543 Dispatcher &dispatcher = Dispatcher::getInstance();
544
545 assert(&source == &dispatcher.m_exposureCompensation);
546
547 if (m_iAutoControlSettings->setExposureCompensation(
548 dispatcher.m_exposureCompensation.get()) != Argus::STATUS_OK)
549 {
550 ORIGINATE_ERROR("Failed to set the exposure compensation");
551 }
552
553 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
554
555 return true;
556 }
557
558 Argus::IAutoControlSettings *m_iAutoControlSettings;
559 };
560
561 /**
562 * DeFog settings observer. Update Argus DeFog settings if values which are set through the
563 * DeFog settings change.
564 */
565 class DeFogSettingsObserver : public IObserverForInterface
566 {
567 public:
568 DeFogSettingsObserver(Argus::Ext::IDeFogSettings *iDeFogSettings)
569 : m_iDeFogSettings(iDeFogSettings)
570 {
571 Dispatcher &dispatcher = Dispatcher::getInstance();
572
573 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogEnable.registerObserver(this,
574 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogEnableChanged)));
575 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogAmount.registerObserver(this,
576 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogAmountChanged)));
577 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogQuality.registerObserver(this,
578 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogQualityChanged)));
579 }
580
581 virtual ~DeFogSettingsObserver()
582 {
583 Dispatcher &dispatcher = Dispatcher::getInstance();
584
585 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogEnable.unregisterObserver(this,
586 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogEnableChanged)));
587 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogAmount.unregisterObserver(this,
588 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogAmountChanged)));
589 PROPAGATE_ERROR_CONTINUE(dispatcher.m_deFogQuality.unregisterObserver(this,
590 static_cast<IObserver::CallbackFunction>(&DeFogSettingsObserver::onDeFogQualityChanged)));
591 }
592
593 virtual bool isInterface(Argus::Interface *interface) const
594 {
595 return (interface == m_iDeFogSettings);
596 }
597
598 private:
599 bool onDeFogEnableChanged(const Observed &source)
600 {
601 Dispatcher &dispatcher = Dispatcher::getInstance();
602
603 assert(&source == &dispatcher.m_deFogEnable);
604
605 m_iDeFogSettings->setDeFogEnable(dispatcher.m_deFogEnable.get());
606
607 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
608
609 return true;
610 }
611
612 bool onDeFogAmountChanged(const Observed &source)
613 {
614 Dispatcher &dispatcher = Dispatcher::getInstance();
615
616 assert(&source == &dispatcher.m_deFogAmount);
617
618 if (m_iDeFogSettings->setDeFogAmount(dispatcher.m_deFogAmount.get()) != Argus::STATUS_OK)
619 ORIGINATE_ERROR("Failed to set the DeFog amount");
620
621 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
622
623 return true;
624 }
625
626 bool onDeFogQualityChanged(const Observed &source)
627 {
628 Dispatcher &dispatcher = Dispatcher::getInstance();
629
630 assert(&source == &dispatcher.m_deFogQuality);
631
632 if (m_iDeFogSettings->setDeFogQuality(dispatcher.m_deFogQuality.get()) != Argus::STATUS_OK)
633 ORIGINATE_ERROR("Failed to set the DeFog quality");
634
635 PROPAGATE_ERROR(Dispatcher::getInstance().restartActiveRequests());
636
637 return true;
638 }
639
640 Argus::Ext::IDeFogSettings *m_iDeFogSettings;
641 };
642
643 // valid denoise modes
644 static const ValidatorEnum<Argus::DenoiseMode>::ValueStringPair s_denoiseModes[] =
645 {
646 { Argus::DENOISE_MODE_OFF, "off" },
647 { Argus::DENOISE_MODE_FAST, "fast" },
648 { Argus::DENOISE_MODE_HIGH_QUALITY, "highquality" }
649 };
650
651 // valid edge enhance modes
652 static const ValidatorEnum<Argus::EdgeEnhanceMode>::ValueStringPair s_edgeEnhanceModes[] =
653 {
654 { Argus::EDGE_ENHANCE_MODE_OFF, "off" },
655 { Argus::EDGE_ENHANCE_MODE_FAST, "fast" },
656 { Argus::EDGE_ENHANCE_MODE_HIGH_QUALITY, "highquality" }
657 };
658
659 // valid vstab modes
660 static const ValidatorEnum<Argus::VideoStabilizationMode>::ValueStringPair s_vstabModes[] =
661 {
662 { Argus::VIDEO_STABILIZATION_MODE_OFF, "off" },
663 { Argus::VIDEO_STABILIZATION_MODE_ON, "on" }
664 };
665
666 // valid AE antibanding modes
667 static const ValidatorEnum<Argus::AeAntibandingMode>::ValueStringPair s_aeAntibandingModes[] =
668 {
669 { Argus::AE_ANTIBANDING_MODE_OFF, "off" },
670 { Argus::AE_ANTIBANDING_MODE_AUTO, "auto" },
671 { Argus::AE_ANTIBANDING_MODE_50HZ, "50hz" },
672 { Argus::AE_ANTIBANDING_MODE_60HZ, "60hz" }
673 };
674
675 // valid AWB modes
676 static const ValidatorEnum<Argus::AwbMode>::ValueStringPair s_awbModes[] =
677 {
678 { Argus::AWB_MODE_OFF, "off" },
679 { Argus::AWB_MODE_AUTO, "auto" },
680 { Argus::AWB_MODE_INCANDESCENT, "incandescent" },
681 { Argus::AWB_MODE_FLUORESCENT, "fluorescent" },
682 { Argus::AWB_MODE_WARM_FLUORESCENT, "warmfluorescent" },
683 { Argus::AWB_MODE_DAYLIGHT, "daylight" },
684 { Argus::AWB_MODE_CLOUDY_DAYLIGHT, "cloudydaylight" },
685 { Argus::AWB_MODE_TWILIGHT, "twilight" },
686 { Argus::AWB_MODE_SHADE, "shade" },
687 { Argus::AWB_MODE_MANUAL, "manual" }
688 };
689
690 // valid video formats
691 static const ValidatorEnum<VideoPipeline::VideoFormat>::ValueStringPair s_videoFormats[] =
692 {
693 { VideoPipeline::VIDEO_FORMAT_H263, "h263" },
694 { VideoPipeline::VIDEO_FORMAT_H264, "h264" },
695 { VideoPipeline::VIDEO_FORMAT_H265, "h265" },
696 { VideoPipeline::VIDEO_FORMAT_VP8, "vp8" },
697 { VideoPipeline::VIDEO_FORMAT_MPEG4, "mpeg4" }
698 };
699
700 // valid video file types
701 static const ValidatorEnum<VideoPipeline::VideoFileType>::ValueStringPair s_videoFileTypes[] =
702 {
703 { VideoPipeline::VIDEO_FILE_TYPE_MP4, "mp4" },
704 { VideoPipeline::VIDEO_FILE_TYPE_3GP, "3gp" },
705 { VideoPipeline::VIDEO_FILE_TYPE_AVI, "avi" },
706 { VideoPipeline::VIDEO_FILE_TYPE_MKV, "mkv" },
707 { VideoPipeline::VIDEO_FILE_TYPE_H265, "h265" }
708 };
709
710 static const Argus::Size s_outputSizes[] =
711 {
712 Argus::Size(0, 0), // if size is 0,0 take the current sensor size
713 Argus::Size(176, 144), // QCIF
714 Argus::Size(320, 240),
715 Argus::Size(640, 480),
716 Argus::Size(1280, 720), // 720p HDTV
717 Argus::Size(1920, 1080), // 1080p HDTV
718 Argus::Size(3840, 2160), // 2160p 4K UHDTV
719 };
720
721 Dispatcher::Dispatcher()
722 : m_deviceFocusPositionRange(0)
723 , m_sensorExposureTimeRange(Argus::Range<uint64_t>(0))
724 , m_sensorAnalogGainRange(Argus::Range<float>(0.0f))
725 , m_sensorFrameRateRange(0.0f)
726 , m_deviceIndex(new ValidatorStdVector<uint32_t, Argus::CameraDevice*>(&m_cameraDevices), 0)
727 , m_deviceOpen(false)
728 , m_verbose(false)
729 , m_kpi(false)
730 , m_exposureTimeRange(new ValidatorRange<Argus::Range<uint64_t> >(&m_sensorExposureTimeRange),
731 Argus::Range<uint64_t>(0))
732 , m_gainRange(new ValidatorRange<Argus::Range<float > >(&m_sensorAnalogGainRange),
733 Argus::Range<float>(0.0f))
734 , m_sensorModeIndex(new ValidatorEnum<uint32_t>(), 0)
735 , m_frameRate(new ValidatorRange<float>(&m_sensorFrameRateRange), 0.0f)
736 , m_focusPosition(new ValidatorRange<int32_t>(&m_deviceFocusPositionRange), 0)
737 , m_denoiseMode(new ValidatorEnum<Argus::DenoiseMode>(
738 s_denoiseModes, sizeof(s_denoiseModes) / sizeof(s_denoiseModes[0])),
739 Argus::DENOISE_MODE_OFF)
740 , m_denoiseStrength(new ValidatorRange<float>(0.0f, 1.0f), 1.0f)
741 , m_edgeEnhanceMode(new ValidatorEnum<Argus::EdgeEnhanceMode>(
742 s_edgeEnhanceModes, sizeof(s_edgeEnhanceModes) / sizeof(s_edgeEnhanceModes[0])),
743 Argus::EDGE_ENHANCE_MODE_OFF)
744 , m_edgeEnhanceStrength(new ValidatorRange<float>(0.0f, 1.0f), 1.0f)
745 , m_vstabMode(new ValidatorEnum<Argus::VideoStabilizationMode>(
746 s_vstabModes, sizeof(s_vstabModes) / sizeof(s_vstabModes[0])),
747 Argus::VIDEO_STABILIZATION_MODE_OFF)
748 , m_aeAntibandingMode(new ValidatorEnum<Argus::AeAntibandingMode>(
749 s_aeAntibandingModes, sizeof(s_aeAntibandingModes) / sizeof(s_aeAntibandingModes[0])),
750 Argus::AE_ANTIBANDING_MODE_AUTO)
751 , m_aeLock(false)
752 , m_awbLock(false)
753 , m_awbMode(new ValidatorEnum<Argus::AwbMode>(
754 s_awbModes, sizeof(s_awbModes) / sizeof(s_awbModes[0])),
755 Argus::AWB_MODE_AUTO)
756 , m_exposureCompensation(new ValidatorRange<float>(-10.0f, 10.0f), 0.0f)
757 , m_videoFormat(new ValidatorEnum<VideoPipeline::VideoFormat>(
758 s_videoFormats, sizeof(s_videoFormats) / sizeof(s_videoFormats[0])),
759 VideoPipeline::VIDEO_FORMAT_H264)
760 , m_videoFileType(new ValidatorEnum<VideoPipeline::VideoFileType>(
761 s_videoFileTypes, sizeof(s_videoFileTypes) / sizeof(s_videoFileTypes[0])),
762 VideoPipeline::VIDEO_FILE_TYPE_MP4)
763 , m_videoBitRate(new ValidatorRange<uint32_t>(0, std::numeric_limits<uint32_t>::max()), 0)
764 , m_outputSize(new ValidatorSize(s_outputSizes,
765 sizeof(s_outputSizes) / sizeof(s_outputSizes[0]), true /*allowArbitrarySizes*/),
766 Argus::Size(0, 0))
767 , m_outputPath(".")
768 , m_deFogEnable(false)
769 , m_deFogAmount(new ValidatorRange<float>(0.0f, 1.0f), 0.9f)
770 , m_deFogQuality(new ValidatorRange<float>(0.0f, 1.0f), 0.14285f)
771 , m_initialized(false)
772 , m_iCameraProvider(NULL)
773 {
774 PROPAGATE_ERROR_CONTINUE(initialize());
775 }
776
777 Dispatcher::~Dispatcher()
778 {
779 if (!shutdown())
780 REPORT_ERROR("Failed to shutdown");
781 }
782
783 Dispatcher &Dispatcher::getInstance()
784 {
785 static InitOnce initOnce;
786 static Dispatcher instance;
787
788 if (initOnce.begin())
789 {
790 if (instance.initialize())
791 {
792 initOnce.complete();
793 }
794 else
795 {
796 initOnce.failed();
797 REPORT_ERROR("Initalization failed");
798 }
799 }
800
801 return instance;
802 }
803
804 bool Dispatcher::initialize()
805 {
806 if (m_initialized)
807 return true;
808
809 // Create the CameraProvider object and obtain its interface.
810 m_cameraProvider = Argus::UniqueObj<Argus::CameraProvider>(Argus::CameraProvider::create());
811 m_iCameraProvider = Argus::interface_cast<Argus::ICameraProvider>(m_cameraProvider);
812 if (!m_iCameraProvider)
813 ORIGINATE_ERROR("Failed to create CameraProvider");
814
815 // Get the camera devices
816 m_iCameraProvider->getCameraDevices(&m_cameraDevices);
817 if (m_cameraDevices.size() == 0)
818 {
819 PROPAGATE_ERROR(shutdown());
820 ORIGINATE_ERROR("No cameras available");
821 }
822
823 m_initialized = true;
824
825 // register the device index observer after 'm_initialize' is set, the call back will be
826 // called immediately and assert that 'm_initialize' is set
827 PROPAGATE_ERROR_CONTINUE(m_deviceIndex.registerObserver(this,
828 static_cast<IObserver::CallbackFunction>(&Dispatcher::onDeviceIndexChanged)));
829 PROPAGATE_ERROR_CONTINUE(m_sensorModeIndex.registerObserver(this,
830 static_cast<IObserver::CallbackFunction>(&Dispatcher::onSensorModeIndexChanged)));
831
832 return true;
833 }
834
835 bool Dispatcher::shutdown()
836 {
837 PROPAGATE_ERROR_CONTINUE(closeSession());
838
839 m_cameraProvider.reset();
840 m_cameraDevices.clear();
841
842 return true;
843 }
844
845 bool Dispatcher::onDeviceIndexChanged(const Observed &source)
846 {
847 assert(static_cast<const Value<uint32_t>&>(source).get() == m_deviceIndex);
848 assert(m_initialized);
849
850 // close the currently open device
851 if (m_deviceOpen)
852 {
853 PROPAGATE_ERROR(m_deviceOpen.set(false));
854
855 PROPAGATE_ERROR(closeSession());
856
857 // reset the current device properties
858 }
859
860 // open the new device
861 const Argus::ICameraProperties *iCameraProperties =
862 Argus::interface_cast<Argus::ICameraProperties>(m_cameraDevices[m_deviceIndex]);
863 if (!iCameraProperties)
864 ORIGINATE_ERROR("Failed to get ICameraProperties interface");
865
866 // get the sensor modes
867 if (iCameraProperties->getSensorModes(&m_sensorModes) != Argus::STATUS_OK)
868 ORIGINATE_ERROR("Failed to get sensor modes");
869
870 if (m_sensorModes.size() == 0)
871 ORIGINATE_ERROR("No sensor modes found");
872
873 // get the focus position range
874 PROPAGATE_ERROR(m_deviceFocusPositionRange.set(iCameraProperties->getFocusPositionRange()));
875
876 // add value/string pairs for each sensor mode index
877 std::vector<ValidatorEnum<uint32_t>::ValueStringPair> valueStringPairs;
878 valueStringPairs.resize(m_sensorModes.size());
879 for (size_t index = 0; index < m_sensorModes.size(); ++index)
880 {
881 Argus::ISensorMode *sensorMode =
882 Argus::interface_cast<Argus::ISensorMode>(m_sensorModes[index]);
883
884 valueStringPairs[index].value = (uint32_t)index;
885
886 std::ostringstream stream;
887 stream << index+1 << ": "
888 << sensorMode->getResolution().width << "x" << sensorMode->getResolution().height;
889
890 valueStringPairs[index].string = stream.str();
891 }
892 // update the validator with the new value/string pairs
893 ValidatorEnum<uint32_t> *validator =
894 static_cast<ValidatorEnum<uint32_t>*>(m_sensorModeIndex.getValidator());
895 PROPAGATE_ERROR(validator->setValidValues(valueStringPairs.data(), valueStringPairs.size()));
896
897 // set the sensor mode index (force notify observer because the sensor modes are different now
898 // although the sensor mode index could be the same)
899 PROPAGATE_ERROR(m_sensorModeIndex.set(0, true /*forceNotify*/));
900
901 PROPAGATE_ERROR(m_deviceOpen.set(true));
902
903 return true;
904 }
905
906 bool Dispatcher::onSensorModeIndexChanged(const Observed &source)
907 {
908 assert(static_cast<const Value<uint32_t>&>(source).get() == m_sensorModeIndex);
909 assert(m_initialized);
910
911 Argus::ISensorMode *iSensorMode =
912 Argus::interface_cast<Argus::ISensorMode>(m_sensorModes[m_sensorModeIndex.get()]);
913 if (!iSensorMode)
914 ORIGINATE_ERROR("Failed to get ISensorMode interface");
915
916 // get new limits
917 Argus::Range<uint64_t> sensorExposureTimeRange = iSensorMode->getExposureTimeRange();
918 Argus::Range<float> sensorAnalogGainRange = iSensorMode->getAnalogGainRange();
919 Argus::Range<TimeValue> sensorFrameDurationRange(
920 TimeValue::fromNSec(iSensorMode->getFrameDurationRange().min),
921 TimeValue::fromNSec(iSensorMode->getFrameDurationRange().max));
922 Argus::Range<float> sensorFrameRateRange(
923 sensorFrameDurationRange.max.toCyclesPerSec(),
924 sensorFrameDurationRange.min.toCyclesPerSec());
925
926 // set ranges to unified range (to avoid errors when setting new values)
927 Argus::Range<uint64_t> unifiedSensorExposureTimeRange(0);
928 unifiedSensorExposureTimeRange.min =
929 std::min(m_sensorExposureTimeRange.get().min.min, sensorExposureTimeRange.min);
930 unifiedSensorExposureTimeRange.max =
931 std::max(m_sensorExposureTimeRange.get().max.max, sensorExposureTimeRange.max);
932 Argus::Range<float> unifiedSensorAnalogGainRange(0);
933 unifiedSensorAnalogGainRange.min =
934 std::min(m_sensorAnalogGainRange.get().min.min, sensorAnalogGainRange.min);
935 unifiedSensorAnalogGainRange.max =
936 std::max(m_sensorAnalogGainRange.get().max.max, sensorAnalogGainRange.max);
937 Argus::Range<float> unifiedSensorFrameRateRange(0.0f);
938 unifiedSensorFrameRateRange.min =
939 std::min(m_sensorFrameRateRange.get().min, sensorFrameRateRange.min);
940 unifiedSensorFrameRateRange.max =
941 std::max(m_sensorFrameRateRange.get().max, sensorFrameRateRange.max);
942
943 PROPAGATE_ERROR(m_sensorExposureTimeRange.set(
944 Argus::Range<Argus::Range<uint64_t> >(unifiedSensorExposureTimeRange)));
945 PROPAGATE_ERROR(m_sensorAnalogGainRange.set(
946 Argus::Range<Argus::Range<float> >(unifiedSensorAnalogGainRange)));
947 PROPAGATE_ERROR(m_sensorFrameRateRange.set(unifiedSensorFrameRateRange));
948
949 // update dependent values
950 PROPAGATE_ERROR(m_exposureTimeRange.set(sensorExposureTimeRange));
951 PROPAGATE_ERROR(m_gainRange.set(sensorAnalogGainRange));
952 PROPAGATE_ERROR(m_frameRate.set(sensorFrameRateRange.max));
953
954 // set to final ranges
955 PROPAGATE_ERROR(m_sensorExposureTimeRange.set(Argus::Range<Argus::Range<uint64_t> >(
956 sensorExposureTimeRange, sensorExposureTimeRange)));
957 PROPAGATE_ERROR(m_sensorAnalogGainRange.set(Argus::Range<Argus::Range<float> >(
958 sensorAnalogGainRange, sensorAnalogGainRange)));
959 PROPAGATE_ERROR(m_sensorFrameRateRange.set(sensorFrameRateRange));
960
961 return true;
962 }
963
964 bool Dispatcher::supportsExtension(const Argus::ExtensionName& extension) const
965 {
966 return m_iCameraProvider->supportsExtension(extension);
967 }
968
969 bool Dispatcher::getInfo(std::string &info) const
970 {
971 std::ostringstream stream;
972
973 assert(m_initialized);
974
975 stream << "Argus extensions:" << std::endl;
976 stream << " BayerSharpnessMap: " <<
977 (supportsExtension(Argus::EXT_BAYER_SHARPNESS_MAP) ?
978 "supported" : "not supported") << std::endl;
979 stream << " DebugCaptureMetadata: " <<
980 (supportsExtension(Argus::EXT_DEBUG_CAPTURE_METADATA) ?
981 "supported" : "not supported") << std::endl;
982 stream << " DebugCaptureSession: " <<
983 (supportsExtension(Argus::EXT_DEBUG_CAPTURE_SESSION) ?
984 "supported" : "not supported") << std::endl;
985 stream << " DeFog: " <<
986 (supportsExtension(Argus::EXT_DE_FOG) ?
987 "supported" : "not supported") << std::endl;
988 stream << " FaceDetect: " <<
989 (supportsExtension(Argus::EXT_FACE_DETECT) ?
990 "supported" : "not supported") << std::endl;
991 stream << " SensorPrivateMetadata: " <<
992 (supportsExtension(Argus::EXT_SENSOR_PRIVATE_METADATA) ?
993 "supported" : "not supported") << std::endl;
994
995 stream << "Number of camera devices: " << m_cameraDevices.size() << std::endl;
996
997 for (uint32_t deviceIndex = 0; deviceIndex < m_cameraDevices.size(); ++deviceIndex)
998 {
999 stream << "Device: " << deviceIndex << std::endl;
1000
1001 const Argus::ICameraProperties *iCameraProperties =
1002 Argus::interface_cast<Argus::ICameraProperties>(m_cameraDevices[deviceIndex]);
1003 if (!iCameraProperties)
1004 ORIGINATE_ERROR("Failed to get ICameraProperties interface");
1005
1006 stream << " Max AE Regions: " <<
1007 iCameraProperties->getMaxAeRegions() << std::endl;
1008 stream << " Max AWB Regions: " <<
1009 iCameraProperties->getMaxAwbRegions() << std::endl;
1010 stream << " Focus Position Range: " <<
1011 iCameraProperties->getFocusPositionRange().min << " - " <<
1012 iCameraProperties->getFocusPositionRange().max << std::endl;
1013 stream << " Lens Aperture Range: " <<
1014 iCameraProperties->getLensApertureRange().min << " - " <<
1015 iCameraProperties->getLensApertureRange().max << std::endl;
1016
1017 // print the sensor modes
1018 std::vector<Argus::SensorMode*> sensorModes;
1019 iCameraProperties->getSensorModes(&sensorModes);
1020 stream << " Number of sensor modes: " << sensorModes.size() << std::endl;
1021 for (uint32_t sensorModeIndex = 0; sensorModeIndex < sensorModes.size(); ++sensorModeIndex)
1022 {
1023 Argus::ISensorMode *sensorMode =
1024 Argus::interface_cast<Argus::ISensorMode>(sensorModes[sensorModeIndex]);
1025 if (!sensorMode)
1026 ORIGINATE_ERROR("Failed to get ISensorMode interface");
1027
1028 // convert ns to fps
1029 float maximum_fps = TimeValue::fromNSec(
1030 sensorMode->getFrameDurationRange().min).toCyclesPerSec();
1031 float minimum_fps = TimeValue::fromNSec(
1032 sensorMode->getFrameDurationRange().max).toCyclesPerSec();
1033
1034 stream << " Sensor mode: " << sensorModeIndex << std::endl;
1035 stream << " Resolution: " <<
1036 sensorMode->getResolution().width << "x" <<
1037 sensorMode->getResolution().height << std::endl;
1038 stream << " Exposure time range: " <<
1039 sensorMode->getExposureTimeRange().min << " - " <<
1040 sensorMode->getExposureTimeRange().max << " ns" << std::endl;
1041 stream << " Frame duration range: " <<
1042 sensorMode->getFrameDurationRange().min << " - " <<
1043 sensorMode->getFrameDurationRange().max << " ns" << std::endl;
1044 stream << " Framerate range: " <<
1045 minimum_fps << " - " <<
1046 maximum_fps << " fps" << std::endl;
1047 stream << " Analog gain range: " <<
1048 sensorMode->getAnalogGainRange().min << " - " <<
1049 sensorMode->getAnalogGainRange().max << std::endl;
1050 stream << std::endl;
1051 }
1052 }
1053
1054 info = stream.str();
1055
1056 return true;
1057 }
1058
1059 bool Dispatcher::getSensorMode(uint32_t sensorModeIndex, Argus::SensorMode **sensorMode) const
1060 {
1061 assert(m_deviceOpen);
1062
1063 if (sensorModeIndex >= m_sensorModes.size())
1064 ORIGINATE_ERROR("Invalid sensor mode index");
1065
1066 *sensorMode = m_sensorModes[sensorModeIndex];
1067
1068 return true;
1069 }
1070
1071 Argus::Range<int32_t> Dispatcher::getDeviceFocusPositionRange() const
1072 {
1073 return m_deviceFocusPositionRange.get();
1074 }
1075
1076 uint32_t Dispatcher::getDeviceCount() const
1077 {
1078 assert(m_deviceOpen);
1079
1080 return m_cameraDevices.size();
1081 }
1082
1083 bool Dispatcher::createSession(Argus::UniqueObj<Argus::CaptureSession> &session,
1084 uint32_t deviceIndex)
1085 {
1086 assert(m_deviceOpen);
1087
1088 if (deviceIndex > m_cameraDevices.size())
1089 ORIGINATE_ERROR("Invalid device index");
1090
1091 // close the internal session, it might be using the device which will be used by the new
1092 // session
1093 PROPAGATE_ERROR(closeSession());
1094
1095 // create the new capture session
1096 session = Argus::UniqueObj<Argus::CaptureSession>(
1097 m_iCameraProvider->createCaptureSession(m_cameraDevices[deviceIndex]));
1098 if (!session)
1099 ORIGINATE_ERROR("Failed to create CaptureSession");
1100
1101 return true;
1102 }
1103
1104 bool Dispatcher::waitForEvents(Argus::EventQueue *eventQueue, TimeValue timeout,
1105 Argus::CaptureSession *session)
1106 {
1107 if (!session)
1108 {
1109 // use the internal session
1110 PROPAGATE_ERROR(createSession());
1111 session = m_captureSession.get();
1112 }
1113
1114 Argus::IEventProvider *iEventProvider = Argus::interface_cast<Argus::IEventProvider>(session);
1115 if (!iEventProvider)
1116 ORIGINATE_ERROR("Failed to get iEventProvider interface");
1117
1118 // wait for the events
1119 const Argus::Status status = iEventProvider->waitForEvents(eventQueue, timeout.toNSec());
1120 if ((status != Argus::STATUS_OK) && (status != Argus::STATUS_TIMEOUT))
1121 ORIGINATE_ERROR("Failed to get events");
1122
1123 return true;
1124 }
1125
1126 /**
1127 * Create the internal session
1128 */
1129 bool Dispatcher::createSession()
1130 {
1131 if (m_captureSession)
1132 return true;
1133
1134 PROPAGATE_ERROR(createSession(m_captureSession, m_deviceIndex));
1135
1136 return true;
1137 }
1138
1139 /**
1140 * Close the internal session
1141 */
1142 bool Dispatcher::closeSession()
1143 {
1144 /// @todo store session with active requests, just clear now when the session is closed
1145 m_activeRequests.clear();
1146
1147 m_captureSession.reset();
1148
1149 return true;
1150 }
1151
1152 bool Dispatcher::createRequest(Argus::UniqueObj<Argus::Request> &request,
1153 Argus::CaptureIntent captureIntent, Argus::CaptureSession *session)
1154 {
1155 if (!session)
1156 {
1157 // use the internal session
1158 PROPAGATE_ERROR(createSession());
1159 session = m_captureSession.get();
1160 }
1161
1162 Argus::ICaptureSession *iCaptureSession =
1163 Argus::interface_cast<Argus::ICaptureSession>(session);
1164 if (!iCaptureSession)
1165 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1166
1167 // Create request
1168 Argus::UniqueObj<Argus::Request> newRequest =
1169 Argus::UniqueObj<Argus::Request>(iCaptureSession->createRequest(captureIntent));
1170 if (!newRequest)
1171 ORIGINATE_ERROR("Failed to create request");
1172
1173 // Setup request
1174 Argus::IRequest *iRequest = Argus::interface_cast<Argus::IRequest>(newRequest);
1175 if (!iRequest)
1176 ORIGINATE_ERROR("Failed to get IRequest interface");
1177
1178 // get source settings interface
1179 Argus::ISourceSettings *iSourceSettings =
1180 Argus::interface_cast<Argus::ISourceSettings>(iRequest->getSourceSettings());
1181 if (!iSourceSettings)
1182 ORIGINATE_ERROR("Failed to get ISourceSettings interface");
1183
1184 // register the source settings observer
1185 PROPAGATE_ERROR(registerObserver(iSourceSettings));
1186
1187 // get auto control settings interface
1188 Argus::IAutoControlSettings *iAutoControlSettings =
1189 Argus::interface_cast<Argus::IAutoControlSettings>(iRequest->getAutoControlSettings());
1190 if (!iAutoControlSettings)
1191 ORIGINATE_ERROR("Failed to get IAutoControlSettings interface");
1192
1193 // register the auto control settings observer
1194 PROPAGATE_ERROR(registerObserver(iAutoControlSettings));
1195
1196 // register denoise settings interface
1197 Argus::IDenoiseSettings *iDenoiseSettings =
1198 Argus::interface_cast<Argus::IDenoiseSettings>(newRequest);
1199 if (!iDenoiseSettings)
1200 ORIGINATE_ERROR("Failed to get IDenoiseSettings interface");
1201 PROPAGATE_ERROR(registerObserver(iDenoiseSettings));
1202
1203 // register edge enhance settings interface
1204 Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings =
1205 Argus::interface_cast<Argus::IEdgeEnhanceSettings>(newRequest);
1206 if (!iEdgeEnhanceSettings)
1207 ORIGINATE_ERROR("Failed to get IEdgeEnhanceSettings interface");
1208 PROPAGATE_ERROR(registerObserver(iEdgeEnhanceSettings));
1209
1210 // register video stabilization settings interface
1211 Argus::IVideoStabilizationSettings *iVideoStabilizationSettings =
1212 Argus::interface_cast<Argus::IVideoStabilizationSettings>(newRequest);
1213 if (!iVideoStabilizationSettings)
1214 ORIGINATE_ERROR("Failed to get IVideoStabilizationSettings interface");
1215 PROPAGATE_ERROR(registerObserver(iVideoStabilizationSettings));
1216
1217 if (supportsExtension(Argus::EXT_DE_FOG))
1218 {
1219 // get DeFog settings interface
1220 Argus::Ext::IDeFogSettings *iDeFogSettings =
1221 Argus::interface_cast<Argus::Ext::IDeFogSettings>(newRequest);
1222 if (iDeFogSettings)
1223 {
1224 // register the DeFog settings observer
1225 PROPAGATE_ERROR(registerObserver(iDeFogSettings));
1226 }
1227 }
1228
1229 request.reset(newRequest.release());
1230
1231 return true;
1232 }
1233
1234 bool Dispatcher::destroyRequest(Argus::UniqueObj<Argus::Request> &request)
1235 {
1236 Argus::IRequest *iRequest = Argus::interface_cast<Argus::IRequest>(request.get());
1237 if (!iRequest)
1238 ORIGINATE_ERROR("Failed to get IRequest interface");
1239
1240 // get source settings interface
1241 Argus::ISourceSettings *iSourceSettings =
1242 Argus::interface_cast<Argus::ISourceSettings>(iRequest->getSourceSettings());
1243 if (!iSourceSettings)
1244 ORIGINATE_ERROR("Failed to get ISourceSettings interface");
1245
1246 // unregister the source settings observer
1247 PROPAGATE_ERROR(unregisterObserver(iSourceSettings));
1248
1249 // get auto control settings interface
1250 Argus::IAutoControlSettings *iAutoControlSettings =
1251 Argus::interface_cast<Argus::IAutoControlSettings>(iRequest->getAutoControlSettings());
1252 if (!iAutoControlSettings)
1253 ORIGINATE_ERROR("Failed to get IAutoControlSettings interface");
1254
1255 // unregister the auto control settings observer
1256 PROPAGATE_ERROR(unregisterObserver(iAutoControlSettings));
1257
1258 // unregister denoise settings interface
1259 Argus::IDenoiseSettings *iDenoiseSettings =
1260 Argus::interface_cast<Argus::IDenoiseSettings>(request);
1261 if (!iDenoiseSettings)
1262 ORIGINATE_ERROR("Failed to get IDenoiseSettings interface");
1263 PROPAGATE_ERROR(unregisterObserver(iDenoiseSettings));
1264
1265 // unregister edge enhance settings interface
1266 Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings =
1267 Argus::interface_cast<Argus::IEdgeEnhanceSettings>(request);
1268 if (!iEdgeEnhanceSettings)
1269 ORIGINATE_ERROR("Failed to get IEdgeEnhanceSettings interface");
1270 PROPAGATE_ERROR(unregisterObserver(iEdgeEnhanceSettings));
1271
1272 // unregister video stabilization settings interface
1273 Argus::IVideoStabilizationSettings *iVideoStabilizationSettings =
1274 Argus::interface_cast<Argus::IVideoStabilizationSettings>(request);
1275 if (!iVideoStabilizationSettings)
1276 ORIGINATE_ERROR("Failed to get IVideoStabilizationSettings interface");
1277 PROPAGATE_ERROR(unregisterObserver(iVideoStabilizationSettings));
1278
1279 if (supportsExtension(Argus::EXT_DE_FOG))
1280 {
1281 // get DeFog settings interface
1282 Argus::Ext::IDeFogSettings *iDeFogSettings =
1283 Argus::interface_cast<Argus::Ext::IDeFogSettings>(request);
1284 if (iDeFogSettings)
1285 {
1286 // unregister the DeFog settings observer
1287 PROPAGATE_ERROR(unregisterObserver(iDeFogSettings));
1288 }
1289 }
1290
1291 request.reset();
1292
1293 return true;
1294 }
1295
1296 bool Dispatcher::createEventQueue(const std::vector<Argus::EventType> &eventTypes,
1297 Argus::UniqueObj<Argus::EventQueue>& eventQueue, Argus::CaptureSession *session)
1298 {
1299 if (!session)
1300 {
1301 // use the internal session
1302 PROPAGATE_ERROR(createSession());
1303 session = m_captureSession.get();
1304 }
1305
1306 Argus::IEventProvider *iEventProvider =
1307 Argus::interface_cast<Argus::IEventProvider>(session);
1308 if (!iEventProvider)
1309 ORIGINATE_ERROR("Failed to get IEventProvider interface");
1310
1311 Argus::EventQueue *newEventQueue = iEventProvider->createEventQueue(eventTypes);
1312 if (!newEventQueue)
1313 ORIGINATE_ERROR("Failed to create eventQueue");
1314
1315 eventQueue.reset(newEventQueue);
1316
1317 return true;
1318 }
1319
1320 bool Dispatcher::capture(Argus::Request *request, Argus::CaptureSession *session)
1321 {
1322 if (!session)
1323 {
1324 // use the internal session
1325 PROPAGATE_ERROR(createSession());
1326 session = m_captureSession.get();
1327 }
1328
1329 Argus::ICaptureSession *iCaptureSession =
1330 Argus::interface_cast<Argus::ICaptureSession>(session);
1331 if (!iCaptureSession)
1332 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1333
1334 if (iCaptureSession->capture(request, Argus::TIMEOUT_INFINITE) == 0)
1335 ORIGINATE_ERROR("Failed to submit the still capture request");
1336
1337 return true;
1338 }
1339
1340 bool Dispatcher::startRepeat(Argus::Request *request, Argus::CaptureSession *session)
1341 {
1342 if (!session)
1343 {
1344 // use the internal session
1345 PROPAGATE_ERROR(createSession());
1346 session = m_captureSession.get();
1347 }
1348
1349 Argus::ICaptureSession *iCaptureSession =
1350 Argus::interface_cast<Argus::ICaptureSession>(session);
1351 if (!iCaptureSession)
1352 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1353
1354 if (iCaptureSession->repeat(request) != Argus::STATUS_OK)
1355 ORIGINATE_ERROR("Failed to submit repeating capture request");
1356
1357 m_activeRequests.push_back(request);
1358
1359 return true;
1360 }
1361
1362 bool Dispatcher::startRepeatBurst(const std::vector<const Argus::Request*>& requestList,
1363 Argus::CaptureSession *session)
1364 {
1365 if (!session)
1366 {
1367 // use the internal session
1368 PROPAGATE_ERROR(createSession());
1369 session = m_captureSession.get();
1370 }
1371
1372 Argus::ICaptureSession *iCaptureSession =
1373 Argus::interface_cast<Argus::ICaptureSession>(session);
1374 if (!iCaptureSession)
1375 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1376
1377 if (iCaptureSession->repeatBurst(requestList) != Argus::STATUS_OK)
1378 ORIGINATE_ERROR("Failed to submit repeating burst request");
1379
1380 m_activeRequests.insert(m_activeRequests.end(), requestList.begin(), requestList.end());
1381
1382 return true;
1383 }
1384
1385 bool Dispatcher::stopRepeat(Argus::CaptureSession *session)
1386 {
1387 if (!session)
1388 {
1389 // use the internal session
1390 PROPAGATE_ERROR(createSession());
1391 session = m_captureSession.get();
1392 }
1393
1394 Argus::ICaptureSession *iCaptureSession =
1395 Argus::interface_cast<Argus::ICaptureSession>(session);
1396 if (!iCaptureSession)
1397 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1398
1399 iCaptureSession->stopRepeat();
1400
1401 m_activeRequests.clear();
1402
1403 return true;
1404 }
1405
1406 bool Dispatcher::restartActiveRequests()
1407 {
1408 if (!m_activeRequests.empty())
1409 {
1410 /// @todo store session with active requests list
1411 Argus::CaptureSession *session = m_captureSession.get();
1412
1413 Argus::ICaptureSession *iCaptureSession =
1414 Argus::interface_cast<Argus::ICaptureSession>(session);
1415 if (!iCaptureSession)
1416 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1417
1418 iCaptureSession->stopRepeat();
1419
1420 if (iCaptureSession->repeatBurst(m_activeRequests) != Argus::STATUS_OK)
1421 ORIGINATE_ERROR("Failed to submit repeating burst request");
1422 }
1423
1424 return true;
1425 }
1426
1427 uint32_t Dispatcher::maxBurstRequests(Argus::CaptureSession *session)
1428 {
1429 if (!session)
1430 {
1431 // use the internal session
1432 PROPAGATE_ERROR(createSession());
1433 session = m_captureSession.get();
1434 }
1435
1436 Argus::ICaptureSession *iCaptureSession =
1437 Argus::interface_cast<Argus::ICaptureSession>(session);
1438 if (!iCaptureSession)
1439 {
1440 REPORT_ERROR("Failed to get ICaptureSession interface");
1441 return 0;
1442 }
1443
1444 return iCaptureSession->maxBurstRequests();
1445 }
1446
1447 bool Dispatcher::waitForIdle(Argus::CaptureSession *session)
1448 {
1449 if (!session)
1450 {
1451 // use the internal session
1452 PROPAGATE_ERROR(createSession());
1453 session = m_captureSession.get();
1454 }
1455
1456 Argus::ICaptureSession *iCaptureSession =
1457 Argus::interface_cast<Argus::ICaptureSession>(session);
1458 if (!iCaptureSession)
1459 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1460
1461 if (iCaptureSession->waitForIdle() != Argus::STATUS_OK)
1462 ORIGINATE_ERROR("Waiting for idle failed");
1463
1464 return true;
1465 }
1466
1467 bool Dispatcher::getOutputSize(Argus::Size *size) const
1468 {
1469 // if width or height is 0 take the sensor size
1470 if ((m_outputSize.get().width == 0) || (m_outputSize.get().height == 0))
1471 {
1472 // the device needs to be open to get sensor modes
1473 assert(m_deviceOpen);
1474
1475 Argus::ISensorMode *sensorMode =
1476 Argus::interface_cast<Argus::ISensorMode>(m_sensorModes[m_sensorModeIndex.get()]);
1477 if (!sensorMode)
1478 ORIGINATE_ERROR("Failed to get ISensorMode interface");
1479 *size = sensorMode->getResolution();
1480 }
1481 else
1482 {
1483 *size = m_outputSize;
1484 }
1485
1486 return true;
1487 }
1488
1489 bool Dispatcher::createOutputStream(Argus::Request *request,
1490 Argus::UniqueObj<Argus::OutputStream> &stream, Argus::CaptureSession *session)
1491 {
1492 if (!session)
1493 {
1494 // use the internal session
1495 PROPAGATE_ERROR(createSession());
1496 session = m_captureSession.get();
1497 }
1498
1499 Argus::IRequest *iRequest = Argus::interface_cast<Argus::IRequest>(request);
1500 if (!iRequest)
1501 ORIGINATE_ERROR("Failed to get IRequest interface");
1502
1503 Argus::Size outputSize;
1504 PROPAGATE_ERROR(getOutputSize(&outputSize));
1505
1506 Argus::ICaptureSession *iCaptureSession =
1507 Argus::interface_cast<Argus::ICaptureSession>(session);
1508 if (!iCaptureSession)
1509 ORIGINATE_ERROR("Failed to get ICaptureSession interface");
1510
1511 Argus::UniqueObj<Argus::OutputStreamSettings> outputStreamSettings(
1512 iCaptureSession->createOutputStreamSettings());
1513 Argus::IOutputStreamSettings* iOutputStreamSettings =
1514 Argus::interface_cast<Argus::IOutputStreamSettings>(outputStreamSettings);
1515 if (!iOutputStreamSettings)
1516 ORIGINATE_ERROR("Failed to get IOutputStreamSettings interface");
1517
1518 iOutputStreamSettings->setPixelFormat(Argus::PIXEL_FMT_YCbCr_420_888);
1519 iOutputStreamSettings->setResolution(outputSize);
1520 iOutputStreamSettings->setEGLDisplay(Renderer::getInstance().getEGLDisplay());
1521
1522 Argus::UniqueObj<Argus::OutputStream> outputStream(
1523 iCaptureSession->createOutputStream(outputStreamSettings.get()));
1524 if (!outputStream)
1525 ORIGINATE_ERROR("Failed to create OutputStream");
1526
1527 stream.reset(outputStream.release());
1528
1529 return true;
1530 }
1531
1532 bool Dispatcher::enableOutputStream(Argus::Request *request, Argus::OutputStream *stream)
1533 {
1534 Argus::IRequest *iRequest = Argus::interface_cast<Argus::IRequest>(request);
1535 if (!iRequest)
1536 ORIGINATE_ERROR("Failed to get IRequest interface");
1537
1538 // Enable the stream
1539 if (iRequest->enableOutputStream(stream) != Argus::STATUS_OK)
1540 ORIGINATE_ERROR("Failed to enable the output stream");
1541
1542 return true;
1543 }
1544
1545 bool Dispatcher::disableOutputStream(Argus::Request *request, Argus::OutputStream *stream)
1546 {
1547 Argus::IRequest *iRequest = Argus::interface_cast<Argus::IRequest>(request);
1548 if (!iRequest)
1549 ORIGINATE_ERROR("Failed to get IRequest interface");
1550
1551 // Disable the stream
1552 if (iRequest->disableOutputStream(stream) != Argus::STATUS_OK)
1553 ORIGINATE_ERROR("Failed to disable the output stream");
1554
1555 return true;
1556 }
1557
1558 bool Dispatcher::registerObserver(Argus::IDenoiseSettings *iDenoiseSettings)
1559 {
1560 UniquePointer<DenoiseSettingsObserver> denoiseSettings;
1561
1562 denoiseSettings.reset(new DenoiseSettingsObserver(iDenoiseSettings));
1563 if (!denoiseSettings)
1564 ORIGINATE_ERROR("Out of memory");
1565
1566 m_observers.push_front(denoiseSettings.release());
1567 return true;
1568 }
1569
1570 bool Dispatcher::registerObserver(Argus::IEdgeEnhanceSettings *iEdgeEnhanceSettings)
1571 {
1572 UniquePointer<EdgeEnhanceSettingsObserver> edgeEnhanceSettings;
1573
1574 edgeEnhanceSettings.reset(new EdgeEnhanceSettingsObserver(iEdgeEnhanceSettings));
1575 if (!edgeEnhanceSettings)
1576 ORIGINATE_ERROR("Out of memory");
1577
1578 m_observers.push_front(edgeEnhanceSettings.release());
1579 return true;
1580 }
1581
1582 bool Dispatcher::registerObserver(Argus::IVideoStabilizationSettings *iVideoStabilizationSettings)
1583 {
1584 UniquePointer<VideoStabilizationSettingsObserver> vStabSettings;
1585
1586 vStabSettings.reset(new VideoStabilizationSettingsObserver(iVideoStabilizationSettings));
1587 if (!vStabSettings)
1588 ORIGINATE_ERROR("Out of memory");
1589
1590 m_observers.push_front(vStabSettings.release());
1591 return true;
1592 }
1593
1594 bool Dispatcher::registerObserver(Argus::ISourceSettings *iSourceSettings)
1595 {
1596 UniquePointer<SourceSettingsObserver> sourceSettings;
1597
1598 sourceSettings.reset(new SourceSettingsObserver(iSourceSettings));
1599 if (!sourceSettings)
1600 ORIGINATE_ERROR("Out of memory");
1601
1602 m_observers.push_front(sourceSettings.release());
1603 return true;
1604 }
1605
1606 bool Dispatcher::registerObserver(Argus::IAutoControlSettings *iAutoControlSettings)
1607 {
1608 UniquePointer<AutoControlSettingsObserver> autoControlSettings;
1609
1610 autoControlSettings.reset(new AutoControlSettingsObserver(iAutoControlSettings));
1611 if (!autoControlSettings)
1612 ORIGINATE_ERROR("Out of memory");
1613
1614 m_observers.push_front(autoControlSettings.release());
1615 return true;
1616 }
1617
1618 bool Dispatcher::registerObserver(Argus::Ext::IDeFogSettings *iDeFogSettings)
1619 {
1620 UniquePointer<DeFogSettingsObserver> deFogSettings;
1621
1622 deFogSettings.reset(new DeFogSettingsObserver(iDeFogSettings));
1623 if (!deFogSettings)
1624 ORIGINATE_ERROR("Out of memory");
1625
1626 m_observers.push_front(deFogSettings.release());
1627 return true;
1628 }
1629
1630 bool Dispatcher::unregisterObserver(Argus::Interface *interface)
1631 {
1632 for (std::list<IObserverForInterface*>::iterator it = m_observers.begin();
1633 it != m_observers.end(); ++it)
1634 {
1635 if ((*it)->isInterface(interface))
1636 {
1637 delete *it;
1638 m_observers.erase(it);
1639 return true;
1640 }
1641 }
1642
1643 ORIGINATE_ERROR("Observer not found");
1644 }
1645
1646 bool Dispatcher::message(const char *msg, ...)
1647 {
1648 if (m_verbose)
1649 {
1650 va_list list;
1651
1652 va_start(list, msg);
1653
1654 if (vprintf(msg, list) < 0)
1655 ORIGINATE_ERROR("Failed to print message");
1656
1657 va_end(list);
1658 }
1659
1660 return true;
1661 }
1662
1663 }; // namespace ArgusSamples