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 
79  protected:
80  TrackFragment* traf() { return traf_; }
81 
85  template <typename T>
86  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
87 
88  private:
89  Status FinalizeFragmentForEncryption();
90  // Check if the current fragment starts with SAP.
91  bool StartsWithSAP() const;
92 
93  std::shared_ptr<const StreamInfo> stream_info_;
94  bool use_decoding_timestamp_in_timeline_;
95  TrackFragment* traf_;
96  uint64_t seek_preroll_;
97  bool fragment_initialized_;
98  bool fragment_finalized_;
99  uint64_t fragment_duration_;
100  int64_t earliest_presentation_time_;
101  bool first_fragment_ = true;
102  int64_t first_sap_time_;
103  std::unique_ptr<BufferWriter> data_;
104  // Saves key frames information, for Video.
105  std::vector<KeyFrameInfo> key_frame_infos_;
106 
107  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
108 };
109 
110 template <typename T>
111 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
112  T* default_value) {
113  DCHECK(entries);
114  DCHECK(default_value);
115  DCHECK(!entries->empty());
116 
117  typename std::vector<T>::const_iterator it = entries->begin();
118  T value = *it;
119  for (; it < entries->end(); ++it)
120  if (value != *it)
121  return false;
122 
123  // Clear |entries| if it contains only one value.
124  entries->clear();
125  *default_value = value;
126  return true;
127 }
128 
129 } // namespace mp4
130 } // namespace media
131 } // namespace shaka
132 
133 #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:230
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
Status FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:147
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:111