DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations 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 <memory>
11 #include "packager/base/memory/ref_counted.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 shaka {
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 
61  Status Initialize(std::unique_ptr<MkvWriter> writer,
62  StreamInfo* info,
63  ProgressListener* progress_listener,
64  MuxerListener* muxer_listener,
65  KeySource* encryption_key_source,
66  uint32_t max_sd_pixels,
67  uint32_t max_hd_pixels,
68  uint32_t max_uhd1_pixels,
69  double clear_lead_in_seconds);
70 
73  Status Finalize();
74 
78  Status AddSample(scoped_refptr<MediaSample> sample);
79 
82  virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
83 
86  virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
87 
89  float GetDuration() const;
90 
91  protected:
94  uint64_t FromBMFFTimescale(uint64_t time_timescale);
96  uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
98  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
100  Status SetCluster(uint64_t start_webm_timecode,
101  uint64_t position,
102  MkvWriter* writer);
103 
105  void UpdateProgress(uint64_t progress);
106  void set_progress_target(uint64_t target) { progress_target_ = target; }
107 
108  const MuxerOptions& options() const { return options_; }
109  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
110  mkvmuxer::Cues* cues() { return &cues_; }
111  MuxerListener* muxer_listener() { return muxer_listener_; }
112  StreamInfo* info() { return info_; }
113  SeekHead* seek_head() { return &seek_head_; }
114 
115  int track_id() const { return track_id_; }
116  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
117  uint64_t cluster_length_in_time_scale() const {
118  return cluster_length_in_time_scale_;
119  }
120 
121  virtual Status DoInitialize(std::unique_ptr<MkvWriter> writer) = 0;
122  virtual Status DoFinalize() = 0;
123 
124  private:
125  Status CreateVideoTrack(VideoStreamInfo* info);
126  Status CreateAudioTrack(AudioStreamInfo* info);
127  Status InitializeEncryptor(KeySource* key_source, uint32_t max_sd_pixels,
128  uint32_t max_hd_pixels, uint32_t max_uhd1_pixels);
129 
130  // Writes the previous frame to the file.
131  Status WriteFrame(bool write_duration);
132 
133  // This is called when there needs to be a new subsegment. This does nothing
134  // in single-segment mode. In multi-segment mode this creates a new Cluster
135  // element.
136  virtual Status NewSubsegment(uint64_t start_timescale) = 0;
137  // This is called when there needs to be a new segment. In single-segment
138  // mode, this creates a new Cluster element. In multi-segment mode this
139  // creates a new output file.
140  virtual Status NewSegment(uint64_t start_timescale) = 0;
141 
142  // Store the previous sample so we know which one is the last frame.
143  scoped_refptr<MediaSample> prev_sample_;
144  // The reference frame timestamp; used to populate the ReferenceBlock element
145  // when writing non-keyframe BlockGroups.
146  uint64_t reference_frame_timestamp_;
147 
148  const MuxerOptions& options_;
149  std::unique_ptr<Encryptor> encryptor_;
150  double clear_lead_;
151  bool enable_encryption_; // Encryption is enabled only after clear_lead_.
152 
153  std::unique_ptr<mkvmuxer::Cluster> cluster_;
154  mkvmuxer::Cues cues_;
155  SeekHead seek_head_;
156  mkvmuxer::SegmentInfo segment_info_;
157  mkvmuxer::Tracks tracks_;
158 
159  StreamInfo* info_;
160  MuxerListener* muxer_listener_;
161  ProgressListener* progress_listener_;
162  uint64_t progress_target_;
163  uint64_t accumulated_progress_;
164  uint64_t first_timestamp_;
165  int64_t sample_duration_;
166  // The position (in bytes) of the start of the Segment payload in the init
167  // file. This is also the size of the header before the SeekHead.
168  uint64_t segment_payload_pos_;
169 
170  // Durations in timescale.
171  uint64_t cluster_length_in_time_scale_;
172  uint64_t segment_length_in_time_scale_;
173 
174  int track_id_;
175 
176  DISALLOW_COPY_AND_ASSIGN(Segmenter);
177 };
178 
179 } // namespace webm
180 } // namespace media
181 } // namespace shaka
182 
183 #endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:221
Abstract class holds stream information.
Definition: stream_info.h:53
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: segmenter.cc:124
Status Initialize(std::unique_ptr< MkvWriter > writer, StreamInfo *info, ProgressListener *progress_listener, MuxerListener *muxer_listener, 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: segmenter.cc:51
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
Status SetCluster(uint64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition: segmenter.cc:259
This class listens to progress updates events.
virtual bool GetIndexRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:268
virtual bool GetInitRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
uint64_t FromWebMTimecode(uint64_t time_webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timescale.
Definition: segmenter.cc:215
Holds video stream information.
Holds audio stream information.
uint64_t FromBMFFTimescale(uint64_t time_timescale)
Definition: segmenter.cc:208