DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 MEDIA_FORMATS_MP4_FRAGMENTER_H_
8 #define MEDIA_FORMATS_MP4_FRAGMENTER_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "packager/base/logging.h"
14 #include "packager/media/base/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 SegmentReference;
26 struct TrackFragment;
27 
30 class Fragmenter {
31  public:
34  Fragmenter(std::shared_ptr<StreamInfo> info, TrackFragment* traf);
35 
36  ~Fragmenter();
37 
41  Status AddSample(std::shared_ptr<MediaSample> sample);
42 
47  Status InitializeFragment(int64_t first_sample_dts);
48 
51 
54 
55  void ClearFragmentFinalized() { fragment_finalized_ = false; }
56 
57  uint64_t fragment_duration() const { return fragment_duration_; }
58  uint64_t first_sap_time() const { return first_sap_time_; }
59  uint64_t earliest_presentation_time() const {
60  return earliest_presentation_time_;
61  }
62  bool fragment_initialized() const { return fragment_initialized_; }
63  bool fragment_finalized() const { return fragment_finalized_; }
64  BufferWriter* data() { return data_.get(); }
65 
71  bool use_decoding_timestamp_in_timeline) {
72  use_decoding_timestamp_in_timeline_ = use_decoding_timestamp_in_timeline;
73  }
74 
75  protected:
76  TrackFragment* traf() { return traf_; }
77 
81  template <typename T>
82  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
83 
84  private:
85  Status FinalizeFragmentForEncryption();
86  // Check if the current fragment starts with SAP.
87  bool StartsWithSAP();
88 
89  std::shared_ptr<StreamInfo> stream_info_;
90  bool use_decoding_timestamp_in_timeline_;
91  TrackFragment* traf_;
92  uint64_t seek_preroll_;
93  bool fragment_initialized_;
94  bool fragment_finalized_;
95  uint64_t fragment_duration_;
96  int64_t earliest_presentation_time_;
97  int64_t first_sap_time_;
98  std::unique_ptr<BufferWriter> data_;
99 
100  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
101 };
102 
103 template <typename T>
104 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
105  T* default_value) {
106  DCHECK(entries);
107  DCHECK(default_value);
108  DCHECK(!entries->empty());
109 
110  typename std::vector<T>::const_iterator it = entries->begin();
111  T value = *it;
112  for (; it < entries->end(); ++it)
113  if (value != *it)
114  return false;
115 
116  // Clear |entries| if it contains only one value.
117  entries->clear();
118  *default_value = value;
119  return true;
120 }
121 
122 } // namespace mp4
123 } // namespace media
124 } // namespace shaka
125 
126 #endif // MEDIA_FORMATS_MP4_FRAGMENTER_H_
void set_use_decoding_timestamp_in_timeline(bool use_decoding_timestamp_in_timeline)
Definition: fragmenter.h:70
Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:116
Fragmenter(std::shared_ptr< StreamInfo > info, TrackFragment *traf)
Definition: fragmenter.cc:47
Status AddSample(std::shared_ptr< MediaSample > sample)
Definition: fragmenter.cc:64
void GenerateSegmentReference(SegmentReference *reference)
Fill reference with current fragment information.
Definition: fragmenter.cc:204
Status FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:139
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:104