DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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 namespace mp2t {
21 
22 // TODO(rkuroiwa): For now, this implements multifile segmenter. Like other
23 // make this an abstract super class and implement multifile and single file
24 // segmenters.
25 class TsSegmenter {
26  public:
29  explicit TsSegmenter(const MuxerOptions& options);
30  ~TsSegmenter();
31 
35  Status Initialize(const StreamInfo& stream_info);
36 
39  Status Finalize();
40 
43  Status AddSample(scoped_refptr<MediaSample> sample);
44 
46  void InjectTsWriterForTesting(scoped_ptr<TsWriter> writer);
47 
50  scoped_ptr<PesPacketGenerator> generator);
51 
53  void SetTsWriterFileOpenedForTesting(bool value);
54 
55  private:
56  Status OpenNewSegmentIfClosed(uint32_t next_pts);
57 
58  // Writes PES packets (carried in TsPackets) to a file. If a file is not open,
59  // it will open one. This will not close the file.
60  Status WritePesPacketsToFile();
61 
62  // Flush all the samples that are (possibly) buffered and write them to the
63  // current segment, this will close the file. If a file is not already opened
64  // before calling this, this will open one and write them to file.
65  Status Flush();
66 
67  const MuxerOptions& muxer_options_;
68 
69  // Scale used to scale the input stream to TS's timesccale (which is 90000).
70  // Used for calculating the duration in seconds fo the current segment.
71  double timescale_scale_ = 1.0;
72 
73  // This is the sum of the durations of the samples that were added to
74  // PesPacketGenerator for the current segment (in seconds). Note that this is
75  // not necessarily the same as the length of the PesPackets that have been
76  // written to the current segment in WritePesPacketsToFile().
77  double current_segment_total_sample_duration_ = 0.0;
78 
79  // Used for segment template.
80  uint64_t segment_number_ = 0;
81 
82  scoped_ptr<TsWriter> ts_writer_;
83  // Set to true if TsWriter::NewFile() succeeds, set to false after
84  // TsWriter::FinalizeFile() succeeds.
85  bool ts_writer_file_opened_ = false;
86  scoped_ptr<PesPacketGenerator> pes_packet_generator_;
87 
88  DISALLOW_COPY_AND_ASSIGN(TsSegmenter);
89 };
90 
91 } // namespace mp2t
92 } // namespace media
93 } // namespace edash_packager
94 #endif // PACKAGER_MEDIA_FORMATS_MP2T_TS_SEGMENTER_H_
Status Initialize(const StreamInfo &stream_info)
Definition: ts_segmenter.cc:28
Abstract class holds stream information.
Definition: stream_info.h:26
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
Definition: ts_segmenter.cc:82
void InjectTsWriterForTesting(scoped_ptr< TsWriter > writer)
Only for testing.
Definition: ts_segmenter.cc:73
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: ts_segmenter.cc:49
void InjectPesPacketGeneratorForTesting(scoped_ptr< PesPacketGenerator > generator)
Only for testing.
Definition: ts_segmenter.cc:77
TsSegmenter(const MuxerOptions &options)
Definition: ts_segmenter.cc:22
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18