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