DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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 set_is_key_frame(bool value) {
126  is_key_frame_ = value;
127  }
128 
129  void set_is_encrypted(bool value) {
130  is_encrypted_ = value;
131  }
132 
133  // If there's no data in this buffer, it represents end of stream.
134  bool end_of_stream() const { return data_.size() == 0; }
135 
136  const std::string& config_id() const { return config_id_; }
137  void set_config_id(const std::string& config_id) {
138  config_id_ = config_id;
139  }
140 
142  std::string ToString() const;
143 
144  private:
145  friend class base::RefCountedThreadSafe<MediaSample>;
146 
147  // Create a MediaSample. Buffer will be padded and aligned as necessary.
148  // |data|,|side_data| can be NULL, which indicates an empty sample.
149  // |size|,|side_data_size| should not be negative.
150  MediaSample(const uint8_t* data,
151  size_t size,
152  const uint8_t* side_data,
153  size_t side_data_size,
154  bool is_key_frame);
155  MediaSample();
156  virtual ~MediaSample();
157 
158  // Decoding time stamp.
159  int64_t dts_;
160  // Presentation time stamp.
161  int64_t pts_;
162  int64_t duration_;
163  bool is_key_frame_;
164  // is sample encrypted ?
165  bool is_encrypted_;
166 
167  // Main buffer data.
168  std::vector<uint8_t> data_;
169  // Contain additional buffers to complete the main one. Needed by WebM
170  // http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
171  // Not used by mp4 and other containers.
172  std::vector<uint8_t> side_data_;
173 
174  // Text specific fields.
175  // For now this is the cue identifier for WebVTT.
176  std::string config_id_;
177 
178  DISALLOW_COPY_AND_ASSIGN(MediaSample);
179 };
180 
181 typedef std::deque<scoped_refptr<MediaSample> > BufferQueue;
182 
183 } // namespace media
184 } // namespace edash_packager
185 
186 #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