Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert mpd module to cmake #1234

Merged
merged 18 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packager/media/test/test_data_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ std::vector<uint8_t> ReadTestDataFile(const std::string& name) {
}

} // namespace media

// Get the content of |file_path|. Returns empty string on error.
std::string GetPathContent(std::filesystem::path& file_path) {
std::string content;

FILE* f = fopen(file_path.string().c_str(), "rb");
if (!f) {
LOG(FATAL) << "Failed to read test data from " << file_path;
return std::string{};
}

content.resize(std::filesystem::file_size(file_path));
size_t size = fread(content.data(), 1, content.size(), f);
content.resize(size);
fclose(f);

return content;
}

} // namespace shaka
4 changes: 4 additions & 0 deletions packager/media/test/test_data_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ std::filesystem::path GetAppTestDataFilePath(const std::string& name);
std::vector<uint8_t> ReadTestDataFile(const std::string& name);

} // namespace media

// Get the content of |file_path|. Returns empty string on error.
std::string GetPathContent(std::filesystem::path& file_path);

} // namespace shaka

#endif // PACKAGER_MEDIA_TEST_TEST_DATA_UTIL_H_
110 changes: 109 additions & 1 deletion packager/mpd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,112 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

add_subdirectory(base)
add_subdirectory(base)

add_library(manifest_base STATIC
base/bandwidth_estimator.cc
base/bandwidth_estimator.h
)

target_link_libraries(manifest_base
glog
)

add_library(mpd_builder STATIC
base/adaptation_set.cc
base/adaptation_set.h
base/content_protection_element.cc
base/content_protection_element.h
base/mpd_builder.cc
base/mpd_builder.h
base/mpd_notifier_util.cc
base/mpd_notifier_util.h
base/mpd_notifier.h
base/mpd_options.h
base/mpd_utils.cc
base/mpd_utils.h
base/period.cc
base/period.h
base/representation.cc
base/representation.h
base/segment_info.h
base/simple_mpd_notifier.cc
base/simple_mpd_notifier.h
base/xml/scoped_xml_ptr.h
base/xml/xml_node.cc
base/xml/xml_node.h
public/mpd_params.h
)

target_link_libraries(mpd_builder
absl::flags
absl::strings
absl::str_format
glog
LibXml2
file
media_base
manifest_base
mpd_media_info_proto
utils_clock
)


add_library(mpd_mocks STATIC
base/mock_mpd_builder.cc
base/mock_mpd_builder.h
base/mock_mpd_notifier.cc
base/mock_mpd_notifier.h
)

target_link_libraries(mpd_mocks gmock absl::synchronization LibXml2 mpd_media_info_proto)

add_executable(mpd_unittest
base/adaptation_set_unittest.cc
base/bandwidth_estimator_unittest.cc
base/mpd_builder_unittest.cc
base/mpd_utils_unittest.cc
base/period_unittest.cc
base/representation_unittest.cc
base/simple_mpd_notifier_unittest.cc
base/xml/xml_node_unittest.cc
test/mpd_builder_test_helper.cc
test/mpd_builder_test_helper.h
test/xml_compare.cc
test/xml_compare.h
util/mpd_writer_unittest.cc
)

target_compile_definitions(mpd_unittest
PRIVATE
# We used to build off of __FILE__, but that is not always an absolute
# path, depending on the version of CMake. This is consistent.
TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/data"
TEST_SCHEMA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/test/schema")

target_link_libraries(mpd_unittest
file
file_test_util
test_data_util
absl::flags
mpd_builder
mpd_mocks
mpd_util
gmock
gtest
utils_test_clock
gtest_main)

add_test(NAME mpd_unittest COMMAND mpd_unittest)

add_library(mpd_util STATIC
util/mpd_writer.cc
util/mpd_writer.h)

target_link_libraries(mpd_util
file
absl::flags
mpd_builder
mpd_mocks
)

2 changes: 1 addition & 1 deletion packager/mpd/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# https://developers.google.com/open-source/licenses/bsd

add_proto_library(mpd_media_info_proto STATIC
media_info.proto)
media_info.proto)
63 changes: 32 additions & 31 deletions packager/mpd/base/adaptation_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

#include "packager/mpd/base/adaptation_set.h"

#include <absl/strings/numbers.h>
#include <glog/logging.h>
#include <cmath>

#include "packager/base/logging.h"
#include "packager/base/strings/string_number_conversions.h"
#include "absl/strings/str_format.h"
#include "packager/mpd/base/media_info.pb.h"
#include "packager/mpd/base/mpd_options.h"
#include "packager/mpd/base/mpd_utils.h"
Expand All @@ -30,8 +31,8 @@ AdaptationSet::Role MediaInfoTextTypeToRole(
case MediaInfo::TextInfo::SUBTITLE:
return AdaptationSet::kRoleSubtitle;
default:
NOTREACHED() << "Unknown MediaInfo TextType: " << type
<< " assuming subtitle.";
NOTIMPLEMENTED() << "Unknown MediaInfo TextType: " << type
<< " assuming subtitle.";
return AdaptationSet::kRoleSubtitle;
}
}
Expand Down Expand Up @@ -98,7 +99,7 @@ std::string GetPictureAspectRatio(uint32_t width,
<< scaled_height << ") reduced to " << par_num << ":" << par_den
<< " with error " << min_error << ".";

return base::IntToString(par_num) + ":" + base::IntToString(par_den);
return absl::StrFormat("%d:%d", par_num, par_den);
}

// Adds an entry to picture_aspect_ratio if the size of picture_aspect_ratio is
Expand Down Expand Up @@ -239,54 +240,54 @@ void AdaptationSet::AddRole(Role role) {
// can be passed to Representation to avoid setting redundant attributes. For
// example, if AdaptationSet@width is set, then Representation@width is
// redundant and should not be set.
base::Optional<xml::XmlNode> AdaptationSet::GetXml() {
std::optional<xml::XmlNode> AdaptationSet::GetXml() {
xml::AdaptationSetXmlNode adaptation_set;

bool suppress_representation_width = false;
bool suppress_representation_height = false;
bool suppress_representation_frame_rate = false;

if (id_ && !adaptation_set.SetId(id_.value()))
return base::nullopt;
return std::nullopt;
if (!adaptation_set.SetStringAttribute("contentType", content_type_))
return base::nullopt;
return std::nullopt;
if (!language_.empty() && language_ != "und" &&
!adaptation_set.SetStringAttribute("lang", language_)) {
return base::nullopt;
return std::nullopt;
}

// Note that std::{set,map} are ordered, so the last element is the max value.
if (video_widths_.size() == 1) {
suppress_representation_width = true;
if (!adaptation_set.SetIntegerAttribute("width", *video_widths_.begin()))
return base::nullopt;
return std::nullopt;
} else if (video_widths_.size() > 1) {
if (!adaptation_set.SetIntegerAttribute("maxWidth",
*video_widths_.rbegin())) {
return base::nullopt;
return std::nullopt;
}
}
if (video_heights_.size() == 1) {
suppress_representation_height = true;
if (!adaptation_set.SetIntegerAttribute("height", *video_heights_.begin()))
return base::nullopt;
return std::nullopt;
} else if (video_heights_.size() > 1) {
if (!adaptation_set.SetIntegerAttribute("maxHeight",
*video_heights_.rbegin())) {
return base::nullopt;
return std::nullopt;
}
}

if (video_frame_rates_.size() == 1) {
suppress_representation_frame_rate = true;
if (!adaptation_set.SetStringAttribute(
"frameRate", video_frame_rates_.begin()->second)) {
return base::nullopt;
return std::nullopt;
}
} else if (video_frame_rates_.size() > 1) {
if (!adaptation_set.SetStringAttribute(
"maxFrameRate", video_frame_rates_.rbegin()->second)) {
return base::nullopt;
return std::nullopt;
}
}

Expand All @@ -302,60 +303,60 @@ base::Optional<xml::XmlNode> AdaptationSet::GetXml() {
? "subsegmentAlignment"
: "segmentAlignment",
"true")) {
return base::nullopt;
return std::nullopt;
}
}

if (picture_aspect_ratio_.size() == 1 &&
!adaptation_set.SetStringAttribute("par",
*picture_aspect_ratio_.begin())) {
return base::nullopt;
return std::nullopt;
}

if (!adaptation_set.AddContentProtectionElements(
content_protection_elements_)) {
return base::nullopt;
return std::nullopt;
}

std::string trick_play_reference_ids;
for (const AdaptationSet* adaptation_set : trick_play_references_) {
for (const AdaptationSet* tp_adaptation_set : trick_play_references_) {
// Should be a whitespace-separated list, see DASH-IOP 3.2.9.
if (!trick_play_reference_ids.empty())
trick_play_reference_ids += ' ';
CHECK(adaptation_set->has_id());
trick_play_reference_ids += std::to_string(adaptation_set->id());
CHECK(tp_adaptation_set->has_id());
trick_play_reference_ids += std::to_string(tp_adaptation_set->id());
}
if (!trick_play_reference_ids.empty() &&
!adaptation_set.AddEssentialProperty(
"http://dashif.org/guidelines/trickmode", trick_play_reference_ids)) {
return base::nullopt;
return std::nullopt;
}

std::string switching_ids;
for (const AdaptationSet* adaptation_set : switchable_adaptation_sets_) {
for (const AdaptationSet* s_adaptation_set : switchable_adaptation_sets_) {
// Should be a comma-separated list, see DASH-IOP 3.8.
if (!switching_ids.empty())
switching_ids += ',';
CHECK(adaptation_set->has_id());
switching_ids += std::to_string(adaptation_set->id());
CHECK(s_adaptation_set->has_id());
switching_ids += std::to_string(s_adaptation_set->id());
}
if (!switching_ids.empty() &&
!adaptation_set.AddSupplementalProperty(
"urn:mpeg:dash:adaptation-set-switching:2016", switching_ids)) {
return base::nullopt;
return std::nullopt;
}

for (const AdaptationSet::Accessibility& accessibility : accessibilities_) {
if (!adaptation_set.AddAccessibilityElement(accessibility.scheme,
accessibility.value)) {
return base::nullopt;
return std::nullopt;
}
}

for (AdaptationSet::Role role : roles_) {
if (!adaptation_set.AddRoleElement("urn:mpeg:dash:role:2011",
RoleToText(role))) {
return base::nullopt;
return std::nullopt;
}
}

Expand All @@ -369,10 +370,10 @@ base::Optional<xml::XmlNode> AdaptationSet::GetXml() {
representation->SuppressOnce(Representation::kSuppressFrameRate);
auto child = representation->GetXml();
if (!child || !adaptation_set.AddChild(std::move(*child)))
return base::nullopt;
return std::nullopt;
}

return std::move(adaptation_set);
return adaptation_set;
}

void AdaptationSet::ForceSetSegmentAlignment(bool segment_alignment) {
Expand Down Expand Up @@ -593,7 +594,7 @@ void AdaptationSet::RecordFrameRate(int32_t frame_duration, int32_t timescale) {
return;
}
video_frame_rates_[static_cast<double>(timescale) / frame_duration] =
base::IntToString(timescale) + "/" + base::IntToString(frame_duration);
absl::StrFormat("%d/%d", timescale, frame_duration);
}

} // namespace shaka
6 changes: 3 additions & 3 deletions packager/mpd/base/adaptation_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <vector>

#include "packager/base/optional.h"
#include "packager/mpd/base/xml/xml_node.h"

namespace shaka {
Expand Down Expand Up @@ -109,7 +109,7 @@ class AdaptationSet {
/// and ContentProtection elements.
/// @return On success returns a non-NULL scoped_xml_ptr. Otherwise returns a
/// NULL scoped_xml_ptr.
base::Optional<xml::XmlNode> GetXml();
std::optional<xml::XmlNode> GetXml();

/// Forces the (sub)segmentAlignment field to be set to @a segment_alignment.
/// Use this if you are certain that the (sub)segments are alinged/unaligned
Expand Down Expand Up @@ -248,7 +248,7 @@ class AdaptationSet {

uint32_t* const representation_counter_;

base::Optional<uint32_t> id_;
std::optional<uint32_t> id_;
const std::string language_;
const MpdOptions& mpd_options_;

Expand Down
4 changes: 2 additions & 2 deletions packager/mpd/base/bandwidth_estimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cmath>
#include <numeric>

#include "packager/base/logging.h"
#include "glog/logging.h"

namespace shaka {

Expand Down Expand Up @@ -106,7 +106,7 @@ uint64_t BandwidthEstimator::GetBitrate(const Block& block,
VLOG(1) << "Exclude short segment (duration " << block.duration
<< ", target_duration " << target_block_duration
<< ") in peak bandwidth computation.";
return 0.0;
return 0;
}
return static_cast<uint64_t>(ceil(block.size_in_bits / block.duration));
}
Expand Down
Loading