Rename Encoder -> FilmEncoder, and subclasses.
[dcpomatic.git] / src / wx / film_viewer.h
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  src/film_viewer.h
23  *  @brief FilmViewer class.
24  */
25
26
27 #include "video_view.h"
28 #include "lib/change_signaller.h"
29 #include "lib/config.h"
30 #include "lib/film_property.h"
31 #include "lib/player.h"
32 #include "lib/player_text.h"
33 #include "lib/signaller.h"
34 #include "lib/timer.h"
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <RtAudio.h>
38 #include <wx/wx.h>
39 LIBDCP_ENABLE_WARNINGS
40 #include <vector>
41
42
43 class Butler;
44 class ClosedCaptionsDialog;
45 class FFmpegPlayer;
46 class Image;
47 class Player;
48 class PlayerVideo;
49 class RGBPlusAlphaImage;
50 class wxToggleButton;
51
52
53 /** @class FilmViewer
54  *  @brief A wx widget to view a Film.
55  */
56 class FilmViewer : public Signaller
57 {
58 public:
59         FilmViewer (wxWindow *);
60         ~FilmViewer ();
61
62         /** @return the window showing the film's video */
63         wxWindow* panel () const {
64                 return _video_view->get();
65         }
66
67         std::shared_ptr<const VideoView> video_view () const {
68                 return _video_view;
69         }
70
71         void show_closed_captions ();
72
73         void set_film (std::shared_ptr<Film>);
74         std::shared_ptr<Film> film () const {
75                 return _film;
76         }
77
78         void seek (dcpomatic::DCPTime t, bool accurate);
79         void seek (std::shared_ptr<Content> content, dcpomatic::ContentTime p, bool accurate);
80         void seek_by (dcpomatic::DCPTime by, bool accurate);
81         /** @return our `playhead' position; this may not lie exactly on a frame boundary */
82         dcpomatic::DCPTime position () const {
83                 return _video_view->position();
84         }
85         boost::optional<dcpomatic::ContentTime> position_in_content (std::shared_ptr<const Content> content) const;
86         dcpomatic::DCPTime one_video_frame () const;
87
88         void start ();
89         bool stop ();
90         void suspend ();
91         void resume ();
92
93         bool playing () const {
94                 return _playing;
95         }
96
97         void set_coalesce_player_changes (bool c);
98         void set_dcp_decode_reduction (boost::optional<int> reduction);
99         boost::optional<int> dcp_decode_reduction () const;
100         void set_outline_content (bool o);
101         void set_outline_subtitles (boost::optional<dcpomatic::Rect<double>>);
102         void set_eyes (Eyes e);
103         void set_pad_black (bool p);
104         void set_optimise_for_j2k (bool o);
105         void set_crop_guess (dcpomatic::Rect<float> crop);
106         void unset_crop_guess ();
107
108         void slow_refresh ();
109
110         dcpomatic::DCPTime time () const;
111         boost::optional<dcpomatic::DCPTime> audio_time () const;
112
113         int dropped () const;
114         int errored () const;
115         int gets () const;
116
117         int audio_callback (void* out, unsigned int frames);
118
119         StateTimer const & state_timer () const {
120                 return _video_view->state_timer ();
121         }
122
123         /* Some accessors and utility methods that VideoView classes need */
124         bool outline_content () const {
125                 return _outline_content;
126         }
127         boost::optional<dcpomatic::Rect<double>> outline_subtitles () const {
128                 return _outline_subtitles;
129         }
130         bool pad_black () const {
131                 return _pad_black;
132         }
133         std::shared_ptr<Butler> butler () const {
134                 return _butler;
135         }
136         ClosedCaptionsDialog* closed_captions_dialog () const {
137                 return _closed_captions_dialog;
138         }
139         void finished ();
140         void image_changed (std::shared_ptr<PlayerVideo> video);
141         boost::optional<dcpomatic::Rect<float>> crop_guess () const {
142                 return _crop_guess;
143         }
144
145         bool pending_idle_get () const {
146                 return _idle_get;
147         }
148
149         boost::signals2::signal<void (std::shared_ptr<PlayerVideo>)> ImageChanged;
150         boost::signals2::signal<void ()> Started;
151         boost::signals2::signal<void ()> Stopped;
152         /** While playing back we reached the end of the film (emitted from GUI thread) */
153         boost::signals2::signal<void ()> Finished;
154         /** Emitted from the GUI thread when a lot of frames are being dropped */
155         boost::signals2::signal<void()> TooManyDropped;
156
157         boost::signals2::signal<bool ()> PlaybackPermitted;
158
159 private:
160
161         void video_view_sized ();
162         void calculate_sizes ();
163         void player_change (ChangeType type, int, bool);
164         void player_change (std::vector<int> properties);
165         void idle_handler ();
166         void request_idle_display_next_frame ();
167         void film_change(ChangeType, FilmProperty);
168         void destroy_butler();
169         void create_butler();
170         void destroy_and_maybe_create_butler();
171         void config_changed (Config::Property);
172         void film_length_change ();
173         void ui_finished ();
174         void start_audio_stream_if_open ();
175
176 #if (RTAUDIO_VERSION_MAJOR >= 6)
177         void rtaudio_error_callback(std::string const& error);
178         mutable boost::mutex _last_rtaudio_error_mutex;
179         std::string _last_rtaudio_error;
180         std::string last_rtaudio_error() const;
181 #endif
182
183         dcpomatic::DCPTime uncorrected_time () const;
184         Frame average_latency () const;
185
186         bool quick_refresh ();
187
188         std::shared_ptr<Film> _film;
189         boost::optional<Player> _player;
190
191         std::shared_ptr<VideoView> _video_view;
192         bool _coalesce_player_changes = false;
193         std::vector<int> _pending_player_changes;
194
195         RtAudio _audio;
196         int _audio_channels = 0;
197         unsigned int _audio_block_size = 1024;
198         bool _playing = false;
199         int _suspended = 0;
200         std::shared_ptr<Butler> _butler;
201
202         std::list<Frame> _latency_history;
203         /** Mutex to protect _latency_history */
204         mutable boost::mutex _latency_history_mutex;
205         int _latency_history_count = 0;
206
207         boost::optional<int> _dcp_decode_reduction;
208
209         /** true to assume that this viewer is only being used for JPEG2000 sources
210          *  so it can optimise accordingly.
211          */
212         bool _optimise_for_j2k = false;
213
214         ClosedCaptionsDialog* _closed_captions_dialog = nullptr;
215
216         bool _outline_content = false;
217         boost::optional<dcpomatic::Rect<double>> _outline_subtitles;
218         /** true to pad the viewer panel with black, false to use
219             the normal window background colour.
220         */
221         bool _pad_black = false;
222
223         /** true if an get() is required next time we are idle */
224         bool _idle_get = false;
225
226         boost::optional<dcpomatic::Rect<float>> _crop_guess;
227
228         boost::signals2::scoped_connection _config_changed_connection;
229 };