Hacks.
[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 <boost/filesystem.hpp>
29 #include "dcp.h"
30 #include "asset.h"
31 #include "sound_asset.h"
32 #include "picture_asset.h"
33 #include "util.h"
34 #include "metadata.h"
35
36 using namespace std;
37 using namespace boost;
38 using namespace libdcp;
39
40 DCP::DCP (string directory, string name, ContentType content_type, int fps, int length)
41         : _directory (directory)
42         , _name (name)
43         , _content_type (content_type)
44         , _fps (fps)
45         , _length (length)
46 {
47         
48 }
49
50 void
51 DCP::add_sound_asset (vector<string> const & files)
52 {
53         filesystem::path p;
54         p /= _directory;
55         p /= "audio.mxf";
56         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), &Progress, _fps, _length)));
57 }
58
59 void
60 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
61 {
62         filesystem::path p;
63         p /= _directory;
64         p /= "audio.mxf";
65         _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, p.string(), &Progress, _fps, _length, channels)));
66 }
67
68 void
69 DCP::add_picture_asset (vector<string> const & files, int width, int height)
70 {
71         filesystem::path p;
72         p /= _directory;
73         p /= "video.mxf";
74         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, width, height)));
75 }
76
77 void
78 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
79 {
80         filesystem::path p;
81         p /= _directory;
82         p /= "video.mxf";
83         _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, p.string(), &Progress, _fps, _length, width, height)));
84 }
85
86 void
87 DCP::write_xml () const
88 {
89         string cpl_uuid = make_uuid ();
90         string cpl_path = write_cpl (cpl_uuid);
91         int cpl_length = filesystem::file_size (cpl_path);
92         string cpl_digest = make_digest (cpl_path, 0);
93
94         string pkl_uuid = make_uuid ();
95         string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
96         
97         write_volindex ();
98         write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
99 }
100
101 string
102 DCP::write_cpl (string cpl_uuid) const
103 {
104         filesystem::path p;
105         p /= _directory;
106         stringstream s;
107         s << cpl_uuid << "_cpl.xml";
108         p /= s.str();
109         ofstream cpl (p.string().c_str());
110         
111         cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
112             << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
113             << "  <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
114             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
115             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
116             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
117             << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
118             << "  <ContentKind>" << content_type_string (_content_type) << "</ContentKind>\n"
119             << "  <ContentVersion>\n"
120             << "    <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
121             << "    <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
122             << "  </ContentVersion>\n"
123             << "  <RatingList/>\n"
124             << "  <ReelList>\n";
125
126         cpl << "    <Reel>\n"
127             << "      <Id>urn:uuid:" << make_uuid() << "</Id>\n"
128             << "      <AssetList>\n";
129
130         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
131                 (*i)->write_to_cpl (cpl);
132         }
133
134         cpl << "      </AssetList>\n"
135             << "    </Reel>\n"
136             << "  </ReelList>\n"
137             << "</CompositionPlaylist>\n";
138
139         return p.string ();
140 }
141
142 std::string
143 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
144 {
145         filesystem::path p;
146         p /= _directory;
147         stringstream s;
148         s << pkl_uuid << "_pkl.xml";
149         p /= s.str();
150         ofstream pkl (p.string().c_str());
151
152         pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
153             << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
154             << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
155             << "  <AnnotationText>" << _name << "</AnnotationText>\n"
156             << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
157             << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
158             << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
159             << "  <AssetList>\n";
160
161         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
162                 (*i)->write_to_pkl (pkl);
163         }
164
165         pkl << "    <Asset>\n"
166             << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
167             << "      <Hash>" << cpl_digest << "</Hash>\n"
168             << "      <Size>" << cpl_length << "</Size>\n"
169             << "      <Type>text/xml</Type>\n"
170             << "    </Asset>\n";
171
172         pkl << "  </AssetList>\n"
173             << "</PackingList>\n";
174
175         return p.string ();
176 }
177
178 void
179 DCP::write_volindex () const
180 {
181         filesystem::path p;
182         p /= _directory;
183         p /= "VOLINDEX.xml";
184         ofstream vi (p.string().c_str());
185
186         vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
187            << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
188            << "  <Index>1</Index>\n"
189            << "</VolumeIndex>\n";
190 }
191
192 void
193 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
194 {
195         filesystem::path p;
196         p /= _directory;
197         p /= "ASSETMAP.xml";
198         ofstream am (p.string().c_str());
199
200         am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
201            << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
202            << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
203            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
204            << "  <VolumeCount>1</VolumeCount>\n"
205            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
206            << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
207            << "  <AssetList>\n";
208
209         am << "    <Asset>\n"
210            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
211            << "      <PackingList>true</PackingList>\n"
212            << "      <ChunkList>\n"
213            << "        <Chunk>\n"
214            << "          <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
215            << "          <VolumeIndex>1</VolumeIndex>\n"
216            << "          <Offset>0</Offset>\n"
217            << "          <Length>" << pkl_length << "</Length>\n"
218            << "        </Chunk>\n"
219            << "      </ChunkList>\n"
220            << "    </Asset>\n";
221
222         am << "    <Asset>\n"
223            << "      <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
224            << "      <ChunkList>\n"
225            << "        <Chunk>\n"
226            << "          <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
227            << "          <VolumeIndex>1</VolumeIndex>\n"
228            << "          <Offset>0</Offset>\n"
229            << "          <Length>" << cpl_length << "</Length>\n"
230            << "        </Chunk>\n"
231            << "      </ChunkList>\n"
232            << "    </Asset>\n";
233         
234         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
235                 (*i)->write_to_assetmap (am);
236         }
237
238         am << "  </AssetList>\n"
239            << "</AssetMap>\n";
240 }
241
242
243 DCP::DCP (string directory)
244         : _directory (directory)
245 {
246         string cpl;
247         string pkl;
248         string asset_map;
249
250         for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
251                 string const t = i->path()->string ();
252                 if (ends_with (t, "_cpl.xml")) {
253                         if (cpl.empty ()) {
254                                 cpl = t;
255                         } else {
256                                 throw DCPReadError ("duplicate CPLs found");
257                         }
258                 } else if (ends_with (t, "_pkl.xml")) {
259                         if (pkl.empty ()) {
260                                 pkl = t;
261                         } else {
262                                 throw DCPReadError ("duplicate PKLs found");
263                         }
264                 } else if (ends_with (t, "ASSETMAP.xml")) {
265                         if (asset_map.empty ()) {
266                                 asset_map = t;
267                         } else {
268                                 throw DCPReadError ("duplicate AssetMaps found");
269                         }
270                 }
271         }
272
273         load_cpl (cpl);
274         load_pkl (pkl);
275         load_asset_map (asset_map);
276 }
277
278 void
279 DCP::load_cpl (string file)
280 {
281         xmlpp::DOMParser parser;
282         parser.parser_file (file);
283         if (!parser) {
284                 throw DCPReadError ("could not create parser for CPL");
285         }
286
287         xmlpp::Element* root = parser.get_document()->get_root_node ();
288         dcp_read_assert (root->get_name() == "CompositionPlaylist", "unrecognised CPL format");
289
290         xmlpp::Node::NodeList children = root->get_children ();
291         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
292                 bool taken = false;
293                 xml_maybe (*i, taken, _cpl_id, "Id");
294                 xml_maybe (*i, taken, _annotation_text, "AnnotationText");
295                 xml_maybe (*i, taken, _issue_date, "IssueDate");
296                 xml_maybe (*i, taken, _creator, "Creator");
297                 xml_maybe (*i, taken, _content_title_text, "ContentTitleText");
298                 xml_maybe (*i, taken, _content_kind, "ContentKind");
299
300                 if ((*i)->get_name() == "ContentVersion") {
301                         taken = true;
302                         load_cpl_content_version (*i);
303                 }
304
305                 if ((*i)->get_name() == "RatingList") {
306                         taken = true;
307                 }
308
309                 if ((*i)->get_name() == "ReelList") {
310                         taken = true;
311                         load_cpl_reel_list (*i);
312                 }
313
314                 xml_assert_taken (*i, taken);
315         }
316 }
317
318 void
319 DCP::load_cpl_content_version (xmlpp::Node const * node)
320 {
321         xmlpp::Node::NodeList children = node->get_children ();
322         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
323                 bool taken = false;
324                 xml_maybe (*i, taken, _content_version_id, "Id");
325                 xml_maybe (*i, taken, _content_version_label_text, "LabelText");
326                 xml_assert_taken (*i, taken);
327         }
328 }
329
330 void
331 DCP::load_cpl_reel_list (xmlpp::Node const * node)
332 {
333         xmlpp::Node::NodeList children = node->get_children ();
334         bool had_reel = false;
335         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
336                 bool taken = false;
337                 if ((*i)->get_name() == "Reel") {
338                         if (!had_reel) {
339                                 load_cpl_reel (*i);
340                                 had_reel = true;
341                         } else {
342                                 throw DCPReadError ("multiple reels not supported");
343                         }
344                 }
345                 xml_assert_taken (*i, taken);
346         }
347 }
348
349 void
350 DCP::load_cpl_reel (xmlpp::Node const * node)
351 {
352         xmlpp::Node::NodeList children = node->get_children ();
353         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
354                 bool taken = false;
355                 xml_taken (*i, taken, _reel_id, "Id");
356                 if ((*i)->name() == "AssetList") {
357                         taken = true;
358                         load_cpl_asset_list (*i);
359                 }
360                 xml_assert_taken (*i, taken);
361         }
362 }
363
364 void
365 DCP::load_cpl_asset_list (xmlpp::Node const * node)
366 {
367         xmlpp::Node::NodeList children = node->get_children ();
368         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
369                 bool taken = false;
370                 if ((*i)->name() == "MainPicture") {
371                         taken = true;
372                         load_cpl_main_picture (*i);
373                 } else if ((*i)->name() == "MainSound") {
374                         taken = true;
375                         load_cpl_main_sound (*i);
376                 }
377                 xml_assert_taken (*i, taken);
378         }
379 }
380
381 void
382 DCP::load_cpl_main_picture (xmlpp::Node const * node)
383 {
384         xmlpp::Node::NodeList children = node->get_children ();
385         for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
386                 bool taken = false;
387                 xml_maybe (*i, taken, _video_id, "Id");
388                 
389         }
390 }