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.
28 #include <boost/filesystem.hpp>
31 #include "sound_asset.h"
32 #include "picture_asset.h"
37 using namespace boost;
38 using namespace libdcp;
40 DCP::DCP (string directory, string name, ContentType content_type, int fps, int length)
41 : _directory (directory)
43 , _content_type (content_type)
51 DCP::add_sound_asset (vector<string> const & files)
56 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), &Progress, _fps, _length)));
60 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
65 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, p.string(), &Progress, _fps, _length, channels)));
69 DCP::add_picture_asset (vector<string> const & files, int width, int height)
74 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, width, height)));
78 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
83 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, p.string(), &Progress, _fps, _length, width, height)));
87 DCP::write_xml () const
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);
94 string pkl_uuid = make_uuid ();
95 string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
98 write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
102 DCP::write_cpl (string cpl_uuid) const
107 s << cpl_uuid << "_cpl.xml";
109 ofstream cpl (p.string().c_str());
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"
127 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
130 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
131 (*i)->write_to_cpl (cpl);
134 cpl << " </AssetList>\n"
137 << "</CompositionPlaylist>\n";
143 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
148 s << pkl_uuid << "_pkl.xml";
150 ofstream pkl (p.string().c_str());
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"
161 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
162 (*i)->write_to_pkl (pkl);
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"
172 pkl << " </AssetList>\n"
173 << "</PackingList>\n";
179 DCP::write_volindex () const
184 ofstream vi (p.string().c_str());
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";
193 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
198 ofstream am (p.string().c_str());
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"
210 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
211 << " <PackingList>true</PackingList>\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"
223 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\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"
234 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
235 (*i)->write_to_assetmap (am);
238 am << " </AssetList>\n"
243 DCP::DCP (string directory)
244 : _directory (directory)
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")) {
256 throw DCPReadError ("duplicate CPLs found");
258 } else if (ends_with (t, "_pkl.xml")) {
262 throw DCPReadError ("duplicate PKLs found");
264 } else if (ends_with (t, "ASSETMAP.xml")) {
265 if (asset_map.empty ()) {
268 throw DCPReadError ("duplicate AssetMaps found");
275 load_asset_map (asset_map);
279 DCP::load_cpl (string file)
281 xmlpp::DOMParser parser;
282 parser.parser_file (file);
284 throw DCPReadError ("could not create parser for CPL");
287 xmlpp::Element* root = parser.get_document()->get_root_node ();
288 dcp_read_assert (root->get_name() == "CompositionPlaylist", "unrecognised CPL format");
290 xmlpp::Node::NodeList children = root->get_children ();
291 for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
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");
300 if ((*i)->get_name() == "ContentVersion") {
302 load_cpl_content_version (*i);
305 if ((*i)->get_name() == "RatingList") {
309 if ((*i)->get_name() == "ReelList") {
311 load_cpl_reel_list (*i);
314 xml_assert_taken (*i, taken);
319 DCP::load_cpl_content_version (xmlpp::Node const * node)
321 xmlpp::Node::NodeList children = node->get_children ();
322 for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
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);
331 DCP::load_cpl_reel_list (xmlpp::Node const * node)
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) {
337 if ((*i)->get_name() == "Reel") {
342 throw DCPReadError ("multiple reels not supported");
345 xml_assert_taken (*i, taken);
350 DCP::load_cpl_reel (xmlpp::Node const * node)
352 xmlpp::Node::NodeList children = node->get_children ();
353 for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
355 xml_taken (*i, taken, _reel_id, "Id");
356 if ((*i)->name() == "AssetList") {
358 load_cpl_asset_list (*i);
360 xml_assert_taken (*i, taken);
365 DCP::load_cpl_asset_list (xmlpp::Node const * node)
367 xmlpp::Node::NodeList children = node->get_children ();
368 for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
370 if ((*i)->name() == "MainPicture") {
372 load_cpl_main_picture (*i);
373 } else if ((*i)->name() == "MainSound") {
375 load_cpl_main_sound (*i);
377 xml_assert_taken (*i, taken);
382 DCP::load_cpl_main_picture (xmlpp::Node const * node)
384 xmlpp::Node::NodeList children = node->get_children ();
385 for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
387 xml_maybe (*i, taken, _video_id, "Id");