Shaka Packager SDK
fragmenter.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_FRAGMENTER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP4_FRAGMENTER_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include <absl/log/check.h>
14 #include <absl/log/log.h>
15 
16 #include <packager/macros/classes.h>
17 #include <packager/status.h>
18 
19 namespace shaka {
20 namespace media {
21 
22 class BufferWriter;
23 class MediaSample;
24 class StreamInfo;
25 
26 namespace mp4 {
27 
28 struct KeyFrameInfo;
29 struct SegmentReference;
30 struct TrackFragment;
31 
34 class Fragmenter {
35  public:
40  Fragmenter(std::shared_ptr<const StreamInfo> info,
41  TrackFragment* traf,
42  int64_t edit_list_offset);
43 
44  ~Fragmenter();
45 
49  Status AddSample(const MediaSample& sample);
50 
55  Status InitializeFragment(int64_t first_sample_dts);
56 
58  Status FinalizeFragment();
59 
61  void GenerateSegmentReference(SegmentReference* reference) const;
62 
63  void ClearFragmentFinalized() { fragment_finalized_ = false; }
64 
65  int64_t fragment_duration() const { return fragment_duration_; }
66  int64_t first_sap_time() const { return first_sap_time_; }
67  int64_t earliest_presentation_time() const {
68  return earliest_presentation_time_;
69  }
70  bool fragment_initialized() const { return fragment_initialized_; }
71  bool fragment_finalized() const { return fragment_finalized_; }
72  BufferWriter* data() { return data_.get(); }
73  const std::vector<KeyFrameInfo>& key_frame_infos() const {
74  return key_frame_infos_;
75  }
76 
77  protected:
78  TrackFragment* traf() { return traf_; }
79 
83  template <typename T>
84  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
85 
86  private:
87  Status FinalizeFragmentForEncryption();
88  // Check if the current fragment starts with SAP.
89  bool StartsWithSAP() const;
90 
91  std::shared_ptr<const StreamInfo> stream_info_;
92  TrackFragment* traf_ = nullptr;
93  int64_t edit_list_offset_ = 0;
94  int64_t seek_preroll_ = 0;
95  bool fragment_initialized_ = false;
96  bool fragment_finalized_ = false;
97  int64_t fragment_duration_ = 0;
98  int64_t earliest_presentation_time_ = 0;
99  int64_t first_sap_time_ = 0;
100  std::unique_ptr<BufferWriter> data_;
101  // Saves key frames information, for Video.
102  std::vector<KeyFrameInfo> key_frame_infos_;
103 
104  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
105 };
106 
107 template <typename T>
108 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
109  T* default_value) {
110  DCHECK(entries);
111  DCHECK(default_value);
112  DCHECK(!entries->empty());
113 
114  typename std::vector<T>::const_iterator it = entries->begin();
115  T value = *it;
116  for (; it < entries->end(); ++it)
117  if (value != *it)
118  return false;
119 
120  // Clear |entries| if it contains only one value.
121  entries->clear();
122  *default_value = value;
123  return true;
124 }
125 
126 } // namespace mp4
127 } // namespace media
128 } // namespace shaka
129 
130 #endif // PACKAGER_MEDIA_FORMATS_MP4_FRAGMENTER_H_
Class to hold a media sample.
Definition: media_sample.h:25
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:108
Status AddSample(const MediaSample &sample)
Definition: fragmenter.cc:67
Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:131
void GenerateSegmentReference(SegmentReference *reference) const
Fill reference with current fragment information.
Definition: fragmenter.cc:229
Status FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:161
Fragmenter(std::shared_ptr< const StreamInfo > info, TrackFragment *traf, int64_t edit_list_offset)
Definition: fragmenter.cc:52
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66