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