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;
57 using boost::shared_ptr;
58 using boost::lexical_cast;
59 using boost::dynamic_pointer_cast;
62 DCP::DCP (boost::filesystem::path directory)
63 : _directory (directory)
65 boost::filesystem::create_directories (directory);
71 /* Read the ASSETMAP */
73 boost::filesystem::path asset_map_file;
74 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
75 asset_map_file = _directory / "ASSETMAP";
76 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
77 asset_map_file = _directory / "ASSETMAP.xml";
79 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
82 cxml::Document asset_map ("AssetMap");
83 asset_map.read_file (asset_map_file);
84 list<shared_ptr<cxml::Node> > assets = asset_map.node_child("AssetList")->node_children ("Asset");
85 map<string, boost::filesystem::path> paths;
86 for (list<shared_ptr<cxml::Node> >::const_iterator i = assets.begin(); i != assets.end(); ++i) {
87 if ((*i)->node_child("ChunkList")->node_children("Chunk").size() != 1) {
88 boost::throw_exception (XMLError ("unsupported asset chunk count"));
90 paths.insert (make_pair (
91 (*i)->string_child ("Id"),
92 (*i)->node_child("ChunkList")->node_child("Chunk")->string_child ("Path")
96 /* Read all the assets from the asset map */
98 for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
99 if (boost::algorithm::ends_with (i->second.string(), ".xml")) {
100 xmlpp::DomParser* p = new xmlpp::DomParser;
102 p->parse_file (i->second.string());
103 } catch (std::exception& e) {
108 string const root = p->get_document()->get_root_node()->get_name ();
111 if (root == "CompositionPlaylist") {
112 _assets.push_back (shared_ptr<CPL> (new CPL (i->second)));
113 } else if (root == "DCSubtitle") {
114 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (i->second)));
116 } else if (boost::algorithm::ends_with (i->second.string(), ".mxf")) {
117 ASDCP::EssenceType_t type;
118 if (ASDCP::EssenceType (i->second.string().c_str(), type) != ASDCP::RESULT_OK) {
119 throw DCPReadError ("Could not find essence type");
122 case ASDCP::ESS_UNKNOWN:
123 case ASDCP::ESS_MPEG2_VES:
124 throw DCPReadError ("MPEG2 video essences are not supported");
125 case ASDCP::ESS_JPEG_2000:
126 _assets.push_back (shared_ptr<MonoPictureMXF> (new MonoPictureMXF (i->second)));
128 case ASDCP::ESS_PCM_24b_48k:
129 case ASDCP::ESS_PCM_24b_96k:
130 _assets.push_back (shared_ptr<SoundMXF> (new SoundMXF (i->second)));
132 case ASDCP::ESS_JPEG_2000_S:
133 _assets.push_back (shared_ptr<StereoPictureMXF> (new StereoPictureMXF (i->second)));
136 throw DCPReadError ("Unknown MXF essence type");
143 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
145 if (_assets.size() != other._assets.size()) {
146 note (ERROR, "Asset counts differ");
150 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
151 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
153 while (a != _assets.end ()) {
154 if (!(*a)->equals (*b, opt, note)) {
165 DCP::add (boost::shared_ptr<Asset> asset)
167 _assets.push_back (asset);
170 class AssetComparator
173 bool operator() (shared_ptr<const Content> a, shared_ptr<const Content> b) {
174 return a->id() < b->id();
179 DCP::encrypted () const
181 list<shared_ptr<CPL> > cpl = cpls ();
182 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
183 if ((*i)->encrypted ()) {
192 DCP::add (KDM const & kdm)
194 list<KDMKey> keys = kdm.keys ();
195 list<shared_ptr<CPL> > cpl = cpls ();
197 for (list<shared_ptr<CPL> >::iterator i = cpl.begin(); i != cpl.end(); ++i) {
198 for (list<KDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
199 if (j->cpl_id() == (*i)->id()) {
206 boost::filesystem::path
207 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const Signer> signer) const
209 boost::filesystem::path p = _directory;
211 s << pkl_uuid << "_pkl.xml";
216 if (standard == INTEROP) {
217 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
219 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
223 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
226 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
228 /* XXX: this is a bit of a hack */
229 list<shared_ptr<Asset> >::const_iterator i = _assets.begin();
230 shared_ptr<CPL> first_cpl;
231 while (i != _assets.end()) {
232 first_cpl = dynamic_pointer_cast<CPL> (*i);
239 pkl->add_child("AnnotationText")->add_child_text (first_cpl->annotation_text ());
241 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
242 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
243 pkl->add_child("Creator")->add_child_text (metadata.creator);
245 xmlpp::Element* asset_list = pkl->add_child("AssetList");
247 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
248 shared_ptr<Content> c = dynamic_pointer_cast<Content> (*i);
250 c->write_to_pkl (asset_list);
255 signer->sign (pkl, standard);
258 doc.write_to_file (p.string (), "UTF-8");
262 /** Write the VOLINDEX file.
263 * @param standard DCP standard to use (INTEROP or SMPTE)
266 DCP::write_volindex (Standard standard) const
268 boost::filesystem::path p = _directory;
269 if (standard == INTEROP) {
276 xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
277 root->add_child("Index")->add_child_text ("1");
278 doc.write_to_file (p.string (), "UTF-8");
282 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
284 boost::filesystem::path p = _directory;
285 if (standard == INTEROP) {
292 xmlpp::Element* root;
293 if (standard == INTEROP) {
294 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
296 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
299 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
300 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
301 if (standard == INTEROP) {
302 root->add_child("VolumeCount")->add_child_text ("1");
303 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
304 root->add_child("Issuer")->add_child_text (metadata.issuer);
305 root->add_child("Creator")->add_child_text (metadata.creator);
307 root->add_child("Creator")->add_child_text (metadata.creator);
308 root->add_child("VolumeCount")->add_child_text ("1");
309 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
310 root->add_child("Issuer")->add_child_text (metadata.issuer);
313 xmlpp::Node* asset_list = root->add_child ("AssetList");
315 xmlpp::Node* asset = asset_list->add_child ("Asset");
316 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
317 asset->add_child("PackingList")->add_child_text ("true");
318 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
319 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
320 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
321 chunk->add_child("VolumeIndex")->add_child_text ("1");
322 chunk->add_child("Offset")->add_child_text ("0");
323 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
325 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
326 (*i)->write_to_assetmap (asset_list);
329 /* This must not be the _formatted version otherwise signature digests will be wrong */
330 doc.write_to_file (p.string (), "UTF-8");
336 XMLMetadata metadata,
337 shared_ptr<const Signer> signer
340 string const pkl_uuid = make_uuid ();
341 boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
343 write_volindex (standard);
344 write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
346 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
347 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);
349 string const filename = c->id() + "_cpl.xml";
350 c->write_xml (_directory / filename, standard, metadata, signer);
355 list<shared_ptr<CPL> >
358 list<shared_ptr<CPL> > cpls;
359 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
360 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);