| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/media/buffered_resource_loader.h" | 5 #include "webkit/media/buffered_resource_loader.h" |
| 6 | 6 |
| 7 #include "base/bits.h" | 7 #include "base/bits.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 135 |
| 136 // Set the initial capacity of |buffer_| based on |bitrate_| and | 136 // Set the initial capacity of |buffer_| based on |bitrate_| and |
| 137 // |playback_rate_|. | 137 // |playback_rate_|. |
| 138 UpdateBufferWindow(); | 138 UpdateBufferWindow(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 BufferedResourceLoader::~BufferedResourceLoader() {} | 141 BufferedResourceLoader::~BufferedResourceLoader() {} |
| 142 | 142 |
| 143 void BufferedResourceLoader::Start( | 143 void BufferedResourceLoader::Start( |
| 144 const StartCB& start_cb, | 144 const StartCB& start_cb, |
| 145 const base::Closure& event_cb, | 145 const LoadingCB& loading_cb, |
| 146 const ProgressCB& progress_cb, |
| 146 WebFrame* frame) { | 147 WebFrame* frame) { |
| 147 // Make sure we have not started. | 148 // Make sure we have not started. |
| 148 DCHECK(start_cb_.is_null()); | 149 DCHECK(start_cb_.is_null()); |
| 149 DCHECK(event_cb_.is_null()); | 150 DCHECK(loading_cb_.is_null()); |
| 151 DCHECK(progress_cb_.is_null()); |
| 150 DCHECK(!start_cb.is_null()); | 152 DCHECK(!start_cb.is_null()); |
| 151 DCHECK(!event_cb.is_null()); | 153 DCHECK(!loading_cb.is_null()); |
| 154 DCHECK(!progress_cb.is_null()); |
| 152 CHECK(frame); | 155 CHECK(frame); |
| 153 | 156 |
| 154 start_cb_ = start_cb; | 157 start_cb_ = start_cb; |
| 155 event_cb_ = event_cb; | 158 loading_cb_ = loading_cb; |
| 159 progress_cb_ = progress_cb; |
| 156 | 160 |
| 157 if (first_byte_position_ != kPositionNotSpecified) { | 161 if (first_byte_position_ != kPositionNotSpecified) { |
| 158 // TODO(hclam): server may not support range request so |offset_| may not | 162 // TODO(hclam): server may not support range request so |offset_| may not |
| 159 // equal to |first_byte_position_|. | 163 // equal to |first_byte_position_|. |
| 160 offset_ = first_byte_position_; | 164 offset_ = first_byte_position_; |
| 161 } | 165 } |
| 162 | 166 |
| 163 // Prepare the request. | 167 // Prepare the request. |
| 164 WebURLRequest request(url_); | 168 WebURLRequest request(url_); |
| 165 request.setTargetType(WebURLRequest::TargetIsMedia); | 169 request.setTargetType(WebURLRequest::TargetIsMedia); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 186 WebURLLoaderOptions options; | 190 WebURLLoaderOptions options; |
| 187 options.allowCredentials = true; | 191 options.allowCredentials = true; |
| 188 options.crossOriginRequestPolicy = | 192 options.crossOriginRequestPolicy = |
| 189 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; | 193 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 190 loader.reset(frame->createAssociatedURLLoader(options)); | 194 loader.reset(frame->createAssociatedURLLoader(options)); |
| 191 } | 195 } |
| 192 | 196 |
| 193 // Start the resource loading. | 197 // Start the resource loading. |
| 194 loader->loadAsynchronously(request, this); | 198 loader->loadAsynchronously(request, this); |
| 195 active_loader_.reset(new ActiveLoader(loader.Pass())); | 199 active_loader_.reset(new ActiveLoader(loader.Pass())); |
| 200 loading_cb_.Run(kLoading); |
| 196 } | 201 } |
| 197 | 202 |
| 198 void BufferedResourceLoader::Stop() { | 203 void BufferedResourceLoader::Stop() { |
| 199 // Reset callbacks. | 204 // Reset callbacks. |
| 200 start_cb_.Reset(); | 205 start_cb_.Reset(); |
| 201 event_cb_.Reset(); | 206 loading_cb_.Reset(); |
| 207 progress_cb_.Reset(); |
| 202 read_cb_.Reset(); | 208 read_cb_.Reset(); |
| 203 | 209 |
| 204 // Cancel and reset any active loaders. | 210 // Cancel and reset any active loaders. |
| 205 active_loader_.reset(); | 211 active_loader_.reset(); |
| 206 } | 212 } |
| 207 | 213 |
| 208 void BufferedResourceLoader::Read( | 214 void BufferedResourceLoader::Read( |
| 209 int64 position, | 215 int64 position, |
| 210 int read_size, | 216 int read_size, |
| 211 uint8* buffer, | 217 uint8* buffer, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 DCHECK(!ShouldEnableDefer()) | 302 DCHECK(!ShouldEnableDefer()) |
| 297 << "Capacity was not adjusted properly to prevent deferring."; | 303 << "Capacity was not adjusted properly to prevent deferring."; |
| 298 | 304 |
| 299 return; | 305 return; |
| 300 } | 306 } |
| 301 | 307 |
| 302 // Make a callback to report failure. | 308 // Make a callback to report failure. |
| 303 DoneRead(kCacheMiss, 0); | 309 DoneRead(kCacheMiss, 0); |
| 304 } | 310 } |
| 305 | 311 |
| 306 int64 BufferedResourceLoader::GetBufferedPosition() { | |
| 307 return offset_ + buffer_.forward_bytes() - 1; | |
| 308 } | |
| 309 | |
| 310 int64 BufferedResourceLoader::content_length() { | 312 int64 BufferedResourceLoader::content_length() { |
| 311 return content_length_; | 313 return content_length_; |
| 312 } | 314 } |
| 313 | 315 |
| 314 int64 BufferedResourceLoader::instance_size() { | 316 int64 BufferedResourceLoader::instance_size() { |
| 315 return instance_size_; | 317 return instance_size_; |
| 316 } | 318 } |
| 317 | 319 |
| 318 bool BufferedResourceLoader::range_supported() { | 320 bool BufferedResourceLoader::range_supported() { |
| 319 return range_supported_; | 321 return range_supported_; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 UpdateDeferBehavior(); | 462 UpdateDeferBehavior(); |
| 461 | 463 |
| 462 // Consume excess bytes from our in-memory buffer if necessary. | 464 // Consume excess bytes from our in-memory buffer if necessary. |
| 463 if (buffer_.forward_bytes() > buffer_.forward_capacity()) { | 465 if (buffer_.forward_bytes() > buffer_.forward_capacity()) { |
| 464 int excess = buffer_.forward_bytes() - buffer_.forward_capacity(); | 466 int excess = buffer_.forward_bytes() - buffer_.forward_capacity(); |
| 465 bool success = buffer_.Seek(excess); | 467 bool success = buffer_.Seek(excess); |
| 466 DCHECK(success); | 468 DCHECK(success); |
| 467 offset_ += first_offset_ + excess; | 469 offset_ += first_offset_ + excess; |
| 468 } | 470 } |
| 469 | 471 |
| 470 // Notify that we have received some data. | 472 // Notify latest progress and buffered offset. |
| 471 NotifyNetworkEvent(); | 473 progress_cb_.Run(offset_ + buffer_.forward_bytes() - 1); |
| 472 Log(); | 474 Log(); |
| 473 } | 475 } |
| 474 | 476 |
| 475 void BufferedResourceLoader::didDownloadData( | 477 void BufferedResourceLoader::didDownloadData( |
| 476 WebKit::WebURLLoader* loader, | 478 WebKit::WebURLLoader* loader, |
| 477 int dataLength) { | 479 int dataLength) { |
| 478 NOTIMPLEMENTED(); | 480 NOTIMPLEMENTED(); |
| 479 } | 481 } |
| 480 | 482 |
| 481 void BufferedResourceLoader::didReceiveCachedMetadata( | 483 void BufferedResourceLoader::didReceiveCachedMetadata( |
| 482 WebURLLoader* loader, | 484 WebURLLoader* loader, |
| 483 const char* data, | 485 const char* data, |
| 484 int data_length) { | 486 int data_length) { |
| 485 NOTIMPLEMENTED(); | 487 NOTIMPLEMENTED(); |
| 486 } | 488 } |
| 487 | 489 |
| 488 void BufferedResourceLoader::didFinishLoading( | 490 void BufferedResourceLoader::didFinishLoading( |
| 489 WebURLLoader* loader, | 491 WebURLLoader* loader, |
| 490 double finishTime) { | 492 double finishTime) { |
| 491 DVLOG(1) << "didFinishLoading"; | 493 DVLOG(1) << "didFinishLoading"; |
| 492 DCHECK(active_loader_.get()); | 494 DCHECK(active_loader_.get()); |
| 493 | 495 |
| 494 // We're done with the loader. | 496 // We're done with the loader. |
| 495 active_loader_.reset(); | 497 active_loader_.reset(); |
| 496 NotifyNetworkEvent(); | 498 loading_cb_.Run(kLoadingFinished); |
| 497 | 499 |
| 498 // If we didn't know the |instance_size_| we do now. | 500 // If we didn't know the |instance_size_| we do now. |
| 499 if (instance_size_ == kPositionNotSpecified) { | 501 if (instance_size_ == kPositionNotSpecified) { |
| 500 instance_size_ = offset_ + buffer_.forward_bytes(); | 502 instance_size_ = offset_ + buffer_.forward_bytes(); |
| 501 } | 503 } |
| 502 | 504 |
| 503 // If there is a start callback, run it. | 505 // If there is a start callback, run it. |
| 504 if (!start_cb_.is_null()) { | 506 if (!start_cb_.is_null()) { |
| 505 DCHECK(read_cb_.is_null()) | 507 DCHECK(read_cb_.is_null()) |
| 506 << "Shouldn't have a read callback during start"; | 508 << "Shouldn't have a read callback during start"; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 527 const WebURLError& error) { | 529 const WebURLError& error) { |
| 528 DVLOG(1) << "didFail: reason=" << error.reason | 530 DVLOG(1) << "didFail: reason=" << error.reason |
| 529 << " isCancellation=" << error.isCancellation; | 531 << " isCancellation=" << error.isCancellation; |
| 530 DCHECK(active_loader_.get()); | 532 DCHECK(active_loader_.get()); |
| 531 | 533 |
| 532 // We don't need to continue loading after failure. | 534 // We don't need to continue loading after failure. |
| 533 // | 535 // |
| 534 // Keep it alive until we exit this method so that |error| remains valid. | 536 // Keep it alive until we exit this method so that |error| remains valid. |
| 535 scoped_ptr<ActiveLoader> active_loader = active_loader_.Pass(); | 537 scoped_ptr<ActiveLoader> active_loader = active_loader_.Pass(); |
| 536 loader_failed_ = true; | 538 loader_failed_ = true; |
| 537 NotifyNetworkEvent(); | 539 loading_cb_.Run(kLoadingFailed); |
| 538 | 540 |
| 539 // Don't leave start callbacks hanging around. | 541 // Don't leave start callbacks hanging around. |
| 540 if (!start_cb_.is_null()) { | 542 if (!start_cb_.is_null()) { |
| 541 DCHECK(read_cb_.is_null()) | 543 DCHECK(read_cb_.is_null()) |
| 542 << "Shouldn't have a read callback during start"; | 544 << "Shouldn't have a read callback during start"; |
| 543 DoneStart(kFailed); | 545 DoneStart(kFailed); |
| 544 return; | 546 return; |
| 545 } | 547 } |
| 546 | 548 |
| 547 // Don't leave read callbacks hanging around. | 549 // Don't leave read callbacks hanging around. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 return; | 603 return; |
| 602 | 604 |
| 603 // If necessary, toggle defer state and continue/pause downloading data | 605 // If necessary, toggle defer state and continue/pause downloading data |
| 604 // accordingly. | 606 // accordingly. |
| 605 if (ShouldEnableDefer() || ShouldDisableDefer()) | 607 if (ShouldEnableDefer() || ShouldDisableDefer()) |
| 606 SetDeferred(!active_loader_->deferred()); | 608 SetDeferred(!active_loader_->deferred()); |
| 607 } | 609 } |
| 608 | 610 |
| 609 void BufferedResourceLoader::SetDeferred(bool deferred) { | 611 void BufferedResourceLoader::SetDeferred(bool deferred) { |
| 610 active_loader_->SetDeferred(deferred); | 612 active_loader_->SetDeferred(deferred); |
| 611 NotifyNetworkEvent(); | 613 loading_cb_.Run(deferred ? kLoadingDeferred : kLoading); |
| 612 } | 614 } |
| 613 | 615 |
| 614 bool BufferedResourceLoader::ShouldEnableDefer() const { | 616 bool BufferedResourceLoader::ShouldEnableDefer() const { |
| 615 // If we're already deferring, then enabling makes no sense. | 617 // If we're already deferring, then enabling makes no sense. |
| 616 if (active_loader_->deferred()) | 618 if (active_loader_->deferred()) |
| 617 return false; | 619 return false; |
| 618 | 620 |
| 619 switch(defer_strategy_) { | 621 switch(defer_strategy_) { |
| 620 // Never defer at all, so never enable defer. | 622 // Never defer at all, so never enable defer. |
| 621 case kNeverDefer: | 623 case kNeverDefer: |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 Log(); | 816 Log(); |
| 815 | 817 |
| 816 base::ResetAndReturn(&read_cb_).Run(status, bytes_read); | 818 base::ResetAndReturn(&read_cb_).Run(status, bytes_read); |
| 817 } | 819 } |
| 818 | 820 |
| 819 | 821 |
| 820 void BufferedResourceLoader::DoneStart(Status status) { | 822 void BufferedResourceLoader::DoneStart(Status status) { |
| 821 base::ResetAndReturn(&start_cb_).Run(status); | 823 base::ResetAndReturn(&start_cb_).Run(status); |
| 822 } | 824 } |
| 823 | 825 |
| 824 void BufferedResourceLoader::NotifyNetworkEvent() { | |
| 825 if (!event_cb_.is_null()) | |
| 826 event_cb_.Run(); | |
| 827 } | |
| 828 | |
| 829 bool BufferedResourceLoader::IsRangeRequest() const { | 826 bool BufferedResourceLoader::IsRangeRequest() const { |
| 830 return first_byte_position_ != kPositionNotSpecified; | 827 return first_byte_position_ != kPositionNotSpecified; |
| 831 } | 828 } |
| 832 | 829 |
| 833 void BufferedResourceLoader::Log() { | 830 void BufferedResourceLoader::Log() { |
| 834 media_log_->AddEvent( | 831 media_log_->AddEvent( |
| 835 media_log_->CreateBufferedExtentsChangedEvent( | 832 media_log_->CreateBufferedExtentsChangedEvent( |
| 836 offset_ - buffer_.backward_bytes(), | 833 offset_ - buffer_.backward_bytes(), |
| 837 offset_, | 834 offset_, |
| 838 offset_ + buffer_.forward_bytes())); | 835 offset_ + buffer_.forward_bytes())); |
| 839 } | 836 } |
| 840 | 837 |
| 841 } // namespace webkit_media | 838 } // namespace webkit_media |
| OLD | NEW |