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