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>
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
#include <absl/strings/str_split.h>
|
|
|
|
|
|
|
|
#include <packager/kv_pairs/kv_pairs.h>
|
|
|
|
#include <packager/media/base/text_stream_info.h>
|
|
|
|
#include <packager/media/formats/webvtt/webvtt_utils.h>
|
|
|
|
#include <packager/utils/string_trim_split.h>
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
namespace {
|
2020-06-30 21:31:07 +00:00
|
|
|
|
2018-03-23 17:13:44 +00:00
|
|
|
const uint64_t kStreamIndex = 0;
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2018-03-01 21:46:08 +00:00
|
|
|
std::string BlockToString(const std::string* block, size_t size) {
|
|
|
|
std::string out = " --- BLOCK START ---\n";
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
out.append(" ");
|
|
|
|
out.append(block[i]);
|
|
|
|
out.append("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
out.append(" --- BLOCK END ---");
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
// Comments are just blocks that are preceded by a blank line, start with the
|
|
|
|
// word "NOTE" (followed by a space or newline), and end at the first blank
|
|
|
|
// line.
|
|
|
|
// SOURCE: https://www.w3.org/TR/webvtt1
|
|
|
|
bool IsLikelyNote(const std::string& line) {
|
2023-12-01 17:32:19 +00:00
|
|
|
return line == "NOTE" || absl::StartsWith(line, "NOTE ") ||
|
|
|
|
absl::StartsWith(line, "NOTE\t");
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// As cue time is the only part of a WEBVTT file that is allowed to have
|
|
|
|
// "-->" appear, then if the given line contains it, we can safely assume
|
|
|
|
// that the line is likely to be a cue time.
|
|
|
|
bool IsLikelyCueTiming(const std::string& line) {
|
|
|
|
return line.find("-->") != std::string::npos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A WebVTT cue identifier is any sequence of one or more characters not
|
|
|
|
// containing the substring "-->" (U+002D HYPHEN-MINUS, U+002D HYPHEN-MINUS,
|
|
|
|
// U+003E GREATER-THAN SIGN), nor containing any U+000A LINE FEED (LF)
|
|
|
|
// characters or U+000D CARRIAGE RETURN (CR) characters.
|
|
|
|
// SOURCE: https://www.w3.org/TR/webvtt1/#webvtt-cue-identifier
|
|
|
|
bool MaybeCueId(const std::string& line) {
|
|
|
|
return line.find("-->") == std::string::npos;
|
|
|
|
}
|
2018-04-24 17:42:10 +00:00
|
|
|
|
|
|
|
// Check to see if the block is likely a style block. Style blocks are
|
|
|
|
// identified as any block that starts with a line that only contains
|
|
|
|
// "STYLE".
|
|
|
|
// SOURCE: https://w3c.github.io/webvtt/#styling
|
|
|
|
bool IsLikelyStyle(const std::string& line) {
|
2023-12-01 17:32:19 +00:00
|
|
|
return absl::StripTrailingAsciiWhitespace(line) == "STYLE";
|
2018-04-24 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if the block is likely a region block. Region blocks are
|
|
|
|
// identified as any block that starts with a line that only contains
|
|
|
|
// "REGION".
|
|
|
|
// SOURCE: https://w3c.github.io/webvtt/#webvtt-region
|
|
|
|
bool IsLikelyRegion(const std::string& line) {
|
2023-12-01 17:32:19 +00:00
|
|
|
return absl::StripTrailingAsciiWhitespace(line) == "REGION";
|
2018-04-24 17:42:10 +00:00
|
|
|
}
|
2018-08-17 20:27:59 +00:00
|
|
|
|
2020-08-26 19:31:58 +00:00
|
|
|
bool ParsePercent(const std::string& str, float* value) {
|
|
|
|
// https://www.w3.org/TR/webvtt1/#webvtt-percentage
|
|
|
|
// E.g. "4%" or "1.5%"
|
2021-05-03 05:53:50 +00:00
|
|
|
if (str[str.size() - 1] != '%') {
|
2020-08-26 19:31:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
double temp;
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtod(str.substr(0, str.size() - 1), &temp) || temp > 100) {
|
2020-08-26 19:31:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*value = temp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
bool ParseDoublePercent(const std::string& str, float* a, float* b) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::vector<std::string> percents = SplitAndTrimSkipEmpty(str, ',');
|
|
|
|
|
2021-05-03 05:53:50 +00:00
|
|
|
if (percents.size() != 2) {
|
2020-08-26 21:21:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 05:53:50 +00:00
|
|
|
float temp_a, temp_b;
|
|
|
|
if (!ParsePercent(percents[0], &temp_a) ||
|
|
|
|
!ParsePercent(percents[1], &temp_b)) {
|
2020-08-26 21:21:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-03 05:53:50 +00:00
|
|
|
*a = temp_a;
|
|
|
|
*b = temp_b;
|
2020-08-26 21:21:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-26 19:31:58 +00:00
|
|
|
void ParseSettings(const std::string& id,
|
|
|
|
const std::string& value,
|
|
|
|
TextSettings* settings) {
|
|
|
|
// https://www.w3.org/TR/webvtt1/#ref-for-parse-the-webvtt-cue-settings-1
|
|
|
|
if (id == "region") {
|
|
|
|
settings->region = value;
|
|
|
|
} else if (id == "vertical") {
|
|
|
|
if (value == "rl") {
|
|
|
|
settings->writing_direction = WritingDirection::kVerticalGrowingLeft;
|
|
|
|
} else if (value == "lr") {
|
|
|
|
settings->writing_direction = WritingDirection::kVerticalGrowingRight;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Invalid WebVTT vertical setting: " << value;
|
|
|
|
}
|
|
|
|
} else if (id == "line") {
|
|
|
|
const auto pos = value.find(',');
|
|
|
|
const std::string line = value.substr(0, pos);
|
|
|
|
const std::string align =
|
|
|
|
pos != std::string::npos ? value.substr(pos + 1) : "";
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
LOG(WARNING) << "WebVTT line alignment isn't supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!line.empty() && line[line.size() - 1] == '%') {
|
|
|
|
float temp;
|
|
|
|
if (!ParsePercent(line, &temp)) {
|
|
|
|
LOG(WARNING) << "Invalid WebVTT line: " << value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
settings->line.emplace(temp, TextUnitType::kPercent);
|
|
|
|
} else {
|
|
|
|
double temp;
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtod(line, &temp)) {
|
2020-08-26 19:31:58 +00:00
|
|
|
LOG(WARNING) << "Invalid WebVTT line: " << value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
settings->line.emplace(temp, TextUnitType::kLines);
|
|
|
|
}
|
|
|
|
} else if (id == "position") {
|
|
|
|
const auto pos = value.find(',');
|
|
|
|
const std::string position = value.substr(0, pos);
|
|
|
|
const std::string align =
|
|
|
|
pos != std::string::npos ? value.substr(pos + 1) : "";
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
LOG(WARNING) << "WebVTT position alignment isn't supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
float temp;
|
|
|
|
if (ParsePercent(position, &temp)) {
|
|
|
|
settings->position.emplace(temp, TextUnitType::kPercent);
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Invalid WebVTT position: " << value;
|
|
|
|
}
|
|
|
|
} else if (id == "size") {
|
|
|
|
float temp;
|
|
|
|
if (ParsePercent(value, &temp)) {
|
2020-12-01 19:32:39 +00:00
|
|
|
settings->width.emplace(temp, TextUnitType::kPercent);
|
2020-08-26 19:31:58 +00:00
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Invalid WebVTT size: " << value;
|
|
|
|
}
|
|
|
|
} else if (id == "align") {
|
|
|
|
if (value == "start") {
|
|
|
|
settings->text_alignment = TextAlignment::kStart;
|
2021-01-19 19:45:20 +00:00
|
|
|
} else if (value == "center" || value == "middle") {
|
2020-08-26 19:31:58 +00:00
|
|
|
settings->text_alignment = TextAlignment::kCenter;
|
|
|
|
} else if (value == "end") {
|
|
|
|
settings->text_alignment = TextAlignment::kEnd;
|
|
|
|
} else if (value == "left") {
|
|
|
|
settings->text_alignment = TextAlignment::kLeft;
|
|
|
|
} else if (value == "right") {
|
|
|
|
settings->text_alignment = TextAlignment::kRight;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Invalid WebVTT align: " << value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Unknown WebVTT setting: " << id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
WebVttParser::WebVttParser() {}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
void WebVttParser::Init(const InitCB& init_cb,
|
|
|
|
const NewMediaSampleCB& new_media_sample_cb,
|
|
|
|
const NewTextSampleCB& new_text_sample_cb,
|
|
|
|
KeySource* decryption_key_source) {
|
2023-12-01 17:32:19 +00:00
|
|
|
DCHECK(init_cb_ == nullptr);
|
|
|
|
DCHECK(init_cb != nullptr);
|
|
|
|
DCHECK(new_text_sample_cb != nullptr);
|
2020-07-07 21:29:43 +00:00
|
|
|
DCHECK(!decryption_key_source) << "Encrypted WebVTT not supported";
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
init_cb_ = init_cb;
|
|
|
|
new_text_sample_cb_ = new_text_sample_cb;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool WebVttParser::Flush() {
|
|
|
|
reader_.Flush();
|
|
|
|
return Parse();
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool WebVttParser::Parse(const uint8_t* buf, int size) {
|
|
|
|
reader_.PushData(buf, size);
|
|
|
|
return Parse();
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool WebVttParser::Parse() {
|
|
|
|
if (!initialized_) {
|
|
|
|
std::vector<std::string> block;
|
|
|
|
if (!reader_.Next(&block)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
// Check the header. It is possible for a 0xFEFF BOM to come before the
|
|
|
|
// header text.
|
|
|
|
if (block.size() != 1) {
|
|
|
|
LOG(ERROR) << "Failed to read WEBVTT header - "
|
|
|
|
<< "block size should be 1 but was " << block.size() << ".";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (block[0] != "WEBVTT" && block[0] != "\xEF\xBB\xBFWEBVTT") {
|
|
|
|
LOG(ERROR) << "Failed to read WEBVTT header - should be WEBVTT but was "
|
|
|
|
<< block[0];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
initialized_ = true;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
std::vector<std::string> block;
|
|
|
|
while (reader_.Next(&block)) {
|
|
|
|
if (!ParseBlock(block))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool WebVttParser::ParseBlock(const std::vector<std::string>& block) {
|
|
|
|
// NOTE
|
|
|
|
if (IsLikelyNote(block[0])) {
|
|
|
|
// We can safely ignore the whole block.
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 17:42:10 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
// STYLE
|
|
|
|
if (IsLikelyStyle(block[0])) {
|
|
|
|
if (saw_cue_) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Found style block after seeing cue. Ignoring style block";
|
|
|
|
} else {
|
2020-08-26 21:21:09 +00:00
|
|
|
for (size_t i = 1; i < block.size(); i++) {
|
|
|
|
if (!css_styles_.empty())
|
|
|
|
css_styles_ += "\n";
|
|
|
|
css_styles_ += block[i];
|
|
|
|
}
|
2018-04-24 17:42:10 +00:00
|
|
|
}
|
2020-07-07 21:29:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-24 17:42:10 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
// REGION
|
|
|
|
if (IsLikelyRegion(block[0])) {
|
|
|
|
if (saw_cue_) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Found region block after seeing cue. Ignoring region block";
|
2020-08-26 21:21:09 +00:00
|
|
|
return true;
|
2020-07-07 21:29:43 +00:00
|
|
|
} else {
|
2020-08-26 21:21:09 +00:00
|
|
|
return ParseRegion(block);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
2020-07-07 21:29:43 +00:00
|
|
|
}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
// CUE with ID
|
|
|
|
if (block.size() >= 2 && MaybeCueId(block[0]) &&
|
|
|
|
IsLikelyCueTiming(block[1]) && ParseCueWithId(block)) {
|
|
|
|
saw_cue_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
// CUE with no ID
|
|
|
|
if (IsLikelyCueTiming(block[0]) && ParseCueWithNoId(block)) {
|
|
|
|
saw_cue_ = true;
|
|
|
|
return true;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
LOG(ERROR) << "Failed to determine block classification:\n"
|
|
|
|
<< BlockToString(block.data(), block.size());
|
|
|
|
return false;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
bool WebVttParser::ParseRegion(const std::vector<std::string>& block) {
|
|
|
|
TextRegion region;
|
|
|
|
std::string region_id;
|
|
|
|
// Fill in defaults. Some may already be this, but set them anyway.
|
|
|
|
// See https://www.w3.org/TR/webvtt1/#regions
|
|
|
|
region.width.value = 100;
|
|
|
|
region.width.type = TextUnitType::kPercent;
|
|
|
|
region.height.value = 3;
|
|
|
|
region.height.type = TextUnitType::kLines;
|
|
|
|
region.window_anchor_x.value = 0;
|
|
|
|
region.window_anchor_x.type = TextUnitType::kPercent;
|
|
|
|
region.window_anchor_y.value = 100;
|
|
|
|
region.window_anchor_y.type = TextUnitType::kPercent;
|
|
|
|
region.region_anchor_x.value = 0;
|
|
|
|
region.region_anchor_x.type = TextUnitType::kPercent;
|
|
|
|
region.region_anchor_y.value = 100;
|
|
|
|
region.region_anchor_y.type = TextUnitType::kPercent;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (const auto& line : block) {
|
|
|
|
// First line is "REGION", skip.
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::vector<KVPair> kv_pairs = SplitStringIntoKeyValuePairs(line, ':', ' ');
|
|
|
|
|
|
|
|
for (const auto& pair : kv_pairs) {
|
2020-08-26 21:21:09 +00:00
|
|
|
const std::string& value = pair.second;
|
|
|
|
if (pair.first == "id") {
|
|
|
|
if (value.find("-->") != std::string::npos) {
|
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION ID: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (regions_.find(value) != regions_.end()) {
|
|
|
|
LOG(ERROR) << "Duplicate WebVTT REGION: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
region_id = value;
|
|
|
|
} else if (pair.first == "width") {
|
|
|
|
if (!ParsePercent(value, ®ion.width.value)) {
|
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION width: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pair.first == "lines") {
|
|
|
|
unsigned int temp;
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!absl::SimpleAtoi(value, &temp)) {
|
2020-08-26 21:21:09 +00:00
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION lines: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
region.height.value = temp;
|
|
|
|
} else if (pair.first == "regionanchor") {
|
|
|
|
if (!ParseDoublePercent(value, ®ion.region_anchor_x.value,
|
|
|
|
®ion.region_anchor_y.value)) {
|
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION regionanchor: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pair.first == "viewportanchor") {
|
|
|
|
if (!ParseDoublePercent(value, ®ion.window_anchor_x.value,
|
|
|
|
®ion.window_anchor_y.value)) {
|
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION windowanchor: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (pair.first == "scroll") {
|
|
|
|
if (value != "up") {
|
|
|
|
LOG(ERROR) << "Invalid WebVTT REGION scroll: " << value;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
region.scroll = true;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unknown WebVTT REGION setting: " << pair.first;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (region_id.empty()) {
|
|
|
|
LOG(ERROR) << "WebVTT REGION id is required";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
regions_.insert(std::make_pair(region_id, std::move(region)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
bool WebVttParser::ParseCueWithNoId(const std::vector<std::string>& block) {
|
2020-07-07 21:29:43 +00:00
|
|
|
return ParseCue("", block.data(), block.size());
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebVttParser::ParseCueWithId(const std::vector<std::string>& block) {
|
2020-07-07 21:29:43 +00:00
|
|
|
return ParseCue(block[0], block.data() + 1, block.size() - 1);
|
2018-03-01 21:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
bool WebVttParser::ParseCue(const std::string& id,
|
|
|
|
const std::string* block,
|
|
|
|
size_t block_size) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::vector<std::string> time_and_style =
|
|
|
|
SplitAndTrimSkipEmpty(block[0], ' ');
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t start_time = 0;
|
|
|
|
int64_t end_time = 0;
|
2018-03-01 21:46:08 +00:00
|
|
|
|
|
|
|
const bool parsed_time =
|
|
|
|
time_and_style.size() >= 3 && time_and_style[1] == "-->" &&
|
2017-09-14 16:15:24 +00:00
|
|
|
WebVttTimestampToMs(time_and_style[0], &start_time) &&
|
2018-03-01 21:46:08 +00:00
|
|
|
WebVttTimestampToMs(time_and_style[2], &end_time);
|
|
|
|
|
|
|
|
if (!parsed_time) {
|
2020-07-07 21:29:43 +00:00
|
|
|
LOG(ERROR) << "Could not parse start time, -->, and end time from "
|
|
|
|
<< block[0];
|
|
|
|
return false;
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 20:27:59 +00:00
|
|
|
if (!stream_info_dispatched_)
|
2020-07-07 21:29:43 +00:00
|
|
|
DispatchTextStreamInfo();
|
2018-08-17 20:27:59 +00:00
|
|
|
|
2018-07-09 21:39:33 +00:00
|
|
|
// According to the WebVTT spec end time must be greater than the start time
|
|
|
|
// of the cue. Since we are seeing content with invalid times in the field, we
|
|
|
|
// are going to drop the cue instead of failing to package.
|
|
|
|
//
|
|
|
|
// For more context see:
|
|
|
|
// - https://www.w3.org/TR/webvtt1/#webvtt-cue-timings
|
2022-03-07 19:56:34 +00:00
|
|
|
// - https://github.com/shaka-project/shaka-packager/issues/335
|
|
|
|
// - https://github.com/shaka-project/shaka-packager/issues/425
|
2018-03-01 21:46:08 +00:00
|
|
|
//
|
|
|
|
// Print a warning so that those packaging content can know that their
|
|
|
|
// content is not spec compliant.
|
2018-07-09 21:39:33 +00:00
|
|
|
if (end_time <= start_time) {
|
|
|
|
LOG(WARNING) << "WebVTT input is not spec compliant. Start time ("
|
|
|
|
<< start_time << ") should be less than end time (" << end_time
|
|
|
|
<< "). Skipping webvtt cue:"
|
2018-03-01 21:46:08 +00:00
|
|
|
<< BlockToString(block, block_size);
|
2020-07-07 21:29:43 +00:00
|
|
|
return true;
|
2018-03-01 21:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 22:23:15 +00:00
|
|
|
TextSettings settings;
|
2017-09-14 16:15:24 +00:00
|
|
|
for (size_t i = 3; i < time_and_style.size(); i++) {
|
2020-08-26 19:31:58 +00:00
|
|
|
const auto pos = time_and_style[i].find(':');
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
continue;
|
2020-08-24 22:23:15 +00:00
|
|
|
}
|
2020-08-26 19:31:58 +00:00
|
|
|
|
|
|
|
const std::string key = time_and_style[i].substr(0, pos);
|
|
|
|
const std::string value = time_and_style[i].substr(pos + 1);
|
|
|
|
ParseSettings(key, value, &settings);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The rest of the block is the payload.
|
2020-08-26 19:31:58 +00:00
|
|
|
// TODO: Parse tags to support <b>, <i>, etc.
|
2020-08-24 22:23:15 +00:00
|
|
|
TextFragment body;
|
2020-08-26 20:47:14 +00:00
|
|
|
TextFragmentStyle no_styles;
|
2017-09-14 16:15:24 +00:00
|
|
|
for (size_t i = 1; i < block_size; i++) {
|
2022-06-02 16:27:47 +00:00
|
|
|
if (i > 1 && i != block_size) {
|
2020-08-26 20:47:14 +00:00
|
|
|
body.sub_fragments.emplace_back(no_styles, /* newline= */ true);
|
2020-08-24 22:23:15 +00:00
|
|
|
}
|
2020-08-26 20:47:14 +00:00
|
|
|
body.sub_fragments.emplace_back(no_styles, block[i]);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 19:31:58 +00:00
|
|
|
const auto sample =
|
2020-08-24 22:23:15 +00:00
|
|
|
std::make_shared<TextSample>(id, start_time, end_time, settings, body);
|
2023-12-01 17:32:19 +00:00
|
|
|
return new_text_sample_cb_(kStreamIndex, sample);
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 21:29:43 +00:00
|
|
|
void WebVttParser::DispatchTextStreamInfo() {
|
2018-08-17 20:27:59 +00:00
|
|
|
stream_info_dispatched_ = true;
|
|
|
|
|
2018-03-23 17:13:44 +00:00
|
|
|
const int kTrackId = 0;
|
2017-09-14 16:15:24 +00:00
|
|
|
// The resolution of timings are in milliseconds.
|
|
|
|
const int kTimescale = 1000;
|
|
|
|
// The duration passed here is not very important. Also the whole file
|
|
|
|
// must be read before determining the real duration which doesn't
|
|
|
|
// work nicely with the current demuxer.
|
|
|
|
const int kDuration = 0;
|
|
|
|
const char kWebVttCodecString[] = "wvtt";
|
2018-03-23 17:13:44 +00:00
|
|
|
const int64_t kNoWidth = 0;
|
|
|
|
const int64_t kNoHeight = 0;
|
2020-07-07 21:29:43 +00:00
|
|
|
// The language of the stream will be overwritten by the Demuxer later.
|
|
|
|
const char kNoLanguage[] = "";
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
const auto stream = std::make_shared<TextStreamInfo>(
|
|
|
|
kTrackId, kTimescale, kDuration, kCodecWebVtt, kWebVttCodecString, "",
|
|
|
|
kNoWidth, kNoHeight, kNoLanguage);
|
|
|
|
stream->set_css_styles(css_styles_);
|
|
|
|
for (const auto& pair : regions_)
|
|
|
|
stream->AddRegion(pair.first, pair.second);
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<StreamInfo>> streams{stream};
|
2023-12-01 17:32:19 +00:00
|
|
|
init_cb_(streams);
|
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
|