Shaka Packager SDK
fragmenter.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_FRAGMENTER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP4_FRAGMENTER_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "packager/base/logging.h"
14 #include "packager/status.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 class BufferWriter;
20 class MediaSample;
21 class StreamInfo;
22 
23 namespace mp4 {
24 
25 struct KeyFrameInfo;
26 struct SegmentReference;
27 struct TrackFragment;
28 
31 class Fragmenter {
32  public:
35  Fragmenter(std::shared_ptr<const StreamInfo> info, TrackFragment* traf);
36 
37  ~Fragmenter();
38 
42  Status AddSample(const MediaSample& sample);
43 
48  Status InitializeFragment(int64_t first_sample_dts);
49 
52 
54  void GenerateSegmentReference(SegmentReference* reference) const;
55 
56  void ClearFragmentFinalized() { fragment_finalized_ = false; }
57 
58  uint64_t fragment_duration() const { return fragment_duration_; }
59  uint64_t first_sap_time() const { return first_sap_time_; }
60  uint64_t earliest_presentation_time() const {
61  return earliest_presentation_time_;
62  }
63  bool fragment_initialized() const { return fragment_initialized_; }
64  bool fragment_finalized() const { return fragment_finalized_; }
65  BufferWriter* data() { return data_.get(); }
66  const std::vector<KeyFrameInfo>& key_frame_infos() const {
67  return key_frame_infos_;
68  }
69 
75  bool use_decoding_timestamp_in_timeline) {
76  use_decoding_timestamp_in_timeline_ = use_decoding_timestamp_in_timeline;
77  }
78 
83  bool allow_adjust_earliest_presentation_time) {
84  allow_adjust_earliest_presentation_time_ =
85  allow_adjust_earliest_presentation_time;
86  }
87 
88  protected:
89  TrackFragment* traf() { return traf_; }
90 
94  template <typename T>
95  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
96 
97  private:
98  Status FinalizeFragmentForEncryption();
99  // Check if the current fragment starts with SAP.
100  bool StartsWithSAP() const;
101 
102  std::shared_ptr<const StreamInfo> stream_info_;
103  bool use_decoding_timestamp_in_timeline_;
104  TrackFragment* traf_;
105  uint64_t seek_preroll_;
106  bool fragment_initialized_;
107  bool fragment_finalized_;
108  uint64_t fragment_duration_;
109  int64_t earliest_presentation_time_;
110  bool first_fragment_ = true;
111  bool allow_adjust_earliest_presentation_time_ = false;
112  int64_t first_sap_time_;
113  std::unique_ptr<BufferWriter> data_;
114  // Saves key frames information, for Video.
115  std::vector<KeyFrameInfo> key_frame_infos_;
116 
117  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
118 };
119 
120 template <typename T>
121 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
122  T* default_value) {
123  DCHECK(entries);
124  DCHECK(default_value);
125  DCHECK(!entries->empty());
126 
127  typename std::vector<T>::const_iterator it = entries->begin();
128  T value = *it;
129  for (; it < entries->end(); ++it)
130  if (value != *it)
131  return false;
132 
133  // Clear |entries| if it contains only one value.
134  entries->clear();
135  *default_value = value;
136  return true;
137 }
138 
139 } // namespace mp4
140 } // namespace media
141 } // namespace shaka
142 
143 #endif // PACKAGER_MEDIA_FORMATS_MP4_FRAGMENTER_H_
void set_use_decoding_timestamp_in_timeline(bool use_decoding_timestamp_in_timeline)
Definition: fragmenter.h:74
All the methods that are virtual are virtual for mocking.
Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:123
void GenerateSegmentReference(SegmentReference *reference) const
Fill reference with current fragment information.
Definition: fragmenter.cc:232
Fragmenter(std::shared_ptr< const StreamInfo > info, TrackFragment *traf)
Definition: fragmenter.cc:49
Status AddSample(const MediaSample &sample)
Definition: fragmenter.cc:66
Class to hold a media sample.
Definition: media_sample.h:22
void set_allow_adjust_earliest_presentation_time(bool allow_adjust_earliest_presentation_time)
Definition: fragmenter.h:82
Status FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:147
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:121