2 Copyright (C) 2018-2020 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 "playlist_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 PlaylistControls::PlaylistControls (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"))
57 _button_sizer->Add (_previous_button, 0, wxEXPAND);
58 _button_sizer->Add (_play_button, 0, wxEXPAND);
59 _button_sizer->Add (_pause_button, 0, wxEXPAND);
60 _button_sizer->Add (_stop_button, 0, wxEXPAND);
61 _button_sizer->Add (_next_button, 0, wxEXPAND);
63 _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
64 _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
66 wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
67 wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
69 wxFont subheading_font (*wxNORMAL_FONT);
70 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
72 wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
74 wxStaticText* m = new StaticText (this, "Playlists");
75 m->SetFont (subheading_font);
76 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
78 _refresh_spl_view = new Button (this, "Refresh");
79 spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
81 left_sizer->Add (spl_header, 0, wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
82 left_sizer->Add (_spl_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
84 _content_view = new ContentView (this);
86 wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
88 wxStaticText* m = new StaticText (this, "Content");
89 m->SetFont (subheading_font);
90 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
92 _refresh_content_view = new Button (this, "Refresh");
93 content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
95 left_sizer->Add (content_header, 0, wxTOP | wxLEFT | wxRIGHT | wxEXPAND, DCPOMATIC_SIZER_GAP);
96 left_sizer->Add (_content_view, 1, wxLEFT | wxRIGHT | wxBOTTOM | wxEXPAND, DCPOMATIC_SIZER_GAP);
98 _current_spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
99 _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 500);
100 _current_spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 80);
101 e_sizer->Add (left_sizer, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
102 e_sizer->Add (_current_spl_view, 1, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
104 _v_sizer->Add (e_sizer, 1, wxEXPAND);
106 _play_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::play_clicked, this));
107 _pause_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::pause_clicked, this));
108 _stop_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::stop_clicked, this));
109 _next_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::next_clicked, this));
110 _previous_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::previous_clicked, this));
111 _spl_view->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
112 _spl_view->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
113 _viewer->Finished.connect (boost::bind(&PlaylistControls::viewer_finished, this));
114 _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::update_playlist_directory, this));
115 _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
117 _content_view->update ();
118 update_playlist_directory ();
122 PlaylistControls::started ()
124 Controls::started ();
125 _play_button->Enable (false);
126 _pause_button->Enable (true);
129 /** Called when the viewer finishes a single piece of content, or it is explicitly stopped */
131 PlaylistControls::stopped ()
133 Controls::stopped ();
134 _play_button->Enable (true);
135 _pause_button->Enable (false);
139 PlaylistControls::deselect_playlist ()
141 long int const selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142 if (selected != -1) {
143 _selected_playlist = boost::none;
144 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
146 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
150 PlaylistControls::play_clicked ()
156 PlaylistControls::setup_sensitivity ()
158 Controls::setup_sensitivity ();
159 bool const active_job = _active_job && *_active_job != "examine_content";
160 bool const c = _film && !_film->content().empty() && !active_job;
161 _play_button->Enable (c && !_viewer->playing());
162 _pause_button->Enable (_viewer->playing());
163 _spl_view->Enable (!_viewer->playing());
164 _next_button->Enable (can_do_next());
165 _previous_button->Enable (can_do_previous());
169 PlaylistControls::pause_clicked ()
175 PlaylistControls::stop_clicked ()
178 _viewer->seek (DCPTime(), true);
179 if (_selected_playlist) {
180 _selected_playlist_position = 0;
181 update_current_content ();
183 deselect_playlist ();
187 PlaylistControls::can_do_previous ()
189 return _selected_playlist && (_selected_playlist_position - 1) >= 0;
193 PlaylistControls::previous_clicked ()
195 if (!can_do_previous ()) {
199 _selected_playlist_position--;
200 update_current_content ();
204 PlaylistControls::can_do_next ()
206 return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
210 PlaylistControls::next_clicked ()
212 if (!can_do_next ()) {
216 _selected_playlist_position++;
217 update_current_content ();
221 PlaylistControls::log (wxString s)
223 optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
229 gettimeofday (&time, 0);
231 time_t const sec = time.tv_sec;
232 struct tm* t = localtime (&sec);
233 strftime (buffer, 64, "%c", t);
234 wxString ts = std_to_wx(string(buffer)) + N_(": ");
235 FILE* f = fopen_boost (*log, "a");
239 fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
244 PlaylistControls::add_playlist_to_list (SPL spl)
246 int const N = _spl_view->GetItemCount();
251 string t = spl.name();
253 t += " (content missing)";
255 it.SetText (std_to_wx(t));
256 _spl_view->InsertItem (it);
261 bool operator() (SPL const & a, SPL const & b) {
262 return a.name() < b.name();
267 PlaylistControls::update_playlist_directory ()
269 using namespace boost::filesystem;
271 _spl_view->DeleteAllItems ();
272 optional<path> dir = Config::instance()->player_playlist_directory();
279 for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
281 if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
283 spl.read (i->path(), _content_view);
284 _playlists.push_back (spl);
286 } catch (exception& e) {
291 sort (_playlists.begin(), _playlists.end(), SPLComparator());
292 BOOST_FOREACH (SPL i, _playlists) {
293 add_playlist_to_list (i);
296 _selected_playlist = boost::none;
299 optional<dcp::EncryptedKDM>
300 PlaylistControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
302 using namespace boost::filesystem;
303 optional<path> kdm_dir = Config::instance()->player_kdm_directory();
305 return optional<dcp::EncryptedKDM>();
307 for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
309 if (file_size(i->path()) < MAX_KDM_SIZE) {
310 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
311 if (kdm.cpl_id() == dcp->cpl()) {
315 } catch (std::exception& e) {
319 return optional<dcp::EncryptedKDM>();
323 PlaylistControls::spl_selection_changed ()
325 long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
326 if (selected == -1) {
327 _current_spl_view->DeleteAllItems ();
328 _selected_playlist = boost::none;
332 if (_playlists[selected].missing()) {
333 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
334 deselect_playlist ();
338 if (_playlists[selected].get().empty()) {
339 error_dialog (this, "This playlist is empty.");
343 select_playlist (selected, 0);
347 PlaylistControls::select_playlist (int selected, int position)
349 log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
351 wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
353 BOOST_FOREACH (SPLEntry const & i, _playlists[selected].get()) {
355 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
356 if (dcp && dcp->needs_kdm()) {
357 optional<dcp::EncryptedKDM> kdm;
358 kdm = get_kdm_from_directory (dcp);
362 dcp->examine (_film, shared_ptr<Job>());
363 } catch (KDMError& e) {
364 error_dialog (this, "Could not load KDM.");
367 if (dcp->needs_kdm()) {
368 /* We didn't get a KDM for this */
369 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
370 deselect_playlist ();
376 _current_spl_view->DeleteAllItems ();
379 BOOST_FOREACH (SPLEntry i, _playlists[selected].get()) {
383 it.SetText (std_to_wx(i.name));
384 _current_spl_view->InsertItem (it);
388 _selected_playlist = selected;
389 _selected_playlist_position = position;
393 update_current_content ();
397 PlaylistControls::reset_film ()
399 DCPOMATIC_ASSERT (_selected_playlist);
400 shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
401 film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
406 PlaylistControls::config_changed (int property)
408 Controls::config_changed (property);
410 if (property == Config::PLAYER_CONTENT_DIRECTORY) {
411 _content_view->update ();
412 } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
413 update_playlist_directory ();
418 PlaylistControls::set_film (shared_ptr<Film> film)
420 Controls::set_film (film);
421 setup_sensitivity ();
425 PlaylistControls::update_current_content ()
427 DCPOMATIC_ASSERT (_selected_playlist);
429 wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
431 setup_sensitivity ();
436 /** One piece of content in our SPL has finished playing */
438 PlaylistControls::viewer_finished ()
440 if (!_selected_playlist) {
444 _selected_playlist_position++;
445 if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
446 /* Next piece of content on the SPL */
447 update_current_content ();
450 /* Finished the whole SPL */
451 _selected_playlist_position = 0;
452 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
453 _play_button->Enable (true);
454 _pause_button->Enable (false);
458 #ifdef DCPOMATIC_PLAYER_STRESS_TEST
460 PlaylistControls::play ()
466 PlaylistControls::stop ()