2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-09-14 16:15:24 +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/media/formats/webvtt/webvtt_parser.h>
|
|
|
|
|
|
|
|
#include <functional>
|
2020-08-26 21:21:09 +00:00
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/stream_info.h>
|
|
|
|
#include <packager/media/base/text_sample.h>
|
2018-05-25 17:41:02 +00:00
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
namespace {
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint32_t kStreamId = 0;
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t kTimeScale = 1000;
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
const char* kNoId = "";
|
2018-08-17 20:27:59 +00:00
|
|
|
|
2020-08-26 20:47:14 +00:00
|
|
|
void ExpectNoStyle(const TextFragmentStyle& style) {
|
|
|
|
EXPECT_FALSE(style.underline);
|
|
|
|
EXPECT_FALSE(style.bold);
|
|
|
|
EXPECT_FALSE(style.italic);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExpectPlainCueWithBody(const TextFragment& fragment,
|
|
|
|
const std::string& expected) {
|
|
|
|
ExpectNoStyle(fragment.style);
|
|
|
|
ASSERT_TRUE(fragment.body.empty());
|
|
|
|
ASSERT_FALSE(fragment.newline);
|
|
|
|
|
|
|
|
if (expected.empty()) {
|
|
|
|
EXPECT_TRUE(fragment.sub_fragments.empty());
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(fragment.sub_fragments.size(), 1u);
|
|
|
|
ExpectNoStyle(fragment.sub_fragments[0].style);
|
|
|
|
EXPECT_EQ(fragment.sub_fragments[0].body, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
class WebVttParserTest : public testing::Test {
|
2017-09-14 16:15:24 +00:00
|
|
|
protected:
|
2020-07-07 21:29:43 +00:00
|
|
|
void SetUpAndInitialize() {
|
|
|
|
parser_ = std::make_shared<WebVttParser>();
|
|
|
|
parser_->Init(
|
2023-12-01 17:32:19 +00:00
|
|
|
std::bind(&WebVttParserTest::InitCB, this, std::placeholders::_1),
|
|
|
|
std::bind(&WebVttParserTest::NewMediaSampleCB, this,
|
|
|
|
std::placeholders::_1, std::placeholders::_2),
|
|
|
|
std::bind(&WebVttParserTest::NewTextSampleCB, this,
|
|
|
|
std::placeholders::_1, std::placeholders::_2),
|
2020-07-07 21:29:43 +00:00
|
|
|
nullptr);
|
|
|
|
}
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
void InitCB(const std::vector<std::shared_ptr<StreamInfo>>& streams) {
|
|
|
|
streams_ = streams;
|
|
|
|
}
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool NewMediaSampleCB(uint32_t stream_id,
|
|
|
|
std::shared_ptr<MediaSample> sample) {
|
|
|
|
ADD_FAILURE() << "Should not get media samples";
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool NewTextSampleCB(uint32_t stream_id, std::shared_ptr<TextSample> sample) {
|
|
|
|
EXPECT_EQ(stream_id, kStreamId);
|
|
|
|
samples_.emplace_back(std::move(sample));
|
|
|
|
return true;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
std::shared_ptr<WebVttParser> parser_;
|
|
|
|
std::vector<std::shared_ptr<StreamInfo>> streams_;
|
|
|
|
std::vector<std::shared_ptr<TextSample>> samples_;
|
2017-09-14 16:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, FailToParseEmptyFile) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] = "";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(streams_.empty());
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, ParseOnlyHeader) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(streams_.empty());
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, ParseHeaderWithBOM) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2018-05-16 23:17:45 +00:00
|
|
|
"\xEF\xBB\xBFWEBVTT\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(streams_.empty());
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
feat: Allow LIVE UDP WebVTT input (#1349)
An updated version of PR #1027
That previous PR was done using 2021 code, and there were many changes
in the codebase from there, so a rebase was needed and also some minor
tweak here and there. But it's the same code, just reimplemented on a
newer codebase.
If you want to take a look at this in action, after building shaka
packager with this PR's code included, try this commands in 3 different
simultaneous bash sessions:
1. Video UDP input: `ffmpeg -f lavfi -re -i
"testsrc=s=320x240:r=30,format=yuv420p" -c:v h264 -sc_threshold 0 -g 30
-keyint_min 30 -r 30 -a53cc 1 -b:v 150k -preset ultrafast -r 30 -f
mpegts "udp://127.0.0.1:10000?pkt_size=1316"`
2. WebVTT UDP input: `for sec in $(seq 0 9999) ; do printf
"%02d:%02d.000 --> %02d:%02d.000\ntest second ${sec}\n\n" "$(( ${sec} /
60 ))" "$(( ${sec} % 60 ))" "$(( (${sec} + 1) / 60 ))" "$(( (${sec} + 1)
% 60 ))" ; sleep 1 ; done > /dev/udp/127.0.0.1/12345`
3. shaka packager command line: `timeout 60
path/to/build/packager/packager
'in=udp://127.0.0.1:10000?timeout=8000000,stream_selector=0,init_segment=240_init.m4s,segment_template=240_$Number%09d$.m4s,bandwidth=150000'
'in=udp://127.0.0.1:12345?timeout=8000000,stream_selector=0,input_format=webvtt,format=webvtt+mp4,init_segment=text_init.m4s,segment_template=text_$Number%09d$.m4s,language=eng,dash_roles=subtitle'
--mpd_output ./manifest.mpd --segment_duration 3.2
--suggested_presentation_delay 3.2 --min_buffer_time 3.2
--minimum_update_period 3.2 --time_shift_buffer_depth 60
--preserved_segments_outside_live_window 1 --default_language=eng
--dump_stream_info 2>&1`
Note the added `input_format=webvtt` to the shaka packager command's
second selector. That's new from this PR. If you don't use that, shaka's
format autodetection will not detect the webvtt format from the input,
as explained in
https://github.com/shaka-project/shaka-packager/issues/685#issuecomment-1029407191.
Try the command without it if you want to.
Fixes #685
Fixes #1017
---------
Co-authored-by: Daniel Cantarín <canta@canta.com.ar>
2024-02-24 00:02:19 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseNoHeaderWithoutExiting) {
|
|
|
|
// A proper WebVTT file should have the "WEBVTT" string header.
|
|
|
|
// But UDP input (not file) may be ingested when the header already
|
|
|
|
// passed, and it will not be repeated later.
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
feat: Allow LIVE UDP WebVTT input (#1349)
An updated version of PR #1027
That previous PR was done using 2021 code, and there were many changes
in the codebase from there, so a rebase was needed and also some minor
tweak here and there. But it's the same code, just reimplemented on a
newer codebase.
If you want to take a look at this in action, after building shaka
packager with this PR's code included, try this commands in 3 different
simultaneous bash sessions:
1. Video UDP input: `ffmpeg -f lavfi -re -i
"testsrc=s=320x240:r=30,format=yuv420p" -c:v h264 -sc_threshold 0 -g 30
-keyint_min 30 -r 30 -a53cc 1 -b:v 150k -preset ultrafast -r 30 -f
mpegts "udp://127.0.0.1:10000?pkt_size=1316"`
2. WebVTT UDP input: `for sec in $(seq 0 9999) ; do printf
"%02d:%02d.000 --> %02d:%02d.000\ntest second ${sec}\n\n" "$(( ${sec} /
60 ))" "$(( ${sec} % 60 ))" "$(( (${sec} + 1) / 60 ))" "$(( (${sec} + 1)
% 60 ))" ; sleep 1 ; done > /dev/udp/127.0.0.1/12345`
3. shaka packager command line: `timeout 60
path/to/build/packager/packager
'in=udp://127.0.0.1:10000?timeout=8000000,stream_selector=0,init_segment=240_init.m4s,segment_template=240_$Number%09d$.m4s,bandwidth=150000'
'in=udp://127.0.0.1:12345?timeout=8000000,stream_selector=0,input_format=webvtt,format=webvtt+mp4,init_segment=text_init.m4s,segment_template=text_$Number%09d$.m4s,language=eng,dash_roles=subtitle'
--mpd_output ./manifest.mpd --segment_duration 3.2
--suggested_presentation_delay 3.2 --min_buffer_time 3.2
--minimum_update_period 3.2 --time_shift_buffer_depth 60
--preserved_segments_outside_live_window 1 --default_language=eng
--dump_stream_info 2>&1`
Note the added `input_format=webvtt` to the shaka packager command's
second selector. That's new from this PR. If you don't use that, shaka's
format autodetection will not detect the webvtt format from the input,
as explained in
https://github.com/shaka-project/shaka-packager/issues/685#issuecomment-1029407191.
Try the command without it if you want to.
Fixes #685
Fixes #1017
---------
Co-authored-by: Daniel Cantarín <canta@canta.com.ar>
2024-02-24 00:02:19 +00:00
|
|
|
"00:00:01.000 --> 00:00:02.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
feat: Allow LIVE UDP WebVTT input (#1349)
An updated version of PR #1027
That previous PR was done using 2021 code, and there were many changes
in the codebase from there, so a rebase was needed and also some minor
tweak here and there. But it's the same code, just reimplemented on a
newer codebase.
If you want to take a look at this in action, after building shaka
packager with this PR's code included, try this commands in 3 different
simultaneous bash sessions:
1. Video UDP input: `ffmpeg -f lavfi -re -i
"testsrc=s=320x240:r=30,format=yuv420p" -c:v h264 -sc_threshold 0 -g 30
-keyint_min 30 -r 30 -a53cc 1 -b:v 150k -preset ultrafast -r 30 -f
mpegts "udp://127.0.0.1:10000?pkt_size=1316"`
2. WebVTT UDP input: `for sec in $(seq 0 9999) ; do printf
"%02d:%02d.000 --> %02d:%02d.000\ntest second ${sec}\n\n" "$(( ${sec} /
60 ))" "$(( ${sec} % 60 ))" "$(( (${sec} + 1) / 60 ))" "$(( (${sec} + 1)
% 60 ))" ; sleep 1 ; done > /dev/udp/127.0.0.1/12345`
3. shaka packager command line: `timeout 60
path/to/build/packager/packager
'in=udp://127.0.0.1:10000?timeout=8000000,stream_selector=0,init_segment=240_init.m4s,segment_template=240_$Number%09d$.m4s,bandwidth=150000'
'in=udp://127.0.0.1:12345?timeout=8000000,stream_selector=0,input_format=webvtt,format=webvtt+mp4,init_segment=text_init.m4s,segment_template=text_$Number%09d$.m4s,language=eng,dash_roles=subtitle'
--mpd_output ./manifest.mpd --segment_duration 3.2
--suggested_presentation_delay 3.2 --min_buffer_time 3.2
--minimum_update_period 3.2 --time_shift_buffer_depth 60
--preserved_segments_outside_live_window 1 --default_language=eng
--dump_stream_info 2>&1`
Note the added `input_format=webvtt` to the shaka packager command's
second selector. That's new from this PR. If you don't use that, shaka's
format autodetection will not detect the webvtt format from the input,
as explained in
https://github.com/shaka-project/shaka-packager/issues/685#issuecomment-1029407191.
Try the command without it if you want to.
Fixes #685
Fixes #1017
---------
Co-authored-by: Daniel Cantarín <canta@canta.com.ar>
2024-02-24 00:02:19 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(streams_.empty());
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, FailToParseHeaderNotOneLine) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"WEBVTT\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
feat: Allow LIVE UDP WebVTT input (#1349)
An updated version of PR #1027
That previous PR was done using 2021 code, and there were many changes
in the codebase from there, so a rebase was needed and also some minor
tweak here and there. But it's the same code, just reimplemented on a
newer codebase.
If you want to take a look at this in action, after building shaka
packager with this PR's code included, try this commands in 3 different
simultaneous bash sessions:
1. Video UDP input: `ffmpeg -f lavfi -re -i
"testsrc=s=320x240:r=30,format=yuv420p" -c:v h264 -sc_threshold 0 -g 30
-keyint_min 30 -r 30 -a53cc 1 -b:v 150k -preset ultrafast -r 30 -f
mpegts "udp://127.0.0.1:10000?pkt_size=1316"`
2. WebVTT UDP input: `for sec in $(seq 0 9999) ; do printf
"%02d:%02d.000 --> %02d:%02d.000\ntest second ${sec}\n\n" "$(( ${sec} /
60 ))" "$(( ${sec} % 60 ))" "$(( (${sec} + 1) / 60 ))" "$(( (${sec} + 1)
% 60 ))" ; sleep 1 ; done > /dev/udp/127.0.0.1/12345`
3. shaka packager command line: `timeout 60
path/to/build/packager/packager
'in=udp://127.0.0.1:10000?timeout=8000000,stream_selector=0,init_segment=240_init.m4s,segment_template=240_$Number%09d$.m4s,bandwidth=150000'
'in=udp://127.0.0.1:12345?timeout=8000000,stream_selector=0,input_format=webvtt,format=webvtt+mp4,init_segment=text_init.m4s,segment_template=text_$Number%09d$.m4s,language=eng,dash_roles=subtitle'
--mpd_output ./manifest.mpd --segment_duration 3.2
--suggested_presentation_delay 3.2 --min_buffer_time 3.2
--minimum_update_period 3.2 --time_shift_buffer_depth 60
--preserved_segments_outside_live_window 1 --default_language=eng
--dump_stream_info 2>&1`
Note the added `input_format=webvtt` to the shaka packager command's
second selector. That's new from this PR. If you don't use that, shaka's
format autodetection will not detect the webvtt format from the input,
as explained in
https://github.com/shaka-project/shaka-packager/issues/685#issuecomment-1029407191.
Try the command without it if you want to.
Fixes #685
Fixes #1017
---------
Co-authored-by: Daniel Cantarín <canta@canta.com.ar>
2024-02-24 00:02:19 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(streams_.empty());
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, SendsStreamInfo) {
|
|
|
|
const uint8_t text[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:00.000 --> 00:01:00.000\n"
|
|
|
|
"Testing\n";
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
EXPECT_EQ(streams_[0]->time_scale(), kTimeScale);
|
|
|
|
EXPECT_EQ(streams_[0]->is_encrypted(), false);
|
|
|
|
EXPECT_EQ(streams_[0]->codec(), kCodecWebVtt);
|
|
|
|
EXPECT_EQ(streams_[0]->codec_string(), "wvtt");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:27:59 +00:00
|
|
|
TEST_F(WebVttParserTest, IgnoresZeroDurationCues) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2018-04-24 17:42:10 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
2018-08-17 20:27:59 +00:00
|
|
|
"00:01:00.000 --> 00:01:00.000\n"
|
|
|
|
"This subtitle would never show\n";
|
2018-04-24 17:42:10 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2018-04-24 17:42:10 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2018-04-24 17:42:10 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_TRUE(samples_.empty());
|
2018-04-24 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:27:59 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseOneCue) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
2018-08-17 20:27:59 +00:00
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
|
|
|
"subtitle\n";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
EXPECT_EQ(samples_[0]->id(), kNoId);
|
|
|
|
EXPECT_EQ(samples_[0]->start_time(), 60000u);
|
|
|
|
EXPECT_EQ(samples_[0]->duration(), 3540000u);
|
2022-06-02 16:27:47 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle");
|
|
|
|
|
|
|
|
// No settings
|
|
|
|
const auto& settings = samples_[0]->settings();
|
|
|
|
EXPECT_FALSE(settings.line);
|
|
|
|
EXPECT_FALSE(settings.position);
|
|
|
|
EXPECT_FALSE(settings.width);
|
|
|
|
EXPECT_FALSE(settings.height);
|
|
|
|
EXPECT_EQ(settings.region, "");
|
|
|
|
EXPECT_EQ(settings.writing_direction, WritingDirection::kHorizontal);
|
|
|
|
EXPECT_EQ(settings.text_alignment, TextAlignment::kCenter);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, ParseOneCueWithoutNewLine) {
|
|
|
|
const uint8_t text[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
|
|
|
"subtitle";
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
EXPECT_EQ(samples_[0]->id(), kNoId);
|
|
|
|
EXPECT_EQ(samples_[0]->start_time(), 60000u);
|
|
|
|
EXPECT_EQ(samples_[0]->duration(), 3540000u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle");
|
2020-08-26 19:31:58 +00:00
|
|
|
|
|
|
|
// No settings
|
|
|
|
const auto& settings = samples_[0]->settings();
|
|
|
|
EXPECT_FALSE(settings.line);
|
|
|
|
EXPECT_FALSE(settings.position);
|
2020-12-01 19:32:39 +00:00
|
|
|
EXPECT_FALSE(settings.width);
|
|
|
|
EXPECT_FALSE(settings.height);
|
2020-08-26 19:31:58 +00:00
|
|
|
EXPECT_EQ(settings.region, "");
|
|
|
|
EXPECT_EQ(settings.writing_direction, WritingDirection::kHorizontal);
|
|
|
|
EXPECT_EQ(settings.text_alignment, TextAlignment::kCenter);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseOneCueWithStyle) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
2018-08-17 20:27:59 +00:00
|
|
|
"STYLE\n"
|
|
|
|
"::cue { color:lime }\n"
|
|
|
|
"\n"
|
|
|
|
"REGION\n"
|
|
|
|
"id:scroll\n"
|
2020-08-26 21:21:09 +00:00
|
|
|
"scroll:up\n"
|
2018-08-17 20:27:59 +00:00
|
|
|
"\n"
|
2017-09-14 16:15:24 +00:00
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
2020-08-26 21:21:09 +00:00
|
|
|
auto* stream = static_cast<const TextStreamInfo*>(streams_[0].get());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
EXPECT_EQ(stream->css_styles(), "::cue { color:lime }");
|
2020-07-07 21:29:43 +00:00
|
|
|
EXPECT_EQ(samples_[0]->id(), kNoId);
|
|
|
|
EXPECT_EQ(samples_[0]->start_time(), 60000u);
|
|
|
|
EXPECT_EQ(samples_[0]->duration(), 3540000u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 23:39:41 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseOneEmptyCue) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2018-07-16 23:39:41 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2018-07-16 23:39:41 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2018-07-16 23:39:41 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "");
|
2018-07-16 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
TEST_F(WebVttParserTest, FailToParseCueWithArrowInId) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"-->\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_FALSE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, ParseOneCueWithId) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"id\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
EXPECT_EQ(samples_[0]->id(), "id");
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 23:39:41 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseOneEmptyCueWithId) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2018-07-16 23:39:41 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"id\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000\n"
|
|
|
|
"\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2018-07-16 23:39:41 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2018-07-16 23:39:41 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
EXPECT_EQ(samples_[0]->id(), "id");
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "");
|
2018-07-16 23:39:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseSettingSize) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000 size:50%\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
2020-12-01 19:32:39 +00:00
|
|
|
ASSERT_TRUE(samples_[0]->settings().width);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().width->type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().width->value, 50.0f);
|
2020-08-26 19:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(WebVttParserTest, ParseOneCueWithManySettings) {
|
|
|
|
const uint8_t text[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000 line:5 vertical:lr region:foo"
|
|
|
|
" align:right position:20%\n"
|
|
|
|
"subtitle\n";
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().writing_direction,
|
|
|
|
WritingDirection::kVerticalGrowingRight);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().text_alignment, TextAlignment::kRight);
|
2020-12-01 19:32:39 +00:00
|
|
|
EXPECT_FALSE(samples_[0]->settings().width);
|
2020-08-26 19:31:58 +00:00
|
|
|
ASSERT_TRUE(samples_[0]->settings().position);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().position->type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().position->value, 20.0f);
|
|
|
|
ASSERT_TRUE(samples_[0]->settings().line);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().line->type, TextUnitType::kLines);
|
|
|
|
EXPECT_EQ(samples_[0]->settings().line->value, 5.0f);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseRegions) {
|
|
|
|
const uint8_t text[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"REGION\n"
|
|
|
|
"id:foo\n"
|
|
|
|
"width:20%\n"
|
|
|
|
"lines:6\n"
|
|
|
|
"viewportanchor:25%,75%\n"
|
|
|
|
"scroll:up\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000 region:foo\n"
|
|
|
|
"subtitle\n";
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
|
|
|
|
auto* stream = static_cast<const TextStreamInfo*>(streams_[0].get());
|
|
|
|
const auto& regions = stream->regions();
|
|
|
|
ASSERT_EQ(regions.size(), 1u);
|
|
|
|
ASSERT_EQ(regions.count("foo"), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(samples_[0]->settings().region, "foo");
|
|
|
|
const auto& region = regions.at("foo");
|
|
|
|
EXPECT_EQ(region.width.value, 20.0f);
|
|
|
|
EXPECT_EQ(region.width.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(region.height.value, 6.0f);
|
|
|
|
EXPECT_EQ(region.height.type, TextUnitType::kLines);
|
|
|
|
EXPECT_EQ(region.window_anchor_x.value, 25.0f);
|
|
|
|
EXPECT_EQ(region.window_anchor_x.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(region.window_anchor_y.value, 75.0f);
|
|
|
|
EXPECT_EQ(region.window_anchor_y.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_TRUE(region.scroll);
|
|
|
|
}
|
|
|
|
|
2021-11-16 05:28:15 +00:00
|
|
|
TEST_F(WebVttParserTest, ParseRegionsMaxPercent) {
|
|
|
|
const uint8_t text[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"REGION\n"
|
|
|
|
"id:foo\n"
|
|
|
|
"width:20%\n"
|
|
|
|
"lines:6\n"
|
|
|
|
"viewportanchor:25%,100%\n"
|
|
|
|
"scroll:up\n"
|
|
|
|
"\n"
|
|
|
|
"00:01:00.000 --> 01:00:00.000 region:foo\n"
|
|
|
|
"subtitle\n";
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 1u);
|
|
|
|
|
|
|
|
auto* stream = static_cast<const TextStreamInfo*>(streams_[0].get());
|
|
|
|
const auto& regions = stream->regions();
|
|
|
|
ASSERT_EQ(regions.size(), 1u);
|
|
|
|
ASSERT_EQ(regions.count("foo"), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(samples_[0]->settings().region, "foo");
|
|
|
|
const auto& region = regions.at("foo");
|
|
|
|
EXPECT_EQ(region.width.value, 20.0f);
|
|
|
|
EXPECT_EQ(region.width.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(region.height.value, 6.0f);
|
|
|
|
EXPECT_EQ(region.height.type, TextUnitType::kLines);
|
|
|
|
EXPECT_EQ(region.window_anchor_x.value, 25.0f);
|
|
|
|
EXPECT_EQ(region.window_anchor_x.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_EQ(region.window_anchor_y.value, 100.0f);
|
|
|
|
EXPECT_EQ(region.window_anchor_y.type, TextUnitType::kPercent);
|
|
|
|
EXPECT_TRUE(region.scroll);
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
// Verify that a typical case with mulitple cues work.
|
|
|
|
TEST_F(WebVttParserTest, ParseMultipleCues) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:01.000 --> 00:00:05.200\n"
|
|
|
|
"subtitle A\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:02.321 --> 00:00:07.000\n"
|
|
|
|
"subtitle B\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:05.800 --> 00:00:08.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle C\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 3u);
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
EXPECT_EQ(samples_[0]->start_time(), 1000u);
|
|
|
|
EXPECT_EQ(samples_[0]->duration(), 4200u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle A");
|
2020-07-07 21:29:43 +00:00
|
|
|
EXPECT_EQ(samples_[1]->start_time(), 2321u);
|
|
|
|
EXPECT_EQ(samples_[1]->duration(), 4679u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[1]->body(), "subtitle B");
|
2020-07-07 21:29:43 +00:00
|
|
|
EXPECT_EQ(samples_[2]->start_time(), 5800u);
|
|
|
|
EXPECT_EQ(samples_[2]->duration(), 2200u);
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[2]->body(), "subtitle C");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that a typical case with mulitple cues work even when comments are
|
|
|
|
// present.
|
|
|
|
TEST_F(WebVttParserTest, ParseWithComments) {
|
2020-07-07 21:29:43 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"NOTE This is a one line comment\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:01.000 --> 00:00:05.200\n"
|
|
|
|
"subtitle A\n"
|
|
|
|
"\n"
|
|
|
|
"NOTE\n"
|
|
|
|
"This is a multi-line comment\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:02.321 --> 00:00:07.000\n"
|
|
|
|
"subtitle B\n"
|
|
|
|
"\n"
|
|
|
|
"NOTE This is a single line comment that\n"
|
|
|
|
"spans two lines\n"
|
|
|
|
"\n"
|
|
|
|
"NOTE\tThis is a comment that using a tab\n"
|
|
|
|
"\n"
|
|
|
|
"00:00:05.800 --> 00:00:08.000\n"
|
2018-01-03 00:35:38 +00:00
|
|
|
"subtitle C\n";
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
ASSERT_NO_FATAL_FAILURE(SetUpAndInitialize());
|
|
|
|
|
|
|
|
ASSERT_TRUE(parser_->Parse(text, sizeof(text) - 1));
|
|
|
|
ASSERT_TRUE(parser_->Flush());
|
|
|
|
|
|
|
|
ASSERT_EQ(streams_.size(), 1u);
|
|
|
|
ASSERT_EQ(samples_.size(), 3u);
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-08-26 20:47:14 +00:00
|
|
|
ExpectPlainCueWithBody(samples_[0]->body(), "subtitle A");
|
|
|
|
ExpectPlainCueWithBody(samples_[1]->body(), "subtitle B");
|
|
|
|
ExpectPlainCueWithBody(samples_[2]->body(), "subtitle C");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
2020-08-24 22:23:15 +00:00
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|