Attempt to fix Sony digest validation by indenting the <Signer>
[libdcp.git] / src / dcp.cc
index 8aff6c407f512f94faa56ffaee33a69dfc3bb651..5d908530edc43561f5e8d1231a1f4837fbd6c4b4 100644 (file)
@@ -38,6 +38,7 @@
 #include "raw_convert.h"
 #include "dcp.h"
 #include "sound_asset.h"
+#include "atmos_asset.h"
 #include "picture_asset.h"
 #include "interop_subtitle_asset.h"
 #include "smpte_subtitle_asset.h"
@@ -55,6 +56,8 @@
 #include "dcp_assert.h"
 #include "reel_asset.h"
 #include "font_asset.h"
+#include "pkl.h"
+#include "asset_factory.h"
 #include <asdcp/AS_DCP.h>
 #include <xmlsec/xmldsig.h>
 #include <xmlsec/app.h>
 
 using std::string;
 using std::list;
+using std::vector;
 using std::cout;
 using std::make_pair;
 using std::map;
-using std::cout;
 using std::cerr;
 using std::exception;
 using boost::shared_ptr;
 using boost::dynamic_pointer_cast;
+using boost::optional;
 using boost::algorithm::starts_with;
 using namespace dcp;
 
 static string const assetmap_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
 static string const assetmap_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
-static string const pkl_interop_ns      = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
-static string const pkl_smpte_ns        = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
-static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-AM-20040311#";
+static string const volindex_interop_ns = "http://www.digicine.com/PROTO-ASDCP-VL-20040311#";
 static string const volindex_smpte_ns   = "http://www.smpte-ra.org/schemas/429-9/2007/AM";
 
 DCP::DCP (boost::filesystem::path directory)
@@ -109,7 +111,7 @@ survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
 void
 DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
 {
-       /* Read the ASSETMAP */
+       /* Read the ASSETMAP and PKL */
 
        boost::filesystem::path asset_map_file;
        if (boost::filesystem::exists (_directory / "ASSETMAP")) {
@@ -133,6 +135,7 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
 
        list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
        map<string, boost::filesystem::path> paths;
+       list<boost::filesystem::path> pkl_paths;
        BOOST_FOREACH (shared_ptr<cxml::Node> i, asset_nodes) {
                if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
                        boost::throw_exception (XMLError ("unsupported asset chunk count"));
@@ -141,15 +144,38 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                if (starts_with (p, "file://")) {
                        p = p.substr (7);
                }
-               paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+               switch (*_standard) {
+               case INTEROP:
+                       if (i->optional_node_child("PackingList")) {
+                               pkl_paths.push_back (p);
+                       } else {
+                               paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+                       }
+                       break;
+               case SMPTE:
+               {
+                       optional<string> pkl_bool = i->optional_string_child("PackingList");
+                       if (pkl_bool && *pkl_bool == "true") {
+                               pkl_paths.push_back (p);
+                       } else {
+                               paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+                       }
+                       break;
+               }
+               }
+       }
+
+       if (pkl_paths.empty()) {
+               boost::throw_exception (XMLError ("No packing lists found in asset map"));
+       }
+
+       BOOST_FOREACH (boost::filesystem::path i, pkl_paths) {
+               _pkls.push_back (shared_ptr<PKL>(new PKL(_directory / i)));
        }
 
        /* Read all the assets from the asset map */
-       /* XXX: I think we should be looking at the PKL here to decide type, not
-          the extension of the file.
-       */
 
-       /* Make a list of non-CPL assets so that we can resolve the references
+       /* Make a list of non-CPL/PKL assets so that we can resolve the references
           from the CPLs.
        */
        list<shared_ptr<Asset> > other_assets;
@@ -162,13 +188,23 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                        continue;
                }
 
-               if (boost::filesystem::extension (path) == ".xml") {
+               optional<string> pkl_type;
+               BOOST_FOREACH (shared_ptr<PKL> j, _pkls) {
+                       pkl_type = j->type(i->first);
+                       if (pkl_type) {
+                               break;
+                       }
+               }
+
+               DCP_ASSERT (pkl_type);
+
+               if (*pkl_type == CPL::static_pkl_type(*_standard) || *pkl_type == InteropSubtitleAsset::static_pkl_type(*_standard)) {
                        xmlpp::DomParser* p = new xmlpp::DomParser;
                        try {
                                p->parse_file (path.string());
                        } catch (std::exception& e) {
                                delete p;
-                               continue;
+                               throw DCPReadError(String::compose("XML error in %1", path.string()), e.what());
                        }
 
                        string const root = p->get_document()->get_root_node()->get_name ();
@@ -186,47 +222,20 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                                }
                                other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
                        }
-               } else if (boost::filesystem::extension (path) == ".mxf") {
-
-                       /* XXX: asdcplib does not appear to support discovery of read MXFs standard
-                          (Interop / SMPTE)
-                       */
-
-                       ASDCP::EssenceType_t type;
-                       if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
-                               throw DCPReadError ("Could not find essence type");
-                       }
-                       switch (type) {
-                               case ASDCP::ESS_UNKNOWN:
-                               case ASDCP::ESS_MPEG2_VES:
-                                       throw DCPReadError ("MPEG2 video essences are not supported");
-                               case ASDCP::ESS_JPEG_2000:
-                                       try {
-                                               other_assets.push_back (shared_ptr<MonoPictureAsset> (new MonoPictureAsset (path)));
-                                       } catch (dcp::MXFFileError& e) {
-                                               if (ignore_incorrect_picture_mxf_type && e.number() == ASDCP::RESULT_SFORMAT) {
-                                                       /* Tried to load it as mono but the error says it's stereo; try that instead */
-                                                       other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
-                                               } else {
-                                                       throw;
-                                               }
-                                       }
-                                       break;
-                               case ASDCP::ESS_PCM_24b_48k:
-                               case ASDCP::ESS_PCM_24b_96k:
-                                       other_assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (path)));
-                                       break;
-                               case ASDCP::ESS_JPEG_2000_S:
-                                       other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
-                                       break;
-                               case ASDCP::ESS_TIMED_TEXT:
-                                       other_assets.push_back (shared_ptr<SMPTESubtitleAsset> (new SMPTESubtitleAsset (path)));
-                                       break;
-                               default:
-                                       throw DCPReadError ("Unknown MXF essence type");
-                       }
-               } else if (boost::filesystem::extension (path) == ".ttf") {
+               } else if (
+                       *pkl_type == PictureAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == SoundAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == AtmosAsset::static_pkl_type(*_standard) ||
+                       *pkl_type == SMPTESubtitleAsset::static_pkl_type(*_standard)
+                       ) {
+
+                       other_assets.push_back (asset_factory(path, ignore_incorrect_picture_mxf_type));
+               } else if (*pkl_type == FontAsset::static_pkl_type(*_standard)) {
                        other_assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->first, path)));
+               } else if (*pkl_type == "image/png") {
+                       /* It's an Interop PNG subtitle; let it go */
+               } else {
+                       throw DCPReadError (String::compose("Unknown asset type %1 in PKL", *pkl_type));
                }
        }
 
@@ -288,6 +297,10 @@ DCP::encrypted () const
        return false;
 }
 
+/** Add a KDM to decrypt this DCP.  This method must be called after DCP::read()
+ *  or the KDM you specify will be ignored.
+ *  @param kdm KDM to use.
+ */
 void
 DCP::add (DecryptedKDM const & kdm)
 {
@@ -302,48 +315,6 @@ DCP::add (DecryptedKDM const & kdm)
        }
 }
 
-/** @return full pathname of PKL file that was written */
-boost::filesystem::path
-DCP::write_pkl (string file, Standard standard, string pkl_uuid, XMLMetadata metadata, shared_ptr<const CertificateChain> signer) const
-{
-       boost::filesystem::path p = _directory;
-       p /= file;
-
-       xmlpp::Document doc;
-       xmlpp::Element* pkl;
-       if (standard == INTEROP) {
-               pkl = doc.create_root_node("PackingList", pkl_interop_ns);
-       } else {
-               pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
-       }
-
-       if (signer) {
-               pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
-       }
-
-       pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
-
-       /* XXX: this is a bit of a hack */
-       DCP_ASSERT (cpls().size() > 0);
-       pkl->add_child("AnnotationText")->add_child_text (cpls().front()->annotation_text ());
-
-       pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
-       pkl->add_child("Issuer")->add_child_text (metadata.issuer);
-       pkl->add_child("Creator")->add_child_text (metadata.creator);
-
-       xmlpp::Element* asset_list = pkl->add_child("AssetList");
-       BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
-               i->write_to_pkl (asset_list, _directory, standard);
-       }
-
-       if (signer) {
-               signer->sign (pkl, standard);
-       }
-
-       doc.write_to_file (p.string (), "UTF-8");
-       return p.string ();
-}
-
 /** Write the VOLINDEX file.
  *  @param standard DCP standard to use (INTEROP or SMPTE)
  */
@@ -377,7 +348,7 @@ DCP::write_volindex (Standard standard) const
        }
 
        root->add_child("Index")->add_child_text ("1");
-       doc.write_to_file (p.string (), "UTF-8");
+       doc.write_to_file_formatted (p.string (), "UTF-8");
 }
 
 void
@@ -411,7 +382,7 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, boost::filesystem::path
        }
 
        root->add_child("Id")->add_child_text ("urn:uuid:" + make_uuid());
-       root->add_child("AnnotationText")->add_child_text ("Created by " + metadata.creator);
+       root->add_child("AnnotationText")->add_child_text (metadata.annotation_text);
 
        switch (standard) {
        case INTEROP:
@@ -446,8 +417,7 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, boost::filesystem::path
                i->write_to_assetmap (asset_list, _directory);
        }
 
-       /* This must not be the _formatted version otherwise signature digests will be wrong */
-       doc.write_to_file (p.string (), "UTF-8");
+       doc.write_to_file_formatted (p.string (), "UTF-8");
 }
 
 /** Write all the XML files for this DCP.
@@ -466,18 +436,28 @@ DCP::write_xml (
        BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
                NameFormat::Map values;
                values['t'] = "cpl";
-               values['i'] = i->id();
-               i->write_xml (_directory / (name_format.get(values) + ".xml"), standard, signer);
+               i->write_xml (_directory / (name_format.get(values, "_" + i->id() + ".xml")), standard, signer);
+       }
+
+       shared_ptr<PKL> pkl;
+
+       if (_pkls.empty()) {
+               pkl.reset (new PKL (standard, metadata.annotation_text, metadata.issue_date, metadata.issuer, metadata.creator));
+               _pkls.push_back (pkl);
+               BOOST_FOREACH (shared_ptr<Asset> i, assets ()) {
+                       i->add_to_pkl (pkl, _directory);
+               }
+        } else {
+               pkl = _pkls.front ();
        }
 
-       string const pkl_uuid = make_uuid ();
        NameFormat::Map values;
        values['t'] = "pkl";
-       values['i'] = pkl_uuid;
-       boost::filesystem::path const pkl_path = write_pkl (name_format.get(values) + ".xml", standard, pkl_uuid, metadata, signer);
+       boost::filesystem::path pkl_path = _directory / name_format.get(values, "_" + pkl->id() + ".xml");
+       pkl->write (pkl_path, signer);
 
        write_volindex (standard);
-       write_assetmap (standard, pkl_uuid, pkl_path, metadata);
+       write_assetmap (standard, pkl->id(), pkl_path, metadata);
 }
 
 list<shared_ptr<CPL> >
@@ -486,14 +466,20 @@ DCP::cpls () const
        return _cpls;
 }
 
-/** @return All assets (including CPLs) */
+/** @param ignore_unresolved true to silently ignore unresolved assets, otherwise
+ *  an exception is thrown if they are found.
+ *  @return All assets (including CPLs).
+ */
 list<shared_ptr<Asset> >
-DCP::assets () const
+DCP::assets (bool ignore_unresolved) const
 {
        list<shared_ptr<Asset> > assets;
        BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
                assets.push_back (i);
                BOOST_FOREACH (shared_ptr<const ReelAsset> j, i->reel_assets ()) {
+                       if (ignore_unresolved && !j->asset_ref().resolved()) {
+                               continue;
+                       }
                        shared_ptr<Asset> o = j->asset_ref().asset ();
                        assets.push_back (o);
                        /* More Interop special-casing */
@@ -506,3 +492,16 @@ DCP::assets () const
 
        return assets;
 }
+
+/** Given a list of files that make up 1 or more DCPs, return the DCP directories */
+vector<boost::filesystem::path>
+DCP::directories_from_files (vector<boost::filesystem::path> files)
+{
+       vector<boost::filesystem::path> d;
+       BOOST_FOREACH (boost::filesystem::path i, files) {
+               if (i.filename() == "ASSETMAP" || i.filename() == "ASSETMAP.xml") {
+                       d.push_back (i.parent_path ());
+               }
+       }
+       return d;
+}