Some allowances for video/audio/subtitle possibly being null.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-2016 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 "video_content.h"
22 #include "audio_content.h"
23 #include "dcp_examiner.h"
24 #include "job.h"
25 #include "film.h"
26 #include "config.h"
27 #include "overlaps.h"
28 #include "compose.hpp"
29 #include "dcp_decoder.h"
30 #include "subtitle_content.h"
31 #include <dcp/dcp.h>
32 #include <dcp/exceptions.h>
33 #include <dcp/reel_picture_asset.h>
34 #include <dcp/reel.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/foreach.hpp>
37 #include <iterator>
38 #include <iostream>
39
40 #include "i18n.h"
41
42 using std::string;
43 using std::cout;
44 using std::distance;
45 using std::pair;
46 using std::vector;
47 using std::list;
48 using boost::shared_ptr;
49 using boost::scoped_ptr;
50 using boost::optional;
51
52 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
53 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
54 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
55 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
56
57 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
58         : Content (film)
59         , _encrypted (false)
60         , _kdm_valid (false)
61         , _reference_video (false)
62         , _reference_audio (false)
63         , _reference_subtitle (false)
64 {
65         video.reset (new VideoContent (this, film));
66         audio.reset (new AudioContent (this, film));
67
68         read_directory (p);
69         set_default_colour_conversion ();
70 }
71
72 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
73         : Content (film, node)
74 {
75         video = VideoContent::from_xml (this, film, node, version);
76         audio = AudioContent::from_xml (this, film, node);
77         subtitle = SubtitleContent::from_xml (this, film, node, version);
78
79         audio->set_stream (
80                 AudioStreamPtr (
81                         new AudioStream (node->number_child<int> ("AudioFrameRate"), AudioMapping (node->node_child ("AudioMapping"), version))
82                         )
83                 );
84
85         _name = node->string_child ("Name");
86
87         _encrypted = node->bool_child ("Encrypted");
88         if (node->optional_node_child ("KDM")) {
89                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
90         }
91         _kdm_valid = node->bool_child ("KDMValid");
92         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
93         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
94         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
95 }
96
97 void
98 DCPContent::read_directory (boost::filesystem::path p)
99 {
100         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
101                 if (boost::filesystem::is_regular_file (i->path ())) {
102                         _paths.push_back (i->path ());
103                 } else if (boost::filesystem::is_directory (i->path ())) {
104                         read_directory (i->path ());
105                 }
106         }
107 }
108
109 void
110 DCPContent::examine (shared_ptr<Job> job)
111 {
112         bool const could_be_played = can_be_played ();
113
114         job->set_progress_unknown ();
115         Content::examine (job);
116
117         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
118         video->take_from_examiner (examiner);
119         set_default_colour_conversion ();
120
121         {
122                 boost::mutex::scoped_lock lm (_mutex);
123
124                 AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_channels ()));
125                 audio->set_stream (as);
126                 AudioMapping m = as->mapping ();
127                 film()->make_audio_mapping_default (m);
128                 as->set_mapping (m);
129         }
130
131         signal_changed (AudioContentProperty::STREAMS);
132
133         {
134                 boost::mutex::scoped_lock lm (_mutex);
135                 _name = examiner->name ();
136                 if (examiner->has_subtitles ()) {
137                         subtitle.reset (new SubtitleContent (this, film()));
138                 }
139                 _encrypted = examiner->encrypted ();
140                 _kdm_valid = examiner->kdm_valid ();
141         }
142
143         if (could_be_played != can_be_played ()) {
144                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
145         }
146 }
147
148 string
149 DCPContent::summary () const
150 {
151         boost::mutex::scoped_lock lm (_mutex);
152         return String::compose (_("%1 [DCP]"), _name);
153 }
154
155 string
156 DCPContent::technical_summary () const
157 {
158         return Content::technical_summary() + " - "
159                 + video->technical_summary() + " - "
160                 + audio->technical_summary() + " - ";
161 }
162
163 void
164 DCPContent::as_xml (xmlpp::Node* node) const
165 {
166         node->add_child("Type")->add_child_text ("DCP");
167
168         Content::as_xml (node);
169
170         if (video) {
171                 video->as_xml (node);
172         }
173
174         if (audio) {
175                 audio->as_xml (node);
176                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
177                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
178         }
179
180         if (subtitle) {
181                 subtitle->as_xml (node);
182         }
183
184         boost::mutex::scoped_lock lm (_mutex);
185         node->add_child("Name")->add_child_text (_name);
186         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
187         if (_kdm) {
188                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
189         }
190         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
191         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
192         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
193         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
194 }
195
196 DCPTime
197 DCPContent::full_length () const
198 {
199         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
200         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
201 }
202
203 string
204 DCPContent::identifier () const
205 {
206         SafeStringStream s;
207         s << Content::identifier() << "_" << video->identifier() << "_";
208         if (subtitle) {
209                 s << subtitle->identifier () << " ";
210         }
211         s << (_reference_video ? "1" : "0")
212           << (_reference_subtitle ? "1" : "0");
213         return s.str ();
214 }
215
216 void
217 DCPContent::add_kdm (dcp::EncryptedKDM k)
218 {
219         _kdm = k;
220 }
221
222 bool
223 DCPContent::can_be_played () const
224 {
225         boost::mutex::scoped_lock lm (_mutex);
226         return !_encrypted || _kdm_valid;
227 }
228
229 boost::filesystem::path
230 DCPContent::directory () const
231 {
232         optional<size_t> smallest;
233         boost::filesystem::path dir;
234         for (size_t i = 0; i < number_of_paths(); ++i) {
235                 boost::filesystem::path const p = path (i).parent_path ();
236                 size_t const d = distance (p.begin(), p.end());
237                 if (!smallest || d < smallest.get ()) {
238                         dir = p;
239                 }
240         }
241
242         return dir;
243 }
244
245 void
246 DCPContent::add_properties (list<UserProperty>& p) const
247 {
248         audio->add_properties (p);
249 }
250
251 void
252 DCPContent::set_default_colour_conversion ()
253 {
254         /* Default to no colour conversion for DCPs */
255         video->unset_colour_conversion ();
256 }
257
258 void
259 DCPContent::set_reference_video (bool r)
260 {
261         {
262                 boost::mutex::scoped_lock lm (_mutex);
263                 _reference_video = r;
264         }
265
266         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
267 }
268
269 void
270 DCPContent::set_reference_audio (bool r)
271 {
272         {
273                 boost::mutex::scoped_lock lm (_mutex);
274                 _reference_audio = r;
275         }
276
277         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
278 }
279
280 void
281 DCPContent::set_reference_subtitle (bool r)
282 {
283         {
284                 boost::mutex::scoped_lock lm (_mutex);
285                 _reference_subtitle = r;
286         }
287
288         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
289 }
290
291 list<DCPTimePeriod>
292 DCPContent::reels () const
293 {
294         list<DCPTimePeriod> p;
295         scoped_ptr<DCPDecoder> decoder;
296         try {
297                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
298         } catch (...) {
299                 /* Could not load the DCP; guess reels */
300                 list<DCPTimePeriod> p;
301                 p.push_back (DCPTimePeriod (position(), end()));
302                 return p;
303         }
304
305         DCPTime from = position ();
306         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
307                 DCPTime const to = from + DCPTime::from_frames (i->main_picture()->duration(), film()->video_frame_rate());
308                 p.push_back (DCPTimePeriod (from, to));
309                 from = to;
310         }
311
312         return p;
313 }
314
315 list<DCPTime>
316 DCPContent::reel_split_points () const
317 {
318         list<DCPTime> s;
319         BOOST_FOREACH (DCPTimePeriod i, reels()) {
320                 s.push_back (i.from);
321         }
322         return s;
323 }
324
325 template <class T>
326 bool
327 DCPContent::can_reference (string overlapping, list<string>& why_not) const
328 {
329         list<DCPTimePeriod> const fr = film()->reels ();
330         /* fr must contain reels().  It can also contain other reels, but it must at
331            least contain reels().
332         */
333         BOOST_FOREACH (DCPTimePeriod i, reels()) {
334                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
335                         why_not.push_back (_("Reel lengths in the project differ from those in the DCP; set the reel mode to 'split by video content'."));
336                         return false;
337                 }
338         }
339
340         list<shared_ptr<T> > a = overlaps<T> (film()->content(), position(), end());
341         if (a.size() != 1 || a.front().get() != this) {
342                 why_not.push_back (overlapping);
343                 return false;
344         }
345
346         return true;
347 }
348
349 bool
350 DCPContent::can_reference_video (list<string>& why_not) const
351 {
352         /* XXX: this needs to be fixed */
353         return true;
354 }
355
356 bool
357 DCPContent::can_reference_audio (list<string>& why_not) const
358 {
359         /* XXX: this needs to be fixed */
360         return true;
361 }
362
363 bool
364 DCPContent::can_reference_subtitle (list<string>& why_not) const
365 {
366         /* XXX: this needs to be fixed */
367         return true;
368 }