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  virtual ~Fragmenter();
37 
41  virtual Status AddSample(std::shared_ptr<MediaSample> sample);
42 
47  virtual Status InitializeFragment(int64_t first_sample_dts);
48 
50  virtual void FinalizeFragment();
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  // Check if the current fragment starts with SAP.
86  bool StartsWithSAP();
87 
88  bool use_decoding_timestamp_in_timeline_;
89  TrackFragment* traf_;
90  uint64_t seek_preroll_;
91  bool fragment_initialized_;
92  bool fragment_finalized_;
93  uint64_t fragment_duration_;
94  int64_t earliest_presentation_time_;
95  int64_t first_sap_time_;
96  std::unique_ptr<BufferWriter> data_;
97 
98  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
99 };
100 
101 template <typename T>
102 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
103  T* default_value) {
104  DCHECK(entries);
105  DCHECK(default_value);
106  DCHECK(!entries->empty());
107 
108  typename std::vector<T>::const_iterator it = entries->begin();
109  T value = *it;
110  for (; it < entries->end(); ++it)
111  if (value != *it)
112  return false;
113 
114  // Clear |entries| if it contains only one value.
115  entries->clear();
116  *default_value = value;
117  return true;
118 }
119 
120 } // namespace mp4
121 } // namespace media
122 } // namespace shaka
123 
124 #endif // MEDIA_FORMATS_MP4_FRAGMENTER_H_
void set_use_decoding_timestamp_in_timeline(bool use_decoding_timestamp_in_timeline)
Definition: fragmenter.h:70
virtual Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:92
virtual void FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:111
Fragmenter(std::shared_ptr< StreamInfo > info, TrackFragment *traf)
Definition: fragmenter.cc:32
virtual Status AddSample(std::shared_ptr< MediaSample > sample)
Definition: fragmenter.cc:46
void GenerateSegmentReference(SegmentReference *reference)
Fill reference with current fragment information.
Definition: fragmenter.cc:169
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:102