2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 * @brief A class to create a DCP.
29 #include <boost/filesystem.hpp>
30 #include <libxml++/libxml++.h>
33 #include "sound_asset.h"
34 #include "picture_asset.h"
35 #include "subtitle_asset.h"
38 #include "exceptions.h"
41 #include "asset_map.h"
45 using namespace boost;
46 using namespace libdcp;
48 DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
49 : _directory (directory)
51 , _content_kind (content_kind)
55 filesystem::create_directories (directory);
59 DCP::add_reel (shared_ptr<const Reel> reel)
61 _reels.push_back (reel);
65 DCP::write_xml () const
67 string cpl_uuid = make_uuid ();
68 string cpl_path = write_cpl (cpl_uuid);
69 int cpl_length = filesystem::file_size (cpl_path);
70 string cpl_digest = make_digest (cpl_path, 0);
72 string pkl_uuid = make_uuid ();
73 string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
76 write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
80 DCP::write_cpl (string cpl_uuid) const
85 s << cpl_uuid << "_cpl.xml";
87 ofstream cpl (p.string().c_str());
89 cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
90 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
91 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
92 << " <AnnotationText>" << _name << "</AnnotationText>\n"
93 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
94 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
95 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
96 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
97 << " <ContentVersion>\n"
98 << " <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
99 << " <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
100 << " </ContentVersion>\n"
101 << " <RatingList/>\n"
104 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
105 (*i)->write_to_cpl (cpl);
108 cpl << " </AssetList>\n"
111 << "</CompositionPlaylist>\n";
117 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
122 s << pkl_uuid << "_pkl.xml";
124 ofstream pkl (p.string().c_str());
126 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
127 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
128 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
129 << " <AnnotationText>" << _name << "</AnnotationText>\n"
130 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
131 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
132 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
135 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
136 (*i)->write_to_pkl (pkl);
140 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
141 << " <Hash>" << cpl_digest << "</Hash>\n"
142 << " <Size>" << cpl_length << "</Size>\n"
143 << " <Type>text/xml</Type>\n"
146 pkl << " </AssetList>\n"
147 << "</PackingList>\n";
153 DCP::write_volindex () const
158 ofstream vi (p.string().c_str());
160 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
161 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
162 << " <Index>1</Index>\n"
163 << "</VolumeIndex>\n";
167 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
172 ofstream am (p.string().c_str());
174 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
175 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
176 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
177 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
178 << " <VolumeCount>1</VolumeCount>\n"
179 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
180 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
184 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
185 << " <PackingList>true</PackingList>\n"
188 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
189 << " <VolumeIndex>1</VolumeIndex>\n"
190 << " <Offset>0</Offset>\n"
191 << " <Length>" << pkl_length << "</Length>\n"
197 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
200 << " <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
201 << " <VolumeIndex>1</VolumeIndex>\n"
202 << " <Offset>0</Offset>\n"
203 << " <Length>" << cpl_length << "</Length>\n"
208 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
209 (*i)->write_to_assetmap (am);
212 am << " </AssetList>\n"
217 DCP::DCP (string directory)
218 : _directory (directory)
221 scan (files, directory);
223 if (files.cpl.empty ()) {
224 throw FileError ("no CPL file found", "");
227 if (files.pkl.empty ()) {
228 throw FileError ("no PKL file found", "");
231 if (files.asset_map.empty ()) {
232 throw FileError ("no AssetMap file found", "");
238 cpl.reset (new CPL (files.cpl));
239 } catch (FileError& e) {
240 throw FileError ("could not load CPL file", files.cpl);
245 pkl.reset (new PKL (files.pkl));
246 } catch (FileError& e) {
247 throw FileError ("could not load PKL file", files.pkl);
250 shared_ptr<AssetMap> asset_map;
252 asset_map.reset (new AssetMap (files.asset_map));
253 } catch (FileError& e) {
254 throw FileError ("could not load AssetMap file", files.asset_map);
260 /* Now cherry-pick the required bits into our own data structure */
262 _name = cpl->annotation_text;
263 _content_kind = cpl->content_kind;
267 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
269 shared_ptr<Picture> p;
271 if ((*i)->asset_list->main_picture) {
272 p = (*i)->asset_list->main_picture;
274 p = (*i)->asset_list->main_stereoscopic_picture;
277 assert (_fps == 0 || _fps == p->edit_rate.numerator);
278 _fps = p->edit_rate.numerator;
279 _length += p->duration;
281 shared_ptr<PictureAsset> picture;
282 shared_ptr<SoundAsset> sound;
283 shared_ptr<SubtitleAsset> subtitle;
285 if ((*i)->asset_list->main_picture) {
287 string n = pkl->asset_from_id (p->id)->original_file_name;
289 n = p->annotation_text;
292 picture.reset (new MonoPictureAsset (
296 (*i)->asset_list->main_picture->entry_point,
297 (*i)->asset_list->main_picture->duration
301 } else if ((*i)->asset_list->main_stereoscopic_picture) {
303 string n = pkl->asset_from_id (p->id)->original_file_name;
305 n = p->annotation_text;
308 picture.reset (new StereoPictureAsset (
312 (*i)->asset_list->main_stereoscopic_picture->entry_point,
313 (*i)->asset_list->main_stereoscopic_picture->duration
319 if ((*i)->asset_list->main_sound) {
321 string n = pkl->asset_from_id ((*i)->asset_list->main_sound->id)->original_file_name;
323 n = (*i)->asset_list->main_sound->annotation_text;
326 sound.reset (new SoundAsset (
330 (*i)->asset_list->main_sound->entry_point,
331 (*i)->asset_list->main_sound->duration
336 if ((*i)->asset_list->main_subtitle) {
338 string n = pkl->asset_from_id ((*i)->asset_list->main_subtitle->id)->original_file_name;
340 n = (*i)->asset_list->main_subtitle->annotation_text;
343 subtitle.reset (new SubtitleAsset (
350 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
356 DCP::scan (Files& files, string directory) const
358 for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
360 string const t = i->path().string ();
362 if (filesystem::is_directory (*i)) {
367 if (ends_with (t, ".mxf") || ends_with (t, ".ttf")) {
371 xmlpp::DomParser* p = new xmlpp::DomParser;
375 } catch (std::exception& e) {
385 string const root = p->get_document()->get_root_node()->get_name ();
388 if (root == "CompositionPlaylist") {
389 if (files.cpl.empty ()) {
392 throw DCPReadError ("duplicate CPLs found");
394 } else if (root == "PackingList") {
395 if (files.pkl.empty ()) {
398 throw DCPReadError ("duplicate PKLs found");
400 } else if (root == "AssetMap") {
401 if (files.asset_map.empty ()) {
404 throw DCPReadError ("duplicate AssetMaps found");
407 } else if (root == "DCSubtitle") {
408 files.subtitles.push_back (t);
415 DCP::equals (DCP const & other, EqualityOptions opt) const
419 if (opt.flags & LIBDCP_METADATA) {
420 if (_name != other._name) {
421 notes.push_back ("names differ");
423 if (_content_kind != other._content_kind) {
424 notes.push_back ("content kinds differ");
426 if (_fps != other._fps) {
427 notes.push_back ("frames per second differ");
429 if (_length != other._length) {
430 notes.push_back ("lengths differ");
434 if (_reels.size() != other._reels.size()) {
435 notes.push_back ("reel counts differ");
438 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
439 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
441 while (a != _reels.end ()) {
442 list<string> n = (*a)->equals (*b, opt);