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