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 SetTsWriterFileOpenedForTesting(bool value);
72 
73  private:
74  Status OpenNewSegmentIfClosed(uint32_t next_pts);
75 
76  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
77  // it will open one. This will not close the file.
78  Status WritePesPacketsToFile();
79 
80  const MuxerOptions& muxer_options_;
81  MuxerListener* const listener_;
82 
83  // Codec for the stream.
84  Codec codec_ = kUnknownCodec;
85  std::vector<uint8_t> audio_codec_config_;
86 
87  const uint32_t transport_stream_timestamp_offset_ = 0;
88  // Scale used to scale the input stream to TS's timesccale (which is 90000).
89  // Used for calculating the duration in seconds fo the current segment.
90  double timescale_scale_ = 1.0;
91 
92  // Used for segment template.
93  uint64_t segment_number_ = 0;
94 
95  std::unique_ptr<TsWriter> ts_writer_;
96  // Set to true if TsWriter::NewFile() succeeds, set to false after
97  // TsWriter::FinalizeFile() succeeds.
98  bool ts_writer_file_opened_ = false;
99  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
100 
101  // For OnNewSegment().
102  // Path of the current segment so that File::GetFileSize() can be used after
103  // the segment has been finalized.
104  std::string current_segment_path_;
105 
106  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
107 };
108 
109 } // namespace mp2t
110 } // namespace media
111 } // namespace shaka
112 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Abstract class holds stream information.
Definition: stream_info.h:61
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:46
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
Status AddSample(const MediaSample &sample)
Definition: ts_segmenter.cc:74
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:36
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
Class to hold a media sample.
Definition: media_sample.h:22
Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration)