2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/media_sample.h"
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-06-20 21:45:09 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 04:17:12 +00:00
|
|
|
namespace media {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
MediaSample::MediaSample(const uint8_t* data,
|
2017-09-18 23:31:00 +00:00
|
|
|
size_t data_size,
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* side_data,
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t side_data_size,
|
2013-09-24 04:17:12 +00:00
|
|
|
bool is_key_frame)
|
2017-09-18 23:31:00 +00:00
|
|
|
: is_key_frame_(is_key_frame) {
|
2013-09-24 04:17:12 +00:00
|
|
|
if (!data) {
|
2017-09-18 23:31:00 +00:00
|
|
|
CHECK_EQ(data_size, 0u);
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
SetData(data, data_size);
|
|
|
|
if (side_data) {
|
|
|
|
std::shared_ptr<uint8_t> shared_side_data(new uint8_t[side_data_size],
|
|
|
|
std::default_delete<uint8_t[]>());
|
|
|
|
memcpy(shared_side_data.get(), side_data, side_data_size);
|
|
|
|
side_data_ = std::move(shared_side_data);
|
|
|
|
side_data_size_ = side_data_size;
|
|
|
|
}
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
MediaSample::MediaSample() {}
|
2014-09-16 19:55:49 +00:00
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
MediaSample::~MediaSample() {}
|
|
|
|
|
|
|
|
// static
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
bool is_key_frame) {
|
2013-09-24 04:17:12 +00:00
|
|
|
// If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
|
|
|
|
CHECK(data);
|
2017-09-18 23:31:00 +00:00
|
|
|
return std::shared_ptr<MediaSample>(
|
|
|
|
new MediaSample(data, data_size, nullptr, 0u, is_key_frame));
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> MediaSample::CopyFrom(const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
const uint8_t* side_data,
|
|
|
|
size_t side_data_size,
|
|
|
|
bool is_key_frame) {
|
2013-09-24 04:17:12 +00:00
|
|
|
// If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
|
|
|
|
CHECK(data);
|
2017-09-18 23:31:00 +00:00
|
|
|
return std::shared_ptr<MediaSample>(new MediaSample(
|
|
|
|
data, data_size, side_data, side_data_size, is_key_frame));
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 17:22:25 +00:00
|
|
|
// static
|
|
|
|
std::shared_ptr<MediaSample> MediaSample::CopyFrom(
|
|
|
|
const MediaSample& media_sample) {
|
2017-09-18 23:31:00 +00:00
|
|
|
std::shared_ptr<MediaSample> new_media_sample(new MediaSample);
|
|
|
|
new_media_sample->dts_ = media_sample.dts_;
|
|
|
|
new_media_sample->pts_ = media_sample.pts_;
|
|
|
|
new_media_sample->duration_ = media_sample.duration_;
|
|
|
|
new_media_sample->is_key_frame_ = media_sample.is_key_frame_;
|
|
|
|
new_media_sample->is_encrypted_ = media_sample.is_encrypted_;
|
|
|
|
new_media_sample->data_ = media_sample.data_;
|
|
|
|
new_media_sample->data_size_ = media_sample.data_size_;
|
|
|
|
new_media_sample->side_data_ = media_sample.side_data_;
|
|
|
|
new_media_sample->side_data_size_ = media_sample.side_data_size_;
|
|
|
|
new_media_sample->config_id_ = media_sample.config_id_;
|
|
|
|
if (media_sample.decrypt_config_) {
|
|
|
|
new_media_sample->decrypt_config_.reset(
|
|
|
|
new DecryptConfig(media_sample.decrypt_config_->key_id(),
|
|
|
|
media_sample.decrypt_config_->iv(),
|
|
|
|
media_sample.decrypt_config_->subsamples(),
|
|
|
|
media_sample.decrypt_config_->protection_scheme(),
|
|
|
|
media_sample.decrypt_config_->crypt_byte_block(),
|
|
|
|
media_sample.decrypt_config_->skip_byte_block()));
|
2017-02-27 17:22:25 +00:00
|
|
|
}
|
|
|
|
return new_media_sample;
|
|
|
|
}
|
|
|
|
|
2015-11-19 23:58:29 +00:00
|
|
|
// static
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> MediaSample::FromMetadata(const uint8_t* metadata,
|
|
|
|
size_t metadata_size) {
|
2017-09-18 23:31:00 +00:00
|
|
|
return std::shared_ptr<MediaSample>(
|
|
|
|
new MediaSample(nullptr, 0, metadata, metadata_size, false));
|
2015-11-19 23:58:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-14 21:35:57 +00:00
|
|
|
// static
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> MediaSample::CreateEmptyMediaSample() {
|
2017-09-18 23:31:00 +00:00
|
|
|
return std::shared_ptr<MediaSample>(new MediaSample);
|
2014-07-14 21:35:57 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
// static
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> MediaSample::CreateEOSBuffer() {
|
2017-09-18 23:31:00 +00:00
|
|
|
return std::shared_ptr<MediaSample>(
|
|
|
|
new MediaSample(nullptr, 0, nullptr, 0, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaSample::TransferData(std::shared_ptr<uint8_t> data,
|
|
|
|
size_t data_size) {
|
|
|
|
data_ = std::move(data);
|
|
|
|
data_size_ = data_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaSample::SetData(const uint8_t* data, size_t data_size) {
|
|
|
|
std::shared_ptr<uint8_t> shared_data(new uint8_t[data_size],
|
|
|
|
std::default_delete<uint8_t[]>());
|
|
|
|
memcpy(shared_data.get(), data, data_size);
|
|
|
|
TransferData(std::move(shared_data), data_size);
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
std::string MediaSample::ToString() const {
|
2014-03-31 22:25:45 +00:00
|
|
|
if (end_of_stream())
|
|
|
|
return "End of stream sample\n";
|
|
|
|
return base::StringPrintf(
|
2017-09-18 23:31:00 +00:00
|
|
|
"dts: %" PRId64 "\n pts: %" PRId64 "\n duration: %" PRId64
|
|
|
|
"\n "
|
2014-08-25 22:51:19 +00:00
|
|
|
"is_key_frame: %s\n size: %zu\n side_data_size: %zu\n",
|
2017-09-18 23:31:00 +00:00
|
|
|
dts_, pts_, duration_, is_key_frame_ ? "true" : "false", data_size_,
|
|
|
|
side_data_size_);
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|