Rename split-by-video content slightly; fix referencing to multi-reel DCPs.
[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 "compose.hpp"
26 #include "dcp_decoder.h"
27 #include <dcp/dcp.h>
28 #include <dcp/exceptions.h>
29 #include <dcp/reel_picture_asset.h>
30 #include <dcp/reel.h>
31 #include <libxml++/libxml++.h>
32 #include <boost/foreach.hpp>
33 #include <iterator>
34 #include <iostream>
35
36 #include "i18n.h"
37
38 using std::string;
39 using std::cout;
40 using std::distance;
41 using std::pair;
42 using std::list;
43 using boost::shared_ptr;
44 using boost::optional;
45
46 int const DCPContentProperty::CAN_BE_PLAYED      = 600;
47 int const DCPContentProperty::REFERENCE_VIDEO    = 601;
48 int const DCPContentProperty::REFERENCE_AUDIO    = 602;
49 int const DCPContentProperty::REFERENCE_SUBTITLE = 603;
50
51 DCPContent::DCPContent (shared_ptr<const Film> film, boost::filesystem::path p)
52         : Content (film)
53         , VideoContent (film)
54         , SingleStreamAudioContent (film)
55         , SubtitleContent (film)
56         , _has_subtitles (false)
57         , _encrypted (false)
58         , _kdm_valid (false)
59         , _reference_video (false)
60         , _reference_audio (false)
61         , _reference_subtitle (false)
62 {
63         read_directory (p);
64 }
65
66 DCPContent::DCPContent (shared_ptr<const Film> film, cxml::ConstNodePtr node, int version)
67         : Content (film, node)
68         , VideoContent (film, node, version)
69         , SingleStreamAudioContent (film, node, version)
70         , SubtitleContent (film, node, version)
71 {
72         _name = node->string_child ("Name");
73         _has_subtitles = node->bool_child ("HasSubtitles");
74         _encrypted = node->bool_child ("Encrypted");
75         if (node->optional_node_child ("KDM")) {
76                 _kdm = dcp::EncryptedKDM (node->string_child ("KDM"));
77         }
78         _kdm_valid = node->bool_child ("KDMValid");
79         _reference_video = node->optional_bool_child ("ReferenceVideo").get_value_or (false);
80         _reference_audio = node->optional_bool_child ("ReferenceAudio").get_value_or (false);
81         _reference_subtitle = node->optional_bool_child ("ReferenceSubtitle").get_value_or (false);
82 }
83
84 void
85 DCPContent::read_directory (boost::filesystem::path p)
86 {
87         for (boost::filesystem::directory_iterator i(p); i != boost::filesystem::directory_iterator(); ++i) {
88                 if (boost::filesystem::is_regular_file (i->path ())) {
89                         _paths.push_back (i->path ());
90                 } else if (boost::filesystem::is_directory (i->path ())) {
91                         read_directory (i->path ());
92                 }
93         }
94 }
95
96 void
97 DCPContent::examine (shared_ptr<Job> job)
98 {
99         bool const could_be_played = can_be_played ();
100
101         job->set_progress_unknown ();
102         Content::examine (job);
103
104         shared_ptr<DCPExaminer> examiner (new DCPExaminer (shared_from_this ()));
105         take_from_video_examiner (examiner);
106         take_from_audio_examiner (examiner);
107
108         {
109                 boost::mutex::scoped_lock lm (_mutex);
110                 _name = examiner->name ();
111                 _has_subtitles = examiner->has_subtitles ();
112                 _encrypted = examiner->encrypted ();
113                 _kdm_valid = examiner->kdm_valid ();
114         }
115
116         if (could_be_played != can_be_played ()) {
117                 signal_changed (DCPContentProperty::CAN_BE_PLAYED);
118         }
119 }
120
121 string
122 DCPContent::summary () const
123 {
124         boost::mutex::scoped_lock lm (_mutex);
125         return String::compose (_("%1 [DCP]"), _name);
126 }
127
128 string
129 DCPContent::technical_summary () const
130 {
131         return Content::technical_summary() + " - "
132                 + VideoContent::technical_summary() + " - "
133                 + AudioContent::technical_summary() + " - ";
134 }
135
136 void
137 DCPContent::as_xml (xmlpp::Node* node) const
138 {
139         node->add_child("Type")->add_child_text ("DCP");
140
141         Content::as_xml (node);
142         VideoContent::as_xml (node);
143         SingleStreamAudioContent::as_xml (node);
144         SubtitleContent::as_xml (node);
145
146         boost::mutex::scoped_lock lm (_mutex);
147         node->add_child("Name")->add_child_text (_name);
148         node->add_child("HasSubtitles")->add_child_text (_has_subtitles ? "1" : "0");
149         node->add_child("Encrypted")->add_child_text (_encrypted ? "1" : "0");
150         if (_kdm) {
151                 node->add_child("KDM")->add_child_text (_kdm->as_xml ());
152         }
153         node->add_child("KDMValid")->add_child_text (_kdm_valid ? "1" : "0");
154         node->add_child("ReferenceVideo")->add_child_text (_reference_video ? "1" : "0");
155         node->add_child("ReferenceAudio")->add_child_text (_reference_audio ? "1" : "0");
156         node->add_child("ReferenceSubtitle")->add_child_text (_reference_subtitle ? "1" : "0");
157 }
158
159 DCPTime
160 DCPContent::full_length () const
161 {
162         shared_ptr<const Film> film = _film.lock ();
163         DCPOMATIC_ASSERT (film);
164         FrameRateChange const frc (video_frame_rate (), film->video_frame_rate ());
165         return DCPTime::from_frames (llrint (video_length () * frc.factor ()), film->video_frame_rate ());
166 }
167
168 string
169 DCPContent::identifier () const
170 {
171         SafeStringStream s;
172         s << VideoContent::identifier() << "_" << SubtitleContent::identifier () << " "
173           << (_reference_video ? "1" : "0")
174           << (_reference_subtitle ? "1" : "0");
175         return s.str ();
176 }
177
178 void
179 DCPContent::add_kdm (dcp::EncryptedKDM k)
180 {
181         _kdm = k;
182 }
183
184 bool
185 DCPContent::can_be_played () const
186 {
187         boost::mutex::scoped_lock lm (_mutex);
188         return !_encrypted || _kdm_valid;
189 }
190
191 boost::filesystem::path
192 DCPContent::directory () const
193 {
194         optional<size_t> smallest;
195         boost::filesystem::path dir;
196         for (size_t i = 0; i < number_of_paths(); ++i) {
197                 boost::filesystem::path const p = path (i).parent_path ();
198                 size_t const d = distance (p.begin(), p.end());
199                 if (!smallest || d < smallest.get ()) {
200                         dir = p;
201                 }
202         }
203
204         return dir;
205 }
206
207 void
208 DCPContent::add_properties (list<pair<string, string> >& p) const
209 {
210         SingleStreamAudioContent::add_properties (p);
211 }
212
213 void
214 DCPContent::set_default_colour_conversion ()
215 {
216         /* Default to no colour conversion for DCPs */
217         unset_colour_conversion ();
218 }
219
220 void
221 DCPContent::set_reference_video (bool r)
222 {
223         {
224                 boost::mutex::scoped_lock lm (_mutex);
225                 _reference_video = r;
226         }
227
228         signal_changed (DCPContentProperty::REFERENCE_VIDEO);
229 }
230
231 void
232 DCPContent::set_reference_audio (bool r)
233 {
234         {
235                 boost::mutex::scoped_lock lm (_mutex);
236                 _reference_audio = r;
237         }
238
239         signal_changed (DCPContentProperty::REFERENCE_AUDIO);
240 }
241
242 void
243 DCPContent::set_reference_subtitle (bool r)
244 {
245         {
246                 boost::mutex::scoped_lock lm (_mutex);
247                 _reference_subtitle = r;
248         }
249
250         signal_changed (DCPContentProperty::REFERENCE_SUBTITLE);
251 }
252
253 list<DCPTime>
254 DCPContent::reel_split_points () const
255 {
256         list<DCPTime> s;
257         DCPDecoder decoder (shared_from_this(), false);
258         DCPTime t = position();
259
260         shared_ptr<const Film> film = _film.lock ();
261         DCPOMATIC_ASSERT (film);
262         BOOST_FOREACH (shared_ptr<dcp::Reel> k, decoder.reels()) {
263                 s.push_back (t);
264                 t += DCPTime::from_frames (k->main_picture()->duration(), film->video_frame_rate());
265         }
266
267         return s;
268 }