Cope with paths having file:// at the start of them.
[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 namespace std;
45 using namespace boost;
46 using namespace libdcp;
47
48 DCP::DCP (string directory)
49         : _directory (directory)
50 {
51         filesystem::create_directories (directory);
52 }
53
54 void
55 DCP::write_xml () const
56 {
57         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
58                 (*i)->write_xml ();
59         }
60
61         string pkl_uuid = make_uuid ();
62         string pkl_path = write_pkl (pkl_uuid);
63         
64         write_volindex ();
65         write_assetmap (pkl_uuid, filesystem::file_size (pkl_path));
66 }
67
68 std::string
69 DCP::write_pkl (string pkl_uuid) const
70 {
71         assert (!_cpls.empty ());
72         
73         filesystem::path p;
74         p /= _directory;
75         stringstream s;
76         s << pkl_uuid << "_pkl.xml";
77         p /= s.str();
78         ofstream pkl (p.string().c_str());
79
80         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
81             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
82             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
83                 /* XXX: this is a bit of a hack */
84             << "  <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
85             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
86             << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
87             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
88             << "  <AssetList>\n";
89
90         list<shared_ptr<const Asset> > a = assets ();
91         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
92                 (*i)->write_to_pkl (pkl);
93         }
94
95         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
96                 (*i)->write_to_pkl (pkl);
97         }
98
99         pkl << "  </AssetList>\n"
100             << "</PackingList>\n";
101
102         return p.string ();
103 }
104
105 void
106 DCP::write_volindex () const
107 {
108         filesystem::path p;
109         p /= _directory;
110         p /= "VOLINDEX.xml";
111         ofstream vi (p.string().c_str());
112
113         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
114            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
115            << "  <Index>1</Index>\n"
116            << "</VolumeIndex>\n";
117 }
118
119 void
120 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
121 {
122         filesystem::path p;
123         p /= _directory;
124         p /= "ASSETMAP.xml";
125         ofstream am (p.string().c_str());
126
127         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
128            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
129            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
130            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
131            << "  <VolumeCount>1</VolumeCount>\n"
132            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
133            << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
134            << "  <AssetList>\n";
135
136         am << "    <Asset>\n"
137            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
138            << "      <PackingList>true</PackingList>\n"
139            << "      <ChunkList>\n"
140            << "        <Chunk>\n"
141            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
142            << "          <VolumeIndex>1</VolumeIndex>\n"
143            << "          <Offset>0</Offset>\n"
144            << "          <Length>" << pkl_length << "</Length>\n"
145            << "        </Chunk>\n"
146            << "      </ChunkList>\n"
147            << "    </Asset>\n";
148         
149         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
150                 (*i)->write_to_assetmap (am);
151         }
152
153         list<shared_ptr<const Asset> > a = assets ();
154         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
155                 (*i)->write_to_assetmap (am);
156         }
157
158         am << "  </AssetList>\n"
159            << "</AssetMap>\n";
160 }
161
162
163 void
164 DCP::read (bool require_mxfs)
165 {
166         Files files;
167
168         shared_ptr<AssetMap> asset_map;
169         try {
170                 filesystem::path p = _directory;
171                 p /= "ASSETMAP";
172                 if (filesystem::exists (p)) {
173                         asset_map.reset (new AssetMap (p.string ()));
174                 } else {
175                         p = _directory;
176                         p /= "ASSETMAP.xml";
177                         if (filesystem::exists (p)) {
178                                 asset_map.reset (new AssetMap (p.string ()));
179                         } else {
180                                 throw DCPReadError ("could not find AssetMap file");
181                         }
182                 }
183                 
184         } catch (FileError& e) {
185                 throw FileError ("could not load AssetMap file", files.asset_map);
186         }
187
188         for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
189                 if ((*i)->chunks.size() != 1) {
190                         throw XMLError ("unsupported asset chunk count");
191                 }
192
193                 filesystem::path t = _directory;
194                 t /= (*i)->chunks.front()->path;
195                 
196                 if (ends_with (t.string(), ".mxf") || ends_with (t.string(), ".ttf")) {
197                         continue;
198                 }
199
200                 xmlpp::DomParser* p = new xmlpp::DomParser;
201                 try {
202                         p->parse_file (t.string());
203                 } catch (std::exception& e) {
204                         delete p;
205                         continue;
206                 }
207
208                 string const root = p->get_document()->get_root_node()->get_name ();
209                 delete p;
210
211                 if (root == "CompositionPlaylist") {
212                         files.cpls.push_back (t.string());
213                 } else if (root == "PackingList") {
214                         if (files.pkl.empty ()) {
215                                 files.pkl = t.string();
216                         } else {
217                                 throw DCPReadError ("duplicate PKLs found");
218                         }
219                 } else if (root == "DCSubtitle") {
220                         files.subtitles.push_back (t.string());
221                 }
222         }
223         
224         if (files.cpls.empty ()) {
225                 throw FileError ("no CPL files found", "");
226         }
227
228         if (files.pkl.empty ()) {
229                 throw FileError ("no PKL file found", "");
230         }
231
232         shared_ptr<PKLFile> pkl;
233         try {
234                 pkl.reset (new PKLFile (files.pkl));
235         } catch (FileError& e) {
236                 throw FileError ("could not load PKL file", files.pkl);
237         }
238
239         /* Cross-check */
240         /* XXX */
241
242         for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
243                 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
244         }
245
246 }
247
248 list<string>
249 DCP::equals (DCP const & other, EqualityOptions opt) const
250 {
251         list<string> notes;
252
253         if (_cpls.size() != other._cpls.size()) {
254                 notes.push_back ("CPL counts differ");
255         }
256
257         list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
258         list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
259
260         while (a != _cpls.end ()) {
261                 list<string> n = (*a)->equals (*b->get(), opt);
262                 notes.merge (n);
263                 ++a;
264                 ++b;
265         }
266
267         return notes;
268 }
269
270
271 void
272 DCP::add_cpl (shared_ptr<CPL> cpl)
273 {
274         _cpls.push_back (cpl);
275 }
276
277 class AssetComparator
278 {
279 public:
280         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
281                 return a->uuid() < b->uuid();
282         }
283 };
284
285 list<shared_ptr<const Asset> >
286 DCP::assets () const
287 {
288         list<shared_ptr<const Asset> > a;
289         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
290                 list<shared_ptr<const Asset> > t = (*i)->assets ();
291                 a.merge (t);
292         }
293
294         a.sort ();
295         a.unique ();
296         return a;
297 }
298
299 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
300         : _directory (directory)
301         , _name (name)
302         , _content_kind (content_kind)
303         , _length (length)
304         , _fps (frames_per_second)
305 {
306         _uuid = make_uuid ();
307 }
308
309 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
310         : _directory (directory)
311         , _content_kind (FEATURE)
312         , _length (0)
313         , _fps (0)
314 {
315         /* Read the XML */
316         shared_ptr<CPLFile> cpl;
317         try {
318                 cpl.reset (new CPLFile (file));
319         } catch (FileError& e) {
320                 throw FileError ("could not load CPL file", file);
321         }
322         
323         /* Now cherry-pick the required bits into our own data structure */
324         
325         _name = cpl->annotation_text;
326         _content_kind = cpl->content_kind;
327
328         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
329
330                 shared_ptr<Picture> p;
331
332                 if ((*i)->asset_list->main_picture) {
333                         p = (*i)->asset_list->main_picture;
334                 } else {
335                         p = (*i)->asset_list->main_stereoscopic_picture;
336                 }
337                 
338                 _fps = p->edit_rate.numerator;
339                 _length += p->duration;
340
341                 shared_ptr<PictureAsset> picture;
342                 shared_ptr<SoundAsset> sound;
343                 shared_ptr<SubtitleAsset> subtitle;
344
345                 if ((*i)->asset_list->main_picture) {
346
347                         try {
348                                 picture.reset (new MonoPictureAsset (
349                                                        _directory,
350                                                        asset_map->asset_from_id ((*i)->asset_list->main_picture->id)->chunks.front()->path,
351                                                        _fps,
352                                                        (*i)->asset_list->main_picture->entry_point,
353                                                        (*i)->asset_list->main_picture->duration
354                                                        )
355                                         );
356                         } catch (MXFFileError) {
357                                 if (require_mxfs) {
358                                         throw;
359                                 }
360                         }
361                         
362                 } else if ((*i)->asset_list->main_stereoscopic_picture) {
363                         
364                         try {
365                                 picture.reset (new StereoPictureAsset (
366                                                        _directory,
367                                                        asset_map->asset_from_id ((*i)->asset_list->main_stereoscopic_picture->id)->chunks.front()->path,
368                                                        _fps,
369                                                        (*i)->asset_list->main_stereoscopic_picture->entry_point,
370                                                        (*i)->asset_list->main_stereoscopic_picture->duration
371                                                        )
372                                         );
373                         } catch (MXFFileError) {
374                                 if (require_mxfs) {
375                                         throw;
376                                 }
377                         }
378                         
379                 }
380                 
381                 if ((*i)->asset_list->main_sound) {
382                         
383                         try {
384                                 sound.reset (new SoundAsset (
385                                                      _directory,
386                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
387                                                      _fps,
388                                                      (*i)->asset_list->main_sound->entry_point,
389                                                      (*i)->asset_list->main_sound->duration
390                                                      )
391                                         );
392                         } catch (MXFFileError) {
393                                 if (require_mxfs) {
394                                         throw;
395                                 }
396                         }
397                 }
398
399                 if ((*i)->asset_list->main_subtitle) {
400                         
401                         subtitle.reset (new SubtitleAsset (
402                                                 _directory,
403                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
404                                                 )
405                                 );
406                 }
407                         
408                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
409         }
410 }
411
412 void
413 CPL::add_reel (shared_ptr<const Reel> reel)
414 {
415         _reels.push_back (reel);
416 }
417
418 void
419 CPL::write_xml () const
420 {
421         filesystem::path p;
422         p /= _directory;
423         stringstream s;
424         s << _uuid << "_cpl.xml";
425         p /= s.str();
426         ofstream os (p.string().c_str());
427         
428         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
429            << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
430            << "  <Id>urn:uuid:" << _uuid << "</Id>\n"
431            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
432            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
433            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
434            << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
435            << "  <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
436            << "  <ContentVersion>\n"
437            << "    <Id>urn:uri:" << _uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
438            << "    <LabelText>" << _uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
439            << "  </ContentVersion>\n"
440            << "  <RatingList/>\n"
441            << "  <ReelList>\n";
442         
443         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
444                 (*i)->write_to_cpl (os);
445         }
446
447         os << "      </AssetList>\n"
448            << "    </Reel>\n"
449            << "  </ReelList>\n"
450            << "</CompositionPlaylist>\n";
451
452         os.close ();
453
454         _digest = make_digest (p.string (), 0);
455         _length = filesystem::file_size (p.string ());
456 }
457
458 void
459 CPL::write_to_pkl (ostream& s) const
460 {
461         s << "    <Asset>\n"
462           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
463           << "      <Hash>" << _digest << "</Hash>\n"
464           << "      <Size>" << _length << "</Size>\n"
465           << "      <Type>text/xml</Type>\n"
466           << "    </Asset>\n";
467 }
468
469 list<shared_ptr<const Asset> >
470 CPL::assets () const
471 {
472         list<shared_ptr<const Asset> > a;
473         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
474                 if ((*i)->main_picture ()) {
475                         a.push_back ((*i)->main_picture ());
476                 }
477                 if ((*i)->main_sound ()) {
478                         a.push_back ((*i)->main_sound ());
479                 }
480                 if ((*i)->main_subtitle ()) {
481                         a.push_back ((*i)->main_subtitle ());
482                 }
483         }
484
485         return a;
486 }
487
488 void
489 CPL::write_to_assetmap (ostream& s) const
490 {
491         s << "    <Asset>\n"
492           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
493           << "      <ChunkList>\n"
494           << "        <Chunk>\n"
495           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
496           << "          <VolumeIndex>1</VolumeIndex>\n"
497           << "          <Offset>0</Offset>\n"
498           << "          <Length>" << _length << "</Length>\n"
499           << "        </Chunk>\n"
500           << "      </ChunkList>\n"
501           << "    </Asset>\n";
502 }
503         
504         
505         
506 list<string>
507 CPL::equals (CPL const & other, EqualityOptions opt) const
508 {
509         list<string> notes;
510         
511         if (opt.flags & LIBDCP_METADATA) {
512                 if (_name != other._name) {
513                         notes.push_back ("names differ");
514                 }
515                 if (_content_kind != other._content_kind) {
516                         notes.push_back ("content kinds differ");
517                 }
518                 if (_fps != other._fps) {
519                         notes.push_back ("frames per second differ");
520                 }
521                 if (_length != other._length) {
522                         notes.push_back ("lengths differ");
523                 }
524         }
525
526         if (_reels.size() != other._reels.size()) {
527                 notes.push_back ("reel counts differ");
528         }
529         
530         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
531         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
532         
533         while (a != _reels.end ()) {
534                 list<string> n = (*a)->equals (*b, opt);
535                 notes.merge (n);
536                 ++a;
537                 ++b;
538         }
539
540         return notes;
541 }