2 Copyright (C) 2018 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 <dcp/raw_convert.h>
33 #include <dcp/exceptions.h>
34 #include <wx/listctrl.h>
35 #include <wx/progdlg.h>
41 using boost::shared_ptr;
42 using boost::dynamic_pointer_cast;
43 using boost::optional;
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)
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);
61 _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
62 _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
64 wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
65 wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
67 wxFont subheading_font (*wxNORMAL_FONT);
68 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
70 wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
72 wxStaticText* m = new StaticText (this, "Playlists");
73 m->SetFont (subheading_font);
74 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
76 _refresh_spl_view = new Button (this, "Refresh");
77 spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
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);
82 _content_view = new ContentView (this);
84 wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
86 wxStaticText* m = new StaticText (this, "Content");
87 m->SetFont (subheading_font);
88 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
90 _refresh_content_view = new Button (this, "Refresh");
91 content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
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);
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);
102 _v_sizer->Add (e_sizer, 1, wxEXPAND);
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);
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));
119 _content_view->update ();
120 update_playlist_directory ();
124 SwaroopControls::check_restart ()
126 FILE* f = fopen_boost (Config::path("position"), "r");
134 fscanf (f, "%63s %d %ld", id, &index, &time);
136 for (size_t i = 0; i < _playlists.size(); ++i) {
137 if (_playlists[i].id() == id) {
138 select_playlist (i, index);
139 update_current_content ();
140 _viewer->seek (DCPTime(time), false);
149 SwaroopControls::viewer_position_changed ()
151 if (!_selected_playlist || !_viewer->playing() || _viewer->position().get() % DCPTime::HZ) {
155 FILE* f = fopen_boost (Config::path("position"), "w");
157 string const p = _playlists[*_selected_playlist].id()
158 + " " + dcp::raw_convert<string>(_selected_playlist_position)
159 + " " + dcp::raw_convert<string>(_viewer->position().get());
161 checked_fwrite (p.c_str(), p.length(), f, Config::path("position"));
167 SwaroopControls::started ()
169 Controls::started ();
170 _play_button->Enable (false);
171 _pause_button->Enable (true);
175 SwaroopControls::stopped ()
177 Controls::stopped ();
178 _play_button->Enable (true);
179 _pause_button->Enable (false);
183 SwaroopControls::play_clicked ()
189 SwaroopControls::setup_sensitivity ()
191 Controls::setup_sensitivity ();
192 bool const active_job = _active_job && *_active_job != "examine_content";
193 bool const c = _film && !_film->content().empty() && !active_job;
194 _play_button->Enable (c && !_viewer->playing());
195 _pause_button->Enable (_viewer->playing());
196 _slider->Enable (!_current_disable_timeline);
197 _spl_view->Enable (!_viewer->playing());
198 _next_button->Enable (!_current_disable_next && can_do_next());
199 _previous_button->Enable (can_do_previous());
203 SwaroopControls::pause_clicked ()
209 SwaroopControls::stop_clicked ()
212 _viewer->seek (DCPTime(), true);
213 if (_selected_playlist) {
214 _selected_playlist_position = 0;
215 update_current_content ();
220 SwaroopControls::can_do_previous ()
222 return _selected_playlist && (_selected_playlist_position - 1) >= 0;
226 SwaroopControls::previous_clicked ()
228 if (!can_do_previous ()) {
232 _selected_playlist_position--;
233 update_current_content ();
237 SwaroopControls::can_do_next ()
239 return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
243 SwaroopControls::next_clicked ()
245 if (!can_do_next ()) {
249 _selected_playlist_position++;
250 update_current_content ();
254 SwaroopControls::log (wxString s)
257 gettimeofday (&time, 0);
259 time_t const sec = time.tv_sec;
260 struct tm* t = localtime (&sec);
261 strftime (buffer, 64, "%c", t);
262 wxString ts = std_to_wx(string(buffer)) + N_(": ");
263 _log->SetValue(_log->GetValue() + ts + s + "\n");
265 optional<boost::filesystem::path> log = Config::instance()->player_log_file();
270 FILE* f = fopen_boost (*log, "a");
271 fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
276 SwaroopControls::add_playlist_to_list (SPL spl)
278 int const N = _spl_view->GetItemCount();
283 string t = spl.name();
285 t += " (content missing)";
287 it.SetText (std_to_wx(t));
288 _spl_view->InsertItem (it);
293 bool operator() (SPL const & a, SPL const & b) {
294 return a.name() < b.name();
299 SwaroopControls::update_playlist_directory ()
301 using namespace boost::filesystem;
303 _spl_view->DeleteAllItems ();
304 optional<path> dir = Config::instance()->player_playlist_directory();
311 for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
313 if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
315 spl.read (i->path(), _content_view);
316 _playlists.push_back (spl);
318 } catch (exception& e) {
323 sort (_playlists.begin(), _playlists.end(), SPLComparator());
324 BOOST_FOREACH (SPL i, _playlists) {
325 add_playlist_to_list (i);
328 _selected_playlist = boost::none;
331 optional<dcp::EncryptedKDM>
332 SwaroopControls::get_kdm_from_url (shared_ptr<DCPContent> dcp)
334 ScopedTemporary temp;
335 string url = Config::instance()->kdm_server_url();
336 boost::algorithm::replace_all (url, "{CPL}", *dcp->cpl());
337 optional<dcp::EncryptedKDM> kdm;
338 if (dcp->cpl() && !get_from_url(url, false, temp)) {
340 kdm = dcp::EncryptedKDM (dcp::file_to_string(temp.file()));
341 if (kdm->cpl_id() != dcp->cpl()) {
344 } catch (std::exception& e) {
351 optional<dcp::EncryptedKDM>
352 SwaroopControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
354 using namespace boost::filesystem;
355 optional<path> kdm_dir = Config::instance()->player_kdm_directory();
357 return optional<dcp::EncryptedKDM>();
359 for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
361 if (file_size(i->path()) < MAX_KDM_SIZE) {
362 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
363 if (kdm.cpl_id() == dcp->cpl()) {
367 } catch (std::exception& e) {
371 return optional<dcp::EncryptedKDM>();
375 SwaroopControls::spl_selection_changed ()
377 long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
378 if (selected == -1) {
379 _current_spl_view->DeleteAllItems ();
380 _selected_playlist = boost::none;
384 if (_playlists[selected].missing()) {
385 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
386 _selected_playlist = boost::none;
387 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
391 if (_playlists[selected].get().empty()) {
392 error_dialog (this, "This playlist is empty.");
396 select_playlist (selected, 0);
400 SwaroopControls::select_playlist (int selected, int position)
402 log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
404 wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
406 BOOST_FOREACH (SPLEntry const & i, _playlists[selected].get()) {
408 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
409 if (dcp && dcp->needs_kdm()) {
410 optional<dcp::EncryptedKDM> kdm;
411 kdm = get_kdm_from_url (dcp);
413 kdm = get_kdm_from_directory (dcp);
418 dcp->examine (_film, shared_ptr<Job>());
419 } catch (KDMError& e) {
420 error_dialog (this, "Could not load KDM.");
423 if (dcp->needs_kdm()) {
424 /* We didn't get a KDM for this */
425 error_dialog (this, "This playlist cannot be loaded as a KDM is missing.");
426 _selected_playlist = boost::none;
427 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
433 _current_spl_view->DeleteAllItems ();
436 BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
440 it.SetText (std_to_wx(i.name));
441 _current_spl_view->InsertItem (it);
445 _selected_playlist = selected;
446 _selected_playlist_position = position;
450 update_current_content ();
454 SwaroopControls::reset_film ()
456 DCPOMATIC_ASSERT (_selected_playlist);
457 shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
458 film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
463 SwaroopControls::config_changed (int property)
465 Controls::config_changed (property);
467 if (property == Config::PLAYER_CONTENT_DIRECTORY) {
468 _content_view->update ();
469 } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
470 update_playlist_directory ();
475 SwaroopControls::set_film (shared_ptr<Film> film)
477 Controls::set_film (film);
478 setup_sensitivity ();
482 SwaroopControls::update_current_content ()
484 DCPOMATIC_ASSERT (_selected_playlist);
486 wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
488 SPLEntry const & e = _playlists[*_selected_playlist].get()[_selected_playlist_position];
489 _current_disable_timeline = e.disable_timeline;
490 _current_disable_next = !e.skippable;
492 setup_sensitivity ();
498 SwaroopControls::viewer_finished ()
500 if (!_selected_playlist) {
504 bool const stop = _playlists[*_selected_playlist].get()[_selected_playlist_position].stop_after_play;
506 _selected_playlist_position++;
507 if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
508 update_current_content ();
513 _selected_playlist_position = 0;
514 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));