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