cgv
Loading...
Searching...
No Matches
video_reader.cxx
1#include "video_reader.h"
2#include <cgv/base/register.h>
3#include <cgv/utils/scan.h>
4#include <cgv/utils/file.h>
5#include <vector>
6
7using namespace cgv::base;
8using namespace cgv::utils;
9using namespace cgv::data;
10
11namespace cgv {
12 namespace media {
13 namespace video {
14
15
18{
19 static std::vector<abst_video_reader*>& ref_readers()
20 {
21 static std::vector<abst_video_reader*> readers;
22 return readers;
23 }
24 std::string get_type_name() const
25 {
26 return "video_reader_listener";
27 }
28 void register_object(base_ptr object, const std::string&)
29 {
31 if (rd)
32 ref_readers().push_back(rd);
33 }
34 void unregister_object(base_ptr object, const std::string&)
35 {
36 for (unsigned int i=0; i<ref_readers().size(); ++i) {
37 if (object == ref_readers()[i]) {
38 ref_readers().erase(ref_readers().begin()+i);
39 ++i;
40 }
41 }
42 }
43};
44
47 : image_format_ptr(&image_format), rd(0)
48{
49 fps = 25;
50}
51
57
58
61{
62 static std::string exts;
63 exts.clear();
64 std::vector<abst_video_reader*>& readers = video_reader_listener::ref_readers();
65 for (unsigned int i=0; i<readers.size(); ++i) {
66 if (!exts.empty())
67 exts += ';';
68 exts += readers[i]->get_supported_extensions();
69 }
70 if (sep != ';')
72 return exts;
73}
74
76{
77 std::string text = "Video Files (";
78 std::string exts = "*.";
79 text += get_supported_extensions(',');
80 text += "):";
82 replace(exts,",",";*.");
83 return text+exts;
84}
85
87const std::string& video_reader::get_last_error() const
88{
89 static std::string dummy;
90 if (!rd)
91 return dummy;
92 return rd->get_last_error();
93}
94
96bool video_reader::open(const std::string& file_name)
97{
98 if (!image_format_ptr) {
99 last_error = "no space provided to store the image format";
100 return false;
101 }
102 if (rd)
103 close();
104 std::string ext = to_lower(cgv::utils::file::get_extension(file_name));
105 std::vector<abst_video_reader*>& readers = video_reader_listener::ref_readers();
106 for (unsigned int i=0; i<readers.size(); ++i) {
108 rd = readers[i]->clone();
109 return rd->open(file_name, *image_format_ptr, fps);
110 }
111 }
112 last_error = "no video writer found for extension: ";
113 last_error += ext;
114 return false;
115}
116
122
125{
126 return fps;
127}
128
134{
135 if (!image_format_ptr) {
136 last_error = "no image format available";
137 return false;
138 }
139 if (dv.empty())
140 new (&dv) data_view(image_format_ptr);
141 if (rd)
142 return rd->read_frame(dv);
143 last_error = "no video reader available";
144 return false;
145}
149{
150 if (rd)
151 return rd->read_frame(dv);
152 last_error = "no video reader available";
153 return false;
154}
157{
158 if (rd) {
159 bool res = rd->close();
160 delete rd;
161 rd = 0;
162 return res;
163 }
164 last_error = "no video reader available";
165 return false;
166}
167
170{
171 if (rd)
172 return rd->get_type_name();
173 return "cmv::video_reader";
174}
182
184bool video_reader::set_void(const std::string& property, const std::string& type, const void* value)
185{
186 if (rd)
187 return rd->set_void(property, type, value);
188 return cgv::base::base::set_void(property, type, value);
189}
191bool video_reader::get_void(const std::string& property, const std::string& type, void* value)
192{
193 if (rd)
194 return rd->get_void(property, type, value);
195 return cgv::base::base::get_void(property, type, value);
196}
197
198object_registration<video_reader_listener> video_reader_listener_registration("register video reader listener");
199
200 }
201 }
202}
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
virtual std::string get_property_declarations()
return a semicolon separated list of property declarations
Definition base.cxx:264
T * get_interface()
use dynamic type cast to check for the given interface
Definition base.h:128
virtual bool set_void(const std::string &property, const std::string &value_type, const void *value_ptr)
abstract interface for the setter of a dynamic property.
Definition base.cxx:181
virtual std::string get_type_name() const
overload to return the type name of this object. By default the type interface is queried over get_ty...
Definition base.cxx:241
virtual bool get_void(const std::string &property, const std::string &value_type, void *value_ptr)
abstract interface for the getter of a dynamic property.
Definition base.cxx:215
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
bool empty() const
return whether the data pointer is a null pointer
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
abstract interface for a video reader
virtual bool open(const std::string &file_name, cgv::data::data_format &df, float &fps)=0
open the file and read the header in order to determine the image format and the fps
virtual bool close()=0
close the video file
virtual bool read_frame(const cgv::data::data_view &dv)=0
read a frame and return whether this was successful
virtual const std::string & get_last_error() const =0
return a reference to the last error message
float fps
store the fps of the opened video file
cgv::data::data_format * image_format_ptr
store the image format
const cgv::data::data_format & get_image_format() const
return the image format of the video file
bool get_void(const std::string &property, const std::string &type, void *value)
abstract interface for the getter, by default it simply returns false
std::string last_error
store the last error not resulting from video writer implementations
bool open(const std::string &file_name)
open the file and read the video header in order to determine the image format
static const std::string & get_supported_extensions(char sep=';')
return a string with a list of supported extensions, where the list entries are separated with the pa...
bool set_void(const std::string &property, const std::string &type, const void *value)
abstract interface for the setter, by default it simply returns false
bool close()
close the video file
abst_video_reader * rd
store a pointer to the chosen reader
std::string get_property_declarations()
return a semicolon separated list of property declarations of the form "name:type",...
float get_fps() const
return the frame rate
video_reader(cgv::data::data_format &image_format)
construct a video reader from a reference to the image format, which will be set after opening a file...
std::string get_type_name() const
overload to return the type name of this object
const std::string & get_last_error() const
return a reference to the last error message
bool read_frame(cgv::data::data_view &dv)
read the next frame to the given data view, if this is empty recreate it with a newly allocated data ...
~video_reader()
destruct reader and implementation
static std::string construct_filter_string()
use this to generate a file_open_dialog or file_save_dialog
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
namespace for data management components
namespace that holds tools that dont fit any other namespace
unsigned int replace(std::string &s, char c1, char c2)
replace char c1 with c2 in the given string _s and return number of replacements
Definition scan.cxx:159
char to_lower(char c)
convert char to lower case
Definition scan.cxx:39
bool is_element(char c, const std::string &s)
check if char c arises in string s
Definition scan.cxx:291
the cgv namespace
Definition print.h:11
Helper functions to process strings.
interfaces that allows to listen to registration events.
Definition register.h:218
interfaces that allows to listen to registration events
void unregister_object(base_ptr object, const std::string &)
overload to handle unregistration events
std::string get_type_name() const
overload to return the type name of this object. By default the type interface is queried over get_ty...
void register_object(base_ptr object, const std::string &)
overload to handle registration events