Shaka Packager SDK
Loading...
Searching...
No Matches
segmenter.h
1// Copyright 2014 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_MP4_SEGMENTER_H_
8#define PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
9
10#include <cstddef>
11#include <cstdint>
12#include <memory>
13#include <vector>
14
15#include <packager/macros/classes.h>
16#include <packager/media/base/encryption_config.h>
17#include <packager/media/base/range.h>
18#include <packager/media/formats/mp4/box_definitions.h>
19#include <packager/status.h>
20
21namespace shaka {
22namespace media {
23
24struct MuxerOptions;
25struct SegmentInfo;
26
27class BufferWriter;
28class MediaSample;
29class MuxerListener;
30class ProgressListener;
31class StreamInfo;
32
33namespace mp4 {
34
35class Fragmenter;
36struct KeyFrameInfo;
37
44class Segmenter {
45 public:
46 Segmenter(const MuxerOptions& options,
47 std::unique_ptr<FileType> ftyp,
48 std::unique_ptr<Movie> moov);
49 virtual ~Segmenter();
50
58 Status Initialize(
59 const std::vector<std::shared_ptr<const StreamInfo>>& streams,
60 MuxerListener* muxer_listener,
61 ProgressListener* progress_listener);
62
65 Status Finalize();
66
71 Status AddSample(size_t stream_id, const MediaSample& sample);
72
77 Status FinalizeSegment(size_t stream_id, const SegmentInfo& segment_info);
78
79 // TODO(rkuroiwa): Change these Get*Range() methods to return
80 // std::optional<Range> as well.
83 virtual bool GetInitRange(size_t* offset, size_t* size) = 0;
84
87 virtual bool GetIndexRange(size_t* offset, size_t* size) = 0;
88
89 // Returns an empty vector if there are no specific ranges for the segments,
90 // e.g. the media is in multiple files.
91 // Otherwise, a vector of ranges for the media segments are returned.
92 virtual std::vector<Range> GetSegmentRanges() = 0;
93
94 int32_t GetReferenceTimeScale() const;
95
97 double GetDuration() const;
98
99 void SetAes128EncryptionConfig(const EncryptionConfig& config) {
100 aes128_encryption_config_ = config;
101 }
102
105 int64_t sample_duration() const {
106 return sample_durations_[num_samples_ < 2 ? 0 : 1];
107 }
108
109 protected:
111 void UpdateProgress(uint64_t progress);
113 void SetComplete();
114
115 const MuxerOptions& options() const { return options_; }
116 FileType* ftyp() { return ftyp_.get(); }
117 Movie* moov() { return moov_.get(); }
118 BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
119 SegmentIndex* sidx() { return sidx_.get(); }
120 MuxerListener* muxer_listener() { return muxer_listener_; }
121 uint64_t progress_target() { return progress_target_; }
122 const std::vector<KeyFrameInfo>& key_frame_infos() const {
123 return key_frame_infos_;
124 }
125
126 void set_progress_target(uint64_t progress_target) {
127 progress_target_ = progress_target;
128 }
129
130 const EncryptionConfig& aes128_encryption_config() const {
131 return aes128_encryption_config_;
132 }
133
134 private:
135 virtual Status DoInitialize() = 0;
136 virtual Status DoFinalize() = 0;
137 virtual Status DoFinalizeSegment(int64_t segment_number) = 0;
138 virtual Status DoFinalizeChunk(int64_t segment_number) { return Status::OK; }
139
140 uint32_t GetReferenceStreamId();
141
142 void FinalizeFragmentForKeyRotation(
143 size_t stream_id,
144 bool fragment_encrypted,
145 const EncryptionConfig& encryption_config);
146
147 const MuxerOptions& options_;
148 std::unique_ptr<FileType> ftyp_;
149 std::unique_ptr<Movie> moov_;
150 std::unique_ptr<MovieFragment> moof_;
151 std::unique_ptr<BufferWriter> fragment_buffer_;
152 std::unique_ptr<SegmentIndex> sidx_;
153 std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
154 MuxerListener* muxer_listener_ = nullptr;
155 ProgressListener* progress_listener_ = nullptr;
156 uint64_t progress_target_ = 0u;
157 uint64_t accumulated_progress_ = 0u;
158 int64_t sample_durations_[2] = {0, 0};
159 size_t num_samples_ = 0;
160 std::vector<uint64_t> stream_durations_;
161 std::vector<KeyFrameInfo> key_frame_infos_;
162 // Only set for AES-128; holds key/IV for whole-segment encryption.
163 EncryptionConfig aes128_encryption_config_;
164
165 DISALLOW_COPY_AND_ASSIGN(Segmenter);
166};
167
168} // namespace mp4
169} // namespace media
170} // namespace shaka
171
172#endif // PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
Class to hold a media sample.
This class listens to progress updates events.
virtual bool GetInitRange(size_t *offset, size_t *size)=0
Status AddSample(size_t stream_id, const MediaSample &sample)
Definition segmenter.cc:135
int64_t sample_duration() const
Definition segmenter.h:105
virtual bool GetIndexRange(size_t *offset, size_t *size)=0
Status Initialize(const std::vector< std::shared_ptr< const StreamInfo > > &streams, MuxerListener *muxer_listener, ProgressListener *progress_listener)
Definition segmenter.cc:62
void SetComplete()
Set progress to 100%.
Definition segmenter.cc:294
Status FinalizeSegment(size_t stream_id, const SegmentInfo &segment_info)
Definition segmenter.cc:163
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition segmenter.cc:276
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.