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/algorithm/string.hpp>
44 #include <boost/lexical_cast.hpp>
52 using std::stringstream;
56 using boost::shared_ptr;
57 using boost::lexical_cast;
60 DCP::DCP (boost::filesystem::path directory)
61 : _directory (directory)
63 boost::filesystem::create_directories (directory);
64 _directory = boost::filesystem::canonical (_directory);
70 /* Read the ASSETMAP */
72 boost::filesystem::path asset_map_file;
73 if (boost::filesystem::exists (_directory / "ASSETMAP")) {
74 asset_map_file = _directory / "ASSETMAP";
75 } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
76 asset_map_file = _directory / "ASSETMAP.xml";
78 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
81 cxml::Document asset_map ("AssetMap");
82 asset_map.read_file (asset_map_file);
83 list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
84 map<string, boost::filesystem::path> paths;
85 for (list<shared_ptr<cxml::Node> >::const_iterator i = asset_nodes.begin(); i != asset_nodes.end(); ++i) {
86 if ((*i)->node_child("ChunkList")->node_children("Chunk").size() != 1) {
87 boost::throw_exception (XMLError ("unsupported asset chunk count"));
89 paths.insert (make_pair (
90 (*i)->string_child ("Id"),
91 (*i)->node_child("ChunkList")->node_child("Chunk")->string_child ("Path")
95 /* Read all the assets from the asset map */
97 for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
98 boost::filesystem::path path = _directory / i->second;
99 if (boost::algorithm::ends_with (path.string(), ".xml")) {
100 xmlpp::DomParser* p = new xmlpp::DomParser;
102 p->parse_file (path.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 (path)));
113 } else if (root == "DCSubtitle") {
114 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (path)));
116 } else if (boost::algorithm::ends_with (path.string(), ".mxf")) {
117 ASDCP::EssenceType_t type;
118 if (ASDCP::EssenceType (path.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 (path)));
128 case ASDCP::ESS_PCM_24b_48k:
129 case ASDCP::ESS_PCM_24b_96k:
130 _assets.push_back (shared_ptr<SoundMXF> (new SoundMXF (path)));
132 case ASDCP::ESS_JPEG_2000_S:
133 _assets.push_back (shared_ptr<StereoPictureMXF> (new StereoPictureMXF (path)));
136 throw DCPReadError ("Unknown MXF essence type");
141 list<shared_ptr<CPL> > cpl = cpls ();
142 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
143 (*i)->resolve_refs (list_of_type<Asset, Object> (assets ()));
148 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
150 if (_assets.size() != other._assets.size()) {
151 note (ERROR, "Asset counts differ");
155 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
156 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
158 while (a != _assets.end ()) {
159 if (!(*a)->equals (*b, opt, note)) {
170 DCP::add (boost::shared_ptr<Asset> asset)
172 _assets.push_back (asset);
175 class AssetComparator
178 bool operator() (shared_ptr<const Content> a, shared_ptr<const Content> b) {
179 return a->id() < b->id();
184 DCP::encrypted () const
186 list<shared_ptr<CPL> > cpl = cpls ();
187 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
188 if ((*i)->encrypted ()) {
197 DCP::add (KDM const & kdm)
199 list<KDMKey> keys = kdm.keys ();
200 list<shared_ptr<CPL> > cpl = cpls ();
202 for (list<shared_ptr<CPL> >::iterator i = cpl.begin(); i != cpl.end(); ++i) {
203 for (list<KDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
204 if (j->cpl_id() == (*i)->id()) {
211 boost::filesystem::path
212 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const Signer> signer) const
214 boost::filesystem::path p = _directory;
216 s << pkl_uuid << "_pkl.xml";
221 if (standard == INTEROP) {
222 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
224 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
228 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
231 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
233 /* XXX: this is a bit of a hack */
234 assert (cpls().size() > 0);
235 pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
237 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
238 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
239 pkl->add_child("Creator")->add_child_text (metadata.creator);
241 xmlpp::Element* asset_list = pkl->add_child("AssetList");
242 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
243 (*i)->write_to_pkl (asset_list);
247 signer->sign (pkl, standard);
250 doc.write_to_file (p.string (), "UTF-8");
254 /** Write the VOLINDEX file.
255 * @param standard DCP standard to use (INTEROP or SMPTE)
258 DCP::write_volindex (Standard standard) const
260 boost::filesystem::path p = _directory;
261 if (standard == INTEROP) {
268 xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
269 root->add_child("Index")->add_child_text ("1");
270 doc.write_to_file (p.string (), "UTF-8");
274 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
276 boost::filesystem::path p = _directory;
277 if (standard == INTEROP) {
284 xmlpp::Element* root;
285 if (standard == INTEROP) {
286 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
288 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
291 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
292 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
293 if (standard == INTEROP) {
294 root->add_child("VolumeCount")->add_child_text ("1");
295 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
296 root->add_child("Issuer")->add_child_text (metadata.issuer);
297 root->add_child("Creator")->add_child_text (metadata.creator);
299 root->add_child("Creator")->add_child_text (metadata.creator);
300 root->add_child("VolumeCount")->add_child_text ("1");
301 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
302 root->add_child("Issuer")->add_child_text (metadata.issuer);
305 xmlpp::Node* asset_list = root->add_child ("AssetList");
307 xmlpp::Node* asset = asset_list->add_child ("Asset");
308 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
309 asset->add_child("PackingList")->add_child_text ("true");
310 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
311 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
312 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
313 chunk->add_child("VolumeIndex")->add_child_text ("1");
314 chunk->add_child("Offset")->add_child_text ("0");
315 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
317 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
318 (*i)->write_to_assetmap (asset_list, _directory);
321 /* This must not be the _formatted version otherwise signature digests will be wrong */
322 doc.write_to_file (p.string (), "UTF-8");
328 XMLMetadata metadata,
329 shared_ptr<const Signer> signer
332 list<shared_ptr<CPL> > cpl = cpls ();
333 for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
334 string const filename = (*i)->id() + "_cpl.xml";
335 (*i)->write_xml (_directory / filename, standard, metadata, signer);
338 string const pkl_uuid = make_uuid ();
339 boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
341 write_volindex (standard);
342 write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
345 list<shared_ptr<CPL> >
348 return list_of_type<Asset, CPL> (_assets);