2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
#include <deque>
|
2017-01-24 00:55:02 +00:00
|
|
|
#include <memory>
|
2013-09-24 04:17:12 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/decrypt_config.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-01-24 18:46:46 +00:00
|
|
|
/// Class to hold a media sample.
|
2017-01-24 00:55:02 +00:00
|
|
|
class MediaSample {
|
2013-09-24 04:17:12 +00:00
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample object from input.
|
|
|
|
/// @param data points to the buffer containing the sample data.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param size indicates sample size in bytes. Must not be negative.
|
|
|
|
/// @param is_key_frame indicates whether the sample is a key frame.
|
2017-01-24 00:55:02 +00:00
|
|
|
static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
|
|
|
|
size_t size,
|
|
|
|
bool is_key_frame);
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample object from input.
|
|
|
|
/// @param data points to the buffer containing the sample data.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param side_data points to the buffer containing the additional data.
|
|
|
|
/// Some containers allow additional data to be specified.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param size indicates sample size in bytes. Must not be negative.
|
|
|
|
/// @param side_data_size indicates additional sample data size in bytes.
|
|
|
|
/// @param is_key_frame indicates whether the sample is a key frame.
|
2017-01-24 00:55:02 +00:00
|
|
|
static std::shared_ptr<MediaSample> CopyFrom(const uint8_t* data,
|
|
|
|
size_t size,
|
|
|
|
const uint8_t* side_data,
|
|
|
|
size_t side_data_size,
|
|
|
|
bool is_key_frame);
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2015-11-19 23:58:29 +00:00
|
|
|
/// Create a MediaSample object from metadata.
|
|
|
|
/// Unlike other factory methods, this cannot be a key frame. It must be only
|
|
|
|
/// for metadata.
|
|
|
|
/// @param metadata points to the buffer containing metadata.
|
|
|
|
/// Must not be NULL.
|
|
|
|
/// @param metadata_size is the size of metadata in bytes.
|
2017-01-24 00:55:02 +00:00
|
|
|
static std::shared_ptr<MediaSample> FromMetadata(const uint8_t* metadata,
|
|
|
|
size_t metadata_size);
|
2015-11-19 23:58:29 +00:00
|
|
|
|
2014-07-14 21:35:57 +00:00
|
|
|
/// Create a MediaSample object with default members.
|
2017-01-24 00:55:02 +00:00
|
|
|
static std::shared_ptr<MediaSample> CreateEmptyMediaSample();
|
2014-07-14 21:35:57 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a MediaSample indicating we've reached end of stream.
|
|
|
|
/// Calling any method other than end_of_stream() on the resulting buffer
|
|
|
|
/// is disallowed.
|
2017-01-24 00:55:02 +00:00
|
|
|
static std::shared_ptr<MediaSample> CreateEOSBuffer();
|
|
|
|
|
|
|
|
virtual ~MediaSample();
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2017-09-25 22:22:18 +00:00
|
|
|
/// Clone the object and return a new MediaSample.
|
|
|
|
std::shared_ptr<MediaSample> Clone() const;
|
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
/// Transfer data to this media sample. No data copying is involved.
|
|
|
|
/// @param data points to the data to be transferred.
|
|
|
|
/// @param data_size is the size of the data to be transferred.
|
|
|
|
void TransferData(std::shared_ptr<uint8_t> data, size_t data_size);
|
|
|
|
|
|
|
|
/// Set the data in this media sample. Note that this method involves data
|
|
|
|
/// copying.
|
|
|
|
/// @param data points to the data to be copied.
|
|
|
|
/// @param data_size is the size of the data to be copied.
|
|
|
|
void SetData(const uint8_t* data, size_t data_size);
|
|
|
|
|
|
|
|
/// @return a human-readable string describing |*this|.
|
|
|
|
std::string ToString() const;
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t dts() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return dts_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_dts(int64_t dts) { dts_ = dts; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t pts() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return pts_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_pts(int64_t pts) { pts_ = pts; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t duration() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return duration_;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void set_duration(int64_t duration) {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
duration_ = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_key_frame() const {
|
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return is_key_frame_;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:17:10 +00:00
|
|
|
bool is_encrypted() const {
|
|
|
|
DCHECK(!end_of_stream());
|
|
|
|
return is_encrypted_;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
2017-09-18 23:31:00 +00:00
|
|
|
return data_.get();
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 22:25:45 +00:00
|
|
|
size_t data_size() const {
|
2013-09-24 04:17:12 +00:00
|
|
|
DCHECK(!end_of_stream());
|
2017-09-18 23:31:00 +00:00
|
|
|
return data_size_;
|
2013-09-24 04:17:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
const uint8_t* side_data() const { return side_data_.get(); }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
size_t side_data_size() const { return side_data_size_; }
|
2016-08-17 00:57:34 +00:00
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
const DecryptConfig* decrypt_config() const { return decrypt_config_.get(); }
|
2015-11-18 21:11:31 +00:00
|
|
|
|
2014-07-14 21:35:57 +00:00
|
|
|
void set_is_key_frame(bool value) {
|
|
|
|
is_key_frame_ = value;
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:17:10 +00:00
|
|
|
void set_is_encrypted(bool value) {
|
|
|
|
is_encrypted_ = value;
|
|
|
|
}
|
|
|
|
|
2016-08-17 00:57:34 +00:00
|
|
|
void set_decrypt_config(std::unique_ptr<DecryptConfig> decrypt_config) {
|
|
|
|
decrypt_config_ = std::move(decrypt_config);
|
|
|
|
}
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
// If there's no data in this buffer, it represents end of stream.
|
2017-09-18 23:31:00 +00:00
|
|
|
bool end_of_stream() const { return data_size_ == 0; }
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2015-11-19 23:58:29 +00:00
|
|
|
const std::string& config_id() const { return config_id_; }
|
|
|
|
void set_config_id(const std::string& config_id) {
|
|
|
|
config_id_ = config_id;
|
|
|
|
}
|
|
|
|
|
2017-09-18 23:31:00 +00:00
|
|
|
protected:
|
|
|
|
// Made it protected to disallow the constructor to be called directly.
|
|
|
|
// Create a MediaSample. Buffer will be padded and aligned as necessary.
|
|
|
|
// |data|,|side_data| can be nullptr, which indicates an empty sample.
|
|
|
|
MediaSample(const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
const uint8_t* side_data,
|
|
|
|
size_t side_data_size,
|
|
|
|
bool is_key_frame);
|
|
|
|
MediaSample();
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2014-04-24 18:37:33 +00:00
|
|
|
private:
|
2013-09-24 04:17:12 +00:00
|
|
|
// Decoding time stamp.
|
2017-09-18 23:31:00 +00:00
|
|
|
int64_t dts_ = 0;
|
2013-09-24 04:17:12 +00:00
|
|
|
// Presentation time stamp.
|
2017-09-18 23:31:00 +00:00
|
|
|
int64_t pts_ = 0;
|
|
|
|
int64_t duration_ = 0;
|
|
|
|
bool is_key_frame_ = false;
|
2015-01-12 21:17:10 +00:00
|
|
|
// is sample encrypted ?
|
2017-09-18 23:31:00 +00:00
|
|
|
bool is_encrypted_ = false;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
// Main buffer data.
|
2017-09-18 23:31:00 +00:00
|
|
|
std::shared_ptr<const uint8_t> data_;
|
|
|
|
size_t data_size_ = 0;
|
2013-09-24 04:17:12 +00:00
|
|
|
// Contain additional buffers to complete the main one. Needed by WebM
|
|
|
|
// http://www.matroska.org/technical/specs/index.html BlockAdditional[A5].
|
|
|
|
// Not used by mp4 and other containers.
|
2017-09-18 23:31:00 +00:00
|
|
|
std::shared_ptr<const uint8_t> side_data_;
|
|
|
|
size_t side_data_size_ = 0;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2015-11-19 23:58:29 +00:00
|
|
|
// Text specific fields.
|
|
|
|
// For now this is the cue identifier for WebVTT.
|
|
|
|
std::string config_id_;
|
|
|
|
|
2016-08-17 00:57:34 +00:00
|
|
|
// Decrypt configuration.
|
|
|
|
std::unique_ptr<DecryptConfig> decrypt_config_;
|
|
|
|
|
2013-09-24 04:17:12 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(MediaSample);
|
|
|
|
};
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
typedef std::deque<std::shared_ptr<MediaSample>> BufferQueue;
|
2013-09-24 04:17:12 +00:00
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-09-24 04:17:12 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_MEDIA_SAMPLE_H_
|