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"
35 #include "exceptions.h"
38 #include "asset_map.h"
41 using namespace boost;
42 using namespace libdcp;
44 DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
45 : _directory (directory)
47 , _content_kind (content_kind)
55 DCP::add_sound_asset (vector<string> const & files)
57 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, _directory, "audio.mxf", &Progress, _fps, _length)));
61 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
63 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, _directory, "audio.mxf", &Progress, _fps, _length, channels)));
67 DCP::add_picture_asset (vector<string> const & files, int width, int height)
69 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
73 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
75 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
79 DCP::write_xml () const
81 string cpl_uuid = make_uuid ();
82 string cpl_path = write_cpl (cpl_uuid);
83 int cpl_length = filesystem::file_size (cpl_path);
84 string cpl_digest = make_digest (cpl_path, 0);
86 string pkl_uuid = make_uuid ();
87 string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
90 write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
94 DCP::write_cpl (string cpl_uuid) const
99 s << cpl_uuid << "_cpl.xml";
101 ofstream cpl (p.string().c_str());
103 cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
104 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
105 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
106 << " <AnnotationText>" << _name << "</AnnotationText>\n"
107 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
108 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
109 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
110 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
111 << " <ContentVersion>\n"
112 << " <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
113 << " <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
114 << " </ContentVersion>\n"
115 << " <RatingList/>\n"
119 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
122 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
123 (*i)->write_to_cpl (cpl);
126 cpl << " </AssetList>\n"
129 << "</CompositionPlaylist>\n";
135 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
140 s << pkl_uuid << "_pkl.xml";
142 ofstream pkl (p.string().c_str());
144 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
145 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
146 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
147 << " <AnnotationText>" << _name << "</AnnotationText>\n"
148 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
149 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
150 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
153 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
154 (*i)->write_to_pkl (pkl);
158 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
159 << " <Hash>" << cpl_digest << "</Hash>\n"
160 << " <Size>" << cpl_length << "</Size>\n"
161 << " <Type>text/xml</Type>\n"
164 pkl << " </AssetList>\n"
165 << "</PackingList>\n";
171 DCP::write_volindex () const
176 ofstream vi (p.string().c_str());
178 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
179 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
180 << " <Index>1</Index>\n"
181 << "</VolumeIndex>\n";
185 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
190 ofstream am (p.string().c_str());
192 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
193 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
194 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
195 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
196 << " <VolumeCount>1</VolumeCount>\n"
197 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
198 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
202 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
203 << " <PackingList>true</PackingList>\n"
206 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
207 << " <VolumeIndex>1</VolumeIndex>\n"
208 << " <Offset>0</Offset>\n"
209 << " <Length>" << pkl_length << "</Length>\n"
215 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
218 << " <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
219 << " <VolumeIndex>1</VolumeIndex>\n"
220 << " <Offset>0</Offset>\n"
221 << " <Length>" << cpl_length << "</Length>\n"
226 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
227 (*i)->write_to_assetmap (am);
230 am << " </AssetList>\n"
235 DCP::DCP (string directory)
236 : _directory (directory)
240 string asset_map_file;
242 for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
243 string const t = i->path().string ();
244 if (ends_with (t, "_cpl.xml")) {
245 if (cpl_file.empty ()) {
248 throw DCPReadError ("duplicate CPLs found");
250 } else if (ends_with (t, "_pkl.xml")) {
251 if (pkl_file.empty ()) {
254 throw DCPReadError ("duplicate PKLs found");
256 } else if (ends_with (t, "ASSETMAP.xml")) {
257 if (asset_map_file.empty ()) {
260 throw DCPReadError ("duplicate AssetMaps found");
265 if (cpl_file.empty ()) {
266 throw FileError ("no CPL file found", "");
269 if (pkl_file.empty ()) {
270 throw FileError ("no PKL file found", "");
273 if (asset_map_file.empty ()) {
274 throw FileError ("no AssetMap file found", "");
280 cpl.reset (new CPL (cpl_file));
281 } catch (FileError& e) {
282 throw FileError ("could not load CPL file", cpl_file);
287 pkl.reset (new PKL (pkl_file));
288 } catch (FileError& e) {
289 throw FileError ("could not load PKL file", pkl_file);
292 shared_ptr<AssetMap> asset_map;
294 asset_map.reset (new AssetMap (asset_map_file));
295 } catch (FileError& e) {
296 throw FileError ("could not load AssetMap file", asset_map_file);
302 /* Now cherry-pick the required bits into our own data structure */
304 _name = cpl->annotation_text;
305 _content_kind = cpl->content_kind;
307 shared_ptr<CPLAssetList> cpl_assets = cpl->reels.front()->asset_list;
310 _fps = cpl_assets->main_picture->frame_rate.numerator;
311 _length = cpl_assets->main_picture->duration;
313 _assets.push_back (shared_ptr<PictureAsset> (
316 cpl_assets->main_picture->annotation_text,
319 cpl_assets->main_picture->screen_aspect_ratio.numerator,
320 cpl_assets->main_picture->screen_aspect_ratio.denominator
324 if (cpl_assets->main_sound) {
325 _assets.push_back (shared_ptr<SoundAsset> (
328 cpl_assets->main_sound->annotation_text,
337 DCP::equals (DCP const & other, EqualityFlags flags, double max_mean, double max_std_dev) const
341 if (flags & LIBDCP_METADATA) {
342 if (_name != other._name) {
343 notes.push_back ("names differ");
345 if (_content_kind != other._content_kind) {
346 notes.push_back ("content kinds differ");
348 if (_fps != other._fps) {
349 notes.push_back ("frames per second differ");
351 if (_length != other._length) {
352 notes.push_back ("lengths differ");
356 if (_assets.size() != other._assets.size()) {
357 notes.push_back ("asset counts differ");
360 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
361 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
363 while (a != _assets.end ()) {
364 list<string> n = (*a)->equals (*b, flags, max_mean, max_std_dev);