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 
58  void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
59 
62  std::unique_ptr<PesPacketGenerator> generator);
63 
65  void SetTsWriterFileOpenedForTesting(bool value);
66 
67  private:
68  Status OpenNewSegmentIfClosed(uint32_t next_pts);
69 
70  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
71  // it will open one. This will not close the file.
72  Status WritePesPacketsToFile();
73 
74  // Flush all the samples that are (possibly) buffered and write them to the
75  // current segment, this will close the file. If a file is not already opened
76  // before calling this, this will open one and write them to file.
77  Status Flush();
78 
79  // If conditions are met, notify objects that the data is encrypted.
80  Status NotifyEncrypted();
81 
82  const MuxerOptions& muxer_options_;
83  MuxerListener* const listener_;
84 
85  // Scale used to scale the input stream to TS's timesccale (which is 90000).
86  // Used for calculating the duration in seconds fo the current segment.
87  double timescale_scale_ = 1.0;
88 
89  // This is the sum of the durations of the samples that were added to
90  // PesPacketGenerator for the current segment (in seconds). Note that this is
91  // not necessarily the same as the length of the PesPackets that have been
92  // written to the current segment in WritePesPacketsToFile().
93  double current_segment_total_sample_duration_ = 0.0;
94 
95  // Used for segment template.
96  uint64_t segment_number_ = 0;
97 
98  std::unique_ptr<TsWriter> ts_writer_;
99  // Set to true if TsWriter::NewFile() succeeds, set to false after
100  // TsWriter::FinalizeFile() succeeds.
101  bool ts_writer_file_opened_ = false;
102  std::unique_ptr<PesPacketGenerator> pes_packet_generator_;
103 
104  // For OnNewSegment().
105  uint64_t current_segment_start_time_ = 0;
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:57
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:91
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.
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
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30