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