Shaka Packager SDK
ts_segmenter.h
1 // Copyright 2016 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_FORMATS_MP2T_TS_SEGMENTER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
9 
10 #include <memory>
11 
12 #include <packager/file.h>
13 #include <packager/macros/classes.h>
14 #include <packager/media/base/muxer_options.h>
15 #include <packager/media/formats/mp2t/pes_packet_generator.h>
16 #include <packager/media/formats/mp2t/ts_writer.h>
17 #include <packager/status.h>
18 
19 namespace shaka {
20 namespace media {
21 
22 class KeySource;
23 class MuxerListener;
24 
25 namespace mp2t {
26 
27 class TsSegmenter {
28  public:
29  // TODO(rkuroiwa): Add progress listener?
34  TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
35  ~TsSegmenter();
36 
40  Status Initialize(const StreamInfo& stream_info);
41 
44  Status Finalize();
45 
48  Status AddSample(const MediaSample& sample);
49 
58  // TODO(kqyang): Remove the usage of segment start timestamp and duration in
59  // xx_segmenter, which could cause confusions on which is the source of truth
60  // as the segment start timestamp and duration could be tracked locally.
61  Status FinalizeSegment(int64_t start_timestamp, int64_t duration);
62 
64  void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
65 
68  std::unique_ptr<PesPacketGenerator> generator);
69 
71  void SetSegmentStartedForTesting(bool value);
72 
73  int64_t segment_start_timestamp() const { return segment_start_timestamp_; }
74  BufferWriter* segment_buffer() { return &segment_buffer_; }
75  void set_segment_started(bool value) { segment_started_ = value; }
76  bool segment_started() const { return segment_started_; }
77 
78  double timescale() const { return timescale_scale_; }
79  uint32_t transport_stream_timestamp_offset() const {
80  return transport_stream_timestamp_offset_;
81  }
82 
83  private:
84  Status StartSegmentIfNeeded(int64_t next_pts);
85 
86  // Writes PES packets (carried in TsPackets) to a buffer.
87  Status WritePesPackets();
88 
89  MuxerListener* const listener_;
90 
91  // Codec for the stream.
92  Codec codec_ = kUnknownCodec;
93  std::vector<uint8_t> audio_codec_config_;
94 
95  const int32_t transport_stream_timestamp_offset_ = 0;
96  // Scale used to scale the input stream to TS's timesccale (which is 90000).
97 
98  // Used for calculating the duration in seconds fo the current segment.
99  double timescale_scale_ = 1.0;
100 
101  std::unique_ptr<TsWriter> ts_writer_;
102 
103  BufferWriter segment_buffer_;
104 
105  // Set to true if segment_buffer_ is initialized, set to false after
106  // FinalizeSegment() succeeds in ts_muxer.
107  bool segment_started_ = false;
108  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
109 
110  int64_t segment_start_timestamp_ = -1;
111  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
112 };
113 
114 } // namespace mp2t
115 } // namespace media
116 } // namespace shaka
117 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Class to hold a media sample.
Definition: media_sample.h:25
Abstract class holds stream information.
Definition: stream_info.h:71
Status FinalizeSegment(int64_t start_timestamp, int64_t duration)
void SetSegmentStartedForTesting(bool value)
Only for testing.
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:48
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
Status AddSample(const MediaSample &sample)
Definition: ts_segmenter.cc:74
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:39
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:19