7 #include <packager/media/formats/webvtt/webvtt_file_buffer.h>
9 #include <absl/log/check.h>
10 #include <absl/strings/str_format.h>
12 #include <packager/media/base/text_sample.h>
13 #include <packager/media/formats/webvtt/webvtt_utils.h>
18 const char* kHeader =
"WEBVTT\n";
19 const int kTsTimescale = 90000;
22 WebVttFileBuffer::WebVttFileBuffer(int32_t transport_stream_timestamp_offset_ms,
23 const std::string& style_region_config)
24 : transport_stream_timestamp_offset_(transport_stream_timestamp_offset_ms *
26 style_region_config_(style_region_config) {
32 void WebVttFileBuffer::Reset() {
36 buffer_.append(kHeader);
37 if (transport_stream_timestamp_offset_ > 0) {
39 absl::StrAppendFormat(&buffer_,
40 "X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:%d\n",
41 transport_stream_timestamp_offset_);
44 if (!style_region_config_.empty()) {
45 buffer_.append(style_region_config_);
46 buffer_.append(
"\n\n");
50 void WebVttFileBuffer::Append(
const TextSample& sample) {
51 DCHECK_GT(buffer_.size(), 0u) <<
"The buffer should at least have a header";
56 if (sample.id().length()) {
57 buffer_.append(sample.id());
62 buffer_.append(MsToWebVttTimestamp(sample.start_time()));
63 buffer_.append(
" --> ");
64 buffer_.append(MsToWebVttTimestamp(sample.EndTime()));
65 const std::string settings = WebVttSettingsToString(sample.settings());
66 if (!settings.empty()) {
68 buffer_.append(settings);
72 buffer_.append(WebVttFragmentToString(sample.body()));
77 bool WebVttFileBuffer::WriteTo(File* file, uint64_t* size) {
79 DCHECK_GT(buffer_.size(), 0u) <<
"The buffer should at least have a header";
82 *size = buffer_.size();
83 const int written = file->Write(buffer_.c_str(), buffer_.size());
88 return static_cast<size_t>(written) == buffer_.size();
All the methods that are virtual are virtual for mocking.