No-op; Fix GPL address and mention libdcp by name.
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012-2016 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 */
20
21 #include "cpl.h"
22 #include "util.h"
23 #include "reel.h"
24 #include "metadata.h"
25 #include "certificate_chain.h"
26 #include "xml.h"
27 #include "reel_picture_asset.h"
28 #include "reel_sound_asset.h"
29 #include "reel_subtitle_asset.h"
30 #include "reel_atmos_asset.h"
31 #include "local_time.h"
32 #include "dcp_assert.h"
33 #include "compose.hpp"
34 #include <libxml/parser.h>
35 #include <libxml++/libxml++.h>
36 #include <boost/foreach.hpp>
37
38 using std::string;
39 using std::stringstream;
40 using std::ostream;
41 using std::list;
42 using std::pair;
43 using std::make_pair;
44 using std::cout;
45 using boost::shared_ptr;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
48 using namespace dcp;
49
50 static string const cpl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#";
51 static string const cpl_smpte_ns   = "http://www.smpte-ra.org/schemas/429-7/2006/CPL";
52
53 CPL::CPL (string annotation_text, ContentKind content_kind)
54         : _annotation_text (annotation_text)
55         /* default _content_title_text to _annotation_text */
56         , _content_title_text (annotation_text)
57         , _content_kind (content_kind)
58         , _content_version_id ("urn:uuid:" + make_uuid ())
59 {
60         /* default _content_version_id to a random ID and _content_version_label to
61            a random ID and the current time.
62         */
63         _content_version_id = "urn:uuid:" + make_uuid();
64         _content_version_label_text = _content_version_id + LocalTime().as_string ();
65 }
66
67 /** Construct a CPL object from a XML file */
68 CPL::CPL (boost::filesystem::path file)
69         : Asset (file)
70         , _content_kind (FEATURE)
71 {
72         cxml::Document f ("CompositionPlaylist");
73         f.read_file (file);
74
75         if (f.namespace_uri() == cpl_interop_ns) {
76                 _standard = INTEROP;
77         } else if (f.namespace_uri() == cpl_smpte_ns) {
78                 _standard = SMPTE;
79         } else {
80                 boost::throw_exception (XMLError ("Unrecognised CPL namespace " + f.namespace_uri()));
81         }
82
83         _id = remove_urn_uuid (f.string_child ("Id"));
84         _annotation_text = f.optional_string_child ("AnnotationText").get_value_or ("");
85         _metadata.issuer = f.optional_string_child ("Issuer").get_value_or ("");
86         _metadata.creator = f.optional_string_child ("Creator").get_value_or ("");
87         _metadata.issue_date = f.string_child ("IssueDate");
88         _content_title_text = f.string_child ("ContentTitleText");
89         _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
90         shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
91         if (content_version) {
92                 _content_version_id = content_version->optional_string_child ("Id").get_value_or ("");
93                 _content_version_label_text = content_version->string_child ("LabelText");
94                 content_version->done ();
95         }
96         f.ignore_child ("RatingList");
97         _reels = type_grand_children<Reel> (f, "ReelList", "Reel");
98
99         f.ignore_child ("Issuer");
100         f.ignore_child ("Signer");
101         f.ignore_child ("Signature");
102
103         f.done ();
104 }
105
106 /** Add a reel to this CPL.
107  *  @param reel Reel to add.
108  */
109 void
110 CPL::add (boost::shared_ptr<Reel> reel)
111 {
112         _reels.push_back (reel);
113 }
114
115 /** Write an CompositonPlaylist XML file.
116  *  @param file Filename to write.
117  *  @param standard INTEROP or SMPTE.
118  *  @param signer Signer to sign the CPL, or 0 to add no signature.
119  */
120 void
121 CPL::write_xml (boost::filesystem::path file, Standard standard, shared_ptr<const CertificateChain> signer) const
122 {
123         xmlpp::Document doc;
124         xmlpp::Element* root;
125         if (standard == INTEROP) {
126                 root = doc.create_root_node ("CompositionPlaylist", cpl_interop_ns);
127         } else {
128                 root = doc.create_root_node ("CompositionPlaylist", cpl_smpte_ns);
129         }
130
131         if (signer) {
132                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
133         }
134
135         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
136         root->add_child("AnnotationText")->add_child_text (_annotation_text);
137         root->add_child("IssueDate")->add_child_text (_metadata.issue_date);
138         root->add_child("Issuer")->add_child_text (_metadata.issuer);
139         root->add_child("Creator")->add_child_text (_metadata.creator);
140         root->add_child("ContentTitleText")->add_child_text (_content_title_text);
141         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
142         {
143                 xmlpp::Node* cv = root->add_child ("ContentVersion");
144                 cv->add_child ("Id")->add_child_text (_content_version_id);
145                 cv->add_child ("LabelText")->add_child_text (_content_version_label_text);
146         }
147         root->add_child("RatingList");
148
149         xmlpp::Element* reel_list = root->add_child ("ReelList");
150
151         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
152                 i->write_to_cpl (reel_list, standard);
153         }
154
155         if (signer) {
156                 signer->sign (root, standard);
157         }
158
159         /* This must not be the _formatted version otherwise signature digests will be wrong */
160         doc.write_to_file (file.string (), "UTF-8");
161
162         set_file (file);
163 }
164
165 list<shared_ptr<const ReelAsset> >
166 CPL::reel_assets () const
167 {
168         list<shared_ptr<const ReelAsset> > c;
169
170         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
171                 if (i->main_picture ()) {
172                         c.push_back (i->main_picture());
173                 }
174                 if (i->main_sound ()) {
175                         c.push_back (i->main_sound());
176                 }
177                 if (i->main_subtitle ()) {
178                         c.push_back (i->main_subtitle());
179                 }
180                 if (i->atmos ()) {
181                         c.push_back (i->atmos());
182                 }
183         }
184
185         return c;
186 }
187
188 bool
189 CPL::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
190 {
191         shared_ptr<const CPL> other_cpl = dynamic_pointer_cast<const CPL> (other);
192         if (!other_cpl) {
193                 return false;
194         }
195
196         if (_annotation_text != other_cpl->_annotation_text && !opt.cpl_annotation_texts_can_differ) {
197                 stringstream s;
198                 s << "CPL: annotation texts differ: " << _annotation_text << " vs " << other_cpl->_annotation_text << "\n";
199                 note (DCP_ERROR, s.str ());
200                 return false;
201         }
202
203         if (_content_kind != other_cpl->_content_kind) {
204                 note (DCP_ERROR, "CPL: content kinds differ");
205                 return false;
206         }
207
208         if (_reels.size() != other_cpl->_reels.size()) {
209                 note (DCP_ERROR, String::compose ("CPL: reel counts differ (%1 vs %2)", _reels.size(), other_cpl->_reels.size()));
210                 return false;
211         }
212
213         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
214         list<shared_ptr<Reel> >::const_iterator b = other_cpl->_reels.begin ();
215
216         while (a != _reels.end ()) {
217                 if (!(*a)->equals (*b, opt, note)) {
218                         return false;
219                 }
220                 ++a;
221                 ++b;
222         }
223
224         return true;
225 }
226
227 /** @return true if we have any encrypted content */
228 bool
229 CPL::encrypted () const
230 {
231         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
232                 if (i->encrypted ()) {
233                         return true;
234                 }
235         }
236
237         return false;
238 }
239
240 /** Add a KDM to this CPL.  If the KDM is for any of this CPLs assets it will be used
241  *  to decrypt those assets.
242  *  @param kdm KDM.
243  */
244 void
245 CPL::add (DecryptedKDM const & kdm)
246 {
247         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
248                 i->add (kdm);
249         }
250 }
251
252 void
253 CPL::resolve_refs (list<shared_ptr<Asset> > assets)
254 {
255         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
256                 i->resolve_refs (assets);
257         }
258 }
259
260 string
261 CPL::pkl_type (Standard standard) const
262 {
263         switch (standard) {
264         case INTEROP:
265                 return "text/xml;asdcpKind=CPL";
266         case SMPTE:
267                 return "text/xml";
268         default:
269                 DCP_ASSERT (false);
270         }
271 }
272
273 int64_t
274 CPL::duration () const
275 {
276         int64_t d = 0;
277         BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
278                 d += i->duration ();
279         }
280         return d;
281 }