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
MultiSession.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2016-2017, 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 <sstream>
30
31 #include "MultiSession.h"
32 #include "Composer.h"
33 #include "Dispatcher.h"
34 #include "Error.h"
35 #include "UniquePointer.h"
36 #include "PerfTracker.h"
37
38 namespace ArgusSamples
39 {
40
41 TaskMultiSession::TaskMultiSession()
42 : m_initialized(false)
43 , m_running(false)
44 , m_prevRunning(false)
45 {
46 }
47
48 TaskMultiSession::~TaskMultiSession()
49 {
50 shutdown();
51 }
52
53 TaskMultiSession::Session::Session()
54 {
55 }
56
57 TaskMultiSession::Session::~Session()
58 {
59 shutdown();
60 }
61
62 bool TaskMultiSession::Session::initialize(uint32_t deviceIndex)
63 {
64 // create the perf tracker
65 m_perfTracker.reset(new SessionPerfTracker());
66 if (!m_perfTracker)
67 ORIGINATE_ERROR("Out of memory");
68
69 PROPAGATE_ERROR(m_perfTracker->onEvent(SESSION_EVENT_TASK_START));
70
71 Composer &composer = Composer::getInstance();
72 Dispatcher &dispatcher = Dispatcher::getInstance();
73
74 // create the session using the current device index
75 PROPAGATE_ERROR(dispatcher.createSession(m_session, deviceIndex));
76 PROPAGATE_ERROR(m_perfTracker->setSession(m_session.get()));
77
78 // create the request
79 PROPAGATE_ERROR(dispatcher.createRequest(m_request, Argus::CAPTURE_INTENT_STILL_CAPTURE,
80 m_session.get()));
81
82 // Create the preview stream
83 PROPAGATE_ERROR(dispatcher.createOutputStream(m_request.get(), false, m_outputStream,
84 m_session.get()));
85
86 // bind the preview stream to the composer
87 Argus::IEGLOutputStream *iEGLOutputStream =
88 Argus::interface_cast<Argus::IEGLOutputStream>(m_outputStream.get());
89 if (!iEGLOutputStream)
90 ORIGINATE_ERROR("Failed to get IEGLOutputStream interface");
91
92 // Bind the stream to the composer
93 PROPAGATE_ERROR(composer.bindStream(iEGLOutputStream->getEGLStream()));
94
95 const Argus::Size2D<uint32_t> streamSize = iEGLOutputStream->getResolution();
96 PROPAGATE_ERROR(composer.setStreamAspectRatio(iEGLOutputStream->getEGLStream(),
97 (float)streamSize.width() / (float)streamSize.height()));
98
99 // Enable the output stream
100 PROPAGATE_ERROR(dispatcher.enableOutputStream(m_request.get(), m_outputStream.get()));
101
102 return true;
103 }
104
105 bool TaskMultiSession::Session::start()
106 {
107 Composer &composer = Composer::getInstance();
108
109 // activate the streams and populate the burst request array
110 PROPAGATE_ERROR(composer.setStreamActive(
111 Argus::interface_cast<Argus::IEGLOutputStream>(m_outputStream)->getEGLStream(), true));
112
113 // start the repeating burst request for the preview
114 PROPAGATE_ERROR(m_perfTracker->onEvent(SESSION_EVENT_ISSUE_CAPTURE));
115 PROPAGATE_ERROR(Dispatcher::getInstance().startRepeat(m_request.get(), m_session.get()));
116
117 return true;
118 }
119
120 bool TaskMultiSession::Session::stop()
121 {
122 PROPAGATE_ERROR(m_perfTracker->onEvent(SESSION_EVENT_CLOSE_REQUESTED));
123
124 Dispatcher &dispatcher = Dispatcher::getInstance();
125 Composer &composer = Composer::getInstance();
126
127 PROPAGATE_ERROR(dispatcher.stopRepeat(m_session.get()));
128 PROPAGATE_ERROR(composer.setStreamActive(
129 Argus::interface_cast<Argus::IEGLOutputStream>(m_outputStream)->getEGLStream(), false));
130 PROPAGATE_ERROR(dispatcher.waitForIdle(m_session.get()));
131 PROPAGATE_ERROR(m_perfTracker->onEvent(SESSION_EVENT_FLUSH_DONE));
132
133 return true;
134 }
135
136 bool TaskMultiSession::Session::shutdown()
137 {
138 if (m_request)
139 {
140 Dispatcher &dispatcher = Dispatcher::getInstance();
141 Composer &composer = Composer::getInstance();
142
143 if (m_outputStream)
144 {
145 // destroy the producer
146 PROPAGATE_ERROR_CONTINUE(dispatcher.disableOutputStream(m_request.get(),
147 m_outputStream.get()));
148
149 Argus::IEGLOutputStream *iEGLOutputStream =
150 Argus::interface_cast<Argus::IEGLOutputStream>(m_outputStream);
151 if (!iEGLOutputStream)
152 REPORT_ERROR("Failed to get IEGLOutputStream interface");
153
154 // disconnect the EGL stream
155 iEGLOutputStream->disconnect();
156
157 // unbind the EGL stream from the composer
158 PROPAGATE_ERROR_CONTINUE(composer.unbindStream(iEGLOutputStream->getEGLStream()));
159
160 m_outputStream.reset();
161 }
162
163 // destroy the request
164 PROPAGATE_ERROR_CONTINUE(m_request.reset());
165 }
166
167 PROPAGATE_ERROR(m_perfTracker->onEvent(SESSION_EVENT_CLOSE_DONE));
168
169 // Destroy the session
170 m_session.reset();
171
172 return true;
173 }
174
175 bool TaskMultiSession::initialize()
176 {
177 if (m_initialized)
178 return true;
179
180 PROPAGATE_ERROR(Dispatcher::getInstance().m_sensorModeValid.registerObserver(this,
181 static_cast<IObserver::CallbackFunction>(&TaskMultiSession::onSensorModeValidChanged)));
182 PROPAGATE_ERROR(Dispatcher::getInstance().m_outputSize.registerObserver(this,
183 static_cast<IObserver::CallbackFunction>(&TaskMultiSession::restartStreams)));
184
185 m_initialized = true;
186
187 return true;
188 }
189
190 bool TaskMultiSession::shutdown()
191 {
192 if (!m_initialized)
193 return true;
194
195 // stop the preview
196 PROPAGATE_ERROR(stop());
197
198 PROPAGATE_ERROR_CONTINUE(Dispatcher::getInstance().m_outputSize.unregisterObserver(this,
199 static_cast<IObserver::CallbackFunction>(&TaskMultiSession::restartStreams)));
200 PROPAGATE_ERROR_CONTINUE(Dispatcher::getInstance().m_sensorModeValid.unregisterObserver(this,
201 static_cast<IObserver::CallbackFunction>(&TaskMultiSession::onSensorModeValidChanged)));
202
203 m_initialized = false;
204
205 return true;
206 }
207
208 bool TaskMultiSession::shutdownSessions()
209 {
210 if (!m_sessions.empty())
211 {
212 // shutdown the sessions
213 for (std::list<Session*>::iterator it = m_sessions.begin(); it != m_sessions.end(); ++it)
214 {
215 Session *session = *it;
216 PROPAGATE_ERROR_CONTINUE(session->shutdown());
217 delete session;
218 }
219 m_sessions.clear();
220 }
221
222 return true;
223 }
224
225 bool TaskMultiSession::start()
226 {
227 if (m_running)
228 return true;
229
230 Dispatcher &dispatcher = Dispatcher::getInstance();
231
232 if (m_sessions.empty())
233 {
234 const uint32_t deviceCount = dispatcher.getDeviceCount();
235
236 if (deviceCount == 0)
237 ORIGINATE_ERROR("No camera devices found");
238
239 // create a request and streams for each session
240 for (uint32_t deviceIndex = 0; deviceIndex < deviceCount; ++deviceIndex)
241 {
242 UniquePointer<Session> session(new Session);
243
244 if (!session)
245 ORIGINATE_ERROR("Out of memory");
246
247 PROPAGATE_ERROR(session->initialize(deviceIndex));
248
249 m_sessions.push_back(session.release());
250 }
251 }
252
253 // start the sessions
254 for (std::list<Session*>::iterator it = m_sessions.begin(); it != m_sessions.end(); ++it)
255 {
256 Session *session = *it;
257 PROPAGATE_ERROR(session->start());
258 }
259
260 m_running = true;
261
262 return true;
263 }
264
265 bool TaskMultiSession::stop()
266 {
267 if (!m_running)
268 return true;
269
270 for (std::list<Session*>::iterator it = m_sessions.begin(); it != m_sessions.end(); ++it)
271 {
272 Session *session = *it;
273 PROPAGATE_ERROR(session->stop());
274 }
275
276 PROPAGATE_ERROR(shutdownSessions());
277
278 m_running = false;
279
280 return true;
281 }
282
283 bool TaskMultiSession::onSensorModeValidChanged(const Observed &source)
284 {
285 const bool isTrue = static_cast<const Value<bool>&>(source).get();
286
287 if (!isTrue)
288 {
289 m_prevRunning = m_running;
290 if (m_running)
291 {
292 PROPAGATE_ERROR(stop());
293 }
294 }
295 else if (m_prevRunning)
296 {
297 m_prevRunning = false;
298 PROPAGATE_ERROR(start());
299 }
300
301 return true;
302 }
303
304 bool TaskMultiSession::restartStreams(__attribute__((unused)) const Observed &source)
305 {
306 if (m_running)
307 {
308 PROPAGATE_ERROR(stop());
309 PROPAGATE_ERROR(start());
310 }
311 return true;
312 }
313
314 }; // namespace ArgusSamples