Shaka Packager SDK
Loading...
Searching...
No Matches
segmenter.h
1// Copyright 2015 Google LLC. 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 <cstddef>
11#include <cstdint>
12#include <memory>
13#include <vector>
14
15#include <mkvmuxer/mkvmuxer.h>
16
17#include <packager/macros/classes.h>
18#include <packager/media/base/range.h>
19#include <packager/media/formats/webm/mkv_writer.h>
20#include <packager/media/formats/webm/seek_head.h>
21#include <packager/status.h>
22
23namespace shaka {
24namespace media {
25
26struct MuxerOptions;
27
28class AudioStreamInfo;
29class MediaSample;
30class MuxerListener;
31class ProgressListener;
32class StreamInfo;
33class VideoStreamInfo;
34
35namespace webm {
36
37class Segmenter {
38 public:
39 explicit Segmenter(const MuxerOptions& options);
40 virtual ~Segmenter();
41
48 Status Initialize(const StreamInfo& info,
49 ProgressListener* progress_listener,
50 MuxerListener* muxer_listener);
51
54 Status Finalize();
55
59 Status AddSample(const MediaSample& sample);
60
62 virtual Status FinalizeSegment(int64_t start_timestamp,
63 int64_t duration_timestamp,
64 bool is_subsegment,
65 int64_t segment_number) = 0;
66
69 virtual bool GetInitRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
70
73 virtual bool GetIndexRangeStartAndEnd(uint64_t* start, uint64_t* end) = 0;
74
75 // Returns an empty vector if there are no specific ranges for the segments,
76 // e.g. the media is in multiple files.
77 // Otherwise, a vector of ranges for the media segments are returned.
78 virtual std::vector<Range> GetSegmentRanges() = 0;
79
81 float GetDurationInSeconds() const;
82
83 protected:
85 int64_t FromBmffTimestamp(int64_t bmff_timestamp);
87 int64_t FromWebMTimecode(int64_t webm_timecode);
89 Status WriteSegmentHeader(uint64_t file_size, MkvWriter* writer);
91 Status SetCluster(int64_t start_webm_timecode,
92 uint64_t position,
93 MkvWriter* writer);
94
96 void UpdateProgress(uint64_t progress);
97 void set_progress_target(uint64_t target) { progress_target_ = target; }
98
99 const MuxerOptions& options() const { return options_; }
100 mkvmuxer::Cluster* cluster() { return cluster_.get(); }
101 mkvmuxer::Cues* cues() { return &cues_; }
102 MuxerListener* muxer_listener() { return muxer_listener_; }
103 SeekHead* seek_head() { return &seek_head_; }
104
105 int track_id() const { return track_id_; }
106 uint64_t segment_payload_pos() const { return segment_payload_pos_; }
107
108 int64_t duration() const { return duration_; }
109
110 virtual Status DoInitialize() = 0;
111 virtual Status DoFinalize() = 0;
112
113 private:
114 Status InitializeAudioTrack(const AudioStreamInfo& info,
115 mkvmuxer::AudioTrack* track);
116 Status InitializeVideoTrack(const VideoStreamInfo& info,
117 mkvmuxer::VideoTrack* track);
118
119 // Writes the previous frame to the file.
120 Status WriteFrame(bool write_duration);
121
122 // This is called when there needs to be a new (sub)segment.
123 // In single-segment mode, a Cluster is a segment and there is no subsegment.
124 // In multi-segment mode, a new file is a segment and the clusters in the file
125 // are subsegments.
126 virtual Status NewSegment(int64_t start_timestamp, bool is_subsegment) = 0;
127
128 // Store the previous sample so we know which one is the last frame.
129 std::shared_ptr<const MediaSample> prev_sample_;
130 // The reference frame timestamp; used to populate the ReferenceBlock element
131 // when writing non-keyframe BlockGroups.
132 int64_t reference_frame_timestamp_ = 0;
133
134 const MuxerOptions& options_;
135
136 std::unique_ptr<mkvmuxer::Cluster> cluster_;
137 mkvmuxer::Cues cues_;
138 SeekHead seek_head_;
139 mkvmuxer::SegmentInfo segment_info_;
140 mkvmuxer::Tracks tracks_;
141
142 MuxerListener* muxer_listener_ = nullptr;
143 ProgressListener* progress_listener_ = nullptr;
144 uint64_t progress_target_ = 0;
145 uint64_t accumulated_progress_ = 0;
146
147 int64_t first_timestamp_ = 0;
148 int64_t sample_durations_[2] = {0, 0};
149 size_t num_samples_ = 0;
150
151 // The position (in bytes) of the start of the Segment payload in the init
152 // file. This is also the size of the header before the SeekHead.
153 uint64_t segment_payload_pos_ = 0;
154
155 // Indicate whether a new segment needed to be created, which is always true
156 // in the beginning.
157 bool new_segment_ = true;
158 // Indicate whether a new subsegment needed to be created.
159 bool new_subsegment_ = false;
160 int track_id_ = 0;
161
162 // The subset of information that we need from StreamInfo
163 bool is_encrypted_ = false;
164 int64_t time_scale_ = 0;
165 int64_t duration_ = 0;
166
167 DISALLOW_COPY_AND_ASSIGN(Segmenter);
168};
169
170} // namespace webm
171} // namespace media
172} // namespace shaka
173
174#endif // PACKAGER_MEDIA_FORMATS_WEBM_SEGMENTER_H_
Class to hold a media sample.
An implementation of IMkvWriter using our File type.
Definition mkv_writer.h:26
This class listens to progress updates events.
Abstract class holds stream information.
Definition stream_info.h:73
Status SetCluster(int64_t start_webm_timecode, uint64_t position, MkvWriter *writer)
Creates a Cluster object with the given parameters.
Definition segmenter.cc:278
Status Initialize(const StreamInfo &info, ProgressListener *progress_listener, MuxerListener *muxer_listener)
Definition segmenter.cc:89
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition segmenter.cc:287
virtual bool GetIndexRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
virtual Status FinalizeSegment(int64_t start_timestamp, int64_t duration_timestamp, bool is_subsegment, int64_t segment_number)=0
Finalize the (sub)segment.
Definition segmenter.cc:212
virtual bool GetInitRangeStartAndEnd(uint64_t *start, uint64_t *end)=0
float GetDurationInSeconds() const
Definition segmenter.cc:223
int64_t FromWebMTimecode(int64_t webm_timecode)
Converts the given time in WebM timecode to ISO BMFF timestamp.
Definition segmenter.cc:234
Status AddSample(const MediaSample &sample)
Definition segmenter.cc:172
int64_t FromBmffTimestamp(int64_t bmff_timestamp)
Converts the given time in ISO BMFF timestamp to WebM timecode.
Definition segmenter.cc:229
Status WriteSegmentHeader(uint64_t file_size, MkvWriter *writer)
Writes the Segment header to writer.
Definition segmenter.cc:240
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.