7 #include <packager/media/base/media_sample.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 #include <absl/strings/str_format.h>
18 MediaSample::MediaSample(
const uint8_t* data,
20 const uint8_t* side_data,
21 size_t side_data_size,
23 : is_key_frame_(is_key_frame) {
25 CHECK_EQ(data_size, 0u);
28 SetData(data, data_size);
30 std::shared_ptr<uint8_t> shared_side_data(
new uint8_t[side_data_size],
31 std::default_delete<uint8_t[]>());
32 memcpy(shared_side_data.get(), side_data, side_data_size);
33 side_data_ = std::move(shared_side_data);
34 side_data_size_ = side_data_size;
38 MediaSample::MediaSample() {}
40 MediaSample::~MediaSample() {}
43 std::shared_ptr<MediaSample> MediaSample::CopyFrom(
const uint8_t* data,
48 return std::shared_ptr<MediaSample>(
49 new MediaSample(data, data_size,
nullptr, 0u, is_key_frame));
53 std::shared_ptr<MediaSample> MediaSample::CopyFrom(
const uint8_t* data,
55 const uint8_t* side_data,
56 size_t side_data_size,
60 return std::shared_ptr<MediaSample>(
new MediaSample(
61 data, data_size, side_data, side_data_size, is_key_frame));
65 std::shared_ptr<MediaSample> MediaSample::FromMetadata(
const uint8_t* metadata,
66 size_t metadata_size) {
67 return std::shared_ptr<MediaSample>(
68 new MediaSample(
nullptr, 0, metadata, metadata_size,
false));
72 std::shared_ptr<MediaSample> MediaSample::CreateEmptyMediaSample() {
73 return std::shared_ptr<MediaSample>(
new MediaSample);
77 std::shared_ptr<MediaSample> MediaSample::CreateEOSBuffer() {
78 return std::shared_ptr<MediaSample>(
82 std::shared_ptr<MediaSample> MediaSample::Clone()
const {
83 std::shared_ptr<MediaSample> new_media_sample(
new MediaSample);
84 new_media_sample->dts_ = dts_;
85 new_media_sample->pts_ = pts_;
86 new_media_sample->duration_ = duration_;
87 new_media_sample->is_key_frame_ = is_key_frame_;
88 new_media_sample->is_encrypted_ = is_encrypted_;
89 new_media_sample->data_ = data_;
90 new_media_sample->data_size_ = data_size_;
91 new_media_sample->side_data_ = side_data_;
92 new_media_sample->side_data_size_ = side_data_size_;
93 new_media_sample->config_id_ = config_id_;
94 if (decrypt_config_) {
96 decrypt_config_->key_id(), decrypt_config_->iv(),
97 decrypt_config_->subsamples(), decrypt_config_->protection_scheme(),
98 decrypt_config_->crypt_byte_block(),
99 decrypt_config_->skip_byte_block()));
101 return new_media_sample;
104 void MediaSample::TransferData(std::shared_ptr<uint8_t> data,
106 data_ = std::move(data);
107 data_size_ = data_size;
110 void MediaSample::SetData(
const uint8_t* data,
size_t data_size) {
111 std::shared_ptr<uint8_t> shared_data(
new uint8_t[data_size],
112 std::default_delete<uint8_t[]>());
113 memcpy(shared_data.get(), data, data_size);
114 TransferData(std::move(shared_data), data_size);
117 std::string MediaSample::ToString()
const {
119 return "End of stream sample\n";
120 return absl::StrFormat(
121 "dts: %" PRId64
"\n pts: %" PRId64
"\n duration: %" PRId64
123 "is_key_frame: %s\n size: %zu\n side_data_size: %zu\n",
124 dts_, pts_, duration_, is_key_frame_ ?
"true" :
"false", data_size_,
All the methods that are virtual are virtual for mocking.