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