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, 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_captureButton(NULL)
88 {
89 }
90 
92 {
93  shutdown();
94 }
95 
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",
106  "do COUNT still captures and save as jpg files. If COUNT is not specified do one still "
107  "capture.", still, this)));
108 
109  m_initialized = true;
110 
111  return true;
112 }
113 
115 {
116  if (!m_initialized)
117  return true;
118 
119  PROPAGATE_ERROR_CONTINUE(stop());
120 
121  PROPAGATE_ERROR_CONTINUE(m_stillCapture.shutdown());
122 
123  delete m_captureButton;
124  m_captureButton = NULL;
125 
126  m_guiContainerConfig = NULL;
127 
128  m_initialized = false;
129  return true;
130 }
131 
132 bool AppModuleCapture::start(Window::IGuiMenuBar *iGuiMenuBar,
133  Window::IGuiContainer *iGuiContainerConfig)
134 {
135  if (m_running)
136  return true;
137 
138  // register key observer
139  PROPAGATE_ERROR(Window::getInstance().registerObserver(this));
140 
141  // initialize the GUI
142  if (iGuiContainerConfig && !m_guiContainerConfig)
143  {
144  PROPAGATE_ERROR(Window::IGuiElement::createAction("Capture",
145  AppModuleCapture::capture, this, Window::IGuiElement::FLAG_NONE,
146  Window::IGuiElement::ICON_NONE, &m_captureButton));
147 
148  m_guiContainerConfig = iGuiContainerConfig;
149  }
150 
152  PROPAGATE_ERROR(m_guiContainerConfig->add(m_captureButton));
153 
154  PROPAGATE_ERROR(m_stillCapture.start());
155 
156  m_running = true;
157 
158  return true;
159 }
160 
162 {
163  if (!m_running)
164  return true;
165 
166  PROPAGATE_ERROR(m_stillCapture.stop());
167 
169  PROPAGATE_ERROR(m_guiContainerConfig->remove(m_captureButton));
170 
171  // unregister key observer
172  PROPAGATE_ERROR(Window::getInstance().unregisterObserver(this));
173 
174  m_running = false;
175 
176  return true;
177 }
178 
179 bool AppModuleCapture::onKey(const Key &key)
180 {
181  if (key == Key("space"))
182  {
183  PROPAGATE_ERROR(m_stillCapture.execute());
184  }
185 
186  return true;
187 }
188 
189 }; // namespace ArgusSamples