Shaka Packager SDK
text_padder.cc
1 // Copyright 2018 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/webvtt/text_padder.h"
8 
9 #include <algorithm>
10 
11 #include "packager/status_macros.h"
12 
13 namespace shaka {
14 namespace media {
15 namespace {
16 const uint64_t kStreamIndex = 0;
17 } // namespace
18 
19 TextPadder::TextPadder(int64_t duration_ms) : duration_ms_(duration_ms) {}
20 
21 Status TextPadder::InitializeInternal() {
22  return Status::OK;
23 }
24 
25 Status TextPadder::Process(std::unique_ptr<StreamData> data) {
26  DCHECK_EQ(data->stream_index, kStreamIndex);
27  const bool is_text_sample =
28  data->stream_data_type == StreamDataType::kTextSample;
29  return is_text_sample ? OnTextSample(std::move(data))
30  : Dispatch(std::move(data));
31 }
32 
33 Status TextPadder::OnFlushRequest(size_t index) {
34  if (duration_ms_ > max_end_time_ms_) {
35  std::shared_ptr<TextSample> filler = std::make_shared<TextSample>();
36  filler->SetTime(max_end_time_ms_, duration_ms_);
37  RETURN_IF_ERROR(
38  MediaHandler::DispatchTextSample(kStreamIndex, std::move(filler)));
39  }
40 
41  return FlushDownstream(index);
42 }
43 
44 Status TextPadder::OnTextSample(std::unique_ptr<StreamData> data) {
45  const TextSample& sample = *data->text_sample;
46 
47  // Check if there will be a gap between samples if we just dispatch this
48  // sample right away. If there will be one, create an empty sample that will
49  // fill in that gap.
50  if (sample.start_time() > max_end_time_ms_) {
51  std::shared_ptr<TextSample> filler = std::make_shared<TextSample>();
52  filler->SetTime(max_end_time_ms_, sample.start_time());
53  RETURN_IF_ERROR(
54  MediaHandler::DispatchTextSample(kStreamIndex, std::move(filler)));
55  }
56 
57  max_end_time_ms_ = std::max(max_end_time_ms_, sample.EndTime());
58  return Dispatch(std::move(data));
59 }
60 } // namespace media
61 } // namespace shaka
Status Dispatch(std::unique_ptr< StreamData > stream_data) const
All the methods that are virtual are virtual for mocking.
TextPadder(int64_t duration_ms)
Create a new text padder that will ensure the stream&#39;s duration is.
Definition: text_padder.cc:19
Status DispatchTextSample(size_t stream_index, std::shared_ptr< const TextSample > text_sample) const
Dispatch the text sample to downstream handlers.
Status FlushDownstream(size_t output_stream_index)
Flush the downstream connected at the specified output stream index.