2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-05 02:32:46 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/mpd/test/mpd_builder_test_helper.h>
|
2014-02-05 02:32:46 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2014-08-28 18:35:15 +00:00
|
|
|
#include <google/protobuf/text_format.h>
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/test/test_data_util.h>
|
|
|
|
#include <packager/mpd/base/media_info.pb.h>
|
|
|
|
#include <packager/mpd/base/xml/scoped_xml_ptr.h>
|
|
|
|
#include <packager/mpd/test/xml_compare.h>
|
2014-02-05 02:32:46 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-02-05 02:32:46 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path GetTestDataFilePath(const std::string& name) {
|
|
|
|
auto data_dir = std::filesystem::u8path(TEST_DATA_DIR);
|
|
|
|
return data_dir / name;
|
2014-02-05 02:32:46 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path GetSchemaPath() {
|
|
|
|
auto schema_dir = std::filesystem::u8path(TEST_SCHEMA_DIR);
|
|
|
|
return schema_dir / "DASH-MPD.xsd";
|
2014-02-05 02:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaInfo ConvertToMediaInfo(const std::string& media_info_string) {
|
|
|
|
MediaInfo media_info;
|
|
|
|
CHECK(::google::protobuf::TextFormat::ParseFromString(media_info_string,
|
|
|
|
&media_info));
|
|
|
|
return media_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaInfo GetTestMediaInfo(const std::string& media_info_file_name) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path test_path = GetTestDataFilePath(media_info_file_name);
|
|
|
|
return ConvertToMediaInfo(GetPathContent(test_path));
|
2014-02-05 02:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ValidateMpdSchema(const std::string& mpd) {
|
2015-11-17 00:06:17 +00:00
|
|
|
xml::scoped_xml_ptr<xmlDoc> doc(
|
2014-02-05 02:32:46 +00:00
|
|
|
xmlParseMemory(mpd.data(), mpd.size()));
|
|
|
|
if (!doc) {
|
|
|
|
LOG(ERROR) << "Failed to parse mpd into an xml doc.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path schema_path = GetSchemaPath();
|
2014-02-05 02:32:46 +00:00
|
|
|
std::string schema_str = GetPathContent(schema_path);
|
|
|
|
|
|
|
|
// First, I need to load the schema as a xmlDoc so that I can pass the path of
|
|
|
|
// the DASH-MPD.xsd. Then it can resolve the relative path included from the
|
|
|
|
// XSD when creating xmlSchemaParserCtxt.
|
2015-11-17 00:06:17 +00:00
|
|
|
xml::scoped_xml_ptr<xmlDoc> schema_as_doc(
|
2023-12-01 17:32:19 +00:00
|
|
|
xmlReadMemory(schema_str.data(), schema_str.size(),
|
|
|
|
schema_path.string().c_str(), NULL, 0));
|
2014-02-05 02:32:46 +00:00
|
|
|
DCHECK(schema_as_doc);
|
2015-11-17 00:06:17 +00:00
|
|
|
xml::scoped_xml_ptr<xmlSchemaParserCtxt>
|
2014-02-05 02:32:46 +00:00
|
|
|
schema_parser_ctxt(xmlSchemaNewDocParserCtxt(schema_as_doc.get()));
|
|
|
|
DCHECK(schema_parser_ctxt);
|
|
|
|
|
2015-11-17 00:06:17 +00:00
|
|
|
xml::scoped_xml_ptr<xmlSchema> schema(
|
2014-02-05 02:32:46 +00:00
|
|
|
xmlSchemaParse(schema_parser_ctxt.get()));
|
|
|
|
DCHECK(schema);
|
|
|
|
|
2015-11-17 00:06:17 +00:00
|
|
|
xml::scoped_xml_ptr<xmlSchemaValidCtxt> valid_ctxt(
|
2014-02-05 02:32:46 +00:00
|
|
|
xmlSchemaNewValidCtxt(schema.get()));
|
|
|
|
DCHECK(valid_ctxt);
|
|
|
|
int validation_result =
|
|
|
|
xmlSchemaValidateDoc(valid_ctxt.get(), doc.get());
|
|
|
|
DLOG(INFO) << "XSD validation result: " << validation_result;
|
|
|
|
return validation_result == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectMpdToEqualExpectedOutputFile(
|
|
|
|
const std::string& mpd_string,
|
|
|
|
const std::string& expected_output_file) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::filesystem::path expected_output_file_path =
|
|
|
|
GetTestDataFilePath(expected_output_file);
|
|
|
|
std::string expected_mpd = GetPathContent(expected_output_file_path);
|
|
|
|
|
|
|
|
ASSERT_TRUE(!expected_mpd.empty())
|
2014-02-05 02:32:46 +00:00
|
|
|
<< "Failed to read: " << expected_output_file;
|
|
|
|
|
|
|
|
// Adding extra << here to get a formatted output.
|
2014-04-12 01:23:20 +00:00
|
|
|
ASSERT_TRUE(XmlEqual(expected_mpd, mpd_string))
|
|
|
|
<< "Expected:" << std::endl << expected_mpd << std::endl
|
|
|
|
<< "Actual:" << std::endl << mpd_string;
|
2014-02-05 02:32:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|