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 "media/blink/buffered_data_source.h" | 5 #include "media/blink/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/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "media/base/media_log.h" | 10 #include "media/base/media_log.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 83 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
84 WebFrame* frame, | 84 WebFrame* frame, |
85 MediaLog* media_log, | 85 MediaLog* media_log, |
86 BufferedDataSourceHost* host, | 86 BufferedDataSourceHost* host, |
87 const DownloadingCB& downloading_cb) | 87 const DownloadingCB& downloading_cb) |
88 : url_(url), | 88 : url_(url), |
89 cors_mode_(cors_mode), | 89 cors_mode_(cors_mode), |
90 total_bytes_(kPositionNotSpecified), | 90 total_bytes_(kPositionNotSpecified), |
91 streaming_(false), | 91 streaming_(false), |
92 frame_(frame), | 92 frame_(frame), |
93 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]), | |
94 intermediate_read_buffer_size_(kInitialReadBufferSize), | |
95 render_task_runner_(task_runner), | 93 render_task_runner_(task_runner), |
96 stop_signal_received_(false), | 94 stop_signal_received_(false), |
97 media_has_played_(false), | 95 media_has_played_(false), |
98 preload_(AUTO), | 96 preload_(AUTO), |
99 bitrate_(0), | 97 bitrate_(0), |
100 playback_rate_(0.0), | 98 playback_rate_(0.0), |
101 media_log_(media_log), | 99 media_log_(media_log), |
102 host_(host), | 100 host_(host), |
103 downloading_cb_(downloading_cb), | 101 downloading_cb_(downloading_cb), |
104 weak_factory_(this) { | 102 weak_factory_(this) { |
105 DCHECK(host_); | 103 DCHECK(host_); |
106 DCHECK(!downloading_cb_.is_null()); | 104 DCHECK(!downloading_cb_.is_null()); |
105 intermediate_read_buffer_.resize(kInitialReadBufferSize); | |
xhwang
2014/09/24 16:27:16
You can do this in the initialization list by usin
| |
107 } | 106 } |
108 | 107 |
109 BufferedDataSource::~BufferedDataSource() {} | 108 BufferedDataSource::~BufferedDataSource() {} |
110 | 109 |
111 // A factory method to create BufferedResourceLoader using the read parameters. | 110 // A factory method to create BufferedResourceLoader using the read parameters. |
112 // This method can be overridden to inject mock BufferedResourceLoader object | 111 // This method can be overridden to inject mock BufferedResourceLoader object |
113 // for testing purpose. | 112 // for testing purpose. |
114 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( | 113 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( |
115 int64 first_byte_position, int64 last_byte_position) { | 114 int64 first_byte_position, int64 last_byte_position) { |
116 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 115 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
295 | 294 |
296 bitrate_ = bitrate; | 295 bitrate_ = bitrate; |
297 loader_->SetBitrate(bitrate); | 296 loader_->SetBitrate(bitrate); |
298 } | 297 } |
299 | 298 |
300 // This method is the place where actual read happens, |loader_| must be valid | 299 // This method is the place where actual read happens, |loader_| must be valid |
301 // prior to make this method call. | 300 // prior to make this method call. |
302 void BufferedDataSource::ReadInternal() { | 301 void BufferedDataSource::ReadInternal() { |
303 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 302 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
304 int64 position = 0; | 303 int64 position = 0; |
305 int size = 0; | 304 int size = 0; |
xhwang
2014/09/24 16:27:16
You can change the type to size_t, then you don't
| |
306 { | 305 { |
307 base::AutoLock auto_lock(lock_); | 306 base::AutoLock auto_lock(lock_); |
308 if (stop_signal_received_) | 307 if (stop_signal_received_) |
309 return; | 308 return; |
310 | 309 |
311 position = read_op_->position(); | 310 position = read_op_->position(); |
312 size = read_op_->size(); | 311 size = read_op_->size(); |
313 } | 312 } |
314 | 313 |
315 // First we prepare the intermediate read buffer for BufferedResourceLoader | 314 // First we prepare the intermediate read buffer for BufferedResourceLoader |
316 // to write to. | 315 // to write to. |
317 if (size > intermediate_read_buffer_size_) { | 316 if (static_cast<int>(intermediate_read_buffer_.size()) < size) |
318 intermediate_read_buffer_.reset(new uint8[size]); | 317 intermediate_read_buffer_.resize(size); |
xhwang
2014/09/24 16:27:16
So the buffer size can only grow and never shrinks
xhwang
2014/09/24 16:27:16
For the record: This has another benefit. If the n
xhwang
2014/09/24 16:39:27
I did a quick test on Youtube and a test clip and
| |
319 } | |
320 | 318 |
321 // Perform the actual read with BufferedResourceLoader. | 319 // Perform the actual read with BufferedResourceLoader. |
322 loader_->Read(position, | 320 loader_->Read(position, |
323 size, | 321 size, |
324 intermediate_read_buffer_.get(), | 322 &intermediate_read_buffer_[0], |
xhwang
2014/09/24 16:27:16
This is safe since the intermediate_read_buffer_ i
| |
325 base::Bind(&BufferedDataSource::ReadCallback, | 323 base::Bind(&BufferedDataSource::ReadCallback, |
326 weak_factory_.GetWeakPtr())); | 324 weak_factory_.GetWeakPtr())); |
327 } | 325 } |
328 | 326 |
329 | 327 |
330 ///////////////////////////////////////////////////////////////////////////// | 328 ///////////////////////////////////////////////////////////////////////////// |
331 // BufferedResourceLoader callback methods. | 329 // BufferedResourceLoader callback methods. |
332 void BufferedDataSource::StartCallback( | 330 void BufferedDataSource::StartCallback( |
333 BufferedResourceLoader::Status status) { | 331 BufferedResourceLoader::Status status) { |
334 DCHECK(render_task_runner_->BelongsToCurrentThread()); | 332 DCHECK(render_task_runner_->BelongsToCurrentThread()); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 base::Bind(&BufferedDataSource::ProgressCallback, weak_this), | 439 base::Bind(&BufferedDataSource::ProgressCallback, weak_this), |
442 frame_); | 440 frame_); |
443 return; | 441 return; |
444 } | 442 } |
445 | 443 |
446 ReadOperation::Run(read_op_.Pass(), kReadError); | 444 ReadOperation::Run(read_op_.Pass(), kReadError); |
447 return; | 445 return; |
448 } | 446 } |
449 | 447 |
450 if (bytes_read > 0) { | 448 if (bytes_read > 0) { |
451 memcpy(read_op_->data(), intermediate_read_buffer_.get(), bytes_read); | 449 memcpy(read_op_->data(), &intermediate_read_buffer_[0], bytes_read); |
452 } else if (bytes_read == 0 && total_bytes_ == kPositionNotSpecified) { | 450 } else if (bytes_read == 0 && total_bytes_ == kPositionNotSpecified) { |
453 // We've reached the end of the file and we didn't know the total size | 451 // We've reached the end of the file and we didn't know the total size |
454 // before. Update the total size so Read()s past the end of the file will | 452 // before. Update the total size so Read()s past the end of the file will |
455 // fail like they would if we had known the file size at the beginning. | 453 // fail like they would if we had known the file size at the beginning. |
456 total_bytes_ = loader_->instance_size(); | 454 total_bytes_ = loader_->instance_size(); |
457 | 455 |
458 if (total_bytes_ != kPositionNotSpecified) { | 456 if (total_bytes_ != kPositionNotSpecified) { |
459 host_->SetTotalBytes(total_bytes_); | 457 host_->SetTotalBytes(total_bytes_); |
460 host_->AddBufferedByteRange(loader_->first_byte_position(), | 458 host_->AddBufferedByteRange(loader_->first_byte_position(), |
461 total_bytes_); | 459 total_bytes_); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 } | 524 } |
527 | 525 |
528 // If media is currently playing or the page indicated preload=auto or the | 526 // If media is currently playing or the page indicated preload=auto or the |
529 // the server does not support the byte range request or we do not want to go | 527 // the server does not support the byte range request or we do not want to go |
530 // too far ahead of the read head, use threshold strategy to enable/disable | 528 // too far ahead of the read head, use threshold strategy to enable/disable |
531 // deferring when the buffer is full/depleted. | 529 // deferring when the buffer is full/depleted. |
532 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); | 530 loader_->UpdateDeferStrategy(BufferedResourceLoader::kCapacityDefer); |
533 } | 531 } |
534 | 532 |
535 } // namespace media | 533 } // namespace media |
OLD | NEW |