Rename CPL/PKL files to be {cpl,pkl}_uuid.xml rather than
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <libxml/parser.h>
21 #include "cpl.h"
22 #include "parse/cpl.h"
23 #include "util.h"
24 #include "mono_picture_asset.h"
25 #include "stereo_picture_asset.h"
26 #include "sound_asset.h"
27 #include "subtitle_asset.h"
28 #include "parse/asset_map.h"
29 #include "reel.h"
30 #include "metadata.h"
31 #include "signer.h"
32 #include "exceptions.h"
33 #include "compose.hpp"
34 #include "raw_convert.h"
35
36 using std::string;
37 using std::stringstream;
38 using std::ostream;
39 using std::list;
40 using std::pair;
41 using std::make_pair;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::optional;
45 using namespace libdcp;
46
47 CPL::CPL (boost::filesystem::path directory, string name, ContentKind content_kind, int length, int frames_per_second)
48         : _directory (directory)
49         , _name (name)
50         , _content_kind (content_kind)
51         , _length (length)
52         , _fps (frames_per_second)
53 {
54         _id = make_uuid ();
55 }
56
57 /** Construct a CPL object from a XML file.
58  *  @param directory The directory containing this CPL's DCP.
59  *  @param file The CPL XML filename.
60  *  @param asset_maps AssetMaps to look for assets in.
61  *  @param require_mxfs true to throw an exception if a required MXF file does not exist.
62  */
63 CPL::CPL (boost::filesystem::path directory, string file, list<PathAssetMap> asset_maps, bool require_mxfs)
64         : _directory (directory)
65         , _content_kind (FEATURE)
66         , _length (0)
67         , _fps (0)
68 {
69         /* Read the XML */
70         shared_ptr<parse::CPL> cpl;
71         try {
72                 cpl.reset (new parse::CPL (file));
73         } catch (FileError& e) {
74                 boost::throw_exception (FileError ("could not load CPL file", file, e.number ()));
75         }
76         
77         /* Now cherry-pick the required bits into our own data structure */
78         
79         _name = cpl->annotation_text;
80         _content_kind = cpl->content_kind;
81
82         /* Trim urn:uuid: off the front */
83         _id = cpl->id.substr (9);
84
85         for (list<shared_ptr<parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
86
87                 shared_ptr<parse::Picture> p;
88
89                 if ((*i)->asset_list->main_picture) {
90                         p = (*i)->asset_list->main_picture;
91                 } else {
92                         p = (*i)->asset_list->main_stereoscopic_picture;
93                 }
94                 
95                 _fps = p->edit_rate.numerator;
96                 _length += p->duration;
97
98                 shared_ptr<PictureAsset> picture;
99                 shared_ptr<SoundAsset> sound;
100                 shared_ptr<SubtitleAsset> subtitle;
101
102                 /* Some rather twisted logic to decide if we are 3D or not;
103                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
104                    just have a FrameRate twice the EditRate and apparently
105                    expect you to divine the fact that they are hence 3D.
106                 */
107
108                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
109
110                         try {
111                                 pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
112
113                                 picture.reset (new MonoPictureAsset (asset.first, asset.second->chunks.front()->path));
114
115                                 picture->read ();
116                                 picture->set_edit_rate (_fps);
117                                 picture->set_entry_point (p->entry_point);
118                                 picture->set_duration (p->duration);
119                                 if (p->key_id.length() > 9) {
120                                         /* Trim urn:uuid: */
121                                         picture->set_key_id (p->key_id.substr (9));
122                                 }
123                         } catch (MXFFileError) {
124                                 if (require_mxfs) {
125                                         throw;
126                                 }
127                         }
128                         
129                 } else {
130                         try {
131                                 pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
132
133                                 picture.reset (new StereoPictureAsset (asset.first, asset.second->chunks.front()->path));
134
135                                 picture->read ();
136                                 picture->set_edit_rate (_fps);
137                                 picture->set_entry_point (p->entry_point);
138                                 picture->set_duration (p->duration);
139                                 if (p->key_id.length() > 9) {
140                                         /* Trim urn:uuid: */
141                                         picture->set_key_id (p->key_id.substr (9));
142                                 }
143                                 
144                         } catch (MXFFileError) {
145                                 if (require_mxfs) {
146                                         throw;
147                                 }
148                         }
149                         
150                 }
151                 
152                 if ((*i)->asset_list->main_sound) {
153                         
154                         try {
155                                 pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_sound->id);
156                         
157                                 sound.reset (new SoundAsset (asset.first, asset.second->chunks.front()->path));
158                                 shared_ptr<parse::MainSound> s = (*i)->asset_list->main_sound;
159
160                                 sound->read ();
161                                 sound->set_entry_point (s->entry_point);
162                                 sound->set_duration (s->duration);
163                                 if (s->key_id.length() > 9) {
164                                         /* Trim urn:uuid: */
165                                         sound->set_key_id (s->key_id.substr (9));
166                                 }
167                         } catch (MXFFileError) {
168                                 if (require_mxfs) {
169                                         throw;
170                                 }
171                         }
172                 }
173
174                 if ((*i)->asset_list->main_subtitle) {
175                         
176                         pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_subtitle->id);
177
178                         subtitle.reset (new SubtitleAsset (asset.first, asset.second->chunks.front()->path));
179
180                         subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
181                         subtitle->set_edit_rate (_fps);
182                         subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
183                 }
184                         
185                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
186         }
187 }
188
189 void
190 CPL::add_reel (shared_ptr<Reel> reel)
191 {
192         _reels.push_back (reel);
193 }
194
195 boost::filesystem::path
196 CPL::filename () const
197 {
198         return _directory / String::compose ("cpl_%1.xml", _id);
199 }
200
201 void
202 CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<const Signer> signer) const
203 {
204         boost::filesystem::path p = filename ();
205
206         xmlpp::Document doc;
207         xmlpp::Element* root;
208         if (interop) {
209                 root = doc.create_root_node ("CompositionPlaylist", "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#");
210         } else {
211                 root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
212         }
213
214         if (signer) {
215                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
216         }
217         
218         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
219         root->add_child("AnnotationText")->add_child_text (_name);
220         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
221         root->add_child("Issuer")->add_child_text (metadata.issuer);
222         root->add_child("Creator")->add_child_text (metadata.creator);
223         root->add_child("ContentTitleText")->add_child_text (_name);
224         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
225         {
226                 xmlpp::Node* cv = root->add_child ("ContentVersion");
227                 cv->add_child ("Id")->add_child_text ("urn:uri:" + _id + "_" + metadata.issue_date);
228                 cv->add_child ("LabelText")->add_child_text (_id + "_" + metadata.issue_date);
229         }
230         root->add_child("RatingList");
231
232         xmlpp::Element* reel_list = root->add_child ("ReelList");
233         
234         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
235                 (*i)->write_to_cpl (reel_list);
236         }
237
238         if (signer) {
239                 signer->sign (root, interop);
240         }
241
242         /* This must not be the _formatted version otherwise signature digests will be wrong */
243         doc.write_to_file (p.string (), "UTF-8");
244
245         _digest = make_digest (p.string (), 0);
246         _length = boost::filesystem::file_size (p.string ());
247 }
248
249 void
250 CPL::write_to_pkl (xmlpp::Node* node, bool interop) const
251 {
252         xmlpp::Node* asset = node->add_child ("Asset");
253         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
254         asset->add_child("Hash")->add_child_text (_digest);
255         asset->add_child("Size")->add_child_text (raw_convert<string> (_length));
256         if (interop) {
257                 asset->add_child("Type")->add_child_text ("text/xml;asdcpKind=CPL");
258         } else {
259                 asset->add_child("Type")->add_child_text ("text/xml");
260         }
261 }
262
263 list<shared_ptr<const Asset> >
264 CPL::assets () const
265 {
266         list<shared_ptr<const Asset> > a;
267         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
268                 if ((*i)->main_picture ()) {
269                         a.push_back ((*i)->main_picture ());
270                 }
271                 if ((*i)->main_sound ()) {
272                         a.push_back ((*i)->main_sound ());
273                 }
274                 if ((*i)->main_subtitle ()) {
275                         a.push_back ((*i)->main_subtitle ());
276                 }
277         }
278
279         return a;
280 }
281
282 void
283 CPL::write_to_assetmap (xmlpp::Node* node) const
284 {
285         xmlpp::Node* asset = node->add_child ("Asset");
286         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
287         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
288         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
289         chunk->add_child("Path")->add_child_text ("cpl_" + _id + ".xml");
290         chunk->add_child("VolumeIndex")->add_child_text ("1");
291         chunk->add_child("Offset")->add_child_text("0");
292         chunk->add_child("Length")->add_child_text (raw_convert<string> (_length));
293 }
294         
295         
296         
297 bool
298 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
299 {
300         if (_name != other._name && !opt.cpl_names_can_differ) {
301                 stringstream s;
302                 s << "names differ: " << _name << " vs " << other._name << "\n";
303                 note (ERROR, s.str ());
304                 return false;
305         }
306
307         if (_content_kind != other._content_kind) {
308                 note (ERROR, "content kinds differ");
309                 return false;
310         }
311
312         if (_fps != other._fps) {
313                 note (ERROR, String::compose ("frames per second differ (%1 vs %2)", _fps, other._fps));
314                 return false;
315         }
316
317         if (_length != other._length) {
318                 stringstream s;
319                 note (ERROR, String::compose ("lengths differ (%1 vs %2)", _length, other._length));
320         }
321
322         if (_reels.size() != other._reels.size()) {
323                 note (ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
324                 return false;
325         }
326         
327         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
328         list<shared_ptr<Reel> >::const_iterator b = other._reels.begin ();
329         
330         while (a != _reels.end ()) {
331                 if (!(*a)->equals (*b, opt, note)) {
332                         return false;
333                 }
334                 ++a;
335                 ++b;
336         }
337
338         return true;
339 }
340
341 /** @return true if we have any encrypted content */
342 bool
343 CPL::encrypted () const
344 {
345         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
346                 if ((*i)->encrypted ()) {
347                         return true;
348                 }
349         }
350
351         return false;
352 }
353
354 void
355 CPL::add_kdm (KDM const & kdm)
356 {
357         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
358                 (*i)->add_kdm (kdm);
359         }
360 }
361
362 pair<string, shared_ptr<const parse::AssetMapAsset> >
363 CPL::asset_from_id (list<PathAssetMap> asset_maps, string id) const
364 {
365         for (list<PathAssetMap>::const_iterator i = asset_maps.begin(); i != asset_maps.end(); ++i) {
366                 shared_ptr<parse::AssetMapAsset> a = i->second->asset_from_id (id);
367                 if (a) {
368                         return make_pair (i->first, a);
369                 }
370         }
371
372         return make_pair ("", shared_ptr<const parse::AssetMapAsset> ());
373 }
374
375 void
376 CPL::set_mxf_keys (Key key)
377 {
378         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
379                 (*i)->set_mxf_keys (key);
380         }
381 }