Move namespaces into variables.
[libdcp.git] / src / dcp.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 /** @file  src/dcp.cc
21  *  @brief DCP class.
22  */
23
24 #include "raw_convert.h"
25 #include "dcp.h"
26 #include "sound_asset.h"
27 #include "picture_asset.h"
28 #include "interop_subtitle_asset.h"
29 #include "smpte_subtitle_asset.h"
30 #include "mono_picture_asset.h"
31 #include "stereo_picture_asset.h"
32 #include "reel_subtitle_asset.h"
33 #include "util.h"
34 #include "metadata.h"
35 #include "exceptions.h"
36 #include "cpl.h"
37 #include "certificate_chain.h"
38 #include "compose.hpp"
39 #include "AS_DCP.h"
40 #include "decrypted_kdm.h"
41 #include "decrypted_kdm_key.h"
42 #include "dcp_assert.h"
43 #include "reel_asset.h"
44 #include "font_asset.h"
45 #include <xmlsec/xmldsig.h>
46 #include <xmlsec/app.h>
47 #include <libxml++/libxml++.h>
48 #include <boost/filesystem.hpp>
49 #include <boost/algorithm/string.hpp>
50 #include <boost/foreach.hpp>
51 #include <iostream>
52
53 using std::string;
54 using std::list;
55 using std::cout;
56 using std::ostream;
57 using std::make_pair;
58 using std::map;
59 using std::cout;
60 using std::cerr;
61 using std::exception;
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::algorithm::starts_with;
65 using namespace dcp;
66
67 static string const assetmap_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
68 static string const assetmap_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
69 static string const pkl_interop_ns      = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
70 static string const pkl_smpte_ns        = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
71 static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
72 static string const volindex_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
73
74 DCP::DCP (boost::filesystem::path directory)
75         : _directory (directory)
76 {
77         if (!boost::filesystem::exists (directory)) {
78                 boost::filesystem::create_directories (directory);
79         }
80
81         _directory = boost::filesystem::canonical (_directory);
82 }
83
84 template<class T> void
85 survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
86 {
87         if (keep_going) {
88                 if (errors) {
89                         errors->push_back (shared_ptr<T> (new T (e)));
90                 }
91         } else {
92                 throw e;
93         }
94 }
95
96 void
97 DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
98 {
99         /* Read the ASSETMAP */
100
101         boost::filesystem::path asset_map_file;
102         if (boost::filesystem::exists (_directory / "ASSETMAP")) {
103                 asset_map_file = _directory / "ASSETMAP";
104         } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
105                 asset_map_file = _directory / "ASSETMAP.xml";
106         } else {
107                 boost::throw_exception (DCPReadError (String::compose ("could not find AssetMap file in `%1'", _directory.string())));
108         }
109
110         cxml::Document asset_map ("AssetMap");
111         asset_map.read_file (asset_map_file);
112         list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
113         map<string, boost::filesystem::path> paths;
114         BOOST_FOREACH (shared_ptr<cxml::Node> i, asset_nodes) {
115                 if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
116                         boost::throw_exception (XMLError ("unsupported asset chunk count"));
117                 }
118                 string p = i->node_child("ChunkList")->node_child("Chunk")->string_child ("Path");
119                 if (starts_with (p, "file://")) {
120                         p = p.substr (7);
121                 }
122                 paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
123         }
124
125         /* Read all the assets from the asset map */
126         /* XXX: I think we should be looking at the PKL here to decide type, not
127            the extension of the file.
128         */
129
130         /* Make a list of non-CPL assets so that we can resolve the references
131            from the CPLs.
132         */
133         list<shared_ptr<Asset> > other_assets;
134
135         for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
136                 boost::filesystem::path path = _directory / i->second;
137
138                 if (!boost::filesystem::exists (path)) {
139                         survivable_error (keep_going, errors, MissingAssetError (path));
140                         continue;
141                 }
142
143                 if (boost::filesystem::extension (path) == ".xml") {
144                         xmlpp::DomParser* p = new xmlpp::DomParser;
145                         try {
146                                 p->parse_file (path.string());
147                         } catch (std::exception& e) {
148                                 delete p;
149                                 continue;
150                         }
151
152                         string const root = p->get_document()->get_root_node()->get_name ();
153                         delete p;
154
155                         if (root == "CompositionPlaylist") {
156                                 _cpls.push_back (shared_ptr<CPL> (new CPL (path)));
157                         } else if (root == "DCSubtitle") {
158                                 other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
159                         }
160                 } else if (boost::filesystem::extension (path) == ".mxf") {
161                         ASDCP::EssenceType_t type;
162                         if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
163                                 throw DCPReadError ("Could not find essence type");
164                         }
165                         switch (type) {
166                                 case ASDCP::ESS_UNKNOWN:
167                                 case ASDCP::ESS_MPEG2_VES:
168                                         throw DCPReadError ("MPEG2 video essences are not supported");
169                                 case ASDCP::ESS_JPEG_2000:
170                                         try {
171                                                 other_assets.push_back (shared_ptr<MonoPictureAsset> (new MonoPictureAsset (path)));
172                                         } catch (dcp::MXFFileError& e) {
173                                                 if (ignore_incorrect_picture_mxf_type && e.number() == ASDCP::RESULT_SFORMAT) {
174                                                         /* Tried to load it as mono but the error says it's stereo; try that instead */
175                                                         other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
176                                                 } else {
177                                                         throw;
178                                                 }
179                                         }
180                                         break;
181                                 case ASDCP::ESS_PCM_24b_48k:
182                                 case ASDCP::ESS_PCM_24b_96k:
183                                         other_assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (path)));
184                                         break;
185                                 case ASDCP::ESS_JPEG_2000_S:
186                                         other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
187                                         break;
188                                 case ASDCP::ESS_TIMED_TEXT:
189                                         other_assets.push_back (shared_ptr<SMPTESubtitleAsset> (new SMPTESubtitleAsset (path)));
190                                         break;
191                                 default:
192                                         throw DCPReadError ("Unknown MXF essence type");
193                         }
194                 } else if (boost::filesystem::extension (path) == ".ttf") {
195                         other_assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->first, path)));
196                 }
197         }
198
199         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
200                 i->resolve_refs (other_assets);
201         }
202 }
203
204 void
205 DCP::resolve_refs (list<shared_ptr<Asset> > assets)
206 {
207         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
208                 i->resolve_refs (assets);
209         }
210 }
211
212 bool
213 DCP::equals (DCP const & other, EqualityOptions opt, NoteHandler note) const
214 {
215         list<shared_ptr<CPL> > a = cpls ();
216         list<shared_ptr<CPL> > b = other.cpls ();
217
218         if (a.size() != b.size()) {
219                 note (DCP_ERROR, String::compose ("CPL counts differ: %1 vs %2", a.size(), b.size()));
220                 return false;
221         }
222
223         bool r = true;
224
225         BOOST_FOREACH (shared_ptr<CPL> i, a) {
226                 list<shared_ptr<CPL> >::const_iterator j = b.begin ();
227                 while (j != b.end() && !(*j)->equals (i, opt, note)) {
228                         ++j;
229                 }
230
231                 if (j == b.end ()) {
232                         r = false;
233                 }
234         }
235
236         return r;
237 }
238
239 void
240 DCP::add (boost::shared_ptr<CPL> cpl)
241 {
242         _cpls.push_back (cpl);
243 }
244
245 bool
246 DCP::encrypted () const
247 {
248         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
249                 if (i->encrypted ()) {
250                         return true;
251                 }
252         }
253
254         return false;
255 }
256
257 void
258 DCP::add (DecryptedKDM const & kdm)
259 {
260         list<DecryptedKDMKey> keys = kdm.keys ();
261
262         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
263                 BOOST_FOREACH (DecryptedKDMKey const & j, kdm.keys ()) {
264                         if (j.cpl_id() == i->id()) {
265                                 i->add (kdm);
266                         }
267                 }
268         }
269 }
270
271 boost::filesystem::path
272 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
273 {
274         boost::filesystem::path p = _directory;
275         p /= String::compose ("pkl_%1.xml", pkl_uuid);
276
277         xmlpp::Document doc;
278         xmlpp::Element* pkl;
279         if (standard == INTEROP) {
280                 pkl = doc.create_root_node("PackingList", pkl_interop_ns);
281         } else {
282                 pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
283         }
284
285         if (signer) {
286                 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
287         }
288
289         pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
290
291         /* XXX: this is a bit of a hack */
292         DCP_ASSERT (cpls().size() > 0);
293         pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
294
295         pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
296         pkl->add_child("Issuer")->add_child_text (metadata.issuer);
297         pkl->add_child("Creator")->add_child_text (metadata.creator);
298
299         xmlpp::Element* asset_list = pkl->add_child("AssetList");
300         BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
301                 i->write_to_pkl (asset_list, _directory, standard);
302         }
303
304         if (signer) {
305                 signer->sign (pkl, standard);
306         }
307
308         doc.write_to_file (p.string (), "UTF-8");
309         return p.string ();
310 }
311
312 /** Write the VOLINDEX file.
313  *  @param standard DCP standard to use (INTEROP or SMPTE)
314  */
315 void
316 DCP::write_volindex (Standard standard) const
317 {
318         boost::filesystem::path p = _directory;
319         switch (standard) {
320         case INTEROP:
321                 p /= "VOLINDEX";
322                 break;
323         case SMPTE:
324                 p /= "VOLINDEX.xml";
325                 break;
326         default:
327                 DCP_ASSERT (false);
328         }
329
330         xmlpp::Document doc;
331         xmlpp::Element* root;
332
333         switch (standard) {
334         case INTEROP:
335                 root = doc.create_root_node ("VolumeIndex", volindex_interop_ns);
336                 break;
337         case SMPTE:
338                 root = doc.create_root_node ("VolumeIndex", volindex_smpte_ns);
339                 break;
340         default:
341                 DCP_ASSERT (false);
342         }
343
344         root->add_child("Index")->add_child_text ("1");
345         doc.write_to_file (p.string (), "UTF-8");
346 }
347
348 void
349 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
350 {
351         boost::filesystem::path p = _directory;
352
353         switch (standard) {
354         case INTEROP:
355                 p /= "ASSETMAP";
356                 break;
357         case SMPTE:
358                 p /= "ASSETMAP.xml";
359                 break;
360         default:
361                 DCP_ASSERT (false);
362         }
363
364         xmlpp::Document doc;
365         xmlpp::Element* root;
366
367         switch (standard) {
368         case INTEROP:
369                 root = doc.create_root_node ("AssetMap", assetmap_interop_ns);
370                 break;
371         case SMPTE:
372                 root = doc.create_root_node ("AssetMap", assetmap_smpte_ns);
373                 break;
374         default:
375                 DCP_ASSERT (false);
376         }
377
378         root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
379         root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
380
381         switch (standard) {
382         case INTEROP:
383                 root->add_child("VolumeCount")->add_child_text ("1");
384                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
385                 root->add_child("Issuer")->add_child_text (metadata.issuer);
386                 root->add_child("Creator")->add_child_text (metadata.creator);
387                 break;
388         case SMPTE:
389                 root->add_child("Creator")->add_child_text (metadata.creator);
390                 root->add_child("VolumeCount")->add_child_text ("1");
391                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
392                 root->add_child("Issuer")->add_child_text (metadata.issuer);
393                 break;
394         default:
395                 DCP_ASSERT (false);
396         }
397
398         xmlpp::Node* asset_list = root->add_child ("AssetList");
399
400         xmlpp::Node* asset = asset_list->add_child ("Asset");
401         asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
402         asset->add_child("PackingList")->add_child_text ("true");
403         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
404         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
405         chunk->add_child("Path")->add_child_text ("pkl_" + pkl_uuid + ".xml");
406         chunk->add_child("VolumeIndex")->add_child_text ("1");
407         chunk->add_child("Offset")->add_child_text ("0");
408         chunk->add_child("Length")->add_child_text (raw_convert<string> (pkl_length));
409
410         BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
411                 i->write_to_assetmap (asset_list, _directory);
412         }
413
414         /* This must not be the _formatted version otherwise signature digests will be wrong */
415         doc.write_to_file (p.string (), "UTF-8");
416 }
417
418 /** Write all the XML files for this DCP.
419  *  @param standand INTEROP or SMPTE.
420  *  @param metadata Metadata to use for PKL and asset map files.
421  *  @param signer Signer to use, or 0.
422  */
423 void
424 DCP::write_xml (
425         Standard standard,
426         XMLMetadata metadata,
427         shared_ptr<const CertificateChain> signer
428         )
429 {
430         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
431                 string const filename = "cpl_" + i->id() + ".xml";
432                 i->write_xml (_directory / filename, standard, signer);
433         }
434
435         string const pkl_uuid = make_uuid ();
436         boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
437
438         write_volindex (standard);
439         write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
440 }
441
442 list<shared_ptr<CPL> >
443 DCP::cpls () const
444 {
445         return _cpls;
446 }
447
448 /** @return All assets (including CPLs) */
449 list<shared_ptr<Asset> >
450 DCP::assets () const
451 {
452         list<shared_ptr<Asset> > assets;
453         BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
454                 assets.push_back (i);
455                 BOOST_FOREACH (shared_ptr<const ReelAsset> j, i->reel_assets ()) {
456                         shared_ptr<Asset> o = j->asset_ref().asset ();
457                         assets.push_back (o);
458                         /* More Interop special-casing */
459                         shared_ptr<InteropSubtitleAsset> sub = dynamic_pointer_cast<InteropSubtitleAsset> (o);
460                         if (sub) {
461                                 sub->add_font_assets (assets);
462                         }
463                 }
464         }
465
466         return assets;
467 }