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