Use the <Id> read in when the Reel was created from XML (if appropriate)
[libdcp.git] / src / dcp.cc
index 52598610a808defa0031f8379e75c4cc89b01dc5..2929b5d85bf81d45c2cdec4e92c10d6a60446e83 100644 (file)
@@ -58,6 +58,7 @@
 #include "font_asset.h"
 #include "pkl.h"
 #include "asset_factory.h"
+#include "verify.h"
 #include <asdcp/AS_DCP.h>
 #include <xmlsec/xmldsig.h>
 #include <xmlsec/app.h>
@@ -95,36 +96,33 @@ DCP::DCP (boost::filesystem::path directory)
        _directory = boost::filesystem::canonical (_directory);
 }
 
-/** Call this instead of throwing an exception if the error can be tolerated */
-template<class T> void
-survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
-{
-       if (keep_going) {
-               if (errors) {
-                       errors->push_back (shared_ptr<T> (new T (e)));
-               }
-       } else {
-               throw e;
-       }
-}
-
+/** Read a DCP.  This method does not do any deep checking of the DCP's validity, but
+ *  if it comes across any bad things it will do one of two things.
+ *
+ *  Errors that are so serious that they prevent the method from working will result
+ *  in an exception being thrown.  For example, a missing ASSETMAP means that the DCP
+ *  can't be read without a lot of guesswork, so this will throw.
+ *
+ *  Errors that are not fatal will be added to notes, if it's non-0.  For example,
+ *  if the DCP contains a mixture of Interop and SMPTE elements this will result
+ *  in a note being added to the list.
+ */
 void
-DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
+DCP::read (list<dcp::VerificationNote>* notes, bool ignore_incorrect_picture_mxf_type)
 {
        /* Read the ASSETMAP and PKL */
 
-       boost::filesystem::path asset_map_file;
        if (boost::filesystem::exists (_directory / "ASSETMAP")) {
-               asset_map_file = _directory / "ASSETMAP";
+               _asset_map = _directory / "ASSETMAP";
        } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
-               asset_map_file = _directory / "ASSETMAP.xml";
+               _asset_map = _directory / "ASSETMAP.xml";
        } else {
-               boost::throw_exception (DCPReadError (String::compose ("could not find AssetMap file in `%1'", _directory.string())));
+               boost::throw_exception (ReadError (String::compose ("could not find ASSETMAP nor ASSETMAP.xml in `%1'", _directory.string())));
        }
 
        cxml::Document asset_map ("AssetMap");
 
-       asset_map.read_file (asset_map_file);
+       asset_map.read_file (_asset_map.get());
        if (asset_map.namespace_uri() == assetmap_interop_ns) {
                _standard = INTEROP;
        } else if (asset_map.namespace_uri() == assetmap_smpte_ns) {
@@ -173,7 +171,12 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                _pkls.push_back (shared_ptr<PKL>(new PKL(_directory / i)));
        }
 
-       /* Read all the assets from the asset map */
+       /* Now we have:
+            paths - map of files in the DCP that are not PKLs; key is ID, value is path.
+            _pkls - PKL objects for each PKL.
+
+          Read all the assets from the asset map.
+        */
 
        /* Make a list of non-CPL/PKL assets so that we can resolve the references
           from the CPLs.
@@ -183,11 +186,25 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
        for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
                boost::filesystem::path path = _directory / i->second;
 
-               if (!boost::filesystem::exists (path)) {
-                       survivable_error (keep_going, errors, MissingAssetError (path));
+               if (i->second.empty()) {
+                       /* I can't see how this is valid, but it's
+                          been seen in the wild with a DCP that
+                          claims to come from ClipsterDCI 5.10.0.5.
+                       */
+                       if (notes) {
+                               notes->push_back (VerificationNote(VerificationNote::VERIFY_WARNING, VerificationNote::EMPTY_ASSET_PATH));
+                       }
+                       continue;
+               }
+
+               if (!boost::filesystem::exists(path)) {
+                       if (notes) {
+                               notes->push_back (VerificationNote(VerificationNote::VERIFY_ERROR, VerificationNote::MISSING_ASSET, path));
+                       }
                        continue;
                }
 
+               /* Find the <Type> for this asset from the PKL that contains the asset */
                optional<string> pkl_type;
                BOOST_FOREACH (shared_ptr<PKL> j, _pkls) {
                        pkl_type = j->type(i->first);
@@ -204,7 +221,7 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                                p->parse_file (path.string());
                        } catch (std::exception& e) {
                                delete p;
-                               throw DCPReadError(String::compose("XML error in %1", path.string()), e.what());
+                               throw ReadError(String::compose("XML error in %1", path.string()), e.what());
                        }
 
                        string const root = p->get_document()->get_root_node()->get_name ();
@@ -212,13 +229,13 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
 
                        if (root == "CompositionPlaylist") {
                                shared_ptr<CPL> cpl (new CPL (path));
-                               if (_standard && cpl->standard() && cpl->standard().get() != _standard.get()) {
-                                       survivable_error (keep_going, errors, MismatchedStandardError ());
+                               if (_standard && cpl->standard() && cpl->standard().get() != _standard.get() && notes) {
+                                       notes->push_back (VerificationNote(VerificationNote::VERIFY_ERROR, VerificationNote::MISMATCHED_STANDARD));
                                }
                                _cpls.push_back (cpl);
                        } else if (root == "DCSubtitle") {
                                if (_standard && _standard.get() == SMPTE) {
-                                       survivable_error (keep_going, errors, MismatchedStandardError ());
+                                       notes->push_back (VerificationNote(VerificationNote::VERIFY_ERROR, VerificationNote::MISMATCHED_STANDARD));
                                }
                                other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
                        }
@@ -235,12 +252,21 @@ DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mx
                } 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));
+                       throw ReadError (String::compose("Unknown asset type %1 in PKL", *pkl_type));
                }
        }
 
-       BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
-               i->resolve_refs (other_assets);
+       resolve_refs (other_assets);
+
+       /* While we've got the ASSETMAP lets look and see if this DCP refers to things that are not in its ASSETMAP */
+       if (notes) {
+               BOOST_FOREACH (shared_ptr<CPL> i, cpls()) {
+                       BOOST_FOREACH (shared_ptr<const ReelMXF> j, i->reel_mxfs()) {
+                               if (!j->asset_ref().resolved() && paths.find(j->asset_ref().id()) == paths.end()) {
+                                       notes->push_back (VerificationNote(VerificationNote::VERIFY_WARNING, VerificationNote::EXTERNAL_ASSET));
+                               }
+                       }
+               }
        }
 }
 
@@ -418,6 +444,7 @@ DCP::write_assetmap (Standard standard, string pkl_uuid, boost::filesystem::path
        }
 
        doc.write_to_file_formatted (p.string (), "UTF-8");
+       _asset_map = p;
 }
 
 /** Write all the XML files for this DCP.