Barely-functioning GL playback with new arrangement.
[dcpomatic.git] / src / wx / simple_video_view.cc
1 /*
2     Copyright (C) 2019 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 #include "simple_video_view.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "closed_captions_dialog.h"
25 #include "lib/image.h"
26 #include "lib/dcpomatic_log.h"
27 #include "lib/butler.h"
28 #include <dcp/util.h>
29 #include <wx/wx.h>
30 #include <boost/bind.hpp>
31
32 using std::max;
33 using std::string;
34 using boost::optional;
35 using boost::shared_ptr;
36 using namespace dcpomatic;
37
38 SimpleVideoView::SimpleVideoView (FilmViewer* viewer, wxWindow* parent)
39         : VideoView (viewer)
40 {
41         _panel = new wxPanel (parent);
42
43 #ifndef __WXOSX__
44         _panel->SetDoubleBuffered (true);
45 #endif
46
47         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
48         _panel->SetBackgroundColour (*wxBLACK);
49
50         _panel->Bind (wxEVT_PAINT, boost::bind (&SimpleVideoView::paint, this));
51         _panel->Bind (wxEVT_SIZE, boost::bind(boost::ref(Sized)));
52
53         _timer.Bind (wxEVT_TIMER, boost::bind(&SimpleVideoView::timer, this));
54 }
55
56 void
57 SimpleVideoView::paint ()
58 {
59         _viewer->state_timer().set("paint-panel");
60         wxPaintDC dc (_panel);
61
62         dcp::Size const out_size = _viewer->out_size ();
63         wxSize const panel_size = _panel->GetSize ();
64
65 #ifdef DCPOMATIC_VARIANT_SWAROOP
66         if (_viewer->background_image()) {
67                 dc.Clear ();
68                 optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
69                 if (bg) {
70                         wxImage image (std_to_wx(bg->string()));
71                         wxBitmap bitmap (image);
72                         dc.DrawBitmap (bitmap, max(0, (panel_size.GetWidth() - image.GetSize().GetWidth()) / 2), max(0, (panel_size.GetHeight() - image.GetSize().GetHeight()) / 2));
73                 }
74                 return;
75         }
76 #endif
77
78         if (!out_size.width || !out_size.height || !_image || out_size != _image->size()) {
79                 dc.Clear ();
80         } else {
81
82                 wxImage frame (out_size.width, out_size.height, _image->data()[0], true);
83                 wxBitmap frame_bitmap (frame);
84                 dc.DrawBitmap (frame_bitmap, 0, max(0, (panel_size.GetHeight() - out_size.height) / 2));
85
86 #ifdef DCPOMATIC_VARIANT_SWAROOP
87                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
88                 int64_t n = _viewer->position().get() / period.get();
89                 DCPTime from(n * period.get());
90                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
91                 if (from <= _viewer->position() && _viewer->position() <= to) {
92                         if (!_in_watermark) {
93                                 _in_watermark = true;
94                                 _watermark_x = rand() % panel_size.GetWidth();
95                                 _watermark_y = rand() % panel_size.GetHeight();
96                         }
97                         dc.SetTextForeground(*wxWHITE);
98                         string wm = Config::instance()->player_watermark_theatre();
99                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
100                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
101                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
102                 } else {
103                         _in_watermark = false;
104                 }
105 #endif
106         }
107
108         if (out_size.width < panel_size.GetWidth()) {
109                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
110                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
111                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
112                 dc.SetPen (p);
113                 dc.SetBrush (b);
114                 dc.DrawRectangle (out_size.width, 0, panel_size.GetWidth() - out_size.width, panel_size.GetHeight());
115         }
116
117         if (out_size.height < panel_size.GetHeight()) {
118                 wxPen   p (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
119                 wxBrush b (_viewer->pad_black() ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
120                 dc.SetPen (p);
121                 dc.SetBrush (b);
122                 int const gap = (panel_size.GetHeight() - out_size.height) / 2;
123                 dc.DrawRectangle (0, 0, panel_size.GetWidth(), gap);
124                 dc.DrawRectangle (0, gap + out_size.height + 1, panel_size.GetWidth(), gap + 1);
125         }
126
127         if (_viewer->outline_content()) {
128                 Position<int> inter_position = _viewer->inter_position ();
129                 dcp::Size inter_size = _viewer->inter_size ();
130                 wxPen p (wxColour (255, 0, 0), 2);
131                 dc.SetPen (p);
132                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
133                 dc.DrawRectangle (inter_position.x, inter_position.y + (panel_size.GetHeight() - out_size.height) / 2, inter_size.width, inter_size.height);
134         }
135         _viewer->state_timer().unset();
136 }
137
138 void
139 SimpleVideoView::update ()
140 {
141         _panel->Refresh ();
142         _panel->Update ();
143 }
144
145 void
146 SimpleVideoView::timer ()
147 {
148         if (!_viewer->film() || !_viewer->playing()) {
149                 return;
150         }
151
152         get (false);
153         DCPTime const next = _viewer->position() + _viewer->one_video_frame();
154
155         if (next >= _viewer->film()->length()) {
156                 _viewer->stop ();
157                 _viewer->Finished ();
158                 return;
159         }
160
161         LOG_DEBUG_PLAYER("%1 -> %2; delay %3", next.seconds(), _viewer->time().seconds(), max((next.seconds() - _viewer->time().seconds()) * 1000, 1.0));
162         _timer.Start (_viewer->time_until_next_frame(), wxTIMER_ONE_SHOT);
163
164         if (_viewer->butler()) {
165                 _viewer->butler()->rethrow ();
166         }
167 }
168
169 void
170 SimpleVideoView::start ()
171 {
172         timer ();
173 }
174
175 /** Try to get a frame from the butler and display it.
176  *  @param lazy true to return false quickly if no video is available quickly (i.e. we are waiting for the butler).
177  *  false to ask the butler to block until it has video (unless it is suspended).
178  *  @return true on success, false if we did nothing because it would have taken too long.
179  */
180 bool
181 SimpleVideoView::get (bool lazy)
182 {
183         bool r = get_next_frame (lazy);
184         if (!r) {
185                 if (lazy) {
186                         /* No video available; return saying we failed */
187                         return false;
188                 } else {
189                         /* Player was suspended; come back later */
190                         signal_manager->when_idle (boost::bind(&SimpleVideoView::get, this, false));
191                         return false;
192                 }
193         }
194
195         display_player_video ();
196         _viewer->PositionChanged ();
197
198         return true;
199 }
200
201 void
202 SimpleVideoView::display_player_video ()
203 {
204         if (!_player_video.first) {
205                 set_image (shared_ptr<Image>());
206                 _viewer->refresh_view ();
207                 return;
208         }
209
210         if (_viewer->playing() && (_viewer->time() - _player_video.second) > _viewer->one_video_frame()) {
211                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
212                    part if this frame is J2K).
213                 */
214                 _viewer->_video_position = _player_video.second;
215                 ++_viewer->_dropped;
216                 return;
217         }
218
219         /* In an ideal world, what we would do here is:
220          *
221          * 1. convert to XYZ exactly as we do in the DCP creation path.
222          * 2. convert back to RGB for the preview display, compensating
223          *    for the monitor etc. etc.
224          *
225          * but this is inefficient if the source is RGB.  Since we don't
226          * (currently) care too much about the precise accuracy of the preview's
227          * colour mapping (and we care more about its speed) we try to short-
228          * circuit this "ideal" situation in some cases.
229          *
230          * The content's specified colour conversion indicates the colourspace
231          * which the content is in (according to the user).
232          *
233          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
234          * image and convert it (from whatever the user has said it is) to RGB.
235          */
236
237         _viewer->_state_timer.set ("get image");
238
239         set_image (
240                 _player_video.first->image(bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true)
241                 );
242
243         _viewer->_state_timer.set ("ImageChanged");
244         _viewer->ImageChanged (_player_video.first);
245         _viewer->_state_timer.unset ();
246
247         _viewer->_video_position = _player_video.second;
248         _viewer->_inter_position = _player_video.first->inter_position ();
249         _viewer->_inter_size = _player_video.first->inter_size ();
250
251         _viewer->refresh_view ();
252
253         _viewer->_closed_captions_dialog->update (_viewer->time());
254 }