496888d43db4ddf05dc243b98290109ce53f8b9d
[dcpomatic.git] / src / wx / video_view.h
1 /*
2     Copyright (C) 2019-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 #ifndef DCPOMATIC_VIDEO_VIEW_H
23 #define DCPOMATIC_VIDEO_VIEW_H
24
25
26 #include "lib/dcpomatic_time.h"
27 #include "lib/exception_store.h"
28 #include "lib/signaller.h"
29 #include "lib/timer.h"
30 #include "lib/types.h"
31 #include <wx/wx.h>
32 #include <boost/signals2.hpp>
33 #include <boost/thread.hpp>
34
35
36 class Image;
37 class wxWindow;
38 class FilmViewer;
39 class Player;
40 class PlayerVideo;
41
42
43 class VideoView : public ExceptionStore, public Signaller
44 {
45 public:
46         VideoView (FilmViewer* viewer);
47         virtual ~VideoView () {}
48
49         VideoView (VideoView const&) = delete;
50         VideoView& operator= (VideoView const&) = delete;
51
52         /** @return the thing displaying the image */
53         virtual wxWindow* get () const = 0;
54         /** Re-make and display the image from the current _player_video */
55         virtual void update () = 0;
56         /** Called when playback starts */
57         virtual void start ();
58         /** Called when playback stops */
59         virtual void stop () {}
60
61         enum NextFrameResult {
62                 FAIL,
63                 AGAIN,
64                 SUCCESS
65         };
66
67         /** Get the next frame and display it; used after seek */
68         virtual NextFrameResult display_next_frame (bool) = 0;
69
70         void clear ();
71         bool reset_metadata (std::shared_ptr<const Film> film, dcp::Size player_video_container_size);
72
73         /** Emitted from the GUI thread when our display changes in size */
74         boost::signals2::signal<void()> Sized;
75         /** Emitted from the GUI thread when a lot of frames are being dropped */
76         boost::signals2::signal<void()> TooManyDropped;
77
78
79         /* Accessors for FilmViewer */
80
81         int dropped () const {
82                 boost::mutex::scoped_lock lm (_mutex);
83                 return _dropped;
84         }
85
86         int errored () const {
87                 boost::mutex::scoped_lock lm (_mutex);
88                 return _errored;
89         }
90
91         int gets () const {
92                 boost::mutex::scoped_lock lm (_mutex);
93                 return _gets;
94         }
95
96         StateTimer const & state_timer () const {
97                 return _state_timer;
98         }
99
100         dcpomatic::DCPTime position () const {
101                 boost::mutex::scoped_lock lm (_mutex);
102                 return _player_video.second;
103         }
104
105
106         /* Setters for FilmViewer so it can tell us our state and
107          * we can then use (thread) safely.
108          */
109
110         void set_video_frame_rate (int r) {
111                 boost::mutex::scoped_lock lm (_mutex);
112                 _video_frame_rate = r;
113         }
114
115         void set_length (dcpomatic::DCPTime len) {
116                 boost::mutex::scoped_lock lm (_mutex);
117                 _length = len;
118         }
119
120         void set_eyes (Eyes eyes) {
121                 boost::mutex::scoped_lock lm (_mutex);
122                 _eyes = eyes;
123         }
124
125         void set_three_d (bool t) {
126                 boost::mutex::scoped_lock lm (_mutex);
127                 _three_d = t;
128         }
129
130         void set_optimise_for_j2k (bool o) {
131                 _optimise_for_j2k = o;
132         }
133
134 protected:
135         NextFrameResult get_next_frame (bool non_blocking);
136         boost::optional<int> time_until_next_frame () const;
137         dcpomatic::DCPTime one_video_frame () const;
138
139         wxColour pad_colour () const;
140
141         wxColour outline_content_colour () const {
142                 return wxColour(255, 0, 0);
143         }
144
145         wxColour outline_subtitles_colour () const {
146                 return wxColour(0, 255, 0);
147         }
148
149         wxColour crop_guess_colour () const {
150                 return wxColour(0, 0, 255);
151         }
152
153         int video_frame_rate () const {
154                 boost::mutex::scoped_lock lm (_mutex);
155                 return _video_frame_rate;
156         }
157
158         dcpomatic::DCPTime length () const {
159                 boost::mutex::scoped_lock lm (_mutex);
160                 return _length;
161         }
162
163         std::pair<std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime> player_video () const {
164                 boost::mutex::scoped_lock lm (_mutex);
165                 return _player_video;
166         }
167
168         void add_dropped ();
169
170         void add_get () {
171                 boost::mutex::scoped_lock lm (_mutex);
172                 ++_gets;
173         }
174
175         FilmViewer* _viewer;
176
177         StateTimer _state_timer;
178
179         bool _optimise_for_j2k = false;
180
181 private:
182         /** Mutex protecting all the state in this class */
183         mutable boost::mutex _mutex;
184
185         std::pair<std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime> _player_video;
186         int _video_frame_rate = 0;
187         /** length of the film we are playing, or 0 if there is none */
188         dcpomatic::DCPTime _length;
189         Eyes _eyes = Eyes::LEFT;
190         bool _three_d = false;
191
192         int _dropped = 0;
193         struct timeval _dropped_check_period_start;
194         int _errored = 0;
195         int _gets = 0;
196 };
197
198
199 #endif