Merge master; at least partially.
[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 <boost/lexical_cast.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/lexical_cast.hpp>
33 #include <libxml++/libxml++.h>
34 #include <xmlsec/xmldsig.h>
35 #include <xmlsec/app.h>
36 #include "dcp.h"
37 #include "asset.h"
38 #include "sound_asset.h"
39 #include "picture_asset.h"
40 #include "subtitle_asset.h"
41 #include "util.h"
42 #include "metadata.h"
43 #include "exceptions.h"
44 #include "parse/pkl.h"
45 #include "parse/asset_map.h"
46 #include "reel.h"
47 #include "cpl.h"
48 #include "encryption.h"
49
50 using std::string;
51 using std::list;
52 using std::stringstream;
53 using std::ofstream;
54 using std::ostream;
55 using boost::shared_ptr;
56 using boost::lexical_cast;
57 using namespace libdcp;
58
59 DCP::DCP (string directory)
60         : _directory (directory)
61 {
62         boost::filesystem::create_directories (directory);
63 }
64
65 void
66 DCP::write_xml (XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
67 {
68         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
69                 (*i)->write_xml (metadata, crypt);
70         }
71
72         string pkl_uuid = make_uuid ();
73         string pkl_path = write_pkl (pkl_uuid, metadata, crypt);
74         
75         write_volindex ();
76         write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
77 }
78
79 std::string
80 DCP::write_pkl (string pkl_uuid, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
81 {
82         assert (!_cpls.empty ());
83         
84         boost::filesystem::path p;
85         p /= _directory;
86         stringstream s;
87         s << pkl_uuid << "_pkl.xml";
88         p /= s.str();
89
90         xmlpp::Document doc;
91         xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
92         if (crypt) {
93                 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
94         }
95
96         pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
97         /* XXX: this is a bit of a hack */
98         pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
99         pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
100         pkl->add_child("Issuer")->add_child_text (metadata.issuer);
101         pkl->add_child("Creator")->add_child_text (metadata.creator);
102
103         xmlpp::Element* asset_list = pkl->add_child("AssetList");
104         list<shared_ptr<const Asset> > a = assets ();
105         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
106                 (*i)->write_to_pkl (asset_list);
107         }
108         
109         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
110                 (*i)->write_to_pkl (asset_list);
111         }
112
113         if (crypt) {
114                 sign (pkl, crypt->certificates, crypt->signer_key);
115         }
116                 
117         doc.write_to_file_formatted (p.string (), "UTF-8");
118         return p.string ();
119 }
120
121 void
122 DCP::write_volindex () const
123 {
124         boost::filesystem::path p;
125         p /= _directory;
126         p /= "VOLINDEX.xml";
127
128         xmlpp::Document doc;
129         xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
130         root->add_child("Index")->add_child_text ("1");
131         doc.write_to_file_formatted (p.string (), "UTF-8");
132 }
133
134 void
135 DCP::write_assetmap (string pkl_uuid, int pkl_length, XMLMetadata const & metadata) const
136 {
137         boost::filesystem::path p;
138         p /= _directory;
139         p /= "ASSETMAP.xml";
140
141         xmlpp::Document doc;
142         xmlpp::Element* root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
143
144         root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
145         root->add_child("Creator")->add_child_text (metadata.creator);
146         root->add_child("VolumeCount")->add_child_text ("1");
147         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
148         root->add_child("Issuer")->add_child_text (metadata.issuer);
149         xmlpp::Node* asset_list = root->add_child ("AssetList");
150
151         xmlpp::Node* asset = asset_list->add_child ("Asset");
152         asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
153         asset->add_child("PackingList")->add_child_text ("true");
154         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
155         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
156         chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
157         chunk->add_child("VolumeIndex")->add_child_text ("1");
158         chunk->add_child("Offset")->add_child_text ("0");
159         chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
160         
161         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
162                 (*i)->write_to_assetmap (asset_list);
163         }
164
165         list<shared_ptr<const Asset> > a = assets ();
166         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
167                 (*i)->write_to_assetmap (asset_list);
168         }
169
170         doc.write_to_file_formatted (p.string (), "UTF-8");
171 }
172
173
174 void
175 DCP::read (bool require_mxfs)
176 {
177         Files files;
178
179         shared_ptr<parse::AssetMap> asset_map;
180         try {
181                 boost::filesystem::path p = _directory;
182                 p /= "ASSETMAP";
183                 if (boost::filesystem::exists (p)) {
184                         asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
185                 } else {
186                         p = _directory;
187                         p /= "ASSETMAP.xml";
188                         if (boost::filesystem::exists (p)) {
189                                 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
190                         } else {
191                                 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
192                         }
193                 }
194                 
195         } catch (FileError& e) {
196                 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
197         }
198
199         for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
200                 if ((*i)->chunks.size() != 1) {
201                         boost::throw_exception (XMLError ("unsupported asset chunk count"));
202                 }
203
204                 boost::filesystem::path t = _directory;
205                 t /= (*i)->chunks.front()->path;
206                 
207                 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
208                         continue;
209                 }
210
211                 xmlpp::DomParser* p = new xmlpp::DomParser;
212                 try {
213                         p->parse_file (t.string());
214                 } catch (std::exception& e) {
215                         delete p;
216                         continue;
217                 }
218
219                 string const root = p->get_document()->get_root_node()->get_name ();
220                 delete p;
221
222                 if (root == "CompositionPlaylist") {
223                         files.cpls.push_back (t.string());
224                 } else if (root == "PackingList") {
225                         if (files.pkl.empty ()) {
226                                 files.pkl = t.string();
227                         } else {
228                                 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
229                         }
230                 }
231         }
232         
233         if (files.cpls.empty ()) {
234                 boost::throw_exception (FileError ("no CPL files found", ""));
235         }
236
237         if (files.pkl.empty ()) {
238                 boost::throw_exception (FileError ("no PKL file found", ""));
239         }
240
241         shared_ptr<parse::PKL> pkl;
242         try {
243                 pkl.reset (new parse::PKL (files.pkl));
244         } catch (FileError& e) {
245                 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
246         }
247
248         /* Cross-check */
249         /* XXX */
250
251         for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
252                 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
253         }
254 }
255
256 bool
257 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
258 {
259         if (_cpls.size() != other._cpls.size()) {
260                 note (ERROR, "CPL counts differ");
261                 return false;
262         }
263
264         list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
265         list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
266
267         while (a != _cpls.end ()) {
268                 if (!(*a)->equals (*b->get(), opt, note)) {
269                         return false;
270                 }
271                 ++a;
272                 ++b;
273         }
274
275         return true;
276 }
277
278 void
279 DCP::add_cpl (shared_ptr<CPL> cpl)
280 {
281         _cpls.push_back (cpl);
282 }
283
284 class AssetComparator
285 {
286 public:
287         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
288                 return a->uuid() < b->uuid();
289         }
290 };
291
292 list<shared_ptr<const Asset> >
293 DCP::assets () const
294 {
295         list<shared_ptr<const Asset> > a;
296         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
297                 list<shared_ptr<const Asset> > t = (*i)->assets ();
298                 a.merge (t);
299         }
300
301         a.sort (AssetComparator ());
302         a.unique ();
303         return a;
304 }