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 <libxml++/libxml++.h>
32 #include <xmlsec/xmldsig.h>
33 #include <xmlsec/app.h>
36 #include "sound_asset.h"
37 #include "picture_asset.h"
38 #include "subtitle_asset.h"
41 #include "exceptions.h"
44 #include "asset_map.h"
49 using std::stringstream;
52 using boost::shared_ptr;
53 using namespace libdcp;
55 DCP::DCP (string directory)
56 : _directory (directory)
59 boost::filesystem::create_directories (directory);
63 DCP::write_xml () const
65 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
66 (*i)->write_xml (_encrypted, _certificates, _signer_key);
69 string pkl_uuid = make_uuid ();
70 string pkl_path = write_pkl (pkl_uuid);
73 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
77 DCP::write_pkl (string pkl_uuid) const
79 assert (!_cpls.empty ());
81 boost::filesystem::path p;
84 s << pkl_uuid << "_pkl.xml";
88 xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
90 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
93 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
94 /* XXX: this is a bit of a hack */
95 pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
96 pkl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
97 pkl->add_child("Issuer")->add_child_text (Metadata::instance()->issuer);
98 pkl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
101 xmlpp::Element* asset_list = pkl->add_child("AssetList");
102 list<shared_ptr<const Asset> > a = assets ();
103 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
104 (*i)->write_to_pkl (asset_list);
107 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
108 (*i)->write_to_pkl (asset_list);
113 sign (pkl, _certificates, _signer_key);
116 doc.write_to_file_formatted (p.string(), "UTF-8");
122 DCP::write_volindex () const
124 boost::filesystem::path p;
127 ofstream vi (p.string().c_str());
129 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
130 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
131 << " <Index>1</Index>\n"
132 << "</VolumeIndex>\n";
136 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
138 boost::filesystem::path p;
141 ofstream am (p.string().c_str());
143 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
144 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
145 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
146 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
147 << " <VolumeCount>1</VolumeCount>\n"
148 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
149 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
153 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
154 << " <PackingList>true</PackingList>\n"
157 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
158 << " <VolumeIndex>1</VolumeIndex>\n"
159 << " <Offset>0</Offset>\n"
160 << " <Length>" << pkl_length << "</Length>\n"
165 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
166 (*i)->write_to_assetmap (am);
169 list<shared_ptr<const Asset> > a = assets ();
170 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
171 (*i)->write_to_assetmap (am);
174 am << " </AssetList>\n"
180 DCP::read (bool require_mxfs)
184 shared_ptr<AssetMap> asset_map;
186 boost::filesystem::path p = _directory;
188 if (boost::filesystem::exists (p)) {
189 asset_map.reset (new AssetMap (p.string ()));
193 if (boost::filesystem::exists (p)) {
194 asset_map.reset (new AssetMap (p.string ()));
196 throw DCPReadError ("could not find AssetMap file");
200 } catch (FileError& e) {
201 throw FileError ("could not load AssetMap file", files.asset_map);
204 for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
205 if ((*i)->chunks.size() != 1) {
206 throw XMLError ("unsupported asset chunk count");
209 boost::filesystem::path t = _directory;
210 t /= (*i)->chunks.front()->path;
212 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
216 xmlpp::DomParser* p = new xmlpp::DomParser;
218 p->parse_file (t.string());
219 } catch (std::exception& e) {
224 string const root = p->get_document()->get_root_node()->get_name ();
227 if (root == "CompositionPlaylist") {
228 files.cpls.push_back (t.string());
229 } else if (root == "PackingList") {
230 if (files.pkl.empty ()) {
231 files.pkl = t.string();
233 throw DCPReadError ("duplicate PKLs found");
238 if (files.cpls.empty ()) {
239 throw FileError ("no CPL files found", "");
242 if (files.pkl.empty ()) {
243 throw FileError ("no PKL file found", "");
246 shared_ptr<PKLFile> pkl;
248 pkl.reset (new PKLFile (files.pkl));
249 } catch (FileError& e) {
250 throw FileError ("could not load PKL file", files.pkl);
256 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
257 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
262 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
264 if (_cpls.size() != other._cpls.size()) {
265 notes.push_back ("CPL counts differ");
269 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
270 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
272 while (a != _cpls.end ()) {
273 if (!(*a)->equals (*b->get(), opt, notes)) {
285 DCP::add_cpl (shared_ptr<CPL> cpl)
287 _cpls.push_back (cpl);
290 class AssetComparator
293 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
294 return a->uuid() < b->uuid();
298 list<shared_ptr<const Asset> >
301 list<shared_ptr<const Asset> > a;
302 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
303 list<shared_ptr<const Asset> > t = (*i)->assets ();
307 a.sort (AssetComparator ());
312 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
313 : _directory (directory)
315 , _content_kind (content_kind)
317 , _fps (frames_per_second)
319 _uuid = make_uuid ();
322 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
323 : _directory (directory)
324 , _content_kind (FEATURE)
329 shared_ptr<CPLFile> cpl;
331 cpl.reset (new CPLFile (file));
332 } catch (FileError& e) {
333 throw FileError ("could not load CPL file", file);
336 /* Now cherry-pick the required bits into our own data structure */
338 _name = cpl->annotation_text;
339 _content_kind = cpl->content_kind;
341 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
343 shared_ptr<Picture> p;
345 if ((*i)->asset_list->main_picture) {
346 p = (*i)->asset_list->main_picture;
348 p = (*i)->asset_list->main_stereoscopic_picture;
351 _fps = p->edit_rate.numerator;
352 _length += p->duration;
354 shared_ptr<PictureAsset> picture;
355 shared_ptr<SoundAsset> sound;
356 shared_ptr<SubtitleAsset> subtitle;
358 /* Some rather twisted logic to decide if we are 3D or not;
359 some DCPs give a MainStereoscopicPicture to indicate 3D, others
360 just have a FrameRate twice the EditRate and apparently
361 expect you to divine the fact that they are hence 3D.
364 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
367 picture.reset (new MonoPictureAsset (
369 asset_map->asset_from_id (p->id)->chunks.front()->path,
371 (*i)->asset_list->main_picture->entry_point,
372 (*i)->asset_list->main_picture->duration
375 } catch (MXFFileError) {
384 picture.reset (new StereoPictureAsset (
386 asset_map->asset_from_id (p->id)->chunks.front()->path,
392 } catch (MXFFileError) {
400 if ((*i)->asset_list->main_sound) {
403 sound.reset (new SoundAsset (
405 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
407 (*i)->asset_list->main_sound->entry_point,
408 (*i)->asset_list->main_sound->duration
411 } catch (MXFFileError) {
418 if ((*i)->asset_list->main_subtitle) {
420 subtitle.reset (new SubtitleAsset (
422 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
427 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
432 CPL::add_reel (shared_ptr<const Reel> reel)
434 _reels.push_back (reel);
438 CPL::write_xml (bool encrypted, CertificateChain const & certificates, string const & signer_key) const
440 boost::filesystem::path p;
443 s << _uuid << "_cpl.xml";
447 xmlpp::Element* cpl = doc.create_root_node("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
450 cpl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
453 cpl->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
454 cpl->add_child("AnnotationText")->add_child_text (_name);
455 cpl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
456 cpl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
457 cpl->add_child("ContentTitleText")->add_child_text (_name);
458 cpl->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
461 xmlpp::Element* cv = cpl->add_child ("ContentVersion");
462 cv->add_child("Id")->add_child_text ("urn:uri:" + _uuid + "_" + Metadata::instance()->issue_date);
463 cv->add_child("LabelText")->add_child_text (_uuid + "_" + Metadata::instance()->issue_date);
466 cpl->add_child("RatingList");
468 xmlpp::Element* reel_list = cpl->add_child("ReelList");
469 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
470 (*i)->write_to_cpl (reel_list);
474 DCP::sign (cpl, certificates, signer_key);
477 doc.write_to_file_formatted (p.string(), "UTF-8");
479 _digest = make_digest (p.string (), 0);
480 _length = boost::filesystem::file_size (p.string ());
484 DCP::sign (xmlpp::Element* parent, CertificateChain const & certificates, string const & signer_key)
486 xmlpp::Element* signer = parent->add_child("Signer");
489 xmlpp::Element* data = signer->add_child("X509Data", "dsig");
491 xmlpp::Element* serial = data->add_child("X509IssuerSerial", "dsig");
492 serial->add_child("X509IssuerName", "dsig")->add_child_text (
493 Certificate::name_for_xml (certificates.leaf()->issuer())
495 serial->add_child("X509SerialNumber", "dsig")->add_child_text (
496 certificates.leaf()->serial()
499 data->add_child("X509SubjectName", "dsig")->add_child_text (
500 Certificate::name_for_xml (certificates.leaf()->subject())
504 xmlpp::Element* signature = parent->add_child("Signature", "dsig");
507 xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
508 signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
509 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
511 xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
512 reference->set_attribute ("URI", "");
514 xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
515 transforms->add_child("Transform", "dsig")->set_attribute (
516 "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
519 reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
520 /* This will be filled in by the signing later */
521 reference->add_child("DigestValue", "dsig");
525 signature->add_child("SignatureValue", "dsig");
527 xmlpp::Element* key_info = signature->add_child("KeyInfo", "dsig");
528 list<shared_ptr<Certificate> > c = certificates.leaf_to_root ();
529 for (list<shared_ptr<Certificate> >::iterator i = c.begin(); i != c.end(); ++i) {
530 xmlpp::Element* data = key_info->add_child("X509Data", "dsig");
533 xmlpp::Element* serial = data->add_child("X509IssuerSerial", "dsig");
534 serial->add_child("X509IssuerName", "dsig")->add_child_text(
535 Certificate::name_for_xml ((*i)->issuer())
537 serial->add_child("X509SerialNumber", "dsig")->add_child_text((*i)->serial());
540 data->add_child("X509Certificate", "dsig")->add_child_text((*i)->certificate());
543 xmlSecKeysMngrPtr keys_manager = xmlSecKeysMngrCreate();
545 throw MiscError ("could not create keys manager");
547 if (xmlSecCryptoAppDefaultKeysMngrInit (keys_manager) < 0) {
548 throw MiscError ("could not initialise keys manager");
551 xmlSecKeyPtr const key = xmlSecCryptoAppKeyLoad (signer_key.c_str(), xmlSecKeyDataFormatPem, 0, 0, 0);
553 throw MiscError ("could not load signer key");
556 if (xmlSecCryptoAppDefaultKeysMngrAdoptKey (keys_manager, key) < 0) {
557 xmlSecKeyDestroy (key);
558 throw MiscError ("could not use signer key");
561 xmlSecDSigCtx signature_context;
563 if (xmlSecDSigCtxInitialize (&signature_context, keys_manager) < 0) {
564 throw MiscError ("could not initialise XMLSEC context");
567 if (xmlSecDSigCtxSign (&signature_context, signature->cobj()) < 0) {
568 throw MiscError ("could not sign CPL");
571 xmlSecDSigCtxFinalize (&signature_context);
572 xmlSecKeysMngrDestroy (keys_manager);
576 CPL::write_to_pkl (xmlpp::Element* p) const
578 xmlpp::Element* asset = p->add_child("Asset");
579 asset->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
580 asset->add_child("Hash")->add_child_text(_digest);
581 asset->add_child("Size")->add_child_text(boost::lexical_cast<string> (_length));
582 asset->add_child("Type")->add_child_text("text/xml");
585 list<shared_ptr<const Asset> >
588 list<shared_ptr<const Asset> > a;
589 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
590 if ((*i)->main_picture ()) {
591 a.push_back ((*i)->main_picture ());
593 if ((*i)->main_sound ()) {
594 a.push_back ((*i)->main_sound ());
596 if ((*i)->main_subtitle ()) {
597 a.push_back ((*i)->main_subtitle ());
605 CPL::write_to_assetmap (ostream& s) const
608 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
611 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
612 << " <VolumeIndex>1</VolumeIndex>\n"
613 << " <Offset>0</Offset>\n"
614 << " <Length>" << _length << "</Length>\n"
623 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
625 if (_name != other._name) {
626 notes.push_back ("names differ");
630 if (_content_kind != other._content_kind) {
631 notes.push_back ("content kinds differ");
635 if (_fps != other._fps) {
636 notes.push_back ("frames per second differ");
640 if (_length != other._length) {
641 notes.push_back ("lengths differ");
645 if (_reels.size() != other._reels.size()) {
646 notes.push_back ("reel counts differ");
650 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
651 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
653 while (a != _reels.end ()) {
654 if (!(*a)->equals (*b, opt, notes)) {