7 #include "packager/media/formats/webvtt/webvtt_file_buffer.h"
9 #include "packager/base/strings/stringprintf.h"
10 #include "packager/media/base/text_sample.h"
11 #include "packager/media/formats/webvtt/webvtt_utils.h"
16 const char* kHeader =
"WEBVTT\n";
17 const int kTsTimescale = 90000;
20 WebVttFileBuffer::WebVttFileBuffer(
21 uint32_t transport_stream_timestamp_offset_ms,
22 const std::string& style_region_config)
23 : transport_stream_timestamp_offset_(transport_stream_timestamp_offset_ms *
25 style_region_config_(style_region_config) {
31 void WebVttFileBuffer::Reset() {
35 buffer_.append(kHeader);
36 if (transport_stream_timestamp_offset_ > 0) {
38 base::StringAppendF(&buffer_,
39 "X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:%d\n",
40 transport_stream_timestamp_offset_);
43 if (!style_region_config_.empty()) {
44 buffer_.append(style_region_config_);
45 buffer_.append(
"\n\n");
49 void WebVttFileBuffer::Append(
const TextSample& sample) {
50 DCHECK_GT(buffer_.size(), 0u) <<
"The buffer should at least have a header";
55 if (sample.id().length()) {
56 buffer_.append(sample.id());
61 buffer_.append(MsToWebVttTimestamp(sample.start_time()));
62 buffer_.append(
" --> ");
63 buffer_.append(MsToWebVttTimestamp(sample.EndTime()));
64 const std::string settings = WebVttSettingsToString(sample.settings());
65 if (!settings.empty()) {
67 buffer_.append(settings);
71 buffer_.append(WebVttFragmentToString(sample.body()));
76 bool WebVttFileBuffer::WriteTo(File* file, uint64_t* size) {
78 DCHECK_GT(buffer_.size(), 0u) <<
"The buffer should at least have a header";
81 *size = buffer_.size();
82 const int written = file->Write(buffer_.c_str(), buffer_.size());
87 return static_cast<size_t>(written) == buffer_.size();