Commit 360e2c00 authored by mmaliszkiewicz's avatar mmaliszkiewicz Committed by Commit bot

BufferedDataSource: don't reallocate buffer unnecessarily

When buffer size is different than kInitialReadBufferSize then
intermediate buffer would be reallocated on each Read() operation
which is unnecessary.

Review URL: https://codereview.chromium.org/594733004

Cr-Commit-Position: refs/heads/master@{#296469}
parent 73b5ddeb
...@@ -90,8 +90,7 @@ BufferedDataSource::BufferedDataSource( ...@@ -90,8 +90,7 @@ BufferedDataSource::BufferedDataSource(
total_bytes_(kPositionNotSpecified), total_bytes_(kPositionNotSpecified),
streaming_(false), streaming_(false),
frame_(frame), frame_(frame),
intermediate_read_buffer_(new uint8[kInitialReadBufferSize]), intermediate_read_buffer_(kInitialReadBufferSize),
intermediate_read_buffer_size_(kInitialReadBufferSize),
render_task_runner_(task_runner), render_task_runner_(task_runner),
stop_signal_received_(false), stop_signal_received_(false),
media_has_played_(false), media_has_played_(false),
...@@ -314,14 +313,14 @@ void BufferedDataSource::ReadInternal() { ...@@ -314,14 +313,14 @@ void BufferedDataSource::ReadInternal() {
// First we prepare the intermediate read buffer for BufferedResourceLoader // First we prepare the intermediate read buffer for BufferedResourceLoader
// to write to. // to write to.
if (size > intermediate_read_buffer_size_) { if (static_cast<int>(intermediate_read_buffer_.size()) < size)
intermediate_read_buffer_.reset(new uint8[size]); intermediate_read_buffer_.resize(size);
}
// Perform the actual read with BufferedResourceLoader. // Perform the actual read with BufferedResourceLoader.
DCHECK(!intermediate_read_buffer_.empty());
loader_->Read(position, loader_->Read(position,
size, size,
intermediate_read_buffer_.get(), &intermediate_read_buffer_[0],
base::Bind(&BufferedDataSource::ReadCallback, base::Bind(&BufferedDataSource::ReadCallback,
weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
} }
...@@ -448,7 +447,8 @@ void BufferedDataSource::ReadCallback( ...@@ -448,7 +447,8 @@ void BufferedDataSource::ReadCallback(
} }
if (bytes_read > 0) { if (bytes_read > 0) {
memcpy(read_op_->data(), intermediate_read_buffer_.get(), bytes_read); DCHECK(!intermediate_read_buffer_.empty());
memcpy(read_op_->data(), &intermediate_read_buffer_[0], bytes_read);
} else if (bytes_read == 0 && total_bytes_ == kPositionNotSpecified) { } else if (bytes_read == 0 && total_bytes_ == kPositionNotSpecified) {
// We've reached the end of the file and we didn't know the total size // We've reached the end of the file and we didn't know the total size
// before. Update the total size so Read()s past the end of the file will // before. Update the total size so Read()s past the end of the file will
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_ #define MEDIA_BLINK_BUFFERED_DATA_SOURCE_H_
#include <string> #include <string>
#include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
...@@ -196,8 +197,7 @@ class MEDIA_EXPORT BufferedDataSource : public DataSource { ...@@ -196,8 +197,7 @@ class MEDIA_EXPORT BufferedDataSource : public DataSource {
// because we want buffer to be passed into BufferedResourceLoader to be // because we want buffer to be passed into BufferedResourceLoader to be
// always non-null. And by initializing this member with a default size we can // always non-null. And by initializing this member with a default size we can
// avoid creating zero-sized buffered if the first read has zero size. // avoid creating zero-sized buffered if the first read has zero size.
scoped_ptr<uint8[]> intermediate_read_buffer_; std::vector<uint8> intermediate_read_buffer_;
int intermediate_read_buffer_size_;
// The task runner of the render thread. // The task runner of the render thread.
const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_; const scoped_refptr<base::SingleThreadTaskRunner> render_task_runner_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment