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