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