CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
videofile.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 VIDEOFILE_H
31 #define VIDEOFILE_H
32 
33 #include <string>
34 
35 #include "opencvcapture.h"
36 #include "source.h"
37 
38 namespace cavapa_gui
39 {
51 class VideoFile : public Source
52 {
53 public:
62  VideoFile(SourceID desired_id = UNDEFINED_SOURCE) : Source(desired_id) {}
63 
68  bool canRecord() const override final { return false; }
69 
73  virtual void fetchFirstFrame();
74 
75  virtual FrameCapture get(FrameTime passed_time) override;
76 
77  virtual double getBarrelCorrection() override
78  { return source ? source->getBarrelCorrection() : 0.0; }
79 
84  int getCurrentFrame() const { return current_frame; }
85 
86  virtual std::string getDescription() const override;
87 
92  virtual std::vector<std::string> getFilenames() const
93  { return {source->getPath()}; }
94 
95  virtual double getFramerate() const override
96  { return source ? source->getFramerate() : 0.0; }
97 
102  virtual FrameTime getLength() const final
103  { return (totalFrames() * 1000.0) / getFramerate(); }
104 
105  virtual FrameTime getPosition() const override final
106  { return currentFrameTime(); }
107 
108  virtual cv::Size getResolution() const override
109  { return source ? source->getResolution() : cv::Size(0, 0); }
110 
111  virtual SourceType getSourceType() const override
112  { return SourceType::VIDEO; }
113 
114  virtual SourceStats getStats() override;
115 
122  virtual bool hasNew() override
123  { return source ? source->hasNew() : false; }
124 
125  virtual bool isOpen() const override { return opened; }
126 
127  virtual bool isPlaying() const override { return playing; }
128 
143  virtual bool open(const std::string& filename, bool get_first,
144  FrameTime start_point);
145 
151  virtual void play() override { playing = true; }
152 
160  bool record(const std::string& filename,
161  const std::string& codec = "") override final
162  { Q_UNUSED(filename); Q_UNUSED(codec); return false; }
163 
178  virtual void release() { if (source) source->release(); }
179 
188  bool seekTime(FrameTime pos);
189 
201  virtual bool skipFrame(int step);
202 
203  virtual void setBarrelCorrection(double amount) override
204  { source->setBarrelCorrection(amount); }
205 
212 
221  bool setResolution(const cv::Size &new_size) override final
222  { Q_UNUSED(new_size); return false; }
223 
229  static void setSpeed(double multiplier)
230  { speed_ratio = multiplier; }
231 
236  virtual FrameTime startPoint() const final { return initial_position; }
237 
244  virtual void stop() override { playing = false; }
245 
252  int totalFrames() const { return total_frames; }
253 
254 protected:
260  { return frameTimeFromFrame(current_frame); }
261 
271  { return std::floor(static_cast<double>(time) / getFrameDifference()); }
272 
278  FrameTime frameTimeFromFrame(int frame) const
279  { return static_cast<FrameTime>(getFrameDifference() * frame); }
280 
285 
290 
294  bool opened = false;
295 
299  bool playing = false;
300 
304  static double speed_ratio;
305 
309  int total_frames = 0;
310 
315 
316 private:
321  double getFrameDifference() const
322  { return 1000.0 / getFramerate(); }
323 
328  FrameTime nextFrameTime() const
329  { return currentFrameTime() + getFrameDifference(); }
330 
331  std::unique_ptr<OpenCVCapture> source = nullptr; // Capturing device.
332 };
333 } // namespace
334 
335 #endif // VIDEOFILE_H
int getCurrentFrame() const
Returns the current frame of the video shown.
Definition: videofile.h:84
virtual void play() override
Starts frame updates on the file.
Definition: videofile.h:151
virtual bool isPlaying() const override
Checks whether the source is playing or not.
Definition: videofile.h:127
virtual FrameTime getPosition() const overridefinal
Retrieves the current time position of the source.
Definition: videofile.h:105
virtual SourceType getSourceType() const override
Returns the type of the source.
Definition: videofile.h:111
FrameTime video_time
The current time of the video file.
Definition: videofile.h:314
virtual double getBarrelCorrection() override
Returns the barrel correction applied to the source frames.
Definition: videofile.h:77
virtual std::vector< std::string > getFilenames() const
Returns the filenames of the source.
Definition: videofile.h:92
FrameTime frameTimeFromFrame(int frame) const
Returns the expected frame time of the given frame.
Definition: videofile.h:278
unsigned int SourceID
Used to indicate unique source ID-numbers.
Definition: common.h:229
static void setSpeed(double multiplier)
Sets playing speed of the video file.
Definition: videofile.h:229
bool record(const std::string &filename, const std::string &codec="") overridefinal
Starts the recording of the source to the file.
Definition: videofile.h:160
The class for the video file source.
Definition: videofile.h:51
virtual void stop() override
Will stop the file from playing.
Definition: videofile.h:244
const SourceID UNDEFINED_SOURCE
Used to indicate unknown sources.
Definition: common.h:234
virtual void fetchFirstFrame()
Seeks the startup point of the video and the first frame.
Definition: videofile.cpp:38
int total_frames
The total frames in the video file.
Definition: videofile.h:309
void setCurrentAsStartPoint()
Sets the current video time as the seeking start point.
Definition: videofile.h:211
virtual bool open(const std::string &filename, bool get_first, FrameTime start_point)
Opens a video file.
Definition: videofile.cpp:122
bool setResolution(const cv::Size &new_size) overridefinal
Should change resolution, but does not have an effect on video files.
Definition: videofile.h:221
virtual FrameTime getLength() const final
Retrieves the video file length.
Definition: videofile.h:102
virtual FrameTime startPoint() const final
Returns the start point of the video file.
Definition: videofile.h:236
virtual bool isOpen() const override
Checks whether the source has been initialized or not.
Definition: videofile.h:125
virtual double getFramerate() const override
Retrieves the framerate.
Definition: videofile.h:95
The structure to hold statistics about the source performance.
Definition: common.h:327
FrameTime currentFrameTime() const
Returns the time of the currently buffered frame.
Definition: videofile.h:259
virtual void setBarrelCorrection(double amount) override
Sets the new barrel correction value.
Definition: videofile.h:203
The camera and video file source base class.
Definition: source.h:51
virtual cv::Size getResolution() const override
Retrieves the resolution of the source.
Definition: videofile.h:108
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
virtual SourceStats getStats() override
Retrieves the source statistical information.
Definition: videofile.cpp:110
static double speed_ratio
The multiplier of the video file playing speed.
Definition: videofile.h:304
virtual void release()
Forces the release of the worker thread.
Definition: videofile.h:178
VideoFile(SourceID desired_id=UNDEFINED_SOURCE)
Creates a new source.
Definition: videofile.h:62
bool canRecord() const overridefinal
Returns information on the source recording abilities.
Definition: videofile.h:68
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
int totalFrames() const
Returns the total count of the frames in the video file.
Definition: videofile.h:252
int frameFromFrameTime(FrameTime time) const
Returns the expected frame number from the frame time.
Definition: videofile.h:270
bool opened
The open status of the video file.
Definition: videofile.h:294
FrameTime initial_position
The initial position of the video file.
Definition: videofile.h:289
virtual std::string getDescription() const override
Retrieves the description of the source.
Definition: videofile.cpp:105
virtual bool skipFrame(int step)
Skips the specific amount of frames on the video.
Definition: videofile.cpp:165
int current_frame
The number of the currently buffered frame.
Definition: videofile.h:284
virtual bool hasNew() override
Checks whether the device has a new image to be retrieved or not.
Definition: videofile.h:122
bool playing
The flag indicates if the video file is playing or not.
Definition: videofile.h:299
bool seekTime(FrameTime pos)
Seeks a specific position of the video frame.
Definition: videofile.cpp:155