Re-read MXF descriptor after adding a key to a SMPTE subtitle asset (DoM #2660).
[libdcp.git] / src / smpte_subtitle_asset.cc
index 7e2bcb73abda0c42998cf42a81712b24dccd8ef5..0ff1d7ef5048996e7861e2bd7c02e453e68d66de 100644 (file)
@@ -42,6 +42,7 @@
 #include "dcp_assert.h"
 #include "equality_options.h"
 #include "exceptions.h"
+#include "filesystem.h"
 #include "raw_convert.h"
 #include "smpte_load_font_node.h"
 #include "smpte_subtitle_asset.h"
@@ -98,7 +99,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
        auto r = Kumu::RESULT_OK;
        {
                ASDCPErrorSuspender sus;
-               r = reader->OpenRead (_file->string().c_str ());
+               r = reader->OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
        }
        if (!ASDCP_FAILURE(r)) {
                /* MXF-wrapped */
@@ -122,7 +123,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                try {
                        _raw_xml = dcp::file_to_string (file);
                        xml = make_shared<cxml::Document>("SubtitleReel");
-                       xml->read_file (file);
+                       xml->read_file(dcp::filesystem::fix_long_path(file));
                        parse_xml (xml);
                } catch (cxml::Error& e) {
                        boost::throw_exception (
@@ -143,11 +144,11 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                        if (im && im->png_image().size() == 0) {
                                /* Even more dubious; allow <id>.png or urn:uuid:<id>.png */
                                auto p = file.parent_path() / String::compose("%1.png", im->id());
-                               if (boost::filesystem::is_regular_file(p)) {
+                               if (filesystem::is_regular_file(p)) {
                                        im->read_png_file (p);
                                } else if (starts_with (im->id(), "urn:uuid:")) {
                                        p = file.parent_path() / String::compose("%1.png", remove_urn_uuid(im->id()));
-                                       if (boost::filesystem::is_regular_file(p)) {
+                                       if (filesystem::is_regular_file(p)) {
                                                im->read_png_file (p);
                                        }
                                }
@@ -233,8 +234,18 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                ++i) {
 
                ASDCP::TimedText::FrameBuffer buffer;
-               buffer.Capacity (10 * 1024 * 1024);
-               reader->ReadAncillaryResource (i->ResourceID, buffer, dec->context(), dec->hmac());
+               buffer.Capacity(32 * 1024 * 1024);
+               auto const result = reader->ReadAncillaryResource(i->ResourceID, buffer, dec->context(), dec->hmac());
+               if (ASDCP_FAILURE(result)) {
+                       switch (i->Type) {
+                       case ASDCP::TimedText::MT_OPENTYPE:
+                               throw ReadError(String::compose("Could not read font from MXF file (%1)", static_cast<int>(result)));
+                       case ASDCP::TimedText::MT_PNG:
+                               throw ReadError(String::compose("Could not read subtitle image from MXF file (%1)", static_cast<int>(result)));
+                       default:
+                               throw ReadError(String::compose("Could not read resource from MXF file (%1)", static_cast<int>(result)));
+                       }
+               }
 
                char id[64];
                Kumu::bin2UUIDhex (i->ResourceID, ASDCP::UUIDlen, id, sizeof(id));
@@ -295,10 +306,11 @@ SMPTESubtitleAsset::set_key (Key key)
           have read that file.
        */
        auto const had_key = static_cast<bool>(_key);
+       auto const had_key_id = static_cast<bool>(_key_id);
 
        MXF::set_key (key);
 
-       if (!_key_id || !_file || had_key) {
+       if (!had_key_id || !_file || had_key) {
                /* Either we don't have any data to read, it wasn't
                   encrypted, or we've already read it, so we don't
                   need to do anything else.
@@ -309,7 +321,7 @@ SMPTESubtitleAsset::set_key (Key key)
        /* Our data was encrypted; now we can decrypt it */
 
        auto reader = make_shared<ASDCP::TimedText::MXFReader>();
-       auto r = reader->OpenRead (_file->string().c_str ());
+       auto r = reader->OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (
                        ReadError (
@@ -325,6 +337,7 @@ SMPTESubtitleAsset::set_key (Key key)
        auto xml = make_shared<cxml::Document>("SubtitleReel");
        xml->read_string (xml_string);
        parse_xml (xml);
+       read_mxf_descriptor(reader);
        read_mxf_resources (reader, dec);
 }
 
@@ -343,7 +356,7 @@ SMPTESubtitleAsset::valid_mxf (boost::filesystem::path file)
 {
        ASDCP::TimedText::MXFReader reader;
        Kumu::DefaultLogSink().UnsetFilterFlag(Kumu::LOG_ALLOW_ALL);
-       auto r = reader.OpenRead (file.string().c_str ());
+       auto r = reader.OpenRead(dcp::filesystem::fix_long_path(file).string().c_str());
        Kumu::DefaultLogSink().SetFilterFlag(Kumu::LOG_ALLOW_ALL);
        return !ASDCP_FAILURE (r);
 }
@@ -440,7 +453,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
        /* This header size is a guess.  Empirically it seems that each subtitle reference is 90 bytes, and we need some extra.
           The default size is not enough for some feature-length PNG sub projects (see DCP-o-matic #1561).
        */
-       ASDCP::Result_t r = writer.OpenWrite (p.string().c_str(), writer_info, descriptor, _subtitles.size() * 90 + 16384);
+       ASDCP::Result_t r = writer.OpenWrite(dcp::filesystem::fix_long_path(p).string().c_str(), writer_info, descriptor, _subtitles.size() * 90 + 16384);
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (FileError ("could not open subtitle MXF for writing", p.string(), r));
        }
@@ -603,4 +616,3 @@ SMPTESubtitleAsset::schema_namespace() const
 
        DCP_ASSERT(false);
 }
-