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