xvWidgets
VideoPlayer.hpp
1 #pragma once
2 
3 #include <functional>
4 #include <opencv2/highgui.hpp>
5 #include <wx/panel.h>
6 #include <wx/timer.h>
7 #include <wx/stattext.h>
8 #include <wx/button.h>
9 
10 namespace xv{
11 
12  class ImagePanel;
13 
18 class VideoPlayer : public wxPanel
19 {
20  friend class Thread;
21 public:
23  VideoPlayer(wxWindow * parent,
24  wxWindowID id = wxID_ANY,
25  const wxPoint &pos = wxDefaultPosition,
26  const wxSize &size = wxDefaultSize,
27  long style = wxTAB_TRAVERSAL,
28  const wxString &name = wxPanelNameStr
29  );
30 
32  VideoPlayer(cv::VideoCapture* videoCapture);
33 
34  virtual ~VideoPlayer();
35 
37  int getCurrentFrameIdx();
38 
41 
42 #pragma region control
43 
45  virtual bool open(const cv::String &filename);
46 
48  virtual bool open(int device);
49 
51  virtual void play();
52 
54  virtual void play(
55  std::function<void(cv::Mat &)> callback,
56  std::function<void(cv::Mat &)> postProcessCallback = [](cv::Mat &){});
57 
59  virtual void pause();
60 
62  virtual void stop();
63 
65  virtual void seek(int);
66 
67 #pragma endregion control
68 
69 #pragma region event handling
70  virtual void onSliderMove(wxScrollEvent&);
72 
74  virtual void onSliderClickDown(wxMouseEvent&);
75 
77  virtual void onSliderClickUp(wxMouseEvent&);
78 
80  virtual void onPlayClick(wxCommandEvent&);
81 
83  virtual void onMouseMove(wxCommandEvent& evt);
84 #pragma endregion event handling
85 
86 private:
88  template <typename _Tp>
89  bool open_(const _Tp &deviceOrFilename);
90 
92  static const int LABEL_UPDATE_INTERVAL;
93 
95  static const char* LABEL_DEFAULT_TEXT;
96 
97  std::function<void(cv::Mat &)>
98  m_preProcessCallback = [](cv::Mat &){},
99  m_postProcessCallback = [](cv::Mat &){};
100 
101  int
102  m_fps = 0,
103  m_frameCount = 0;
104 
105  wxTimer m_timer;
106 
108  void sizeEvent(wxSizeEvent&);
109  void onTimer(wxTimerEvent&);
110 
112  void showTimeLabel(wxStaticText* label,int frame);
113 
114  std::function<void(cv::Mat &)> m_processCallback = [](cv::Mat &){};
115 
116  enum playbackState{
117  idle, //no video loaded
118  playing,//ongoing playback
119  paused, //video is paused
120  seeking //looking for a frame
121  }
122  m_state = paused,
123  m_previousState = paused;
124 
126  void setState(playbackState);
127 
128 
129  cv::VideoCapture* m_videoCapture = NULL;
130  ImagePanel* m_image;
131  wxButton* m_playButton;
132  wxSlider* m_slider;
133  wxStaticText
134  *m_positionLabel,
135  *m_durationLabel;
136  wxSizer *m_sizer;
137 
138  wxMutex m_mutex;
139  class Thread : public wxThread{
140  public:
141  Thread(VideoPlayer* const);
142  virtual void *Entry();
143  private:
144  VideoPlayer *m_player;
145  } *m_thread;
146 
147  void init();
148 
149  DECLARE_EVENT_TABLE()
150 };
151 
152 }//namespace
virtual bool open(const cv::String &filename)
Try to open the named file.
virtual void onPlayClick(wxCommandEvent &)
The user clicked the playback control button.
virtual void onSliderClickUp(wxMouseEvent &)
The user released the mouse button after clicking the slider.
virtual void onSliderClickDown(wxMouseEvent &)
The user clicked the slider.
virtual void seek(int)
Move playback to frame.
virtual void play()
Start media loop.
Class used to display images and as container of widgets.
Definition: ImagePanel.hpp:32
Definition: Angle.hpp:8
Class to control the playback and processing of media obtained from VideoCapture. ...
Definition: VideoPlayer.hpp:18
virtual void onMouseMove(wxCommandEvent &evt)
The user moved the mouse.
virtual void onSliderMove(wxScrollEvent &)
The user dragged the slider.
ImagePanel & getImage()
Get the xv::Image instance used for rendering.
virtual void stop()
Stops the playback thread.
VideoPlayer(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name=wxPanelNameStr)
Constructor inherited from wxPanel.
virtual void pause()
Pause media loop.
int getCurrentFrameIdx()
Returns the last frame number read from the media.