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