Separate out SPL/SPLEntry; start trying to make player read SPLs sensibly.
[dcpomatic.git] / src / wx / controls.cc
1 /*
2     Copyright (C) 2018 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 "controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "playhead_to_timecode_dialog.h"
25 #include "playhead_to_frame_dialog.h"
26 #include "content_view.h"
27 #include "lib/job_manager.h"
28 #include "lib/player_video.h"
29 #include "lib/dcp_content.h"
30 #include "lib/job.h"
31 #include "lib/examine_content_job.h"
32 #include "lib/content_factory.h"
33 #include "lib/cross.h"
34 #include <dcp/dcp.h>
35 #include <dcp/cpl.h>
36 #include <dcp/reel.h>
37 #include <dcp/reel_picture_asset.h>
38 #include <wx/wx.h>
39 #include <wx/tglbtn.h>
40 #include <wx/listctrl.h>
41 #include <wx/progdlg.h>
42
43 using std::string;
44 using std::list;
45 using std::cout;
46 using std::make_pair;
47 using std::exception;
48 using boost::optional;
49 using boost::shared_ptr;
50 using boost::weak_ptr;
51 using boost::dynamic_pointer_cast;
52
53 Controls::Controls (wxWindow* parent, shared_ptr<FilmViewer> viewer, bool editor_controls)
54         : wxPanel (parent)
55         , _viewer (viewer)
56         , _slider_being_moved (false)
57         , _was_running_before_slider (false)
58         , _outline_content (0)
59         , _eye (0)
60         , _jump_to_selected (0)
61         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
62         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
63         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
64         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
65         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
66         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
67 #ifdef DCPOMATIC_VARIANT_SWAROOP
68         , _play_button (new wxButton(this, wxID_ANY, _("Play")))
69         , _pause_button (new wxButton(this, wxID_ANY, _("Pause")))
70         , _stop_button (new wxButton(this, wxID_ANY, _("Stop")))
71 #else
72         , _play_button (new wxToggleButton(this, wxID_ANY, _("Play")))
73 #endif
74 {
75         _v_sizer = new wxBoxSizer (wxVERTICAL);
76         SetSizer (_v_sizer);
77
78         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
79         if (editor_controls) {
80                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
81                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
82                 _eye = new wxChoice (this, wxID_ANY);
83                 _eye->Append (_("Left"));
84                 _eye->Append (_("Right"));
85                 _eye->SetSelection (0);
86                 view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
87                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
88                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
89         }
90
91         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
92
93         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
94
95         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
96         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
97         left_sizer->Add (_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
98
99         _content_view = new ContentView (this, _film);
100         left_sizer->Add (_content_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
101
102         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
103         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
104
105         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
106         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
107         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
108         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
109
110         _v_sizer->Add (e_sizer, 1, wxEXPAND);
111
112         _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
113         _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
114
115         _content_view->Show (false);
116         _spl_view->Show (false);
117         _current_spl_view->Show (false);
118         _log->Show (false);
119
120         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
121
122         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
123         time_sizer->Add (_frame_number, 0, wxEXPAND);
124         time_sizer->Add (_timecode, 0, wxEXPAND);
125
126         h_sizer->Add (_rewind_button, 0, wxALL, 2);
127         h_sizer->Add (_back_button, 0, wxALL, 2);
128         h_sizer->Add (time_sizer, 0, wxEXPAND);
129         h_sizer->Add (_forward_button, 0, wxALL, 2);
130         h_sizer->Add (_play_button, 0, wxEXPAND);
131 #ifdef DCPOMATIC_VARIANT_SWAROOP
132         h_sizer->Add (_pause_button, 0, wxEXPAND);
133         h_sizer->Add (_stop_button, 0, wxEXPAND);
134 #endif
135         h_sizer->Add (_slider, 1, wxEXPAND);
136
137         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
138
139         _frame_number->SetMinSize (wxSize (84, -1));
140         _rewind_button->SetMinSize (wxSize (32, -1));
141         _back_button->SetMinSize (wxSize (32, -1));
142         _forward_button->SetMinSize (wxSize (32, -1));
143
144         if (_eye) {
145                 _eye->Bind (wxEVT_CHOICE, boost::bind (&Controls::eye_changed, this));
146         }
147         if (_outline_content) {
148                 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::outline_content_changed, this));
149         }
150
151         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,    boost::bind(&Controls::slider_moved,    this, false));
152         _slider->Bind           (wxEVT_SCROLL_PAGEUP,        boost::bind(&Controls::slider_moved,    this, true));
153         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,      boost::bind(&Controls::slider_moved,    this, true));
154         _slider->Bind           (wxEVT_SCROLL_CHANGED,       boost::bind(&Controls::slider_released, this));
155 #ifdef DCPOMATIC_VARIANT_SWAROOP
156         _play_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::play_clicked,    this));
157         _pause_button->Bind     (wxEVT_BUTTON,               boost::bind(&Controls::pause_clicked,   this));
158         _stop_button->Bind      (wxEVT_BUTTON,               boost::bind(&Controls::stop_clicked,    this));
159 #else
160         _play_button->Bind      (wxEVT_TOGGLEBUTTON,         boost::bind(&Controls::play_clicked,    this));
161 #endif
162         _rewind_button->Bind    (wxEVT_LEFT_DOWN,            boost::bind(&Controls::rewind_clicked,  this, _1));
163         _back_button->Bind      (wxEVT_LEFT_DOWN,            boost::bind(&Controls::back_clicked,    this, _1));
164         _forward_button->Bind   (wxEVT_LEFT_DOWN,            boost::bind(&Controls::forward_clicked, this, _1));
165         _frame_number->Bind     (wxEVT_LEFT_DOWN,            boost::bind(&Controls::frame_number_clicked, this));
166         _timecode->Bind         (wxEVT_LEFT_DOWN,            boost::bind(&Controls::timecode_clicked, this));
167         if (_jump_to_selected) {
168                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&Controls::jump_to_selected_clicked, this));
169                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
170         }
171         _spl_view->Bind         (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&Controls::spl_selection_changed, this));
172         _spl_view->Bind         (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&Controls::spl_selection_changed, this));
173
174         _viewer->PositionChanged.connect (boost::bind(&Controls::position_changed, this));
175         _viewer->Started.connect (boost::bind(&Controls::started, this));
176         _viewer->Stopped.connect (boost::bind(&Controls::stopped, this));
177         _viewer->FilmChanged.connect (boost::bind(&Controls::film_changed, this));
178         _viewer->ImageChanged.connect (boost::bind(&Controls::image_changed, this, _1));
179
180         film_changed ();
181
182         setup_sensitivity ();
183         update_playlist_directory ();
184
185         JobManager::instance()->ActiveJobsChanged.connect (
186                 bind (&Controls::active_jobs_changed, this, _2)
187                 );
188
189         _config_changed_connection = Config::instance()->Changed.connect (bind(&Controls::config_changed, this, _1));
190         config_changed (Config::OTHER);
191 }
192
193 void
194 Controls::spl_selection_changed ()
195 {
196         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
197         if (selected == -1) {
198                 _current_spl_view->DeleteAllItems ();
199                 return;
200         }
201
202         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
203
204         int N = 0;
205         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
206                 wxListItem it;
207                 it.SetId (N);
208                 it.SetColumn (0);
209                 it.SetText (std_to_wx(i.name));
210                 _current_spl_view->InsertItem (it);
211                 film->add_content (i.content);
212                 ++N;
213         }
214
215         _viewer->set_film (film);
216 }
217
218 void
219 Controls::config_changed (int property)
220 {
221         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
222                 _content_view->update ();
223         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
224                 update_playlist_directory ();
225         } else {
226                 setup_sensitivity ();
227         }
228 }
229
230 void
231 Controls::started ()
232 {
233 #ifdef DCPOMATIC_VARIANT_SWAROOP
234         _play_button->Enable (false);
235         _pause_button->Enable (true);
236 #else
237         _play_button->SetValue (true);
238 #endif
239         setup_sensitivity ();
240 }
241
242 void
243 Controls::stopped ()
244 {
245 #ifdef DCPOMATIC_VARIANT_SWAROOP
246         _play_button->Enable (true);
247         _pause_button->Enable (false);
248 #else
249         _play_button->SetValue (false);
250 #endif
251         setup_sensitivity ();
252 }
253
254 void
255 Controls::position_changed ()
256 {
257         if (!_slider_being_moved) {
258                 update_position_label ();
259                 update_position_slider ();
260         }
261 }
262
263 void
264 Controls::eye_changed ()
265 {
266         _viewer->set_eyes (_eye->GetSelection() == 0 ? EYES_LEFT : EYES_RIGHT);
267 }
268
269 void
270 Controls::outline_content_changed ()
271 {
272         _viewer->set_outline_content (_outline_content->GetValue());
273 }
274
275 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
276 void
277 Controls::slider_moved (bool page)
278 {
279         if (!_film) {
280                 return;
281         }
282
283         if (!page && !_slider_being_moved) {
284                 /* This is the first event of a drag; stop playback for the duration of the drag */
285                 _was_running_before_slider = _viewer->stop ();
286                 _slider_being_moved = true;
287         }
288
289         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
290         t = t.round (_film->video_frame_rate());
291         /* Ensure that we hit the end of the film at the end of the slider */
292         if (t >= _film->length ()) {
293                 t = _film->length() - _viewer->one_video_frame();
294         }
295         _viewer->seek (t, false);
296         update_position_label ();
297 }
298
299 void
300 Controls::slider_released ()
301 {
302         if (_was_running_before_slider) {
303                 /* Restart after a drag */
304                 _viewer->start ();
305         }
306         _slider_being_moved = false;
307 }
308
309 void
310 Controls::play_clicked ()
311 {
312 #ifdef DCPOMATIC_VARIANT_SWAROOP
313         _viewer->start ();
314 #else
315         check_play_state ();
316 #endif
317 }
318
319
320 #ifndef DCPOMATIC_VARIANT_SWAROOP
321 void
322 Controls::check_play_state ()
323 {
324         if (!_film || _film->video_frame_rate() == 0) {
325                 return;
326         }
327
328         if (_play_button->GetValue()) {
329                 _viewer->start ();
330         } else {
331                 _viewer->stop ();
332         }
333 }
334 #endif
335
336 void
337 Controls::update_position_slider ()
338 {
339         if (!_film) {
340                 _slider->SetValue (0);
341                 return;
342         }
343
344         DCPTime const len = _film->length ();
345
346         if (len.get ()) {
347                 int const new_slider_position = 4096 * _viewer->position().get() / len.get();
348                 if (new_slider_position != _slider->GetValue()) {
349                         _slider->SetValue (new_slider_position);
350                 }
351         }
352 }
353
354 void
355 Controls::update_position_label ()
356 {
357         if (!_film) {
358                 _frame_number->SetLabel ("0");
359                 _timecode->SetLabel ("0:0:0.0");
360                 return;
361         }
362
363         double const fps = _film->video_frame_rate ();
364         /* Count frame number from 1 ... not sure if this is the best idea */
365         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_viewer->position().seconds() * fps) + 1));
366         _timecode->SetLabel (time_to_timecode (_viewer->position(), fps));
367 }
368
369 void
370 Controls::active_jobs_changed (optional<string> j)
371 {
372         _active_job = j;
373         setup_sensitivity ();
374 }
375
376 DCPTime
377 Controls::nudge_amount (wxKeyboardState& ev)
378 {
379         DCPTime amount = _viewer->one_video_frame ();
380
381         if (ev.ShiftDown() && !ev.ControlDown()) {
382                 amount = DCPTime::from_seconds (1);
383         } else if (!ev.ShiftDown() && ev.ControlDown()) {
384                 amount = DCPTime::from_seconds (10);
385         } else if (ev.ShiftDown() && ev.ControlDown()) {
386                 amount = DCPTime::from_seconds (60);
387         }
388
389         return amount;
390 }
391
392 void
393 Controls::rewind_clicked (wxMouseEvent& ev)
394 {
395         _viewer->seek (DCPTime(), true);
396         ev.Skip();
397 }
398
399 void
400 Controls::back_frame ()
401 {
402         _viewer->seek_by (-_viewer->one_video_frame(), true);
403 }
404
405 void
406 Controls::forward_frame ()
407 {
408         _viewer->seek_by (_viewer->one_video_frame(), true);
409 }
410
411 void
412 Controls::back_clicked (wxKeyboardState& ev)
413 {
414         _viewer->seek_by (-nudge_amount(ev), true);
415 }
416
417 void
418 Controls::forward_clicked (wxKeyboardState& ev)
419 {
420         _viewer->seek_by (nudge_amount(ev), true);
421 }
422
423 void
424 Controls::setup_sensitivity ()
425 {
426         /* examine content is the only job which stops the viewer working */
427         bool const active_job = _active_job && *_active_job != "examine_content";
428         bool const c = _film && !_film->content().empty() && !active_job;
429
430         _slider->Enable (c);
431         _rewind_button->Enable (c);
432         _back_button->Enable (c);
433         _forward_button->Enable (c);
434 #ifdef DCPOMATIC_VARIANT_SWAROOP
435         _play_button->Enable (c && !_viewer->playing());
436         _pause_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT) && _viewer->playing());
437         _stop_button->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
438         _slider->Enable (c && (!_current_kind || _current_kind != dcp::ADVERTISEMENT));
439 #else
440         _play_button->Enable (c);
441 #endif
442         if (_outline_content) {
443                 _outline_content->Enable (c);
444         }
445         _frame_number->Enable (c);
446         _timecode->Enable (c);
447         if (_jump_to_selected) {
448                 _jump_to_selected->Enable (c);
449         }
450
451         if (_eye) {
452                 _eye->Enable (c && _film->three_d ());
453         }
454 }
455
456 void
457 Controls::timecode_clicked ()
458 {
459         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
460         if (dialog->ShowModal() == wxID_OK) {
461                 _viewer->seek (dialog->get(), true);
462         }
463         dialog->Destroy ();
464 }
465
466 void
467 Controls::frame_number_clicked ()
468 {
469         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
470         if (dialog->ShowModal() == wxID_OK) {
471                 _viewer->seek (dialog->get(), true);
472         }
473         dialog->Destroy ();
474 }
475
476 void
477 Controls::jump_to_selected_clicked ()
478 {
479         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
480 }
481
482 void
483 Controls::film_changed ()
484 {
485         shared_ptr<Film> film = _viewer->film ();
486
487         if (_film == film) {
488                 return;
489         }
490
491         _film = film;
492
493         setup_sensitivity ();
494
495         update_position_slider ();
496         update_position_label ();
497
498         _content_view->set_film (film);
499         _content_view->update ();
500 }
501
502 shared_ptr<Film>
503 Controls::film () const
504 {
505         return _film;
506 }
507
508 void
509 Controls::show_extended_player_controls (bool s)
510 {
511         _content_view->Show (s);
512         _spl_view->Show (s);
513         if (s) {
514                 _content_view->update ();
515                 update_playlist_directory ();
516         }
517         _current_spl_view->Show (s);
518         _log->Show (s);
519         _v_sizer->Layout ();
520 }
521
522 void
523 Controls::add_playlist_to_list (SPL spl)
524 {
525         int const N = _spl_view->GetItemCount();
526
527         wxListItem it;
528         it.SetId(N);
529         it.SetColumn(0);
530         it.SetText (std_to_wx(spl.name()));
531         _spl_view->InsertItem (it);
532 }
533
534 void
535 Controls::update_playlist_directory ()
536 {
537         if (!_spl_view->IsShown()) {
538                 return;
539         }
540
541         using namespace boost::filesystem;
542
543         _spl_view->DeleteAllItems ();
544         optional<path> dir = Config::instance()->player_playlist_directory();
545         if (!dir) {
546                 return;
547         }
548
549         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
550                 try {
551                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
552                                 SPL spl;
553                                 spl.read (i->path(), _content_view);
554                                 _playlists.push_back (spl);
555                                 add_playlist_to_list (spl);
556                         }
557                 } catch (exception& e) {
558                         /* Never mind */
559                 }
560         }
561 }
562
563 #ifdef DCPOMATIC_VARIANT_SWAROOP
564 void
565 Controls::pause_clicked ()
566 {
567         _viewer->stop ();
568 }
569
570 void
571 Controls::stop_clicked ()
572 {
573         _viewer->stop ();
574         _viewer->seek (DCPTime(), true);
575 }
576 #endif
577
578 void
579 Controls::log (wxString s)
580 {
581         struct timeval time;
582         gettimeofday (&time, 0);
583         char buffer[64];
584         time_t const sec = time.tv_sec;
585         struct tm* t = localtime (&sec);
586         strftime (buffer, 64, "%c", t);
587         wxString ts = std_to_wx(string(buffer)) + N_(": ");
588         _log->SetValue(_log->GetValue() + ts + s + "\n");
589 }
590
591 void
592 Controls::image_changed (boost::weak_ptr<PlayerVideo> weak_pv)
593 {
594 #ifdef DCPOMATIC_VARIANT_SWAROOP
595         shared_ptr<PlayerVideo> pv = weak_pv.lock ();
596         if (!pv) {
597                 return;
598         }
599
600         shared_ptr<Content> c = pv->content().lock();
601         if (!c) {
602                 return;
603         }
604
605         shared_ptr<DCPContent> dc = dynamic_pointer_cast<DCPContent> (c);
606         if (!dc) {
607                 return;
608         }
609
610         if (!_current_kind || *_current_kind != dc->content_kind()) {
611                 _current_kind = dc->content_kind ();
612                 setup_sensitivity ();
613         }
614 #endif
615 }