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 <libxml++/libxml++.h>
31 #include <xmlsec/xmldsig.h>
32 #include <xmlsec/app.h>
35 #include "sound_asset.h"
36 #include "picture_asset.h"
37 #include "subtitle_asset.h"
40 #include "exceptions.h"
43 #include "asset_map.h"
48 using std::stringstream;
51 using boost::shared_ptr;
52 using namespace libdcp;
54 DCP::DCP (string directory)
55 : _directory (directory)
58 boost::filesystem::create_directories (directory);
62 DCP::write_xml () const
64 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
65 (*i)->write_xml (_encrypted, _certificates, _signer_key);
68 string pkl_uuid = make_uuid ();
69 string pkl_path = write_pkl (pkl_uuid);
72 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
76 DCP::write_pkl (string pkl_uuid) const
78 assert (!_cpls.empty ());
80 boost::filesystem::path p;
83 s << pkl_uuid << "_pkl.xml";
85 ofstream pkl (p.string().c_str());
87 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
88 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
89 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
90 /* XXX: this is a bit of a hack */
91 << " <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
92 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
93 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
94 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
97 list<shared_ptr<const Asset> > a = assets ();
98 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
99 (*i)->write_to_pkl (pkl);
102 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
103 (*i)->write_to_pkl (pkl);
106 pkl << " </AssetList>\n"
107 << "</PackingList>\n";
113 DCP::write_volindex () const
115 boost::filesystem::path p;
118 ofstream vi (p.string().c_str());
120 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
121 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
122 << " <Index>1</Index>\n"
123 << "</VolumeIndex>\n";
127 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
129 boost::filesystem::path p;
132 ofstream am (p.string().c_str());
134 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
135 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
136 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
137 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
138 << " <VolumeCount>1</VolumeCount>\n"
139 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
140 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
144 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
145 << " <PackingList>true</PackingList>\n"
148 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
149 << " <VolumeIndex>1</VolumeIndex>\n"
150 << " <Offset>0</Offset>\n"
151 << " <Length>" << pkl_length << "</Length>\n"
156 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
157 (*i)->write_to_assetmap (am);
160 list<shared_ptr<const Asset> > a = assets ();
161 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
162 (*i)->write_to_assetmap (am);
165 am << " </AssetList>\n"
171 DCP::read (bool require_mxfs)
175 shared_ptr<AssetMap> asset_map;
177 boost::filesystem::path p = _directory;
179 if (boost::filesystem::exists (p)) {
180 asset_map.reset (new AssetMap (p.string ()));
184 if (boost::filesystem::exists (p)) {
185 asset_map.reset (new AssetMap (p.string ()));
187 throw DCPReadError ("could not find AssetMap file");
191 } catch (FileError& e) {
192 throw FileError ("could not load AssetMap file", files.asset_map);
195 for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
196 if ((*i)->chunks.size() != 1) {
197 throw XMLError ("unsupported asset chunk count");
200 boost::filesystem::path t = _directory;
201 t /= (*i)->chunks.front()->path;
203 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
207 xmlpp::DomParser* p = new xmlpp::DomParser;
209 p->parse_file (t.string());
210 } catch (std::exception& e) {
215 string const root = p->get_document()->get_root_node()->get_name ();
218 if (root == "CompositionPlaylist") {
219 files.cpls.push_back (t.string());
220 } else if (root == "PackingList") {
221 if (files.pkl.empty ()) {
222 files.pkl = t.string();
224 throw DCPReadError ("duplicate PKLs found");
229 if (files.cpls.empty ()) {
230 throw FileError ("no CPL files found", "");
233 if (files.pkl.empty ()) {
234 throw FileError ("no PKL file found", "");
237 shared_ptr<PKLFile> pkl;
239 pkl.reset (new PKLFile (files.pkl));
240 } catch (FileError& e) {
241 throw FileError ("could not load PKL file", files.pkl);
247 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
248 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
253 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
255 if (_cpls.size() != other._cpls.size()) {
256 notes.push_back ("CPL counts differ");
260 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
261 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
263 while (a != _cpls.end ()) {
264 if (!(*a)->equals (*b->get(), opt, notes)) {
276 DCP::add_cpl (shared_ptr<CPL> cpl)
278 _cpls.push_back (cpl);
281 class AssetComparator
284 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
285 return a->uuid() < b->uuid();
289 list<shared_ptr<const Asset> >
292 list<shared_ptr<const Asset> > a;
293 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
294 list<shared_ptr<const Asset> > t = (*i)->assets ();
298 a.sort (AssetComparator ());
303 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
304 : _directory (directory)
306 , _content_kind (content_kind)
308 , _fps (frames_per_second)
310 _uuid = make_uuid ();
313 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
314 : _directory (directory)
315 , _content_kind (FEATURE)
320 shared_ptr<CPLFile> cpl;
322 cpl.reset (new CPLFile (file));
323 } catch (FileError& e) {
324 throw FileError ("could not load CPL file", file);
327 /* Now cherry-pick the required bits into our own data structure */
329 _name = cpl->annotation_text;
330 _content_kind = cpl->content_kind;
332 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
334 shared_ptr<Picture> p;
336 if ((*i)->asset_list->main_picture) {
337 p = (*i)->asset_list->main_picture;
339 p = (*i)->asset_list->main_stereoscopic_picture;
342 _fps = p->edit_rate.numerator;
343 _length += p->duration;
345 shared_ptr<PictureAsset> picture;
346 shared_ptr<SoundAsset> sound;
347 shared_ptr<SubtitleAsset> subtitle;
349 /* Some rather twisted logic to decide if we are 3D or not;
350 some DCPs give a MainStereoscopicPicture to indicate 3D, others
351 just have a FrameRate twice the EditRate and apparently
352 expect you to divine the fact that they are hence 3D.
355 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
358 picture.reset (new MonoPictureAsset (
360 asset_map->asset_from_id (p->id)->chunks.front()->path,
362 (*i)->asset_list->main_picture->entry_point,
363 (*i)->asset_list->main_picture->duration
366 } catch (MXFFileError) {
375 picture.reset (new StereoPictureAsset (
377 asset_map->asset_from_id (p->id)->chunks.front()->path,
383 } catch (MXFFileError) {
391 if ((*i)->asset_list->main_sound) {
394 sound.reset (new SoundAsset (
396 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
398 (*i)->asset_list->main_sound->entry_point,
399 (*i)->asset_list->main_sound->duration
402 } catch (MXFFileError) {
409 if ((*i)->asset_list->main_subtitle) {
411 subtitle.reset (new SubtitleAsset (
413 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
418 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
423 CPL::add_reel (shared_ptr<const Reel> reel)
425 _reels.push_back (reel);
429 CPL::write_xml (bool encrypted, CertificateChain const & certificates, string const & signer_key) const
431 boost::filesystem::path p;
434 s << _uuid << "_cpl.xml";
438 xmlpp::Element* cpl = doc.create_root_node("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
441 cpl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
444 cpl->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
445 cpl->add_child("AnnotationText")->add_child_text (_name);
446 cpl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
447 cpl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
448 cpl->add_child("ContentTitleText")->add_child_text (_name);
449 cpl->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
452 xmlpp::Element* cv = cpl->add_child ("ContentVersion");
453 cv->add_child("Id")->add_child_text ("urn:uri:" + _uuid + "_" + Metadata::instance()->issue_date);
454 cv->add_child("LabelText")->add_child_text (_uuid + "_" + Metadata::instance()->issue_date);
457 cpl->add_child("RatingList");
459 xmlpp::Element* reel_list = cpl->add_child("ReelList");
460 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
461 (*i)->write_to_cpl (reel_list);
465 xmlpp::Element* signer = cpl->add_child("Signer");
467 xmlpp::Element* data = signer->add_child("X509Data", "dsig");
469 xmlpp::Element* serial = data->add_child("X509IssuerSerial", "dsig");
470 serial->add_child("X509IssuerName", "dsig")->add_child_text (
471 Certificate::name_for_xml (certificates.leaf()->issuer())
473 serial->add_child("X509SerialNumber", "dsig")->add_child_text (
474 certificates.leaf()->serial()
477 data->add_child("X509SubjectName", "dsig")->add_child_text (
478 Certificate::name_for_xml (certificates.leaf()->subject())
482 xmlpp::Element* signature = cpl->add_child("Signature", "dsig");
485 xmlpp::Element* signed_info = signature->add_child ("SignedInfo", "dsig");
486 signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
487 signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
489 xmlpp::Element* reference = signed_info->add_child("Reference", "dsig");
490 reference->set_attribute ("URI", "");
492 xmlpp::Element* transforms = reference->add_child("Transforms", "dsig");
493 transforms->add_child("Transform", "dsig")->set_attribute (
494 "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
497 reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
498 /* This will be filled in by the signing later */
499 reference->add_child("DigestValue", "dsig");
503 signature->add_child("SignatureValue", "dsig");
505 xmlpp::Element* key_info = signature->add_child("KeyInfo", "dsig");
506 list<shared_ptr<Certificate> > c = certificates.leaf_to_root ();
507 for (list<shared_ptr<Certificate> >::iterator i = c.begin(); i != c.end(); ++i) {
508 xmlpp::Element* data = key_info->add_child("X509Data", "dsig");
510 xmlpp::Element* serial = data->add_child("X509IssuerSerial", "dsig");
511 serial->add_child("X509IssuerName", "dsig")->add_child_text(
512 Certificate::name_for_xml ((*i)->issuer())
514 serial->add_child("X509SerialNumber", "dsig")->add_child_text((*i)->serial());
518 xmlSecKeysMngrPtr keys_manager = xmlSecKeysMngrCreate();
520 throw MiscError ("could not create keys manager");
522 if (xmlSecCryptoAppDefaultKeysMngrInit (keys_manager) < 0) {
523 throw MiscError ("could not initialise keys manager");
526 xmlSecKeyPtr const key = xmlSecCryptoAppKeyLoad (signer_key.c_str(), xmlSecKeyDataFormatPem, 0, 0, 0);
528 throw MiscError ("could not load signer key");
531 if (xmlSecCryptoAppDefaultKeysMngrAdoptKey (keys_manager, key) < 0) {
532 xmlSecKeyDestroy (key);
533 throw MiscError ("could not use signer key");
536 xmlSecDSigCtx signature_context;
538 if (xmlSecDSigCtxInitialize (&signature_context, keys_manager) < 0) {
539 throw MiscError ("could not initialise XMLSEC context");
542 if (xmlSecDSigCtxSign (&signature_context, signature->cobj()) < 0) {
543 throw MiscError ("could not sign CPL");
546 xmlSecDSigCtxFinalize (&signature_context);
547 xmlSecKeysMngrDestroy (keys_manager);
550 doc.write_to_file_formatted (p.string(), "UTF-8");
552 _digest = make_digest (p.string (), 0);
553 _length = boost::filesystem::file_size (p.string ());
557 CPL::write_to_pkl (ostream& s) const
560 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
561 << " <Hash>" << _digest << "</Hash>\n"
562 << " <Size>" << _length << "</Size>\n"
563 << " <Type>text/xml</Type>\n"
567 list<shared_ptr<const Asset> >
570 list<shared_ptr<const Asset> > a;
571 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
572 if ((*i)->main_picture ()) {
573 a.push_back ((*i)->main_picture ());
575 if ((*i)->main_sound ()) {
576 a.push_back ((*i)->main_sound ());
578 if ((*i)->main_subtitle ()) {
579 a.push_back ((*i)->main_subtitle ());
587 CPL::write_to_assetmap (ostream& s) const
590 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
593 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
594 << " <VolumeIndex>1</VolumeIndex>\n"
595 << " <Offset>0</Offset>\n"
596 << " <Length>" << _length << "</Length>\n"
605 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
607 if (_name != other._name) {
608 notes.push_back ("names differ");
612 if (_content_kind != other._content_kind) {
613 notes.push_back ("content kinds differ");
617 if (_fps != other._fps) {
618 notes.push_back ("frames per second differ");
622 if (_length != other._length) {
623 notes.push_back ("lengths differ");
627 if (_reels.size() != other._reels.size()) {
628 notes.push_back ("reel counts differ");
632 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
633 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
635 while (a != _reels.end ()) {
636 if (!(*a)->equals (*b, opt, notes)) {