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