Shaka Packager SDK
Loading...
Searching...
No Matches
text_chunker.h
1// Copyright 2017 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#ifndef PACKAGER_MEDIA_CHUNKING_TEXT_CHUNKER_H_
8#define PACKAGER_MEDIA_CHUNKING_TEXT_CHUNKER_H_
9
10#include <cstddef>
11#include <cstdint>
12#include <list>
13#include <memory>
14
15#include <packager/chunking_params.h>
16#include <packager/media/base/media_handler.h>
17#include <packager/media/base/stream_info.h>
18#include <packager/media/base/text_sample.h>
19#include <packager/status.h>
20
21namespace shaka {
22namespace media {
23
24// Media handler for taking a single stream of text samples and inserting
25// segment info based on a fixed segment duration and on cue events. The
26// only time a segment's duration will not match the fixed segment duration
27// is when a cue event is seen.
28class TextChunker : public MediaHandler {
29 public:
30 explicit TextChunker(double segment_duration_in_seconds,
31 int64_t start_segment_number);
32 explicit TextChunker(double segment_duration_in_seconds,
33 int64_t start_segment_number,
34 int64_t ts_ttx_heartbeat_shift);
35 explicit TextChunker(double segment_duration_in_seconds,
36 int64_t start_segment_number,
37 int64_t ts_ttx_heartbeat_shift,
38 bool use_segment_coordinator);
39
40 private:
41 TextChunker(const TextChunker&) = delete;
42 TextChunker& operator=(const TextChunker&) = delete;
43
44 Status InitializeInternal() override { return Status::OK; }
45
46 Status Process(std::unique_ptr<StreamData> stream_data) override;
47 Status OnFlushRequest(size_t input_stream_index) override;
48
49 Status OnStreamInfo(std::shared_ptr<const StreamInfo> info);
50 Status OnCueEvent(std::shared_ptr<const CueEvent> cue);
51 Status OnTextSample(std::shared_ptr<const TextSample> sample);
52 Status OnSegmentInfo(std::shared_ptr<const SegmentInfo> info);
53
54 // This does two things that should always happen together:
55 // 1. Dispatch all the samples and a segment info for the time range
56 // segment_start_ to segment_start_ + duration
57 // 2. Set the next segment to start at segment_start_ + duration and
58 // remove all samples that don't last into that segment.
59 Status DispatchSegment(int64_t duration);
60
61 // Creates cropped copies of ongoing cues (samples_without_end_) that span
62 // into the current segment and adds them to samples_in_current_segment_.
63 // This must be called before DispatchSegment to ensure ongoing cues are
64 // included in the segment output.
65 void AddOngoingCuesToCurrentSegment(int64_t segment_end);
66
67 int64_t ScaleTime(double seconds) const;
68
69 double segment_duration_in_seconds_;
70
71 int64_t time_scale_ = -1; // Set in OnStreamInfo
72
73 // Time values are in scaled units.
74 int64_t segment_start_ = -1; // Set when the first sample comes in.
75 int64_t segment_duration_ = -1; // Set in OnStreamInfo.
76
77 // Segment number that keeps monotonically increasing.
78 // Set to start_segment_number in constructor.
79 int64_t segment_number_ = 1;
80
81 // A shift in PTS values for text heart beats from other MPEG-2 TS
82 // elementary streams. Can be set from command line.
83 int64_t ts_ttx_heartbeat_shift_ = kDefaultTtxHeartbeatShift;
84
85 // Used to check if media heart beats are coming before text timestamps
86 // This value has the shift applied and is used for warnings
87 int64_t latest_media_heartbeat_time_ = -1;
88
89 // All samples that make up the current segment. We must store the samples
90 // until the segment ends because a cue event may end the segment sooner
91 // than we expected.
92 std::list<std::shared_ptr<const TextSample>> samples_in_current_segment_;
93
94 // For live input which we cannot wait for sample end time since
95 // it may come after the current segment is supposed to finish.
96 // By storing them in this list we can retrieve them and crop them
97 // to the segment interval before adding them to samples_in_current_segment_
98 std::list<std::shared_ptr<const TextSample>> samples_without_end_;
99
100 // When true, TextChunker uses SegmentInfo from SegmentCoordinator to align
101 // segment boundaries with video/audio streams. When false (default for
102 // non-teletext streams), uses mathematical calculation.
103 bool use_segment_coordinator_ = false;
104};
105
106} // namespace media
107} // namespace shaka
108
109#endif // PACKAGER_MEDIA_CHUNKING_TEXT_CHUNKER_H_
All the methods that are virtual are virtual for mocking.