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