7 #include <packager/media/base/text_muxer.h>
9 #include <absl/log/check.h>
11 #include <packager/macros/compiler.h>
12 #include <packager/macros/status.h>
13 #include <packager/media/base/muxer_util.h>
18 TextMuxer::TextMuxer(
const MuxerOptions& options) : Muxer(options) {}
19 TextMuxer::~TextMuxer() {}
21 Status TextMuxer::InitializeMuxer() {
22 if (streams().size() != 1 || streams()[0]->stream_type() != kStreamText) {
23 return Status(error::MUXER_FAILURE,
24 "Incorrect streams given to WebVTT muxer");
27 auto copy = streams()[0]->Clone();
28 RETURN_IF_ERROR(InitializeStream(
static_cast<TextStreamInfo*
>(copy.get())));
30 muxer_listener()->OnMediaStart(options(), *copy, copy->time_scale(),
31 MuxerListener::kContainerText);
37 Status TextMuxer::Finalize() {
38 const float duration_ms =
static_cast<float>(total_duration_ms_);
39 float duration_seconds = duration_ms / 1000;
43 MuxerListener::MediaRanges ranges;
44 if (duration_seconds == 0 && last_cue_ms_ != 0) {
45 DCHECK(options().segment_template.empty());
46 duration_seconds =
static_cast<float>(last_cue_ms_) / 1000;
49 RETURN_IF_ERROR(WriteToFile(options().output_file_name, &size));
51 ranges.subsegment_ranges.emplace_back();
54 const uint32_t kArbitrarySegmentNumber = 0;
56 muxer_listener()->OnNewSegment(
57 options().output_file_name, 0,
58 duration_seconds * streams()[0]->time_scale(), size,
59 kArbitrarySegmentNumber);
62 muxer_listener()->OnMediaEnd(ranges, duration_seconds);
67 Status TextMuxer::AddTextSample(
size_t stream_id,
const TextSample& sample) {
71 if (sample.body().is_empty()) {
75 RETURN_IF_ERROR(AddTextSampleInternal(sample));
77 last_cue_ms_ = sample.EndTime();
81 Status TextMuxer::FinalizeSegment(
size_t stream_id,
82 const SegmentInfo& segment_info) {
85 total_duration_ms_ += segment_info.duration;
87 const std::string& segment_template = options().segment_template;
88 DCHECK(!segment_template.empty());
90 const int64_t start = segment_info.start_timestamp;
91 const int64_t duration = segment_info.duration;
92 const uint32_t segment_number = segment_info.segment_number;
94 const uint32_t bandwidth = options().bandwidth;
96 const std::string filename =
97 GetSegmentName(segment_template, start, segment_number, bandwidth);
99 RETURN_IF_ERROR(WriteToFile(filename, &size));
101 muxer_listener()->OnNewSegment(filename, start, duration, size,
All the methods that are virtual are virtual for mocking.