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"
52 using std::stringstream;
55 using boost::shared_ptr;
56 using boost::lexical_cast;
57 using namespace libdcp;
59 DCP::DCP (string directory)
60 : _directory (directory)
62 boost::filesystem::create_directories (directory);
66 DCP::write_xml (XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
68 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
69 (*i)->write_xml (metadata, crypt);
72 string pkl_uuid = make_uuid ();
73 string pkl_path = write_pkl (pkl_uuid, metadata, crypt);
76 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
80 DCP::write_pkl (string pkl_uuid, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
82 assert (!_cpls.empty ());
84 boost::filesystem::path p;
87 s << pkl_uuid << "_pkl.xml";
91 xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
93 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
96 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
97 /* XXX: this is a bit of a hack */
98 pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
99 pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
100 pkl->add_child("Issuer")->add_child_text (metadata.issuer);
101 pkl->add_child("Creator")->add_child_text (metadata.creator);
103 xmlpp::Element* asset_list = pkl->add_child("AssetList");
104 list<shared_ptr<const Asset> > a = assets ();
105 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
106 (*i)->write_to_pkl (asset_list);
109 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
110 (*i)->write_to_pkl (asset_list);
114 sign (pkl, crypt->certificates, crypt->signer_key);
117 doc.write_to_file_formatted (p.string (), "UTF-8");
122 DCP::write_volindex () const
124 boost::filesystem::path p;
129 xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
130 root->add_child("Index")->add_child_text ("1");
131 doc.write_to_file_formatted (p.string (), "UTF-8");
135 DCP::write_assetmap (string pkl_uuid, int pkl_length, XMLMetadata const & metadata) const
137 boost::filesystem::path p;
142 xmlpp::Element* root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
144 root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
145 root->add_child("Creator")->add_child_text (metadata.creator);
146 root->add_child("VolumeCount")->add_child_text ("1");
147 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
148 root->add_child("Issuer")->add_child_text (metadata.issuer);
149 xmlpp::Node* asset_list = root->add_child ("AssetList");
151 xmlpp::Node* asset = asset_list->add_child ("Asset");
152 asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
153 asset->add_child("PackingList")->add_child_text ("true");
154 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
155 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
156 chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
157 chunk->add_child("VolumeIndex")->add_child_text ("1");
158 chunk->add_child("Offset")->add_child_text ("0");
159 chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
161 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
162 (*i)->write_to_assetmap (asset_list);
165 list<shared_ptr<const Asset> > a = assets ();
166 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
167 (*i)->write_to_assetmap (asset_list);
170 doc.write_to_file_formatted (p.string (), "UTF-8");
175 DCP::read (bool require_mxfs)
179 shared_ptr<parse::AssetMap> asset_map;
181 boost::filesystem::path p = _directory;
183 if (boost::filesystem::exists (p)) {
184 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
188 if (boost::filesystem::exists (p)) {
189 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
191 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
195 } catch (FileError& e) {
196 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
199 for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
200 if ((*i)->chunks.size() != 1) {
201 boost::throw_exception (XMLError ("unsupported asset chunk count"));
204 boost::filesystem::path t = _directory;
205 t /= (*i)->chunks.front()->path;
207 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
211 xmlpp::DomParser* p = new xmlpp::DomParser;
213 p->parse_file (t.string());
214 } catch (std::exception& e) {
219 string const root = p->get_document()->get_root_node()->get_name ();
222 if (root == "CompositionPlaylist") {
223 files.cpls.push_back (t.string());
224 } else if (root == "PackingList") {
225 if (files.pkl.empty ()) {
226 files.pkl = t.string();
228 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
233 if (files.cpls.empty ()) {
234 boost::throw_exception (FileError ("no CPL files found", ""));
237 if (files.pkl.empty ()) {
238 boost::throw_exception (FileError ("no PKL file found", ""));
241 shared_ptr<parse::PKL> pkl;
243 pkl.reset (new parse::PKL (files.pkl));
244 } catch (FileError& e) {
245 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
251 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
252 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
257 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
259 if (_cpls.size() != other._cpls.size()) {
260 note (ERROR, "CPL counts differ");
264 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
265 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
267 while (a != _cpls.end ()) {
268 if (!(*a)->equals (*b->get(), opt, note)) {
279 DCP::add_cpl (shared_ptr<CPL> cpl)
281 _cpls.push_back (cpl);
284 class AssetComparator
287 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
288 return a->uuid() < b->uuid();
292 list<shared_ptr<const Asset> >
295 list<shared_ptr<const Asset> > a;
296 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
297 list<shared_ptr<const Asset> > t = (*i)->assets ();
301 a.sort (AssetComparator ());