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  const std::string kNoId = "";
49  auto filler = std::make_shared<TextSample>(kNoId, max_end_time_ms_,
50  sample.start_time(),
51  TextSettings{}, TextFragment{});
52  RETURN_IF_ERROR(
53  MediaHandler::DispatchTextSample(kStreamIndex, std::move(filler)));
54  }
55 
56  max_end_time_ms_ = std::max(max_end_time_ms_, sample.EndTime());
57  return Dispatch(std::move(data));
58 }
59 } // namespace media
60 } // namespace shaka
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::Status
Definition: status.h:110
shaka::media::MediaHandler::Dispatch
Status Dispatch(std::unique_ptr< StreamData > stream_data) const
Definition: media_handler.cc:94
shaka::media::MediaHandler::DispatchTextSample
Status DispatchTextSample(size_t stream_index, std::shared_ptr< const TextSample > text_sample) const
Dispatch the text sample to downstream handlers.
Definition: media_handler.h:216
shaka::media::TextPadder::TextPadder
TextPadder(int64_t zero_start_bias_ms)
Definition: text_padder.cc:19