A few more untested bits.
[libdcp.git] / src / dcp.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 /** @file  src/dcp.cc
21  *  @brief A class to create a DCP.
22  */
23
24 #include <sstream>
25 #include <fstream>
26 #include <iomanip>
27 #include <cassert>
28 #include <iostream>
29 #include <boost/filesystem.hpp>
30 #include <libxml++/libxml++.h>
31 #include "dcp.h"
32 #include "asset.h"
33 #include "sound_asset.h"
34 #include "picture_asset.h"
35 #include "subtitle_asset.h"
36 #include "util.h"
37 #include "metadata.h"
38 #include "exceptions.h"
39 #include "cpl_file.h"
40 #include "pkl_file.h"
41 #include "asset_map.h"
42 #include "reel.h"
43
44 using std::string;
45 using std::list;
46 using std::stringstream;
47 using std::ofstream;
48 using std::ostream;
49 using boost::shared_ptr;
50 using namespace libdcp;
51
52 DCP::DCP (string directory)
53         : _directory (directory)
54         , _encrypted (false)
55 {
56         boost::filesystem::create_directories (directory);
57 }
58
59 void
60 DCP::write_xml () const
61 {
62         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
63                 (*i)->write_xml (_encrypted, _certificates);
64         }
65
66         string pkl_uuid = make_uuid ();
67         string pkl_path = write_pkl (pkl_uuid);
68         
69         write_volindex ();
70         write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
71 }
72
73 std::string
74 DCP::write_pkl (string pkl_uuid) const
75 {
76         assert (!_cpls.empty ());
77         
78         boost::filesystem::path p;
79         p /= _directory;
80         stringstream s;
81         s << pkl_uuid << "_pkl.xml";
82         p /= s.str();
83         ofstream pkl (p.string().c_str());
84
85         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
86             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
87             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
88                 /* XXX: this is a bit of a hack */
89             << "  <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
90             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
91             << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
92             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
93             << "  <AssetList>\n";
94
95         list<shared_ptr<const Asset> > a = assets ();
96         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
97                 (*i)->write_to_pkl (pkl);
98         }
99
100         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
101                 (*i)->write_to_pkl (pkl);
102         }
103
104         pkl << "  </AssetList>\n"
105             << "</PackingList>\n";
106
107         return p.string ();
108 }
109
110 void
111 DCP::write_volindex () const
112 {
113         boost::filesystem::path p;
114         p /= _directory;
115         p /= "VOLINDEX.xml";
116         ofstream vi (p.string().c_str());
117
118         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
119            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
120            << "  <Index>1</Index>\n"
121            << "</VolumeIndex>\n";
122 }
123
124 void
125 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
126 {
127         boost::filesystem::path p;
128         p /= _directory;
129         p /= "ASSETMAP.xml";
130         ofstream am (p.string().c_str());
131
132         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
133            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
134            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
135            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
136            << "  <VolumeCount>1</VolumeCount>\n"
137            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
138            << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
139            << "  <AssetList>\n";
140
141         am << "    <Asset>\n"
142            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
143            << "      <PackingList>true</PackingList>\n"
144            << "      <ChunkList>\n"
145            << "        <Chunk>\n"
146            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
147            << "          <VolumeIndex>1</VolumeIndex>\n"
148            << "          <Offset>0</Offset>\n"
149            << "          <Length>" << pkl_length << "</Length>\n"
150            << "        </Chunk>\n"
151            << "      </ChunkList>\n"
152            << "    </Asset>\n";
153         
154         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
155                 (*i)->write_to_assetmap (am);
156         }
157
158         list<shared_ptr<const Asset> > a = assets ();
159         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
160                 (*i)->write_to_assetmap (am);
161         }
162
163         am << "  </AssetList>\n"
164            << "</AssetMap>\n";
165 }
166
167
168 void
169 DCP::read (bool require_mxfs)
170 {
171         Files files;
172
173         shared_ptr<AssetMap> asset_map;
174         try {
175                 boost::filesystem::path p = _directory;
176                 p /= "ASSETMAP";
177                 if (boost::filesystem::exists (p)) {
178                         asset_map.reset (new AssetMap (p.string ()));
179                 } else {
180                         p = _directory;
181                         p /= "ASSETMAP.xml";
182                         if (boost::filesystem::exists (p)) {
183                                 asset_map.reset (new AssetMap (p.string ()));
184                         } else {
185                                 throw DCPReadError ("could not find AssetMap file");
186                         }
187                 }
188                 
189         } catch (FileError& e) {
190                 throw FileError ("could not load AssetMap file", files.asset_map);
191         }
192
193         for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
194                 if ((*i)->chunks.size() != 1) {
195                         throw XMLError ("unsupported asset chunk count");
196                 }
197
198                 boost::filesystem::path t = _directory;
199                 t /= (*i)->chunks.front()->path;
200                 
201                 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
202                         continue;
203                 }
204
205                 xmlpp::DomParser* p = new xmlpp::DomParser;
206                 try {
207                         p->parse_file (t.string());
208                 } catch (std::exception& e) {
209                         delete p;
210                         continue;
211                 }
212
213                 string const root = p->get_document()->get_root_node()->get_name ();
214                 delete p;
215
216                 if (root == "CompositionPlaylist") {
217                         files.cpls.push_back (t.string());
218                 } else if (root == "PackingList") {
219                         if (files.pkl.empty ()) {
220                                 files.pkl = t.string();
221                         } else {
222                                 throw DCPReadError ("duplicate PKLs found");
223                         }
224                 }
225         }
226         
227         if (files.cpls.empty ()) {
228                 throw FileError ("no CPL files found", "");
229         }
230
231         if (files.pkl.empty ()) {
232                 throw FileError ("no PKL file found", "");
233         }
234
235         shared_ptr<PKLFile> pkl;
236         try {
237                 pkl.reset (new PKLFile (files.pkl));
238         } catch (FileError& e) {
239                 throw FileError ("could not load PKL file", files.pkl);
240         }
241
242         /* Cross-check */
243         /* XXX */
244
245         for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
246                 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
247         }
248 }
249
250 bool
251 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
252 {
253         if (_cpls.size() != other._cpls.size()) {
254                 notes.push_back ("CPL counts differ");
255                 return false;
256         }
257
258         list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
259         list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
260
261         while (a != _cpls.end ()) {
262                 if (!(*a)->equals (*b->get(), opt, notes)) {
263                         return false;
264                 }
265                 ++a;
266                 ++b;
267         }
268
269         return true;
270 }
271
272
273 void
274 DCP::add_cpl (shared_ptr<CPL> cpl)
275 {
276         _cpls.push_back (cpl);
277 }
278
279 class AssetComparator
280 {
281 public:
282         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
283                 return a->uuid() < b->uuid();
284         }
285 };
286
287 list<shared_ptr<const Asset> >
288 DCP::assets () const
289 {
290         list<shared_ptr<const Asset> > a;
291         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
292                 list<shared_ptr<const Asset> > t = (*i)->assets ();
293                 a.merge (t);
294         }
295
296         a.sort ();
297         a.unique ();
298         return a;
299 }
300
301 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
302         : _directory (directory)
303         , _name (name)
304         , _content_kind (content_kind)
305         , _length (length)
306         , _fps (frames_per_second)
307 {
308         _uuid = make_uuid ();
309 }
310
311 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
312         : _directory (directory)
313         , _content_kind (FEATURE)
314         , _length (0)
315         , _fps (0)
316 {
317         /* Read the XML */
318         shared_ptr<CPLFile> cpl;
319         try {
320                 cpl.reset (new CPLFile (file));
321         } catch (FileError& e) {
322                 throw FileError ("could not load CPL file", file);
323         }
324         
325         /* Now cherry-pick the required bits into our own data structure */
326         
327         _name = cpl->annotation_text;
328         _content_kind = cpl->content_kind;
329
330         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
331
332                 shared_ptr<Picture> p;
333
334                 if ((*i)->asset_list->main_picture) {
335                         p = (*i)->asset_list->main_picture;
336                 } else {
337                         p = (*i)->asset_list->main_stereoscopic_picture;
338                 }
339                 
340                 _fps = p->edit_rate.numerator;
341                 _length += p->duration;
342
343                 shared_ptr<PictureAsset> picture;
344                 shared_ptr<SoundAsset> sound;
345                 shared_ptr<SubtitleAsset> subtitle;
346
347                 /* Some rather twisted logic to decide if we are 3D or not;
348                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
349                    just have a FrameRate twice the EditRate and apparently
350                    expect you to divine the fact that they are hence 3D.
351                 */
352
353                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
354
355                         try {
356                                 picture.reset (new MonoPictureAsset (
357                                                        _directory,
358                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
359                                                        _fps,
360                                                        (*i)->asset_list->main_picture->entry_point,
361                                                        (*i)->asset_list->main_picture->duration
362                                                        )
363                                         );
364                         } catch (MXFFileError) {
365                                 if (require_mxfs) {
366                                         throw;
367                                 }
368                         }
369                         
370                 } else {
371
372                         try {
373                                 picture.reset (new StereoPictureAsset (
374                                                        _directory,
375                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
376                                                        _fps,
377                                                        p->entry_point,
378                                                        p->duration
379                                                        )
380                                         );
381                         } catch (MXFFileError) {
382                                 if (require_mxfs) {
383                                         throw;
384                                 }
385                         }
386                         
387                 }
388                 
389                 if ((*i)->asset_list->main_sound) {
390                         
391                         try {
392                                 sound.reset (new SoundAsset (
393                                                      _directory,
394                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
395                                                      _fps,
396                                                      (*i)->asset_list->main_sound->entry_point,
397                                                      (*i)->asset_list->main_sound->duration
398                                                      )
399                                         );
400                         } catch (MXFFileError) {
401                                 if (require_mxfs) {
402                                         throw;
403                                 }
404                         }
405                 }
406
407                 if ((*i)->asset_list->main_subtitle) {
408                         
409                         subtitle.reset (new SubtitleAsset (
410                                                 _directory,
411                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
412                                                 )
413                                 );
414                 }
415                         
416                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
417         }
418 }
419
420 void
421 CPL::add_reel (shared_ptr<const Reel> reel)
422 {
423         _reels.push_back (reel);
424 }
425
426 void
427 CPL::write_xml (bool encrypted, CertificateChain const & certificates) const
428 {
429         boost::filesystem::path p;
430         p /= _directory;
431         stringstream s;
432         s << _uuid << "_cpl.xml";
433         p /= s.str();
434         ofstream os (p.string().c_str());
435         
436         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
437            << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
438            << "  <Id>urn:uuid:" << _uuid << "</Id>\n"
439            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
440            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
441            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
442            << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
443            << "  <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
444            << "  <ContentVersion>\n"
445            << "    <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
446            << "    <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
447            << "  </ContentVersion>\n"
448            << "  <RatingList/>\n"
449            << "  <ReelList>\n";
450         
451         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
452                 (*i)->write_to_cpl (os);
453         }
454
455         os << "      </AssetList>\n"
456            << "    </Reel>\n"
457            << "  </ReelList>\n";
458
459         if (encrypted) {
460                 os << "  <Signer>\n"
461                    << "    <dsig:X509Data>\n"
462                    << "      <dsig:X509IssuerSerial>\n"
463                    << "        <dsig:X509IssuerName>" << Certificate::name_for_xml (certificates.leaf()->issuer()) << "</dsig:IssuerName>\n"
464                    << "        <dsig:X509SerialNumber>" << certificates.leaf()->serial() << "</dsig:X509SerialNumber>\n"
465                    << "      <dsig:X509IssuerSerial>\n"
466                    << "      <dsig:X509SubjectName>" << Certificate::name_for_xml (certificates.leaf()->subject()) << "</dsig:X509SubjectName>\n"
467                    << "    </dsig:X509Data>\n"
468                    << "  </Signer>\n"
469                    << "  <dsig:Signature>\n"
470                    << "    <dsig:SignedInfo>\n"
471                    << "      <dsig:CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>\n"
472                    << "      <dsig:SignatureMethod Algorithm=\"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256\"/>\n"
473                    << "      <dsig:Reference URI=\"\">\n"
474                    << "        <dsig:Transforms>\n"
475                    << "          <dsig:Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/>\n"
476                    << "        </dsig:Transforms>\n"
477                    << "        <dsig:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\n"
478                         /* this is done by xmlsec1 in cinemaslides */
479                    << "        <dsig:DigestValue>" << "XXX" << "</dsig:DigestValue>\n"
480                    << "      </dsig:Reference>\n"
481                    << "    </dsig:SignedInfo>\n"
482                         /* this is done by xmlsec1 in cinemaslides */
483                    << "    <dsig:SignatureValue>" << "XXX" << "</dsig:SignatureValue>\n";
484                 
485                 os << "    <dsig:KeyInfo>\n";
486                 
487                 list<shared_ptr<Certificate> > c = certificates.leaf_to_root ();
488                 for (list<shared_ptr<Certificate> >::iterator i = c.begin(); i != c.end(); ++i) {
489                         os << "      <dsig:X509Data>\n"
490                            << "        <dsig:X509IssuerSerial>\n"
491                            << "          <dsig:X509IssuerName>" << Certificate::name_for_xml ((*i)->issuer()) << "</dsig:IssuerName>\n"
492                            << "          <dsig:X509SerialNumber>" << (*i)->serial() << "</dsig:X509SerialNumber>\n"
493                            << "        </dsig:X509IssuerSerial>\n"
494                            << "        <dsig:X509Certificate>" << "XXX" << "</dsig:X509Certificate>\n"
495                            << "      </dsig:X509Data>\n";
496                 }
497
498                 os << "    </dsig:KeyInfo>\n";
499                 os << "  </dsig:Signature>\n";
500         }
501         
502         os << "</CompositionPlaylist>\n";
503
504         os.close ();
505
506         _digest = make_digest (p.string (), 0);
507         _length = boost::filesystem::file_size (p.string ());
508 }
509
510 void
511 CPL::write_to_pkl (ostream& s) const
512 {
513         s << "    <Asset>\n"
514           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
515           << "      <Hash>" << _digest << "</Hash>\n"
516           << "      <Size>" << _length << "</Size>\n"
517           << "      <Type>text/xml</Type>\n"
518           << "    </Asset>\n";
519 }
520
521 list<shared_ptr<const Asset> >
522 CPL::assets () const
523 {
524         list<shared_ptr<const Asset> > a;
525         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
526                 if ((*i)->main_picture ()) {
527                         a.push_back ((*i)->main_picture ());
528                 }
529                 if ((*i)->main_sound ()) {
530                         a.push_back ((*i)->main_sound ());
531                 }
532                 if ((*i)->main_subtitle ()) {
533                         a.push_back ((*i)->main_subtitle ());
534                 }
535         }
536
537         return a;
538 }
539
540 void
541 CPL::write_to_assetmap (ostream& s) const
542 {
543         s << "    <Asset>\n"
544           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
545           << "      <ChunkList>\n"
546           << "        <Chunk>\n"
547           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
548           << "          <VolumeIndex>1</VolumeIndex>\n"
549           << "          <Offset>0</Offset>\n"
550           << "          <Length>" << _length << "</Length>\n"
551           << "        </Chunk>\n"
552           << "      </ChunkList>\n"
553           << "    </Asset>\n";
554 }
555         
556         
557         
558 bool
559 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
560 {
561         if (_name != other._name) {
562                 notes.push_back ("names differ");
563                 return false;
564         }
565
566         if (_content_kind != other._content_kind) {
567                 notes.push_back ("content kinds differ");
568                 return false;
569         }
570
571         if (_fps != other._fps) {
572                 notes.push_back ("frames per second differ");
573                 return false;
574         }
575
576         if (_length != other._length) {
577                 notes.push_back ("lengths differ");
578                 return false;
579         }
580
581         if (_reels.size() != other._reels.size()) {
582                 notes.push_back ("reel counts differ");
583                 return false;
584         }
585         
586         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
587         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
588         
589         while (a != _reels.end ()) {
590                 if (!(*a)->equals (*b, opt, notes)) {
591                         return false;
592                 }
593                 ++a;
594                 ++b;
595         }
596
597         return true;
598 }