Report failures to read resources from MXF files.
[libdcp.git] / src / smpte_subtitle_asset.cc
index f09d99f37ff21e474fc913704c55b17a1ef87809..b9d7e9a341d25fbf8fe396edaeb2d391e8e4feeb 100644 (file)
@@ -40,6 +40,7 @@
 #include "compose.hpp"
 #include "crypto_context.h"
 #include "dcp_assert.h"
+#include "equality_options.h"
 #include "exceptions.h"
 #include "raw_convert.h"
 #include "smpte_load_font_node.h"
@@ -72,13 +73,16 @@ using boost::starts_with;
 using namespace dcp;
 
 
-static string const subtitle_smpte_ns = "http://www.smpte-ra.org/schemas/428-7/2010/DCST";
+static string const subtitle_smpte_ns_2007 = "http://www.smpte-ra.org/schemas/428-7/2007/DCST";
+static string const subtitle_smpte_ns_2010 = "http://www.smpte-ra.org/schemas/428-7/2010/DCST";
+static string const subtitle_smpte_ns_2014 = "http://www.smpte-ra.org/schemas/428-7/2014/DCST";
 
 
-SMPTESubtitleAsset::SMPTESubtitleAsset ()
-       : MXF (Standard::SMPTE)
+SMPTESubtitleAsset::SMPTESubtitleAsset(SubtitleStandard standard)
+       : MXF(Standard::SMPTE)
        , _edit_rate (24, 1)
        , _time_code_rate (24)
+       , _subtitle_standard(standard)
        , _xml_id (make_uuid())
 {
 
@@ -109,7 +113,7 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
                        xml->read_string (xml_string);
                        parse_xml (xml);
                        read_mxf_descriptor (reader);
-                       read_mxf_resources (reader, make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
+                       read_mxf_resources(reader, std::make_shared<DecryptionContext>(optional<Key>(), Standard::SMPTE));
                } else {
                        read_mxf_descriptor (reader);
                }
@@ -165,6 +169,15 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
 void
 SMPTESubtitleAsset::parse_xml (shared_ptr<cxml::Document> xml)
 {
+       if (xml->namespace_uri() == subtitle_smpte_ns_2007) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2007;
+       } else if (xml->namespace_uri() == subtitle_smpte_ns_2010) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2010;
+       } else if (xml->namespace_uri() == subtitle_smpte_ns_2014) {
+               _subtitle_standard = SubtitleStandard::SMPTE_2014;
+       } else {
+               throw XMLError("Unrecognised subtitle namespace " + xml->namespace_uri());
+       }
        _xml_id = remove_urn_uuid(xml->string_child("Id"));
        _load_font_nodes = type_children<dcp::SMPTELoadFontNode> (xml, "LoadFont");
 
@@ -220,15 +233,22 @@ 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));
 
-               shared_array<uint8_t> data (new uint8_t[buffer.Size()]);
-               memcpy (data.get(), buffer.RoData(), buffer.Size());
-
                switch (i->Type) {
                case ASDCP::TimedText::MT_OPENTYPE:
                {
@@ -238,7 +258,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _load_font_nodes.end ()) {
-                               _fonts.push_back (Font ((*j)->id, (*j)->urn, ArrayData (data, buffer.Size ())));
+                               _fonts.push_back(Font((*j)->id, (*j)->urn, ArrayData(buffer.RoData(), buffer.Size())));
                        }
                        break;
                }
@@ -250,7 +270,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _subtitles.end()) {
-                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (ArrayData(data, buffer.Size()));
+                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image(ArrayData(buffer.RoData(), buffer.Size()));
                        }
                        break;
                }
@@ -344,8 +364,6 @@ SMPTESubtitleAsset::xml_as_string () const
 {
        xmlpp::Document doc;
        auto root = doc.create_root_node ("SubtitleReel");
-       root->set_namespace_declaration (subtitle_smpte_ns);
-       root->set_namespace_declaration ("http://www.w3.org/2001/XMLSchema", "xs");
 
        DCP_ASSERT (_xml_id);
        root->add_child("Id")->add_child_text("urn:uuid:" + *_xml_id);
@@ -353,7 +371,7 @@ SMPTESubtitleAsset::xml_as_string () const
        if (_annotation_text) {
                root->add_child("AnnotationText")->add_child_text(_annotation_text.get());
        }
-       root->add_child("IssueDate")->add_child_text(_issue_date.as_string(true));
+       root->add_child("IssueDate")->add_child_text(_issue_date.as_string(false, false));
        if (_reel_number) {
                root->add_child("ReelNumber")->add_child_text(raw_convert<string>(_reel_number.get()));
        }
@@ -374,7 +392,7 @@ SMPTESubtitleAsset::xml_as_string () const
 
        subtitles_as_xml (root->add_child("SubtitleList"), _time_code_rate, Standard::SMPTE);
 
-       return doc.write_to_string ("UTF-8");
+       return format_xml(doc, std::make_pair(string{}, schema_namespace()));
 }
 
 
@@ -421,7 +439,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
        }
 
-       descriptor.NamespaceName = subtitle_smpte_ns;
+       descriptor.NamespaceName = schema_namespace();
        unsigned int c;
        DCP_ASSERT (_xml_id);
        Kumu::hex2bin (_xml_id->c_str(), descriptor.AssetID, ASDCP::UUIDlen, &c);
@@ -484,7 +502,7 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
 }
 
 bool
-SMPTESubtitleAsset::equals (shared_ptr<const Asset> other_asset, EqualityOptions options, NoteHandler note) const
+SMPTESubtitleAsset::equals(shared_ptr<const Asset> other_asset, EqualityOptions const& options, NoteHandler note) const
 {
        if (!SubtitleAsset::equals (other_asset, options, note)) {
                return false;
@@ -578,3 +596,20 @@ SMPTESubtitleAsset::add (shared_ptr<Subtitle> s)
        _intrinsic_duration = latest_subtitle_out().as_editable_units_ceil(_edit_rate.numerator / _edit_rate.denominator);
 }
 
+
+string
+SMPTESubtitleAsset::schema_namespace() const
+{
+       switch (_subtitle_standard) {
+       case SubtitleStandard::SMPTE_2007:
+               return subtitle_smpte_ns_2007;
+       case SubtitleStandard::SMPTE_2010:
+               return subtitle_smpte_ns_2010;
+       case SubtitleStandard::SMPTE_2014:
+               return subtitle_smpte_ns_2014;
+       default:
+               DCP_ASSERT(false);
+       }
+
+       DCP_ASSERT(false);
+}