Shaka Packager SDK
media_sample.h
1 // Copyright 2014 Google LLC. 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_BASE_MEDIA_SAMPLE_H_
8 #define PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
9 
10 #include <deque>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include <absl/log/check.h>
16 #include <absl/log/log.h>
17 
18 #include <packager/macros/classes.h>
19 #include <packager/media/base/decrypt_config.h>
20 
21 namespace shaka {
22 namespace media {
23 
25 class MediaSample {
26  public:
32  static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
33  size_t size,
34  bool is_key_frame);
35 
45  static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
46  size_t size,
47  const uint8_t* side_data,
48  size_t side_data_size,
49  bool is_key_frame);
50 
57  static std::shared_ptr<MediaSample> FromMetadata(const uint8_t* metadata,
58  size_t metadata_size);
59 
61  static std::shared_ptr<MediaSample> CreateEmptyMediaSample();
62 
66  static std::shared_ptr<MediaSample> CreateEOSBuffer();
67 
68  virtual ~MediaSample();
69 
71  std::shared_ptr<MediaSample> Clone() const;
72 
76  void TransferData(std::shared_ptr<uint8_t> data, size_t data_size);
77 
82  void SetData(const uint8_t* data, size_t data_size);
83 
85  std::string ToString() const;
86 
87  int64_t dts() const {
88  DCHECK(!end_of_stream());
89  return dts_;
90  }
91 
92  void set_dts(int64_t dts) { dts_ = dts; }
93 
94  int64_t pts() const {
95  DCHECK(!end_of_stream());
96  return pts_;
97  }
98 
99  void set_pts(int64_t pts) { pts_ = pts; }
100 
101  int64_t duration() const {
102  DCHECK(!end_of_stream());
103  return duration_;
104  }
105 
106  void set_duration(int64_t duration) {
107  DCHECK(!end_of_stream());
108  duration_ = duration;
109  }
110 
111  bool is_key_frame() const {
112  DCHECK(!end_of_stream());
113  return is_key_frame_;
114  }
115 
116  bool is_encrypted() const {
117  DCHECK(!end_of_stream());
118  return is_encrypted_;
119  }
120  const uint8_t* data() const {
121  DCHECK(!end_of_stream());
122  return data_.get();
123  }
124 
125  size_t data_size() const {
126  DCHECK(!end_of_stream());
127  return data_size_;
128  }
129 
130  const uint8_t* side_data() const { return side_data_.get(); }
131 
132  size_t side_data_size() const { return side_data_size_; }
133 
134  const DecryptConfig* decrypt_config() const { return decrypt_config_.get(); }
135 
136  void set_is_key_frame(bool value) {
137  is_key_frame_ = value;
138  }
139 
140  void set_is_encrypted(bool value) {
141  is_encrypted_ = value;
142  }
143 
144  void set_decrypt_config(std::unique_ptr<DecryptConfig> decrypt_config) {
145  decrypt_config_ = std::move(decrypt_config);
146  }
147 
148  // If there's no data in this buffer, it represents end of stream.
149  bool end_of_stream() const { return data_size_ == 0; }
150 
151  const std::string& config_id() const { return config_id_; }
152  void set_config_id(const std::string& config_id) {
153  config_id_ = config_id;
154  }
155 
156  protected:
157  // Made it protected to disallow the constructor to be called directly.
158  // Create a MediaSample. Buffer will be padded and aligned as necessary.
159  // |data|,|side_data| can be nullptr, which indicates an empty sample.
160  MediaSample(const uint8_t* data,
161  size_t data_size,
162  const uint8_t* side_data,
163  size_t side_data_size,
164  bool is_key_frame);
165  MediaSample();
166 
167  private:
168  // Decoding time stamp.
169  int64_t dts_ = 0;
170  // Presentation time stamp.
171  int64_t pts_ = 0;
172  int64_t duration_ = 0;
173  bool is_key_frame_ = false;
174  // is sample encrypted ?
175  bool is_encrypted_ = false;
176 
177  // Main buffer data.
178  std::shared_ptr<const uint8_t> data_;
179  size_t data_size_ = 0;
180  // Contain additional buffers to complete the main one. Needed by WebM
181  // http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
182  // Not used by mp4 and other containers.
183  std::shared_ptr<const uint8_t> side_data_;
184  size_t side_data_size_ = 0;
185 
186  // Text specific fields.
187  // For now this is the cue identifier for WebVTT.
188  std::string config_id_;
189 
190  // Decrypt configuration.
191  std::unique_ptr<DecryptConfig> decrypt_config_;
192 
193  DISALLOW_COPY_AND_ASSIGN(MediaSample);
194 };
195 
196 typedef std::deque<std::shared_ptr<MediaSample>> BufferQueue;
197 
198 } // namespace media
199 } // namespace shaka
200 
201 #endif // PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
Class to hold a media sample.
Definition: media_sample.h:25
static std::shared_ptr< MediaSample > CreateEOSBuffer()
Definition: media_sample.cc:77
static std::shared_ptr< MediaSample > CreateEmptyMediaSample()
Create a MediaSample object with default members.
Definition: media_sample.cc:72
void TransferData(std::shared_ptr< uint8_t > data, size_t data_size)
std::shared_ptr< MediaSample > Clone() const
Clone the object and return a new MediaSample.
Definition: media_sample.cc:82
static std::shared_ptr< MediaSample > FromMetadata(const uint8_t *metadata, size_t metadata_size)
Definition: media_sample.cc:65
void SetData(const uint8_t *data, size_t data_size)
std::string ToString() const
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:43
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66