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.
25 #include "sound_mxf.h"
26 #include "picture_mxf.h"
27 #include "subtitle_content.h"
28 #include "mono_picture_mxf.h"
29 #include "stereo_picture_mxf.h"
32 #include "exceptions.h"
37 #include "compose.hpp"
39 #include <xmlsec/xmldsig.h>
40 #include <xmlsec/app.h>
41 #include <libxml++/libxml++.h>
42 #include <boost/filesystem.hpp>
43 #include <boost/lexical_cast.hpp>
44 #include <boost/algorithm/string.hpp>
45 #include <boost/lexical_cast.hpp>
53 using std::stringstream;
56 using std::back_inserter;
59 using boost::shared_ptr;
60 using boost::lexical_cast;
61 using boost::dynamic_pointer_cast;
64 DCP::DCP (boost::filesystem::path directory)
65 : _directory (directory)
67 boost::filesystem::create_directories (directory);
73 /* Read the ASSETMAP */
75 boost::filesystem::path asset_map_file;
76 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
77 asset_map_file = _directory / "ASSETMAP";
78 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
79 asset_map_file = _directory / "ASSETMAP.xml";
81 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
84 cxml::Document asset_map ("AssetMap");
85 asset_map.read_file (asset_map_file);
86 list<shared_ptr<cxml::Node> > assets = asset_map.node_child("AssetList")->node_children ("Asset");
87 map<string, boost::filesystem::path> paths;
88 for (list<shared_ptr<cxml::Node> >::const_iterator i = assets.begin(); i != assets.end(); ++i) {
89 if ((*i)->node_child("ChunkList")->node_children("Chunk").size() != 1) {
90 boost::throw_exception (XMLError ("unsupported asset chunk count"));
92 paths.insert (make_pair (
93 (*i)->string_child ("Id"),
94 (*i)->node_child("ChunkList")->node_child("Chunk")->string_child ("Path")
98 /* Read all the assets from the asset map */
100 for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
101 if (boost::algorithm::ends_with (i->second.string(), ".xml")) {
102 xmlpp::DomParser* p = new xmlpp::DomParser;
104 p->parse_file (i->second.string());
105 } catch (std::exception& e) {
110 string const root = p->get_document()->get_root_node()->get_name ();
113 if (root == "CompositionPlaylist") {
114 _assets.push_back (shared_ptr<CPL> (new CPL (i->second)));
115 } else if (root == "DCSubtitle") {
116 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (i->second)));
118 } else if (boost::algorithm::ends_with (i->second.string(), ".mxf")) {
119 ASDCP::EssenceType_t type;
120 if (ASDCP::EssenceType (i->second.string().c_str(), type) != ASDCP::RESULT_OK) {
121 throw DCPReadError ("Could not find essence type");
124 case ASDCP::ESS_UNKNOWN:
125 case ASDCP::ESS_MPEG2_VES:
126 throw DCPReadError ("MPEG2 video essences are not supported");
127 case ASDCP::ESS_JPEG_2000:
128 _assets.push_back (shared_ptr<MonoPictureMXF> (new MonoPictureMXF (i->second)));
130 case ASDCP::ESS_PCM_24b_48k:
131 case ASDCP::ESS_PCM_24b_96k:
132 _assets.push_back (shared_ptr<SoundMXF> (new SoundMXF (i->second)));
134 case ASDCP::ESS_JPEG_2000_S:
135 _assets.push_back (shared_ptr<StereoPictureMXF> (new StereoPictureMXF (i->second)));
138 throw DCPReadError ("Unknown MXF essence type");
145 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
147 if (_assets.size() != other._assets.size()) {
148 note (ERROR, "Asset counts differ");
152 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
153 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
155 while (a != _assets.end ()) {
156 if (!(*a)->equals (*b, opt, note)) {
167 DCP::add (shared_ptr<Asset> asset)
169 _assets.push_back (asset);
172 class AssetComparator
175 bool operator() (shared_ptr<const Content> a, shared_ptr<const Content> b) {
176 return a->id() < b->id();
181 DCP::encrypted () const
183 list<shared_ptr<CPL> > cpl = cpls ();
184 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
185 if ((*i)->encrypted ()) {
194 DCP::add (KDM const & kdm)
196 list<KDMKey> keys = kdm.keys ();
197 list<shared_ptr<CPL> > cpl = cpls ();
199 for (list<shared_ptr<CPL> >::iterator i = cpl.begin(); i != cpl.end(); ++i) {
200 for (list<KDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
201 if (j->cpl_id() == (*i)->id()) {
208 boost::filesystem::path
209 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const Signer> signer) const
211 boost::filesystem::path p = _directory;
213 s << pkl_uuid << "_pkl.xml";
218 if (standard == INTEROP) {
219 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
221 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
225 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
228 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
230 /* XXX: this is a bit of a hack */
231 list<shared_ptr<Asset> >::const_iterator i = _assets.begin();
232 shared_ptr<CPL> first_cpl;
233 while (i != _assets.end()) {
234 first_cpl = dynamic_pointer_cast<CPL> (*i);
241 pkl->add_child("AnnotationText")->add_child_text (first_cpl->annotation_text ());
243 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
244 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
245 pkl->add_child("Creator")->add_child_text (metadata.creator);
247 xmlpp::Element* asset_list = pkl->add_child("AssetList");
249 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
250 shared_ptr<Content> c = dynamic_pointer_cast<Content> (*i);
252 c->write_to_pkl (asset_list);
257 signer->sign (pkl, standard);
260 doc.write_to_file (p.string (), "UTF-8");
264 /** Write the VOLINDEX file.
265 * @param standard DCP standard to use (INTEROP or SMPTE)
268 DCP::write_volindex (Standard standard) const
270 boost::filesystem::path p = _directory;
271 if (standard == INTEROP) {
278 xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
279 root->add_child("Index")->add_child_text ("1");
280 doc.write_to_file (p.string (), "UTF-8");
284 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
286 boost::filesystem::path p = _directory;
287 if (standard == INTEROP) {
294 xmlpp::Element* root;
295 if (standard == INTEROP) {
296 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
298 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
301 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
302 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
303 if (standard == INTEROP) {
304 root->add_child("VolumeCount")->add_child_text ("1");
305 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
306 root->add_child("Issuer")->add_child_text (metadata.issuer);
307 root->add_child("Creator")->add_child_text (metadata.creator);
309 root->add_child("Creator")->add_child_text (metadata.creator);
310 root->add_child("VolumeCount")->add_child_text ("1");
311 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
312 root->add_child("Issuer")->add_child_text (metadata.issuer);
315 xmlpp::Node* asset_list = root->add_child ("AssetList");
317 xmlpp::Node* asset = asset_list->add_child ("Asset");
318 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
319 asset->add_child("PackingList")->add_child_text ("true");
320 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
321 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
322 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
323 chunk->add_child("VolumeIndex")->add_child_text ("1");
324 chunk->add_child("Offset")->add_child_text ("0");
325 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
327 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
328 (*i)->write_to_assetmap (asset_list);
331 /* This must not be the _formatted version otherwise signature digests will be wrong */
332 doc.write_to_file (p.string (), "UTF-8");
338 XMLMetadata metadata,
339 shared_ptr<const Signer> signer
342 string const pkl_uuid = make_uuid ();
343 boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
345 write_volindex (standard);
346 write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
348 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
349 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);
351 string const filename = c->id() + "_cpl.xml";
352 c->write_xml (_directory / filename, standard, metadata, signer);
357 list<shared_ptr<CPL> >
360 list<shared_ptr<CPL> > cpls;
361 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
362 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);