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.
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <libxml++/libxml++.h>
34 #include <xmlsec/xmldsig.h>
35 #include <xmlsec/app.h>
38 #include "sound_asset.h"
39 #include "picture_asset.h"
40 #include "subtitle_asset.h"
43 #include "exceptions.h"
44 #include "parse/pkl.h"
45 #include "parse/asset_map.h"
48 #include "encryption.h"
53 using std::stringstream;
57 using std::back_inserter;
58 using boost::shared_ptr;
59 using boost::lexical_cast;
60 using namespace libdcp;
62 DCP::DCP (string directory)
63 : _directory (directory)
65 boost::filesystem::create_directories (directory);
69 DCP::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
71 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
72 (*i)->write_xml (interop, metadata, crypt);
75 string pkl_uuid = make_uuid ();
76 string pkl_path = write_pkl (pkl_uuid, interop, metadata, crypt);
79 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), interop, metadata);
83 DCP::write_pkl (string pkl_uuid, bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
85 assert (!_cpls.empty ());
87 boost::filesystem::path p;
90 s << pkl_uuid << "_pkl.xml";
96 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
98 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
102 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
105 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
106 /* XXX: this is a bit of a hack */
107 pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
108 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
109 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
110 pkl->add_child("Creator")->add_child_text (metadata.creator);
112 xmlpp::Element* asset_list = pkl->add_child("AssetList");
113 list<shared_ptr<const Asset> > a = assets ();
114 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
115 (*i)->write_to_pkl (asset_list);
118 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
119 (*i)->write_to_pkl (asset_list);
123 sign (pkl, crypt->certificates, crypt->signer_key, interop);
126 doc.write_to_file_formatted (p.string (), "UTF-8");
131 DCP::write_volindex () const
133 boost::filesystem::path p;
138 xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
139 root->add_child("Index")->add_child_text ("1");
140 doc.write_to_file_formatted (p.string (), "UTF-8");
144 DCP::write_assetmap (string pkl_uuid, int pkl_length, bool interop, XMLMetadata const & metadata) const
146 boost::filesystem::path p;
151 xmlpp::Element* root;
153 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
155 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
158 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
159 root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
161 root->add_child("VolumeCount")->add_child_text ("1");
162 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
163 root->add_child("Issuer")->add_child_text (metadata.issuer);
164 root->add_child("Creator")->add_child_text (metadata.creator);
166 root->add_child("Creator")->add_child_text (metadata.creator);
167 root->add_child("VolumeCount")->add_child_text ("1");
168 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
169 root->add_child("Issuer")->add_child_text (metadata.issuer);
172 xmlpp::Node* asset_list = root->add_child ("AssetList");
174 xmlpp::Node* asset = asset_list->add_child ("Asset");
175 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
176 asset->add_child("PackingList")->add_child_text ("true");
177 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
178 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
179 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
180 chunk->add_child("VolumeIndex")->add_child_text ("1");
181 chunk->add_child("Offset")->add_child_text ("0");
182 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
184 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
185 (*i)->write_to_assetmap (asset_list);
188 list<shared_ptr<const Asset> > a = assets ();
189 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
190 (*i)->write_to_assetmap (asset_list);
193 doc.write_to_file_formatted (p.string (), "UTF-8");
198 DCP::read (bool require_mxfs)
202 shared_ptr<parse::AssetMap> asset_map;
204 boost::filesystem::path p = _directory;
206 if (boost::filesystem::exists (p)) {
207 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
211 if (boost::filesystem::exists (p)) {
212 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
214 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
218 } catch (FileError& e) {
219 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
222 for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
223 if ((*i)->chunks.size() != 1) {
224 boost::throw_exception (XMLError ("unsupported asset chunk count"));
227 boost::filesystem::path t = _directory;
228 t /= (*i)->chunks.front()->path;
230 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
234 xmlpp::DomParser* p = new xmlpp::DomParser;
236 p->parse_file (t.string());
237 } catch (std::exception& e) {
242 string const root = p->get_document()->get_root_node()->get_name ();
245 if (root == "CompositionPlaylist") {
246 files.cpls.push_back (t.string());
247 } else if (root == "PackingList") {
248 if (files.pkl.empty ()) {
249 files.pkl = t.string();
251 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
256 if (files.cpls.empty ()) {
257 boost::throw_exception (FileError ("no CPL files found", ""));
260 if (files.pkl.empty ()) {
261 boost::throw_exception (FileError ("no PKL file found", ""));
264 shared_ptr<parse::PKL> pkl;
266 pkl.reset (new parse::PKL (files.pkl));
267 } catch (FileError& e) {
268 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
274 _asset_maps.push_back (asset_map);
276 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
277 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, _asset_maps, require_mxfs)));
282 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
284 if (_cpls.size() != other._cpls.size()) {
285 note (ERROR, "CPL counts differ");
289 list<shared_ptr<CPL> >::const_iterator a = _cpls.begin ();
290 list<shared_ptr<CPL> >::const_iterator b = other._cpls.begin ();
292 while (a != _cpls.end ()) {
293 if (!(*a)->equals (*b->get(), opt, note)) {
304 DCP::add_cpl (shared_ptr<CPL> cpl)
306 _cpls.push_back (cpl);
309 class AssetComparator
312 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
313 return a->uuid() < b->uuid();
317 list<shared_ptr<const Asset> >
320 list<shared_ptr<const Asset> > a;
321 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
322 list<shared_ptr<const Asset> > t = (*i)->assets ();
326 a.sort (AssetComparator ());
332 DCP::encrypted () const
334 for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
335 if ((*i)->encrypted ()) {
344 DCP::add_kdm (KDM const & kdm)
346 list<KDMCipher> ciphers = kdm.ciphers ();
348 for (list<shared_ptr<CPL> >::iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
349 for (list<KDMCipher>::iterator j = ciphers.begin(); j != ciphers.end(); ++j) {
350 if (j->cpl_id() == (*i)->id()) {
358 DCP::add_assets_from (DCP const & ov)
360 copy (ov._asset_maps.begin(), ov._asset_maps.end(), back_inserter (_asset_maps));