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