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