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:
37  Fragmenter(std::shared_ptr<const StreamInfo> info,
38  TrackFragment* traf,
39  int64_t edit_list_offset);
40 
41  ~Fragmenter();
42 
46  Status AddSample(const MediaSample& sample);
47 
52  Status InitializeFragment(int64_t first_sample_dts);
53 
56 
58  void GenerateSegmentReference(SegmentReference* reference) const;
59 
60  void ClearFragmentFinalized() { fragment_finalized_ = false; }
61 
62  uint64_t fragment_duration() const { return fragment_duration_; }
63  uint64_t first_sap_time() const { return first_sap_time_; }
64  uint64_t earliest_presentation_time() const {
65  return earliest_presentation_time_;
66  }
67  bool fragment_initialized() const { return fragment_initialized_; }
68  bool fragment_finalized() const { return fragment_finalized_; }
69  BufferWriter* data() { return data_.get(); }
70  const std::vector<KeyFrameInfo>& key_frame_infos() const {
71  return key_frame_infos_;
72  }
73 
74  protected:
75  TrackFragment* traf() { return traf_; }
76 
80  template <typename T>
81  bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value);
82 
83  private:
84  Status FinalizeFragmentForEncryption();
85  // Check if the current fragment starts with SAP.
86  bool StartsWithSAP() const;
87 
88  std::shared_ptr<const StreamInfo> stream_info_;
89  TrackFragment* traf_ = nullptr;
90  int64_t edit_list_offset_ = 0;
91  int64_t seek_preroll_ = 0;
92  bool fragment_initialized_ = false;
93  bool fragment_finalized_ = false;
94  int64_t fragment_duration_ = 0;
95  int64_t earliest_presentation_time_ = 0;
96  int64_t first_sap_time_ = 0;
97  std::unique_ptr<BufferWriter> data_;
98  // Saves key frames information, for Video.
99  std::vector<KeyFrameInfo> key_frame_infos_;
100 
101  DISALLOW_COPY_AND_ASSIGN(Fragmenter);
102 };
103 
104 template <typename T>
105 bool Fragmenter::OptimizeSampleEntries(std::vector<T>* entries,
106  T* default_value) {
107  DCHECK(entries);
108  DCHECK(default_value);
109  DCHECK(!entries->empty());
110 
111  typename std::vector<T>::const_iterator it = entries->begin();
112  T value = *it;
113  for (; it < entries->end(); ++it)
114  if (value != *it)
115  return false;
116 
117  // Clear |entries| if it contains only one value.
118  entries->clear();
119  *default_value = value;
120  return true;
121 }
122 
123 } // namespace mp4
124 } // namespace media
125 } // namespace shaka
126 
127 #endif // PACKAGER_MEDIA_FORMATS_MP4_FRAGMENTER_H_
shaka::media::BufferWriter
Definition: buffer_writer.h:23
shaka::media::mp4::Fragmenter::OptimizeSampleEntries
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:105
shaka::media::mp4::Fragmenter::GenerateSegmentReference
void GenerateSegmentReference(SegmentReference *reference) const
Fill reference with current fragment information.
Definition: fragmenter.cc:227
shaka::media::mp4::Fragmenter
Definition: fragmenter.h:31
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::mp4::SegmentReference
Definition: box_definitions.h:779
shaka::media::mp4::Fragmenter::FinalizeFragment
Status FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:159
shaka::media::mp4::Fragmenter::InitializeFragment
Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:129
shaka::media::mp4::TrackFragment
Definition: box_definitions.h:757
shaka::Status
Definition: status.h:110
shaka::media::mp4::Fragmenter::AddSample
Status AddSample(const MediaSample &sample)
Definition: fragmenter.cc:65
shaka::media::mp4::Fragmenter::Fragmenter
Fragmenter(std::shared_ptr< const StreamInfo > info, TrackFragment *traf, int64_t edit_list_offset)
Definition: fragmenter.cc:50
shaka::media::MediaSample
Class to hold a media sample.
Definition: media_sample.h:22