#ifndef MEDIA_BASE_STREAM_PARSER_BUFFER_H_
#define MEDIA_BASE_STREAM_PARSER_BUFFER_H_
#include <stddef.h>
#include <stdint.h>
#include "base/time/time.h"
#include "base/types/pass_key.h"
#include "media/base/decoder_buffer.h"
#include "media/base/demuxer_stream.h"
#include "media/base/media_export.h"
#include "media/base/stream_parser.h"
#include "media/base/timestamp_constants.h"
namespace media {
class DecodeTimestamp {
public:
constexpr DecodeTimestamp() = default;
constexpr bool operator<(const DecodeTimestamp& rhs) const {
return ts_ < rhs.ts_;
}
constexpr bool operator>(const DecodeTimestamp& rhs) const {
return ts_ > rhs.ts_;
}
constexpr bool operator==(const DecodeTimestamp& rhs) const {
return ts_ == rhs.ts_;
}
constexpr bool operator!=(const DecodeTimestamp& rhs) const {
return ts_ != rhs.ts_;
}
constexpr bool operator>=(const DecodeTimestamp& rhs) const {
return ts_ >= rhs.ts_;
}
constexpr bool operator<=(const DecodeTimestamp& rhs) const {
return ts_ <= rhs.ts_;
}
constexpr base::TimeDelta operator-(const DecodeTimestamp& rhs) const {
return ts_ - rhs.ts_;
}
constexpr DecodeTimestamp& operator+=(base::TimeDelta rhs) {
ts_ += rhs;
return *this;
}
constexpr DecodeTimestamp& operator-=(base::TimeDelta rhs) {
ts_ -= rhs;
return *this;
}
constexpr DecodeTimestamp operator+(base::TimeDelta rhs) const {
return DecodeTimestamp(ts_ + rhs);
}
constexpr DecodeTimestamp operator-(base::TimeDelta rhs) const {
return DecodeTimestamp(ts_ - rhs);
}
constexpr double operator/(base::TimeDelta rhs) const { return ts_ / rhs; }
constexpr int64_t IntDiv(base::TimeDelta rhs) const {
return ts_.IntDiv(rhs);
}
static constexpr DecodeTimestamp FromSecondsD(double seconds) {
return DecodeTimestamp(base::Seconds(seconds));
}
static constexpr DecodeTimestamp FromMilliseconds(int64_t milliseconds) {
return DecodeTimestamp(base::Milliseconds(milliseconds));
}
static constexpr DecodeTimestamp FromMicroseconds(int64_t microseconds) {
return DecodeTimestamp(base::Microseconds(microseconds));
}
static constexpr DecodeTimestamp FromPresentationTime(
base::TimeDelta timestamp) {
return DecodeTimestamp(timestamp);
}
constexpr double InSecondsF() const { return ts_.InSecondsF(); }
int64_t InMilliseconds() const { return ts_.InMilliseconds(); }
constexpr int64_t InMicroseconds() const { return ts_.InMicroseconds(); }
constexpr bool is_inf() const { return ts_.is_inf(); }
constexpr base::TimeDelta ToPresentationTime() const { return ts_; }
private:
constexpr explicit DecodeTimestamp(base::TimeDelta timestamp)
: ts_(timestamp) {}
base::TimeDelta ts_;
};
static_assert(kNoTimestamp.is_min() && kNoTimestamp.is_inf());
static_assert(kInfiniteDuration.is_max() && kInfiniteDuration.is_inf());
constexpr DecodeTimestamp kNoDecodeTimestamp =
DecodeTimestamp::FromPresentationTime(kNoTimestamp);
constexpr DecodeTimestamp kMaxDecodeTimestamp =
DecodeTimestamp::FromPresentationTime(kInfiniteDuration);
class MEDIA_EXPORT StreamParserBuffer : public DecoderBuffer {
public:
enum { kInvalidConfigId = -1 };
typedef DemuxerStream::Type Type;
typedef StreamParser::TrackId TrackId;
static scoped_refptr<StreamParserBuffer> CreateEOSBuffer(
std::optional<ConfigVariant> next_config = std::nullopt);
static scoped_refptr<StreamParserBuffer> CopyFrom(
base::span<const uint8_t> data,
bool is_key_frame,
Type type,
TrackId track_id);
static scoped_refptr<StreamParserBuffer> FromExternalMemory(
std::unique_ptr<ExternalMemory> external_memory,
bool is_key_frame,
Type type,
TrackId track_id);
static scoped_refptr<StreamParserBuffer> FromArray(
base::HeapArray<uint8_t> heap_array,
bool is_key_frame,
Type type,
TrackId track_id);
StreamParserBuffer(base::PassKey<StreamParserBuffer>,
base::HeapArray<uint8_t> heap_array,
bool is_key_frame,
Type type,
TrackId track_id);
StreamParserBuffer(base::PassKey<StreamParserBuffer>,
std::unique_ptr<ExternalMemory> external_memory,
bool is_key_frame,
Type type,
TrackId track_id);
StreamParserBuffer(base::PassKey<StreamParserBuffer>,
base::span<const uint8_t> data,
bool is_key_frame,
Type type,
TrackId track_id);
StreamParserBuffer(base::PassKey<StreamParserBuffer>,
DecoderBufferType decoder_buffer_type,
std::optional<ConfigVariant> next_config);
StreamParserBuffer(const StreamParserBuffer&) = delete;
StreamParserBuffer& operator=(const StreamParserBuffer&) = delete;
DecodeTimestamp GetDecodeTimestamp() const;
void SetDecodeTimestamp(DecodeTimestamp timestamp);
int GetConfigId() const;
void SetConfigId(int config_id);
Type type() const { return static_cast<Type>(type_); }
const char* GetTypeName() const;
TrackId track_id() const { return track_id_; }
void SetPrerollBuffer(scoped_refptr<StreamParserBuffer> preroll);
scoped_refptr<StreamParserBuffer> preroll_buffer() { return preroll_buffer_; }
void set_timestamp(base::TimeDelta timestamp) override;
bool is_duration_estimated() const { return is_duration_estimated_; }
void set_is_duration_estimated(bool is_estimated) {
is_duration_estimated_ = is_estimated;
}
size_t GetMemoryUsage() const override;
private:
~StreamParserBuffer() override;
const uint8_t type_ : 2;
bool is_duration_estimated_ : 1 = false;
DecodeTimestamp decode_timestamp_ = kNoDecodeTimestamp;
int config_id_ = kInvalidConfigId;
const TrackId track_id_;
scoped_refptr<StreamParserBuffer> preroll_buffer_;
};
}
#endif