7 #include "packager/media/base/media_sample.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/stringprintf.h"
17 MediaSample::MediaSample(
const uint8_t* data,
19 const uint8_t* side_data,
20 size_t side_data_size,
22 : is_key_frame_(is_key_frame) {
24 CHECK_EQ(data_size, 0u);
27 SetData(data, data_size);
29 std::shared_ptr<uint8_t> shared_side_data(
new uint8_t[side_data_size],
30 std::default_delete<uint8_t[]>());
31 memcpy(shared_side_data.get(), side_data, side_data_size);
32 side_data_ = std::move(shared_side_data);
33 side_data_size_ = side_data_size;
37 MediaSample::MediaSample() {}
39 MediaSample::~MediaSample() {}
42 std::shared_ptr<MediaSample> MediaSample::CopyFrom(
const uint8_t* data,
47 return std::shared_ptr<MediaSample>(
48 new MediaSample(data, data_size,
nullptr, 0u, is_key_frame));
52 std::shared_ptr<MediaSample> MediaSample::CopyFrom(
const uint8_t* data,
54 const uint8_t* side_data,
55 size_t side_data_size,
59 return std::shared_ptr<MediaSample>(
new MediaSample(
60 data, data_size, side_data, side_data_size, is_key_frame));
64 std::shared_ptr<MediaSample> MediaSample::FromMetadata(
const uint8_t* metadata,
65 size_t metadata_size) {
66 return std::shared_ptr<MediaSample>(
67 new MediaSample(
nullptr, 0, metadata, metadata_size,
false));
71 std::shared_ptr<MediaSample> MediaSample::CreateEmptyMediaSample() {
72 return std::shared_ptr<MediaSample>(
new MediaSample);
76 std::shared_ptr<MediaSample> MediaSample::CreateEOSBuffer() {
77 return std::shared_ptr<MediaSample>(
81 std::shared_ptr<MediaSample> MediaSample::Clone()
const {
82 std::shared_ptr<MediaSample> new_media_sample(
new MediaSample);
83 new_media_sample->dts_ = dts_;
84 new_media_sample->pts_ = pts_;
85 new_media_sample->duration_ = duration_;
86 new_media_sample->is_key_frame_ = is_key_frame_;
87 new_media_sample->is_encrypted_ = is_encrypted_;
88 new_media_sample->data_ = data_;
89 new_media_sample->data_size_ = data_size_;
90 new_media_sample->side_data_ = side_data_;
91 new_media_sample->side_data_size_ = side_data_size_;
92 new_media_sample->config_id_ = config_id_;
93 if (decrypt_config_) {
95 decrypt_config_->key_id(), decrypt_config_->iv(),
96 decrypt_config_->subsamples(), decrypt_config_->protection_scheme(),
97 decrypt_config_->crypt_byte_block(),
98 decrypt_config_->skip_byte_block()));
100 return new_media_sample;
103 void MediaSample::TransferData(std::shared_ptr<uint8_t> data,
105 data_ = std::move(data);
106 data_size_ = data_size;
109 void MediaSample::SetData(
const uint8_t* data,
size_t data_size) {
110 std::shared_ptr<uint8_t> shared_data(
new uint8_t[data_size],
111 std::default_delete<uint8_t[]>());
112 memcpy(shared_data.get(), data, data_size);
113 TransferData(std::move(shared_data), data_size);
116 std::string MediaSample::ToString()
const {
118 return "End of stream sample\n";
119 return base::StringPrintf(
120 "dts: %" PRId64
"\n pts: %" PRId64
"\n duration: %" PRId64
122 "is_key_frame: %s\n size: %zu\n side_data_size: %zu\n",
123 dts_, pts_, duration_, is_key_frame_ ?
"true" :
"false", data_size_,
All the methods that are virtual are virtual for mocking.