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/base/status.h"
13 #include "packager/media/file/file.h"
14 #include "packager/media/formats/mp2t/pes_packet_generator.h"
15 #include "packager/media/formats/mp2t/ts_writer.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 
42  Status Initialize(const StreamInfo& stream_info,
43  KeySource* encryption_key_source,
44  uint32_t max_sd_pixels,
45  uint32_t max_hd_pixels,
46  uint32_t max_uhd1_pixels,
47  double clear_lead_in_seconds);
48 
51  Status Finalize();
52 
55  Status AddSample(std::shared_ptr<MediaSample> sample);
56 
64  // TODO(kqyang): Remove the usage of segment start timestamp and duration in
65  // xx_segmenter, which could cause confusions on which is the source of truth
66  // as the segment start timestamp and duration could be tracked locally.
67  Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration);
68 
70  void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
71 
74  std::unique_ptr<PesPacketGenerator> generator);
75 
77  void SetTsWriterFileOpenedForTesting(bool value);
78 
79  private:
80  Status OpenNewSegmentIfClosed(uint32_t next_pts);
81 
82  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
83  // it will open one. This will not close the file.
84  Status WritePesPacketsToFile();
85 
86  // If conditions are met, notify objects that the data is encrypted.
87  Status NotifyEncrypted();
88 
89  const MuxerOptions& muxer_options_;
90  MuxerListener* const listener_;
91 
92  // Scale used to scale the input stream to TS's timesccale (which is 90000).
93  // Used for calculating the duration in seconds fo the current segment.
94  double timescale_scale_ = 1.0;
95 
96  // Used for segment template.
97  uint64_t segment_number_ = 0;
98 
99  std::unique_ptr<TsWriter> ts_writer_;
100  // Set to true if TsWriter::NewFile() succeeds, set to false after
101  // TsWriter::FinalizeFile() succeeds.
102  bool ts_writer_file_opened_ = false;
103  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
104 
105  // For OnNewSegment().
106  // Path of the current segment so that File::GetFileSize() can be used after
107  // the segment has been finalized.
108  std::string current_segment_path_;
109 
110  std::unique_ptr<EncryptionKey> encryption_key_;
111  double clear_lead_in_seconds_ = 0;
112 
113  // The total duration of the segments that it has segmented. This only
114  // includes segments that have been finailzed. IOW, this does not count the
115  // current segments duration.
116  double total_duration_in_seconds_ = 0.0;
117 
118  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
119 };
120 
121 } // namespace mp2t
122 } // namespace media
123 } // namespace shaka
124 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Abstract class holds stream information.
Definition: stream_info.h:58
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:88
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:27
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.
Definition: ts_segmenter.cc:99
Status Initialize(const StreamInfo &stream_info, KeySource *encryption_key_source, uint32_t max_sd_pixels, uint32_t max_hd_pixels, uint32_t max_uhd1_pixels, double clear_lead_in_seconds)
Definition: ts_segmenter.cc:34
Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30