Basic support for decryption of imported DCPs.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/film_viewer.cc
21  *  @brief A wx widget to view a preview of a Film.
22  */
23
24 #include <iostream>
25 #include <iomanip>
26 #include <wx/tglbtn.h>
27 #include <dcp/exceptions.h>
28 #include "lib/film.h"
29 #include "lib/ratio.h"
30 #include "lib/util.h"
31 #include "lib/job_manager.h"
32 #include "lib/image.h"
33 #include "lib/scaler.h"
34 #include "lib/exceptions.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/filter.h"
37 #include "lib/player.h"
38 #include "lib/player_video.h"
39 #include "lib/video_content.h"
40 #include "lib/video_decoder.h"
41 #include "lib/timer.h"
42 #include "film_viewer.h"
43 #include "wx_util.h"
44
45 using std::string;
46 using std::pair;
47 using std::min;
48 using std::max;
49 using std::cout;
50 using std::list;
51 using std::bad_alloc;
52 using std::make_pair;
53 using boost::shared_ptr;
54 using boost::dynamic_pointer_cast;
55 using boost::weak_ptr;
56 using dcp::Size;
57
58 FilmViewer::FilmViewer (shared_ptr<Film> f, wxWindow* p)
59         : wxPanel (p)
60         , _panel (new wxPanel (this))
61         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
62         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
63         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
64         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
65         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
66         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
67         , _last_get_accurate (true)
68 {
69 #ifndef __WXOSX__
70         _panel->SetDoubleBuffered (true);
71 #endif
72         
73         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
74         
75         _v_sizer = new wxBoxSizer (wxVERTICAL);
76         SetSizer (_v_sizer);
77
78         _v_sizer->Add (_panel, 1, wxEXPAND);
79
80         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
81
82         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
83         time_sizer->Add (_frame_number, 0, wxEXPAND);
84         time_sizer->Add (_timecode, 0, wxEXPAND);
85
86         h_sizer->Add (_back_button, 0, wxALL, 2);
87         h_sizer->Add (time_sizer, 0, wxEXPAND);
88         h_sizer->Add (_forward_button, 0, wxALL, 2);
89         h_sizer->Add (_play_button, 0, wxEXPAND);
90         h_sizer->Add (_slider, 1, wxEXPAND);
91
92         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
93
94         _frame_number->SetMinSize (wxSize (84, -1));
95         _back_button->SetMinSize (wxSize (32, -1));
96         _forward_button->SetMinSize (wxSize (32, -1));
97
98         _panel->Bind          (wxEVT_PAINT,                        boost::bind (&FilmViewer::paint_panel,     this));
99         _panel->Bind          (wxEVT_SIZE,                         boost::bind (&FilmViewer::panel_sized,     this, _1));
100         _slider->Bind         (wxEVT_SCROLL_THUMBTRACK,            boost::bind (&FilmViewer::slider_moved,    this));
101         _slider->Bind         (wxEVT_SCROLL_PAGEUP,                boost::bind (&FilmViewer::slider_moved,    this));
102         _slider->Bind         (wxEVT_SCROLL_PAGEDOWN,              boost::bind (&FilmViewer::slider_moved,    this));
103         _play_button->Bind    (wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, boost::bind (&FilmViewer::play_clicked,    this));
104         _timer.Bind           (wxEVT_TIMER,                        boost::bind (&FilmViewer::timer,           this));
105         _back_button->Bind    (wxEVT_COMMAND_BUTTON_CLICKED,       boost::bind (&FilmViewer::back_clicked,    this));
106         _forward_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,       boost::bind (&FilmViewer::forward_clicked, this));
107
108         set_film (f);
109
110         JobManager::instance()->ActiveJobsChanged.connect (
111                 bind (&FilmViewer::active_jobs_changed, this, _1)
112                 );
113 }
114
115 void
116 FilmViewer::set_film (shared_ptr<Film> f)
117 {
118         if (_film == f) {
119                 return;
120         }
121
122         _film = f;
123
124         _frame.reset ();
125         
126         _slider->SetValue (0);
127         set_position_text ();
128         
129         if (!_film) {
130                 return;
131         }
132
133         try {
134                 _player = f->make_player ();
135         } catch (bad_alloc) {
136                 error_dialog (this, _("There is not enough free memory to do that."));
137                 _film.reset ();
138                 return;
139         }
140         
141         _player->set_approximate_size ();
142         _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
143
144         calculate_sizes ();
145         get (_position, _last_get_accurate);
146 }
147
148 void
149 FilmViewer::get (DCPTime p, bool accurate)
150 {
151         if (!_player) {
152                 return;
153         }
154
155         list<shared_ptr<PlayerVideo> > pvf = _player->get_video (p, accurate);
156         if (!pvf.empty ()) {
157                 try {
158                         _frame = pvf.front()->image (true);
159                         _frame = _frame->scale (_frame->size(), Scaler::from_id ("fastbilinear"), PIX_FMT_RGB24, false);
160                         _position = pvf.front()->time ();
161                 } catch (dcp::DCPReadError& e) {
162                         /* This can happen on the following sequence of events:
163                          * - load encrypted DCP
164                          * - add KDM
165                          * - DCP is examined again, which sets its "playable" flag to 1
166                          * - as a side effect of the exam, the viewer is updated using the old pieces
167                          * - the DCPDecoder in the old piece gives us an encrypted frame
168                          * - then, the pieces are re-made (but too late).
169                          *
170                          * I hope there's a better way to handle this ...
171                          */
172                         _frame.reset ();
173                         _position = p;
174                 }
175         } else {
176                 _frame.reset ();
177                 _position = p;
178         }
179
180         set_position_text ();
181         _panel->Refresh ();
182         _panel->Update ();
183
184         _last_get_accurate = accurate;
185 }
186
187 void
188 FilmViewer::timer ()
189 {
190         get (_position + DCPTime::from_frames (1, _film->video_frame_rate ()), true);
191
192         DCPTime const len = _film->length ();
193
194         if (len.get ()) {
195                 int const new_slider_position = 4096 * _position.get() / len.get();
196                 if (new_slider_position != _slider->GetValue()) {
197                         _slider->SetValue (new_slider_position);
198                 }
199         }
200 }
201
202
203 void
204 FilmViewer::paint_panel ()
205 {
206         wxPaintDC dc (_panel);
207
208         if (!_frame || !_film || !_out_size.width || !_out_size.height) {
209                 dc.Clear ();
210                 return;
211         }
212
213         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
214         wxBitmap frame_bitmap (frame);
215         dc.DrawBitmap (frame_bitmap, 0, 0);
216
217         if (_out_size.width < _panel_size.width) {
218                 wxPen p (GetBackgroundColour ());
219                 wxBrush b (GetBackgroundColour ());
220                 dc.SetPen (p);
221                 dc.SetBrush (b);
222                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
223         }
224
225         if (_out_size.height < _panel_size.height) {
226                 wxPen p (GetBackgroundColour ());
227                 wxBrush b (GetBackgroundColour ());
228                 dc.SetPen (p);
229                 dc.SetBrush (b);
230                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
231         }               
232 }
233
234
235 void
236 FilmViewer::slider_moved ()
237 {
238         if (!_film) {
239                 return;
240         }
241
242         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
243         /* Ensure that we hit the end of the film at the end of the slider */
244         if (t >= _film->length ()) {
245                 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
246         }
247         get (t, false);
248 }
249
250 void
251 FilmViewer::panel_sized (wxSizeEvent& ev)
252 {
253         _panel_size.width = ev.GetSize().GetWidth();
254         _panel_size.height = ev.GetSize().GetHeight();
255         calculate_sizes ();
256         get (_position, _last_get_accurate);
257 }
258
259 void
260 FilmViewer::calculate_sizes ()
261 {
262         if (!_film || !_player) {
263                 return;
264         }
265
266         Ratio const * container = _film->container ();
267         
268         float const panel_ratio = _panel_size.ratio ();
269         float const film_ratio = container ? container->ratio () : 1.78;
270                         
271         if (panel_ratio < film_ratio) {
272                 /* panel is less widscreen than the film; clamp width */
273                 _out_size.width = _panel_size.width;
274                 _out_size.height = _out_size.width / film_ratio;
275         } else {
276                 /* panel is more widescreen than the film; clamp height */
277                 _out_size.height = _panel_size.height;
278                 _out_size.width = _out_size.height * film_ratio;
279         }
280
281         /* Catch silly values */
282         _out_size.width = max (64, _out_size.width);
283         _out_size.height = max (64, _out_size.height);
284
285         /* The player will round its image down to the nearest 4 pixels
286            to speed up its scale, so do similar here to avoid black borders
287            around things.  This is a bit of a hack.
288         */
289         _out_size.width &= ~3;
290         _out_size.height &= ~3;
291
292         _player->set_video_container_size (_out_size);
293 }
294
295 void
296 FilmViewer::play_clicked ()
297 {
298         check_play_state ();
299 }
300
301 void
302 FilmViewer::check_play_state ()
303 {
304         if (!_film || _film->video_frame_rate() == 0) {
305                 return;
306         }
307         
308         if (_play_button->GetValue()) {
309                 _timer.Start (1000 / _film->video_frame_rate());
310         } else {
311                 _timer.Stop ();
312         }
313 }
314
315 void
316 FilmViewer::set_position_text ()
317 {
318         if (!_film) {
319                 _frame_number->SetLabel ("0");
320                 _timecode->SetLabel ("0:0:0.0");
321                 return;
322         }
323                 
324         double const fps = _film->video_frame_rate ();
325         /* Count frame number from 1 ... not sure if this is the best idea */
326         _frame_number->SetLabel (wxString::Format (wxT("%d"), int (rint (_position.seconds() * fps)) + 1));
327         
328         double w = _position.seconds ();
329         int const h = (w / 3600);
330         w -= h * 3600;
331         int const m = (w / 60);
332         w -= m * 60;
333         int const s = floor (w);
334         w -= s;
335         int const f = rint (w * fps);
336         _timecode->SetLabel (wxString::Format (wxT("%02d:%02d:%02d.%02d"), h, m, s, f));
337 }
338
339 void
340 FilmViewer::active_jobs_changed (bool a)
341 {
342         if (a) {
343                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
344                 list<shared_ptr<Job> >::iterator i = jobs.begin ();             
345                 while (i != jobs.end() && boost::dynamic_pointer_cast<ExamineContentJob> (*i) == 0) {
346                         ++i;
347                 }
348                 
349                 if (i == jobs.end() || (*i)->finished()) {
350                         /* no examine content job running, so we're ok to use the viewer */
351                         a = false;
352                 }
353         }
354                         
355         _slider->Enable (!a);
356         _play_button->Enable (!a);
357 }
358
359 void
360 FilmViewer::back_clicked ()
361 {
362         DCPTime p = _position - DCPTime::from_frames (1, _film->video_frame_rate ());
363         if (p < DCPTime ()) {
364                 p = DCPTime ();
365         }
366
367         get (p, true);
368 }
369
370 void
371 FilmViewer::forward_clicked ()
372 {
373         get (_position + DCPTime::from_frames (1, _film->video_frame_rate ()), true);
374 }
375
376 void
377 FilmViewer::player_changed (bool frequent)
378 {
379         if (frequent) {
380                 return;
381         }
382
383         calculate_sizes ();
384         get (_position, _last_get_accurate);
385 }