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 <vector>
11 
12 #include "packager/base/logging.h"
13 #include "packager/base/memory/ref_counted.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 #include "packager/media/base/status.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 class BufferWriter;
21 class MediaSample;
22 class StreamInfo;
23 
24 namespace mp4 {
25 
26 struct SegmentReference;
27 struct TrackFragment;
28 
31 class Fragmenter {
32  public:
35  Fragmenter(scoped_refptr<StreamInfo> info, TrackFragment* traf);
36 
37  virtual ~Fragmenter();
38 
42  virtual Status AddSample(scoped_refptr<MediaSample> sample);
43 
48  virtual Status InitializeFragment(int64_t first_sample_dts);
49 
51  virtual void FinalizeFragment();
52 
55 
56  uint64_t fragment_duration() const { return fragment_duration_; }
57  uint64_t first_sap_time() const { return first_sap_time_; }
58  uint64_t earliest_presentation_time() const {
59  return earliest_presentation_time_;
60  }
61  bool fragment_initialized() const { return fragment_initialized_; }
62  bool fragment_finalized() const { return fragment_finalized_; }
63  BufferWriter* data() { return data_.get(); }
64 
65  protected:
66  TrackFragment* traf() { return traf_; }
67 
71  template <typename T>
72  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
73 
74  private:
75  // Check if the current fragment starts with SAP.
76  bool StartsWithSAP();
77 
78  TrackFragment* traf_;
79  uint64_t seek_preroll_;
80  bool fragment_initialized_;
81  bool fragment_finalized_;
82  uint64_t fragment_duration_;
83  int64_t presentation_start_time_;
84  int64_t earliest_presentation_time_;
85  int64_t first_sap_time_;
86  scoped_ptr<BufferWriter> data_;
87 
88  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
89 };
90 
91 template <typename T>
92 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
93  T* default_value) {
94  DCHECK(entries);
95  DCHECK(default_value);
96  DCHECK(!entries->empty());
97 
98  typename std::vector<T>::const_iterator it = entries->begin();
99  T value = *it;
100  for (; it < entries->end(); ++it)
101  if (value != *it)
102  return false;
103 
104  // Clear |entries| if it contains only one value.
105  entries->clear();
106  *default_value = value;
107  return true;
108 }
109 
110 } // namespace mp4
111 } // namespace media
112 } // namespace shaka
113 
114 #endif // MEDIA_FORMATS_MP4_FRAGMENTER_H_
Fragmenter(scoped_refptr< StreamInfo > info, TrackFragment *traf)
Definition: fragmenter.cc:31
virtual Status AddSample(scoped_refptr< MediaSample > sample)
Definition: fragmenter.cc:45
virtual Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:85
virtual void FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:104
void GenerateSegmentReference(SegmentReference *reference)
Fill reference with current fragment information.
Definition: fragmenter.cc:160
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:92