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