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/media_stream.h"
12 #include "packager/media/base/muxer_options.h"
13 #include "packager/media/base/status.h"
14 #include "packager/media/file/file.h"
15 #include "packager/media/formats/mp2t/pes_packet_generator.h"
16 #include "packager/media/formats/mp2t/ts_writer.h"
17 
18 namespace shaka {
19 namespace media {
20 
21 class KeySource;
22 class MuxerListener;
23 
24 namespace mp2t {
25 
26 // TODO(rkuroiwa): For now, this implements multifile segmenter. Like other
27 // make this an abstract super class and implement multifile and single file
28 // segmenters.
29 class TsSegmenter {
30  public:
31  // TODO(rkuroiwa): Add progress listener?
36  TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
37  ~TsSegmenter();
38 
43  Status Initialize(const StreamInfo& stream_info,
44  KeySource* encryption_key_source,
45  uint32_t max_sd_pixels,
46  uint32_t max_hd_pixels,
47  uint32_t max_uhd1_pixels,
48  double clear_lead_in_seconds);
49 
52  Status Finalize();
53 
56  Status AddSample(scoped_refptr<MediaSample> sample);
57 
59  void InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer);
60 
63  std::unique_ptr<PesPacketGenerator> generator);
64 
66  void SetTsWriterFileOpenedForTesting(bool value);
67 
68  private:
69  Status OpenNewSegmentIfClosed(uint32_t next_pts);
70 
71  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
72  // it will open one. This will not close the file.
73  Status WritePesPacketsToFile();
74 
75  // Flush all the samples that are (possibly) buffered and write them to the
76  // current segment, this will close the file. If a file is not already opened
77  // before calling this, this will open one and write them to file.
78  Status Flush();
79 
80  // If conditions are met, notify objects that the data is encrypted.
81  Status NotifyEncrypted();
82 
83  const MuxerOptions& muxer_options_;
84  MuxerListener* const listener_;
85 
86  // Scale used to scale the input stream to TS's timesccale (which is 90000).
87  // Used for calculating the duration in seconds fo the current segment.
88  double timescale_scale_ = 1.0;
89 
90  // This is the sum of the durations of the samples that were added to
91  // PesPacketGenerator for the current segment (in seconds). Note that this is
92  // not necessarily the same as the length of the PesPackets that have been
93  // written to the current segment in WritePesPacketsToFile().
94  double current_segment_total_sample_duration_ = 0.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  uint64_t current_segment_start_time_ = 0;
107  // Path of the current segment so that File::GetFileSize() can be used after
108  // the segment has been finalized.
109  std::string current_segment_path_;
110 
111  std::unique_ptr<EncryptionKey> encryption_key_;
112  double clear_lead_in_seconds_ = 0;
113 
114  // The total duration of the segments that it has segmented. This only
115  // includes segments that have been finailzed. IOW, this does not count the
116  // current segments duration.
117  double total_duration_in_seconds_ = 0.0;
118 
119  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
120 };
121 
122 } // namespace mp2t
123 } // namespace media
124 } // namespace shaka
125 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Abstract class holds stream information.
Definition: stream_info.h:53
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: ts_segmenter.cc:91
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
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