CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
camera.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef CAMERA_H
31 #define CAMERA_H
32 
33 #include <cassert>
34 #include <memory>
35 #include <string>
36 #include <list>
37 
38 #include "captureinterface.h"
39 #include "recorder.h"
40 #include "source.h"
41 
42 namespace cavapa_gui
43 {
64 class Camera : public Source
65 {
66 public:
75  Camera(SourceID desired_id = UNDEFINED_SOURCE) : Source(desired_id) {}
76 
77  virtual ~Camera() { stop(); }
78 
83 
84  bool canRecord() const override { return true; }
85 
94  FrameCapture get(FrameTime passed_time) override;
95 
96  double getBarrelCorrection() override
97  { return source == nullptr ? 0.0 : source->getBarrelCorrection(); }
98 
105  static std::string getCodec() { return recorder_codec; }
106 
114  static double getDefaultFPS() { return frames_per_second; }
115 
125  std::string getDescription() const override
126  { return source == nullptr ? "" : source->getDescription(); }
127 
132  int getDeviceID() const
133  { return source == nullptr ? UNKNOWN_DEVICE : source->getDeviceID(); }
134 
135  double getFramerate() const override;
136 
141  FrameTime getPosition() const override final { return 0; }
142 
147  std::vector<std::string> getRecordings() const;
148 
149  cv::Size getResolution() const override
150  { return source == nullptr ? cv::Size(0, 0) : source->getResolution(); }
151 
152  SourceType getSourceType() const override;
153 
154  SourceStats getStats() override;
155 
161  bool hasNew() override { return true; }
162 
168  bool open(int index);
169 
175  bool open(const std::string& url);
176 
181  bool isOpen() const override
182  { return source == nullptr ? false : source->isOpen(); }
183 
188  bool isPlaying() const override { return true; }
189 
190  bool record(const std::string& filename,
191  const std::string& codec = "") override;
192 
193  void setBarrelCorrection(double amount) override;
194 
204  static bool setCodec(const std::string &codec);
205 
213  static void setDefaultFPS(double fps) { frames_per_second = fps; }
214 
215  bool setResolution(const cv::Size &new_size) override;
216 
222  void stop() override;
223 
224 private:
229  static FrameTime getFrameDifference()
230  { return static_cast<FrameTime>(1000.0 / frames_per_second); }
231 
242  inline void pushToRecorder(const FrameCapture& capture,bool repush = false);
243 
244  static std::string recorder_codec; // Codec to use for new recordings
245  static double frames_per_second; // Framerate to use for new recordings
246 
247  int record_timer = 0;
248  std::unique_ptr<Recorder> recorder = nullptr; // File recorder.
249  std::vector<std::string> recorded_files; // List of recorded files.
250  std::unique_ptr<CaptureInterface> source = nullptr; // Capturing device.
251 };
252 } // namespace
253 
254 #endif // CAMERA_H
double getFramerate() const override
Retrieves the framerate.
Definition: camera.cpp:106
void stop() override
Stops the camera from recording.
Definition: camera.cpp:259
DISALLOW_COPY_AND_ASSIGN(Camera)
Copy and assign of the class is not allowed.
bool record(const std::string &filename, const std::string &codec="") override
Starts the recording of the source to the file.
Definition: camera.cpp:202
std::vector< std::string > getRecordings() const
Returns the names of the video files that were recorded.
Definition: camera.cpp:119
unsigned int SourceID
Used to indicate unique source ID-numbers.
Definition: common.h:229
SourceType getSourceType() const override
Returns the type of the source.
Definition: camera.cpp:134
const SourceID UNDEFINED_SOURCE
Used to indicate unknown sources.
Definition: common.h:234
bool canRecord() const override
Returns information on the source recording abilities.
Definition: camera.h:84
bool isPlaying() const override
Checks whether the source is playing or not.
Definition: camera.h:188
std::string getDescription() const override
Retrieves the description of the source.
Definition: camera.h:125
The structure to hold statistics about the source performance.
Definition: common.h:327
Camera(SourceID desired_id=UNDEFINED_SOURCE)
Creates a new source.
Definition: camera.h:75
static void setDefaultFPS(double fps)
Sets the cameras default framerate.
Definition: camera.h:213
cv::Size getResolution() const override
Retrieves the resolution of the source.
Definition: camera.h:149
bool setResolution(const cv::Size &new_size) override
Sets the source resolution.
Definition: camera.cpp:252
The camera and video file source base class.
Definition: source.h:51
int getDeviceID() const
Retrieves the device number used to initialize the device.
Definition: camera.h:132
SourceType
Available source types are the following ones: CAMERA = hardware or network camera, NOTHING = not a working source, STREAM = network stream, VIDEO = video file and VIDEOSET = set of multiple files.
Definition: common.h:68
FrameTime getPosition() const overridefinal
Retrieves the current time position of the source.
Definition: camera.h:141
std::uint64_t FrameTime
Used to store milliseconds interval in frame times.
Definition: common.h:138
The structure is used for storing a single source frame.
Definition: common.h:241
bool isOpen() const override
Checks if the source has been initialized.
Definition: camera.h:181
double getBarrelCorrection() override
Returns the barrel correction applied to the source frames.
Definition: camera.h:96
SourceStats getStats() override
Retrieves the source statistical information.
Definition: camera.cpp:145
static std::string getCodec()
Retrieves the current codec used.
Definition: camera.h:105
bool hasNew() override
Checks whether the device has a new image to be retrieved or not.
Definition: camera.h:161
bool open(int index)
Opens a new camera object.
Definition: camera.cpp:158
const int UNKNOWN_DEVICE
Used to indicate unknown hardware device.
Definition: common.h:224
void setBarrelCorrection(double amount) override
Sets the new barrel correction value.
Definition: camera.cpp:234
static bool setCodec(const std::string &codec)
Sets recorders codec for new videos.
Definition: camera.cpp:241
The class for a camera source.
Definition: camera.h:64
static double getDefaultFPS()
Retrieves the default framerate that has been set.
Definition: camera.h:114