This source file includes following definitions.
- retries
- IncrementRetries
- position
- size
- data
- callback_
- Run
- weak_factory_
- CreateResourceLoader
- Initialize
- SetPreload
- HasSingleOrigin
- DidPassCORSAccessCheck
- Abort
- MediaPlaybackRateChanged
- MediaIsPlaying
- MediaIsPaused
- Stop
- SetBitrate
- Read
- GetSize
- IsStreaming
- ReadTask
- StopInternal_Locked
- StopLoader
- SetBitrateTask
- ReadInternal
- StartCallback
- PartialReadStartCallback
- ReadCallback
- LoadingStateChangedCallback
- ProgressCallback
- UpdateDeferStrategy
#include "content/renderer/media/buffered_data_source.h"
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/message_loop/message_loop_proxy.h"
#include "media/base/media_log.h"
#include "net/base/net_errors.h"
using blink::WebFrame;
namespace {
const int kInitialReadBufferSize = 32768;
const int kNumCacheMissRetries = 3;
}
namespace content {
class BufferedDataSource::ReadOperation {
public:
ReadOperation(int64 position, int size, uint8* data,
const media::DataSource::ReadCB& callback);
~ReadOperation();
static void Run(scoped_ptr<ReadOperation> read_op, int result);
int retries() { return retries_; }
void IncrementRetries() { ++retries_; }
int64 position() { return position_; }
int size() { return size_; }
uint8* data() { return data_; }
private:
int retries_;
const int64 position_;
const int size_;
uint8* data_;
media::DataSource::ReadCB callback_;
DISALLOW_IMPLICIT_CONSTRUCTORS(ReadOperation);
};
BufferedDataSource::ReadOperation::ReadOperation(
int64 position, int size, uint8* data,
const media::DataSource::ReadCB& callback)
: retries_(0),
position_(position),
size_(size),
data_(data),
callback_(callback) {
DCHECK(!callback_.is_null());
}
BufferedDataSource::ReadOperation::~ReadOperation() {
DCHECK(callback_.is_null());
}
void BufferedDataSource::ReadOperation::Run(
scoped_ptr<ReadOperation> read_op, int result) {
base::ResetAndReturn(&read_op->callback_).Run(result);
}
BufferedDataSource::BufferedDataSource(
const scoped_refptr<base::MessageLoopProxy>& render_loop,
WebFrame* frame,
media::MediaLog* media_log,
media::DataSourceHost* host,
const DownloadingCB& downloading_cb)
: cors_mode_(BufferedResourceLoader::kUnspecified),
total_bytes_(kPositionNotSpecified),
assume_fully_buffered_(false),
streaming_(false),
frame_(frame),
intermediate_read_buffer_(new uint8[kInitialReadBufferSize]),
intermediate_read_buffer_size_(kInitialReadBufferSize),
render_loop_(render_loop),
stop_signal_received_(false),
media_has_played_(false),
preload_(AUTO),
bitrate_(0),
playback_rate_(0.0),
media_log_(media_log),
host_(host),
downloading_cb_(downloading_cb),
weak_factory_(this) {
DCHECK(host_);
DCHECK(!downloading_cb_.is_null());
}
BufferedDataSource::~BufferedDataSource() {}
BufferedResourceLoader* BufferedDataSource::CreateResourceLoader(
int64 first_byte_position, int64 last_byte_position) {
DCHECK(render_loop_->BelongsToCurrentThread());
BufferedResourceLoader::DeferStrategy strategy = preload_ == METADATA ?
BufferedResourceLoader::kReadThenDefer :
BufferedResourceLoader::kCapacityDefer;
return new BufferedResourceLoader(url_,
cors_mode_,
first_byte_position,
last_byte_position,
strategy,
bitrate_,
playback_rate_,
media_log_.get());
}
void BufferedDataSource::Initialize(
const GURL& url,
BufferedResourceLoader::CORSMode cors_mode,
const InitializeCB& init_cb) {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(!init_cb.is_null());
DCHECK(!loader_.get());
url_ = url;
cors_mode_ = cors_mode;
init_cb_ = init_cb;
if (url_.SchemeIs(kHttpScheme) || url_.SchemeIs(kHttpsScheme)) {
loader_.reset(CreateResourceLoader(0, kPositionNotSpecified));
} else {
loader_.reset(CreateResourceLoader(kPositionNotSpecified,
kPositionNotSpecified));
assume_fully_buffered_ = true;
}
base::WeakPtr<BufferedDataSource> weak_this = weak_factory_.GetWeakPtr();
loader_->Start(
base::Bind(&BufferedDataSource::StartCallback, weak_this),
base::Bind(&BufferedDataSource::LoadingStateChangedCallback, weak_this),
base::Bind(&BufferedDataSource::ProgressCallback, weak_this),
frame_);
}
void BufferedDataSource::SetPreload(Preload preload) {
DCHECK(render_loop_->BelongsToCurrentThread());
preload_ = preload;
}
bool BufferedDataSource::HasSingleOrigin() {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(init_cb_.is_null() && loader_.get())
<< "Initialize() must complete before calling HasSingleOrigin()";
return loader_->HasSingleOrigin();
}
bool BufferedDataSource::DidPassCORSAccessCheck() const {
return loader_.get() && loader_->DidPassCORSAccessCheck();
}
void BufferedDataSource::Abort() {
DCHECK(render_loop_->BelongsToCurrentThread());
{
base::AutoLock auto_lock(lock_);
StopInternal_Locked();
}
StopLoader();
frame_ = NULL;
}
void BufferedDataSource::MediaPlaybackRateChanged(float playback_rate) {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(loader_.get());
if (playback_rate < 0.0f)
return;
playback_rate_ = playback_rate;
loader_->SetPlaybackRate(playback_rate);
}
void BufferedDataSource::MediaIsPlaying() {
DCHECK(render_loop_->BelongsToCurrentThread());
media_has_played_ = true;
UpdateDeferStrategy(false);
}
void BufferedDataSource::MediaIsPaused() {
DCHECK(render_loop_->BelongsToCurrentThread());
UpdateDeferStrategy(true);
}
void BufferedDataSource::Stop(const base::Closure& closure) {
{
base::AutoLock auto_lock(lock_);
StopInternal_Locked();
}
closure.Run();
render_loop_->PostTask(
FROM_HERE,
base::Bind(&BufferedDataSource::StopLoader, weak_factory_.GetWeakPtr()));
}
void BufferedDataSource::SetBitrate(int bitrate) {
render_loop_->PostTask(FROM_HERE,
base::Bind(&BufferedDataSource::SetBitrateTask,
weak_factory_.GetWeakPtr(),
bitrate));
}
void BufferedDataSource::Read(
int64 position, int size, uint8* data,
const media::DataSource::ReadCB& read_cb) {
DVLOG(1) << "Read: " << position << " offset, " << size << " bytes";
DCHECK(!read_cb.is_null());
{
base::AutoLock auto_lock(lock_);
DCHECK(!read_op_);
if (stop_signal_received_) {
read_cb.Run(kReadError);
return;
}
read_op_.reset(new ReadOperation(position, size, data, read_cb));
}
render_loop_->PostTask(
FROM_HERE,
base::Bind(&BufferedDataSource::ReadTask, weak_factory_.GetWeakPtr()));
}
bool BufferedDataSource::GetSize(int64* size_out) {
if (total_bytes_ != kPositionNotSpecified) {
*size_out = total_bytes_;
return true;
}
*size_out = 0;
return false;
}
bool BufferedDataSource::IsStreaming() {
return streaming_;
}
void BufferedDataSource::ReadTask() {
DCHECK(render_loop_->BelongsToCurrentThread());
ReadInternal();
}
void BufferedDataSource::StopInternal_Locked() {
lock_.AssertAcquired();
if (stop_signal_received_)
return;
stop_signal_received_ = true;
init_cb_.Reset();
if (read_op_)
ReadOperation::Run(read_op_.Pass(), kReadError);
}
void BufferedDataSource::StopLoader() {
DCHECK(render_loop_->BelongsToCurrentThread());
if (loader_)
loader_->Stop();
}
void BufferedDataSource::SetBitrateTask(int bitrate) {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(loader_.get());
bitrate_ = bitrate;
loader_->SetBitrate(bitrate);
}
void BufferedDataSource::ReadInternal() {
DCHECK(render_loop_->BelongsToCurrentThread());
int64 position = 0;
int size = 0;
{
base::AutoLock auto_lock(lock_);
if (stop_signal_received_)
return;
position = read_op_->position();
size = read_op_->size();
}
if (size > intermediate_read_buffer_size_) {
intermediate_read_buffer_.reset(new uint8[size]);
}
loader_->Read(position,
size,
intermediate_read_buffer_.get(),
base::Bind(&BufferedDataSource::ReadCallback,
weak_factory_.GetWeakPtr()));
}
void BufferedDataSource::StartCallback(
BufferedResourceLoader::Status status) {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(loader_.get());
bool init_cb_is_null = false;
{
base::AutoLock auto_lock(lock_);
init_cb_is_null = init_cb_.is_null();
}
if (init_cb_is_null) {
loader_->Stop();
return;
}
bool success = status == BufferedResourceLoader::kOk &&
(!assume_fully_buffered_ ||
loader_->instance_size() != kPositionNotSpecified);
if (success) {
total_bytes_ = loader_->instance_size();
streaming_ = !assume_fully_buffered_ &&
(total_bytes_ == kPositionNotSpecified || !loader_->range_supported());
media_log_->SetDoubleProperty("total_bytes",
static_cast<double>(total_bytes_));
media_log_->SetBooleanProperty("streaming", streaming_);
} else {
loader_->Stop();
}
base::AutoLock auto_lock(lock_);
if (stop_signal_received_)
return;
if (success) {
if (total_bytes_ != kPositionNotSpecified) {
host_->SetTotalBytes(total_bytes_);
if (assume_fully_buffered_)
host_->AddBufferedByteRange(0, total_bytes_);
}
media_log_->SetBooleanProperty("single_origin", loader_->HasSingleOrigin());
media_log_->SetBooleanProperty("passed_cors_access_check",
loader_->DidPassCORSAccessCheck());
media_log_->SetBooleanProperty("range_header_supported",
loader_->range_supported());
}
base::ResetAndReturn(&init_cb_).Run(success);
}
void BufferedDataSource::PartialReadStartCallback(
BufferedResourceLoader::Status status) {
DCHECK(render_loop_->BelongsToCurrentThread());
DCHECK(loader_.get());
if (status == BufferedResourceLoader::kOk) {
ReadInternal();
return;
}
loader_->Stop();
base::AutoLock auto_lock(lock_);
if (stop_signal_received_)
return;
ReadOperation::Run(read_op_.Pass(), kReadError);
}
void BufferedDataSource::ReadCallback(
BufferedResourceLoader::Status status,
int bytes_read) {
DCHECK(render_loop_->BelongsToCurrentThread());
base::AutoLock auto_lock(lock_);
if (stop_signal_received_)
return;
if (status != BufferedResourceLoader::kOk) {
loader_->Stop();
if (status == BufferedResourceLoader::kCacheMiss &&
read_op_->retries() < kNumCacheMissRetries) {
read_op_->IncrementRetries();
loader_.reset(CreateResourceLoader(
read_op_->position(), kPositionNotSpecified));
base::WeakPtr<BufferedDataSource> weak_this = weak_factory_.GetWeakPtr();
loader_->Start(
base::Bind(&BufferedDataSource::PartialReadStartCallback, weak_this),
base::Bind(&BufferedDataSource::LoadingStateChangedCallback,
weak_this),
base::Bind(&BufferedDataSource::ProgressCallback, weak_this),
frame_);
return;
}
ReadOperation::Run(read_op_.Pass(), kReadError);
return;
}
if (bytes_read > 0) {
memcpy(read_op_->data(), intermediate_read_buffer_.get(), bytes_read);
} else if (bytes_read == 0 && total_bytes_ == kPositionNotSpecified) {
total_bytes_ = loader_->instance_size();
if (total_bytes_ != kPositionNotSpecified) {
host_->SetTotalBytes(total_bytes_);
host_->AddBufferedByteRange(loader_->first_byte_position(),
total_bytes_);
}
}
ReadOperation::Run(read_op_.Pass(), bytes_read);
}
void BufferedDataSource::LoadingStateChangedCallback(
BufferedResourceLoader::LoadingState state) {
DCHECK(render_loop_->BelongsToCurrentThread());
if (assume_fully_buffered_)
return;
bool is_downloading_data;
switch (state) {
case BufferedResourceLoader::kLoading:
is_downloading_data = true;
break;
case BufferedResourceLoader::kLoadingDeferred:
case BufferedResourceLoader::kLoadingFinished:
is_downloading_data = false;
break;
case BufferedResourceLoader::kLoadingFailed:
return;
}
downloading_cb_.Run(is_downloading_data);
}
void BufferedDataSource::ProgressCallback(int64 position) {
DCHECK(render_loop_->BelongsToCurrentThread());
if (assume_fully_buffered_)
return;
base::AutoLock auto_lock(lock_);
if (stop_signal_received_)
return;
host_->AddBufferedByteRange(loader_->first_byte_position(), position);
}
void BufferedDataSource::UpdateDeferStrategy(bool paused) {
if (!loader_->range_supported()) {
loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer);
return;
}
if (media_has_played_ && paused) {
loader_->UpdateDeferStrategy(BufferedResourceLoader::kNeverDefer);
return;
}
loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer);
}
}