| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/buffered_data_source.h" | 5 #include "content/renderer/media/buffered_data_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "media/base/media_log.h" | 10 #include "media/base/media_log.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 render_loop_(render_loop), | 92 render_loop_(render_loop), |
| 93 stop_signal_received_(false), | 93 stop_signal_received_(false), |
| 94 media_has_played_(false), | 94 media_has_played_(false), |
| 95 preload_(AUTO), | 95 preload_(AUTO), |
| 96 bitrate_(0), | 96 bitrate_(0), |
| 97 playback_rate_(0.0), | 97 playback_rate_(0.0), |
| 98 media_log_(media_log), | 98 media_log_(media_log), |
| 99 downloading_cb_(downloading_cb), | 99 downloading_cb_(downloading_cb), |
| 100 weak_factory_(this) { | 100 weak_factory_(this) { |
| 101 DCHECK(!downloading_cb_.is_null()); | 101 DCHECK(!downloading_cb_.is_null()); |
| 102 weak_this_ = weak_factory_.GetWeakPtr(); | |
| 103 } | 102 } |
| 104 | 103 |
| 105 BufferedDataSource::~BufferedDataSource() {} | 104 BufferedDataSource::~BufferedDataSource() {} |
| 106 | 105 |
| 107 // A factory method to create BufferedResourceLoader using the read parameters. | 106 // A factory method to create BufferedResourceLoader using the read parameters. |
| 108 // This method can be overridden to inject mock BufferedResourceLoader object | 107 // This method can be overridden to inject mock BufferedResourceLoader object |
| 109 // for testing purpose. | 108 // for testing purpose. |
| 110 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( | 109 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( |
| 111 int64 first_byte_position, int64 last_byte_position) { | 110 int64 first_byte_position, int64 last_byte_position) { |
| 112 DCHECK(render_loop_->BelongsToCurrentThread()); | 111 DCHECK(render_loop_->BelongsToCurrentThread()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 loader_.reset(CreateResourceLoader(0, kPositionNotSpecified)); | 151 loader_.reset(CreateResourceLoader(0, kPositionNotSpecified)); |
| 153 } else { | 152 } else { |
| 154 // For all other protocols, assume they support range request. We fetch | 153 // For all other protocols, assume they support range request. We fetch |
| 155 // the full range of the resource to obtain the instance size because | 154 // the full range of the resource to obtain the instance size because |
| 156 // we won't be served HTTP headers. | 155 // we won't be served HTTP headers. |
| 157 loader_.reset(CreateResourceLoader(kPositionNotSpecified, | 156 loader_.reset(CreateResourceLoader(kPositionNotSpecified, |
| 158 kPositionNotSpecified)); | 157 kPositionNotSpecified)); |
| 159 assume_fully_buffered_ = true; | 158 assume_fully_buffered_ = true; |
| 160 } | 159 } |
| 161 | 160 |
| 161 base::WeakPtr<BufferedDataSource> weak_this = weak_factory_.GetWeakPtr(); |
| 162 loader_->Start( | 162 loader_->Start( |
| 163 base::Bind(&BufferedDataSource::StartCallback, weak_this_), | 163 base::Bind(&BufferedDataSource::StartCallback, weak_this), |
| 164 base::Bind(&BufferedDataSource::LoadingStateChangedCallback, weak_this_), | 164 base::Bind(&BufferedDataSource::LoadingStateChangedCallback, weak_this), |
| 165 base::Bind(&BufferedDataSource::ProgressCallback, weak_this_), | 165 base::Bind(&BufferedDataSource::ProgressCallback, weak_this), |
| 166 frame_); | 166 frame_); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void BufferedDataSource::SetPreload(Preload preload) { | 169 void BufferedDataSource::SetPreload(Preload preload) { |
| 170 DCHECK(render_loop_->BelongsToCurrentThread()); | 170 DCHECK(render_loop_->BelongsToCurrentThread()); |
| 171 preload_ = preload; | 171 preload_ = preload; |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool BufferedDataSource::HasSingleOrigin() { | 174 bool BufferedDataSource::HasSingleOrigin() { |
| 175 DCHECK(render_loop_->BelongsToCurrentThread()); | 175 DCHECK(render_loop_->BelongsToCurrentThread()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 216 |
| 217 ///////////////////////////////////////////////////////////////////////////// | 217 ///////////////////////////////////////////////////////////////////////////// |
| 218 // media::DataSource implementation. | 218 // media::DataSource implementation. |
| 219 void BufferedDataSource::Stop(const base::Closure& closure) { | 219 void BufferedDataSource::Stop(const base::Closure& closure) { |
| 220 { | 220 { |
| 221 base::AutoLock auto_lock(lock_); | 221 base::AutoLock auto_lock(lock_); |
| 222 StopInternal_Locked(); | 222 StopInternal_Locked(); |
| 223 } | 223 } |
| 224 closure.Run(); | 224 closure.Run(); |
| 225 | 225 |
| 226 render_loop_->PostTask(FROM_HERE, | 226 render_loop_->PostTask( |
| 227 base::Bind(&BufferedDataSource::StopLoader, weak_this_)); | 227 FROM_HERE, |
| 228 base::Bind(&BufferedDataSource::StopLoader, weak_factory_.GetWeakPtr())); |
| 228 } | 229 } |
| 229 | 230 |
| 230 void BufferedDataSource::SetBitrate(int bitrate) { | 231 void BufferedDataSource::SetBitrate(int bitrate) { |
| 231 render_loop_->PostTask(FROM_HERE, base::Bind( | 232 render_loop_->PostTask(FROM_HERE, |
| 232 &BufferedDataSource::SetBitrateTask, weak_this_, bitrate)); | 233 base::Bind(&BufferedDataSource::SetBitrateTask, |
| 234 weak_factory_.GetWeakPtr(), |
| 235 bitrate)); |
| 233 } | 236 } |
| 234 | 237 |
| 235 void BufferedDataSource::Read( | 238 void BufferedDataSource::Read( |
| 236 int64 position, int size, uint8* data, | 239 int64 position, int size, uint8* data, |
| 237 const media::DataSource::ReadCB& read_cb) { | 240 const media::DataSource::ReadCB& read_cb) { |
| 238 DVLOG(1) << "Read: " << position << " offset, " << size << " bytes"; | 241 DVLOG(1) << "Read: " << position << " offset, " << size << " bytes"; |
| 239 DCHECK(!read_cb.is_null()); | 242 DCHECK(!read_cb.is_null()); |
| 240 | 243 |
| 241 { | 244 { |
| 242 base::AutoLock auto_lock(lock_); | 245 base::AutoLock auto_lock(lock_); |
| 243 DCHECK(!read_op_); | 246 DCHECK(!read_op_); |
| 244 | 247 |
| 245 if (stop_signal_received_) { | 248 if (stop_signal_received_) { |
| 246 read_cb.Run(kReadError); | 249 read_cb.Run(kReadError); |
| 247 return; | 250 return; |
| 248 } | 251 } |
| 249 | 252 |
| 250 read_op_.reset(new ReadOperation(position, size, data, read_cb)); | 253 read_op_.reset(new ReadOperation(position, size, data, read_cb)); |
| 251 } | 254 } |
| 252 | 255 |
| 253 render_loop_->PostTask(FROM_HERE, base::Bind( | 256 render_loop_->PostTask( |
| 254 &BufferedDataSource::ReadTask, weak_this_)); | 257 FROM_HERE, |
| 258 base::Bind(&BufferedDataSource::ReadTask, weak_factory_.GetWeakPtr())); |
| 255 } | 259 } |
| 256 | 260 |
| 257 bool BufferedDataSource::GetSize(int64* size_out) { | 261 bool BufferedDataSource::GetSize(int64* size_out) { |
| 258 if (total_bytes_ != kPositionNotSpecified) { | 262 if (total_bytes_ != kPositionNotSpecified) { |
| 259 *size_out = total_bytes_; | 263 *size_out = total_bytes_; |
| 260 return true; | 264 return true; |
| 261 } | 265 } |
| 262 *size_out = 0; | 266 *size_out = 0; |
| 263 return false; | 267 return false; |
| 264 } | 268 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 size = read_op_->size(); | 323 size = read_op_->size(); |
| 320 } | 324 } |
| 321 | 325 |
| 322 // First we prepare the intermediate read buffer for BufferedResourceLoader | 326 // First we prepare the intermediate read buffer for BufferedResourceLoader |
| 323 // to write to. | 327 // to write to. |
| 324 if (size > intermediate_read_buffer_size_) { | 328 if (size > intermediate_read_buffer_size_) { |
| 325 intermediate_read_buffer_.reset(new uint8[size]); | 329 intermediate_read_buffer_.reset(new uint8[size]); |
| 326 } | 330 } |
| 327 | 331 |
| 328 // Perform the actual read with BufferedResourceLoader. | 332 // Perform the actual read with BufferedResourceLoader. |
| 329 loader_->Read( | 333 loader_->Read(position, |
| 330 position, size, intermediate_read_buffer_.get(), | 334 size, |
| 331 base::Bind(&BufferedDataSource::ReadCallback, weak_this_)); | 335 intermediate_read_buffer_.get(), |
| 336 base::Bind(&BufferedDataSource::ReadCallback, |
| 337 weak_factory_.GetWeakPtr())); |
| 332 } | 338 } |
| 333 | 339 |
| 334 | 340 |
| 335 ///////////////////////////////////////////////////////////////////////////// | 341 ///////////////////////////////////////////////////////////////////////////// |
| 336 // BufferedResourceLoader callback methods. | 342 // BufferedResourceLoader callback methods. |
| 337 void BufferedDataSource::StartCallback( | 343 void BufferedDataSource::StartCallback( |
| 338 BufferedResourceLoader::Status status) { | 344 BufferedResourceLoader::Status status) { |
| 339 DCHECK(render_loop_->BelongsToCurrentThread()); | 345 DCHECK(render_loop_->BelongsToCurrentThread()); |
| 340 DCHECK(loader_.get()); | 346 DCHECK(loader_.get()); |
| 341 | 347 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 loader_->Stop(); | 430 loader_->Stop(); |
| 425 | 431 |
| 426 if (status == BufferedResourceLoader::kCacheMiss && | 432 if (status == BufferedResourceLoader::kCacheMiss && |
| 427 read_op_->retries() < kNumCacheMissRetries) { | 433 read_op_->retries() < kNumCacheMissRetries) { |
| 428 read_op_->IncrementRetries(); | 434 read_op_->IncrementRetries(); |
| 429 | 435 |
| 430 // Recreate a loader starting from where we last left off until the | 436 // Recreate a loader starting from where we last left off until the |
| 431 // end of the resource. | 437 // end of the resource. |
| 432 loader_.reset(CreateResourceLoader( | 438 loader_.reset(CreateResourceLoader( |
| 433 read_op_->position(), kPositionNotSpecified)); | 439 read_op_->position(), kPositionNotSpecified)); |
| 440 |
| 441 base::WeakPtr<BufferedDataSource> weak_this = weak_factory_.GetWeakPtr(); |
| 434 loader_->Start( | 442 loader_->Start( |
| 435 base::Bind(&BufferedDataSource::PartialReadStartCallback, weak_this_), | 443 base::Bind(&BufferedDataSource::PartialReadStartCallback, weak_this), |
| 436 base::Bind(&BufferedDataSource::LoadingStateChangedCallback, | 444 base::Bind(&BufferedDataSource::LoadingStateChangedCallback, |
| 437 weak_this_), | 445 weak_this), |
| 438 base::Bind(&BufferedDataSource::ProgressCallback, weak_this_), | 446 base::Bind(&BufferedDataSource::ProgressCallback, weak_this), |
| 439 frame_); | 447 frame_); |
| 440 return; | 448 return; |
| 441 } | 449 } |
| 442 | 450 |
| 443 ReadOperation::Run(read_op_.Pass(), kReadError); | 451 ReadOperation::Run(read_op_.Pass(), kReadError); |
| 444 return; | 452 return; |
| 445 } | 453 } |
| 446 | 454 |
| 447 if (bytes_read > 0) { | 455 if (bytes_read > 0) { |
| 448 memcpy(read_op_->data(), intermediate_read_buffer_.get(), bytes_read); | 456 memcpy(read_op_->data(), intermediate_read_buffer_.get(), bytes_read); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 return; | 559 return; |
| 552 } | 560 } |
| 553 | 561 |
| 554 // If media is currently playing or the page indicated preload=auto, | 562 // If media is currently playing or the page indicated preload=auto, |
| 555 // use threshold strategy to enable/disable deferring when the buffer | 563 // use threshold strategy to enable/disable deferring when the buffer |
| 556 // is full/depleted. | 564 // is full/depleted. |
| 557 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); | 565 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); |
| 558 } | 566 } |
| 559 | 567 |
| 560 } // namespace content | 568 } // namespace content |
| OLD | NEW |