Argus Camera Sample
Argus Camera Sample
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AppModuleCapture.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 "AppModuleCapture.h"
30 
31 #include <stdlib.h>
32 
33 #include "Dispatcher.h"
34 #include "Error.h"
35 #include "Options.h"
36 #include "tasks/StillCapture.h"
37 #include "ScopedGuard.h"
38 
39 namespace ArgusSamples
40 {
41 
42 /* static */ bool AppModuleCapture::still(void *userPtr, const char *optArg)
43 {
44  AppModuleCapture *module = reinterpret_cast<AppModuleCapture*>(userPtr);
45  uint32_t count = 1;
46 
47  if (optArg)
48  {
49  count = atoi(optArg);
50  if (count < 1)
51  ORIGINATE_ERROR("'COUNT' is invalid, must be at least 1");
52  }
53 
54  // start the capture module
55  PROPAGATE_ERROR(module->start());
56  // the scoped guard is used to call the stop function if following calls fail so that
57  // the function is exited with module stopped.
59 
60  while (count)
61  {
62  PROPAGATE_ERROR(module->m_stillCapture.execute());
63  PROPAGATE_ERROR(Window::getInstance().pollEvents());
64  --count;
65  }
66 
67  // stop the module
68  runningGuard.cancel();
69  PROPAGATE_ERROR(module->stop());
70 
71  return true;
72 }
73 
74 /* static */ bool AppModuleCapture::capture(void *userPtr, const char *optArg)
75 {
76  AppModuleCapture *module = reinterpret_cast<AppModuleCapture*>(userPtr);
77 
78  PROPAGATE_ERROR(module->m_stillCapture.execute());
79 
80  return true;
81 }
82 
84  : m_initialized(false)
85  , m_running(false)
86  , m_guiContainerConfig(NULL)
87  , m_guiConfig(NULL)
88 {
89 }
90 
92 {
93  shutdown();
94 }
95 
96 bool AppModuleCapture::initialize(Options &options)
97 {
98  if (m_initialized)
99  return true;
100 
101  PROPAGATE_ERROR(m_stillCapture.initialize());
102 
103  PROPAGATE_ERROR(options.addOption(
104  Options::Option("still", 's', "COUNT",
105  Options::Option::TYPE_ACTION, "do COUNT still captures and save as jpg files. ",
106  still, this, "1")));
107 
108  PROPAGATE_ERROR(options.addOption(
109  createValueOption("stillfiletype", 0, "FORMAT",
110  "set image file type.", Dispatcher::getInstance().m_stillFileType)));
111 
112  m_initialized = true;
113 
114  return true;
115 }
116 
118 {
119  if (!m_initialized)
120  return true;
121 
122  PROPAGATE_ERROR_CONTINUE(stop());
123 
124  PROPAGATE_ERROR_CONTINUE(m_stillCapture.shutdown());
125 
126  delete m_guiConfig;
127  m_guiConfig = NULL;
128 
129  m_guiContainerConfig = NULL;
130 
131  m_initialized = false;
132  return true;
133 }
134 
135 bool AppModuleCapture::start(Window::IGuiMenuBar *iGuiMenuBar,
136  Window::IGuiContainer *iGuiContainerConfig)
137 {
138  if (m_running)
139  return true;
140 
141  // register key observer
142  PROPAGATE_ERROR(Window::getInstance().registerObserver(this));
143 
144  // initialize the GUI
145  if (iGuiContainerConfig && !m_guiContainerConfig)
146  {
147  // create a grid container
148  PROPAGATE_ERROR(Window::IGuiContainerGrid::create(&m_guiConfig));
149 
150  // create the elements
151  UniquePointer<Window::IGuiElement> element;
152  Dispatcher &dispatcher = Dispatcher::getInstance();
153 
154  Window::IGuiContainerGrid::BuildHelper buildHelper(m_guiConfig);
155 
156 #define CREATE_GUI_ELEMENT_COMBO_BOX(_NAME, _VALUE, _FROMTYPE, _TOTYPE) \
157  assert(sizeof(_FROMTYPE) == sizeof(_TOTYPE)); \
158  PROPAGATE_ERROR(Window::IGuiElement::createValue(reinterpret_cast< \
159  Value<_TOTYPE>*>(&dispatcher._VALUE), &element)); \
160  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
161  element.release();
162 
163  CREATE_GUI_ELEMENT_COMBO_BOX("Still File Type", m_stillFileType,
164  StillFileType, Window::IGuiElement::ValueTypeEnum);
165 
166 #undef CREATE_GUI_ELEMENT_COMBO_BOX
167 
168  PROPAGATE_ERROR(Window::IGuiElement::createAction("Capture",
169  AppModuleCapture::capture, this, Window::IGuiElement::FLAG_NONE,
170  Window::IGuiElement::ICON_NONE, &element));
171  PROPAGATE_ERROR(buildHelper.append(element.get(), 2/*width*/));
172  element.release();
173 
174  m_guiContainerConfig = iGuiContainerConfig;
175  }
176 
178  PROPAGATE_ERROR(m_guiContainerConfig->add(m_guiConfig));
179 
180  PROPAGATE_ERROR(m_stillCapture.start());
181 
182  m_running = true;
183 
184  return true;
185 }
186 
188 {
189  if (!m_running)
190  return true;
191 
192  PROPAGATE_ERROR(m_stillCapture.stop());
193 
195  PROPAGATE_ERROR(m_guiContainerConfig->remove(m_guiConfig));
196 
197  // unregister key observer
198  PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
199 
200  m_running = false;
201 
202  return true;
203 }
204 
205 bool AppModuleCapture::onKey(const Key &key)
206 {
207  if (key == Key("space"))
208  {
209  PROPAGATE_ERROR(m_stillCapture.execute());
210  }
211 
212  return true;
213 }
214 
215 }; // namespace ArgusSamples