Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventThread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 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 "EventThread.h"
30 #include "Dispatcher.h"
31 #include "Util.h"
32 #include "PerfTracker.h"
33 
34 #include <Argus/Ext/InternalFrameCount.h>
35 
36 namespace ArgusSamples {
37 
38 EventThread::EventThread(Argus::CaptureSession *session,
39  SessionPerfTracker *sessionPerfTracker)
40  : m_session(session)
41  , m_sessionPerfTracker(sessionPerfTracker)
42 {
43 }
44 
46 {
47 }
48 
50 {
51  std::vector<Argus::EventType> eventTypes;
52  eventTypes.push_back(Argus::EVENT_TYPE_CAPTURE_COMPLETE);
53  eventTypes.push_back(Argus::EVENT_TYPE_ERROR);
54 
55  PROPAGATE_ERROR(Dispatcher::getInstance().createEventQueue(eventTypes, m_eventQueue,
56  m_session));
57 
58  return true;
59 }
60 
62 {
63  Dispatcher &dispatcher = Dispatcher::getInstance();
64 
65  // wait for events (use a time out to allow the thread to be shutdown even if there are no
66  // new events)
67  PROPAGATE_ERROR(dispatcher.waitForEvents(m_eventQueue.get(), TimeValue::fromMSec(100),
68  m_session));
69 
70  Argus::IEventQueue *iEventQueue =
71  Argus::interface_cast<Argus::IEventQueue>(m_eventQueue.get());
72  if (!iEventQueue)
73  ORIGINATE_ERROR("Failed to get iEventQueue");
74 
75  for (uint32_t i = 0; i < iEventQueue->getSize(); i++)
76  {
77  const Argus::Event *event = iEventQueue->getEvent(i);
78  const Argus::IEvent *iEvent = Argus::interface_cast<const Argus::IEvent>(event);
79  if (!iEvent)
80  ORIGINATE_ERROR("Failed to get IEvent interface");
81 
82  if (iEvent->getEventType() == Argus::EVENT_TYPE_CAPTURE_COMPLETE)
83  {
85 
86  const Argus::IEventCaptureComplete *iEventCaptureComplete
87  = Argus::interface_cast<const Argus::IEventCaptureComplete>(event);
88  const Argus::CaptureMetadata *metaData = iEventCaptureComplete->getMetadata();
89  if (metaData)
90  {
91  const Argus::ICaptureMetadata *iCaptureMeta =
92  Argus::interface_cast<const Argus::ICaptureMetadata>(metaData);
93  if (iCaptureMeta)
94  {
95  const TimeValue latency =
96  TimeValue::fromNSec(iEvent->getTime()) -
97  TimeValue::fromNSec(iCaptureMeta->getSensorTimestamp());
98  PROPAGATE_ERROR(m_sessionPerfTracker->onEvent(
100 
101  // AF
102  std::vector< Argus::AcRegion > regions;
103  std::vector<float> sharpnessScore;
104  if (iCaptureMeta->getAfRegions(&regions) != Argus::STATUS_OK)
105  ORIGINATE_ERROR("Failed to get AF regions");
106 
107  if (iCaptureMeta->getSharpnessScore(&sharpnessScore) != Argus::STATUS_OK)
108  ORIGINATE_ERROR("Failed to get sharpness score");
109 
110  PROPAGATE_ERROR(dispatcher.message("Focus control info: focuser position %d ",
111  iCaptureMeta->getFocuserPosition()));
112  for (uint32_t j = 0; j < regions.size(); j++)
113  {
114  PROPAGATE_ERROR(dispatcher.message(" region %d %d %d %d, score %f ",
115  regions[j].left(), regions[j].top(), regions[j].right(),
116  regions[j].bottom(), sharpnessScore[j]));
117  }
118  PROPAGATE_ERROR(dispatcher.message("\n"));
119 
120  // bayerHistogram
121  Argus::Rectangle<uint32_t> region;
122  region = iCaptureMeta->getBayerHistogramRegion();
123  PROPAGATE_ERROR(dispatcher.message("BayerHistogram region %d %d %d %d, \n",
124  region.left(), region.top(), region.right(), region.bottom()));
125 
126  // Flicker
127  Argus::AeFlickerState state = iCaptureMeta->getFlickerState();
128  PROPAGATE_ERROR(dispatcher.message("Flicker state %s \n", state.getName()));
129 
130  PROPAGATE_ERROR(dispatcher.message("aperture info: aperture position %d \n",
131  iCaptureMeta->getAperturePosition()));
132  }
133 
134  const Argus::Ext::IInternalFrameCount *iInternalFrameCount =
135  Argus::interface_cast<const Argus::Ext::IInternalFrameCount>(metaData);
136  if (iInternalFrameCount)
137  {
138  const uint64_t currentFrameCount = iInternalFrameCount->getInternalFrameCount();
140  currentFrameCount));
141  }
142  }
143  }
144  else if (iEvent->getEventType() == Argus::EVENT_TYPE_ERROR)
145  {
146  ORIGINATE_ERROR("Error event received, the app should shutdown \n");
148 
149  // stop the repeating request
150  PROPAGATE_ERROR(dispatcher.stopRepeat());
151 
152  PROPAGATE_ERROR(dispatcher.waitForIdle());
154 
156 
158 
159  }
160  }
161 
162  return true;
163 }
164 
166 {
167  return true;
168 }
169 
170 }; // namespace ArgusSamples