Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MEDIA_FORMATS_MP4_SEGMENTER_H_
8 #define 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 
43 class Segmenter {
44  public:
45  Segmenter(const MuxerOptions& options,
46  std::unique_ptr<FileType> ftyp,
47  std::unique_ptr<Movie> moov);
48  virtual ~Segmenter();
49 
58  const std::vector<std::shared_ptr<const StreamInfo>>& streams,
59  MuxerListener* muxer_listener,
60  ProgressListener* progress_listener);
61 
64  Status Finalize();
65 
70  Status AddSample(size_t stream_id, const MediaSample& sample);
71 
76  Status FinalizeSegment(size_t stream_id, const SegmentInfo& segment_info);
77 
78  // TODO(rkuroiwa): Change these Get*Range() methods to return
79  // base::Optional<Range> as well.
82  virtual bool GetInitRange(size_t* offset, size_t* size) = 0;
83 
86  virtual bool GetIndexRange(size_t* offset, size_t* size) = 0;
87 
88  // Returns an empty vector if there are no specific ranges for the segments,
89  // e.g. the media is in multiple files.
90  // Otherwise, a vector of ranges for the media segments are returned.
91  virtual std::vector<Range> GetSegmentRanges() = 0;
92 
93  uint32_t GetReferenceTimeScale() const;
94 
96  double GetDuration() const;
97 
100  uint32_t sample_duration() const { return sample_duration_; }
101 
102  protected:
104  void UpdateProgress(uint64_t progress);
106  void SetComplete();
107 
108  const MuxerOptions& options() const { return options_; }
109  FileType* ftyp() { return ftyp_.get(); }
110  Movie* moov() { return moov_.get(); }
111  BufferWriter* fragment_buffer() { return fragment_buffer_.get(); }
112  SegmentIndex* sidx() { return sidx_.get(); }
113  MuxerListener* muxer_listener() { return muxer_listener_; }
114  uint64_t progress_target() { return progress_target_; }
115 
116  void set_progress_target(uint64_t progress_target) {
117  progress_target_ = progress_target;
118  }
119 
120  private:
121  virtual Status DoInitialize() = 0;
122  virtual Status DoFinalize() = 0;
123  virtual Status DoFinalizeSegment() = 0;
124 
125  uint32_t GetReferenceStreamId();
126 
127  void FinalizeFragmentForKeyRotation(
128  size_t stream_id,
129  bool fragment_encrypted,
130  const EncryptionConfig& encryption_config);
131 
132  const MuxerOptions& options_;
133  std::unique_ptr<FileType> ftyp_;
134  std::unique_ptr<Movie> moov_;
135  std::unique_ptr<MovieFragment> moof_;
136  std::unique_ptr<BufferWriter> fragment_buffer_;
137  std::unique_ptr<SegmentIndex> sidx_;
138  std::vector<std::unique_ptr<Fragmenter>> fragmenters_;
139  MuxerListener* muxer_listener_ = nullptr;
140  ProgressListener* progress_listener_ = nullptr;
141  uint64_t progress_target_ = 0u;
142  uint64_t accumulated_progress_ = 0u;
143  uint32_t sample_duration_ = 0u;
144  std::vector<uint64_t> stream_durations_;
145 
146  DISALLOW_COPY_AND_ASSIGN(Segmenter);
147 };
148 
149 } // namespace mp4
150 } // namespace media
151 } // namespace shaka
152 
153 #endif // 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:49
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:224
void SetComplete()
Set progress to 100%.
Definition: segmenter.cc:240
virtual bool GetInitRange(size_t *offset, size_t *size)=0
Status AddSample(size_t stream_id, const MediaSample &sample)
Definition: segmenter.cc:115
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20
uint32_t sample_duration() const
Definition: segmenter.h:100
This class listens to progress updates events.
Class to hold a media sample.
Definition: media_sample.h:22
Status FinalizeSegment(size_t stream_id, const SegmentInfo &segment_info)
Definition: segmenter.cc:139
double GetDuration() const
Definition: segmenter.cc:215
virtual bool GetIndexRange(size_t *offset, size_t *size)=0