7 #include "packager/media/base/text_muxer.h"
9 #include "packager/media/base/muxer_util.h"
10 #include "packager/status_macros.h"
15 TextMuxer::TextMuxer(
const MuxerOptions& options) : Muxer(options) {}
16 TextMuxer::~TextMuxer() {}
18 Status TextMuxer::InitializeMuxer() {
19 if (streams().size() != 1 || streams()[0]->stream_type() != kStreamText) {
20 return Status(error::MUXER_FAILURE,
21 "Incorrect streams given to WebVTT muxer");
24 auto copy = streams()[0]->Clone();
25 RETURN_IF_ERROR(InitializeStream(
static_cast<TextStreamInfo*
>(copy.get())));
27 muxer_listener()->
OnMediaStart(options(), *copy, copy->time_scale(),
28 MuxerListener::kContainerText);
34 Status TextMuxer::Finalize() {
35 const float duration_ms =
static_cast<float>(total_duration_ms_);
36 float duration_seconds = duration_ms / 1000;
40 MuxerListener::MediaRanges ranges;
41 if (duration_seconds == 0 && last_cue_ms_ != 0) {
42 DCHECK(options().segment_template.empty());
43 duration_seconds =
static_cast<float>(last_cue_ms_) / 1000;
46 RETURN_IF_ERROR(WriteToFile(options().output_file_name, &size));
48 ranges.subsegment_ranges.emplace_back();
51 options().output_file_name, 0,
52 duration_seconds * streams()[0]->time_scale(), size);
55 muxer_listener()->
OnMediaEnd(ranges, duration_seconds);
60 Status TextMuxer::AddTextSample(
size_t stream_id,
const TextSample& sample) {
62 if (sample.body().is_empty()) {
66 RETURN_IF_ERROR(AddTextSampleInternal(sample));
68 last_cue_ms_ = sample.EndTime();
72 Status TextMuxer::FinalizeSegment(
size_t stream_id,
73 const SegmentInfo& segment_info) {
74 total_duration_ms_ += segment_info.duration;
77 DCHECK(!segment_template.empty());
78 const uint32_t index = segment_index_++;
79 const uint64_t start = segment_info.start_timestamp;
80 const uint64_t duration = segment_info.duration;
81 const uint32_t bandwidth = options().
bandwidth;
83 const std::string filename =
84 GetSegmentName(segment_template, start, index, bandwidth);
86 RETURN_IF_ERROR(WriteToFile(filename, &size));
88 muxer_listener()->
OnNewSegment(filename, start, duration, size);