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