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