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 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 CPL::write_to_pkl (xmlpp::Element* p) const
486 xmlpp::Element* asset = p->add_child("Asset");
487 asset->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
488 asset->add_child("Hash")->add_child_text(_digest);
489 asset->add_child("Size")->add_child_text(boost::lexical_cast<string> (_length));
490 asset->add_child("Type")->add_child_text("text/xml");
493 list<shared_ptr<const Asset> >
496 list<shared_ptr<const Asset> > a;
497 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
498 if ((*i)->main_picture ()) {
499 a.push_back ((*i)->main_picture ());
501 if ((*i)->main_sound ()) {
502 a.push_back ((*i)->main_sound ());
504 if ((*i)->main_subtitle ()) {
505 a.push_back ((*i)->main_subtitle ());
513 CPL::write_to_assetmap (ostream& s) const
516 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
519 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
520 << " <VolumeIndex>1</VolumeIndex>\n"
521 << " <Offset>0</Offset>\n"
522 << " <Length>" << _length << "</Length>\n"
531 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
533 if (_name != other._name) {
534 notes.push_back ("names differ");
538 if (_content_kind != other._content_kind) {
539 notes.push_back ("content kinds differ");
543 if (_fps != other._fps) {
544 notes.push_back ("frames per second differ");
548 if (_length != other._length) {
549 notes.push_back ("lengths differ");
553 if (_reels.size() != other._reels.size()) {
554 notes.push_back ("reel counts differ");
558 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
559 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
561 while (a != _reels.end ()) {
562 if (!(*a)->equals (*b, opt, notes)) {
572 shared_ptr<xmlpp::Document>
573 CPL::make_kdm (CertificateChain const & certificates, string const & signer_key, shared_ptr<const Certificate> recipient_cert) const
575 shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
576 xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
577 root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
578 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
579 root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
582 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
583 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
584 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
586 authenticated_public->add_child("MessageId")->add_child_text("urn:uuid:" + make_uuid());
587 authenticated_public->add_child("MessageType")->add_child_text("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
588 authenticated_public->add_child("AnnotationText")->add_child_text(Metadata::instance()->product_name);
589 authenticated_public->add_child("IssueDate")->add_child_text(Metadata::instance()->issue_date);
592 xmlpp::Element* signer = authenticated_public->add_child("Signer");
593 signer->add_child("X509IssuerName", "ds")->add_child_text (
594 Certificate::name_for_xml (recipient_cert->issuer())
596 signer->add_child("X509SerialNumber", "ds")->add_child_text (
597 recipient_cert->serial()
602 xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
605 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
606 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
608 xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
610 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
611 serial_element->add_child("X509IssuerName", "ds")->add_child_text (
612 Certificate::name_for_xml (recipient_cert->issuer())
614 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (
615 recipient_cert->serial()
619 recipient->add_child("X509SubjectName")->add_child_text (Certificate::name_for_xml (recipient_cert->subject()));
622 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("XXX");
623 kdm_required_extensions->add_child("ContentTitleText")->add_child_text("XXX");
624 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text("XXX");
625 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
626 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
629 xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
630 authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
631 authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
633 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
634 device_list->add_child("CertificateThumbprint")->add_child_text("XXX");
639 xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
640 list<shared_ptr<const Asset> > a = assets();
641 for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
642 /* XXX: non-MXF assets? */
643 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
645 mxf->add_typed_key_id (key_id_list);
651 xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
652 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
653 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
655 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
656 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
662 authenticated_public->add_child("NonCriticalExtensions");
666 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
667 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
668 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
670 xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
672 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
673 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
674 encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
677 xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
678 cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
682 /* XXX: x2 one for each mxf? */
685 xmlpp::Element* signature = root->add_child("Signature", "ds");
688 xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
689 signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
690 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
692 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
693 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
696 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
697 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
698 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
699 reference->add_child("DigestValue", "ds");
703 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
704 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
705 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
706 reference->add_child("DigestValue", "ds");
710 add_signature_value (signature, certificates, signer_key, "ds");