DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
segmenter.h
1 // Copyright 2015 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 MEDIA_FORMATS_WEBM_SEGMENTER_H_
8 #define MEDIA_FORMATS_WEBM_SEGMENTER_H_
9 
10 #include "packager/base/memory/ref_counted.h"
11 #include "packager/base/memory/scoped_ptr.h"
12 #include "packager/media/base/status.h"
13 #include "packager/media/formats/webm/encryptor.h"
14 #include "packager/media/formats/webm/mkv_writer.h"
15 #include "packager/media/formats/webm/seek_head.h"
16 #include "packager/third_party/libwebm/src/mkvmuxer.hpp"
17 
18 namespace edash_packager {
19 namespace media {
20 
21 struct MuxerOptions;
22 
23 class AudioStreamInfo;
24 class KeySource;
25 class MediaSample;
26 class StreamInfo;
27 class MuxerListener;
28 class ProgressListener;
29 class StreamInfo;
30 class VideoStreamInfo;
31 
32 namespace webm {
33 
34 class Segmenter {
35  public:
36  explicit Segmenter(const MuxerOptions& options);
37  virtual ~Segmenter();
38 
53  Status Initialize(scoped_ptr<MkvWriter> writer,
54  StreamInfo* info,
55  ProgressListener* progress_listener,
56  MuxerListener* muxer_listener,
57  KeySource* encryption_key_source,
58  uint32_t max_sd_pixels,
59  double clear_lead_in_seconds);
60 
63  Status Finalize();
64 
68  Status AddSample(scoped_refptr<MediaSample> sample);
69 
72  virtual bool GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) = 0;
73 
76  virtual bool GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) = 0;
77 
79  float GetDuration() const;
80 
81  protected:
84  uint64_t FromBMFFTimescale(uint64_t time_timescale);
86  uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
88  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
90  Status SetCluster(uint64_t start_webm_timecode,
91  uint64_t position,
92  MkvWriter* writer);
93 
95  void UpdateProgress(uint64_t progress);
96  void set_progress_target(uint64_t target) { progress_target_ = target; }
97 
98  const MuxerOptions& options() const { return options_; }
99  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
100  mkvmuxer::Cues* cues() { return &cues_; }
101  MuxerListener* muxer_listener() { return muxer_listener_; }
102  StreamInfo* info() { return info_; }
103  SeekHead* seek_head() { return &seek_head_; }
104 
105  int track_id() const { return track_id_; }
106  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
107  double cluster_length_sec() const { return cluster_length_sec_; }
108 
109  virtual Status DoInitialize(scoped_ptr<MkvWriter> writer) = 0;
110  virtual Status DoFinalize() = 0;
111 
112  private:
113  Status CreateVideoTrack(VideoStreamInfo* info);
114  Status CreateAudioTrack(AudioStreamInfo* info);
115  Status InitializeEncryptor(KeySource* key_source, uint32_t max_sd_pixels);
116 
117  // This is called when there needs to be a new subsegment. This does nothing
118  // in single-segment mode. In multi-segment mode this creates a new Cluster
119  // element.
120  virtual Status NewSubsegment(uint64_t start_timescale) = 0;
121  // This is called when there needs to be a new segment. In single-segment
122  // mode, this creates a new Cluster element. In multi-segment mode this
123  // creates a new output file.
124  virtual Status NewSegment(uint64_t start_timescale) = 0;
125  // This is called when a segment ends. This is called right before a call
126  // to NewSegment and at the start of Finalize.
127  Status FinalizeSegment(uint64_t end_timescale);
128 
129  const MuxerOptions& options_;
130  scoped_ptr<Encryptor> encryptor_;
131  double clear_lead_;
132 
133  scoped_ptr<mkvmuxer::Cluster> cluster_;
134  mkvmuxer::Cues cues_;
135  SeekHead seek_head_;
136  mkvmuxer::SegmentInfo segment_info_;
137  mkvmuxer::Tracks tracks_;
138 
139  StreamInfo* info_;
140  MuxerListener* muxer_listener_;
141  ProgressListener* progress_listener_;
142  uint64_t progress_target_;
143  uint64_t accumulated_progress_;
144  uint64_t total_duration_;
145  uint64_t sample_duration_;
146  // The position (in bytes) of the start of the Segment payload in the init
147  // file. This is also the size of the header before the SeekHead.
148  uint64_t segment_payload_pos_;
149 
150  double cluster_length_sec_;
151  double segment_length_sec_;
152 
153  int track_id_;
154 
155  DISALLOW_COPY_AND_ASSIGN(Segmenter);
156 };
157 
158 } // namespace webm
159 } // namespace media
160 } // namespace edash_packager
161 
162 #endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
Holds audio stream information.
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
Status SetCluster(uint64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition: segmenter.cc:231
Status Initialize(scoped_ptr< MkvWriter > writer, StreamInfo *info, ProgressListener *progress_listener, MuxerListener *muxer_listener, KeySource *encryption_key_source, uint32_t max_sd_pixels, double clear_lead_in_seconds)
Definition: segmenter.cc:45
Abstract class holds stream information.
Definition: stream_info.h:26
This class listens to progress updates events.
virtual bool GetIndexRangeStartAndEnd(uint32_t *start, uint32_t *end)=0
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
uint64_t FromWebMTimecode(uint64_t time_webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timescale.
Definition: segmenter.cc:189
virtual bool GetInitRangeStartAndEnd(uint32_t *start, uint32_t *end)=0
Holds video stream information.
uint64_t FromBMFFTimescale(uint64_t time_timescale)
Definition: segmenter.cc:182
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:195
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:240
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: segmenter.cc:104