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)
241 scan (files, directory);
243 if (files.cpl.empty ()) {
244 throw FileError ("no CPL file found", "");
247 if (files.pkl.empty ()) {
248 throw FileError ("no PKL file found", "");
251 if (files.asset_map.empty ()) {
252 throw FileError ("no AssetMap file found", "");
258 cpl.reset (new CPL (files.cpl));
259 } catch (FileError& e) {
260 throw FileError ("could not load CPL file", files.cpl);
265 pkl.reset (new PKL (files.pkl));
266 } catch (FileError& e) {
267 throw FileError ("could not load PKL file", files.pkl);
270 shared_ptr<AssetMap> asset_map;
272 asset_map.reset (new AssetMap (files.asset_map));
273 } catch (FileError& e) {
274 throw FileError ("could not load AssetMap file", files.asset_map);
280 /* Now cherry-pick the required bits into our own data structure */
282 _name = cpl->annotation_text;
283 _content_kind = cpl->content_kind;
285 shared_ptr<CPLAssetList> cpl_assets = cpl->reels.front()->asset_list;
288 _fps = cpl_assets->main_picture->frame_rate.numerator;
289 _length = cpl_assets->main_picture->duration;
291 string n = cpl_assets->main_picture->annotation_text;
293 n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
296 _assets.push_back (shared_ptr<PictureAsset> (
305 if (cpl_assets->main_sound) {
307 n = cpl_assets->main_sound->annotation_text;
309 n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
312 _assets.push_back (shared_ptr<SoundAsset> (
325 DCP::scan (Files& files, string directory) const
327 for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
329 string const t = i->path().string ();
331 if (filesystem::is_directory (*i)) {
336 if (ends_with (t, ".mxf")) {
340 xmlpp::DomParser* p = new xmlpp::DomParser;
344 } catch (std::exception& e) {
354 string const root = p->get_document()->get_root_node()->get_name ();
357 if (root == "CompositionPlaylist") {
358 if (files.cpl.empty ()) {
361 throw DCPReadError ("duplicate CPLs found");
363 } else if (root == "PackingList") {
364 if (files.pkl.empty ()) {
367 throw DCPReadError ("duplicate PKLs found");
369 } else if (root == "AssetMap") {
370 if (files.asset_map.empty ()) {
373 throw DCPReadError ("duplicate AssetMaps found");
382 DCP::equals (DCP const & other, EqualityOptions opt) const
386 if (opt.flags & LIBDCP_METADATA) {
387 if (_name != other._name) {
388 notes.push_back ("names differ");
390 if (_content_kind != other._content_kind) {
391 notes.push_back ("content kinds differ");
393 if (_fps != other._fps) {
394 notes.push_back ("frames per second differ");
396 if (_length != other._length) {
397 notes.push_back ("lengths differ");
401 if (_assets.size() != other._assets.size()) {
402 notes.push_back ("asset counts differ");
405 list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
406 list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
408 while (a != _assets.end ()) {
409 list<string> n = (*a)->equals (*b, opt);
418 shared_ptr<const PictureAsset>
419 DCP::picture_asset () const
421 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
422 shared_ptr<PictureAsset> p = dynamic_pointer_cast<PictureAsset> (*i);
428 return shared_ptr<const PictureAsset> ();
431 shared_ptr<const SoundAsset>
432 DCP::sound_asset () const
434 for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
435 shared_ptr<SoundAsset> s = dynamic_pointer_cast<SoundAsset> (*i);
441 return shared_ptr<const SoundAsset> ();