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