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