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