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 <boost/algorithm/string.hpp>
31 #include <libxml++/libxml++.h>
34 #include "sound_asset.h"
35 #include "picture_asset.h"
36 #include "subtitle_asset.h"
39 #include "exceptions.h"
42 #include "asset_map.h"
48 using std::stringstream;
51 using boost::shared_ptr;
52 using namespace libdcp;
54 DCP::DCP (string directory)
55 : _directory (directory)
57 boost::filesystem::create_directories (directory);
61 DCP::write_xml () const
63 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
67 string pkl_uuid = make_uuid ();
68 string pkl_path = write_pkl (pkl_uuid);
71 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
75 DCP::write_pkl (string pkl_uuid) const
77 assert (!_cpls.empty ());
79 boost::filesystem::path p;
82 s << pkl_uuid << "_pkl.xml";
84 ofstream pkl (p.string().c_str());
86 pkl << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
87 << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
88 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
89 /* XXX: this is a bit of a hack */
90 << " <AnnotationText>" << _cpls.front()->name() << "</AnnotationText>\n"
91 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
92 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
93 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
96 list<shared_ptr<const Asset> > a = assets ();
97 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
98 (*i)->write_to_pkl (pkl);
101 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
102 (*i)->write_to_pkl (pkl);
105 pkl << " </AssetList>\n"
106 << "</PackingList>\n";
112 DCP::write_volindex () const
114 boost::filesystem::path p;
117 ofstream vi (p.string().c_str());
119 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
120 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
121 << " <Index>1</Index>\n"
122 << "</VolumeIndex>\n";
126 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
128 boost::filesystem::path p;
131 ofstream am (p.string().c_str());
133 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
134 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
135 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
136 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
137 << " <VolumeCount>1</VolumeCount>\n"
138 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
139 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
143 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
144 << " <PackingList>true</PackingList>\n"
147 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
148 << " <VolumeIndex>1</VolumeIndex>\n"
149 << " <Offset>0</Offset>\n"
150 << " <Length>" << pkl_length << "</Length>\n"
155 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
156 (*i)->write_to_assetmap (am);
159 list<shared_ptr<const Asset> > a = assets ();
160 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
161 (*i)->write_to_assetmap (am);
164 am << " </AssetList>\n"
170 DCP::read (bool require_mxfs)
174 shared_ptr<AssetMap> asset_map;
176 boost::filesystem::path p = _directory;
178 if (boost::filesystem::exists (p)) {
179 asset_map.reset (new AssetMap (p.string ()));
183 if (boost::filesystem::exists (p)) {
184 asset_map.reset (new AssetMap (p.string ()));
186 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
190 } catch (FileError& e) {
191 boost::throw_exception (FileError ("could not load AssetMap file", files.asset_map));
194 for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
195 if ((*i)->chunks.size() != 1) {
196 boost::throw_exception (XMLError ("unsupported asset chunk count"));
199 boost::filesystem::path t = _directory;
200 t /= (*i)->chunks.front()->path;
202 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
206 xmlpp::DomParser* p = new xmlpp::DomParser;
208 p->parse_file (t.string());
209 } catch (std::exception& e) {
214 string const root = p->get_document()->get_root_node()->get_name ();
217 if (root == "CompositionPlaylist") {
218 files.cpls.push_back (t.string());
219 } else if (root == "PackingList") {
220 if (files.pkl.empty ()) {
221 files.pkl = t.string();
223 boost::throw_exception (DCPReadError ("duplicate PKLs found"));
228 if (files.cpls.empty ()) {
229 boost::throw_exception (FileError ("no CPL files found", ""));
232 if (files.pkl.empty ()) {
233 boost::throw_exception (FileError ("no PKL file found", ""));
236 shared_ptr<PKLFile> pkl;
238 pkl.reset (new PKLFile (files.pkl));
239 } catch (FileError& e) {
240 boost::throw_exception (FileError ("could not load PKL file", files.pkl));
246 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
247 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
252 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
254 if (_cpls.size() != other._cpls.size()) {
255 note (ERROR, "CPL counts differ");
259 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
260 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
262 while (a != _cpls.end ()) {
263 if (!(*a)->equals (*b->get(), opt, note)) {
275 DCP::add_cpl (shared_ptr<CPL> cpl)
277 _cpls.push_back (cpl);
280 class AssetComparator
283 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
284 return a->uuid() < b->uuid();
288 list<shared_ptr<const Asset> >
291 list<shared_ptr<const Asset> > a;
292 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
293 list<shared_ptr<const Asset> > t = (*i)->assets ();