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 #include <libxml/parser.h>
23 #include "parse/cpl.h"
25 #include "picture_asset.h"
26 #include "sound_asset.h"
27 #include "subtitle_asset.h"
28 #include "parse/asset_map.h"
31 #include "encryption.h"
32 #include "exceptions.h"
33 #include "compose.hpp"
36 using std::stringstream;
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using namespace libdcp;
44 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
45 : _directory (directory)
47 , _content_kind (content_kind)
49 , _fps (frames_per_second)
54 /** Construct a CPL object from a XML file.
55 * @param directory The directory containing this CPL's DCP.
56 * @param file The CPL XML filename.
57 * @param asset_map The corresponding asset map.
58 * @param require_mxfs true to throw an exception if a required MXF file does not exist.
60 CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMap> asset_map, bool require_mxfs)
61 : _directory (directory)
62 , _content_kind (FEATURE)
67 shared_ptr<parse::CPL> cpl;
69 cpl.reset (new parse::CPL (file));
70 } catch (FileError& e) {
71 boost::throw_exception (FileError ("could not load CPL file", file));
74 /* Now cherry-pick the required bits into our own data structure */
76 _name = cpl->annotation_text;
77 _content_kind = cpl->content_kind;
79 /* Trim urn:uuid: off the front */
80 _id = cpl->id.substr (9);
82 for (list<shared_ptr<libdcp::parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
84 shared_ptr<parse::Picture> p;
86 if ((*i)->asset_list->main_picture) {
87 p = (*i)->asset_list->main_picture;
89 p = (*i)->asset_list->main_stereoscopic_picture;
92 _fps = p->edit_rate.numerator;
93 _length += p->duration;
95 shared_ptr<PictureAsset> picture;
96 shared_ptr<SoundAsset> sound;
97 shared_ptr<SubtitleAsset> subtitle;
99 /* Some rather twisted logic to decide if we are 3D or not;
100 some DCPs give a MainStereoscopicPicture to indicate 3D, others
101 just have a FrameRate twice the EditRate and apparently
102 expect you to divine the fact that they are hence 3D.
105 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
108 picture.reset (new MonoPictureAsset (
110 asset_map->asset_from_id (p->id)->chunks.front()->path
114 picture->set_entry_point (p->entry_point);
115 picture->set_duration (p->duration);
116 if (p->key_id.length() > 9) {
118 picture->set_key_id (p->key_id.substr (9));
120 } catch (MXFFileError) {
128 picture.reset (new StereoPictureAsset (
130 asset_map->asset_from_id (p->id)->chunks.front()->path,
136 picture->set_entry_point (p->entry_point);
137 picture->set_duration (p->duration);
138 if (p->key_id.length() > 9) {
140 picture->set_key_id (p->key_id.substr (9));
143 } catch (MXFFileError) {
151 if ((*i)->asset_list->main_sound) {
154 sound.reset (new SoundAsset (
156 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
160 shared_ptr<parse::MainSound> s = (*i)->asset_list->main_sound;
162 sound->set_entry_point (s->entry_point);
163 sound->set_duration (s->duration);
164 if (s->key_id.length() > 9) {
166 sound->set_key_id (s->key_id.substr (9));
168 } catch (MXFFileError) {
175 if ((*i)->asset_list->main_subtitle) {
177 subtitle.reset (new SubtitleAsset (
179 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
183 subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
184 subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
187 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
192 CPL::add_reel (shared_ptr<Reel> reel)
194 _reels.push_back (reel);
198 CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
200 boost::filesystem::path p;
203 s << _id << "_cpl.xml";
207 xmlpp::Element* root;
209 root = doc.create_root_node ("CompositionPlaylist", "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#");
211 root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
215 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
218 root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
219 root->add_child("AnnotationText")->add_child_text (_name);
220 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
221 root->add_child("Issuer")->add_child_text (metadata.issuer);
222 root->add_child("Creator")->add_child_text (metadata.creator);
223 root->add_child("ContentTitleText")->add_child_text (_name);
224 root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
226 xmlpp::Node* cv = root->add_child ("ContentVersion");
227 cv->add_child ("Id")->add_child_text ("urn:uri:" + _id + "_" + metadata.issue_date);
228 cv->add_child ("LabelText")->add_child_text (_id + "_" + metadata.issue_date);
230 root->add_child("RatingList");
232 xmlpp::Node* reel_list = root->add_child ("ReelList");
234 for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
235 (*i)->write_to_cpl (reel_list, interop);
239 sign (root, crypt->certificates, crypt->signer_key, interop);
242 doc.write_to_file_formatted (p.string (), "UTF-8");
244 _digest = make_digest (p.string ());
245 _length = boost::filesystem::file_size (p.string ());
249 CPL::write_to_pkl (xmlpp::Node* node) const
251 xmlpp::Node* asset = node->add_child ("Asset");
252 asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
253 asset->add_child("Hash")->add_child_text (_digest);
254 asset->add_child("Size")->add_child_text (lexical_cast<string> (_length));
255 asset->add_child("Type")->add_child_text ("text/xml");
258 list<shared_ptr<const Asset> >
261 list<shared_ptr<const Asset> > a;
262 for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
263 if ((*i)->main_picture ()) {
264 a.push_back ((*i)->main_picture ());
266 if ((*i)->main_sound ()) {
267 a.push_back ((*i)->main_sound ());
269 if ((*i)->main_subtitle ()) {
270 a.push_back ((*i)->main_subtitle ());
278 CPL::write_to_assetmap (xmlpp::Node* node) const
280 xmlpp::Node* asset = node->add_child ("Asset");
281 asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
282 xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
283 xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
284 chunk->add_child("Path")->add_child_text (_id + "_cpl.xml");
285 chunk->add_child("VolumeIndex")->add_child_text ("1");
286 chunk->add_child("Offset")->add_child_text("0");
287 chunk->add_child("Length")->add_child_text(lexical_cast<string> (_length));
293 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
295 if (_name != other._name && !opt.cpl_names_can_differ) {
297 s << "names differ: " << _name << " vs " << other._name << "\n";
298 note (ERROR, s.str ());
302 if (_content_kind != other._content_kind) {
303 note (ERROR, "content kinds differ");
307 if (_fps != other._fps) {
308 note (ERROR, String::compose ("frames per second differ (%1 vs %2)", _fps, other._fps));
312 if (_length != other._length) {
314 note (ERROR, String::compose ("lengths differ (%1 vs %2)", _length, other._length));
317 if (_reels.size() != other._reels.size()) {
318 note (ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
322 list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
323 list<shared_ptr<Reel> >::const_iterator b = other._reels.begin ();
325 while (a != _reels.end ()) {
326 if (!(*a)->equals (*b, opt, note)) {
336 shared_ptr<xmlpp::Document>
338 CertificateChain const & certificates,
339 string const & signer_key,
340 shared_ptr<const Certificate> recipient_cert,
341 boost::posix_time::ptime from,
342 boost::posix_time::ptime until,
344 MXFMetadata const & mxf_metadata,
345 XMLMetadata const & xml_metadata
348 assert (recipient_cert);
350 shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
351 xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
352 root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
353 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
354 root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
357 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
358 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
359 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
361 authenticated_public->add_child("MessageId")->add_child_text ("urn:uuid:" + make_uuid());
362 authenticated_public->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
363 authenticated_public->add_child("AnnotationText")->add_child_text (mxf_metadata.product_name);
364 authenticated_public->add_child("IssueDate")->add_child_text (xml_metadata.issue_date);
367 xmlpp::Element* signer = authenticated_public->add_child("Signer");
368 signer->add_child("X509IssuerName", "ds")->add_child_text (recipient_cert->issuer());
369 signer->add_child("X509SerialNumber", "ds")->add_child_text (recipient_cert->serial());
373 xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
376 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
377 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
379 xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
381 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
382 serial_element->add_child("X509IssuerName", "ds")->add_child_text (recipient_cert->issuer());
383 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (recipient_cert->serial());
386 recipient->add_child("X509SubjectName")->add_child_text (recipient_cert->subject());
389 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("urn:uuid:" + _id);
390 kdm_required_extensions->add_child("ContentTitleText")->add_child_text(_name);
391 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(certificates.leaf()->thumbprint());
392 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
393 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
396 xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
397 authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
398 authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
400 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
401 device_list->add_child("CertificateThumbprint")->add_child_text(recipient_cert->thumbprint());
406 xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
407 list<shared_ptr<const Asset> > a = assets();
408 for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
409 /* XXX: non-MXF assets? */
410 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
412 mxf->add_typed_key_id (key_id_list);
418 xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
419 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
420 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
422 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
423 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
429 authenticated_public->add_child("NonCriticalExtensions");
433 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
434 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
435 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
437 xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
439 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
440 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
441 encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
444 xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
445 cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
449 /* XXX: x2 one for each mxf? */
452 xmlpp::Element* signature = root->add_child("Signature", "ds");
455 xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
456 signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
457 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
461 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
462 "Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
465 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
466 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
471 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
472 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
473 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
474 reference->add_child("DigestValue", "ds");
478 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
479 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
480 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
481 reference->add_child("DigestValue", "ds");
485 add_signature_value (signature, certificates, signer_key, "ds");
491 /** @return true if we have any encrypted content */
493 CPL::encrypted () const
495 for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
496 if ((*i)->encrypted ()) {
505 CPL::add_kdm (KDM const & kdm)
507 for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {