7 #include "packager/media/formats/webvtt/webvtt_segmenter.h" 12 const size_t kStreamIndex = 0;
15 WebVttSegmenter::WebVttSegmenter(uint64_t segment_duration_ms)
16 : segment_duration_ms_(segment_duration_ms) {}
18 Status WebVttSegmenter::InitializeInternal() {
23 switch (stream_data->stream_data_type) {
24 case StreamDataType::kStreamInfo:
25 return DispatchStreamInfo(kStreamIndex,
26 std::move(stream_data->stream_info));
27 case StreamDataType::kTextSample:
28 return OnTextSample(stream_data->text_sample);
30 return Status(error::INTERNAL_ERROR,
31 "Invalid stream data type for this handler");
38 for (
const auto& pair : segment_map_) {
39 Status status = DispatchSegmentWithSamples(pair.first, pair.second);
48 return FlushAllDownstreams();
51 Status WebVttSegmenter::OnTextSample(std::shared_ptr<const TextSample> sample) {
52 const uint64_t start_segment = sample->start_time() / segment_duration_ms_;
56 DCHECK_GT(sample->duration(), 0u);
57 const uint64_t ending_segment =
58 (sample->EndTime() - 1) / segment_duration_ms_;
60 DCHECK_GE(ending_segment, start_segment);
64 if (head_segment_ > start_segment) {
65 LOG(WARNING) <<
"New sample has arrived out of order. Skipping sample " 66 <<
"as segment start is " << start_segment <<
" and segment " 67 <<
"head is " << head_segment_ <<
".";
72 for (uint64_t segment = start_segment; segment <= ending_segment; segment++) {
73 segment_map_[segment].push_back(sample);
78 for (uint64_t segment = head_segment_; segment < start_segment; segment++) {
79 auto it = segment_map_.find(segment);
82 if (it == segment_map_.end()) {
83 const WebVttSegmentSamples kNoSamples;
84 status.
Update(DispatchSegmentWithSamples(segment, kNoSamples));
88 status.
Update(DispatchSegmentWithSamples(segment, it->second));
89 segment_map_.erase(it);
100 head_segment_ = start_segment;
105 Status WebVttSegmenter::DispatchSegmentWithSamples(
107 const WebVttSegmentSamples& samples) {
109 for (
const auto& sample : samples) {
110 status.
Update(DispatchTextSample(kStreamIndex, sample));
118 std::shared_ptr<SegmentInfo> info = std::make_shared<SegmentInfo>();
119 info->start_timestamp = segment * segment_duration_ms_;
120 info->duration = segment_duration_ms_;
122 return DispatchSegmentInfo(kStreamIndex, std::move(info));
All the methods that are virtual are virtual for mocking.
void Update(Status new_status)