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