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