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"
37 #include "exceptions.h"
40 #include "asset_map.h"
43 using namespace boost;
44 using namespace libdcp;
46 DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
47 : _directory (directory)
49 , _content_kind (content_kind)
57 DCP::add_sound_asset (vector<string> const & files)
59 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, _directory, "audio.mxf", &Progress, _fps, _length)));
63 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
65 _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, _directory, "audio.mxf", &Progress, _fps, _length, channels)));
69 DCP::add_picture_asset (vector<string> const & files, int width, int height)
71 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
75 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
77 _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
81 DCP::write_xml () const
83 string cpl_uuid = make_uuid ();
84 string cpl_path = write_cpl (cpl_uuid);
85 int cpl_length = filesystem::file_size (cpl_path);
86 string cpl_digest = make_digest (cpl_path, 0);
88 string pkl_uuid = make_uuid ();
89 string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
92 write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
96 DCP::write_cpl (string cpl_uuid) const
101 s << cpl_uuid << "_cpl.xml";
103 ofstream cpl (p.string().c_str());
105 cpl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
106 << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
107 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
108 << " <AnnotationText>" << _name << "</AnnotationText>\n"
109 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
110 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
111 << " <ContentTitleText>" << _name << "</ContentTitleText>\n"
112 << " <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
113 << " <ContentVersion>\n"
114 << " <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
115 << " <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
116 << " </ContentVersion>\n"
117 << " <RatingList/>\n"
121 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
124 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
125 (*i)->write_to_cpl (cpl);
128 cpl << " </AssetList>\n"
131 << "</CompositionPlaylist>\n";
137 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
142 s << pkl_uuid << "_pkl.xml";
144 ofstream pkl (p.string().c_str());
146 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
147 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
148 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
149 << " <AnnotationText>" << _name << "</AnnotationText>\n"
150 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
151 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
152 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
155 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
156 (*i)->write_to_pkl (pkl);
160 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
161 << " <Hash>" << cpl_digest << "</Hash>\n"
162 << " <Size>" << cpl_length << "</Size>\n"
163 << " <Type>text/xml</Type>\n"
166 pkl << " </AssetList>\n"
167 << "</PackingList>\n";
173 DCP::write_volindex () const
178 ofstream vi (p.string().c_str());
180 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
181 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
182 << " <Index>1</Index>\n"
183 << "</VolumeIndex>\n";
187 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
192 ofstream am (p.string().c_str());
194 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
195 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
196 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
197 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
198 << " <VolumeCount>1</VolumeCount>\n"
199 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
200 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
204 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
205 << " <PackingList>true</PackingList>\n"
208 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
209 << " <VolumeIndex>1</VolumeIndex>\n"
210 << " <Offset>0</Offset>\n"
211 << " <Length>" << pkl_length << "</Length>\n"
217 << " <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
220 << " <Path>" << cpl_uuid << "_cpl.xml</Path>\n"
221 << " <VolumeIndex>1</VolumeIndex>\n"
222 << " <Offset>0</Offset>\n"
223 << " <Length>" << cpl_length << "</Length>\n"
228 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
229 (*i)->write_to_assetmap (am);
232 am << " </AssetList>\n"
237 DCP::DCP (string directory)
238 : _directory (directory)
242 string asset_map_file;
244 for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
246 string const t = i->path().string ();
248 if (ends_with (t, ".mxf")) {
252 xmlpp::DomParser* p = new xmlpp::DomParser;
256 } catch (std::exception& e) {
266 string const root = p->get_document()->get_root_node()->get_name ();
269 if (root == "CompositionPlaylist") {
270 if (cpl_file.empty ()) {
273 throw DCPReadError ("duplicate CPLs found");
275 } else if (root == "PackingList") {
276 if (pkl_file.empty ()) {
279 throw DCPReadError ("duplicate PKLs found");
281 } else if (root == "AssetMap") {
282 if (asset_map_file.empty ()) {
285 throw DCPReadError ("duplicate AssetMaps found");
291 if (cpl_file.empty ()) {
292 throw FileError ("no CPL file found", "");
295 if (pkl_file.empty ()) {
296 throw FileError ("no PKL file found", "");
299 if (asset_map_file.empty ()) {
300 throw FileError ("no AssetMap file found", "");
306 cpl.reset (new CPL (cpl_file));
307 } catch (FileError& e) {
308 throw FileError ("could not load CPL file", cpl_file);
313 pkl.reset (new PKL (pkl_file));
314 } catch (FileError& e) {
315 throw FileError ("could not load PKL file", pkl_file);
318 shared_ptr<AssetMap> asset_map;
320 asset_map.reset (new AssetMap (asset_map_file));
321 } catch (FileError& e) {
322 throw FileError ("could not load AssetMap file", asset_map_file);
328 /* Now cherry-pick the required bits into our own data structure */
330 _name = cpl->annotation_text;
331 _content_kind = cpl->content_kind;
333 shared_ptr<CPLAssetList> cpl_assets = cpl->reels.front()->asset_list;
336 _fps = cpl_assets->main_picture->frame_rate.numerator;
337 _length = cpl_assets->main_picture->duration;
339 string n = cpl_assets->main_picture->annotation_text;
341 n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
344 _assets.push_back (shared_ptr<PictureAsset> (
353 if (cpl_assets->main_sound) {
355 n = cpl_assets->main_sound->annotation_text;
357 n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
360 _assets.push_back (shared_ptr<SoundAsset> (
372 DCP::equals (DCP const & other, EqualityOptions opt) const
376 if (opt.flags & LIBDCP_METADATA) {
377 if (_name != other._name) {
378 notes.push_back ("names differ");
380 if (_content_kind != other._content_kind) {
381 notes.push_back ("content kinds differ");
383 if (_fps != other._fps) {
384 notes.push_back ("frames per second differ");
386 if (_length != other._length) {
387 notes.push_back ("lengths differ");
391 if (_assets.size() != other._assets.size()) {
392 notes.push_back ("asset counts differ");
395 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
396 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
398 while (a != _assets.end ()) {
399 list<string> n = (*a)->equals (*b, opt);
408 shared_ptr<const PictureAsset>
409 DCP::picture_asset () const
411 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
412 shared_ptr<PictureAsset> p = dynamic_pointer_cast<PictureAsset> (*i);
418 return shared_ptr<const PictureAsset> ();
421 shared_ptr<const SoundAsset>
422 DCP::sound_asset () const
424 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
425 shared_ptr<SoundAsset> s = dynamic_pointer_cast<SoundAsset> (*i);
431 return shared_ptr<const SoundAsset> ();