Shaka Packager SDK
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 PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_H_
8 #define PACKAGER_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(const StreamInfo& info,
45  ProgressListener* progress_listener,
46  MuxerListener* muxer_listener);
47 
50  Status Finalize();
51 
55  Status AddSample(const 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  SeekHead* seek_head() { return &seek_head_; }
99 
100  int track_id() const { return track_id_; }
101  uint64_t segment_payload_pos() const { return segment_payload_pos_; }
102 
103  uint64_t duration() const { return duration_; }
104 
105  virtual Status DoInitialize() = 0;
106  virtual Status DoFinalize() = 0;
107 
108  private:
109  Status InitializeAudioTrack(const AudioStreamInfo& info,
110  mkvmuxer::AudioTrack* track);
111  Status InitializeVideoTrack(const VideoStreamInfo& info,
112  mkvmuxer::VideoTrack* track);
113 
114  // Writes the previous frame to the file.
115  Status WriteFrame(bool write_duration);
116 
117  // This is called when there needs to be a new (sub)segment.
118  // In single-segment mode, a Cluster is a segment and there is no subsegment.
119  // In multi-segment mode, a new file is a segment and the clusters in the file
120  // are subsegments.
121  virtual Status NewSegment(uint64_t start_timestamp, bool is_subsegment) = 0;
122 
123  // Store the previous sample so we know which one is the last frame.
124  std::shared_ptr<const MediaSample> prev_sample_;
125  // The reference frame timestamp; used to populate the ReferenceBlock element
126  // when writing non-keyframe BlockGroups.
127  uint64_t reference_frame_timestamp_ = 0;
128 
129  const MuxerOptions& options_;
130 
131  std::unique_ptr<mkvmuxer::Cluster> cluster_;
132  mkvmuxer::Cues cues_;
133  SeekHead seek_head_;
134  mkvmuxer::SegmentInfo segment_info_;
135  mkvmuxer::Tracks tracks_;
136 
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  // The subset of information that we need from StreamInfo
155  bool is_encrypted_ = false;
156  uint64_t time_scale_ = 0;
157  uint64_t duration_ = 0;
158 
159  DISALLOW_COPY_AND_ASSIGN(Segmenter);
160 };
161 
162 } // namespace webm
163 } // namespace media
164 } // namespace shaka
165 
166 #endif // PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_H_
shaka::media::webm::Segmenter::FromWebMTimecode
uint64_t FromWebMTimecode(uint64_t webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timestamp.
Definition: segmenter.cc:219
shaka::media::webm::Segmenter::GetInitRangeStartAndEnd
virtual bool GetInitRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
shaka::media::AudioStreamInfo
Holds audio stream information.
Definition: audio_stream_info.h:18
shaka::media::webm::Segmenter::FromBmffTimestamp
uint64_t FromBmffTimestamp(uint64_t bmff_timestamp)
Converts the given time in ISO BMFF timestamp to WebM timecode.
Definition: segmenter.cc:213
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::webm::Segmenter
Definition: segmenter.h:33
shaka::media::webm::Segmenter::Initialize
Status Initialize(const StreamInfo &info, ProgressListener *progress_listener, MuxerListener *muxer_listener)
Definition: segmenter.cc:78
shaka::media::webm::Segmenter::AddSample
Status AddSample(const MediaSample &sample)
Definition: segmenter.cc:161
shaka::media::webm::Segmenter::FinalizeSegment
virtual Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration_timestamp, bool is_subsegment)=0
Finalize the (sub)segment.
Definition: segmenter.cc:197
shaka::Status
Definition: status.h:110
shaka::media::MuxerOptions
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20
shaka::media::MuxerListener
Definition: muxer_listener.h:32
shaka::media::webm::Segmenter::GetIndexRangeStartAndEnd
virtual bool GetIndexRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
shaka::media::SeekHead
Definition: seek_head.h:20
shaka::media::ProgressListener
This class listens to progress updates events.
Definition: progress_listener.h:20
shaka::media::webm::Segmenter::SetCluster
Status SetCluster(uint64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition: segmenter.cc:263
shaka::media::webm::Segmenter::GetDurationInSeconds
float GetDurationInSeconds() const
Definition: segmenter.cc:207
shaka::media::webm::Segmenter::UpdateProgress
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:272
shaka::media::VideoStreamInfo
Holds video stream information.
Definition: video_stream_info.h:23
shaka::media::MkvWriter
An implementation of IMkvWriter using our File type.
Definition: mkv_writer.h:21
shaka::media::MediaSample
Class to hold a media sample.
Definition: media_sample.h:22
shaka::media::webm::Segmenter::Finalize
Status Finalize()
Definition: segmenter.cc:152
shaka::media::StreamInfo
Abstract class holds stream information.
Definition: stream_info.h:65
shaka::media::webm::Segmenter::WriteSegmentHeader
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition: segmenter.cc:225