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/multibuffer_data_source.h" | 5 #include "media/blink/multibuffer_data_source.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "media/base/media_log.h" | 14 #include "media/base/media_log.h" |
15 #include "media/blink/buffered_data_source_host_impl.h" | 15 #include "media/blink/buffered_data_source_host_impl.h" |
16 #include "media/blink/multibuffer_reader.h" | 16 #include "media/blink/multibuffer_reader.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 | 18 |
19 using blink::WebFrame; | 19 using blink::WebFrame; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Minimum preload buffer. | 23 // Minimum preload buffer. |
24 const int64_t kMinBufferPreload = 2 << 20; // 2 Mb | 24 const int64_t kMinBufferPreload = 2 << 20; // 2 Mb |
25 // Maxmimum preload buffer. | 25 // Maxmimum preload buffer. |
26 const int64_t kMaxBufferPreload = 20 << 20; // 20 Mb | 26 const int64_t kMaxBufferPreload = 50 << 20; // 50 Mb |
27 | 27 |
28 // Preload this much extra, then stop preloading until we fall below the | 28 // Preload this much extra, then stop preloading until we fall below the |
29 // kTargetSecondsBufferedAhead. | 29 // kTargetSecondsBufferedAhead. |
30 const int64_t kPreloadHighExtra = 1 << 20; // 1 Mb | 30 const int64_t kPreloadHighExtra = 1 << 20; // 1 Mb |
31 | 31 |
32 // Total size of the pinned region in the cache. | 32 // Default pin region size. |
33 const int64_t kMaxBufferSize = 25 << 20; // 25 Mb | 33 // Note that we go over this if preload is calculated high enough. |
| 34 const int64_t kDefaultPinSize = 25 << 20; // 25 Mb |
34 | 35 |
35 // If bitrate is not known, use this. | 36 // If bitrate is not known, use this. |
36 const int64_t kDefaultBitrate = 200 * 8 << 10; // 200 Kbps. | 37 const int64_t kDefaultBitrate = 200 * 8 << 10; // 200 Kbps. |
37 | 38 |
38 // Maximum bitrate for buffer calculations. | 39 // Maximum bitrate for buffer calculations. |
39 const int64_t kMaxBitrate = 20 * 8 << 20; // 20 Mbps. | 40 const int64_t kMaxBitrate = 20 * 8 << 20; // 20 Mbps. |
40 | 41 |
41 // Maximum playback rate for buffer calculations. | 42 // Maximum playback rate for buffer calculations. |
42 const double kMaxPlaybackRate = 25.0; | 43 const double kMaxPlaybackRate = 25.0; |
43 | 44 |
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 | 593 |
593 // Only scale the buffer window for playback rates greater than 1.0 in | 594 // Only scale the buffer window for playback rates greater than 1.0 in |
594 // magnitude and clamp to prevent overflow. | 595 // magnitude and clamp to prevent overflow. |
595 double playback_rate = playback_rate_; | 596 double playback_rate = playback_rate_; |
596 | 597 |
597 playback_rate = std::max(playback_rate, 1.0); | 598 playback_rate = std::max(playback_rate, 1.0); |
598 playback_rate = std::min(playback_rate, kMaxPlaybackRate); | 599 playback_rate = std::min(playback_rate, kMaxPlaybackRate); |
599 | 600 |
600 int64_t bytes_per_second = (bitrate / 8.0) * playback_rate; | 601 int64_t bytes_per_second = (bitrate / 8.0) * playback_rate; |
601 | 602 |
| 603 // Preload 10 seconds of data, clamped to some min/max value. |
602 int64_t preload = clamp(kTargetSecondsBufferedAhead * bytes_per_second, | 604 int64_t preload = clamp(kTargetSecondsBufferedAhead * bytes_per_second, |
603 kMinBufferPreload, kMaxBufferPreload); | 605 kMinBufferPreload, kMaxBufferPreload); |
| 606 // We preload this much, then we stop unil we read |preload| before resuming. |
604 int64_t preload_high = preload + kPreloadHighExtra; | 607 int64_t preload_high = preload + kPreloadHighExtra; |
605 | 608 |
606 // Assert that kMaxBufferSize is big enough that the subtraction on the next | 609 // We pin a few seconds of data behind the current reading position. |
607 // line cannot go negative. | 610 int64_t pin_backward = clamp(kTargetSecondsBufferedBehind * bytes_per_second, |
608 static_assert(kMaxBufferSize > kMaxBufferPreload + kPreloadHighExtra, | 611 kMinBufferPreload, kMaxBufferPreload); |
609 "kMaxBufferSize too small to contain preload."); | 612 |
610 int64_t back_buffer = clamp(kTargetSecondsBufferedBehind * bytes_per_second, | 613 // We always pin at least kDefaultPinSize ahead of the read position. |
611 kMinBufferPreload, kMaxBufferSize - preload_high); | 614 // Normally, the extra space between preload_high and kDefaultPinSize will |
| 615 // not actually have any data in it, but if it does, we don't want to throw it |
| 616 // away right before we need it. |
| 617 int64_t pin_forward = std::max(preload_high, kDefaultPinSize); |
| 618 |
| 619 // Note that the buffer size is advisory as only non-pinned data is allowed |
| 620 // to be thrown away. Most of the time we pin a region that is larger than |
| 621 // |buffer_size|, which only makes sense because most of the time, some of |
| 622 // the data in pinned region is not present in the cache. |
612 int64_t buffer_size = | 623 int64_t buffer_size = |
613 std::min((kTargetSecondsBufferedAhead + kTargetSecondsBufferedBehind) * | 624 std::min((kTargetSecondsBufferedAhead + kTargetSecondsBufferedBehind) * |
614 bytes_per_second, | 625 bytes_per_second, |
615 kMaxBufferSize); | 626 preload_high + pin_backward); |
616 reader_->SetMaxBuffer(buffer_size); | 627 reader_->SetMaxBuffer(buffer_size); |
617 reader_->SetPinRange(back_buffer, kMaxBufferPreload + kPreloadHighExtra); | 628 reader_->SetPinRange(pin_backward, pin_forward); |
618 | 629 |
619 if (preload_ == METADATA) { | 630 if (preload_ == METADATA) { |
620 reader_->SetPreload(0, 0); | 631 reader_->SetPreload(0, 0); |
621 } else { | 632 } else { |
622 reader_->SetPreload(preload_high, preload); | 633 reader_->SetPreload(preload_high, preload); |
623 } | 634 } |
624 } | 635 } |
625 | 636 |
626 } // namespace media | 637 } // namespace media |
OLD | NEW |