2 Copyright (C) 2012-2014 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_mxf.h"
27 #include "picture_mxf.h"
28 #include "subtitle_content.h"
29 #include "mono_picture_mxf.h"
30 #include "stereo_picture_mxf.h"
33 #include "exceptions.h"
36 #include "compose.hpp"
38 #include "decrypted_kdm.h"
39 #include "decrypted_kdm_key.h"
40 #include <xmlsec/xmldsig.h>
41 #include <xmlsec/app.h>
42 #include <libxml++/libxml++.h>
43 #include <boost/filesystem.hpp>
44 #include <boost/algorithm/string.hpp>
56 using boost::shared_ptr;
57 using boost::algorithm::starts_with;
60 DCP::DCP (boost::filesystem::path directory)
61 : _directory (directory)
63 if (!boost::filesystem::exists (directory)) {
64 boost::filesystem::create_directories (directory);
67 _directory = boost::filesystem::canonical (_directory);
70 template<class T> void
71 survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
75 errors->push_back (shared_ptr<T> (new T (e)));
83 DCP::read (bool keep_going, ReadErrors* errors)
85 /* Read the ASSETMAP */
87 boost::filesystem::path asset_map_file;
88 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
89 asset_map_file = _directory / "ASSETMAP";
90 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
91 asset_map_file = _directory / "ASSETMAP.xml";
93 boost::throw_exception (DCPReadError (String::compose ("could not find AssetMap file in `%1'", _directory.string())));
96 cxml::Document asset_map ("AssetMap");
97 asset_map.read_file (asset_map_file);
98 list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
99 map<string, boost::filesystem::path> paths;
100 for (list<shared_ptr<cxml::Node> >::const_iterator i = asset_nodes.begin(); i != asset_nodes.end(); ++i) {
101 if ((*i)->node_child("ChunkList")->node_children("Chunk").size() != 1) {
102 boost::throw_exception (XMLError ("unsupported asset chunk count"));
104 string p = (*i)->node_child("ChunkList")->node_child("Chunk")->string_child ("Path");
105 if (starts_with (p, "file://")) {
108 paths.insert (make_pair ((*i)->string_child ("Id"), p));
111 /* Read all the assets from the asset map */
112 for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
113 boost::filesystem::path path = _directory / i->second;
115 if (!boost::filesystem::exists (path)) {
116 survivable_error (keep_going, errors, MissingAssetError (path));
120 if (boost::algorithm::ends_with (path.string(), ".xml")) {
121 xmlpp::DomParser* p = new xmlpp::DomParser;
123 p->parse_file (path.string());
124 } catch (std::exception& e) {
129 string const root = p->get_document()->get_root_node()->get_name ();
132 if (root == "CompositionPlaylist") {
133 _assets.push_back (shared_ptr<CPL> (new CPL (path)));
134 } else if (root == "DCSubtitle") {
135 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (path, false)));
137 } else if (boost::algorithm::ends_with (path.string(), ".mxf")) {
138 ASDCP::EssenceType_t type;
139 if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
140 throw DCPReadError ("Could not find essence type");
143 case ASDCP::ESS_UNKNOWN:
144 case ASDCP::ESS_MPEG2_VES:
145 throw DCPReadError ("MPEG2 video essences are not supported");
146 case ASDCP::ESS_JPEG_2000:
147 _assets.push_back (shared_ptr<MonoPictureMXF> (new MonoPictureMXF (path)));
149 case ASDCP::ESS_PCM_24b_48k:
150 case ASDCP::ESS_PCM_24b_96k:
151 _assets.push_back (shared_ptr<SoundMXF> (new SoundMXF (path)));
153 case ASDCP::ESS_JPEG_2000_S:
154 _assets.push_back (shared_ptr<StereoPictureMXF> (new StereoPictureMXF (path)));
156 case ASDCP::ESS_TIMED_TEXT:
157 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (path, true)));
160 throw DCPReadError ("Unknown MXF essence type");
165 list<shared_ptr<CPL> > cpl = cpls ();
166 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
167 (*i)->resolve_refs (list_of_type<Asset, Object> (assets ()));
172 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
174 list<shared_ptr<CPL> > a = cpls ();
175 list<shared_ptr<CPL> > b = other.cpls ();
177 if (a.size() != b.size()) {
178 note (DCP_ERROR, String::compose ("CPL counts differ: %1 vs %2", a.size(), b.size()));
184 for (list<shared_ptr<CPL> >::const_iterator i = a.begin(); i != a.end(); ++i) {
185 list<shared_ptr<CPL> >::const_iterator j = b.begin ();
186 while (j != b.end() && !(*j)->equals (*i, opt, note)) {
199 DCP::add (boost::shared_ptr<Asset> asset)
201 _assets.push_back (asset);
205 DCP::encrypted () const
207 list<shared_ptr<CPL> > cpl = cpls ();
208 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
209 if ((*i)->encrypted ()) {
218 DCP::add (DecryptedKDM const & kdm)
220 list<DecryptedKDMKey> keys = kdm.keys ();
221 list<shared_ptr<CPL> > cpl = cpls ();
223 for (list<shared_ptr<CPL> >::iterator i = cpl.begin(); i != cpl.end(); ++i) {
224 for (list<DecryptedKDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
225 if (j->cpl_id() == (*i)->id()) {
232 boost::filesystem::path
233 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const Signer> signer) const
235 boost::filesystem::path p = _directory;
236 p /= String::compose ("%1_pkl.xml", pkl_uuid);
240 if (standard == INTEROP) {
241 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
243 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
247 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
250 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
252 /* XXX: this is a bit of a hack */
253 assert (cpls().size() > 0);
254 pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
256 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
257 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
258 pkl->add_child("Creator")->add_child_text (metadata.creator);
260 xmlpp::Element* asset_list = pkl->add_child("AssetList");
261 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
262 (*i)->write_to_pkl (asset_list, standard);
266 signer->sign (pkl, standard);
269 doc.write_to_file (p.string (), "UTF-8");
273 /** Write the VOLINDEX file.
274 * @param standard DCP standard to use (INTEROP or SMPTE)
277 DCP::write_volindex (Standard standard) const
279 boost::filesystem::path p = _directory;
292 xmlpp::Element* root;
296 root = doc.create_root_node ("VolumeIndex", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
299 root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
305 root->add_child("Index")->add_child_text ("1");
306 doc.write_to_file (p.string (), "UTF-8");
310 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
312 boost::filesystem::path p = _directory;
326 xmlpp::Element* root;
330 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
333 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
339 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
340 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
344 root->add_child("VolumeCount")->add_child_text ("1");
345 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
346 root->add_child("Issuer")->add_child_text (metadata.issuer);
347 root->add_child("Creator")->add_child_text (metadata.creator);
350 root->add_child("Creator")->add_child_text (metadata.creator);
351 root->add_child("VolumeCount")->add_child_text ("1");
352 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
353 root->add_child("Issuer")->add_child_text (metadata.issuer);
359 xmlpp::Node* asset_list = root->add_child ("AssetList");
361 xmlpp::Node* asset = asset_list->add_child ("Asset");
362 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
363 asset->add_child("PackingList")->add_child_text ("true");
364 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
365 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
366 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
367 chunk->add_child("VolumeIndex")->add_child_text ("1");
368 chunk->add_child("Offset")->add_child_text ("0");
369 chunk->add_child("Length")->add_child_text (raw_convert<string> (pkl_length));
371 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
372 (*i)->write_to_assetmap (asset_list, _directory);
375 /* This must not be the _formatted version otherwise signature digests will be wrong */
376 doc.write_to_file (p.string (), "UTF-8");
379 /** Write all the XML files for this DCP.
380 * @param standand INTEROP or SMPTE.
381 * @param metadata Metadata to use for PKL and asset map files.
382 * @param signer Signer to use, or 0.
387 XMLMetadata metadata,
388 shared_ptr<const Signer> signer
391 list<shared_ptr<CPL> > cpl = cpls ();
392 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
393 string const filename = (*i)->id() + "_cpl.xml";
394 (*i)->write_xml (_directory / filename, standard, signer);
397 string const pkl_uuid = make_uuid ();
398 boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
400 write_volindex (standard);
401 write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
404 list<shared_ptr<CPL> >
407 return list_of_type<Asset, CPL> (_assets);