Skip to content

Commit

Permalink
fix: produce output segment for header only VTT input
Browse files Browse the repository at this point in the history
  • Loading branch information
lee.fordyce committed Feb 29, 2024
1 parent 4d874c9 commit b0c5722
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions include/packager/packager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ struct PackagingParams {

/// Flag used to adjust negative CTS offset values to correct PTS < DTS
bool cts_offset_adjustment = false;

/// Flag used as a workaround to produce output segments when the input is a
/// header only VTT file
bool webvtt_header_only_output_segment = false;
};

/// Defines a single input/output stream.
Expand Down
1 change: 1 addition & 0 deletions packager/live_packager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ Status LivePackager::PackageTimedText(const Segment& in,
packaging_params.mp4_output_params.sequence_number = config_.segment_number;
packaging_params.chunking_params.adjust_sample_boundaries = true;
packaging_params.mp4_output_params.include_pssh_in_stream = false;
packaging_params.webvtt_header_only_output_segment = true;

StreamDescriptors descriptors =
setupStreamDescriptors(config_, callback_params, init_callback_params);
Expand Down
2 changes: 1 addition & 1 deletion packager/live_packager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ INSTANTIATE_TEST_CASE_P(
"audio/en/%05d.m4s", false}));

TEST_F(LivePackagerBaseTest, TestPackageTimedTextHybrikComp) {
for (unsigned int i = 1; i < kNumSegments; i++) {
for (unsigned int i = 0; i < kNumSegments; i++) {
std::string segment_num =
absl::StrFormat("hybrik_comp/text_in/en.m3u8_%010d.vtt", i);
std::vector<uint8_t> segment_buffer = ReadTestDataFile(segment_num);
Expand Down
2 changes: 1 addition & 1 deletion packager/media/demuxer/demuxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Status Demuxer::InitializeParser() {
parser_.reset(new WebMMediaParser());
break;
case CONTAINER_WEBVTT:
parser_.reset(new WebVttParser());
parser_.reset(new WebVttParser(webvtt_header_only_output_segment_));
break;
case CONTAINER_UNKNOWN: {
const int64_t kDumpSizeLimit = 512;
Expand Down
8 changes: 8 additions & 0 deletions packager/media/demuxer/demuxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Demuxer : public OriginHandler {
cts_offset_adjustment_ = cts_offset_adjustment;
}

void set_webvtt_header_only_output_segment(
bool webvtt_header_only_output_segment) {
webvtt_header_only_output_segment_ = webvtt_header_only_output_segment;
}

protected:
/// @name MediaHandler implementation overrides.
/// @{
Expand Down Expand Up @@ -153,6 +158,9 @@ class Demuxer : public OriginHandler {
bool dump_stream_info_ = false;
// flag used to adjust negative CTS offset values to correct PTS < DTS
bool cts_offset_adjustment_ = false;
// flag used as a workaround to generate output segment in the case of input
// WEBVTT with header only
bool webvtt_header_only_output_segment_ = false;
Status init_event_status_;
};

Expand Down
19 changes: 17 additions & 2 deletions packager/media/formats/webvtt/webvtt_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ void ParseSettings(const std::string& id,

} // namespace

WebVttParser::WebVttParser() {}
WebVttParser::WebVttParser(bool webvtt_header_only_output_segment)
: webvtt_header_only_output_segment_(webvtt_header_only_output_segment) {}

void WebVttParser::Init(const InitCB& init_cb,
const NewMediaSampleCB& new_media_sample_cb,
Expand All @@ -207,7 +208,21 @@ void WebVttParser::Init(const InitCB& init_cb,

bool WebVttParser::Flush() {
reader_.Flush();
return Parse();
const bool isOK = Parse();
// Handle case when Parser was initialized but stream information not filled
// This happens when we parse an empty webvtt file (only contains a header)
if (initialized_ && !stream_info_dispatched_) {
if (webvtt_header_only_output_segment_) {
// This is a workaround in the case of input VTT and output VTT (or TTML)
// in MP4 where we need to generate and output MP4 segment.
std::vector<std::string> block;
block.emplace_back(R"(00:00:00.000 --> 00:00:00.001)");
ParseCue("", block.data(), block.size());
} else {
DispatchTextStreamInfo();
}
}
return isOK;
}

bool WebVttParser::Parse(const uint8_t* buf, int size) {
Expand Down
3 changes: 2 additions & 1 deletion packager/media/formats/webvtt/webvtt_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace media {
// Used to parse a WebVTT source into Cues that will be sent downstream.
class WebVttParser : public MediaParser {
public:
WebVttParser();
WebVttParser(bool webvtt_header_only_output_segment = false);

void Init(const InitCB& init_cb,
const NewMediaSampleCB& new_media_sample_cb,
Expand Down Expand Up @@ -52,6 +52,7 @@ class WebVttParser : public MediaParser {
bool saw_cue_ = false;
bool stream_info_dispatched_ = false;
bool initialized_ = false;
bool webvtt_header_only_output_segment_ = false;
};

} // namespace media
Expand Down
4 changes: 2 additions & 2 deletions packager/media/formats/webvtt/webvtt_parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST_F(WebVttParserTest, ParseOnlyHeader) {
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
ASSERT_TRUE(parser_->Flush());

ASSERT_TRUE(streams_.empty());
ASSERT_FALSE(streams_.empty());
ASSERT_TRUE(samples_.empty());
}

Expand All @@ -115,7 +115,7 @@ TEST_F(WebVttParserTest, ParseHeaderWithBOM) {
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
ASSERT_TRUE(parser_->Flush());

ASSERT_TRUE(streams_.empty());
ASSERT_FALSE(streams_.empty());
ASSERT_TRUE(samples_.empty());
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WEBVTT
2 changes: 2 additions & 0 deletions packager/packager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ Status CreateDemuxer(const StreamDescriptor& stream,
std::shared_ptr<Demuxer> demuxer = std::make_shared<Demuxer>(stream.input);
demuxer->set_dump_stream_info(packaging_params.test_params.dump_stream_info);
demuxer->set_cts_offset_adjustment(packaging_params.cts_offset_adjustment);
demuxer->set_webvtt_header_only_output_segment(
packaging_params.webvtt_header_only_output_segment);

if (packaging_params.decryption_params.key_provider != KeyProvider::kNone) {
std::unique_ptr<KeySource> decryption_key_source(
Expand Down

0 comments on commit b0c5722

Please sign in to comment.