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 std::shared_ptr;
44 using std::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"))
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);
62 _spl_view = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_NO_HEADER);
63 _spl_view->AppendColumn (wxT(""), wxLIST_FORMAT_LEFT, 740);
65 wxBoxSizer* left_sizer = new wxBoxSizer (wxVERTICAL);
66 wxBoxSizer* e_sizer = new wxBoxSizer (wxHORIZONTAL);
68 wxFont subheading_font (*wxNORMAL_FONT);
69 subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
71 wxBoxSizer* spl_header = new wxBoxSizer (wxHORIZONTAL);
73 wxStaticText* m = new StaticText (this, "Playlists");
74 m->SetFont (subheading_font);
75 spl_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
77 _refresh_spl_view = new Button (this, "Refresh");
78 spl_header->Add (_refresh_spl_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
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);
83 _content_view = new ContentView (this);
85 wxBoxSizer* content_header = new wxBoxSizer (wxHORIZONTAL);
87 wxStaticText* m = new StaticText (this, "Content");
88 m->SetFont (subheading_font);
89 content_header->Add (m, 1, wxALIGN_CENTER_VERTICAL);
91 _refresh_content_view = new Button (this, "Refresh");
92 content_header->Add (_refresh_content_view, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP / 2);
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);
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);
103 _v_sizer->Add (e_sizer, 1, wxEXPAND);
105 _play_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::play_clicked, this));
106 _pause_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::pause_clicked, this));
107 _stop_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::stop_clicked, this));
108 _next_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::next_clicked, this));
109 _previous_button->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::previous_clicked, this));
110 _spl_view->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
111 _spl_view->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&PlaylistControls::spl_selection_changed, this));
112 _viewer->Finished.connect (boost::bind(&PlaylistControls::viewer_finished, this));
113 _refresh_spl_view->Bind (wxEVT_BUTTON, boost::bind(&PlaylistControls::update_playlist_directory, this));
114 _refresh_content_view->Bind (wxEVT_BUTTON, boost::bind(&ContentView::update, _content_view));
116 _content_view->update ();
117 update_playlist_directory ();
121 PlaylistControls::started ()
123 Controls::started ();
124 _play_button->Enable (false);
125 _pause_button->Enable (true);
128 /** Called when the viewer finishes a single piece of content, or it is explicitly stopped */
130 PlaylistControls::stopped ()
132 Controls::stopped ();
133 _play_button->Enable (true);
134 _pause_button->Enable (false);
138 PlaylistControls::deselect_playlist ()
140 long int const selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
141 if (selected != -1) {
142 _selected_playlist = boost::none;
143 _spl_view->SetItemState (selected, 0, wxLIST_STATE_SELECTED);
145 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
149 PlaylistControls::play_clicked ()
155 PlaylistControls::setup_sensitivity ()
157 Controls::setup_sensitivity ();
158 bool const active_job = _active_job && *_active_job != "examine_content";
159 bool const c = _film && !_film->content().empty() && !active_job;
160 _play_button->Enable (c && !_viewer->playing());
161 _pause_button->Enable (_viewer->playing());
162 _spl_view->Enable (!_viewer->playing());
163 _next_button->Enable (can_do_next());
164 _previous_button->Enable (can_do_previous());
168 PlaylistControls::pause_clicked ()
174 PlaylistControls::stop_clicked ()
177 _viewer->seek (DCPTime(), true);
178 if (_selected_playlist) {
179 _selected_playlist_position = 0;
180 update_current_content ();
182 deselect_playlist ();
186 PlaylistControls::can_do_previous ()
188 return _selected_playlist && (_selected_playlist_position - 1) >= 0;
192 PlaylistControls::previous_clicked ()
194 if (!can_do_previous ()) {
198 _selected_playlist_position--;
199 update_current_content ();
203 PlaylistControls::can_do_next ()
205 return _selected_playlist && (_selected_playlist_position + 1) < int(_playlists[*_selected_playlist].get().size());
209 PlaylistControls::next_clicked ()
211 if (!can_do_next ()) {
215 _selected_playlist_position++;
216 update_current_content ();
220 PlaylistControls::log (wxString s)
222 optional<boost::filesystem::path> log = Config::instance()->player_activity_log_file();
228 gettimeofday (&time, 0);
230 time_t const sec = time.tv_sec;
231 struct tm* t = localtime (&sec);
232 strftime (buffer, 64, "%c", t);
233 wxString ts = std_to_wx(string(buffer)) + N_(": ");
234 FILE* f = fopen_boost (*log, "a");
238 fprintf (f, "%s%s\n", wx_to_std(ts).c_str(), wx_to_std(s).c_str());
243 PlaylistControls::add_playlist_to_list (SPL spl)
245 int const N = _spl_view->GetItemCount();
250 string t = spl.name();
252 t += " (content missing)";
254 it.SetText (std_to_wx(t));
255 _spl_view->InsertItem (it);
260 bool operator() (SPL const & a, SPL const & b) {
261 return a.name() < b.name();
266 PlaylistControls::update_playlist_directory ()
268 using namespace boost::filesystem;
270 _spl_view->DeleteAllItems ();
271 optional<path> dir = Config::instance()->player_playlist_directory();
278 for (directory_iterator i = directory_iterator(*dir); i != directory_iterator(); ++i) {
280 if (is_regular_file(i->path()) && i->path().extension() == ".xml") {
282 spl.read (i->path(), _content_view);
283 _playlists.push_back (spl);
285 } catch (exception& e) {
290 sort (_playlists.begin(), _playlists.end(), SPLComparator());
291 for (auto i: _playlists) {
292 add_playlist_to_list (i);
295 _selected_playlist = boost::none;
298 optional<dcp::EncryptedKDM>
299 PlaylistControls::get_kdm_from_directory (shared_ptr<DCPContent> dcp)
301 using namespace boost::filesystem;
302 optional<path> kdm_dir = Config::instance()->player_kdm_directory();
304 return optional<dcp::EncryptedKDM>();
306 for (directory_iterator i = directory_iterator(*kdm_dir); i != directory_iterator(); ++i) {
308 if (file_size(i->path()) < MAX_KDM_SIZE) {
309 dcp::EncryptedKDM kdm (dcp::file_to_string(i->path()));
310 if (kdm.cpl_id() == dcp->cpl()) {
314 } catch (std::exception& e) {
318 return optional<dcp::EncryptedKDM>();
322 PlaylistControls::spl_selection_changed ()
324 long int selected = _spl_view->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
325 if (selected == -1) {
326 _current_spl_view->DeleteAllItems ();
327 _selected_playlist = boost::none;
331 if (_playlists[selected].missing()) {
332 error_dialog (this, "This playlist cannot be loaded as some content is missing.");
333 deselect_playlist ();
337 if (_playlists[selected].get().empty()) {
338 error_dialog (this, "This playlist is empty.");
342 select_playlist (selected, 0);
346 PlaylistControls::select_playlist (int selected, int position)
348 log (wxString::Format("load-playlist %s", std_to_wx(_playlists[selected].name()).data()));
350 wxProgressDialog dialog (_("DCP-o-matic"), "Loading playlist and KDMs");
352 for (auto const& i: _playlists[selected].get()) {
354 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (i.content);
355 if (dcp && dcp->needs_kdm()) {
356 optional<dcp::EncryptedKDM> kdm;
357 kdm = get_kdm_from_directory (dcp);
361 dcp->examine (_film, shared_ptr<Job>());
362 } catch (KDMError& e) {
363 error_dialog (this, "Could not load KDM.");
366 if (dcp->needs_kdm()) {
367 /* We didn't get a KDM for this */
368 error_dialog (this, "This playlist cannot be loaded as a KDM is missing or incorrect.");
369 deselect_playlist ();
375 _current_spl_view->DeleteAllItems ();
378 for (auto i: _playlists[selected].get()) {
382 it.SetText (std_to_wx(i.name));
383 _current_spl_view->InsertItem (it);
387 _selected_playlist = selected;
388 _selected_playlist_position = position;
392 update_current_content ();
396 PlaylistControls::reset_film ()
398 DCPOMATIC_ASSERT (_selected_playlist);
399 shared_ptr<Film> film (new Film(optional<boost::filesystem::path>()));
400 film->add_content (_playlists[*_selected_playlist].get()[_selected_playlist_position].content);
405 PlaylistControls::config_changed (int property)
407 Controls::config_changed (property);
409 if (property == Config::PLAYER_CONTENT_DIRECTORY) {
410 _content_view->update ();
411 } else if (property == Config::PLAYER_PLAYLIST_DIRECTORY) {
412 update_playlist_directory ();
417 PlaylistControls::set_film (shared_ptr<Film> film)
419 Controls::set_film (film);
420 setup_sensitivity ();
424 PlaylistControls::update_current_content ()
426 DCPOMATIC_ASSERT (_selected_playlist);
428 wxProgressDialog dialog (_("DCP-o-matic"), "Loading content");
430 setup_sensitivity ();
435 /** One piece of content in our SPL has finished playing */
437 PlaylistControls::viewer_finished ()
439 if (!_selected_playlist) {
443 _selected_playlist_position++;
444 if (_selected_playlist_position < int(_playlists[*_selected_playlist].get().size())) {
445 /* Next piece of content on the SPL */
446 update_current_content ();
449 /* Finished the whole SPL */
450 _selected_playlist_position = 0;
451 ResetFilm (shared_ptr<Film>(new Film(optional<boost::filesystem::path>())));
452 _play_button->Enable (true);
453 _pause_button->Enable (false);
458 PlaylistControls::play ()
464 PlaylistControls::stop ()