It builds again.
[libdcp.git] / src / dcp.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 /** @file  src/dcp.cc
21  *  @brief DCP class.
22  */
23
24 #include "dcp.h"
25 #include "sound_mxf.h"
26 #include "picture_mxf.h"
27 #include "subtitle_content.h"
28 #include "mono_picture_mxf.h"
29 #include "stereo_picture_mxf.h"
30 #include "util.h"
31 #include "metadata.h"
32 #include "exceptions.h"
33 #include "reel.h"
34 #include "cpl.h"
35 #include "signer.h"
36 #include "kdm.h"
37 #include "compose.hpp"
38 #include "AS_DCP.h"
39 #include <xmlsec/xmldsig.h>
40 #include <xmlsec/app.h>
41 #include <libxml++/libxml++.h>
42 #include <boost/filesystem.hpp>
43 #include <boost/lexical_cast.hpp>
44 #include <boost/algorithm/string.hpp>
45 #include <boost/lexical_cast.hpp>
46 #include <sstream>
47 #include <iomanip>
48 #include <cassert>
49 #include <iostream>
50
51 using std::string;
52 using std::list;
53 using std::stringstream;
54 using std::ostream;
55 using std::copy;
56 using std::back_inserter;
57 using std::make_pair;
58 using std::map;
59 using boost::shared_ptr;
60 using boost::lexical_cast;
61 using boost::dynamic_pointer_cast;
62 using namespace dcp;
63
64 DCP::DCP (boost::filesystem::path directory)
65         : _directory (directory)
66 {
67         boost::filesystem::create_directories (directory);
68 }
69
70 void
71 DCP::read ()
72 {
73         /* Read the ASSETMAP */
74         
75         boost::filesystem::path asset_map_file;
76         if (boost::filesystem::exists (_directory / "ASSETMAP")) {
77                 asset_map_file = _directory / "ASSETMAP";
78         } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
79                 asset_map_file = _directory / "ASSETMAP.xml";
80         } else {
81                 boost::throw_exception (DCPReadError ("could not find AssetMap file"));
82         }
83
84         cxml::Document asset_map ("AssetMap");
85         asset_map.read_file (asset_map_file);
86         list<shared_ptr<cxml::Node> > assets = asset_map.node_child("AssetList")->node_children ("Asset");
87         map<string, boost::filesystem::path> paths;
88         for (list<shared_ptr<cxml::Node> >::const_iterator i = assets.begin(); i != assets.end(); ++i) {
89                 if ((*i)->node_child("ChunkList")->node_children("Chunk").size() != 1) {
90                         boost::throw_exception (XMLError ("unsupported asset chunk count"));
91                 }
92                 paths.insert (make_pair (
93                         (*i)->string_child ("Id"),
94                         (*i)->node_child("ChunkList")->node_child("Chunk")->string_child ("Path")
95                                       ));
96         }
97
98         /* Read all the assets from the asset map */
99         
100         for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
101                 if (boost::algorithm::ends_with (i->second.string(), ".xml")) {
102                         xmlpp::DomParser* p = new xmlpp::DomParser;
103                         try {
104                                 p->parse_file (i->second.string());
105                         } catch (std::exception& e) {
106                                 delete p;
107                                 continue;
108                         }
109                         
110                         string const root = p->get_document()->get_root_node()->get_name ();
111                         delete p;
112                         
113                         if (root == "CompositionPlaylist") {
114                                 _assets.push_back (shared_ptr<CPL> (new CPL (i->second)));
115                         } else if (root == "DCSubtitle") {
116                                 _assets.push_back (shared_ptr<SubtitleContent> (new SubtitleContent (i->second)));
117                         }
118                 } else if (boost::algorithm::ends_with (i->second.string(), ".mxf")) {
119                         ASDCP::EssenceType_t type;
120                         if (ASDCP::EssenceType (i->second.string().c_str(), type) != ASDCP::RESULT_OK) {
121                                 throw DCPReadError ("Could not find essence type");
122                         }
123                         switch (type) {
124                                 case ASDCP::ESS_UNKNOWN:
125                                 case ASDCP::ESS_MPEG2_VES:
126                                         throw DCPReadError ("MPEG2 video essences are not supported");
127                                 case ASDCP::ESS_JPEG_2000:
128                                         _assets.push_back (shared_ptr<MonoPictureMXF> (new MonoPictureMXF (i->second)));
129                                         break;
130                                 case ASDCP::ESS_PCM_24b_48k:
131                                 case ASDCP::ESS_PCM_24b_96k:
132                                         _assets.push_back (shared_ptr<SoundMXF> (new SoundMXF (i->second)));
133                                         break;
134                                 case ASDCP::ESS_JPEG_2000_S:
135                                         _assets.push_back (shared_ptr<StereoPictureMXF> (new StereoPictureMXF (i->second)));
136                                         break;
137                                 default:
138                                         throw DCPReadError ("Unknown MXF essence type");
139                                 }
140                 }
141         }
142 }
143
144 bool
145 DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
146 {
147         if (_assets.size() != other._assets.size()) {
148                 note (ERROR, "Asset counts differ");
149                 return false;
150         }
151
152         list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
153         list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
154
155         while (a != _assets.end ()) {
156                 if (!(*a)->equals (*b, opt, note)) {
157                         return false;
158                 }
159                 ++a;
160                 ++b;
161         }
162
163         return true;
164 }
165
166 void
167 DCP::add (shared_ptr<Asset> asset)
168 {
169         _assets.push_back (asset);
170 }
171
172 class AssetComparator
173 {
174 public:
175         bool operator() (shared_ptr<const Content> a, shared_ptr<const Content> b) {
176                 return a->id() < b->id();
177         }
178 };
179
180 bool
181 DCP::encrypted () const
182 {
183         list<shared_ptr<CPL> > cpl = cpls ();
184         for (list<shared_ptr<CPL> >::const_iterator i = cpl.begin(); i != cpl.end(); ++i) {
185                 if ((*i)->encrypted ()) {
186                         return true;
187                 }
188         }
189
190         return false;
191 }
192
193 void
194 DCP::add (KDM const & kdm)
195 {
196         list<KDMKey> keys = kdm.keys ();
197         list<shared_ptr<CPL> > cpl = cpls ();
198         
199         for (list<shared_ptr<CPL> >::iterator i = cpl.begin(); i != cpl.end(); ++i) {
200                 for (list<KDMKey>::iterator j = keys.begin(); j != keys.end(); ++j) {
201                         if (j->cpl_id() == (*i)->id()) {
202                                 (*i)->add (kdm);
203                         }                               
204                 }
205         }
206 }
207
208 boost::filesystem::path
209 DCP::write_pkl (Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const Signer> signer) const
210 {
211         boost::filesystem::path p = _directory;
212         stringstream s;
213         s << pkl_uuid << "_pkl.xml";
214         p /= s.str();
215
216         xmlpp::Document doc;
217         xmlpp::Element* pkl;
218         if (standard == INTEROP) {
219                 pkl = doc.create_root_node("PackingList", "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#");
220         } else {
221                 pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
222         }
223         
224         if (signer) {
225                 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
226         }
227
228         pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
229
230         /* XXX: this is a bit of a hack */
231         list<shared_ptr<Asset> >::const_iterator i = _assets.begin();
232         shared_ptr<CPL> first_cpl;
233         while (i != _assets.end()) {
234                 first_cpl = dynamic_pointer_cast<CPL> (*i);
235                 if (first_cpl) {
236                         break;
237                 }
238         }
239
240         assert (first_cpl);
241         pkl->add_child("AnnotationText")->add_child_text (first_cpl->annotation_text ());
242         
243         pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
244         pkl->add_child("Issuer")->add_child_text (metadata.issuer);
245         pkl->add_child("Creator")->add_child_text (metadata.creator);
246
247         xmlpp::Element* asset_list = pkl->add_child("AssetList");
248
249         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
250                 shared_ptr<Content> c = dynamic_pointer_cast<Content> (*i);
251                 if (c) {
252                         c->write_to_pkl (asset_list);
253                 }
254         }
255
256         if (signer) {
257                 signer->sign (pkl, standard);
258         }
259                 
260         doc.write_to_file (p.string (), "UTF-8");
261         return p.string ();
262 }
263
264 /** Write the VOLINDEX file.
265  *  @param standard DCP standard to use (INTEROP or SMPTE)
266  */
267 void
268 DCP::write_volindex (Standard standard) const
269 {
270         boost::filesystem::path p = _directory;
271         if (standard == INTEROP) {
272                 p /= "VOLINDEX";
273         } else {
274                 p /= "VOLINDEX.xml";
275         }
276
277         xmlpp::Document doc;
278         xmlpp::Element* root = doc.create_root_node ("VolumeIndex", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
279         root->add_child("Index")->add_child_text ("1");
280         doc.write_to_file (p.string (), "UTF-8");
281 }
282
283 void
284 DCP::write_assetmap (Standard standard, string pkl_uuid, int pkl_length, XMLMetadata metadata) const
285 {
286         boost::filesystem::path p = _directory;
287         if (standard == INTEROP) {
288                 p /= "ASSETMAP";
289         } else {
290                 p /= "ASSETMAP.xml";
291         }
292
293         xmlpp::Document doc;
294         xmlpp::Element* root;
295         if (standard == INTEROP) {
296                 root = doc.create_root_node ("AssetMap", "http://www.digicine.com/PROTO-ASDCP-AM-20040311#");
297         } else {
298                 root = doc.create_root_node ("AssetMap", "http://www.smpte-ra.org/schemas/429-9/2007/AM");
299         }
300
301         root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
302         root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
303         if (standard == INTEROP) {
304                 root->add_child("VolumeCount")->add_child_text ("1");
305                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
306                 root->add_child("Issuer")->add_child_text (metadata.issuer);
307                 root->add_child("Creator")->add_child_text (metadata.creator);
308         } else {
309                 root->add_child("Creator")->add_child_text (metadata.creator);
310                 root->add_child("VolumeCount")->add_child_text ("1");
311                 root->add_child("IssueDate")->add_child_text (metadata.issue_date);
312                 root->add_child("Issuer")->add_child_text (metadata.issuer);
313         }
314                 
315         xmlpp::Node* asset_list = root->add_child ("AssetList");
316
317         xmlpp::Node* asset = asset_list->add_child ("Asset");
318         asset->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
319         asset->add_child("PackingList")->add_child_text ("true");
320         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
321         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
322         chunk->add_child("Path")->add_child_text (pkl_uuid + "_pkl.xml");
323         chunk->add_child("VolumeIndex")->add_child_text ("1");
324         chunk->add_child("Offset")->add_child_text ("0");
325         chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
326         
327         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
328                 (*i)->write_to_assetmap (asset_list);
329         }
330
331         /* This must not be the _formatted version otherwise signature digests will be wrong */
332         doc.write_to_file (p.string (), "UTF-8");
333 }
334
335 void
336 DCP::write_xml (
337         Standard standard,
338         XMLMetadata metadata,
339         shared_ptr<const Signer> signer
340         )
341 {
342         string const pkl_uuid = make_uuid ();
343         boost::filesystem::path const pkl_path = write_pkl (standard, pkl_uuid, metadata, signer);
344         
345         write_volindex (standard);
346         write_assetmap (standard, pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
347
348         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
349                 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);
350                 if (c) {
351                         string const filename = c->id() + "_cpl.xml";
352                         c->write_xml (_directory / filename, standard, metadata, signer);
353                 }
354         }
355 }
356
357 list<shared_ptr<CPL> >
358 DCP::cpls () const
359 {
360         list<shared_ptr<CPL> > cpls;
361         for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
362                 shared_ptr<CPL> c = dynamic_pointer_cast<CPL> (*i);
363                 if (c) {
364                         cpls.push_back (c);
365                 }
366         }
367
368         return cpls;
369 }
370
371