Signal when streams change and rebuild player pieces when it happens.
[dcpomatic.git] / src / lib / dcp_content.cc
1 /*
2     Copyright (C) 2014-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 "dcp_content.h"
22 #include "video_content.h"
23 #include "audio_content.h"
24 #include "dcp_examiner.h"
25 #include "job.h"
26 #include "film.h"
27 #include "config.h"
28 #include "overlaps.h"
29 #include "compose.hpp"
30 #include "dcp_decoder.h"
31 #include "subtitle_content.h"
32 #include <dcp/dcp.h>
33 #include <dcp/raw_convert.h>
34 #include <dcp/exceptions.h>
35 #include <dcp/reel_picture_asset.h>
36 #include <dcp/reel.h>
37 #include <libxml++/libxml++.h>
38 #include <boost/foreach.hpp>
39 #include <iterator>
40 #include <iostream>
41
42 #include "i18n.h"
43
44 using std::string;
45 using std::cout;
46 using std::distance;
47 using std::pair;
48 using std::vector;
49 using std::list;
50 using boost::shared_ptr;
51 using boost::scoped_ptr;
52 using boost::optional;
53 using boost::function;
54 using boost::dynamic_pointer_cast;
55 using dcp::raw_convert;
56
57 int const DCPContentProperty::NEEDS_ASSETS       = 600;
58 int const DCPContentProperty::NEEDS_KDM          = 601;
59 int const DCPContentProperty::REFERENCE_VIDEO    = 602;
60 int const DCPContentProperty::REFERENCE_AUDIO    = 603;
61 int const DCPContentProperty::REFERENCE_SUBTITLE = 604;
62 int const DCPContentProperty::NAME               = 605;
63 int const DCPContentProperty::HAS_SUBTITLES      = 606;
64
65 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
66         : Content (film)
67         , _encrypted (false)
68         , _needs_assets (false)
69         , _kdm_valid (false)
70         , _reference_video (false)
71         , _reference_audio (false)
72         , _reference_subtitle (false)
73         , _three_d (false)
74 {
75         video.reset (new VideoContent (this));
76         audio.reset (new AudioContent (this));
77
78         read_directory (p);
79         set_default_colour_conversion ();
80 }
81
82 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
83         : Content (film, node)
84 {
85         video = VideoContent::from_xml (this, node, version);
86         audio = AudioContent::from_xml (this, node, version);
87         subtitle = SubtitleContent::from_xml (this, node, version);
88
89         audio->set_stream (
90                 AudioStreamPtr (
91                         new AudioStream (
92                                 node->number_child<int> ("AudioFrameRate"),
93                                 /* AudioLength was not present in some old metadata versions */
94                                 node->optional_number_child<Frame>("AudioLength").get_value_or (
95                                         video->length() * node->number_child<int>("AudioFrameRate") / video_frame_rate().get()
96                                         ),
97                                 AudioMapping (node->node_child ("AudioMapping"), version)
98                                 )
99                         )
100                 );
101
102         _name = node->string_child ("Name");
103         _encrypted = node->bool_child ("Encrypted");
104         _needs_assets = node->optional_bool_child("NeedsAssets").get_value_or (false);
105         if (node->optional_node_child ("KDM")) {
106                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
107         }
108         _kdm_valid = node->bool_child ("KDMValid");
109         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
110         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
111         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
112         if (node->optional_string_child("Standard")) {
113                 string const s = node->optional_string_child("Standard").get();
114                 if (s == "Interop") {
115                         _standard = dcp::INTEROP;
116                 } else if (s == "SMPTE") {
117                         _standard = dcp::SMPTE;
118                 } else {
119                         DCPOMATIC_ASSERT (false);
120                 }
121         }
122         _three_d = node->optional_bool_child("ThreeD").get_value_or (false);
123         _cpl = node->optional_string_child("CPL");
124         BOOST_FOREACH (cxml::ConstNodePtr i, node->node_children("ReelLength")) {
125                 _reel_lengths.push_back (raw_convert<int64_t> (i->content ()));
126         }
127 }
128
129 void
130 DCPContent::read_directory (boost::filesystem::path p)
131 {
132         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
133                 if (boost::filesystem::is_regular_file (i->path())) {
134                         _paths.push_back (i->path());
135                 } else if (boost::filesystem::is_directory (i->path ())) {
136                         read_directory (i->path());
137                 }
138         }
139 }
140
141 void
142 DCPContent::examine (shared_ptr<Job> job)
143 {
144         bool const needed_assets = needs_assets ();
145         bool const needed_kdm = needs_kdm ();
146         string const old_name = name ();
147         bool had_subtitles = static_cast<bool> (subtitle);
148
149         if (job) {
150                 job->set_progress_unknown ();
151         }
152         Content::examine (job);
153
154         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
155         video->take_from_examiner (examiner);
156         set_default_colour_conversion ();
157
158         AudioStreamPtr as (new AudioStream (examiner->audio_frame_rate(), examiner->audio_length(), examiner->audio_channels()));
159         audio->set_stream (as);
160         AudioMapping m = as->mapping ();
161         film()->make_audio_mapping_default (m);
162         as->set_mapping (m);
163         signal_changed (AudioContentProperty::STREAMS);
164
165         bool has_subtitles = false;
166         {
167                 boost::mutex::scoped_lock lm (_mutex);
168                 _name = examiner->name ();
169                 if (examiner->has_subtitles ()) {
170                         subtitle.reset (new SubtitleContent (this));
171                 } else {
172                         subtitle.reset ();
173                 }
174                 has_subtitles = static_cast<bool> (subtitle);
175                 _encrypted = examiner->encrypted ();
176                 _needs_assets = examiner->needs_assets ();
177                 _kdm_valid = examiner->kdm_valid ();
178                 _standard = examiner->standard ();
179                 _three_d = examiner->three_d ();
180                 _cpl = examiner->cpl ();
181                 _reel_lengths = examiner->reel_lengths ();
182         }
183
184         if (had_subtitles != has_subtitles) {
185                 signal_changed (DCPContentProperty::HAS_SUBTITLES);
186         }
187
188         if (needed_assets != needs_assets ()) {
189                 signal_changed (DCPContentProperty::NEEDS_ASSETS);
190         }
191
192         if (needed_kdm != needs_kdm ()) {
193                 signal_changed (DCPContentProperty::NEEDS_KDM);
194         }
195
196         if (old_name != name ()) {
197                 signal_changed (DCPContentProperty::NAME);
198         }
199
200         signal_changed (AudioContentProperty::STREAMS);
201
202         video->set_frame_type (_three_d ? VIDEO_FRAME_TYPE_3D : VIDEO_FRAME_TYPE_2D);
203 }
204
205 string
206 DCPContent::summary () const
207 {
208         boost::mutex::scoped_lock lm (_mutex);
209         return String::compose (_("%1 [DCP]"), _name);
210 }
211
212 string
213 DCPContent::technical_summary () const
214 {
215         return Content::technical_summary() + " - "
216                 + video->technical_summary() + " - "
217                 + audio->technical_summary() + " - ";
218 }
219
220 void
221 DCPContent::as_xml (xmlpp::Node* node, bool with_paths) const
222 {
223         node->add_child("Type")->add_child_text ("DCP");
224
225         Content::as_xml (node, with_paths);
226
227         if (video) {
228                 video->as_xml (node);
229         }
230
231         if (audio) {
232                 audio->as_xml (node);
233                 node->add_child("AudioFrameRate")->add_child_text (raw_convert<string> (audio->stream()->frame_rate()));
234                 node->add_child("AudioLength")->add_child_text (raw_convert<string> (audio->stream()->length()));
235                 audio->stream()->mapping().as_xml (node->add_child("AudioMapping"));
236         }
237
238         if (subtitle) {
239                 subtitle->as_xml (node);
240         }
241
242         boost::mutex::scoped_lock lm (_mutex);
243         node->add_child("Name")->add_child_text (_name);
244         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
245         node->add_child("NeedsAssets")->add_child_text (_needs_assets ? "1" : "0");
246         if (_kdm) {
247                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
248         }
249         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
250         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
251         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
252         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
253         if (_standard) {
254                 switch (_standard.get ()) {
255                 case dcp::INTEROP:
256                         node->add_child("Standard")->add_child_text ("Interop");
257                         break;
258                 case dcp::SMPTE:
259                         node->add_child("Standard")->add_child_text ("SMPTE");
260                         break;
261                 default:
262                         DCPOMATIC_ASSERT (false);
263                 }
264         }
265         node->add_child("ThreeD")->add_child_text (_three_d ? "1" : "0");
266         if (_cpl) {
267                 node->add_child("CPL")->add_child_text (_cpl.get ());
268         }
269         BOOST_FOREACH (int64_t i, _reel_lengths) {
270                 node->add_child("ReelLength")->add_child_text (raw_convert<string> (i));
271         }
272 }
273
274 DCPTime
275 DCPContent::full_length () const
276 {
277         FrameRateChange const frc (active_video_frame_rate (), film()->video_frame_rate ());
278         return DCPTime::from_frames (llrint (video->length () * frc.factor ()), film()->video_frame_rate ());
279 }
280
281 string
282 DCPContent::identifier () const
283 {
284         string s = Content::identifier() + "_" + video->identifier() + "_";
285         if (subtitle) {
286                 s += subtitle->identifier () + " ";
287         }
288
289         s += string (_reference_video ? "1" : "0") + string (_reference_subtitle ? "1" : "0");
290         return s;
291 }
292
293 void
294 DCPContent::add_kdm (dcp::EncryptedKDM k)
295 {
296         _kdm = k;
297 }
298
299 void
300 DCPContent::add_ov (boost::filesystem::path ov)
301 {
302         read_directory (ov);
303 }
304
305 bool
306 DCPContent::can_be_played () const
307 {
308         return !needs_kdm() && !needs_assets();
309 }
310
311 bool
312 DCPContent::needs_kdm () const
313 {
314         boost::mutex::scoped_lock lm (_mutex);
315         return _encrypted && !_kdm_valid;
316 }
317
318 bool
319 DCPContent::needs_assets () const
320 {
321         boost::mutex::scoped_lock lm (_mutex);
322         return _needs_assets;
323 }
324
325 vector<boost::filesystem::path>
326 DCPContent::directories () const
327 {
328         return dcp::DCP::directories_from_files (paths());
329 }
330
331 void
332 DCPContent::add_properties (list<UserProperty>& p) const
333 {
334         Content::add_properties (p);
335         video->add_properties (p);
336         audio->add_properties (p);
337 }
338
339 void
340 DCPContent::set_default_colour_conversion ()
341 {
342         /* Default to no colour conversion for DCPs */
343         video->unset_colour_conversion ();
344 }
345
346 void
347 DCPContent::set_reference_video (bool r)
348 {
349         {
350                 boost::mutex::scoped_lock lm (_mutex);
351                 _reference_video = r;
352         }
353
354         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
355 }
356
357 void
358 DCPContent::set_reference_audio (bool r)
359 {
360         {
361                 boost::mutex::scoped_lock lm (_mutex);
362                 _reference_audio = r;
363         }
364
365         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
366 }
367
368 void
369 DCPContent::set_reference_subtitle (bool r)
370 {
371         {
372                 boost::mutex::scoped_lock lm (_mutex);
373                 _reference_subtitle = r;
374         }
375
376         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
377 }
378
379 list<DCPTimePeriod>
380 DCPContent::reels () const
381 {
382         list<int64_t> reel_lengths = _reel_lengths;
383         if (reel_lengths.empty ()) {
384                 /* Old metadata with no reel lengths; get them here instead */
385                 try {
386                         scoped_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this()));
387                         reel_lengths = examiner->reel_lengths ();
388                 } catch (...) {
389                         /* Could not examine the DCP; guess reels */
390                         reel_lengths.push_back (length_after_trim().frames_round (film()->video_frame_rate ()));
391                 }
392         }
393
394         list<DCPTimePeriod> p;
395
396         /* This content's frame rate must be the same as the output DCP rate, so we can
397            convert `directly' from ContentTime to DCPTime.
398         */
399
400         /* The starting point of this content on the timeline */
401         DCPTime pos = position() - DCPTime (trim_start().get());
402
403         BOOST_FOREACH (int64_t i, reel_lengths) {
404                 /* This reel runs from `pos' to `to' */
405                 DCPTime const to = pos + DCPTime::from_frames (i, film()->video_frame_rate());
406                 if (to > position()) {
407                         p.push_back (DCPTimePeriod (max(position(), pos), min(end(), to)));
408                         if (to > end()) {
409                                 break;
410                         }
411                 }
412                 pos = to;
413         }
414
415         return p;
416 }
417
418 list<DCPTime>
419 DCPContent::reel_split_points () const
420 {
421         list<DCPTime> s;
422         BOOST_FOREACH (DCPTimePeriod i, reels()) {
423                 s.push_back (i.from);
424         }
425         return s;
426 }
427
428 bool
429 DCPContent::can_reference (function<shared_ptr<ContentPart> (shared_ptr<const Content>)> part, string overlapping, string& why_not) const
430 {
431         /* We must be using the same standard as the film */
432         if (_standard) {
433                 if (_standard.get() == dcp::INTEROP && !film()->interop()) {
434                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
435                         why_not = _("it is Interop and the film is set to SMPTE.");
436                         return false;
437                 } else if (_standard.get() == dcp::SMPTE && film()->interop()) {
438                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
439                         why_not = _("it is SMPTE and the film is set to Interop.");
440                         return false;
441                 }
442         }
443
444         /* And the same frame rate */
445         if (!video_frame_rate() || (lrint(video_frame_rate().get()) != film()->video_frame_rate())) {
446                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
447                 why_not = _("it has a different frame rate to the film.");
448                 return false;
449         }
450
451         list<DCPTimePeriod> const fr = film()->reels ();
452
453         list<DCPTimePeriod> reel_list;
454         try {
455                 reel_list = reels ();
456         } catch (dcp::DCPReadError) {
457                 /* We couldn't read the DCP; it's probably missing */
458                 return false;
459         } catch (dcp::KDMDecryptionError) {
460                 /* We have an incorrect KDM */
461                 return false;
462         }
463
464         /* fr must contain reels().  It can also contain other reels, but it must at
465            least contain reels().
466         */
467         BOOST_FOREACH (DCPTimePeriod i, reel_list) {
468                 if (find (fr.begin(), fr.end(), i) == fr.end ()) {
469                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
470                         why_not = _("its reel lengths differ from those in the film; set the reel mode to 'split by video content'.");
471                         return false;
472                 }
473         }
474
475         ContentList a = overlaps (film()->content(), part, position(), end());
476         if (a.size() != 1 || a.front().get() != this) {
477                 why_not = overlapping;
478                 return false;
479         }
480
481         return true;
482 }
483
484 bool
485 DCPContent::can_reference_video (string& why_not) const
486 {
487         if (film()->frame_size() != video->size()) {
488                 /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
489                 why_not = _("its video frame size differs from the film's.");
490                 return false;
491         }
492
493         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
494         return can_reference (bind (&Content::video, _1), _("it overlaps other video content; remove the other content."), why_not);
495 }
496
497 bool
498 DCPContent::can_reference_audio (string& why_not) const
499 {
500         shared_ptr<DCPDecoder> decoder;
501         try {
502                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
503         } catch (dcp::DCPReadError) {
504                 /* We couldn't read the DCP, so it's probably missing */
505                 return false;
506         } catch (dcp::KDMDecryptionError) {
507                 /* We have an incorrect KDM */
508                 return false;
509         }
510
511         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
512                 if (!i->main_sound()) {
513                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
514                         why_not = _("it does not have sound in all its reels.");
515                         return false;
516                 }
517         }
518
519         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
520         return can_reference (bind (&Content::audio, _1), _("it overlaps other audio content; remove the other content."), why_not);
521 }
522
523 bool
524 DCPContent::can_reference_subtitle (string& why_not) const
525 {
526         shared_ptr<DCPDecoder> decoder;
527         try {
528                 decoder.reset (new DCPDecoder (shared_from_this(), film()->log(), false));
529         } catch (dcp::DCPReadError) {
530                 /* We couldn't read the DCP, so it's probably missing */
531                 return false;
532         } catch (dcp::KDMDecryptionError) {
533                 /* We have an incorrect KDM */
534                 return false;
535         }
536
537         BOOST_FOREACH (shared_ptr<dcp::Reel> i, decoder->reels()) {
538                 if (!i->main_subtitle()) {
539                         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
540                         why_not = _("it does not have subtitles in all its reels.");
541                         return false;
542                 }
543         }
544
545         /// TRANSLATORS: this string will follow "Cannot reference this DCP: "
546         return can_reference (bind (&Content::subtitle, _1), _("it overlaps other subtitle content; remove the other content."), why_not);
547 }
548
549 void
550 DCPContent::take_settings_from (shared_ptr<const Content> c)
551 {
552         shared_ptr<const DCPContent> dc = dynamic_pointer_cast<const DCPContent> (c);
553         if (!dc) {
554                 return;
555         }
556
557         _reference_video = dc->_reference_video;
558         _reference_audio = dc->_reference_audio;
559         _reference_subtitle = dc->_reference_subtitle;
560 }
561
562 void
563 DCPContent::set_cpl (string id)
564 {
565         boost::mutex::scoped_lock lm (_mutex);
566         _cpl = id;
567 }