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