Skip to content

Commit

Permalink
feat: expand timed text test (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenda6 authored Mar 26, 2024
1 parent 05d44f3 commit fd00013
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 51 deletions.
157 changes: 106 additions & 51 deletions packager/live_packager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const uint8_t kIv[]{
0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
};

const int kNumSegments = 10;
constexpr int kNumSegments = 10;
constexpr unsigned kSegmentDurationMs = 5000;

std::filesystem::path GetTestDataFilePath(const std::string& name) {
auto data_dir = std::filesystem::u8path(TEST_DATA_DIR);
Expand Down Expand Up @@ -95,6 +96,14 @@ bool ParseAndCheckType(media::mp4::Box& box, media::mp4::BoxReader* reader) {
return box.BoxType() == reader->type();
}

bool FormatWithIndex(const char* fmt, int i, std::string& out) {
std::vector<absl::FormatArg> format_args;
format_args.emplace_back(i);
absl::UntypedFormatSpec format(fmt);

return absl::FormatUntyped(&out, format, format_args);
}

struct SegmentIndexBoxChecker {
SegmentIndexBoxChecker(media::mp4::SegmentIndex box)
: sidx_(std::move(box)) {}
Expand Down Expand Up @@ -172,6 +181,19 @@ struct MovieBoxChecker {
EXPECT_EQ(exp_entry.width, act_entry.width);
EXPECT_EQ(exp_entry.height, act_entry.height);
}

const auto& exp_text_entries =
exp_track.media.information.sample_table.description.text_entries;
const auto& act_text_entries =
act_track.media.information.sample_table.description.text_entries;

EXPECT_EQ(exp_text_entries.size(), act_text_entries.size());
for (unsigned j(0); j < exp_text_entries.size(); ++j) {
const auto& exp_entry = exp_text_entries[j];
const auto& act_entry = act_text_entries[j];

EXPECT_EQ(exp_entry.BoxType(), act_entry.BoxType());
}
}
}

Expand Down Expand Up @@ -292,6 +314,46 @@ void CheckVideoInitSegment(const FullSegmentBuffer& buffer,
}
}

void CheckTextInitSegment(const FullSegmentBuffer& buffer,
media::FourCC handler,
media::FourCC format) {
bool err(true);
size_t bytes_to_read(buffer.InitSegmentSize());
const uint8_t* data(buffer.InitSegmentData());

{
std::unique_ptr<media::mp4::BoxReader> reader(
media::mp4::BoxReader::ReadBox(data, bytes_to_read, &err));
EXPECT_FALSE(err);

FileTypeBoxChecker checker;
checker.Check(reader.get());

data += reader->size();
bytes_to_read -= reader->size();
}

{
std::unique_ptr<media::mp4::BoxReader> reader(
media::mp4::BoxReader::ReadBox(data, bytes_to_read, &err));
EXPECT_FALSE(err);

media::mp4::TextSampleEntry entry;
entry.format = format;

media::mp4::Track track;
track.media.handler.handler_type = handler;
track.media.information.sample_table.description.text_entries.push_back(
entry);

media::mp4::Movie expected;
expected.tracks.push_back(track);

MovieBoxChecker checker(expected);
checker.Check(reader.get());
}
}

void CheckSegment(const LiveConfig& config,
const FullSegmentBuffer& buffer,
const uint32_t expected_timescale,
Expand Down Expand Up @@ -878,14 +940,11 @@ TEST_P(LivePackagerEncryptionTest, VerifyWithEncryption) {
live_packager_->PackageInit(init_seg, actual_buf);

for (unsigned int i = 0; i < GetParam().num_segments; i++) {
std::string format_output;

std::vector<absl::FormatArg> format_args;
format_args.emplace_back(i);
absl::UntypedFormatSpec format(GetParam().media_segment_format);
std::string input_fname;
ASSERT_TRUE(
FormatWithIndex(GetParam().media_segment_format, i, input_fname));

ASSERT_TRUE(absl::FormatUntyped(&format_output, format, format_args));
std::vector<uint8_t> segment_buffer = ReadTestDataFile(format_output);
std::vector<uint8_t> segment_buffer = ReadTestDataFile(input_fname);
ASSERT_FALSE(segment_buffer.empty());

FullSegmentBuffer out;
Expand Down Expand Up @@ -950,44 +1009,13 @@ INSTANTIATE_TEST_CASE_P(
LiveConfig::OutputFormat::TS, LiveConfig::TrackType::AUDIO,
"audio/en/%05d.m4s", false}));

TEST_F(LivePackagerBaseTest, TestPackageTimedTextHybrikComp) {
for (unsigned int i = 0; i < kNumSegments; i++) {
std::string segment_num =
absl::StrFormat("timed_text/input/en.m3u8_%010d.vtt", i);
std::vector<uint8_t> segment_buffer = ReadTestDataFile(segment_num);
ASSERT_FALSE(segment_buffer.empty());

SegmentData media_seg(segment_buffer.data(), segment_buffer.size());

FullSegmentBuffer out;

LiveConfig live_config;
live_config.format = LiveConfig::OutputFormat::VTTMP4;
live_config.track_type = LiveConfig::TrackType::TEXT;
live_config.protection_scheme = LiveConfig::EncryptionScheme::NONE;
live_config.segment_number = i + 1;
live_config.timed_text_decode_time = (i * 5000);

SetupLivePackagerConfig(live_config);
ASSERT_EQ(Status::OK, live_packager_->PackageTimedText(media_seg, out));
ASSERT_GT(out.SegmentSize(), 0);

CheckSegment(live_config, out, 1000, true);

std::vector<uint8_t> exp_seg_buf = ReadTestDataFile(
absl::StrFormat("timed_text/expected/%05d.m4s", i + 1));
ASSERT_FALSE(exp_seg_buf.empty());

std::vector<uint8_t> buffer(out.SegmentData(),
out.SegmentData() + out.SegmentSize());
ASSERT_EQ(exp_seg_buf, buffer);
}
}

struct TimedTextTestCase {
const char* media_segment_format;
const char* expected_segment_format;
LiveConfig::TrackType track_type;
LiveConfig::OutputFormat output_format;
media::FourCC handler_type;
media::FourCC format;
Status expected_status;
int64_t start_decode_time;
};
Expand All @@ -998,14 +1026,11 @@ class TimedTextParameterizedTest

TEST_P(TimedTextParameterizedTest, VerifyTimedText) {
for (unsigned int i = 0; i < kNumSegments; i++) {
std::string format_output;

std::vector<absl::FormatArg> format_args;
format_args.emplace_back(i);
absl::UntypedFormatSpec format(GetParam().media_segment_format);
std::string input_fname;
ASSERT_TRUE(
FormatWithIndex(GetParam().media_segment_format, i, input_fname));

ASSERT_TRUE(absl::FormatUntyped(&format_output, format, format_args));
std::vector<uint8_t> segment_buffer = ReadTestDataFile(format_output);
std::vector<uint8_t> segment_buffer = ReadTestDataFile(input_fname);
ASSERT_FALSE(segment_buffer.empty());

SegmentData media_seg(segment_buffer.data(), segment_buffer.size());
Expand All @@ -1019,7 +1044,7 @@ TEST_P(TimedTextParameterizedTest, VerifyTimedText) {
live_config.format == LiveConfig::OutputFormat::TTMLMP4) {
live_config.segment_number = i + 1;
live_config.timed_text_decode_time =
GetParam().start_decode_time + (i * 5000);
GetParam().start_decode_time + (i * kSegmentDurationMs);
}

SetupLivePackagerConfig(live_config);
Expand All @@ -1030,6 +1055,21 @@ TEST_P(TimedTextParameterizedTest, VerifyTimedText) {
if (live_config.format == LiveConfig::OutputFormat::VTTMP4 ||
live_config.format == LiveConfig::OutputFormat::TTMLMP4) {
CheckSegment(live_config, out, 1000, true);

if (i == 0) {
CheckTextInitSegment(out, GetParam().handler_type, GetParam().format);
}

std::string expected_fname;
ASSERT_TRUE(FormatWithIndex(GetParam().expected_segment_format, i + 1,
expected_fname));

std::vector<uint8_t> expected_buf(ReadTestDataFile(expected_fname));
ASSERT_FALSE(expected_buf.empty());

std::vector<uint8_t> actual_buf(out.SegmentData(),
out.SegmentData() + out.SegmentSize());
ASSERT_EQ(expected_buf, actual_buf);
}
}
}
Expand All @@ -1042,40 +1082,55 @@ INSTANTIATE_TEST_CASE_P(
// VTT in text --> VTT in MP4
TimedTextTestCase{
"timed_text/input/en.m3u8_%010d.vtt",
"timed_text/expected/vtt/%05d.m4s",
LiveConfig::TrackType::TEXT,
LiveConfig::OutputFormat::VTTMP4,
media::FourCC::FOURCC_text,
media::FourCC::FOURCC_wvtt,
Status::OK,
16000,
0,
},
// VTT in text --> TTML in Text
TimedTextTestCase{
"timed_text/input/en.m3u8_%010d.vtt",
"",
LiveConfig::TrackType::TEXT,
LiveConfig::OutputFormat::TTML,
media::FourCC::FOURCC_NULL,
media::FourCC::FOURCC_NULL,
Status::OK,
0,
},
// VTT in text --> TTML in MP4
TimedTextTestCase{
"timed_text/input/en.m3u8_%010d.vtt",
"timed_text/expected/ttml/%05d.m4s",
LiveConfig::TrackType::TEXT,
LiveConfig::OutputFormat::TTMLMP4,
media::FourCC::FOURCC_subt,
media::FourCC::FOURCC_stpp,
Status::OK,
16000,
},
// Invalid track type of audio
TimedTextTestCase{
"timed_text/input/en.m3u8_%010d.vtt",
"",
LiveConfig::TrackType::AUDIO,
LiveConfig::OutputFormat::TTMLMP4,
media::FourCC::FOURCC_NULL,
media::FourCC::FOURCC_NULL,
Status(error::INVALID_ARGUMENT, "Stream not available"),
0,
},
// Invalid track type of video
TimedTextTestCase{
"timed_text/input/en.m3u8_%010d.vtt",
"",
LiveConfig::TrackType::VIDEO,
LiveConfig::OutputFormat::TTMLMP4,
media::FourCC::FOURCC_NULL,
media::FourCC::FOURCC_NULL,
Status(error::INVALID_ARGUMENT, "Stream not available"),
0,
}));
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit fd00013

Please sign in to comment.