Shaka Packager SDK
ts_segmenter.h
1 // Copyright 2016 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 #ifndef PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
9 
10 #include <memory>
11 #include "packager/file/file.h"
12 #include "packager/media/base/muxer_options.h"
13 #include "packager/media/formats/mp2t/pes_packet_generator.h"
14 #include "packager/media/formats/mp2t/ts_writer.h"
15 #include "packager/status.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 class KeySource;
21 class MuxerListener;
22 
23 namespace mp2t {
24 
25 // TODO(rkuroiwa): For now, this implements multifile segmenter. Like other
26 // make this an abstract super class and implement multifile and single file
27 // segmenters.
28 class TsSegmenter {
29  public:
30  // TODO(rkuroiwa): Add progress listener?
35  TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
36  ~TsSegmenter();
37 
41  Status Initialize(const StreamInfo& stream_info);
42 
45  Status Finalize();
46 
49  Status AddSample(const MediaSample& sample);
50 
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(uint64_t start_timestamp, uint64_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  private:
74  Status StartSegmentIfNeeded(int64_t next_pts);
75 
76  // Writes PES packets (carried in TsPackets) to a buffer.
77  Status WritePesPackets();
78 
79  const MuxerOptions& muxer_options_;
80  MuxerListener* const listener_;
81 
82  // Codec for the stream.
83  Codec codec_ = kUnknownCodec;
84  std::vector<uint8_t> audio_codec_config_;
85 
86  const uint32_t transport_stream_timestamp_offset_ = 0;
87  // Scale used to scale the input stream to TS's timesccale (which is 90000).
88  // Used for calculating the duration in seconds fo the current segment.
89  double timescale_scale_ = 1.0;
90 
91  // Used for segment template.
92  uint64_t segment_number_ = 0;
93 
94  std::unique_ptr<TsWriter> ts_writer_;
95 
96  BufferWriter segment_buffer_;
97 
98  // Set to true if segment_buffer_ is initialized, set to false after
99  // FinalizeSegment() succeeds.
100  bool segment_started_ = false;
101  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
102 
103  int64_t segment_start_timestamp_ = -1;
104  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
105 };
106 
107 } // namespace mp2t
108 } // namespace media
109 } // namespace shaka
110 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Class to hold a media sample.
Definition: media_sample.h:22
Abstract class holds stream information.
Definition: stream_info.h:65
void SetSegmentStartedForTesting(bool value)
Only for testing.
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:47
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration)
Status AddSample(const MediaSample &sample)
Definition: ts_segmenter.cc:75
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:37
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20