Add OK note when PKL and CPL annotation texts match.
[libdcp.git] / src / smpte_subtitle_asset.cc
index b9d7e9a341d25fbf8fe396edaeb2d391e8e4feeb..4f611583b36c0db47ac19a3ecc03f12b19bea48d 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"
@@ -94,11 +95,12 @@ SMPTESubtitleAsset::SMPTESubtitleAsset (boost::filesystem::path file)
 {
        auto xml = make_shared<cxml::Document>("SubtitleReel");
 
-       auto reader = make_shared<ASDCP::TimedText::MXFReader>();
+       Kumu::FileReaderFactory factory;
+       auto reader = make_shared<ASDCP::TimedText::MXFReader>(factory);
        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 +124,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 +145,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);
                                        }
                                }
@@ -305,10 +307,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.
@@ -318,8 +321,9 @@ 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 ());
+       Kumu::FileReaderFactory factory;
+       auto reader = make_shared<ASDCP::TimedText::MXFReader>(factory);
+       auto r = reader->OpenRead(dcp::filesystem::fix_long_path(*_file).string().c_str());
        if (ASDCP_FAILURE (r)) {
                boost::throw_exception (
                        ReadError (
@@ -335,6 +339,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);
 }
 
@@ -351,9 +356,10 @@ SMPTESubtitleAsset::load_font_nodes () const
 bool
 SMPTESubtitleAsset::valid_mxf (boost::filesystem::path file)
 {
-       ASDCP::TimedText::MXFReader reader;
+       Kumu::FileReaderFactory factory;
+       ASDCP::TimedText::MXFReader reader(factory);
        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);
 }
@@ -366,31 +372,31 @@ SMPTESubtitleAsset::xml_as_string () const
        auto root = doc.create_root_node ("SubtitleReel");
 
        DCP_ASSERT (_xml_id);
-       root->add_child("Id")->add_child_text("urn:uuid:" + *_xml_id);
-       root->add_child("ContentTitleText")->add_child_text(_content_title_text);
+       cxml::add_text_child(root, "Id", "urn:uuid:" + *_xml_id);
+       cxml::add_text_child(root, "ContentTitleText", _content_title_text);
        if (_annotation_text) {
-               root->add_child("AnnotationText")->add_child_text(_annotation_text.get());
+               cxml::add_text_child(root, "AnnotationText", _annotation_text.get());
        }
-       root->add_child("IssueDate")->add_child_text(_issue_date.as_string(false, false));
+       cxml::add_text_child(root, "IssueDate", _issue_date.as_string(false, false));
        if (_reel_number) {
-               root->add_child("ReelNumber")->add_child_text(raw_convert<string>(_reel_number.get()));
+               cxml::add_text_child(root, "ReelNumber", raw_convert<string>(_reel_number.get()));
        }
        if (_language) {
-               root->add_child("Language")->add_child_text(_language.get());
+               cxml::add_text_child(root, "Language", _language.get());
        }
-       root->add_child("EditRate")->add_child_text(_edit_rate.as_string());
-       root->add_child("TimeCodeRate")->add_child_text(raw_convert<string>(_time_code_rate));
+       cxml::add_text_child(root, "EditRate", _edit_rate.as_string());
+       cxml::add_text_child(root, "TimeCodeRate", raw_convert<string>(_time_code_rate));
        if (_start_time) {
-               root->add_child("StartTime")->add_child_text(_start_time.get().as_string(Standard::SMPTE));
+               cxml::add_text_child(root, "StartTime", _start_time.get().as_string(Standard::SMPTE));
        }
 
        for (auto i: _load_font_nodes) {
-               auto load_font = root->add_child("LoadFont");
+               auto load_font = cxml::add_child(root, "LoadFont");
                load_font->add_child_text ("urn:uuid:" + i->urn);
                load_font->set_attribute ("ID", i->id);
        }
 
-       subtitles_as_xml (root->add_child("SubtitleList"), _time_code_rate, Standard::SMPTE);
+       subtitles_as_xml(cxml::add_child(root, "SubtitleList"), _time_code_rate, Standard::SMPTE);
 
        return format_xml(doc, std::make_pair(string{}, schema_namespace()));
 }
@@ -450,7 +456,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));
        }