Skip to content

Commit

Permalink
feat: port hls module to cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmin committed Aug 5, 2023
1 parent 793518c commit 58a0cca
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 269 deletions.
1 change: 1 addition & 0 deletions packager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ include("protobuf.cmake")
add_subdirectory(file)
add_subdirectory(kv_pairs)
add_subdirectory(media)
add_subdirectory(hls)
add_subdirectory(mpd)
add_subdirectory(status)
add_subdirectory(third_party)
Expand Down
52 changes: 52 additions & 0 deletions packager/hls/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# Copyright 2016 Google LLC. All rights reserved.
#
# 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


add_library(hls_builder
base/hls_notifier.h
base/master_playlist.cc
base/master_playlist.h
base/media_playlist.cc
base/media_playlist.h
base/simple_hls_notifier.cc
base/simple_hls_notifier.h
base/tag.cc
base/tag.h
public/hls_params.h
)

target_link_libraries(hls_builder
file
media_base
widevine_protos
manifest_base
mpd_media_info_proto
absl::flags
absl::strings
absl::str_format
glog
)

add_executable(hls_unittest
base/master_playlist_unittest.cc
base/media_playlist_unittest.cc
base/mock_media_playlist.cc
base/mock_media_playlist.h
base/simple_hls_notifier_unittest.cc
)

target_link_libraries(hls_unittest
file
file_test_util
test_data_util
absl::flags
hls_builder
gmock
gtest
gtest_main)

add_test(NAME hls_unittest COMMAND hls_unittest)
42 changes: 20 additions & 22 deletions packager/hls/base/master_playlist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

#include <inttypes.h>

#include "packager/base/files/file_path.h"
#include "packager/base/strings/string_number_conversions.h"
#include "packager/base/strings/string_util.h"
#include "packager/base/strings/stringprintf.h"
#include <absl/strings/numbers.h>
#include <absl/strings/str_format.h>
#include <absl/strings/str_join.h>
#include <glog/logging.h>
#include <filesystem>
#include "packager/file/file.h"
#include "packager/hls/base/media_playlist.h"
#include "packager/hls/base/tag.h"
Expand All @@ -30,8 +31,8 @@ void AppendVersionString(std::string* content) {
const std::string version = GetPackagerVersion();
if (version.empty())
return;
base::StringAppendF(content, "## Generated with %s version %s\n",
GetPackagerProjectUrl().c_str(), version.c_str());
absl::StrAppendFormat(content, "## Generated with %s version %s\n",
GetPackagerProjectUrl().c_str(), version.c_str());
}

// This structure roughly maps to the Variant stream in HLS specification.
Expand Down Expand Up @@ -207,8 +208,8 @@ void BuildStreamInfTag(const MediaPlaylist& playlist,
tag_name = "#EXT-X-I-FRAME-STREAM-INF";
break;
default:
NOTREACHED() << "Cannot build STREAM-INFO tag for type "
<< static_cast<int>(playlist.stream_type());
NOTIMPLEMENTED() << "Cannot build STREAM-INFO tag for type "
<< static_cast<int>(playlist.stream_type());
break;
}
Tag tag(tag_name, out);
Expand All @@ -223,7 +224,7 @@ void BuildStreamInfTag(const MediaPlaylist& playlist,
variant.audio_codecs.end());
all_codecs.insert(all_codecs.end(), variant.text_codecs.begin(),
variant.text_codecs.end());
tag.AddQuotedString("CODECS", base::JoinString(all_codecs, ","));
tag.AddQuotedString("CODECS", absl::StrJoin(all_codecs, ","));

uint32_t width;
uint32_t height;
Expand Down Expand Up @@ -266,8 +267,8 @@ void BuildStreamInfTag(const MediaPlaylist& playlist,
tag.AddQuotedString("URI", base_url + playlist.file_name());
out->append("\n");
} else {
base::StringAppendF(out, "\n%s%s\n", base_url.c_str(),
playlist.file_name().c_str());
absl::StrAppendFormat(out, "\n%s%s\n", base_url.c_str(),
playlist.file_name().c_str());
}
}

Expand Down Expand Up @@ -295,8 +296,8 @@ void BuildMediaTag(const MediaPlaylist& playlist,
break;

default:
NOTREACHED() << "Cannot build media tag for type "
<< static_cast<int>(playlist.stream_type());
NOTIMPLEMENTED() << "Cannot build media tag for type "
<< static_cast<int>(playlist.stream_type());
break;
}

Expand All @@ -313,7 +314,7 @@ void BuildMediaTag(const MediaPlaylist& playlist,
if (is_default) {
tag.AddString("DEFAULT", "YES");
} else {
tag.AddString("DEFAULT", "NO");
tag.AddString("DEFAULT", "NO");
}

if (is_autoselect) {
Expand All @@ -322,8 +323,7 @@ void BuildMediaTag(const MediaPlaylist& playlist,

const std::vector<std::string>& characteristics = playlist.characteristics();
if (!characteristics.empty()) {
tag.AddQuotedString("CHARACTERISTICS",
base::JoinString(characteristics, ","));
tag.AddQuotedString("CHARACTERISTICS", absl::StrJoin(characteristics, ","));
}

const MediaPlaylist::MediaPlaylistStreamType kAudio =
Expand Down Expand Up @@ -514,12 +514,10 @@ bool MasterPlaylist::WriteMasterPlaylist(
if (content == written_playlist_)
return true;

std::string file_path =
base::FilePath::FromUTF8Unsafe(output_dir)
.Append(base::FilePath::FromUTF8Unsafe(file_name_))
.AsUTF8Unsafe();
if (!File::WriteFileAtomically(file_path.c_str(), content)) {
LOG(ERROR) << "Failed to write master playlist to: " << file_path;
std::filesystem::path file_path =
std::filesystem::path(output_dir) / file_name_;
if (!File::WriteFileAtomically(file_path.string().c_str(), content)) {
LOG(ERROR) << "Failed to write master playlist to: " << file_path.string();
return false;
}
written_playlist_ = content;
Expand Down
54 changes: 32 additions & 22 deletions packager/hls/base/master_playlist_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "packager/base/files/file_path.h"
#include <filesystem>
#include "packager/file/file.h"
#include "packager/hls/base/master_playlist.h"
#include "packager/hls/base/media_playlist.h"
Expand All @@ -17,9 +17,9 @@
namespace shaka {
namespace hls {

using base::FilePath;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::DoAll;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::ReturnRef;
Expand Down Expand Up @@ -138,20 +138,18 @@ class MasterPlaylistTest : public ::testing::Test {
protected:
MasterPlaylistTest()
: master_playlist_(new MasterPlaylist(kDefaultMasterPlaylistName,
kDefaultAudioLanguage,
kDefaultTextLanguage,
!kIsIndependentSegments)),
kDefaultAudioLanguage,
kDefaultTextLanguage,
!kIsIndependentSegments)),
test_output_dir_("memory://test_dir"),
master_playlist_path_(
FilePath::FromUTF8Unsafe(test_output_dir_)
.Append(FilePath::FromUTF8Unsafe(kDefaultMasterPlaylistName))
.AsUTF8Unsafe()) {}
master_playlist_path_(std::filesystem::path(test_output_dir_) /
kDefaultMasterPlaylistName) {}

void SetUp() override { SetPackagerVersionForTesting("test"); }

std::unique_ptr<MasterPlaylist> master_playlist_;
std::string test_output_dir_;
std::string master_playlist_path_;
std::filesystem::path master_playlist_path_;
};

TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) {
Expand All @@ -166,7 +164,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) {
{mock_playlist.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -200,7 +199,8 @@ TEST_F(MasterPlaylistTest,
{mock_playlist.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -229,7 +229,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideoWithFrameRate) {
{mock_playlist.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -257,7 +258,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneIframePlaylist) {
{mock_playlist.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -548,7 +550,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndDvsAudio) {
kBaseUrl, test_output_dir_, {video.get(), dvs_audio.get(), audio.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -590,7 +593,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndTextGroups) {
{video.get(), text_eng.get(), text_fr.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -636,7 +640,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudioAndText) {
kBaseUrl, test_output_dir_, {video.get(), audio.get(), text.get()}));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -709,7 +714,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistMixedPlaylistsDifferentGroups) {
media_playlist_list));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -812,7 +818,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistAudioOnly) {
media_playlist_list));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -864,7 +871,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistAudioOnlyJOC) {
media_playlist_list));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -916,7 +924,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistAudioOnlyAC4IMS) {
media_playlist_list));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down Expand Up @@ -969,7 +978,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistAudioOnlyAC4CBI) {
media_playlist_list));

std::string actual;
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
ASSERT_TRUE(
File::ReadFileToString(master_playlist_path_.string().c_str(), &actual));

const std::string expected =
"#EXTM3U\n"
Expand Down
Loading

0 comments on commit 58a0cca

Please sign in to comment.