7 #include "packager/media/chunking/text_chunker.h" 9 #include "packager/status_macros.h" 14 const size_t kStreamIndex = 0;
16 std::shared_ptr<const SegmentInfo> MakeSegmentInfo(int64_t start_ms,
18 DCHECK_LT(start_ms, end_ms);
20 std::shared_ptr<SegmentInfo> info = std::make_shared<SegmentInfo>();
21 info->start_timestamp = start_ms;
22 info->duration = end_ms - start_ms;
28 TextChunker::TextChunker(int64_t segment_duration_ms)
29 : segment_duration_ms_(segment_duration_ms),
31 segment_expected_end_ms_(segment_duration_ms) {}
33 Status TextChunker::InitializeInternal() {
37 Status TextChunker::Process(std::unique_ptr<StreamData> data) {
38 switch (data->stream_data_type) {
39 case StreamDataType::kStreamInfo:
40 return OnStreamInfo(std::move(data->stream_info));
41 case StreamDataType::kTextSample:
42 return OnTextSample(data->text_sample);
43 case StreamDataType::kCueEvent:
44 return OnCueEvent(data->cue_event);
46 return Status(error::INTERNAL_ERROR,
47 "Invalid stream data type for this handler");
51 Status TextChunker::OnFlushRequest(
size_t input_stream_index) {
53 while (segment_samples_.size()) {
54 RETURN_IF_ERROR(EndSegment(segment_expected_end_ms_));
57 return FlushAllDownstreams();
60 Status TextChunker::OnStreamInfo(std::shared_ptr<const StreamInfo> info) {
63 return DispatchStreamInfo(kStreamIndex, std::move(info));
66 Status TextChunker::OnCueEvent(std::shared_ptr<const CueEvent> event) {
69 const int64_t cue_time_in_ms =
event->time_in_seconds * 1000;
74 while (segment_expected_end_ms_ < cue_time_in_ms) {
75 RETURN_IF_ERROR(EndSegment(segment_expected_end_ms_));
78 RETURN_IF_ERROR(EndSegment(cue_time_in_ms));
79 RETURN_IF_ERROR(DispatchCueEvent(kStreamIndex, std::move(event)));
84 Status TextChunker::OnTextSample(std::shared_ptr<const TextSample> sample) {
86 while (segment_expected_end_ms_ <= sample->start_time()) {
87 RETURN_IF_ERROR(EndSegment(segment_expected_end_ms_));
90 segment_samples_.push_back(std::move(sample));
95 Status TextChunker::EndSegment(int64_t segment_actual_end_ms) {
97 for (
const auto& sample : segment_samples_) {
98 RETURN_IF_ERROR(DispatchTextSample(kStreamIndex, sample));
101 RETURN_IF_ERROR(DispatchSegmentInfo(
102 kStreamIndex, MakeSegmentInfo(segment_start_ms_, segment_actual_end_ms)));
106 StartNewSegment(segment_actual_end_ms);
111 void TextChunker::StartNewSegment(int64_t start_ms) {
112 segment_start_ms_ = start_ms;
113 segment_expected_end_ms_ = start_ms + segment_duration_ms_;
116 segment_samples_.remove_if(
117 [start_ms](
const std::shared_ptr<const TextSample>& sample) {
118 return sample->EndTime() <= start_ms;
All the methods that are virtual are virtual for mocking.