129e0ceca407eee9990cac7a69e092552c8bb90e
[dcpomatic.git] / src / wx / playlist_controls.cc
1 /*
2     Copyright (C) 2018-2020 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 "playlist_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 std::shared_ptr;
44 using std::dynamic_pointer_cast;
45 using boost::optional;
46 using namespace dcpomatic;
47
48 PlaylistControls::PlaylistControls (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 {
56         _button_sizer->Add (_previous_button, 0, wxEXPAND);
57         _button_sizer->Add (_play_button, 0, wxEXPAND);
58         _button_sizer->Add (_pause_button, 0, wxEXPAND);
59         _button_sizer->Add (_stop_button, 0, wxEXPAND);
60         _button_sizer->Add (_next_button, 0, wxEXPAND);
61
62         _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
63         _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
64
65         wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
66         wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
67
68         wxFont subheading_font (*wxNORMAL_FONT);
69         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
70
71         wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
72         {
73                 wxStaticText* m = new StaticText (this, "Playlists");
74                 m->SetFont (subheading_font);
75                 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
76         }
77         _refresh_spl_view = new Button (this, "Refresh");
78         spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
79
80         left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
81         left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
82
83         _content_view = new ContentView (this);
84
85         wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
86         {
87                 wxStaticText* m = new StaticText (this, "Content");
88                 m->SetFont (subheading_font);
89                 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
90         }
91         _refresh_content_view = new Button (this, "Refresh");
92         content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
93
94         left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
95         left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
96
97         _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
98         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
99         _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
100         e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
101         e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
102
103         _v_sizer->Add (e_sizer, 1, wxEXPAND);
104
105         _play_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::play_clicked,  this));
106         _pause_button->Bind    (wxEVT_BUTTON, boost::bind(&PlaylistControls::pause_clicked, this));
107         _stop_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::stop_clicked,  this));
108         _next_button->Bind     (wxEVT_BUTTON, boost::bind(&PlaylistControls::next_clicked,  this));
109         _previous_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::previous_clicked,  this));
110         _spl_view->Bind        (wxEVT_LIST_ITEM_SELECTED,   boost::bind(&PlaylistControls::spl_selection_changed, this));
111         _spl_view->Bind        (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
112         viewer->Finished.connect (boost::bind(&PlaylistControls::viewer_finished, this));
113         _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::update_playlist_directory, this));
114         _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
115
116         _content_view->update ();
117         update_playlist_directory ();
118 }
119
120 void
121 PlaylistControls::started ()
122 {
123         Controls::started ();
124         _play_button->Enable (false);
125         _pause_button->Enable (true);
126 }
127
128 /** Called when the viewer finishes a single piece of content, or it is explicitly stopped */
129 void
130 PlaylistControls::stopped ()
131 {
132         Controls::stopped ();
133         _play_button->Enable (true);
134         _pause_button->Enable (false);
135 }
136
137 void
138 PlaylistControls::deselect_playlist ()
139 {
140         long int const selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
141         if (selected != -1) {
142                 _selected_playlist = boost::none;
143                 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
144         }
145         ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
146 }
147
148 void
149 PlaylistControls::play_clicked ()
150 {
151         auto viewer = _viewer.lock ();
152         if (viewer) {
153                 viewer->start ();
154         }
155 }
156
157 void
158 PlaylistControls::setup_sensitivity ()
159 {
160         auto viewer = _viewer.lock ();
161         if (!viewer) {
162                 return;
163         }
164
165         Controls::setup_sensitivity ();
166         bool const active_job = _active_job && *_active_job != "examine_content";
167         bool const c = _film && !_film->content().empty() && !active_job;
168         _play_button->Enable (c && !viewer->playing());
169         _pause_button->Enable (viewer->playing());
170         _spl_view->Enable (!viewer->playing());
171         _next_button->Enable (can_do_next());
172         _previous_button->Enable (can_do_previous());
173 }
174
175 void
176 PlaylistControls::pause_clicked ()
177 {
178         auto viewer = _viewer.lock ();
179         if (viewer) {
180                 viewer->stop ();
181         }
182 }
183
184 void
185 PlaylistControls::stop_clicked ()
186 {
187         auto viewer = _viewer.lock ();
188         if (!viewer) {
189                 return;
190         }
191
192         viewer->stop ();
193         viewer->seek (DCPTime(), true);
194         if (_selected_playlist) {
195                 _selected_playlist_position = 0;
196                 update_current_content ();
197         }
198         deselect_playlist ();
199 }
200
201 bool
202 PlaylistControls::can_do_previous ()
203 {
204         return _selected_playlist && (_selected_playlist_position - 1) >= 0;
205 }
206
207 void
208 PlaylistControls::previous_clicked ()
209 {
210         if (!can_do_previous ()) {
211                 return;
212         }
213
214         _selected_playlist_position--;
215         update_current_content ();
216 }
217
218 bool
219 PlaylistControls::can_do_next ()
220 {
221         return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
222 }
223
224 void
225 PlaylistControls::next_clicked ()
226 {
227         if (!can_do_next ()) {
228                 return;
229         }
230
231         _selected_playlist_position++;
232         update_current_content ();
233 }
234
235 void
236 PlaylistControls::log (wxString s)
237 {
238         optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
239         if (!log) {
240                 return;
241         }
242
243         struct timeval time;
244         gettimeofday (&time, 0);
245         char buffer[64];
246         time_t const sec = time.tv_sec;
247         struct tm* t = localtime (&sec);
248         strftime (buffer, 64, "%c", t);
249         wxString ts = std_to_wx(string(buffer)) + N_(": ");
250         FILE* f = fopen_boost (*log, "a");
251         if (!f) {
252                 return;
253         }
254         fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
255         fclose (f);
256 }
257
258 void
259 PlaylistControls::add_playlist_to_list (SPL spl)
260 {
261         int const N = _spl_view->GetItemCount();
262
263         wxListItem it;
264         it.SetId(N);
265         it.SetColumn(0);
266         string t = spl.name();
267         if (spl.missing()) {
268                 t += " (content missing)";
269         }
270         it.SetText (std_to_wx(t));
271         _spl_view->InsertItem (it);
272 }
273
274 struct SPLComparator
275 {
276         bool operator() (SPL const & a, SPL const & b) {
277                 return a.name() < b.name();
278         }
279 };
280
281 void
282 PlaylistControls::update_playlist_directory ()
283 {
284         using namespace boost::filesystem;
285
286         _spl_view->DeleteAllItems ();
287         optional<path> dir = Config::instance()->player_playlist_directory();
288         if (!dir) {
289                 return;
290         }
291
292         _playlists.clear ();
293
294         for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
295                 try {
296                         if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
297                                 SPL spl;
298                                 spl.read (i->path(), _content_view);
299                                 _playlists.push_back (spl);
300                         }
301                 } catch (exception& e) {
302                         /* Never mind */
303                 }
304         }
305
306         sort (_playlists.begin(), _playlists.end(), SPLComparator());
307         for (auto i: _playlists) {
308                 add_playlist_to_list (i);
309         }
310
311         _selected_playlist = boost::none;
312 }
313
314 optional<dcp::EncryptedKDM>
315 PlaylistControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
316 {
317         using namespace boost::filesystem;
318         optional<path> kdm_dir = Config::instance()->player_kdm_directory();
319         if (!kdm_dir) {
320                 return optional<dcp::EncryptedKDM>();
321         }
322         for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
323                 try {
324                         if (file_size(i->path()) < MAX_KDM_SIZE) {
325                                 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
326                                 if (kdm.cpl_id() == dcp->cpl()) {
327                                         return kdm;
328                                 }
329                         }
330                 } catch (std::exception& e) {
331                         /* Hey well */
332                 }
333         }
334         return optional<dcp::EncryptedKDM>();
335 }
336
337 void
338 PlaylistControls::spl_selection_changed ()
339 {
340         long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
341         if (selected == -1) {
342                 _current_spl_view->DeleteAllItems ();
343                 _selected_playlist = boost::none;
344                 return;
345         }
346
347         if (_playlists[selected].missing()) {
348                 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
349                 deselect_playlist ();
350                 return;
351         }
352
353         if (_playlists[selected].get().empty()) {
354                 error_dialog (this, "This playlist is empty.");
355                 return;
356         }
357
358         select_playlist (selected, 0);
359 }
360
361 void
362 PlaylistControls::select_playlist (int selected, int position)
363 {
364         log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
365
366         wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
367
368         for (auto const& i: _playlists[selected].get()) {
369                 dialog.Pulse ();
370                 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
371                 if (dcp && dcp->needs_kdm()) {
372                         optional<dcp::EncryptedKDM> kdm;
373                         kdm = get_kdm_from_directory (dcp);
374                         if (kdm) {
375                                 try {
376                                         dcp->add_kdm (*kdm);
377                                         dcp->examine (_film, shared_ptr<Job>());
378                                 } catch (KDMError& e) {
379                                         error_dialog (this, "Could not load KDM.");
380                                 }
381                         }
382                         if (dcp->needs_kdm()) {
383                                 /* We didn't get a KDM for this */
384                                 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
385                                 deselect_playlist ();
386                                 return;
387                         }
388                 }
389         }
390
391         _current_spl_view->DeleteAllItems ();
392
393         int N = 0;
394         for (auto i: _playlists[selected].get()) {
395                 wxListItem it;
396                 it.SetId (N);
397                 it.SetColumn (0);
398                 it.SetText (std_to_wx(i.name));
399                 _current_spl_view->InsertItem (it);
400                 ++N;
401         }
402
403         _selected_playlist = selected;
404         _selected_playlist_position = position;
405         dialog.Pulse ();
406         reset_film ();
407         dialog.Pulse ();
408         update_current_content ();
409 }
410
411 void
412 PlaylistControls::reset_film ()
413 {
414         DCPOMATIC_ASSERT (_selected_playlist);
415         shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
416         film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
417         ResetFilm (film);
418 }
419
420 void
421 PlaylistControls::config_changed (int property)
422 {
423         Controls::config_changed (property);
424
425         if (property == Config::PLAYER_CONTENT_DIRECTORY) {
426                 _content_view->update ();
427         } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
428                 update_playlist_directory ();
429         }
430 }
431
432 void
433 PlaylistControls::set_film (shared_ptr<Film> film)
434 {
435         Controls::set_film (film);
436         setup_sensitivity ();
437 }
438
439 void
440 PlaylistControls::update_current_content ()
441 {
442         DCPOMATIC_ASSERT (_selected_playlist);
443
444         wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
445
446         setup_sensitivity ();
447         dialog.Pulse ();
448         reset_film ();
449 }
450
451 /** One piece of content in our SPL has finished playing */
452 void
453 PlaylistControls::viewer_finished ()
454 {
455         auto viewer = _viewer.lock ();
456         if (!_selected_playlist || !viewer) {
457                 return;
458         }
459
460         _selected_playlist_position++;
461         if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
462                 /* Next piece of content on the SPL */
463                 update_current_content ();
464                 viewer->start ();
465         } else {
466                 /* Finished the whole SPL */
467                 _selected_playlist_position = 0;
468                 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
469                 _play_button->Enable (true);
470                 _pause_button->Enable (false);
471         }
472 }
473
474 void
475 PlaylistControls::play ()
476 {
477         play_clicked ();
478 }
479
480 void
481 PlaylistControls::stop ()
482 {
483         stop_clicked ();
484 }