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