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) { is_key_frame_ = value; }
138
139 void set_is_encrypted(bool value) { is_encrypted_ = value; }
140
141 void set_decrypt_config(std::unique_ptr<DecryptConfig> decrypt_config) {
142 decrypt_config_ = std::move(decrypt_config);
143 }
144
145 // If there's no data in this buffer, it represents end of stream.
146 bool end_of_stream() const { return data_size_ == 0; }
147
148 const std::string& config_id() const { return config_id_; }
149 void set_config_id(const std::string& config_id) { config_id_ = config_id; }
150
151 protected:
152 // Made it protected to disallow the constructor to be called directly.
153 // Create a MediaSample. Buffer will be padded and aligned as necessary.
154 // |data|,|side_data| can be nullptr, which indicates an empty sample.
155 MediaSample(const uint8_t* data,
156 size_t data_size,
157 const uint8_t* side_data,
158 size_t side_data_size,
159 bool is_key_frame);
160 MediaSample();
161
162 private:
163 // Decoding time stamp.
164 int64_t dts_ = 0;
165 // Presentation time stamp.
166 int64_t pts_ = 0;
167 int64_t duration_ = 0;
168 bool is_key_frame_ = false;
169 // is sample encrypted ?
170 bool is_encrypted_ = false;
171
172 // Main buffer data.
173 std::shared_ptr<const uint8_t> data_;
174 size_t data_size_ = 0;
175 // Contain additional buffers to complete the main one. Needed by WebM
176 // http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
177 // Not used by mp4 and other containers.
178 std::shared_ptr<const uint8_t> side_data_;
179 size_t side_data_size_ = 0;
180
181 // Text specific fields.
182 // For now this is the cue identifier for WebVTT.
183 std::string config_id_;
184
185 // Decrypt configuration.
186 std::unique_ptr<DecryptConfig> decrypt_config_;
187
188 DISALLOW_COPY_AND_ASSIGN(MediaSample);
189};
190
191typedef std::deque<std::shared_ptr<MediaSample>> BufferQueue;
192
193} // namespace media
194} // namespace shaka
195
196#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.