Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine Demuxer GC for low resolution videos #4483

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions media/base/decoder_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,28 @@ void DecoderBuffer::CopySideDataFrom(const uint8_t* side_data,
}
}

size_t DecoderBuffer::GetMemoryUsage() const {
size_t memory_usage = sizeof(DecoderBuffer);

if (end_of_stream()) {
return memory_usage;
}

memory_usage += data_size();

// Side data and decrypt config would not change after construction.
if (side_data_size_ > 0) {
memory_usage += side_data_size_;
}
if (decrypt_config_) {
memory_usage += sizeof(DecryptConfig);
memory_usage += decrypt_config_->key_id().capacity();
memory_usage += decrypt_config_->iv().capacity();
memory_usage +=
sizeof(SubsampleEntry) * decrypt_config_->subsamples().capacity();
}

return memory_usage;
}

} // namespace media
4 changes: 4 additions & 0 deletions media/base/decoder_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ class MEDIA_EXPORT DecoderBuffer
// Replaces any existing side data with data copied from |side_data|.
void CopySideDataFrom(const uint8_t* side_data, size_t side_data_size);

// Returns total memory usage for both bookkeeping and buffered data. The
// function is added for more accurately memory management.
virtual size_t GetMemoryUsage() const;

protected:
friend class base::RefCountedThreadSafe<DecoderBuffer>;

Expand Down
12 changes: 6 additions & 6 deletions media/base/decoder_buffer_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace media {

DecoderBufferQueue::DecoderBufferQueue()
: earliest_valid_timestamp_(kNoTimestamp), data_size_(0) {}
: earliest_valid_timestamp_(kNoTimestamp) {}

DecoderBufferQueue::~DecoderBufferQueue() = default;

Expand All @@ -21,7 +21,7 @@ void DecoderBufferQueue::Push(scoped_refptr<DecoderBuffer> buffer) {
DCHECK(!buffer->end_of_stream());

queue_.push_back(buffer);
data_size_ += buffer->data_size();
memory_usage_in_bytes_ += buffer->GetMemoryUsage();

// TODO(scherkus): FFmpeg returns some packets with no timestamp after
// seeking. Fix and turn this into CHECK(). See http://crbug.com/162192
Expand Down Expand Up @@ -49,9 +49,9 @@ scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() {
scoped_refptr<DecoderBuffer> buffer = std::move(queue_.front());
queue_.pop_front();

size_t buffer_data_size = buffer->data_size();
DCHECK_LE(buffer_data_size, data_size_);
data_size_ -= buffer_data_size;
size_t buffer_data_size = buffer->GetMemoryUsage();
DCHECK_LE(buffer_data_size, memory_usage_in_bytes_);
memory_usage_in_bytes_ -= buffer_data_size;

if (!in_order_queue_.empty() && in_order_queue_.front() == buffer)
in_order_queue_.pop_front();
Expand All @@ -61,7 +61,7 @@ scoped_refptr<DecoderBuffer> DecoderBufferQueue::Pop() {

void DecoderBufferQueue::Clear() {
queue_.clear();
data_size_ = 0;
memory_usage_in_bytes_ = 0;
in_order_queue_.clear();
earliest_valid_timestamp_ = kNoTimestamp;
}
Expand Down
11 changes: 7 additions & 4 deletions media/base/decoder_buffer_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ class MEDIA_EXPORT DecoderBufferQueue {
// Returns zero if the queue is empty.
base::TimeDelta Duration();

// Returns the total size of buffers inside the queue.
size_t data_size() const { return data_size_; }
// Returns the total memory occupied by this class and the buffers it holds,
// including the bookkeeping data and buffered data. For simplicity, the
// bookkeeping data of this class itself isn't included as it's relatively
// small compared to the other data.
size_t memory_usage_in_bytes() const { return memory_usage_in_bytes_; }

// Returns the number of buffers in the queue.
size_t queue_size() const { return queue_.size(); }
Expand All @@ -73,8 +76,8 @@ class MEDIA_EXPORT DecoderBufferQueue {

base::TimeDelta earliest_valid_timestamp_;

// Total size in bytes of buffers in the queue.
size_t data_size_;
// Total memory usage in bytes for buffers in the queue.
size_t memory_usage_in_bytes_ = 0;
};

} // namespace media
Expand Down
34 changes: 22 additions & 12 deletions media/base/decoder_buffer_queue_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ static scoped_refptr<DecoderBuffer> CreateBuffer(int timestamp, int size) {
return buffer;
}

static size_t GetExpectedMemoryUsage(int number_of_buffers, int data_size) {
return number_of_buffers * sizeof(DecoderBuffer) + data_size;
}

TEST(DecoderBufferQueueTest, IsEmpty) {
DecoderBufferQueue queue;
EXPECT_TRUE(queue.IsEmpty());
Expand Down Expand Up @@ -142,28 +146,34 @@ TEST(DecoderBufferQueueTest, Duration_NoTimestamp) {
EXPECT_EQ(0, queue.Duration().InSeconds());
}

TEST(DecoderBufferQueueTest, DataSize) {
TEST(DecoderBufferQueueTest, MemoryUsage) {
DecoderBufferQueue queue;
EXPECT_EQ(queue.data_size(), 0u);
EXPECT_EQ(queue.memory_usage_in_bytes(), 0u);

queue.Push(CreateBuffer(0, 0u));
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(1, 0));

queue.Pop();
EXPECT_EQ(queue.memory_usage_in_bytes(), 0u);

queue.Push(CreateBuffer(0, 1200u));
EXPECT_EQ(queue.data_size(), 1200u);
queue.Push(CreateBuffer(1, 1200u));
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(1, 1200));

queue.Push(CreateBuffer(1, 1000u));
EXPECT_EQ(queue.data_size(), 2200u);
queue.Push(CreateBuffer(2, 1000u));
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(2, 2200));

queue.Pop();
EXPECT_EQ(queue.data_size(), 1000u);
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(1, 1000));

queue.Push(CreateBuffer(2, 999u));
queue.Push(CreateBuffer(3, 999u));
EXPECT_EQ(queue.data_size(), 2998u);
queue.Push(CreateBuffer(4, 999u));
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(3, 2998));

queue.Clear();
EXPECT_EQ(queue.data_size(), 0u);
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(0, 0));

queue.Push(CreateBuffer(4, 1400u));
EXPECT_EQ(queue.data_size(), 1400u);
queue.Push(CreateBuffer(5, 1400u));
EXPECT_EQ(queue.memory_usage_in_bytes(), GetExpectedMemoryUsage(1, 1400));
}

} // namespace media
11 changes: 11 additions & 0 deletions media/base/stream_parser_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,15 @@ void StreamParserBuffer::set_timestamp(base::TimeDelta timestamp) {
preroll_buffer_->set_timestamp(timestamp);
}

size_t StreamParserBuffer::GetMemoryUsage() const {
size_t memory_usage = DecoderBuffer::GetMemoryUsage() -
sizeof(DecoderBuffer) + sizeof(StreamParserBuffer);

if (preroll_buffer_) {
memory_usage += preroll_buffer_->GetMemoryUsage();
}

return memory_usage;
}

} // namespace media
5 changes: 5 additions & 0 deletions media/base/stream_parser_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ class MEDIA_EXPORT StreamParserBuffer : public DecoderBuffer {
//
// All future timestamp, decode timestamp, config id, or track id changes to
// this buffer will be applied to the preroll buffer as well.
//
// TODO(b/331652782): integrate the setter function into the constructor to
// make |preroll_buffer_| immutable.
void SetPrerollBuffer(scoped_refptr<StreamParserBuffer> preroll);
scoped_refptr<StreamParserBuffer> preroll_buffer() { return preroll_buffer_; }

Expand All @@ -193,6 +196,8 @@ class MEDIA_EXPORT StreamParserBuffer : public DecoderBuffer {
is_duration_estimated_ = is_estimated;
}

size_t GetMemoryUsage() const override;

private:
StreamParserBuffer(std::unique_ptr<ExternalMemory> external_memory,
bool is_key_frame,
Expand Down
8 changes: 4 additions & 4 deletions media/filters/chunk_demuxer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ base::TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
return stream_->GetBufferedDuration();
}

size_t ChunkDemuxerStream::GetBufferedSize() const {
size_t ChunkDemuxerStream::GetMemoryUsage() const {
base::AutoLock auto_lock(lock_);
return stream_->GetBufferedSize();
return stream_->GetMemoryUsage();
}

void ChunkDemuxerStream::OnStartOfCodedFrameGroup(DecodeTimestamp start_dts,
Expand Down Expand Up @@ -681,9 +681,9 @@ int64_t ChunkDemuxer::GetMemoryUsage() const {
base::AutoLock auto_lock(lock_);
int64_t mem = 0;
for (const auto& s : audio_streams_)
mem += s->GetBufferedSize();
mem += s->GetMemoryUsage();
for (const auto& s : video_streams_)
mem += s->GetBufferedSize();
mem += s->GetMemoryUsage();
return mem;
}

Expand Down
4 changes: 2 additions & 2 deletions media/filters/chunk_demuxer.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class MEDIA_EXPORT ChunkDemuxerStream : public DemuxerStream {
// Returns base::TimeDelta() if the stream has no buffered data.
base::TimeDelta GetBufferedDuration() const;

// Returns the size of the buffered data in bytes.
size_t GetBufferedSize() const;
// Returns the memory usage of the buffered data in bytes.
size_t GetMemoryUsage() const;

// Signal to the stream that buffers handed in through subsequent calls to
// Append() belong to a coded frame group that starts at |start_pts|.
Expand Down
Loading
Loading