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/formats/webm/mkv_writer.h"
12 #include "packager/media/formats/webm/seek_head.h"
13 #include "packager/status.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 
41  Status Initialize(StreamInfo* info,
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_timestamp,
56  uint64_t duration_timestamp,
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 GetDurationInSeconds() const;
69 
70  protected:
72  uint64_t FromBmffTimestamp(uint64_t bmff_timestamp);
74  uint64_t FromWebMTimecode(uint64_t webm_timecode);
76  Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
78  Status SetCluster(uint64_t start_webm_timecode,
79  uint64_t position,
80  MkvWriter* writer);
81 
83  void UpdateProgress(uint64_t progress);
84  void set_progress_target(uint64_t target) { progress_target_ = target; }
85 
86  const MuxerOptions& options() const { return options_; }
87  mkvmuxer::Cluster* cluster() { return cluster_.get(); }
88  mkvmuxer::Cues* cues() { return &cues_; }
89  MuxerListener* muxer_listener() { return muxer_listener_; }
90  StreamInfo* info() { return info_; }
91  SeekHead* seek_head() { return &seek_head_; }
92 
93  int track_id() const { return track_id_; }
94  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
95 
96  virtual Status DoInitialize() = 0;
97  virtual Status DoFinalize() = 0;
98 
99  private:
100  Status InitializeAudioTrack(const AudioStreamInfo* info,
101  mkvmuxer::AudioTrack* track);
102  Status InitializeVideoTrack(const VideoStreamInfo* info,
103  mkvmuxer::VideoTrack* track);
104 
105  // Writes the previous frame to the file.
106  Status WriteFrame(bool write_duration);
107 
108  // This is called when there needs to be a new (sub)segment.
109  // In single-segment mode, a Cluster is a segment and there is no subsegment.
110  // In multi-segment mode, a new file is a segment and the clusters in the file
111  // are subsegments.
112  virtual Status NewSegment(uint64_t start_timestamp, bool is_subsegment) = 0;
113 
114  // Store the previous sample so we know which one is the last frame.
115  std::shared_ptr<MediaSample> prev_sample_;
116  // The reference frame timestamp; used to populate the ReferenceBlock element
117  // when writing non-keyframe BlockGroups.
118  uint64_t reference_frame_timestamp_ = 0;
119 
120  const MuxerOptions& options_;
121 
122  std::unique_ptr<mkvmuxer::Cluster> cluster_;
123  mkvmuxer::Cues cues_;
124  SeekHead seek_head_;
125  mkvmuxer::SegmentInfo segment_info_;
126  mkvmuxer::Tracks tracks_;
127 
128  StreamInfo* info_ = nullptr;
129  MuxerListener* muxer_listener_ = nullptr;
130  ProgressListener* progress_listener_ = nullptr;
131  uint64_t progress_target_ = 0;
132  uint64_t accumulated_progress_ = 0;
133  uint64_t first_timestamp_ = 0;
134  int64_t sample_duration_ = 0;
135  // The position (in bytes) of the start of the Segment payload in the init
136  // file. This is also the size of the header before the SeekHead.
137  uint64_t segment_payload_pos_ = 0;
138 
139  // Indicate whether a new segment needed to be created, which is always true
140  // in the beginning.
141  bool new_segment_ = true;
142  // Indicate whether a new subsegment needed to be created.
143  bool new_subsegment_ = false;
144  int track_id_ = 0;
145 
146  DISALLOW_COPY_AND_ASSIGN(Segmenter);
147 };
148 
149 } // namespace webm
150 } // namespace media
151 } // namespace shaka
152 
153 #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.