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