DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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/media/base/muxer_options.h"
12 #include "packager/media/file/file.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(std::shared_ptr<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  // Scale used to scale the input stream to TS's timesccale (which is 90000).
84  // Used for calculating the duration in seconds fo the current segment.
85  double timescale_scale_ = 1.0;
86 
87  // Used for segment template.
88  uint64_t segment_number_ = 0;
89 
90  std::unique_ptr<TsWriter> ts_writer_;
91  // Set to true if TsWriter::NewFile() succeeds, set to false after
92  // TsWriter::FinalizeFile() succeeds.
93  bool ts_writer_file_opened_ = false;
94  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
95 
96  // For OnNewSegment().
97  // Path of the current segment so that File::GetFileSize() can be used after
98  // the segment has been finalized.
99  std::string current_segment_path_;
100 
101  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
102 };
103 
104 } // namespace mp2t
105 } // namespace media
106 } // namespace shaka
107 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Abstract class holds stream information.
Definition: stream_info.h:57
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:30
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
Status AddSample(std::shared_ptr< MediaSample > sample)
Definition: ts_segmenter.cc:48
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:23
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
Definition: ts_segmenter.cc:66
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
Definition: ts_segmenter.cc:71
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
Definition: ts_segmenter.cc:62
Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration)