2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
24 #include "raw_convert.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"
35 #include "exceptions.h"
37 #include "certificate_chain.h"
38 #include "compose.hpp"
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>
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::algorithm::starts_with;
67 DCP::DCP (boost::filesystem::path directory)
68 : _directory (directory)
70 if (!boost::filesystem::exists (directory)) {
71 boost::filesystem::create_directories (directory);
74 _directory = boost::filesystem::canonical (_directory);
77 template<class T> void
78 survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
82 errors->push_back (shared_ptr<T> (new T (e)));
90 DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
92 /* Read the ASSETMAP */
94 boost::filesystem::path asset_map_file;
95 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
96 asset_map_file = _directory / "ASSETMAP";
97 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
98 asset_map_file = _directory / "ASSETMAP.xml";
100 boost::throw_exception (DCPReadError (String::compose ("could not find AssetMap file in `%1'", _directory.string())));
103 cxml::Document asset_map ("AssetMap");
104 asset_map.read_file (asset_map_file);
105 list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
106 map<string, boost::filesystem::path> paths;
107 BOOST_FOREACH (shared_ptr<cxml::Node> i, asset_nodes) {
108 if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
109 boost::throw_exception (XMLError ("unsupported asset chunk count"));
111 string p = i->node_child("ChunkList")->node_child("Chunk")->string_child ("Path");
112 if (starts_with (p, "file://")) {
115 paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
118 /* Read all the assets from the asset map */
119 /* XXX: I think we should be looking at the PKL here to decide type, not
120 the extension of the file.
123 /* Make a list of non-CPL assets so that we can resolve the references
126 list<shared_ptr<Asset> > other_assets;
128 for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
129 boost::filesystem::path path = _directory / i->second;
131 if (!boost::filesystem::exists (path)) {
132 survivable_error (keep_going, errors, MissingAssetError (path));
136 if (boost::filesystem::extension (path) == ".xml") {
137 xmlpp::DomParser* p = new xmlpp::DomParser;
139 p->parse_file (path.string());
140 } catch (std::exception& e) {
145 string const root = p->get_document()->get_root_node()->get_name ();
148 if (root == "CompositionPlaylist") {
149 _cpls.push_back (shared_ptr<CPL> (new CPL (path)));
150 } else if (root == "DCSubtitle") {
151 other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
153 } else if (boost::filesystem::extension (path) == ".mxf") {
154 ASDCP::EssenceType_t type;
155 if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
156 throw DCPReadError ("Could not find essence type");
159 case ASDCP::ESS_UNKNOWN:
160 case ASDCP::ESS_MPEG2_VES:
161 throw DCPReadError ("MPEG2 video essences are not supported");
162 case ASDCP::ESS_JPEG_2000:
164 other_assets.push_back (shared_ptr<MonoPictureAsset> (new MonoPictureAsset (path)));
165 } catch (dcp::MXFFileError& e) {
166 if (ignore_incorrect_picture_mxf_type && e.number() == ASDCP::RESULT_SFORMAT) {
167 /* Tried to load it as mono but the error says it's stereo; try that instead */
168 other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
174 case ASDCP::ESS_PCM_24b_48k:
175 case ASDCP::ESS_PCM_24b_96k:
176 other_assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (path)));
178 case ASDCP::ESS_JPEG_2000_S:
179 other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
181 case ASDCP::ESS_TIMED_TEXT:
182 other_assets.push_back (shared_ptr<SMPTESubtitleAsset> (new SMPTESubtitleAsset (path)));
185 throw DCPReadError ("Unknown MXF essence type");
187 } else if (boost::filesystem::extension (path) == ".ttf") {
188 other_assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->first, path)));
192 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
193 i->resolve_refs (other_assets);
198 DCP::resolve_refs (list<shared_ptr<Asset> > assets)
200 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
201 i->resolve_refs (assets);
206 DCP::equals (DCP const & other, EqualityOptions opt, NoteHandler note) const
208 list<shared_ptr<CPL> > a = cpls ();
209 list<shared_ptr<CPL> > b = other.cpls ();
211 if (a.size() != b.size()) {
212 note (DCP_ERROR, String::compose ("CPL counts differ: %1 vs %2", a.size(), b.size()));
218 BOOST_FOREACH (shared_ptr<CPL> i, a) {
219 list<shared_ptr<CPL> >::const_iterator j = b.begin ();
220 while (j != b.end() && !(*j)->equals (i, opt, note)) {
233 DCP::add (boost::shared_ptr<CPL> cpl)
235 _cpls.push_back (cpl);
239 DCP::encrypted () const
241 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
242 if (i->encrypted ()) {
251 DCP::add (DecryptedKDM const & kdm)
253 list<DecryptedKDMKey> keys = kdm.keys ();
255 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
256 BOOST_FOREACH (DecryptedKDMKey const & j, kdm.keys ()) {
257 if (j.cpl_id() == i->id()) {
264 boost::filesystem::path
265 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
267 boost::filesystem::path p = _directory;
268 p /= String::compose ("pkl_%1.xml", pkl_uuid);
272 if (standard == INTEROP) {
273 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
275 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
279 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
282 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
284 /* XXX: this is a bit of a hack */
285 DCP_ASSERT (cpls().size() > 0);
286 pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
288 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
289 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
290 pkl->add_child("Creator")->add_child_text (metadata.creator);
292 xmlpp::Element* asset_list = pkl->add_child("AssetList");
293 BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
294 i->write_to_pkl (asset_list, _directory, standard);
298 signer->sign (pkl, standard);
301 doc.write_to_file (p.string (), "UTF-8");
305 /** Write the VOLINDEX file.
306 * @param standard DCP standard to use (INTEROP or SMPTE)
309 DCP::write_volindex (Standard standard) const
311 boost::filesystem::path p = _directory;
324 xmlpp::Element* root;
328 root = doc.create_root_node ("VolumeIndex", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
331 root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
337 root->add_child("Index")->add_child_text ("1");
338 doc.write_to_file (p.string (), "UTF-8");
342 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
344 boost::filesystem::path p = _directory;
358 xmlpp::Element* root;
362 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
365 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
371 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
372 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
376 root->add_child("VolumeCount")->add_child_text ("1");
377 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
378 root->add_child("Issuer")->add_child_text (metadata.issuer);
379 root->add_child("Creator")->add_child_text (metadata.creator);
382 root->add_child("Creator")->add_child_text (metadata.creator);
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);
391 xmlpp::Node* asset_list = root->add_child ("AssetList");
393 xmlpp::Node* asset = asset_list->add_child ("Asset");
394 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
395 asset->add_child("PackingList")->add_child_text ("true");
396 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
397 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
398 chunk->add_child("Path")->add_child_text ("pkl_" + pkl_uuid + ".xml");
399 chunk->add_child("VolumeIndex")->add_child_text ("1");
400 chunk->add_child("Offset")->add_child_text ("0");
401 chunk->add_child("Length")->add_child_text (raw_convert<string> (pkl_length));
403 BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
404 i->write_to_assetmap (asset_list, _directory);
407 /* This must not be the _formatted version otherwise signature digests will be wrong */
408 doc.write_to_file (p.string (), "UTF-8");
411 /** Write all the XML files for this DCP.
412 * @param standand INTEROP or SMPTE.
413 * @param metadata Metadata to use for PKL and asset map files.
414 * @param signer Signer to use, or 0.
419 XMLMetadata metadata,
420 shared_ptr<const CertificateChain> signer
423 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
424 string const filename = "cpl_" + i->id() + ".xml";
425 i->write_xml (_directory / filename, standard, signer);
428 string const pkl_uuid = make_uuid ();
429 boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
431 write_volindex (standard);
432 write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
435 list<shared_ptr<CPL> >
441 /** @return All assets (including CPLs) */
442 list<shared_ptr<Asset> >
445 list<shared_ptr<Asset> > assets;
446 BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
447 assets.push_back (i);
448 BOOST_FOREACH (shared_ptr<const ReelAsset> j, i->reel_assets ()) {
449 shared_ptr<Asset> o = j->asset_ref().asset ();
450 assets.push_back (o);
451 /* More Interop special-casing */
452 shared_ptr<InteropSubtitleAsset> sub = dynamic_pointer_cast<InteropSubtitleAsset> (o);
454 sub->add_font_assets (assets);