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/mkv_writer.h"
13 #include "packager/media/formats/webm/seek_head.h"
14 #include "packager/third_party/libwebm/src/mkvmuxer.hpp"
15 
16 namespace shaka {
17 namespace media {
18 
19 struct MuxerOptions;
20 
21 class AudioStreamInfo;
22 class MediaSample;
23 class MuxerListener;
24 class ProgressListener;
25 class StreamInfo;
26 class VideoStreamInfo;
27 
28 namespace webm {
29 
30 class Segmenter {
31  public:
32  explicit Segmenter(const MuxerOptions& options);
33  virtual ~Segmenter();
34 
42  ProgressListener* progress_listener,
43  MuxerListener* muxer_listener);
44 
47  Status Finalize();
48 
52  Status AddSample(std::shared_ptr<MediaSample> sample);
53 
55  virtual Status FinalizeSegment(uint64_t start_timescale,
56  uint64_t duration_timescale,
57  bool is_subsegment) = 0;
58 
61  virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
62 
65  virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
66 
68  float GetDuration() const;
69 
70  protected:
73  uint64_t FromBMFFTimescale(uint64_t time_timescale);
75  uint64_t FromWebMTimecode(uint64_t time_webm_timecode);
77  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
79  Status SetCluster(uint64_t start_webm_timecode,
80  uint64_t position,
81  MkvWriter* writer);
82 
84  void UpdateProgress(uint64_t progress);
85  void set_progress_target(uint64_t target) { progress_target_ = target; }
86 
87  const MuxerOptions& options() const { return options_; }
88  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
89  mkvmuxer::Cues* cues() { return &cues_; }
90  MuxerListener* muxer_listener() { return muxer_listener_; }
91  StreamInfo* info() { return info_; }
92  SeekHead* seek_head() { return &seek_head_; }
93 
94  int track_id() const { return track_id_; }
95  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
96 
97  virtual Status DoInitialize() = 0;
98  virtual Status DoFinalize() = 0;
99 
100  private:
101  Status InitializeAudioTrack(const AudioStreamInfo* info,
102  mkvmuxer::AudioTrack* track);
103  Status InitializeVideoTrack(const VideoStreamInfo* info,
104  mkvmuxer::VideoTrack* track);
105 
106  // Writes the previous frame to the file.
107  Status WriteFrame(bool write_duration);
108 
109  // This is called when there needs to be a new (sub)segment.
110  // In single-segment mode, a Cluster is a segment and there is no subsegment.
111  // In multi-segment mode, a new file is a segment and the clusters in the file
112  // are subsegments.
113  virtual Status NewSegment(uint64_t start_timescale, bool is_subsegment) = 0;
114 
115  // Store the previous sample so we know which one is the last frame.
116  std::shared_ptr<MediaSample> prev_sample_;
117  // The reference frame timestamp; used to populate the ReferenceBlock element
118  // when writing non-keyframe BlockGroups.
119  uint64_t reference_frame_timestamp_ = 0;
120 
121  const MuxerOptions& options_;
122 
123  std::unique_ptr<mkvmuxer::Cluster> cluster_;
124  mkvmuxer::Cues cues_;
125  SeekHead seek_head_;
126  mkvmuxer::SegmentInfo segment_info_;
127  mkvmuxer::Tracks tracks_;
128 
129  StreamInfo* info_ = nullptr;
130  MuxerListener* muxer_listener_ = nullptr;
131  ProgressListener* progress_listener_ = nullptr;
132  uint64_t progress_target_ = 0;
133  uint64_t accumulated_progress_ = 0;
134  uint64_t first_timestamp_ = 0;
135  int64_t sample_duration_ = 0;
136  // The position (in bytes) of the start of the Segment payload in the init
137  // file. This is also the size of the header before the SeekHead.
138  uint64_t segment_payload_pos_ = 0;
139 
140  // Indicate whether a new segment needed to be created, which is always true
141  // in the beginning.
142  bool new_segment_ = true;
143  // Indicate whether a new subsegment needed to be created.
144  bool new_subsegment_ = false;
145  int track_id_ = 0;
146 
147  DISALLOW_COPY_AND_ASSIGN(Segmenter);
148 };
149 
150 } // namespace webm
151 } // namespace media
152 } // namespace shaka
153 
154 #endif // MEDIA_FORMATS_WEBM_SEGMENTER_H_
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:181
Abstract class holds stream information.
Definition: stream_info.h:57
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:219
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:119
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:41
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:228
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:175
Holds video stream information.
Holds audio stream information.
uint64_t FromBMFFTimescale(uint64_t time_timescale)
Definition: segmenter.cc:168
virtual Status FinalizeSegment(uint64_t start_timescale, uint64_t duration_timescale, bool is_subsegment)=0
Finalize the (sub)segment.
Definition: segmenter.cc:153