Merge master; at least partially.
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <fstream>
21 #include "cpl.h"
22 #include "parse/cpl.h"
23 #include "util.h"
24 #include "picture_asset.h"
25 #include "sound_asset.h"
26 #include "subtitle_asset.h"
27 #include "parse/asset_map.h"
28 #include "reel.h"
29 #include "metadata.h"
30 #include "encryption.h"
31 #include "exceptions.h"
32 #include "compose.hpp"
33
34 using std::string;
35 using std::stringstream;
36 using std::ofstream;
37 using std::ostream;
38 using std::list;
39 using boost::shared_ptr;
40 using boost::lexical_cast;
41 using namespace libdcp;
42
43 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
44         : _directory (directory)
45         , _name (name)
46         , _content_kind (content_kind)
47         , _length (length)
48         , _fps (frames_per_second)
49 {
50         _uuid = make_uuid ();
51 }
52
53 /** Construct a CPL object from a XML file.
54  *  @param directory The directory containing this CPL's DCP.
55  *  @param file The CPL XML filename.
56  *  @param asset_map The corresponding asset map.
57  *  @param require_mxfs true to throw an exception if a required MXF file does not exist.
58  */
59 CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMap> asset_map, bool require_mxfs)
60         : _directory (directory)
61         , _content_kind (FEATURE)
62         , _length (0)
63         , _fps (0)
64 {
65         /* Read the XML */
66         shared_ptr<parse::CPL> cpl;
67         try {
68                 cpl.reset (new parse::CPL (file));
69         } catch (FileError& e) {
70                 boost::throw_exception (FileError ("could not load CPL file", file));
71         }
72         
73         /* Now cherry-pick the required bits into our own data structure */
74         
75         _name = cpl->annotation_text;
76         _content_kind = cpl->content_kind;
77
78         for (list<shared_ptr<libdcp::parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
79
80                 shared_ptr<parse::Picture> p;
81
82                 if ((*i)->asset_list->main_picture) {
83                         p = (*i)->asset_list->main_picture;
84                 } else {
85                         p = (*i)->asset_list->main_stereoscopic_picture;
86                 }
87                 
88                 _fps = p->edit_rate.numerator;
89                 _length += p->duration;
90
91                 shared_ptr<PictureAsset> picture;
92                 shared_ptr<SoundAsset> sound;
93                 shared_ptr<SubtitleAsset> subtitle;
94
95                 /* Some rather twisted logic to decide if we are 3D or not;
96                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
97                    just have a FrameRate twice the EditRate and apparently
98                    expect you to divine the fact that they are hence 3D.
99                 */
100
101                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
102
103                         try {
104                                 picture.reset (new MonoPictureAsset (
105                                                        _directory,
106                                                        asset_map->asset_from_id (p->id)->chunks.front()->path
107                                                        )
108                                         );
109
110                                 picture->set_entry_point (p->entry_point);
111                                 picture->set_duration (p->duration);
112                         } catch (MXFFileError) {
113                                 if (require_mxfs) {
114                                         throw;
115                                 }
116                         }
117                         
118                 } else {
119                         try {
120                                 picture.reset (new StereoPictureAsset (
121                                                        _directory,
122                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
123                                                        _fps,
124                                                        p->duration
125                                                        )
126                                         );
127
128                                 picture->set_entry_point (p->entry_point);
129                                 picture->set_duration (p->duration);
130                                 
131                         } catch (MXFFileError) {
132                                 if (require_mxfs) {
133                                         throw;
134                                 }
135                         }
136                         
137                 }
138                 
139                 if ((*i)->asset_list->main_sound) {
140                         
141                         try {
142                                 sound.reset (new SoundAsset (
143                                                      _directory,
144                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
145                                                      )
146                                         );
147
148                                 sound->set_entry_point ((*i)->asset_list->main_sound->entry_point);
149                                 sound->set_duration ((*i)->asset_list->main_sound->duration);
150                         } catch (MXFFileError) {
151                                 if (require_mxfs) {
152                                         throw;
153                                 }
154                         }
155                 }
156
157                 if ((*i)->asset_list->main_subtitle) {
158                         
159                         subtitle.reset (new SubtitleAsset (
160                                                 _directory,
161                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
162                                                 )
163                                 );
164
165                         subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
166                         subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
167                 }
168                         
169                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
170         }
171 }
172
173 void
174 CPL::add_reel (shared_ptr<const Reel> reel)
175 {
176         _reels.push_back (reel);
177 }
178
179 void
180 CPL::write_xml (XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
181 {
182         boost::filesystem::path p;
183         p /= _directory;
184         stringstream s;
185         s << _uuid << "_cpl.xml";
186         p /= s.str();
187
188         xmlpp::Document doc;
189         xmlpp::Element* root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
190
191         if (crypt) {
192                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
193         }
194         
195         root->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
196         root->add_child("AnnotationText")->add_child_text (_name);
197         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
198         root->add_child("Creator")->add_child_text (metadata.creator);
199         root->add_child("ContentTitleText")->add_child_text (_name);
200         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
201         {
202                 xmlpp::Node* cv = root->add_child ("ContentVersion");
203                 cv->add_child ("Id")->add_child_text ("urn:uri:" + _uuid + "_" + metadata.issue_date);
204                 cv->add_child ("LabelText")->add_child_text (_uuid + "_" + metadata.issue_date);
205         }
206         root->add_child("RatingList");
207
208         xmlpp::Node* reel_list = root->add_child ("ReelList");
209         
210         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
211                 (*i)->write_to_cpl (reel_list);
212         }
213
214         if (crypt) {
215                 sign (root, crypt->certificates, crypt->signer_key);
216         }
217
218         doc.write_to_file_formatted (p.string (), "UTF-8");
219
220         _digest = make_digest (p.string ());
221         _length = boost::filesystem::file_size (p.string ());
222 }
223
224 void
225 CPL::write_to_pkl (xmlpp::Node* node) const
226 {
227         xmlpp::Node* asset = node->add_child ("Asset");
228         asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
229         asset->add_child("Hash")->add_child_text (_digest);
230         asset->add_child("Size")->add_child_text (lexical_cast<string> (_length));
231         asset->add_child("Type")->add_child_text ("text/xml");
232 }
233
234 list<shared_ptr<const Asset> >
235 CPL::assets () const
236 {
237         list<shared_ptr<const Asset> > a;
238         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
239                 if ((*i)->main_picture ()) {
240                         a.push_back ((*i)->main_picture ());
241                 }
242                 if ((*i)->main_sound ()) {
243                         a.push_back ((*i)->main_sound ());
244                 }
245                 if ((*i)->main_subtitle ()) {
246                         a.push_back ((*i)->main_subtitle ());
247                 }
248         }
249
250         return a;
251 }
252
253 void
254 CPL::write_to_assetmap (xmlpp::Node* node) const
255 {
256         xmlpp::Node* asset = node->add_child ("Asset");
257         asset->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
258         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
259         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
260         chunk->add_child("Path")->add_child_text (_uuid + "_cpl.xml");
261         chunk->add_child("VolumeIndex")->add_child_text ("1");
262         chunk->add_child("Offset")->add_child_text("0");
263         chunk->add_child("Length")->add_child_text(lexical_cast<string> (_length));
264 }
265         
266         
267         
268 bool
269 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
270 {
271         if (_name != other._name && !opt.cpl_names_can_differ) {
272                 stringstream s;
273                 s << "names differ: " << _name << " vs " << other._name << "\n";
274                 note (ERROR, s.str ());
275                 return false;
276         }
277
278         if (_content_kind != other._content_kind) {
279                 note (ERROR, "content kinds differ");
280                 return false;
281         }
282
283         if (_fps != other._fps) {
284                 note (ERROR, String::compose ("frames per second differ (%1 vs %2)", _fps, other._fps));
285                 return false;
286         }
287
288         if (_length != other._length) {
289                 stringstream s;
290                 s << "lengths differ (" << _length << " cf " << other._length << ")";
291                 note (ERROR, String::compose ("lengths differ (%1 vs %2)", _length, other._length));
292                 return false;
293         }
294
295         if (_reels.size() != other._reels.size()) {
296                 note (ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
297                 return false;
298         }
299         
300         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
301         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
302         
303         while (a != _reels.end ()) {
304                 if (!(*a)->equals (*b, opt, note)) {
305                         return false;
306                 }
307                 ++a;
308                 ++b;
309         }
310
311         return true;
312 }
313
314 shared_ptr<xmlpp::Document>
315 CPL::make_kdm (
316         CertificateChain const & certificates,
317         string const & signer_key,
318         shared_ptr<const Certificate> recipient_cert,
319         boost::posix_time::ptime from,
320         boost::posix_time::ptime until
321         ) const
322 {
323         assert (recipient_cert);
324         
325         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
326         xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
327         root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
328         root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
329         root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
330
331         {
332                 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
333                 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
334                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
335                 
336                 authenticated_public->add_child("MessageId")->add_child_text ("urn:uuid:" + make_uuid());
337                 authenticated_public->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
338                 authenticated_public->add_child("AnnotationText")->add_child_text (MXFMetadata::instance()->product_name);
339                 authenticated_public->add_child("IssueDate")->add_child_text (MXFMetadata::instance()->issue_date);
340
341                 {
342                         xmlpp::Element* signer = authenticated_public->add_child("Signer");
343                         signer->add_child("X509IssuerName", "ds")->add_child_text (
344                                 Certificate::name_for_xml (recipient_cert->issuer())
345                                 );
346                         signer->add_child("X509SerialNumber", "ds")->add_child_text (
347                                 recipient_cert->serial()
348                                 );
349                 }
350
351                 {
352                         xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
353
354                         {
355                                 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
356                                 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
357                                 {
358                                         xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
359                                         {
360                                                 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
361                                                 serial_element->add_child("X509IssuerName", "ds")->add_child_text (
362                                                         Certificate::name_for_xml (recipient_cert->issuer())
363                                                         );
364                                                 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (
365                                                         recipient_cert->serial()
366                                                         );
367                                         }
368
369                                         recipient->add_child("X509SubjectName")->add_child_text (Certificate::name_for_xml (recipient_cert->subject()));
370                                 }
371
372                                 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("urn:uuid:" + _uuid);
373                                 kdm_required_extensions->add_child("ContentTitleText")->add_child_text(_name);
374                                 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(certificates.leaf()->thumbprint());
375                                 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
376                                 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
377
378                                 {
379                                         xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
380                                         authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
381                                         authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
382                                         {
383                                                 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
384                                                 device_list->add_child("CertificateThumbprint")->add_child_text(recipient_cert->thumbprint());
385                                         }
386                                 }
387
388                                 {
389                                         xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
390                                         list<shared_ptr<const Asset> > a = assets();
391                                         for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
392                                                 /* XXX: non-MXF assets? */
393                                                 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
394                                                 if (mxf) {
395                                                         mxf->add_typed_key_id (key_id_list);
396                                                 }
397                                         }
398                                 }
399
400                                 {
401                                         xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
402                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
403                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
404                                                 );
405                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
406                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
407                                                 );
408                                 }
409                         }
410                 }
411                                          
412                 authenticated_public->add_child("NonCriticalExtensions");
413         }
414
415         {
416                 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
417                 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
418                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
419                 {
420                         xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
421                         {
422                                 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
423                                 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
424                                 encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
425                         }
426
427                         xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
428                         cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
429                 }
430         }
431         
432         /* XXX: x2 one for each mxf? */
433
434         {
435                 xmlpp::Element* signature = root->add_child("Signature", "ds");
436                 
437                 {
438                         xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
439                         signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
440                                 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
441                                 );
442                         signed_info->add_child("SignatureMethod", "ds")->set_attribute(
443                                 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
444                                 );
445                         {
446                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
447                                 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
448                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
449                                 reference->add_child("DigestValue", "ds");
450                         }
451                         
452                         {                               
453                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
454                                 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
455                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
456                                 reference->add_child("DigestValue", "ds");
457                         }
458                 }
459                 
460                 add_signature_value (signature, certificates, signer_key, "ds");
461         }
462
463         return doc;
464 }