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 
12 #include "packager/base/optional.h"
13 #include "packager/media/base/range.h"
14 #include "packager/media/formats/webm/mkv_writer.h"
15 #include "packager/media/formats/webm/seek_head.h"
16 #include "packager/status.h"
17 #include "packager/third_party/libwebm/src/mkvmuxer.hpp"
18 
19 namespace shaka {
20 namespace media {
21 
22 struct MuxerOptions;
23 
24 class AudioStreamInfo;
25 class MediaSample;
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 
44  Status Initialize(StreamInfo* info,
45  ProgressListener* progress_listener,
46  MuxerListener* muxer_listener);
47 
50  Status Finalize();
51 
55  Status AddSample(std::shared_ptr<MediaSample> sample);
56 
58  virtual Status FinalizeSegment(uint64_t start_timestamp,
59  uint64_t duration_timestamp,
60  bool is_subsegment) = 0;
61 
64  virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
65 
68  virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
69 
70  // Returns an empty vector if there are no specific ranges for the segments,
71  // e.g. the media is in multiple files.
72  // Otherwise, a vector of ranges for the media segments are returned.
73  virtual std::vector<Range> GetSegmentRanges() = 0;
74 
76  float GetDurationInSeconds() const;
77 
78  protected:
80  uint64_t FromBmffTimestamp(uint64_t bmff_timestamp);
82  uint64_t FromWebMTimecode(uint64_t webm_timecode);
84  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
86  Status SetCluster(uint64_t start_webm_timecode,
87  uint64_t position,
88  MkvWriter* writer);
89 
91  void UpdateProgress(uint64_t progress);
92  void set_progress_target(uint64_t target) { progress_target_ = target; }
93 
94  const MuxerOptions& options() const { return options_; }
95  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
96  mkvmuxer::Cues* cues() { return &cues_; }
97  MuxerListener* muxer_listener() { return muxer_listener_; }
98  StreamInfo* info() { return info_; }
99  SeekHead* seek_head() { return &seek_head_; }
100 
101  int track_id() const { return track_id_; }
102  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
103 
104  virtual Status DoInitialize() = 0;
105  virtual Status DoFinalize() = 0;
106 
107  private:
108  Status InitializeAudioTrack(const AudioStreamInfo* info,
109  mkvmuxer::AudioTrack* track);
110  Status InitializeVideoTrack(const VideoStreamInfo* info,
111  mkvmuxer::VideoTrack* track);
112 
113  // Writes the previous frame to the file.
114  Status WriteFrame(bool write_duration);
115 
116  // This is called when there needs to be a new (sub)segment.
117  // In single-segment mode, a Cluster is a segment and there is no subsegment.
118  // In multi-segment mode, a new file is a segment and the clusters in the file
119  // are subsegments.
120  virtual Status NewSegment(uint64_t start_timestamp, bool is_subsegment) = 0;
121 
122  // Store the previous sample so we know which one is the last frame.
123  std::shared_ptr<MediaSample> prev_sample_;
124  // The reference frame timestamp; used to populate the ReferenceBlock element
125  // when writing non-keyframe BlockGroups.
126  uint64_t reference_frame_timestamp_ = 0;
127 
128  const MuxerOptions& options_;
129 
130  std::unique_ptr<mkvmuxer::Cluster> cluster_;
131  mkvmuxer::Cues cues_;
132  SeekHead seek_head_;
133  mkvmuxer::SegmentInfo segment_info_;
134  mkvmuxer::Tracks tracks_;
135 
136  StreamInfo* info_ = nullptr;
137  MuxerListener* muxer_listener_ = nullptr;
138  ProgressListener* progress_listener_ = nullptr;
139  uint64_t progress_target_ = 0;
140  uint64_t accumulated_progress_ = 0;
141  uint64_t first_timestamp_ = 0;
142  int64_t sample_duration_ = 0;
143  // The position (in bytes) of the start of the Segment payload in the init
144  // file. This is also the size of the header before the SeekHead.
145  uint64_t segment_payload_pos_ = 0;
146 
147  // Indicate whether a new segment needed to be created, which is always true
148  // in the beginning.
149  bool new_segment_ = true;
150  // Indicate whether a new subsegment needed to be created.
151  bool new_subsegment_ = false;
152  int track_id_ = 0;
153 
154  DISALLOW_COPY_AND_ASSIGN(Segmenter);
155 };
156 
157 } // namespace webm
158 } // namespace media
159 } // namespace shaka
160 
161 #endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:218
Abstract class holds stream information.
Definition: stream_info.h:57
uint64_t FromWebMTimecode(uint64_t webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timestamp.
Definition: segmenter.cc:212
uint64_t FromBmffTimestamp(uint64_t bmff_timestamp)
Converts the given time in ISO BMFF timestamp to WebM timecode.
Definition: segmenter.cc:206
float GetDurationInSeconds() const
Definition: segmenter.cc:200
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
virtual Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration_timestamp, bool is_subsegment)=0
Finalize the (sub)segment.
Definition: segmenter.cc:190
Status SetCluster(uint64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition: segmenter.cc:256
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:156
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
Status Initialize(StreamInfo *info, ProgressListener *progress_listener, MuxerListener *muxer_listener)
Definition: segmenter.cc:78
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:265
virtual bool GetInitRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
Holds video stream information.
Holds audio stream information.