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
AppModuleVideo.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 "AppModuleVideo.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "Dispatcher.h"
36 #include "Util.h"
37 #include "Error.h"
38 #include "Options.h"
39 #include "ScopedGuard.h"
40
41 namespace ArgusSamples
42 {
43
44 /* static */ bool AppModuleVideo::video(void *userPtr, const char *optArg)
45 {
46 AppModuleVideo *module = reinterpret_cast< AppModuleVideo*>(userPtr);
47
48 const float seconds = atof(optArg);
49 if (seconds <= 0.0f)
50 ORIGINATE_ERROR("'SECONDS' is invalid, must not be less than or equal to zero");
51
52 // start the video module
53 PROPAGATE_ERROR(module->start());
54 // the scoped guard is used to call the stop function if following calls fail so that
55 // the function is exited with module stopped.
56 ScopedGuard<AppModuleVideo> runningGuard(module, &AppModuleVideo::stop);
57
58 const TimeValue endTime = getCurrentTime() + TimeValue::fromSec(seconds);
59
60 // start the recording
61 PROPAGATE_ERROR(module->m_videoRecord.startRecording());
62 ScopedGuard<TaskVideoRecord> recordingGuard(&module->m_videoRecord,
63 &TaskVideoRecord::stopRecording);
64
65 // wait until the time has elapsed
66 while (getCurrentTime() < endTime)
67 {
68 PROPAGATE_ERROR(Window::getInstance().pollEvents());
69 usleep(1000);
70 }
71
72 // stop recording
73 recordingGuard.cancel();
74 PROPAGATE_ERROR(module->m_videoRecord.stopRecording());
75
76 // stop the module
77 runningGuard.cancel();
78 PROPAGATE_ERROR(module->stop());
79
80 return true;
81 }
82
83 /* static */ bool AppModuleVideo::toggleRecording(void *userPtr, const char *optArg)
84 {
85 AppModuleVideo *module = reinterpret_cast< AppModuleVideo*>(userPtr);
86
87 PROPAGATE_ERROR(module->m_videoRecord.toggleRecording());
88
89 return true;
90 }
91
92 /* static */ bool AppModuleVideo::videoBitRate(void *userPtr, const char *optArg)
93 {
94 PROPAGATE_ERROR(Dispatcher::getInstance().m_videoBitRate.setFromString(optArg));
95
96 return true;
97 }
98
99 /* static */ bool AppModuleVideo::videoFormat(void *userPtr, const char *optArg)
100 {
101 PROPAGATE_ERROR(Dispatcher::getInstance().m_videoFormat.setFromString(optArg));
102 return true;
103 }
104
105 /* static */ bool AppModuleVideo::videoFileType(void *userPtr, const char *optArg)
106 {
107 PROPAGATE_ERROR(Dispatcher::getInstance().m_videoFileType.setFromString(optArg));
108 return true;
109 }
110
111 AppModuleVideo::AppModuleVideo()
112 : m_initialized(false)
113 , m_running(false)
114 , m_guiContainerConfig(NULL)
115 , m_guiConfig(NULL)
116 {
117 }
118
119 AppModuleVideo::~AppModuleVideo()
120 {
121 shutdown();
122 }
123
124 bool AppModuleVideo::initialize(Options &options)
125 {
126 if (m_initialized)
127 return true;
128
129 PROPAGATE_ERROR(m_videoRecord.initialize());
130
131 PROPAGATE_ERROR(options.addOption(
132 Options::Option("video", 'v', "DURATION",
133 Options::Option::TYPE_ACTION, Options::Option::FLAG_REQUIRED_ARGUMENT,
134 "record video for DURATION seconds and save to a file.", video, this)));
135
136 PROPAGATE_ERROR(options.addOption(
137 Options::Option("videobitrate", 0, "RATE", Dispatcher::getInstance().m_videoBitRate,
138 "set the video bit rate mode to RATE. If RATE is zero a reasonable default "
139 "is selected.", videoBitRate)));
140 PROPAGATE_ERROR(options.addOption(
141 Options::Option("videoformat", 0, "FORMAT", Dispatcher::getInstance().m_videoFormat,
142 "set the video format.", videoFormat)));
143 PROPAGATE_ERROR(options.addOption(
144 Options::Option("videofiletype", 0, "TYPE", Dispatcher::getInstance().m_videoFileType,
145 "set the video file type. For 'h265' set the file type as 'mkv' since 'h265' is only "
146 "supported by the 'mkv' container.", videoFileType)));
147
148 m_initialized = true;
149
150 return true;
151 }
152
153 bool AppModuleVideo::shutdown()
154 {
155 if (!m_initialized)
156 return true;
157
158 PROPAGATE_ERROR_CONTINUE(stop());
159
160 PROPAGATE_ERROR_CONTINUE(m_videoRecord.shutdown());
161
162 delete m_guiConfig;
163 m_guiConfig = NULL;
164
165 m_guiContainerConfig = NULL;
166
167 m_initialized = false;
168
169 return true;
170 }
171
172 bool AppModuleVideo::start(Window::IGuiMenuBar *iGuiMenuBar,
173 Window::IGuiContainer *iGuiContainerConfig)
174 {
175 if (m_running)
176 return true;
177
178 // register key observer
179 PROPAGATE_ERROR(Window::getInstance().registerObserver(this));
180
181 // initialize the GUI
182 if (iGuiContainerConfig && !m_guiConfig)
183 {
184 // initialize the GUI
185
186 // create a grid container
187 PROPAGATE_ERROR(Window::IGuiContainerGrid::create(&m_guiConfig));
188
189 // create the elements
190 UniquePointer<Window::IGuiElement> element;
191 Dispatcher &dispatcher = Dispatcher::getInstance();
192
193 Window::IGuiContainerGrid::BuildHelper buildHelper(m_guiConfig);
194
195 #define CREATE_GUI_ELEMENT(_NAME, _VALUE) \
196 PROPAGATE_ERROR(Window::IGuiElement::createValue(&dispatcher._VALUE, &element));\
197 PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
198 element.release();
199
200 #define CREATE_GUI_ELEMENT_COMBO_BOX(_NAME, _VALUE, _FROMTYPE, _TOTYPE) \
201 assert(sizeof(_FROMTYPE) == sizeof(_TOTYPE)); \
202 PROPAGATE_ERROR(Window::IGuiElement::createValue(reinterpret_cast< \
203 Value<_TOTYPE>*>(&dispatcher._VALUE), &element)); \
204 PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
205 element.release();
206
207 CREATE_GUI_ELEMENT_COMBO_BOX("Video Format", m_videoFormat,
208 VideoPipeline::VideoFormat, Window::IGuiElement::ValueTypeEnum);
209 CREATE_GUI_ELEMENT_COMBO_BOX("Video File Type", m_videoFileType,
210 VideoPipeline::VideoFileType, Window::IGuiElement::ValueTypeEnum);
211
212 CREATE_GUI_ELEMENT("Video Bit Rate", m_videoBitRate);
213
214 #undef CREATE_GUI_ELEMENT
215 #undef CREATE_GUI_ELEMENT_COMBO_BOX
216
217 PROPAGATE_ERROR(Window::IGuiElement::createAction("Toggle Recording",
218 AppModuleVideo::toggleRecording, this, Window::IGuiElement::FLAG_BUTTON_TOGGLE,
219 Window::IGuiElement::ICON_MEDIA_RECORD, &element));
220 PROPAGATE_ERROR(buildHelper.append(element.get(), 2));
221 element.release();
222
223 m_guiContainerConfig = iGuiContainerConfig;
224 }
225
226 if (m_guiContainerConfig)
227 PROPAGATE_ERROR(m_guiContainerConfig->add(m_guiConfig));
228
229 PROPAGATE_ERROR(m_videoRecord.start());
230
231 m_running = true;
232
233 return true;
234 }
235
236 bool AppModuleVideo::stop()
237 {
238 if (!m_running)
239 return true;
240
241 PROPAGATE_ERROR(m_videoRecord.stop());
242
243 if (m_guiContainerConfig)
244 PROPAGATE_ERROR(m_guiContainerConfig->remove(m_guiConfig));
245
246 // unregister key observer
247 PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
248
249 m_running = false;
250
251 return true;
252 }
253
254 bool AppModuleVideo::onKey(const Key &key)
255 {
256 if (key == Key("space"))
257 {
258 PROPAGATE_ERROR(m_videoRecord.toggleRecording());
259 }
260
261 return true;
262 }
263
264 }; // namespace ArgusSamples