Argus Camera Sample
Argus Camera Sample
 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-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 "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.
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,
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 
93  : m_initialized(false)
94  , m_running(false)
95  , m_guiContainerConfig(NULL)
96  , m_guiConfig(NULL)
97 {
98 }
99 
101 {
102  shutdown();
103 }
104 
105 bool AppModuleVideo::initialize(Options &options)
106 {
107  if (m_initialized)
108  return true;
109 
110  PROPAGATE_ERROR(m_videoRecord.initialize());
111 
112  PROPAGATE_ERROR(options.addOption(
113  Options::Option("video", 'v', "DURATION",
114  Options::Option::TYPE_ACTION, Options::Option::FLAG_REQUIRED_ARGUMENT,
115  "record video for DURATION seconds and save to a file.", video, this)));
116 
117  PROPAGATE_ERROR(options.addOption(
118  createValueOption("videobitrate", 0, "RATE",
119  "set the video bit rate mode to RATE. If RATE is zero a reasonable default "
120  "is selected.", Dispatcher::getInstance().m_videoBitRate)));
121  PROPAGATE_ERROR(options.addOption(
122  createValueOption("videoformat", 0, "FORMAT",
123  "set the video format. Jetson-tx1 doesn't support vp9, use other "
124  "available formats.", Dispatcher::getInstance().m_videoFormat)));
125  PROPAGATE_ERROR(options.addOption(
126  createValueOption("videofiletype", 0, "TYPE",
127  "set the video file type. For video format 'h265/vp9' set the file type as 'mkv' "
128  "since 'h265 & vp9' are only supported by the 'mkv' container.",
129  Dispatcher::getInstance().m_videoFileType)));
130 
131  m_initialized = true;
132 
133  return true;
134 }
135 
137 {
138  if (!m_initialized)
139  return true;
140 
141  PROPAGATE_ERROR_CONTINUE(stop());
142 
143  PROPAGATE_ERROR_CONTINUE(m_videoRecord.shutdown());
144 
145  delete m_guiConfig;
146  m_guiConfig = NULL;
147 
148  m_guiContainerConfig = NULL;
149 
150  m_initialized = false;
151 
152  return true;
153 }
154 
155 bool AppModuleVideo::start(Window::IGuiMenuBar *iGuiMenuBar,
156  Window::IGuiContainer *iGuiContainerConfig)
157 {
158  if (m_running)
159  return true;
160 
161  // register key observer
162  PROPAGATE_ERROR(Window::getInstance().registerObserver(this));
163 
164  // initialize the GUI
165  if (iGuiContainerConfig && !m_guiConfig)
166  {
167  // initialize the GUI
168 
169  // create a grid container
170  PROPAGATE_ERROR(Window::IGuiContainerGrid::create(&m_guiConfig));
171 
172  // create the elements
173  UniquePointer<Window::IGuiElement> element;
174  Dispatcher &dispatcher = Dispatcher::getInstance();
175 
176  Window::IGuiContainerGrid::BuildHelper buildHelper(m_guiConfig);
177 
178 #define CREATE_GUI_ELEMENT(_NAME, _VALUE) \
179  PROPAGATE_ERROR(Window::IGuiElement::createValue(&dispatcher._VALUE, &element));\
180  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
181  element.release();
182 
183 #define CREATE_GUI_ELEMENT_COMBO_BOX(_NAME, _VALUE, _FROMTYPE, _TOTYPE) \
184  assert(sizeof(_FROMTYPE) == sizeof(_TOTYPE)); \
185  PROPAGATE_ERROR(Window::IGuiElement::createValue(reinterpret_cast< \
186  Value<_TOTYPE>*>(&dispatcher._VALUE), &element)); \
187  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
188  element.release();
189 
190  CREATE_GUI_ELEMENT_COMBO_BOX("Video Format", m_videoFormat,
191  VideoPipeline::VideoFormat, Window::IGuiElement::ValueTypeEnum);
192  CREATE_GUI_ELEMENT_COMBO_BOX("Video File Type", m_videoFileType,
193  VideoPipeline::VideoFileType, Window::IGuiElement::ValueTypeEnum);
194 
195  CREATE_GUI_ELEMENT("Video Bit Rate", m_videoBitRate);
196 
197 #undef CREATE_GUI_ELEMENT
198 #undef CREATE_GUI_ELEMENT_COMBO_BOX
199 
200  PROPAGATE_ERROR(Window::IGuiElement::createAction("Toggle Recording",
201  AppModuleVideo::toggleRecording, this, Window::IGuiElement::FLAG_BUTTON_TOGGLE,
202  Window::IGuiElement::ICON_MEDIA_RECORD, &element));
203  PROPAGATE_ERROR(buildHelper.append(element.get(), 2));
204  element.release();
205 
206  m_guiContainerConfig = iGuiContainerConfig;
207  }
208 
210  PROPAGATE_ERROR(m_guiContainerConfig->add(m_guiConfig));
211 
212  PROPAGATE_ERROR(m_videoRecord.start());
213 
214  m_running = true;
215 
216  return true;
217 }
218 
220 {
221  if (!m_running)
222  return true;
223 
224  PROPAGATE_ERROR(m_videoRecord.stop());
225 
227  PROPAGATE_ERROR(m_guiContainerConfig->remove(m_guiConfig));
228 
229  // unregister key observer
230  PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
231 
232  m_running = false;
233 
234  return true;
235 }
236 
237 bool AppModuleVideo::onKey(const Key &key)
238 {
239  if (key == Key("space"))
240  {
241  PROPAGATE_ERROR(m_videoRecord.toggleRecording());
242  }
243 
244  return true;
245 }
246 
247 }; // namespace ArgusSamples