2 Copyright (C) 2012 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.
21 * @brief A class to create a DCP.
28 #include <boost/filesystem.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <libxml++/libxml++.h>
33 #include <xmlsec/xmldsig.h>
34 #include <xmlsec/app.h>
37 #include "sound_asset.h"
38 #include "picture_asset.h"
39 #include "subtitle_asset.h"
42 #include "exceptions.h"
43 #include "parse/pkl.h"
44 #include "parse/asset_map.h"
52 using std::stringstream;
55 using std::back_inserter;
57 using boost::shared_ptr;
58 using boost::lexical_cast;
59 using namespace libdcp;
61 DCP::DCP (boost::filesystem::path directory)
62 : _directory (directory)
64 boost::filesystem::create_directories (directory);
68 DCP::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<const Signer> signer) const
70 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
71 (*i)->write_xml (interop, metadata, signer);
74 string pkl_uuid = make_uuid ();
75 string pkl_path = write_pkl (pkl_uuid, interop, metadata, signer);
77 write_volindex (interop);
78 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), interop, metadata);
82 DCP::write_pkl (string pkl_uuid, bool interop, XMLMetadata const & metadata, shared_ptr<const Signer> signer) const
84 assert (!_cpls.empty ());
86 boost::filesystem::path p;
89 s << pkl_uuid << "_pkl.xml";
95 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
97 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
101 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
104 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
105 /* XXX: this is a bit of a hack */
106 pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
107 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
108 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
109 pkl->add_child("Creator")->add_child_text (metadata.creator);
111 xmlpp::Element* asset_list = pkl->add_child("AssetList");
112 list<shared_ptr<const Asset> > a = assets ();
113 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
114 (*i)->write_to_pkl (asset_list, interop);
117 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
118 (*i)->write_to_pkl (asset_list, interop);
122 signer->sign (pkl, interop);
125 doc.write_to_file (p.string (), "UTF-8");
130 DCP::write_volindex (bool interop) const
132 boost::filesystem::path p;
141 xmlpp::Element* root;
143 root = doc.create_root_node ("VolumeIndex", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
145 root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
147 root->add_child("Index")->add_child_text ("1");
148 doc.write_to_file (p.string (), "UTF-8");
152 DCP::write_assetmap (string pkl_uuid, int pkl_length, bool interop, XMLMetadata const & metadata) const
154 boost::filesystem::path p;
163 xmlpp::Element* root;
165 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
167 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
170 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
171 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
173 root->add_child("VolumeCount")->add_child_text ("1");
174 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
175 root->add_child("Issuer")->add_child_text (metadata.issuer);
176 root->add_child("Creator")->add_child_text (metadata.creator);
178 root->add_child("Creator")->add_child_text (metadata.creator);
179 root->add_child("VolumeCount")->add_child_text ("1");
180 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
181 root->add_child("Issuer")->add_child_text (metadata.issuer);
184 xmlpp::Node* asset_list = root->add_child ("AssetList");
186 xmlpp::Node* asset = asset_list->add_child ("Asset");
187 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
188 asset->add_child("PackingList")->add_child_text ("true");
189 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
190 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
191 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
192 chunk->add_child("VolumeIndex")->add_child_text ("1");
193 chunk->add_child("Offset")->add_child_text ("0");
194 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
196 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
197 (*i)->write_to_assetmap (asset_list);
200 list<shared_ptr<const Asset> > a = assets ();
201 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
202 (*i)->write_to_assetmap (asset_list);
205 /* This must not be the _formatted version otherwise signature digests will be wrong */
206 doc.write_to_file (p.string (), "UTF-8");
210 DCP::read (bool require_mxfs)
213 read_cpls (require_mxfs);
219 shared_ptr<parse::AssetMap> asset_map;
221 boost::filesystem::path p = _directory;
223 if (boost::filesystem::exists (p)) {
224 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
228 if (boost::filesystem::exists (p)) {
229 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
231 boost::throw_exception (FileError ("could not find AssetMap file", p, -1));
235 } catch (FileError& e) {
236 boost::throw_exception (FileError ("could not load AssetMap file", e.filename(), e.number ()));
239 for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
240 if ((*i)->chunks.size() != 1) {
241 boost::throw_exception (XMLError ("unsupported asset chunk count"));
244 boost::filesystem::path t = _directory;
245 t /= (*i)->chunks.front()->path;
247 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
251 xmlpp::DomParser* p = new xmlpp::DomParser;
253 p->parse_file (t.string());
254 } catch (std::exception& e) {
259 string const root = p->get_document()->get_root_node()->get_name ();
262 if (root == "CompositionPlaylist") {
263 _files.cpls.push_back (t.string());
264 } else if (root == "PackingList") {
265 if (_files.pkl.empty ()) {
266 _files.pkl = t.string();
268 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
273 if (_files.cpls.empty ()) {
274 boost::throw_exception (DCPReadError ("no CPL files found"));
277 if (_files.pkl.empty ()) {
278 boost::throw_exception (DCPReadError ("no PKL file found"));
281 shared_ptr<parse::PKL> pkl;
283 pkl.reset (new parse::PKL (_files.pkl));
284 } catch (FileError& e) {
285 boost::throw_exception (FileError ("could not load PKL file", _files.pkl, e.number ()));
288 _asset_maps.push_back (make_pair (boost::filesystem::absolute (_directory).string(), asset_map));
292 DCP::read_cpls (bool require_mxfs)
294 for (list<string>::iterator i = _files.cpls.begin(); i != _files.cpls.end(); ++i) {
295 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, _asset_maps, require_mxfs)));
300 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
302 if (_cpls.size() != other._cpls.size()) {
303 note (ERROR, "CPL counts differ");
307 list<shared_ptr<CPL> >::const_iterator a = _cpls.begin ();
308 list<shared_ptr<CPL> >::const_iterator b = other._cpls.begin ();
310 while (a != _cpls.end ()) {
311 if (!(*a)->equals (*b->get(), opt, note)) {
322 DCP::add_cpl (shared_ptr<CPL> cpl)
324 _cpls.push_back (cpl);
327 class AssetComparator
330 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
331 return a->uuid() < b->uuid();
335 list<shared_ptr<const Asset> >
338 list<shared_ptr<const Asset> > a;
339 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
340 list<shared_ptr<const Asset> > t = (*i)->assets ();
344 a.sort (AssetComparator ());
350 DCP::encrypted () const
352 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
353 if ((*i)->encrypted ()) {
362 DCP::add_kdm (KDM const & kdm)
364 list<KDMKey> keys = kdm.keys ();
366 for (list<shared_ptr<CPL> >::iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
367 for (list<KDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
368 if (j->cpl_id() == (*i)->id()) {
376 DCP::add_assets_from (DCP const & ov)
378 copy (ov._asset_maps.begin(), ov._asset_maps.end(), back_inserter (_asset_maps));