Take audio device latency into account.
[dcpomatic.git] / src / wx / film_viewer.h
1 /*
2     Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/film_viewer.h
22  *  @brief A wx widget to view `thumbnails' of a Film.
23  */
24
25 #include "lib/film.h"
26 #include "lib/config.h"
27 #include <RtAudio.h>
28 #include <wx/wx.h>
29
30 class wxToggleButton;
31 class FFmpegPlayer;
32 class Image;
33 class RGBPlusAlphaImage;
34 class PlayerVideo;
35 class Player;
36 class Butler;
37
38 /** @class FilmViewer
39  *  @brief A wx widget to view a preview of a Film.
40  */
41 class FilmViewer : public wxPanel
42 {
43 public:
44         FilmViewer (wxWindow *);
45         ~FilmViewer ();
46
47         void set_film (boost::shared_ptr<Film>);
48
49         /** @return our `playhead' position; this may not lie exactly on a frame boundary */
50         DCPTime position () const {
51                 return _video_position;
52         }
53
54         void set_position (DCPTime p);
55         void set_coalesce_player_changes (bool c);
56
57         void refresh ();
58
59         int audio_callback (void* out, unsigned int frames);
60
61         boost::signals2::signal<void (boost::weak_ptr<PlayerVideo>)> ImageChanged;
62
63 private:
64         void paint_panel ();
65         void panel_sized (wxSizeEvent &);
66         void slider_moved ();
67         void play_clicked ();
68         void timer ();
69         void calculate_sizes ();
70         void check_play_state ();
71         void active_jobs_changed (boost::optional<std::string>);
72         void back_clicked (wxMouseEvent &);
73         void forward_clicked (wxMouseEvent &);
74         void player_changed (bool);
75         void update_position_label ();
76         void update_position_slider ();
77         void get ();
78         void seek (DCPTime t, bool accurate);
79         void refresh_panel ();
80         void setup_sensitivity ();
81         void film_changed (Film::Property);
82         DCPTime nudge_amount (wxMouseEvent &);
83         void timecode_clicked ();
84         void frame_number_clicked ();
85         void go_to (DCPTime t);
86         void jump_to_selected_clicked ();
87         void recreate_butler ();
88         void config_changed (Config::Property);
89         DCPTime time () const;
90         void start ();
91         bool stop ();
92         Frame average_latency () const;
93
94         boost::shared_ptr<Film> _film;
95         boost::shared_ptr<Player> _player;
96
97         wxSizer* _v_sizer;
98         wxPanel* _panel;
99         wxCheckBox* _outline_content;
100         wxRadioButton* _left_eye;
101         wxRadioButton* _right_eye;
102         wxCheckBox* _jump_to_selected;
103         wxSlider* _slider;
104         wxButton* _back_button;
105         wxButton* _forward_button;
106         wxStaticText* _frame_number;
107         wxStaticText* _timecode;
108         wxToggleButton* _play_button;
109         wxTimer _timer;
110         bool _coalesce_player_changes;
111         bool _pending_player_change;
112
113         boost::shared_ptr<const Image> _frame;
114         DCPTime _video_position;
115         Position<int> _inter_position;
116         dcp::Size _inter_size;
117
118         /** Size of our output (including padding if we have any) */
119         dcp::Size _out_size;
120         /** Size of the panel that we have available */
121         dcp::Size _panel_size;
122         /** true if the last call to Player::seek() was specified to be accurate;
123          *  this is used so that when re-fetching the current frame we
124          *  can get the same one that we got last time.
125          */
126         bool _last_seek_accurate;
127
128         RtAudio _audio;
129         int _audio_channels;
130         unsigned int _audio_block_size;
131         bool _playing;
132         boost::shared_ptr<Butler> _butler;
133
134         std::list<Frame> _latency_history;
135         /** Mutex to protect _latency_history */
136         mutable boost::mutex _latency_history_mutex;
137         int _latency_history_count;
138
139         boost::signals2::scoped_connection _config_changed_connection;
140 };