KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
video_player.h
Go to the documentation of this file.
1 /* $Id: video_player.h 18791 2015-04-07 18:51:57Z ernesto $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 #ifndef KJB_CPP_GR_PLAYER_H
22 #define KJB_CPP_GR_PLAYER_H
23 
24 #ifdef KJB_HAVE_OPENGL
25 
26 #ifdef KJB_HAVE_GLUT
27 #include <gr_cpp/gr_glut.h>
28 #endif
29 
30 #include <gr_cpp/gr_opengl.h>
31 #include <i_cpp/i_image.h>
32 #include <video_cpp/video.h>
33 
34 namespace kjb
35 {
39  void render_video_frame(const Video& video, size_t frame, bool reset_pos = true)
40  {
41  size_t num_cols = kjb::opengl::get_viewport_width();
42  size_t num_rows = kjb::opengl::get_viewport_height();
43  if(num_cols != video.get_width() || num_rows != video.get_height()) return;
44 
45  if(reset_pos) glRasterPos2f(0, 0);
46  glDrawPixels(num_cols, num_rows, GL_RGB, GL_UNSIGNED_BYTE, video.get_buffer(frame));
47 
48  GL_ETX();
49  }
50 
74 class Video_player
75 {
76 typedef Video_player Self;
77 
78 struct null_deleter
79 {
80  void operator()(void const*) const
81  { }
82 };
83 
84 public:
85  Video_player() :
86  video_(),
87  cur_frame_(0),
88  playing_(false),
89  timer_registered_(false)
90  {}
91 
92  Video_player(const std::string& movie_fname) :
93  video_(),
94  cur_frame_(0),
95  playing_(false),
96  timer_registered_(false)
97  {
98  load(movie_fname);
99  }
100 
101  Video_player(const std::vector<std::string>& fnames) :
102  video_(),
103  cur_frame_(0),
104  playing_(false),
105  timer_registered_(false)
106  {
107  load(fnames, 30.0);
108  }
109 
110  const Video& get_video() const
111  {
112  return *video_;
113  }
114 
115  void load(const std::string& movie_fname)
116  {
117  boost::shared_ptr<Video> tmp(new Video());
118  video_ = tmp;
119 
120  video_->decode_video(movie_fname);
121  }
122 
123  void load(const std::vector<std::string>& fnames, float fps)
124  {
125  boost::shared_ptr<Video> tmp(new Video());
126  video_ = tmp;
127  video_->load_images(fnames, fps);
128  }
129 
130  void set(boost::shared_ptr<Video> video)
131  {
132  video_ = video;
133  }
134 
135  void set(Video* video)
136  {
137  video_ = boost::shared_ptr<Video>(video, null_deleter());
138  }
139 
140 #ifdef KJB_HAVE_GLUT
141  void attach(kjb::opengl::Glut_window& wnd)
142  {
143  using namespace boost;
144  glPixelStorei(GL_PACK_ALIGNMENT, 1);
145  wnd.set_display_callback(bind(&Self::display, this));
146  wnd.set_special_callback(bind(&Self::special_key, this, _1, _2, _3));
147  wnd.set_keyboard_callback(bind(&Self::key, this, _1, _2, _3));
148  wnd.set_size(video_->get_width(), video_->get_height());
149 
150  }
151 #endif
152 
153  void render_image(size_t frame) const
154  {
155  render_video_frame(*video_, frame);
156  }
157 
158 #ifdef KJB_HAVE_GLUT
159  void display() const
160  {
161  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
162  render();
163  glutSwapBuffers();
164  }
165 #endif
166 
167  virtual void render() const
168  {
169  render_image(cur_frame_);
170  }
171 
172  void special_key(int k, int, int)
173  {
174 #ifdef KJB_HAVE_GLUT
175  switch(k)
176  {
177  case GLUT_KEY_UP:
178  cur_frame_ = (cur_frame_ + 10) % video_->size();
179  break;
180  case GLUT_KEY_DOWN:
181  cur_frame_ = (cur_frame_ + video_->size() - 10) % video_->size();
182  break;
183  case GLUT_KEY_RIGHT:
184  cur_frame_ = (cur_frame_ + 1) % video_->size();
185  break;
186  case GLUT_KEY_LEFT:
187  cur_frame_ = (cur_frame_ + video_->size() - 1) % video_->size();
188  break;
189  default:
190  return;
191  break;
192  }
193  redisplay();
194 #else
195  KJB_THROW_2(Missing_dependency, "glut");
196 #endif
197  }
198 
199  void init_timer()
200  {
201 #ifdef KJB_HAVE_GLUT
202  if(timer_registered_ == false)
203  {
204  float period = 1000.0 / video_->get_frame_rate();
205  kjb::opengl::Glut::add_timer_callback(period, boost::bind(&Self::timer, this));
206  timer_registered_ = true;
207  }
208 #else
209  KJB_THROW_2(Missing_dependency, "glut");
210 #endif
211  }
212 
213  void redisplay()
214  {
215 #ifdef KJB_HAVE_GLUT
216  glutPostRedisplay();
217 #endif
218  }
219 
220  void timer()
221  {
222  timer_registered_ = false;
223  if(playing_)
224  {
225  advance_frame();
226  redisplay();
227  init_timer();
228  }
229  }
230 
231  void advance_frame()
232  {
233  cur_frame_ = (cur_frame_ + 1) % video_->size();
234  }
235 
236  void play()
237  {
238  playing_ = true;
239  init_timer();
240  }
241 
242  void key(unsigned int k, int, int)
243  {
244  switch(k)
245  {
246  case 27: // ESC
247  case 'q':
248  case 'Q':
249  exit(0);
250  break;
251  case ' ':
252  playing_ = !playing_;
253  if(playing_)
254  {
255  init_timer();
256  }
257  break;
258  default:
259  break;
260  }
261  }
262 
263  int current_frame_index() const
264  {
265  return cur_frame_;
266  }
267 
268  boost::shared_ptr<kjb::Video> video_;
269 
270  int cur_frame_;
271 
272  bool playing_;
273  bool timer_registered_;
274 };
275 
276 
277 } // namespace kjb
278 #endif /* HAVE_OPENGL */
279 #endif
size_t get_viewport_height()
Get current GL viewport height.
Definition: gr_opengl.cpp:587
for k
Definition: APPgetLargeConnectedEdges.m:61
size_t get_viewport_width()
Get current GL viewport width.
Definition: gr_opengl.cpp:575
void load(Edge_set &edges, const std::string &fname)
Definition: edge.h:603
void render(const Cuboid &c)
Definition: psi_weighted_box.cpp:56
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Code for a wrapper class around the C struct KJB_Image.