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