Shaka Packager SDK
segmenter.h
1 // Copyright 2014 Google Inc. 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 <vector>
13 
14 #include "packager/base/optional.h"
15 #include "packager/media/base/fourccs.h"
16 #include "packager/media/base/range.h"
17 #include "packager/media/formats/mp4/box_definitions.h"
18 #include "packager/status.h"
19 
20 namespace shaka {
21 namespace media {
22 
23 struct EncryptionConfig;
24 struct MuxerOptions;
25 struct SegmentInfo;
26 
27 class BufferWriter;
28 class MediaSample;
29 class MuxerListener;
30 class ProgressListener;
31 class StreamInfo;
32 
33 namespace mp4 {
34 
35 class Fragmenter;
36 struct KeyFrameInfo;
37 
44 class Segmenter {
45  public:
46  Segmenter(const MuxerOptions& options,
47  std::unique_ptr<FileType> ftyp,
48  std::unique_ptr<Movie> moov);
49  virtual ~Segmenter();
50 
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  // base::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  uint32_t GetReferenceTimeScale() const;
95 
97  double GetDuration() const;
98 
101  uint32_t sample_duration() const { return sample_duration_; }
102 
103  protected:
105  void UpdateProgress(uint64_t progress);
107  void SetComplete();
108 
109  const MuxerOptions& options() const { return options_; }
110  FileType* ftyp() { return ftyp_.get(); }
111  Movie* moov() { return moov_.get(); }
112  BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
113  SegmentIndex* sidx() { return sidx_.get(); }
114  MuxerListener* muxer_listener() { return muxer_listener_; }
115  uint64_t progress_target() { return progress_target_; }
116  const std::vector<KeyFrameInfo>& key_frame_infos() const {
117  return key_frame_infos_;
118  }
119 
120  void set_progress_target(uint64_t progress_target) {
121  progress_target_ = progress_target;
122  }
123 
124  private:
125  virtual Status DoInitialize() = 0;
126  virtual Status DoFinalize() = 0;
127  virtual Status DoFinalizeSegment() = 0;
128 
129  uint32_t GetReferenceStreamId();
130 
131  void FinalizeFragmentForKeyRotation(
132  size_t stream_id,
133  bool fragment_encrypted,
134  const EncryptionConfig& encryption_config);
135 
136  const MuxerOptions& options_;
137  std::unique_ptr<FileType> ftyp_;
138  std::unique_ptr<Movie> moov_;
139  std::unique_ptr<MovieFragment> moof_;
140  std::unique_ptr<BufferWriter> fragment_buffer_;
141  std::unique_ptr<SegmentIndex> sidx_;
142  std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
143  MuxerListener* muxer_listener_ = nullptr;
144  ProgressListener* progress_listener_ = nullptr;
145  uint64_t progress_target_ = 0u;
146  uint64_t accumulated_progress_ = 0u;
147  uint32_t sample_duration_ = 0u;
148  std::vector<uint64_t> stream_durations_;
149  std::vector<KeyFrameInfo> key_frame_infos_;
150 
151  DISALLOW_COPY_AND_ASSIGN(Segmenter);
152 };
153 
154 } // namespace mp4
155 } // namespace media
156 } // namespace shaka
157 
158 #endif // PACKAGER_MEDIA_FORMATS_MP4_SEGMENTER_H_
Status Initialize(const std::vector< std::shared_ptr< const StreamInfo >> &streams, MuxerListener *muxer_listener, ProgressListener *progress_listener)
Definition: segmenter.cc:51
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:250
void SetComplete()
Set progress to 100%.
Definition: segmenter.cc:266
virtual bool GetInitRange(size_t *offset, size_t *size)=0
Status AddSample(size_t stream_id, const MediaSample &sample)
Definition: segmenter.cc:123
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20
This class listens to progress updates events.
Class to hold a media sample.
Definition: media_sample.h:22
double GetDuration() const
Definition: segmenter.cc:241
Status FinalizeSegment(size_t stream_id, const SegmentInfo &segment_info)
Definition: segmenter.cc:147
uint32_t sample_duration() const
Definition: segmenter.h:101
virtual bool GetIndexRange(size_t *offset, size_t *size)=0