Shaka Packager SDK
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 <map>
11 #include <memory>
12 #include <optional>
13 #include <vector>
14 
15 #include <packager/macros/classes.h>
16 #include <packager/media/base/fourccs.h>
17 #include <packager/media/base/range.h>
18 #include <packager/media/formats/mp4/box_definitions.h>
19 #include <packager/status.h>
20 
21 namespace shaka {
22 namespace media {
23 
24 struct EncryptionConfig;
25 struct MuxerOptions;
26 struct SegmentInfo;
27 
28 class BufferWriter;
29 class MediaSample;
30 class MuxerListener;
31 class ProgressListener;
32 class StreamInfo;
33 
34 namespace mp4 {
35 
36 class Fragmenter;
37 struct KeyFrameInfo;
38 
45 class Segmenter {
46  public:
47  Segmenter(const MuxerOptions& options,
48  std::unique_ptr<FileType> ftyp,
49  std::unique_ptr<Movie> moov);
50  virtual ~Segmenter();
51 
59  Status Initialize(
60  const std::vector<std::shared_ptr<const StreamInfo>>& streams,
61  MuxerListener* muxer_listener,
62  ProgressListener* progress_listener);
63 
66  Status Finalize();
67 
72  Status AddSample(size_t stream_id, const MediaSample& sample);
73 
78  Status FinalizeSegment(size_t stream_id, const SegmentInfo& segment_info);
79 
80  // TODO(rkuroiwa): Change these Get*Range() methods to return
81  // std::optional<Range> as well.
84  virtual bool GetInitRange(size_t* offset, size_t* size) = 0;
85 
88  virtual bool GetIndexRange(size_t* offset, size_t* size) = 0;
89 
90  // Returns an empty vector if there are no specific ranges for the segments,
91  // e.g. the media is in multiple files.
92  // Otherwise, a vector of ranges for the media segments are returned.
93  virtual std::vector<Range> GetSegmentRanges() = 0;
94 
95  int32_t GetReferenceTimeScale() const;
96 
98  double GetDuration() const;
99 
102  int64_t sample_duration() const {
103  return sample_durations_[num_samples_ < 2 ? 0 : 1];
104  }
105 
106  protected:
108  void UpdateProgress(uint64_t progress);
110  void SetComplete();
111 
112  const MuxerOptions& options() const { return options_; }
113  FileType* ftyp() { return ftyp_.get(); }
114  Movie* moov() { return moov_.get(); }
115  BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
116  SegmentIndex* sidx() { return sidx_.get(); }
117  MuxerListener* muxer_listener() { return muxer_listener_; }
118  uint64_t progress_target() { return progress_target_; }
119  const std::vector<KeyFrameInfo>& key_frame_infos() const {
120  return key_frame_infos_;
121  }
122 
123  void set_progress_target(uint64_t progress_target) {
124  progress_target_ = progress_target;
125  }
126 
127  private:
128  virtual Status DoInitialize() = 0;
129  virtual Status DoFinalize() = 0;
130  virtual Status DoFinalizeSegment(int64_t segment_number) = 0;
131  virtual Status DoFinalizeChunk(int64_t segment_number) { return Status::OK; }
132 
133  uint32_t GetReferenceStreamId();
134 
135  void FinalizeFragmentForKeyRotation(
136  size_t stream_id,
137  bool fragment_encrypted,
138  const EncryptionConfig& encryption_config);
139 
140  const MuxerOptions& options_;
141  std::unique_ptr<FileType> ftyp_;
142  std::unique_ptr<Movie> moov_;
143  std::unique_ptr<MovieFragment> moof_;
144  std::unique_ptr<BufferWriter> fragment_buffer_;
145  std::unique_ptr<SegmentIndex> sidx_;
146  std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
147  MuxerListener* muxer_listener_ = nullptr;
148  ProgressListener* progress_listener_ = nullptr;
149  uint64_t progress_target_ = 0u;
150  uint64_t accumulated_progress_ = 0u;
151  int64_t sample_durations_[2] = {0, 0};
152  size_t num_samples_ = 0;
153  std::vector<uint64_t> stream_durations_;
154  std::vector<KeyFrameInfo> key_frame_infos_;
155 
156  DISALLOW_COPY_AND_ASSIGN(Segmenter);
157 };
158 
159 } // namespace mp4
160 } // namespace media
161 } // namespace shaka
162 
163 #endif // PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
Class to hold a media sample.
Definition: media_sample.h:25
This class listens to progress updates events.
virtual bool GetInitRange(size_t *offset, size_t *size)=0
double GetDuration() const
Definition: segmenter.cc:258
Status AddSample(size_t stream_id, const MediaSample &sample)
Definition: segmenter.cc:126
int64_t sample_duration() const
Definition: segmenter.h:102
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.
Definition: crypto_flags.cc:66
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:19