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/fourccs.h>
18#include <packager/media/base/range.h>
19#include <packager/media/formats/mp4/box_definitions.h>
20#include <packager/status.h>
21
22namespace shaka {
23namespace media {
24
25struct EncryptionConfig;
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
103 int64_t sample_duration() const {
104 return sample_durations_[num_samples_ < 2 ? 0 : 1];
105 }
106
107 protected:
109 void UpdateProgress(uint64_t progress);
111 void SetComplete();
112
113 const MuxerOptions& options() const { return options_; }
114 FileType* ftyp() { return ftyp_.get(); }
115 Movie* moov() { return moov_.get(); }
116 BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
117 SegmentIndex* sidx() { return sidx_.get(); }
118 MuxerListener* muxer_listener() { return muxer_listener_; }
119 uint64_t progress_target() { return progress_target_; }
120 const std::vector<KeyFrameInfo>& key_frame_infos() const {
121 return key_frame_infos_;
122 }
123
124 void set_progress_target(uint64_t progress_target) {
125 progress_target_ = progress_target;
126 }
127
128 private:
129 virtual Status DoInitialize() = 0;
130 virtual Status DoFinalize() = 0;
131 virtual Status DoFinalizeSegment(int64_t segment_number) = 0;
132 virtual Status DoFinalizeChunk(int64_t segment_number) { return Status::OK; }
133
134 uint32_t GetReferenceStreamId();
135
136 void FinalizeFragmentForKeyRotation(
137 size_t stream_id,
138 bool fragment_encrypted,
139 const EncryptionConfig& encryption_config);
140
141 const MuxerOptions& options_;
142 std::unique_ptr<FileType> ftyp_;
143 std::unique_ptr<Movie> moov_;
144 std::unique_ptr<MovieFragment> moof_;
145 std::unique_ptr<BufferWriter> fragment_buffer_;
146 std::unique_ptr<SegmentIndex> sidx_;
147 std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
148 MuxerListener* muxer_listener_ = nullptr;
149 ProgressListener* progress_listener_ = nullptr;
150 uint64_t progress_target_ = 0u;
151 uint64_t accumulated_progress_ = 0u;
152 int64_t sample_durations_[2] = {0, 0};
153 size_t num_samples_ = 0;
154 std::vector<uint64_t> stream_durations_;
155 std::vector<KeyFrameInfo> key_frame_infos_;
156
157 DISALLOW_COPY_AND_ASSIGN(Segmenter);
158};
159
160} // namespace mp4
161} // namespace media
162} // namespace shaka
163
164#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:103
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:283
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.