Add basic KDM information to the player (#2225).
[dcpomatic.git] / src / wx / player_information.cc
1 /*
2     Copyright (C) 2017-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
22 #include "player_information.h"
23 #include "wx_util.h"
24 #include "film_viewer.h"
25 #include "lib/playlist.h"
26 #include "lib/compose.hpp"
27 #include "lib/video_content.h"
28 #include "lib/audio_content.h"
29 #include "lib/dcp_content.h"
30 #include "lib/film.h"
31
32
33 using std::cout;
34 using std::dynamic_pointer_cast;
35 using std::shared_ptr;
36 using std::string;
37 using std::weak_ptr;
38 using boost::optional;
39
40
41 /* This should be even */
42 static int const dcp_lines = 6;
43
44
45 PlayerInformation::PlayerInformation (wxWindow* parent, weak_ptr<FilmViewer> viewer)
46         : wxPanel (parent)
47         , _viewer (viewer)
48         , _sizer (new wxBoxSizer (wxHORIZONTAL))
49 {
50         wxFont title_font (*wxNORMAL_FONT);
51         title_font.SetWeight (wxFONTWEIGHT_BOLD);
52
53         _dcp = new wxStaticText*[dcp_lines];
54
55         DCPOMATIC_ASSERT ((dcp_lines % 2) == 0);
56
57         {
58                 auto s = new wxBoxSizer (wxVERTICAL);
59                 add_label_to_sizer(s, this, _("DCP"), false, 0)->SetFont(title_font);
60                 for (int i = 0; i < dcp_lines / 2; ++i) {
61                         _dcp[i] = add_label_to_sizer(s, this, wxT(""), false, 0);
62                 }
63                 _sizer->Add (s, 1, wxEXPAND | wxALL, 6);
64         }
65
66         {
67                 auto s = new wxBoxSizer (wxVERTICAL);
68                 add_label_to_sizer(s, this, wxT(" "), false, 0);
69                 for (int i = dcp_lines / 2; i < dcp_lines; ++i) {
70                         _dcp[i] = add_label_to_sizer(s, this, wxT(""), false, 0);
71                 }
72                 _sizer->Add (s, 1, wxEXPAND | wxALL, 6);
73         }
74
75         {
76                 _kdm_panel = new wxPanel(this, wxID_ANY);
77                 auto s = new wxBoxSizer(wxVERTICAL);
78                 add_label_to_sizer(s, _kdm_panel, _("KDM"), false, 0)->SetFont(title_font);
79                 auto g = new wxGridBagSizer(0, DCPOMATIC_SIZER_GAP);
80                 add_label_to_sizer(g, _kdm_panel, _("Valid from"), true, wxGBPosition(0, 0));
81                 _kdm_from = add_label_to_sizer(g, _kdm_panel, wxT(""), false, wxGBPosition(0, 1));
82                 add_label_to_sizer(g, _kdm_panel, _("Valid to"), true, wxGBPosition(1, 0));
83                 _kdm_to = add_label_to_sizer(g, _kdm_panel, wxT(""), false, wxGBPosition(1, 1));
84                 auto pad = new wxBoxSizer(wxVERTICAL);
85                 s->Add(g, 1, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_GAP);
86                 _kdm_panel->SetSizer(s);
87                 _sizer->Add(_kdm_panel, 1, wxEXPAND | wxALL, 6);
88         }
89
90         {
91                 auto s = new wxBoxSizer (wxVERTICAL);
92                 add_label_to_sizer(s, this, _("Performance"), false, 0)->SetFont(title_font);
93                 _dropped = add_label_to_sizer(s, this, wxT(""), false, 0);
94                 _decode_resolution = add_label_to_sizer(s, this, wxT(""), false, 0);
95                 _sizer->Add (s, 2, wxEXPAND | wxALL, 6);
96         }
97
98         SetSizerAndFit (_sizer);
99
100         triggered_update ();
101
102         Bind (wxEVT_TIMER, boost::bind (&PlayerInformation::periodic_update, this));
103         _timer.reset (new wxTimer (this));
104         _timer->Start (500);
105 }
106
107
108 void
109 PlayerInformation::periodic_update ()
110 {
111         auto fv = _viewer.lock ();
112         if (fv) {
113                 auto s = wxString::Format(_("Dropped frames: %d"), fv->dropped() + fv->errored());
114                 if (fv->errored() == 1) {
115                         s += wxString::Format(_(" (%d error)"), fv->errored());
116                 } else if (fv->errored() > 1) {
117                         s += wxString::Format(_(" (%d errors)"), fv->errored());
118                 }
119                 checked_set (_dropped, s);
120         }
121 }
122
123
124 void
125 PlayerInformation::triggered_update ()
126 {
127         auto fv = _viewer.lock ();
128         if (!fv) {
129                 return;
130         }
131
132         shared_ptr<DCPContent> dcp;
133         if (fv->film()) {
134                 auto content = fv->film()->content();
135                 if (content.size() == 1) {
136                         dcp = dynamic_pointer_cast<DCPContent>(content.front());
137                 }
138         }
139
140         if (!dcp) {
141                 checked_set (_dcp[0], _("No DCP loaded."));
142                 for (int r = 1; r < dcp_lines; ++r) {
143                         checked_set (_dcp[r], wxT(""));
144                 }
145                 checked_set (_decode_resolution, wxT(""));
146                 _kdm_panel->Hide();
147                 return;
148         }
149
150         int r = 0;
151         checked_set (_dcp[r++], std_to_wx(dcp->name()));
152
153         if (dcp->needs_assets()) {
154                 checked_set (_dcp[r], _("Needs OV"));
155                 return;
156         }
157
158         if (dcp->needs_kdm()) {
159                 checked_set (_dcp[r], _("Needs KDM"));
160                 return;
161         }
162
163         DCPOMATIC_ASSERT (dcp->video);
164
165         checked_set (_dcp[r++], wxString::Format(_("Size: %dx%d"), dcp->video->size().width, dcp->video->size().height));
166         if (dcp->video_frame_rate()) {
167                 checked_set (_dcp[r++], wxString::Format(_("Frame rate: %d"), (int) lrint(*dcp->video_frame_rate())));
168         }
169         if (dcp->audio && !dcp->audio->streams().empty()) {
170                 checked_set (_dcp[r++], wxString::Format(_("Audio channels: %d"), dcp->audio->streams().front()->channels()));
171         }
172         if (!dcp->text.empty()) {
173                 checked_set (_dcp[r++], _("Subtitles: yes"));
174         } else {
175                 checked_set (_dcp[r++], _("Subtitles: no"));
176         }
177
178         optional<double> vfr;
179         vfr = dcp->video_frame_rate ();
180         DCPOMATIC_ASSERT (vfr);
181
182         auto const len = String::compose(
183                 wx_to_std(_("Length: %1 (%2 frames)")),
184                 time_to_hmsf(dcp->full_length(fv->film()), lrint(*vfr)),
185                 dcp->full_length(fv->film()).frames_round(*vfr)
186                 );
187
188         checked_set (_dcp[r++], std_to_wx(len));
189
190         auto decode = dcp->video->size();
191         auto reduction = fv->dcp_decode_reduction();
192         if (reduction) {
193                 decode.width /= pow(2, *reduction);
194                 decode.height /= pow(2, *reduction);
195         }
196
197         checked_set (_decode_resolution, wxString::Format(_("Decode resolution: %dx%d"), decode.width, decode.height));
198
199         DCPOMATIC_ASSERT(r <= dcp_lines);
200
201         if (dcp->encrypted() && dcp->kdm()) {
202                 _kdm_panel->Show();
203                 auto const kdm = *dcp->kdm();
204                 auto const before = kdm.not_valid_before();
205                 checked_set(_kdm_from, wxString::Format(_("%s %s"), std_to_wx(before.date()), std_to_wx(before.time_of_day(true, false))));
206                 auto const after = kdm.not_valid_after();
207                 checked_set(_kdm_to, wxString::Format(_("%s %s"), std_to_wx(after.date()), std_to_wx(after.time_of_day(true, false))));
208         } else {
209                 _kdm_panel->Hide();
210         }
211
212         _sizer->Layout ();
213 }