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