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 "packager/base/memory/scoped_ptr.h"
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  double clear_lead_in_seconds);
47 
50  Status Finalize();
51 
54  Status AddSample(scoped_refptr<MediaSample> sample);
55 
57  void InjectTsWriterForTesting(scoped_ptr<TsWriter> writer);
58 
61  scoped_ptr<PesPacketGenerator> generator);
62 
64  void SetTsWriterFileOpenedForTesting(bool value);
65 
66  private:
67  Status OpenNewSegmentIfClosed(uint32_t next_pts);
68 
69  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
70  // it will open one. This will not close the file.
71  Status WritePesPacketsToFile();
72 
73  // Flush all the samples that are (possibly) buffered and write them to the
74  // current segment, this will close the file. If a file is not already opened
75  // before calling this, this will open one and write them to file.
76  Status Flush();
77 
78  // If conditions are met, notify objects that the data is encrypted.
79  Status NotifyEncrypted();
80 
81  const MuxerOptions& muxer_options_;
82  MuxerListener* const listener_;
83 
84  // Scale used to scale the input stream to TS's timesccale (which is 90000).
85  // Used for calculating the duration in seconds fo the current segment.
86  double timescale_scale_ = 1.0;
87 
88  // This is the sum of the durations of the samples that were added to
89  // PesPacketGenerator for the current segment (in seconds). Note that this is
90  // not necessarily the same as the length of the PesPackets that have been
91  // written to the current segment in WritePesPacketsToFile().
92  double current_segment_total_sample_duration_ = 0.0;
93 
94  // Used for segment template.
95  uint64_t segment_number_ = 0;
96 
97  scoped_ptr<TsWriter> ts_writer_;
98  // Set to true if TsWriter::NewFile() succeeds, set to false after
99  // TsWriter::FinalizeFile() succeeds.
100  bool ts_writer_file_opened_ = false;
101  scoped_ptr<PesPacketGenerator> pes_packet_generator_;
102 
103  // For OnNewSegment().
104  uint64_t current_segment_start_time_ = 0;
105  // Path of the current segment so that File::GetFileSize() can be used after
106  // the segment has been finalized.
107  std::string current_segment_path_;
108 
109  scoped_ptr<EncryptionKey> encryption_key_;
110  double clear_lead_in_seconds_ = 0;
111 
112  // The total duration of the segments that it has segmented. This only
113  // includes segments that have been finailzed. IOW, this does not count the
114  // current segments duration.
115  double total_duration_in_seconds_ = 0.0;
116 
117  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
118 };
119 
120 } // namespace mp2t
121 } // namespace media
122 } // namespace shaka
123 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Abstract class holds stream information.
Definition: stream_info.h:26
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: ts_segmenter.cc:88
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
void InjectTsWriterForTesting(scoped_ptr< TsWriter > writer)
Only for testing.
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:27
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
void InjectPesPacketGeneratorForTesting(scoped_ptr< PesPacketGenerator > generator)
Only for testing.
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
Status Initialize(const StreamInfo &stream_info, KeySource *encryption_key_source, uint32_t max_sd_pixels, double clear_lead_in_seconds)
Definition: ts_segmenter.cc:34