Shaka Packager SDK
Loading...
Searching...
No Matches
media_sample.h
1// Copyright 2014 Google LLC. 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 PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
8#define PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
9
10#include <cstdint>
11#include <deque>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include <absl/log/check.h>
17#include <absl/log/log.h>
18
19#include <packager/macros/classes.h>
20#include <packager/media/base/decrypt_config.h>
21
22namespace shaka {
23namespace media {
24
27 public:
33 static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
34 size_t size,
35 bool is_key_frame);
36
46 static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
47 size_t size,
48 const uint8_t* side_data,
49 size_t side_data_size,
50 bool is_key_frame);
51
58 static std::shared_ptr<MediaSample> FromMetadata(const uint8_t* metadata,
59 size_t metadata_size);
60
62 static std::shared_ptr<MediaSample> CreateEmptyMediaSample();
63
67 static std::shared_ptr<MediaSample> CreateEOSBuffer();
68
69 virtual ~MediaSample();
70
72 std::shared_ptr<MediaSample> Clone() const;
73
77 void TransferData(std::shared_ptr<uint8_t> data, size_t data_size);
78
83 void SetData(const uint8_t* data, size_t data_size);
84
86 std::string ToString() const;
87
88 int64_t dts() const {
89 DCHECK(!end_of_stream());
90 return dts_;
91 }
92
93 void set_dts(int64_t dts) { dts_ = dts; }
94
95 int64_t pts() const {
96 DCHECK(!end_of_stream());
97 return pts_;
98 }
99
100 void set_pts(int64_t pts) { pts_ = pts; }
101
102 int64_t duration() const {
103 DCHECK(!end_of_stream());
104 return duration_;
105 }
106
107 void set_duration(int64_t duration) {
108 DCHECK(!end_of_stream());
109 duration_ = duration;
110 }
111
112 bool is_key_frame() const {
113 DCHECK(!end_of_stream());
114 return is_key_frame_;
115 }
116
117 bool is_encrypted() const {
118 DCHECK(!end_of_stream());
119 return is_encrypted_;
120 }
121 const uint8_t* data() const {
122 DCHECK(!end_of_stream());
123 return data_.get();
124 }
125
126 size_t data_size() const {
127 DCHECK(!end_of_stream());
128 return data_size_;
129 }
130
131 const uint8_t* side_data() const { return side_data_.get(); }
132
133 size_t side_data_size() const { return side_data_size_; }
134
135 const DecryptConfig* decrypt_config() const { return decrypt_config_.get(); }
136
137 void set_is_key_frame(bool value) {
138 is_key_frame_ = value;
139 }
140
141 void set_is_encrypted(bool value) {
142 is_encrypted_ = value;
143 }
144
145 void set_decrypt_config(std::unique_ptr<DecryptConfig> decrypt_config) {
146 decrypt_config_ = std::move(decrypt_config);
147 }
148
149 // If there's no data in this buffer, it represents end of stream.
150 bool end_of_stream() const { return data_size_ == 0; }
151
152 const std::string& config_id() const { return config_id_; }
153 void set_config_id(const std::string& config_id) {
154 config_id_ = config_id;
155 }
156
157 protected:
158 // Made it protected to disallow the constructor to be called directly.
159 // Create a MediaSample. Buffer will be padded and aligned as necessary.
160 // |data|,|side_data| can be nullptr, which indicates an empty sample.
161 MediaSample(const uint8_t* data,
162 size_t data_size,
163 const uint8_t* side_data,
164 size_t side_data_size,
165 bool is_key_frame);
166 MediaSample();
167
168 private:
169 // Decoding time stamp.
170 int64_t dts_ = 0;
171 // Presentation time stamp.
172 int64_t pts_ = 0;
173 int64_t duration_ = 0;
174 bool is_key_frame_ = false;
175 // is sample encrypted ?
176 bool is_encrypted_ = false;
177
178 // Main buffer data.
179 std::shared_ptr<const uint8_t> data_;
180 size_t data_size_ = 0;
181 // Contain additional buffers to complete the main one. Needed by WebM
182 // http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
183 // Not used by mp4 and other containers.
184 std::shared_ptr<const uint8_t> side_data_;
185 size_t side_data_size_ = 0;
186
187 // Text specific fields.
188 // For now this is the cue identifier for WebVTT.
189 std::string config_id_;
190
191 // Decrypt configuration.
192 std::unique_ptr<DecryptConfig> decrypt_config_;
193
194 DISALLOW_COPY_AND_ASSIGN(MediaSample);
195};
196
197typedef std::deque<std::shared_ptr<MediaSample>> BufferQueue;
198
199} // namespace media
200} // namespace shaka
201
202#endif // PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
Class to hold a media sample.
static std::shared_ptr< MediaSample > CreateEOSBuffer()
static std::shared_ptr< MediaSample > CreateEmptyMediaSample()
Create a MediaSample object with default members.
void TransferData(std::shared_ptr< uint8_t > data, size_t data_size)
std::shared_ptr< MediaSample > Clone() const
Clone the object and return a new MediaSample.
static std::shared_ptr< MediaSample > FromMetadata(const uint8_t *metadata, size_t metadata_size)
void SetData(const uint8_t *data, size_t data_size)
std::string ToString() const
static std::shared_ptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
All the methods that are virtual are virtual for mocking.