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/media/base/status.h"
12 #include "packager/media/formats/webm/encryptor.h"
13 #include "packager/media/formats/webm/mkv_writer.h"
14 #include "packager/media/formats/webm/seek_head.h"
15 #include "packager/third_party/libwebm/src/mkvmuxer.hpp"
16 
17 namespace shaka {
18 namespace media {
19 
20 struct MuxerOptions;
21 
22 class AudioStreamInfo;
23 class KeySource;
24 class MediaSample;
25 class StreamInfo;
26 class MuxerListener;
27 class ProgressListener;
28 class StreamInfo;
29 class VideoStreamInfo;
30 
31 namespace webm {
32 
33 class Segmenter {
34  public:
35  explicit Segmenter(const MuxerOptions& options);
36  virtual ~Segmenter();
37 
60  Status Initialize(std::unique_ptr<MkvWriter> writer,
61  StreamInfo* info,
62  ProgressListener* progress_listener,
63  MuxerListener* muxer_listener,
64  KeySource* encryption_key_source,
65  uint32_t max_sd_pixels,
66  uint32_t max_hd_pixels,
67  uint32_t max_uhd1_pixels,
68  double clear_lead_in_seconds);
69 
72  Status Finalize();
73 
77  Status AddSample(std::shared_ptr<MediaSample> sample);
78 
80  virtual Status FinalizeSegment(uint64_t start_timescale,
81  uint64_t duration_timescale,
82  bool is_subsegment) = 0;
83 
86  virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
87 
90  virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
91 
93  float GetDuration() const;
94 
95  protected:
98  uint64_t FromBMFFTimescale(uint64_t time_timescale);
100  uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
102  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
104  Status SetCluster(uint64_t start_webm_timecode,
105  uint64_t position,
106  MkvWriter* writer);
107 
109  void UpdateProgress(uint64_t progress);
110  void set_progress_target(uint64_t target) { progress_target_ = target; }
111 
112  const MuxerOptions& options() const { return options_; }
113  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
114  mkvmuxer::Cues* cues() { return &cues_; }
115  MuxerListener* muxer_listener() { return muxer_listener_; }
116  StreamInfo* info() { return info_; }
117  SeekHead* seek_head() { return &seek_head_; }
118 
119  int track_id() const { return track_id_; }
120  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
121 
122  virtual Status DoInitialize(std::unique_ptr<MkvWriter> writer) = 0;
123  virtual Status DoFinalize() = 0;
124 
125  private:
126  Status CreateVideoTrack(VideoStreamInfo* info);
127  Status CreateAudioTrack(AudioStreamInfo* info);
128  Status InitializeEncryptor(KeySource* key_source, uint32_t max_sd_pixels,
129  uint32_t max_hd_pixels, uint32_t max_uhd1_pixels);
130 
131  // Writes the previous frame to the file.
132  Status WriteFrame(bool write_duration);
133 
134  // This is called when there needs to be a new (sub)segment.
135  // In single-segment mode, a Cluster is a segment and there is no subsegment.
136  // In multi-segment mode, a new file is a segment and the clusters in the file
137  // are subsegments.
138  virtual Status NewSegment(uint64_t start_timescale, bool is_subsegment) = 0;
139 
140  // Store the previous sample so we know which one is the last frame.
141  std::shared_ptr<MediaSample> prev_sample_;
142  // The reference frame timestamp; used to populate the ReferenceBlock element
143  // when writing non-keyframe BlockGroups.
144  uint64_t reference_frame_timestamp_ = 0;
145 
146  const MuxerOptions& options_;
147  std::unique_ptr<Encryptor> encryptor_;
148  double clear_lead_ = 0;
149  // Encryption is enabled only after clear_lead_.
150  bool enable_encryption_ = false;
151 
152  std::unique_ptr<mkvmuxer::Cluster> cluster_;
153  mkvmuxer::Cues cues_;
154  SeekHead seek_head_;
155  mkvmuxer::SegmentInfo segment_info_;
156  mkvmuxer::Tracks tracks_;
157 
158  StreamInfo* info_ = nullptr;
159  MuxerListener* muxer_listener_ = nullptr;
160  ProgressListener* progress_listener_ = nullptr;
161  uint64_t progress_target_ = 0;
162  uint64_t accumulated_progress_ = 0;
163  uint64_t first_timestamp_ = 0;
164  int64_t sample_duration_ = 0;
165  // The position (in bytes) of the start of the Segment payload in the init
166  // file. This is also the size of the header before the SeekHead.
167  uint64_t segment_payload_pos_ = 0;
168 
169  // Indicate whether a new segment needed to be created, which is always true
170  // in the beginning.
171  bool new_segment_ = true;
172  // Indicate whether a new subsegment needed to be created.
173  bool new_subsegment_ = false;
174  int track_id_ = 0;
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:184
Abstract class holds stream information.
Definition: stream_info.h:58
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:36
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:222
This class listens to progress updates events.
virtual bool GetIndexRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
Status AddSample(std::shared_ptr< MediaSample > sample)
Definition: segmenter.cc:105
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:231
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:178
Holds video stream information.
Holds audio stream information.
uint64_t FromBMFFTimescale(uint64_t time_timescale)
Definition: segmenter.cc:171
virtual Status FinalizeSegment(uint64_t start_timescale, uint64_t duration_timescale, bool is_subsegment)=0
Finalize the (sub)segment.
Definition: segmenter.cc:156