Basics of OV/supplemental support when reading.
[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 #include "kdm.h"
50
51 using std::string;
52 using std::list;
53 using std::stringstream;
54 using std::ofstream;
55 using std::ostream;
56 using std::copy;
57 using std::back_inserter;
58 using boost::shared_ptr;
59 using boost::lexical_cast;
60 using namespace libdcp;
61
62 DCP::DCP (string directory)
63         : _directory (directory)
64 {
65         boost::filesystem::create_directories (directory);
66 }
67
68 void
69 DCP::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
70 {
71         for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
72                 (*i)->write_xml (interop, metadata, crypt);
73         }
74
75         string pkl_uuid = make_uuid ();
76         string pkl_path = write_pkl (pkl_uuid, interop, metadata, crypt);
77         
78         write_volindex ();
79         write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), interop, metadata);
80 }
81
82 std::string
83 DCP::write_pkl (string pkl_uuid, bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
84 {
85         assert (!_cpls.empty ());
86         
87         boost::filesystem::path p;
88         p /= _directory;
89         stringstream s;
90         s << pkl_uuid << "_pkl.xml";
91         p /= s.str();
92
93         xmlpp::Document doc;
94         xmlpp::Element* pkl;
95         if (interop) {
96                 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
97         } else {
98                 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
99         }
100         
101         if (crypt) {
102                 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
103         }
104
105         pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
106         /* XXX: this is a bit of a hack */
107         pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
108         pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
109         pkl->add_child("Issuer")->add_child_text (metadata.issuer);
110         pkl->add_child("Creator")->add_child_text (metadata.creator);
111
112         xmlpp::Element* asset_list = pkl->add_child("AssetList");
113         list<shared_ptr<const Asset> > a = assets ();
114         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
115                 (*i)->write_to_pkl (asset_list);
116         }
117         
118         for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
119                 (*i)->write_to_pkl (asset_list);
120         }
121
122         if (crypt) {
123                 sign (pkl, crypt->certificates, crypt->signer_key, interop);
124         }
125                 
126         doc.write_to_file_formatted (p.string (), "UTF-8");
127         return p.string ();
128 }
129
130 void
131 DCP::write_volindex () const
132 {
133         boost::filesystem::path p;
134         p /= _directory;
135         p /= "VOLINDEX.xml";
136
137         xmlpp::Document doc;
138         xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
139         root->add_child("Index")->add_child_text ("1");
140         doc.write_to_file_formatted (p.string (), "UTF-8");
141 }
142
143 void
144 DCP::write_assetmap (string pkl_uuid, int pkl_length, bool interop, XMLMetadata const & metadata) const
145 {
146         boost::filesystem::path p;
147         p /= _directory;
148         p /= "ASSETMAP.xml";
149
150         xmlpp::Document doc;
151         xmlpp::Element* root;
152         if (interop) {
153                 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
154         } else {
155                 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
156         }
157
158         root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
159         root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
160         if (interop) {
161                 root->add_child("VolumeCount")->add_child_text ("1");
162                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
163                 root->add_child("Issuer")->add_child_text (metadata.issuer);
164                 root->add_child("Creator")->add_child_text (metadata.creator);
165         } else {
166                 root->add_child("Creator")->add_child_text (metadata.creator);
167                 root->add_child("VolumeCount")->add_child_text ("1");
168                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
169                 root->add_child("Issuer")->add_child_text (metadata.issuer);
170         }
171                 
172         xmlpp::Node* asset_list = root->add_child ("AssetList");
173
174         xmlpp::Node* asset = asset_list->add_child ("Asset");
175         asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
176         asset->add_child("PackingList")->add_child_text ("true");
177         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
178         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
179         chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
180         chunk->add_child("VolumeIndex")->add_child_text ("1");
181         chunk->add_child("Offset")->add_child_text ("0");
182         chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
183         
184         for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
185                 (*i)->write_to_assetmap (asset_list);
186         }
187
188         list<shared_ptr<const Asset> > a = assets ();
189         for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
190                 (*i)->write_to_assetmap (asset_list);
191         }
192
193         doc.write_to_file_formatted (p.string (), "UTF-8");
194 }
195
196
197 void
198 DCP::read (bool require_mxfs)
199 {
200         Files files;
201
202         shared_ptr<parse::AssetMap> asset_map;
203         try {
204                 boost::filesystem::path p = _directory;
205                 p /= "ASSETMAP";
206                 if (boost::filesystem::exists (p)) {
207                         asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
208                 } else {
209                         p = _directory;
210                         p /= "ASSETMAP.xml";
211                         if (boost::filesystem::exists (p)) {
212                                 asset_map.reset (new libdcp::parse::AssetMap (p.string ()));
213                         } else {
214                                 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
215                         }
216                 }
217                 
218         } catch (FileError& e) {
219                 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
220         }
221
222         for (list<shared_ptr<libdcp::parse::AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
223                 if ((*i)->chunks.size() != 1) {
224                         boost::throw_exception (XMLError ("unsupported asset chunk count"));
225                 }
226
227                 boost::filesystem::path t = _directory;
228                 t /= (*i)->chunks.front()->path;
229                 
230                 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
231                         continue;
232                 }
233
234                 xmlpp::DomParser* p = new xmlpp::DomParser;
235                 try {
236                         p->parse_file (t.string());
237                 } catch (std::exception& e) {
238                         delete p;
239                         continue;
240                 }
241
242                 string const root = p->get_document()->get_root_node()->get_name ();
243                 delete p;
244
245                 if (root == "CompositionPlaylist") {
246                         files.cpls.push_back (t.string());
247                 } else if (root == "PackingList") {
248                         if (files.pkl.empty ()) {
249                                 files.pkl = t.string();
250                         } else {
251                                 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
252                         }
253                 }
254         }
255         
256         if (files.cpls.empty ()) {
257                 boost::throw_exception (FileError ("no CPL files found", ""));
258         }
259
260         if (files.pkl.empty ()) {
261                 boost::throw_exception (FileError ("no PKL file found", ""));
262         }
263
264         shared_ptr<parse::PKL> pkl;
265         try {
266                 pkl.reset (new parse::PKL (files.pkl));
267         } catch (FileError& e) {
268                 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
269         }
270
271         /* Cross-check */
272         /* XXX */
273
274         _asset_maps.push_back (asset_map);
275
276         for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
277                 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, _asset_maps, require_mxfs)));
278         }
279 }
280
281 bool
282 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
283 {
284         if (_cpls.size() != other._cpls.size()) {
285                 note (ERROR, "CPL counts differ");
286                 return false;
287         }
288
289         list<shared_ptr<CPL> >::const_iterator a = _cpls.begin ();
290         list<shared_ptr<CPL> >::const_iterator b = other._cpls.begin ();
291
292         while (a != _cpls.end ()) {
293                 if (!(*a)->equals (*b->get(), opt, note)) {
294                         return false;
295                 }
296                 ++a;
297                 ++b;
298         }
299
300         return true;
301 }
302
303 void
304 DCP::add_cpl (shared_ptr<CPL> cpl)
305 {
306         _cpls.push_back (cpl);
307 }
308
309 class AssetComparator
310 {
311 public:
312         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
313                 return a->uuid() < b->uuid();
314         }
315 };
316
317 list<shared_ptr<const Asset> >
318 DCP::assets () const
319 {
320         list<shared_ptr<const Asset> > a;
321         for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
322                 list<shared_ptr<const Asset> > t = (*i)->assets ();
323                 a.merge (t);
324         }
325
326         a.sort (AssetComparator ());
327         a.unique ();
328         return a;
329 }
330
331 bool
332 DCP::encrypted () const
333 {
334         for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
335                 if ((*i)->encrypted ()) {
336                         return true;
337                 }
338         }
339
340         return false;
341 }
342
343 void
344 DCP::add_kdm (KDM const & kdm)
345 {
346         list<KDMCipher> ciphers = kdm.ciphers ();
347         
348         for (list<shared_ptr<CPL> >::iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
349                 for (list<KDMCipher>::iterator j = ciphers.begin(); j != ciphers.end(); ++j) {
350                         if (j->cpl_id() == (*i)->id()) {
351                                 (*i)->add_kdm (kdm);
352                         }                               
353                 }
354         }
355 }
356
357 void
358 DCP::add_assets_from (DCP const & ov)
359 {
360         copy (ov._asset_maps.begin(), ov._asset_maps.end(), back_inserter (_asset_maps));
361 }