Shaka Packager SDK
text_padder.cc
1 // Copyright 2018 Google LLC. 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 <absl/log/check.h>
12 
13 #include <packager/macros/status.h>
14 
15 namespace shaka {
16 namespace media {
17 namespace {
18 const uint64_t kStreamIndex = 0;
19 } // namespace
20 
21 TextPadder::TextPadder(int64_t zero_start_bias_ms)
22  : zero_start_bias_ms_(zero_start_bias_ms) {}
23 
24 Status TextPadder::InitializeInternal() {
25  return Status::OK;
26 }
27 
28 Status TextPadder::Process(std::unique_ptr<StreamData> data) {
29  DCHECK_EQ(data->stream_index, kStreamIndex);
30  const bool is_text_sample =
31  data->stream_data_type == StreamDataType::kTextSample;
32  return is_text_sample ? OnTextSample(std::move(data))
33  : Dispatch(std::move(data));
34 }
35 
36 Status TextPadder::OnTextSample(std::unique_ptr<StreamData> data) {
37  const TextSample& sample = *data->text_sample;
38 
39  // If this is the first sample we have seen, we need to check if we should
40  // start at time zero.
41  if (max_end_time_ms_ < 0) {
42  max_end_time_ms_ =
43  zero_start_bias_ms_ && sample.start_time() > zero_start_bias_ms_
44  ? sample.start_time()
45  : 0;
46  }
47 
48  // Check if there will be a gap between samples if we just dispatch this
49  // sample right away. If there will be one, create an empty sample that will
50  // fill in that gap.
51  if (sample.start_time() > max_end_time_ms_) {
52  const std::string kNoId = "";
53  auto filler = std::make_shared<TextSample>(kNoId, max_end_time_ms_,
54  sample.start_time(),
55  TextSettings{}, TextFragment{});
56  RETURN_IF_ERROR(
57  MediaHandler::DispatchTextSample(kStreamIndex, std::move(filler)));
58  }
59 
60  max_end_time_ms_ = std::max(max_end_time_ms_, sample.EndTime());
61  return Dispatch(std::move(data));
62 }
63 } // namespace media
64 } // namespace shaka
Status DispatchTextSample(size_t stream_index, std::shared_ptr< const TextSample > text_sample) const
Dispatch the text sample to downstream handlers.
Status Dispatch(std::unique_ptr< StreamData > stream_data) const
TextPadder(int64_t zero_start_bias_ms)
Definition: text_padder.cc:21
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66