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"
44 using namespace boost;
45 using namespace libdcp;
47 DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
48 : _directory (directory)
50 , _content_kind (content_kind)
58 DCP::add_sound_asset (vector<string> const & files)
60 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, _directory, "audio.mxf", &Progress, _fps, _length)));
64 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
66 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, _directory, "audio.mxf", &Progress, _fps, _length, channels)));
70 DCP::add_picture_asset (vector<string> const & files, int width, int height)
72 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
76 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
78 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
82 DCP::write_xml () const
84 string cpl_uuid = make_uuid ();
85 string cpl_path = write_cpl (cpl_uuid);
86 int cpl_length = filesystem::file_size (cpl_path);
87 string cpl_digest = make_digest (cpl_path, 0);
89 string pkl_uuid = make_uuid ();
90 string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
93 write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
97 DCP::write_cpl (string cpl_uuid) const
102 s << cpl_uuid << "_cpl.xml";
104 ofstream cpl (p.string().c_str());
106 cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
107 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
108 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
109 << " <AnnotationText>" << _name << "</AnnotationText>\n"
110 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
111 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
112 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
113 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
114 << " <ContentVersion>\n"
115 << " <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
116 << " <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
117 << " </ContentVersion>\n"
118 << " <RatingList/>\n"
122 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
125 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
126 (*i)->write_to_cpl (cpl);
129 cpl << " </AssetList>\n"
132 << "</CompositionPlaylist>\n";
138 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
143 s << pkl_uuid << "_pkl.xml";
145 ofstream pkl (p.string().c_str());
147 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
148 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
149 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
150 << " <AnnotationText>" << _name << "</AnnotationText>\n"
151 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
152 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
153 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
156 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
157 (*i)->write_to_pkl (pkl);
161 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
162 << " <Hash>" << cpl_digest << "</Hash>\n"
163 << " <Size>" << cpl_length << "</Size>\n"
164 << " <Type>text/xml</Type>\n"
167 pkl << " </AssetList>\n"
168 << "</PackingList>\n";
174 DCP::write_volindex () const
179 ofstream vi (p.string().c_str());
181 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
182 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
183 << " <Index>1</Index>\n"
184 << "</VolumeIndex>\n";
188 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
193 ofstream am (p.string().c_str());
195 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
196 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
197 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
198 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
199 << " <VolumeCount>1</VolumeCount>\n"
200 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
201 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
205 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
206 << " <PackingList>true</PackingList>\n"
209 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
210 << " <VolumeIndex>1</VolumeIndex>\n"
211 << " <Offset>0</Offset>\n"
212 << " <Length>" << pkl_length << "</Length>\n"
218 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
221 << " <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
222 << " <VolumeIndex>1</VolumeIndex>\n"
223 << " <Offset>0</Offset>\n"
224 << " <Length>" << cpl_length << "</Length>\n"
229 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
230 (*i)->write_to_assetmap (am);
233 am << " </AssetList>\n"
238 DCP::DCP (string directory)
239 : _directory (directory)
242 scan (files, directory);
244 if (files.cpl.empty ()) {
245 throw FileError ("no CPL file found", "");
248 if (files.pkl.empty ()) {
249 throw FileError ("no PKL file found", "");
252 if (files.asset_map.empty ()) {
253 throw FileError ("no AssetMap file found", "");
259 cpl.reset (new CPL (files.cpl));
260 } catch (FileError& e) {
261 throw FileError ("could not load CPL file", files.cpl);
266 pkl.reset (new PKL (files.pkl));
267 } catch (FileError& e) {
268 throw FileError ("could not load PKL file", files.pkl);
271 shared_ptr<AssetMap> asset_map;
273 asset_map.reset (new AssetMap (files.asset_map));
274 } catch (FileError& e) {
275 throw FileError ("could not load AssetMap file", files.asset_map);
281 /* Now cherry-pick the required bits into our own data structure */
283 _name = cpl->annotation_text;
284 _content_kind = cpl->content_kind;
286 shared_ptr<CPLAssetList> cpl_assets = cpl->reels.front()->asset_list;
289 _fps = cpl_assets->main_picture->frame_rate.numerator;
290 _length = cpl_assets->main_picture->duration;
292 string n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
294 n = cpl_assets->main_picture->annotation_text;
297 _assets.push_back (shared_ptr<PictureAsset> (
306 if (cpl_assets->main_sound) {
308 n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
310 n = cpl_assets->main_sound->annotation_text;
313 _assets.push_back (shared_ptr<SoundAsset> (
323 for (list<string>::iterator i = files.subtitles.begin(); i != files.subtitles.end(); ++i) {
324 string const l = i->substr (_directory.length ());
325 _assets.push_back (shared_ptr<SubtitleAsset> (new SubtitleAsset (_directory, l)));
331 DCP::scan (Files& files, string directory) const
333 for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
335 string const t = i->path().string ();
337 if (filesystem::is_directory (*i)) {
342 if (ends_with (t, ".mxf") || ends_with (t, ".ttf")) {
346 xmlpp::DomParser* p = new xmlpp::DomParser;
350 } catch (std::exception& e) {
360 string const root = p->get_document()->get_root_node()->get_name ();
363 if (root == "CompositionPlaylist") {
364 if (files.cpl.empty ()) {
367 throw DCPReadError ("duplicate CPLs found");
369 } else if (root == "PackingList") {
370 if (files.pkl.empty ()) {
373 throw DCPReadError ("duplicate PKLs found");
375 } else if (root == "AssetMap") {
376 if (files.asset_map.empty ()) {
379 throw DCPReadError ("duplicate AssetMaps found");
382 } else if (root == "DCSubtitle") {
383 files.subtitles.push_back (t);
390 DCP::equals (DCP const & other, EqualityOptions opt) const
394 if (opt.flags & LIBDCP_METADATA) {
395 if (_name != other._name) {
396 notes.push_back ("names differ");
398 if (_content_kind != other._content_kind) {
399 notes.push_back ("content kinds differ");
401 if (_fps != other._fps) {
402 notes.push_back ("frames per second differ");
404 if (_length != other._length) {
405 notes.push_back ("lengths differ");
409 if (_assets.size() != other._assets.size()) {
410 notes.push_back ("asset counts differ");
413 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
414 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
416 while (a != _assets.end ()) {
417 list<string> n = (*a)->equals (*b, opt);
426 shared_ptr<const PictureAsset>
427 DCP::picture_asset () const
429 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
430 shared_ptr<PictureAsset> p = dynamic_pointer_cast<PictureAsset> (*i);
436 return shared_ptr<const PictureAsset> ();
439 shared_ptr<const SoundAsset>
440 DCP::sound_asset () const
442 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
443 shared_ptr<SoundAsset> s = dynamic_pointer_cast<SoundAsset> (*i);
449 return shared_ptr<const SoundAsset> ();
452 shared_ptr<const SubtitleAsset>
453 DCP::subtitle_asset () const
455 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
456 shared_ptr<SubtitleAsset> s = dynamic_pointer_cast<SubtitleAsset> (*i);
462 return shared_ptr<const SubtitleAsset> ();