Bv2.1 6.2.1: Check that the sound MXF Language tag conforms to RFC 5646.
[libdcp.git] / test / test.cc
index 60dabcd4ab27737515bfce87514d9dad13cd3ddb..c8f41d053d4132d89d2297cdc480c4ebbd0220fc 100644 (file)
 #include "sound_asset.h"
 #include "sound_asset_writer.h"
 #include "smpte_subtitle_asset.h"
+#include "mono_picture_asset.h"
+#include "openjpeg_image.h"
+#include "j2k.h"
+#include "picture_asset_writer.h"
+#include "reel_mono_picture_asset.h"
+#include "reel_asset.h"
 #include "test.h"
 #include "util.h"
 #include <asdcp/KM_util.h>
 using std::string;
 using std::min;
 using std::list;
-using boost::shared_ptr;
+using std::vector;
+using std::shared_ptr;
 using boost::optional;
 
+
 boost::filesystem::path private_test;
 boost::filesystem::path xsd_test = "build/test/xsd with spaces";
 
+
 struct TestConfig
 {
        TestConfig()
@@ -87,52 +96,84 @@ struct TestConfig
 };
 
 void
-check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
+check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore_tags, bool ignore_whitespace)
 {
        BOOST_CHECK_EQUAL (ref->get_name (), test->get_name ());
        BOOST_CHECK_EQUAL (ref->get_namespace_prefix (), test->get_namespace_prefix ());
 
-       if (find (ignore.begin(), ignore.end(), ref->get_name()) != ignore.end ()) {
+       if (find(ignore_tags.begin(), ignore_tags.end(), ref->get_name()) != ignore_tags.end()) {
                return;
        }
 
-       xmlpp::Element::NodeList ref_children = ref->get_children ();
-       xmlpp::Element::NodeList test_children = test->get_children ();
-       BOOST_REQUIRE_MESSAGE (
-               ref_children.size () == test_children.size (),
-               "child counts of " << ref->get_name() << " differ; ref has " << ref_children.size() << ", test has " << test_children.size()
-               );
+       auto whitespace_content = [](xmlpp::Node* node) {
+               auto content = dynamic_cast<xmlpp::ContentNode*>(node);
+               return content && content->get_content().find_first_not_of(" \t\r\n") == string::npos;
+       };
 
-       xmlpp::Element::NodeList::iterator k = ref_children.begin ();
-       xmlpp::Element::NodeList::iterator l = test_children.begin ();
-       while (k != ref_children.end ()) {
+       auto ref_children = ref->get_children ();
+       auto test_children = test->get_children ();
+
+       auto k = ref_children.begin ();
+       auto l = test_children.begin ();
+       while (k != ref_children.end() && l != test_children.end()) {
+
+               if (dynamic_cast<xmlpp::CommentNode*>(*k)) {
+                       ++k;
+                       continue;
+               }
+
+               if (dynamic_cast<xmlpp::CommentNode*>(*l)) {
+                       ++l;
+                       continue;
+               }
+
+               if (whitespace_content(*k) && ignore_whitespace) {
+                       ++k;
+                       continue;
+               }
+
+               if (whitespace_content(*l) && ignore_whitespace) {
+                       ++l;
+                       continue;
+               }
 
                /* XXX: should be doing xmlpp::EntityReference, xmlpp::XIncludeEnd, xmlpp::XIncludeStart */
 
-               xmlpp::Element* ref_el = dynamic_cast<xmlpp::Element*> (*k);
-               xmlpp::Element* test_el = dynamic_cast<xmlpp::Element*> (*l);
+               auto ref_el = dynamic_cast<xmlpp::Element*> (*k);
+               auto test_el = dynamic_cast<xmlpp::Element*> (*l);
                BOOST_CHECK ((ref_el && test_el) || (!ref_el && !test_el));
                if (ref_el && test_el) {
-                       check_xml (ref_el, test_el, ignore);
+                       check_xml (ref_el, test_el, ignore_tags, ignore_whitespace);
                }
 
-               xmlpp::ContentNode* ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
-               xmlpp::ContentNode* test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
+               auto ref_cn = dynamic_cast<xmlpp::ContentNode*> (*k);
+               auto test_cn = dynamic_cast<xmlpp::ContentNode*> (*l);
                BOOST_CHECK ((ref_cn && test_cn) || (!ref_cn && !test_cn));
                if (ref_cn && test_cn) {
-                       BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content ());
+                       BOOST_CHECK_EQUAL (ref_cn->get_content(), test_cn->get_content());
                }
 
                ++k;
                ++l;
        }
 
-       xmlpp::Element::AttributeList ref_attributes = ref->get_attributes ();
-       xmlpp::Element::AttributeList test_attributes = test->get_attributes ();
+       while (k != ref_children.end() && ignore_whitespace && whitespace_content(*k)) {
+               ++k;
+       }
+
+       while (l != test_children.end() && ignore_whitespace && whitespace_content(*l)) {
+               ++l;
+       }
+
+       BOOST_REQUIRE (k == ref_children.end());
+       BOOST_REQUIRE (l == test_children.end());
+
+       auto ref_attributes = ref->get_attributes ();
+       auto test_attributes = test->get_attributes ();
        BOOST_CHECK_EQUAL (ref_attributes.size(), test_attributes.size ());
 
-       xmlpp::Element::AttributeList::const_iterator m = ref_attributes.begin();
-       xmlpp::Element::AttributeList::const_iterator n = test_attributes.begin();
+       auto m = ref_attributes.begin();
+       auto n = test_attributes.begin();
        while (m != ref_attributes.end ()) {
                BOOST_CHECK_EQUAL ((*m)->get_name(), (*n)->get_name());
                BOOST_CHECK_EQUAL ((*m)->get_value(), (*n)->get_value());
@@ -143,7 +184,7 @@ check_xml (xmlpp::Element* ref, xmlpp::Element* test, list<string> ignore)
 }
 
 void
-check_xml (string ref, string test, list<string> ignore)
+check_xml (string ref, string test, list<string> ignore, bool ignore_whitespace)
 {
        xmlpp::DomParser* ref_parser = new xmlpp::DomParser ();
        ref_parser->parse_memory (ref);
@@ -152,7 +193,7 @@ check_xml (string ref, string test, list<string> ignore)
        test_parser->parse_memory (test);
        xmlpp::Element* test_root = test_parser->get_document()->get_root_node ();
 
-       check_xml (ref_root, test_root, ignore);
+       check_xml (ref_root, test_root, ignore, ignore_whitespace);
 }
 
 void
@@ -161,9 +202,9 @@ check_file (boost::filesystem::path ref, boost::filesystem::path check)
        uintmax_t N = boost::filesystem::file_size (ref);
        BOOST_CHECK_EQUAL (N, boost::filesystem::file_size (check));
        FILE* ref_file = dcp::fopen_boost (ref, "rb");
-       BOOST_CHECK (ref_file);
+       BOOST_REQUIRE (ref_file);
        FILE* check_file = dcp::fopen_boost (check, "rb");
-       BOOST_CHECK (check_file);
+       BOOST_REQUIRE (check_file);
 
        int const buffer_size = 65536;
        uint8_t* ref_buffer = new uint8_t[buffer_size];
@@ -229,15 +270,42 @@ simple_picture (boost::filesystem::path path, string suffix)
 }
 
 
+shared_ptr<dcp::SoundAsset>
+simple_sound (boost::filesystem::path path, string suffix, dcp::MXFMetadata mxf_meta, string language)
+{
+       /* Set a valid language, then overwrite it, so that the language parameter can be badly formed */
+       shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset(dcp::Fraction(24, 1), 48000, 1, dcp::LanguageTag("en-US"), dcp::SMPTE));
+       ms->_language = language;
+       ms->set_metadata (mxf_meta);
+       vector<dcp::Channel> active_channels;
+       active_channels.push_back (dcp::LEFT);
+       shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write (path / dcp::String::compose("audio%1.mxf", suffix), active_channels);
+
+       SF_INFO info;
+       info.format = 0;
+       SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
+       BOOST_CHECK (sndfile);
+       float buffer[4096*6];
+       float* channels[1];
+       channels[0] = buffer;
+       while (true) {
+               sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
+               sound_writer->write (channels, N);
+               if (N < 4096) {
+                       break;
+               }
+       }
+
+       sound_writer->finalize ();
+
+       return ms;
+}
+
+
 shared_ptr<dcp::DCP>
 make_simple (boost::filesystem::path path, int reels)
 {
        /* Some known metadata */
-       dcp::XMLMetadata xml_meta;
-       xml_meta.annotation_text = "A Test DCP";
-       xml_meta.issuer = "OpenDCP 0.0.25";
-       xml_meta.creator = "OpenDCP 0.0.25";
-       xml_meta.issue_date = "2012-07-17T04:45:18+00:00";
        dcp::MXFMetadata mxf_meta;
        mxf_meta.company_name = "OpenDCP";
        mxf_meta.product_name = "OpenDCP";
@@ -247,35 +315,19 @@ make_simple (boost::filesystem::path path, int reels)
        boost::filesystem::create_directories (path);
        shared_ptr<dcp::DCP> d (new dcp::DCP (path));
        shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
-       cpl->set_content_version_id ("urn:uuid:75ac29aa-42ac-1234-ecae-49251abefd11");
-       cpl->set_content_version_label_text ("content-version-label-text");
-       cpl->set_metadata (xml_meta);
+       cpl->set_annotation_text ("A Test DCP");
+       cpl->set_issuer ("OpenDCP 0.0.25");
+       cpl->set_creator ("OpenDCP 0.0.25");
+       cpl->set_issue_date ("2012-07-17T04:45:18+00:00");
+       cpl->set_content_version (
+               dcp::ContentVersion("urn:uuid:75ac29aa-42ac-1234-ecae-49251abefd11", "content-version-label-text")
+               );
 
        for (int i = 0; i < reels; ++i) {
                string suffix = reels == 1 ? "" : dcp::String::compose("%1", i);
 
                shared_ptr<dcp::MonoPictureAsset> mp = simple_picture (path, suffix);
-
-               shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
-               ms->set_metadata (mxf_meta);
-               shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write (path / dcp::String::compose("audio%1.mxf", suffix));
-
-               SF_INFO info;
-               info.format = 0;
-               SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
-               BOOST_CHECK (sndfile);
-               float buffer[4096*6];
-               float* channels[1];
-               channels[0] = buffer;
-               while (true) {
-                       sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
-                       sound_writer->write (channels, N);
-                       if (N < 4096) {
-                               break;
-                       }
-               }
-
-               sound_writer->finalize ();
+               shared_ptr<dcp::SoundAsset> ms = simple_sound (path, suffix, mxf_meta, "en-US");
 
                cpl->add (shared_ptr<dcp::Reel> (
                                  new dcp::Reel (
@@ -328,9 +380,8 @@ make_simple_with_interop_subs (boost::filesystem::path path)
        subs->add (simple_subtitle());
 
        boost::filesystem::create_directory (path / "subs");
-       dcp::Data data(4096);
-       data.write (path / "subs" / "font.ttf");
-       subs->add_font ("afont", path / "subs" / "font.ttf");
+       dcp::ArrayData data(4096);
+       subs->add_font ("afont", data);
        subs->write (path / "subs" / "subs.xml");
 
        shared_ptr<dcp::ReelSubtitleAsset> reel_subs(new dcp::ReelSubtitleAsset(subs, dcp::Fraction(24, 1), 240, 0));
@@ -348,7 +399,7 @@ make_simple_with_smpte_subs (boost::filesystem::path path)
        shared_ptr<dcp::SMPTESubtitleAsset> subs(new dcp::SMPTESubtitleAsset());
        subs->add (simple_subtitle());
 
-       dcp::Data data(4096);
+       dcp::ArrayData data(4096);
        subs->write (path / "subs.mxf");
 
        shared_ptr<dcp::ReelSubtitleAsset> reel_subs(new dcp::ReelSubtitleAsset(subs, dcp::Fraction(24, 1), 240, 0));
@@ -390,4 +441,35 @@ make_simple_with_smpte_ccaps (boost::filesystem::path path)
 }
 
 
+shared_ptr<dcp::OpenJPEGImage>
+black_image ()
+{
+       shared_ptr<dcp::OpenJPEGImage> image(new dcp::OpenJPEGImage(dcp::Size(1998, 1080)));
+       int const pixels = 1998 * 1080;
+       for (int i = 0; i < 3; ++i) {
+               memset (image->data(i), 0, pixels * sizeof(int));
+       }
+       return image;
+}
+
+
+shared_ptr<dcp::ReelAsset>
+black_picture_asset (boost::filesystem::path dir, int frames)
+{
+       shared_ptr<dcp::OpenJPEGImage> image = black_image ();
+       dcp::ArrayData frame = dcp::compress_j2k (image, 100000000, 24, false, false);
+       BOOST_REQUIRE (frame.size() < 230000000 / (24 * 8));
+
+       shared_ptr<dcp::MonoPictureAsset> asset(new dcp::MonoPictureAsset(dcp::Fraction(24, 1), dcp::SMPTE));
+       boost::filesystem::create_directories (dir);
+       shared_ptr<dcp::PictureAssetWriter> writer = asset->start_write (dir / "pic.mxf", true);
+       for (int i = 0; i < frames; ++i) {
+               writer->write (frame.data(), frame.size());
+       }
+       writer->finalize ();
+
+       return shared_ptr<dcp::ReelAsset>(new dcp::ReelMonoPictureAsset(asset, 0));
+}
+
+
 BOOST_GLOBAL_FIXTURE (TestConfig);