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 zero_start_bias_ms)
20  : zero_start_bias_ms_(zero_start_bias_ms) {}
21 
22 Status TextPadder::InitializeInternal() {
23  return Status::OK;
24 }
25 
26 Status TextPadder::Process(std::unique_ptr<StreamData> data) {
27  DCHECK_EQ(data->stream_index, kStreamIndex);
28  const bool is_text_sample =
29  data->stream_data_type == StreamDataType::kTextSample;
30  return is_text_sample ? OnTextSample(std::move(data))
31  : Dispatch(std::move(data));
32 }
33 
34 Status TextPadder::OnTextSample(std::unique_ptr<StreamData> data) {
35  const TextSample& sample = *data->text_sample;
36 
37  // If this is the first sample we have seen, we need to check if we should
38  // start at time zero.
39  if (max_end_time_ms_ < 0) {
40  max_end_time_ms_ =
41  sample.start_time() > zero_start_bias_ms_ ? sample.start_time() : 0;
42  }
43 
44  // Check if there will be a gap between samples if we just dispatch this
45  // sample right away. If there will be one, create an empty sample that will
46  // fill in that gap.
47  if (sample.start_time() > max_end_time_ms_) {
48  std::shared_ptr<TextSample> filler = std::make_shared<TextSample>();
49  filler->SetTime(max_end_time_ms_, sample.start_time());
50  RETURN_IF_ERROR(
51  MediaHandler::DispatchTextSample(kStreamIndex, std::move(filler)));
52  }
53 
54  max_end_time_ms_ = std::max(max_end_time_ms_, sample.EndTime());
55  return Dispatch(std::move(data));
56 }
57 } // namespace media
58 } // namespace shaka
Status Dispatch(std::unique_ptr< StreamData > stream_data) const
All the methods that are virtual are virtual for mocking.
Status DispatchTextSample(size_t stream_index, std::shared_ptr< const TextSample > text_sample) const
Dispatch the text sample to downstream handlers.
TextPadder(int64_t zero_start_bias_ms)
Definition: text_padder.cc:19