Support RatingList.
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "cpl.h"
35 #include "util.h"
36 #include "reel.h"
37 #include "metadata.h"
38 #include "certificate_chain.h"
39 #include "xml.h"
40 #include "reel_picture_asset.h"
41 #include "reel_sound_asset.h"
42 #include "reel_subtitle_asset.h"
43 #include "reel_closed_caption_asset.h"
44 #include "reel_atmos_asset.h"
45 #include "local_time.h"
46 #include "dcp_assert.h"
47 #include "compose.hpp"
48 #include <libxml/parser.h>
49 #include <libxml++/libxml++.h>
50 #include <boost/foreach.hpp>
51
52 using std::string;
53 using std::list;
54 using std::pair;
55 using std::make_pair;
56 using std::cout;
57 using boost::shared_ptr;
58 using boost::optional;
59 using boost::dynamic_pointer_cast;
60 using namespace dcp;
61
62 static string const cpl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#";
63 static string const cpl_smpte_ns   = "http://www.smpte-ra.org/schemas/429-7/2006/CPL";
64
65 CPL::CPL (string annotation_text, ContentKind content_kind)
66         /* default _content_title_text to annotation_text */
67         : _content_title_text (annotation_text)
68         , _content_kind (content_kind)
69 {
70         _metadata.annotation_text = annotation_text;
71         /* default _content_version_id to a random ID and _content_version_label to
72            a random ID and the current time.
73         */
74         string const uuid = make_uuid();
75         _content_version_id = "urn:uuid:" + uuid;
76         _content_version_label_text = uuid + LocalTime().as_string ();
77 }
78
79 /** Construct a CPL object from a XML file */
80 CPL::CPL (boost::filesystem::path file)
81         : Asset (file)
82         , _content_kind (FEATURE)
83 {
84         cxml::Document f ("CompositionPlaylist");
85         f.read_file (file);
86
87         if (f.namespace_uri() == cpl_interop_ns) {
88                 _standard = INTEROP;
89         } else if (f.namespace_uri() == cpl_smpte_ns) {
90                 _standard = SMPTE;
91         } else {
92                 boost::throw_exception (XMLError ("Unrecognised CPL namespace " + f.namespace_uri()));
93         }
94
95         _id = remove_urn_uuid (f.string_child ("Id"));
96         _metadata.annotation_text = f.optional_string_child ("AnnotationText").get_value_or ("");
97         _metadata.issuer = f.optional_string_child ("Issuer").get_value_or ("");
98         _metadata.creator = f.optional_string_child ("Creator").get_value_or ("");
99         _metadata.issue_date = f.string_child ("IssueDate");
100         _content_title_text = f.string_child ("ContentTitleText");
101         _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
102         shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
103         if (content_version) {
104                 _content_version_id = content_version->optional_string_child ("Id").get_value_or ("");
105                 _content_version_label_text = content_version->string_child ("LabelText");
106                 content_version->done ();
107         }
108         cxml::ConstNodePtr rating_list = f.node_child ("RatingList");
109         if (rating_list) {
110                 BOOST_FOREACH (cxml::ConstNodePtr i, rating_list->node_children("Rating")) {
111                         _ratings.push_back (Rating(i));
112                 }
113         }
114         _reels = type_grand_children<Reel> (f, "ReelList", "Reel");
115
116         f.ignore_child ("Issuer");
117         f.ignore_child ("Signer");
118         f.ignore_child ("Signature");
119
120         f.done ();
121 }
122
123 /** Add a reel to this CPL.
124  *  @param reel Reel to add.
125  */
126 void
127 CPL::add (boost::shared_ptr<Reel> reel)
128 {
129         _reels.push_back (reel);
130 }
131
132 /** Write an CompositonPlaylist XML file.
133  *  @param file Filename to write.
134  *  @param standard INTEROP or SMPTE.
135  *  @param signer Signer to sign the CPL, or 0 to add no signature.
136  */
137 void
138 CPL::write_xml (boost::filesystem::path file, Standard standard, shared_ptr<const CertificateChain> signer) const
139 {
140         xmlpp::Document doc;
141         xmlpp::Element* root;
142         if (standard == INTEROP) {
143                 root = doc.create_root_node ("CompositionPlaylist", cpl_interop_ns);
144         } else {
145                 root = doc.create_root_node ("CompositionPlaylist", cpl_smpte_ns);
146         }
147
148         if (signer) {
149                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
150         }
151
152         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
153         root->add_child("AnnotationText")->add_child_text (_metadata.annotation_text);
154         root->add_child("IssueDate")->add_child_text (_metadata.issue_date);
155         root->add_child("Issuer")->add_child_text (_metadata.issuer);
156         root->add_child("Creator")->add_child_text (_metadata.creator);
157         root->add_child("ContentTitleText")->add_child_text (_content_title_text);
158         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
159         {
160                 xmlpp::Node* cv = root->add_child ("ContentVersion");
161                 cv->add_child ("Id")->add_child_text (_content_version_id);
162                 cv->add_child ("LabelText")->add_child_text (_content_version_label_text);
163         }
164         xmlpp::Element* rating_list = root->add_child("RatingList");
165         BOOST_FOREACH (Rating i, _ratings) {
166                 i.as_xml (rating_list->add_child("Rating"));
167         }
168
169         xmlpp::Element* reel_list = root->add_child ("ReelList");
170
171         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
172                 i->write_to_cpl (reel_list, standard);
173         }
174
175         indent (root, 0);
176
177         if (signer) {
178                 signer->sign (root, standard);
179         }
180
181         doc.write_to_file_formatted (file.string(), "UTF-8");
182
183         set_file (file);
184 }
185
186 list<shared_ptr<ReelMXF> >
187 CPL::reel_mxfs ()
188 {
189         list<shared_ptr<ReelMXF> > c;
190
191         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
192                 if (i->main_picture ()) {
193                         c.push_back (i->main_picture());
194                 }
195                 if (i->main_sound ()) {
196                         c.push_back (i->main_sound());
197                 }
198                 if (i->main_subtitle ()) {
199                         c.push_back (i->main_subtitle());
200                 }
201                 BOOST_FOREACH (shared_ptr<ReelClosedCaptionAsset> j, i->closed_captions()) {
202                         c.push_back (j);
203                 }
204                 if (i->atmos ()) {
205                         c.push_back (i->atmos());
206                 }
207         }
208
209         return c;
210 }
211
212 list<shared_ptr<const ReelMXF> >
213 CPL::reel_mxfs () const
214 {
215         list<shared_ptr<const ReelMXF> > c;
216
217         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
218                 if (i->main_picture ()) {
219                         c.push_back (i->main_picture());
220                 }
221                 if (i->main_sound ()) {
222                         c.push_back (i->main_sound());
223                 }
224                 if (i->main_subtitle ()) {
225                         c.push_back (i->main_subtitle());
226                 }
227                 BOOST_FOREACH (shared_ptr<ReelClosedCaptionAsset> j, i->closed_captions()) {
228                         c.push_back (j);
229                 }
230                 if (i->atmos ()) {
231                         c.push_back (i->atmos());
232                 }
233         }
234
235         return c;
236 }
237
238 bool
239 CPL::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
240 {
241         shared_ptr<const CPL> other_cpl = dynamic_pointer_cast<const CPL> (other);
242         if (!other_cpl) {
243                 return false;
244         }
245
246         if (_metadata.annotation_text != other_cpl->_metadata.annotation_text && !opt.cpl_annotation_texts_can_differ) {
247                 string const s = "CPL: annotation texts differ: " + _metadata.annotation_text + " vs " + other_cpl->_metadata.annotation_text + "\n";
248                 note (DCP_ERROR, s);
249                 return false;
250         }
251
252         if (_content_kind != other_cpl->_content_kind) {
253                 note (DCP_ERROR, "CPL: content kinds differ");
254                 return false;
255         }
256
257         if (_reels.size() != other_cpl->_reels.size()) {
258                 note (DCP_ERROR, String::compose ("CPL: reel counts differ (%1 vs %2)", _reels.size(), other_cpl->_reels.size()));
259                 return false;
260         }
261
262         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
263         list<shared_ptr<Reel> >::const_iterator b = other_cpl->_reels.begin ();
264
265         while (a != _reels.end ()) {
266                 if (!(*a)->equals (*b, opt, note)) {
267                         return false;
268                 }
269                 ++a;
270                 ++b;
271         }
272
273         return true;
274 }
275
276 /** @return true if we have any encrypted content */
277 bool
278 CPL::encrypted () const
279 {
280         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
281                 if (i->encrypted ()) {
282                         return true;
283                 }
284         }
285
286         return false;
287 }
288
289 /** Add a KDM to this CPL.  If the KDM is for any of this CPLs assets it will be used
290  *  to decrypt those assets.
291  *  @param kdm KDM.
292  */
293 void
294 CPL::add (DecryptedKDM const & kdm)
295 {
296         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
297                 i->add (kdm);
298         }
299 }
300
301 void
302 CPL::resolve_refs (list<shared_ptr<Asset> > assets)
303 {
304         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
305                 i->resolve_refs (assets);
306         }
307 }
308
309 string
310 CPL::pkl_type (Standard standard) const
311 {
312         return static_pkl_type (standard);
313 }
314
315 string
316 CPL::static_pkl_type (Standard standard)
317 {
318         switch (standard) {
319         case INTEROP:
320                 return "text/xml;asdcpKind=CPL";
321         case SMPTE:
322                 return "text/xml";
323         default:
324                 DCP_ASSERT (false);
325         }
326 }
327
328 int64_t
329 CPL::duration () const
330 {
331         int64_t d = 0;
332         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
333                 d += i->duration ();
334         }
335         return d;
336 }