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