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