2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "swaroop_controls.h"
22 #include "film_viewer.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 "lib/ffmpeg_content.h"
33 #include "lib/compose.hpp"
34 #include <dcp/raw_convert.h>
35 #include <dcp/exceptions.h>
36 #include <wx/listctrl.h>
37 #include <wx/progdlg.h>
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using boost::optional;
46 using namespace dcpomatic;
48 SwaroopControls::SwaroopControls (wxWindow* parent, shared_ptr<FilmViewer> viewer)
49 : Controls (parent, viewer, false)
50 , _play_button (new Button(this, _("Play")))
51 , _pause_button (new Button(this, _("Pause")))
52 , _stop_button (new Button(this, _("Stop")))
53 , _next_button (new Button(this, "Next"))
54 , _previous_button (new Button(this, "Previous"))
55 , _current_disable_timeline (false)
56 , _current_disable_next (false)
58 _button_sizer->Add (_previous_button, 0, wxEXPAND);
59 _button_sizer->Add (_play_button, 0, wxEXPAND);
60 _button_sizer->Add (_pause_button, 0, wxEXPAND);
61 _button_sizer->Add (_stop_button, 0, wxEXPAND);
62 _button_sizer->Add (_next_button, 0, wxEXPAND);
64 _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
65 _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
67 wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
68 wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
70 wxFont subheading_font (*wxNORMAL_FONT);
71 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
73 wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
75 wxStaticText* m = new StaticText (this, "Playlists");
76 m->SetFont (subheading_font);
77 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
79 _refresh_spl_view = new Button (this, "Refresh");
80 spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
82 left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
83 left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
85 _content_view = new ContentView (this);
87 wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
89 wxStaticText* m = new StaticText (this, "Content");
90 m->SetFont (subheading_font);
91 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
93 _refresh_content_view = new Button (this, "Refresh");
94 content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
96 left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
97 left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
99 _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
100 _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
101 _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
102 e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
103 e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
105 _v_sizer->Add (e_sizer, 1, wxEXPAND);
107 _log = new wxTextCtrl (this, wxID_ANY, wxT(""), wxDefaultPosition, wxSize(-1, 200), wxTE_READONLY | wxTE_MULTILINE);
108 _v_sizer->Add (_log, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
110 _play_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::play_clicked, this));
111 _pause_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::pause_clicked, this));
112 _stop_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::stop_clicked, this));
113 _next_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::next_clicked, this));
114 _previous_button->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::previous_clicked, this));
115 _spl_view->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
116 _spl_view->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&SwaroopControls::spl_selection_changed, this));
117 _viewer->Finished.connect (boost::bind(&SwaroopControls::viewer_finished, this));
118 _viewer->PositionChanged.connect (boost::bind(&SwaroopControls::viewer_position_changed, this));
119 _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&SwaroopControls::update_playlist_directory, this));
120 _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
122 _content_view->update ();
123 update_playlist_directory ();
125 _viewer->set_background_image (true);
129 SwaroopControls::check_restart ()
131 cout << "check_restart called\n";
132 FILE* f = fopen_boost (Config::path("position"), "r");
134 cout << "could not open position file (" << errno << ")\n";
141 fscanf (f, "%63s %d %ld", id, &index, &time);
143 cout << "Looking for playlist " << id << " to restart.\n";
145 for (size_t i = 0; i < _playlists.size(); ++i) {
146 if (_playlists[i].id() == id) {
147 cout << "Found playlist " << id << "\n";
148 select_playlist (i, index);
149 update_current_content ();
150 _viewer->seek (DCPTime(time), false);
159 SwaroopControls::viewer_position_changed ()
161 if (!_selected_playlist || !_viewer->playing() || _viewer->position().get() % DCPTime::HZ) {
165 FILE* f = fopen_boost (Config::path("position"), "w");
167 string const p = _playlists[*_selected_playlist].id()
168 + " " + dcp::raw_convert<string>(_selected_playlist_position)
169 + " " + dcp::raw_convert<string>(_viewer->position().get());
171 checked_fwrite (p.c_str(), p.length(), f, Config::path("position"));
172 #ifdef DCPOMATIC_LINUX
181 SwaroopControls::started ()
183 Controls::started ();
184 _play_button->Enable (false);
185 _pause_button->Enable (true);
186 _viewer->set_background_image (false);
190 SwaroopControls::stopped ()
192 Controls::stopped ();
193 _play_button->Enable (true);
194 _pause_button->Enable (false);
198 SwaroopControls::decrement_allowed_shows ()
200 if (_selected_playlist) {
201 SPL& spl = _playlists[*_selected_playlist];
202 spl.decrement_allowed_shows();
204 spl.write (*spl.path());
210 SwaroopControls::play_clicked ()
216 SwaroopControls::setup_sensitivity ()
218 Controls::setup_sensitivity ();
219 bool const active_job = _active_job && *_active_job != "examine_content";
220 bool const c = _film && !_film->content().empty() && !active_job;
221 _play_button->Enable (c && !_viewer->playing());
222 _pause_button->Enable (_viewer->playing());
223 _slider->Enable (!_current_disable_timeline);
224 _spl_view->Enable (!_viewer->playing());
225 _next_button->Enable (!_current_disable_next && can_do_next());
226 _previous_button->Enable (can_do_previous());
230 SwaroopControls::pause_clicked ()
236 SwaroopControls::stop_clicked ()
239 _viewer->seek (DCPTime(), true);
240 if (_selected_playlist) {
241 _selected_playlist_position = 0;
242 update_current_content ();
244 _viewer->set_background_image (true);
245 decrement_allowed_shows ();
249 SwaroopControls::can_do_previous ()
251 return _selected_playlist && (_selected_playlist_position - 1) >= 0;
255 SwaroopControls::previous_clicked ()
257 if (!can_do_previous ()) {
261 _selected_playlist_position--;
262 update_current_content ();
266 SwaroopControls::can_do_next ()
268 return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
272 SwaroopControls::next_clicked ()
274 if (!can_do_next ()) {
278 _selected_playlist_position++;
279 update_current_content ();
283 SwaroopControls::log (wxString s)
286 gettimeofday (&time, 0);
288 time_t const sec = time.tv_sec;
289 struct tm* t = localtime (&sec);
290 strftime (buffer, 64, "%c", t);
291 wxString ts = std_to_wx(string(buffer)) + N_(": ");
292 _log->SetValue(_log->GetValue() + ts + s + "\n");
294 optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
299 FILE* f = fopen_boost (*log, "a");
300 fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
305 SwaroopControls::add_playlist_to_list (SPL spl)
307 int const N = _spl_view->GetItemCount();
312 string t = spl.name();
314 t += " (content missing)";
316 it.SetText (std_to_wx(t));
317 _spl_view->InsertItem (it);
322 bool operator() (SPL const & a, SPL const & b) {
323 return a.name() < b.name();
328 SwaroopControls::update_playlist_directory ()
330 using namespace boost::filesystem;
332 _spl_view->DeleteAllItems ();
333 optional<path> dir = Config::instance()->player_playlist_directory();
340 for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
342 if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
344 spl.read (i->path(), _content_view);
345 _playlists.push_back (spl);
347 } catch (exception& e) {
352 sort (_playlists.begin(), _playlists.end(), SPLComparator());
353 BOOST_FOREACH (SPL i, _playlists) {
354 add_playlist_to_list (i);
357 _selected_playlist = boost::none;
360 optional<dcp::EncryptedKDM>
361 SwaroopControls::get_kdm_from_url (shared_ptr<DCPContent> dcp)
363 ScopedTemporary temp;
364 string url = Config::instance()->kdm_server_url();
365 boost::algorithm::replace_all (url, "{CPL}", *dcp->cpl());
366 optional<dcp::EncryptedKDM> kdm;
367 if (dcp->cpl() && !get_from_url(url, false, false, temp)) {
369 kdm = dcp::EncryptedKDM (dcp::file_to_string(temp.file()));
370 if (kdm->cpl_id() != dcp->cpl()) {
373 } catch (std::exception& e) {
380 optional<dcp::EncryptedKDM>
381 SwaroopControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
383 using namespace boost::filesystem;
384 optional<path> kdm_dir = Config::instance()->player_kdm_directory();
386 return optional<dcp::EncryptedKDM>();
388 for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
390 if (file_size(i->path()) < MAX_KDM_SIZE) {
391 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
392 if (kdm.cpl_id() == dcp->cpl()) {
396 } catch (std::exception& e) {
400 return optional<dcp::EncryptedKDM>();
403 optional<EncryptedECinemaKDM>
404 SwaroopControls::get_kdm_from_directory (shared_ptr<FFmpegContent> ffmpeg)
406 using namespace boost::filesystem;
407 optional<path> kdm_dir = Config::instance()->player_kdm_directory();
409 return optional<EncryptedECinemaKDM>();
411 for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
413 if (file_size(i->path()) < MAX_KDM_SIZE) {
414 EncryptedECinemaKDM kdm (dcp::file_to_string(i->path()));
415 if (kdm.id() == ffmpeg->id().get_value_or("")) {
419 } catch (std::exception& e) {
423 return optional<EncryptedECinemaKDM>();
426 SwaroopControls::spl_selection_changed ()
428 long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
429 if (selected == -1) {
430 _current_spl_view->DeleteAllItems ();
431 _selected_playlist = boost::none;
435 if (_playlists[selected].missing()) {
436 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
437 _selected_playlist = boost::none;
438 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
442 if (_playlists[selected].get().empty()) {
443 error_dialog (this, "This playlist is empty.");
447 if (!_playlists[selected].have_allowed_shows()) {
448 error_dialog (this, "There are no more allowed shows of this playlist.");
452 select_playlist (selected, 0);
456 SwaroopControls::select_playlist (int selected, int position)
458 log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
460 wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
462 BOOST_FOREACH (SPLEntry const & i, _playlists[selected].get()) {
464 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
465 if (dcp && dcp->needs_kdm()) {
466 optional<dcp::EncryptedKDM> kdm;
467 kdm = get_kdm_from_url (dcp);
469 kdm = get_kdm_from_directory (dcp);
474 dcp->examine (_film, shared_ptr<Job>());
475 } catch (KDMError& e) {
476 error_dialog (this, "Could not load KDM.");
479 if (dcp->needs_kdm()) {
480 /* We didn't get a KDM for this */
481 error_dialog (this, "This playlist cannot be loaded as a KDM is missing.");
482 _selected_playlist = boost::none;
483 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
487 shared_ptr<FFmpegContent> ffmpeg = dynamic_pointer_cast<FFmpegContent> (i.content);
488 if (ffmpeg && ffmpeg->encrypted()) {
489 optional<EncryptedECinemaKDM> kdm = get_kdm_from_directory (ffmpeg);
492 ffmpeg->add_kdm (*kdm);
493 ffmpeg->examine (_film, shared_ptr<Job>());
494 } catch (KDMError& e) {
495 error_dialog (this, "Could not load KDM.");
498 error_dialog (this, "This playlist cannot be loaded as a KDM is missing.");
499 _selected_playlist = boost::none;
500 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
506 _current_spl_view->DeleteAllItems ();
509 BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
513 it.SetText (std_to_wx(i.name));
514 _current_spl_view->InsertItem (it);
518 _selected_playlist = selected;
519 _selected_playlist_position = position;
523 update_current_content ();
527 SwaroopControls::reset_film ()
529 DCPOMATIC_ASSERT (_selected_playlist);
530 shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
531 film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
536 SwaroopControls::config_changed (int property)
538 Controls::config_changed (property);
540 if (property == Config::PLAYER_CONTENT_DIRECTORY) {
541 _content_view->update ();
542 } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
543 update_playlist_directory ();
548 SwaroopControls::set_film (shared_ptr<Film> film)
550 Controls::set_film (film);
551 setup_sensitivity ();
555 SwaroopControls::update_current_content ()
557 DCPOMATIC_ASSERT (_selected_playlist);
559 wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
561 SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
562 _current_disable_timeline = e.disable_timeline;
563 _current_disable_next = !e.skippable;
565 setup_sensitivity ();
570 /** One piece of content in our SPL has finished playing */
572 SwaroopControls::viewer_finished ()
574 if (!_selected_playlist) {
578 bool const stop = _playlists[*_selected_playlist].get()[_selected_playlist_position].stop_after_play;
580 _selected_playlist_position++;
581 if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
582 /* Next piece of content on the SPL */
583 update_current_content ();
588 /* Finished the whole SPL */
589 _selected_playlist_position = 0;
590 _viewer->set_background_image (true);
591 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
593 decrement_allowed_shows ();