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 edash_packager {
19 namespace media {
20 
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:
34  TsSegmenter(const MuxerOptions& options, MuxerListener* listener);
35  ~TsSegmenter();
36 
40  Status Initialize(const StreamInfo& stream_info);
41 
44  Status Finalize();
45 
48  Status AddSample(scoped_refptr<MediaSample> sample);
49 
51  void InjectTsWriterForTesting(scoped_ptr<TsWriter> writer);
52 
55  scoped_ptr<PesPacketGenerator> generator);
56 
58  void SetTsWriterFileOpenedForTesting(bool value);
59 
60  private:
61  Status OpenNewSegmentIfClosed(uint32_t next_pts);
62 
63  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
64  // it will open one. This will not close the file.
65  Status WritePesPacketsToFile();
66 
67  // Flush all the samples that are (possibly) buffered and write them to the
68  // current segment, this will close the file. If a file is not already opened
69  // before calling this, this will open one and write them to file.
70  Status Flush();
71 
72  const MuxerOptions& muxer_options_;
73  MuxerListener* const listener_;
74 
75  // Scale used to scale the input stream to TS's timesccale (which is 90000).
76  // Used for calculating the duration in seconds fo the current segment.
77  double timescale_scale_ = 1.0;
78 
79  // This is the sum of the durations of the samples that were added to
80  // PesPacketGenerator for the current segment (in seconds). Note that this is
81  // not necessarily the same as the length of the PesPackets that have been
82  // written to the current segment in WritePesPacketsToFile().
83  double current_segment_total_sample_duration_ = 0.0;
84 
85  // Used for segment template.
86  uint64_t segment_number_ = 0;
87 
88  scoped_ptr<TsWriter> ts_writer_;
89  // Set to true if TsWriter::NewFile() succeeds, set to false after
90  // TsWriter::FinalizeFile() succeeds.
91  bool ts_writer_file_opened_ = false;
92  scoped_ptr<PesPacketGenerator> pes_packet_generator_;
93 
94  // For OnNewSegment().
95  uint64_t current_segment_start_time_ = 0;
96  // Path of the current segment so that File::GetFileSize() can be used after
97  // the segment has been finalized.
98  std::string current_segment_path_;
99 
100  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
101 };
102 
103 } // namespace mp2t
104 } // namespace media
105 } // namespace edash_packager
106 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:30
Abstract class holds stream information.
Definition: stream_info.h:26
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
Definition: ts_segmenter.cc:84
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:23
void InjectTsWriterForTesting(scoped_ptr< TsWriter > writer)
Only for testing.
Definition: ts_segmenter.cc:75
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: ts_segmenter.cc:51
void InjectPesPacketGeneratorForTesting(scoped_ptr< PesPacketGenerator > generator)
Only for testing.
Definition: ts_segmenter.cc:79
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18