Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "dcp_content.h"
21 #include "dcp_examiner.h"
22 #include "job.h"
23 #include "film.h"
24 #include "config.h"
25 #include "compose.hpp"
26 #include <dcp/dcp.h>
27 #include <dcp/exceptions.h>
28 #include <libxml++/libxml++.h>
29 #include <iterator>
30 #include <iostream>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::cout;
36 using std::distance;
37 using std::pair;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::optional;
41
42 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
43 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
44 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
45 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
46
47 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
48         : Content (film)
49         , VideoContent (film)
50         , SingleStreamAudioContent (film)
51         , SubtitleContent (film)
52         , _has_subtitles (false)
53         , _encrypted (false)
54         , _kdm_valid (false)
55         , _reference_video (false)
56         , _reference_audio (false)
57         , _reference_subtitle (false)
58 {
59         read_directory (p);
60 }
61
62 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
63         : Content (film, node)
64         , VideoContent (film, node, version)
65         , SingleStreamAudioContent (film, node, version)
66         , SubtitleContent (film, node, version)
67 {
68         _name = node->string_child ("Name");
69         _has_subtitles = node->bool_child ("HasSubtitles");
70         _encrypted = node->bool_child ("Encrypted");
71         if (node->optional_node_child ("KDM")) {
72                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
73         }
74         _kdm_valid = node->bool_child ("KDMValid");
75         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
76         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
77         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
78 }
79
80 void
81 DCPContent::read_directory (boost::filesystem::path p)
82 {
83         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
84                 if (boost::filesystem::is_regular_file (i->path ())) {
85                         _paths.push_back (i->path ());
86                 } else if (boost::filesystem::is_directory (i->path ())) {
87                         read_directory (i->path ());
88                 }
89         }
90 }
91
92 void
93 DCPContent::examine (shared_ptr<Job> job)
94 {
95         bool const could_be_played = can_be_played ();
96
97         job->set_progress_unknown ();
98         Content::examine (job);
99
100         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
101         take_from_video_examiner (examiner);
102         take_from_audio_examiner (examiner);
103
104         {
105                 boost::mutex::scoped_lock lm (_mutex);
106                 _name = examiner->name ();
107                 _has_subtitles = examiner->has_subtitles ();
108                 _encrypted = examiner->encrypted ();
109                 _kdm_valid = examiner->kdm_valid ();
110         }
111
112         if (could_be_played != can_be_played ()) {
113                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
114         }
115 }
116
117 string
118 DCPContent::summary () const
119 {
120         boost::mutex::scoped_lock lm (_mutex);
121         return String::compose (_("%1 [DCP]"), _name);
122 }
123
124 string
125 DCPContent::technical_summary () const
126 {
127         return Content::technical_summary() + " - "
128                 + VideoContent::technical_summary() + " - "
129                 + AudioContent::technical_summary() + " - ";
130 }
131
132 void
133 DCPContent::as_xml (xmlpp::Node* node) const
134 {
135         node->add_child("Type")->add_child_text ("DCP");
136
137         Content::as_xml (node);
138         VideoContent::as_xml (node);
139         SingleStreamAudioContent::as_xml (node);
140         SubtitleContent::as_xml (node);
141
142         boost::mutex::scoped_lock lm (_mutex);
143         node->add_child("Name")->add_child_text (_name);
144         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
145         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
146         if (_kdm) {
147                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
148         }
149         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
150         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
151         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
152         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
153 }
154
155 DCPTime
156 DCPContent::full_length () const
157 {
158         shared_ptr<const Film> film = _film.lock ();
159         DCPOMATIC_ASSERT (film);
160         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
161         return DCPTime::from_frames (llrint (video_length () * frc.factor ()), film->video_frame_rate ());
162 }
163
164 string
165 DCPContent::identifier () const
166 {
167         SafeStringStream s;
168         s << VideoContent::identifier() << "_" << SubtitleContent::identifier () << " "
169           << (_reference_video ? "1" : "0")
170           << (_reference_subtitle ? "1" : "0");
171         return s.str ();
172 }
173
174 void
175 DCPContent::add_kdm (dcp::EncryptedKDM k)
176 {
177         _kdm = k;
178 }
179
180 bool
181 DCPContent::can_be_played () const
182 {
183         boost::mutex::scoped_lock lm (_mutex);
184         return !_encrypted || _kdm_valid;
185 }
186
187 boost::filesystem::path
188 DCPContent::directory () const
189 {
190         optional<size_t> smallest;
191         boost::filesystem::path dir;
192         for (size_t i = 0; i < number_of_paths(); ++i) {
193                 boost::filesystem::path const p = path (i).parent_path ();
194                 size_t const d = distance (p.begin(), p.end());
195                 if (!smallest || d < smallest.get ()) {
196                         dir = p;
197                 }
198         }
199
200         return dir;
201 }
202
203 void
204 DCPContent::add_properties (list<pair<string, string> >& p) const
205 {
206         SingleStreamAudioContent::add_properties (p);
207 }
208
209 void
210 DCPContent::set_default_colour_conversion ()
211 {
212         /* Default to no colour conversion for DCPs */
213         unset_colour_conversion ();
214 }
215
216 void
217 DCPContent::set_reference_video (bool r)
218 {
219         {
220                 boost::mutex::scoped_lock lm (_mutex);
221                 _reference_video = r;
222         }
223
224         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
225 }
226
227 void
228 DCPContent::set_reference_audio (bool r)
229 {
230         {
231                 boost::mutex::scoped_lock lm (_mutex);
232                 _reference_audio = r;
233         }
234
235         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
236 }
237
238 void
239 DCPContent::set_reference_subtitle (bool r)
240 {
241         {
242                 boost::mutex::scoped_lock lm (_mutex);
243                 _reference_subtitle = r;
244         }
245
246         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
247 }