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