Accessor for ClosedCaptionsDialog.
[dcpomatic.git] / src / wx / swaroop_controls.cc
1 /*
2     Copyright (C) 2018-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 "swaroop_controls.h"
22 #include "film_viewer.h"
23 #include "wx_util.h"
24 #include "content_view.h"
25 #include "dcpomatic_button.h"
26 #include "static_text.h"
27 #include "lib/player_video.h"
28 #include "lib/dcp_content.h"
29 #include "lib/cross.h"
30 #include "lib/scoped_temporary.h"
31 #include "lib/internet.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/compose.hpp"
34 #include <dcp/raw_convert.h>
35 #include <dcp/exceptions.h>
36 #include <wx/listctrl.h>
37 #include <wx/progdlg.h>
38
39 using std::string;
40 using std::cout;
41 using std::exception;
42 using std::sort;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
46 using namespace dcpomatic;
47
48 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
49         : Controls (parent, viewer, false)
50         , _play_button (new Button(this, _("Play")))
51         , _pause_button (new Button(this, _("Pause")))
52         , _stop_button (new Button(this, _("Stop")))
53         , _next_button (new Button(this, "Next"))
54         , _previous_button (new Button(this, "Previous"))
55         , _current_disable_timeline (false)
56         , _current_disable_next (false)
57         , _timer (this)
58 {
59         _button_sizer->Add (_previous_button, 0, wxEXPAND);
60         _button_sizer->Add (_play_button, 0, wxEXPAND);
61         _button_sizer->Add (_pause_button, 0, wxEXPAND);
62         _button_sizer->Add (_stop_button, 0, wxEXPAND);
63         _button_sizer->Add (_next_button, 0, wxEXPAND);
64
65         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
66         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
67
68         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
69         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
70
71         wxFont subheading_font (*wxNORMAL_FONT);
72         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
73
74         wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
75         {
76                 wxStaticText* m = new StaticText (this, "Playlists");
77                 m->SetFont (subheading_font);
78                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
79         }
80         _refresh_spl_view = new Button (this, "Refresh");
81         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
82
83         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
84         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
85
86         _content_view = new ContentView (this);
87
88         wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
89         {
90                 wxStaticText* m = new StaticText (this, "Content");
91                 m->SetFont (subheading_font);
92                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
93         }
94         _refresh_content_view = new Button (this, "Refresh");
95         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
96
97         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
98         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
99
100         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
101         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
102         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
103         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
104         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
105
106         _v_sizer->Add (e_sizer, 1, wxEXPAND);
107
108         _play_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked,  this));
109         _pause_button->Bind    (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
110         _stop_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked,  this));
111         _next_button->Bind     (wxEVT_BUTTON, boost::bind(&SwaroopControls::next_clicked,  this));
112         _previous_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::previous_clicked,  this));
113         _spl_view->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&SwaroopControls::spl_selection_changed, this));
114         _spl_view->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
115         _viewer->Finished.connect (boost::bind(&SwaroopControls::viewer_finished, this));
116         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::update_playlist_directory, this));
117         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
118
119         /* Write position every two minutes if we're playing */
120         Bind (wxEVT_TIMER, boost::bind(&SwaroopControls::write_position, this));
121         _timer.Start (2 * 60 * 1000, wxTIMER_CONTINUOUS);
122
123         _content_view->update ();
124         update_playlist_directory ();
125
126         _viewer->set_background_image (true);
127 }
128
129 void
130 SwaroopControls::check_restart ()
131 {
132         cout << "check_restart called\n";
133         FILE* f = fopen_boost (Config::path("position"), "r");
134         if (!f) {
135                 cout << "could not open position file (" << errno << ")\n";
136                 return;
137         }
138
139         char id[64];
140         int index;
141         int64_t time;
142         fscanf (f, "%63s %d %ld", id, &index, &time);
143
144         cout << "Looking for playlist " << id << " to restart.\n";
145
146         for (size_t i = 0; i < _playlists.size(); ++i) {
147                 if (_playlists[i].id() == id) {
148                         cout << "Found playlist " << id << "\n";
149                         select_playlist (i, index);
150                         update_current_content ();
151                         _viewer->seek (DCPTime(time), false);
152                         _viewer->start ();
153                 }
154         }
155
156         fclose (f);
157 }
158
159 void
160 SwaroopControls::write_position ()
161 {
162         if (!_selected_playlist || !_viewer->playing()) {
163                 return;
164         }
165
166         FILE* f = fopen_boost (Config::path("position"), "w");
167         if (f) {
168                 string const p = _playlists[*_selected_playlist].id()
169                         + " " + dcp::raw_convert<string>(_selected_playlist_position)
170                         + " " + dcp::raw_convert<string>(_viewer->position().get());
171
172                 checked_fwrite (p.c_str(), p.length(), f, Config::path("position"));
173 #ifdef DCPOMATIC_LINUX
174                 fflush (f);
175                 fsync (fileno(f));
176 #endif
177                 fclose (f);
178         }
179 }
180
181 void
182 SwaroopControls::started ()
183 {
184         Controls::started ();
185         _play_button->Enable (false);
186         _pause_button->Enable (true);
187         _viewer->set_background_image (false);
188 }
189
190 /** Called when the viewer finishes a single piece of content, or it is explicitly stopped */
191 void
192 SwaroopControls::stopped ()
193 {
194         Controls::stopped ();
195         _play_button->Enable (true);
196         _pause_button->Enable (false);
197 }
198
199 void
200 SwaroopControls::deselect_playlist ()
201 {
202         long int const selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
203         if (selected != -1) {
204                 _selected_playlist = boost::none;
205                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
206         }
207         ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
208 }
209
210 void
211 SwaroopControls::decrement_allowed_shows ()
212 {
213         if (_selected_playlist) {
214                 SPL& spl = _playlists[*_selected_playlist];
215                 spl.decrement_allowed_shows();
216                 if (spl.path()) {
217                         spl.write (*spl.path());
218                 }
219         }
220 }
221
222 void
223 SwaroopControls::play_clicked ()
224 {
225         _viewer->start ();
226 }
227
228 void
229 SwaroopControls::setup_sensitivity ()
230 {
231         Controls::setup_sensitivity ();
232         bool const active_job = _active_job && *_active_job != "examine_content";
233         bool const c = _film && !_film->content().empty() && !active_job;
234         _play_button->Enable (c && !_viewer->playing());
235         _pause_button->Enable (_viewer->playing());
236         _slider->Enable (!_current_disable_timeline);
237         _spl_view->Enable (!_viewer->playing());
238         _next_button->Enable (!_current_disable_next && can_do_next());
239         _previous_button->Enable (can_do_previous());
240 }
241
242 void
243 SwaroopControls::pause_clicked ()
244 {
245         _viewer->stop ();
246 }
247
248 void
249 SwaroopControls::stop_clicked ()
250 {
251         _viewer->stop ();
252         _viewer->seek (DCPTime(), true);
253         if (_selected_playlist) {
254                 _selected_playlist_position = 0;
255                 update_current_content ();
256         }
257         _viewer->set_background_image (true);
258         decrement_allowed_shows ();
259         deselect_playlist ();
260 }
261
262 bool
263 SwaroopControls::can_do_previous ()
264 {
265         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
266 }
267
268 void
269 SwaroopControls::previous_clicked ()
270 {
271         if (!can_do_previous ()) {
272                 return;
273         }
274
275         _selected_playlist_position--;
276         update_current_content ();
277 }
278
279 bool
280 SwaroopControls::can_do_next ()
281 {
282         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
283 }
284
285 void
286 SwaroopControls::next_clicked ()
287 {
288         if (!can_do_next ()) {
289                 return;
290         }
291
292         _selected_playlist_position++;
293         update_current_content ();
294 }
295
296 void
297 SwaroopControls::log (wxString s)
298 {
299         optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
300         if (!log) {
301                 return;
302         }
303
304         struct timeval time;
305         gettimeofday (&time, 0);
306         char buffer[64];
307         time_t const sec = time.tv_sec;
308         struct tm* t = localtime (&sec);
309         strftime (buffer, 64, "%c", t);
310         wxString ts = std_to_wx(string(buffer)) + N_(": ");
311         FILE* f = fopen_boost (*log, "a");
312         if (!f) {
313                 return;
314         }
315         fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
316         fclose (f);
317 }
318
319 void
320 SwaroopControls::add_playlist_to_list (SPL spl)
321 {
322         int const N = _spl_view->GetItemCount();
323
324         wxListItem it;
325         it.SetId(N);
326         it.SetColumn(0);
327         string t = spl.name();
328         if (spl.missing()) {
329                 t += " (content missing)";
330         }
331         it.SetText (std_to_wx(t));
332         _spl_view->InsertItem (it);
333 }
334
335 struct SPLComparator
336 {
337         bool operator() (SPL const & a, SPL const & b) {
338                 return a.name() < b.name();
339         }
340 };
341
342 void
343 SwaroopControls::update_playlist_directory ()
344 {
345         using namespace boost::filesystem;
346
347         _spl_view->DeleteAllItems ();
348         optional<path> dir = Config::instance()->player_playlist_directory();
349         if (!dir) {
350                 return;
351         }
352
353         _playlists.clear ();
354
355         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
356                 try {
357                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
358                                 SPL spl;
359                                 spl.read (i->path(), _content_view);
360                                 _playlists.push_back (spl);
361                         }
362                 } catch (exception& e) {
363                         /* Never mind */
364                 }
365         }
366
367         sort (_playlists.begin(), _playlists.end(), SPLComparator());
368         BOOST_FOREACH (SPL i, _playlists) {
369                 add_playlist_to_list (i);
370         }
371
372         _selected_playlist = boost::none;
373 }
374
375 optional<dcp::EncryptedKDM>
376 SwaroopControls::get_kdm_from_url (shared_ptr<DCPContent> dcp)
377 {
378         ScopedTemporary temp;
379         string url = Config::instance()->kdm_server_url();
380         boost::algorithm::replace_all (url, "{CPL}", *dcp->cpl());
381         optional<dcp::EncryptedKDM> kdm;
382         if (dcp->cpl() && !get_from_url(url, false, false, temp)) {
383                 try {
384                         kdm = dcp::EncryptedKDM (dcp::file_to_string(temp.file()));
385                         if (kdm->cpl_id() != dcp->cpl()) {
386                                 kdm = boost::none;
387                         }
388                 } catch (std::exception& e) {
389                         /* Hey well */
390                 }
391         }
392         return kdm;
393 }
394
395 optional<dcp::EncryptedKDM>
396 SwaroopControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
397 {
398         using namespace boost::filesystem;
399         optional<path> kdm_dir = Config::instance()->player_kdm_directory();
400         if (!kdm_dir) {
401                 return optional<dcp::EncryptedKDM>();
402         }
403         for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
404                 try {
405                         if (file_size(i->path()) < MAX_KDM_SIZE) {
406                                 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
407                                 if (kdm.cpl_id() == dcp->cpl()) {
408                                         return kdm;
409                                 }
410                         }
411                 } catch (std::exception& e) {
412                         /* Hey well */
413                 }
414         }
415         return optional<dcp::EncryptedKDM>();
416 }
417
418 optional<EncryptedECinemaKDM>
419 SwaroopControls::get_kdm_from_directory (shared_ptr<FFmpegContent> ffmpeg)
420 {
421         using namespace boost::filesystem;
422         optional<path> kdm_dir = Config::instance()->player_kdm_directory();
423         if (!kdm_dir) {
424                 return optional<EncryptedECinemaKDM>();
425         }
426         for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
427                 try {
428                         if (file_size(i->path()) < MAX_KDM_SIZE) {
429                                 EncryptedECinemaKDM kdm (dcp::file_to_string(i->path()));
430                                 if (kdm.id() == ffmpeg->id().get_value_or("")) {
431                                         return kdm;
432                                 }
433                         }
434                 } catch (std::exception& e) {
435                         /* Hey well */
436                 }
437         }
438         return optional<EncryptedECinemaKDM>();
439 }
440 void
441 SwaroopControls::spl_selection_changed ()
442 {
443         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
444         if (selected == -1) {
445                 _current_spl_view->DeleteAllItems ();
446                 _selected_playlist = boost::none;
447                 return;
448         }
449
450         if (_playlists[selected].missing()) {
451                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
452                 deselect_playlist ();
453                 return;
454         }
455
456         if (_playlists[selected].get().empty()) {
457                 error_dialog (this, "This playlist is empty.");
458                 return;
459         }
460
461         if (!_playlists[selected].have_allowed_shows()) {
462                 error_dialog (this, "There are no more allowed shows of this playlist.");
463                 return;
464         }
465
466         select_playlist (selected, 0);
467 }
468
469 void
470 SwaroopControls::select_playlist (int selected, int position)
471 {
472         log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
473
474         wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
475
476         BOOST_FOREACH (SPLEntry const & i, _playlists[selected].get()) {
477                 dialog.Pulse ();
478                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
479                 if (dcp && dcp->needs_kdm()) {
480                         optional<dcp::EncryptedKDM> kdm;
481                         kdm = get_kdm_from_url (dcp);
482                         if (!kdm) {
483                                 kdm = get_kdm_from_directory (dcp);
484                         }
485                         if (kdm) {
486                                 try {
487                                         dcp->add_kdm (*kdm);
488                                         dcp->examine (_film, shared_ptr<Job>());
489                                 } catch (KDMError& e) {
490                                         error_dialog (this, "Could not load KDM.");
491                                 }
492                         }
493                         if (dcp->needs_kdm()) {
494                                 /* We didn't get a KDM for this */
495                                 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
496                                 deselect_playlist ();
497                                 return;
498                         }
499                 }
500                 shared_ptr<FFmpegContent> ffmpeg = dynamic_pointer_cast<FFmpegContent> (i.content);
501                 if (ffmpeg && ffmpeg->encrypted()) {
502                         optional<EncryptedECinemaKDM> kdm = get_kdm_from_directory (ffmpeg);
503                         if (kdm) {
504                                 try {
505                                         ffmpeg->add_kdm (*kdm);
506                                         ffmpeg->examine (_film, shared_ptr<Job>());
507                                 } catch (KDMError& e) {
508                                         error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
509                                         deselect_playlist ();
510                                         return;
511                                 }
512                         } else {
513                                 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
514                                 deselect_playlist ();
515                                 return;
516                         }
517                 }
518         }
519
520         _current_spl_view->DeleteAllItems ();
521
522         int N = 0;
523         BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
524                 wxListItem it;
525                 it.SetId (N);
526                 it.SetColumn (0);
527                 it.SetText (std_to_wx(i.name));
528                 _current_spl_view->InsertItem (it);
529                 ++N;
530         }
531
532         _selected_playlist = selected;
533         _selected_playlist_position = position;
534         dialog.Pulse ();
535         reset_film ();
536         dialog.Pulse ();
537         update_current_content ();
538 }
539
540 void
541 SwaroopControls::reset_film ()
542 {
543         DCPOMATIC_ASSERT (_selected_playlist);
544         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
545         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
546         ResetFilm (film);
547 }
548
549 void
550 SwaroopControls::config_changed (int property)
551 {
552         Controls::config_changed (property);
553
554         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
555                 _content_view->update ();
556         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
557                 update_playlist_directory ();
558         }
559 }
560
561 void
562 SwaroopControls::set_film (shared_ptr<Film> film)
563 {
564         Controls::set_film (film);
565         setup_sensitivity ();
566 }
567
568 void
569 SwaroopControls::update_current_content ()
570 {
571         DCPOMATIC_ASSERT (_selected_playlist);
572
573         wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
574
575         SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
576         _current_disable_timeline = e.disable_timeline;
577         _current_disable_next = !e.skippable;
578
579         setup_sensitivity ();
580         dialog.Pulse ();
581         reset_film ();
582 }
583
584 /** One piece of content in our SPL has finished playing */
585 void
586 SwaroopControls::viewer_finished ()
587 {
588         if (!_selected_playlist) {
589                 return;
590         }
591
592         bool const stop = _playlists[*_selected_playlist].get()[_selected_playlist_position].stop_after_play;
593
594         _selected_playlist_position++;
595         if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
596                 /* Next piece of content on the SPL */
597                 update_current_content ();
598                 if (!stop) {
599                         _viewer->start ();
600                 }
601         } else {
602                 /* Finished the whole SPL */
603                 _selected_playlist_position = 0;
604                 _viewer->set_background_image (true);
605                 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
606                 decrement_allowed_shows ();
607                 _play_button->Enable (true);
608                 _pause_button->Enable (false);
609         }
610 }