Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/callback_helpers.h" | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "media/blink/multibuffer_reader.h" | |
| 9 #include "net/base/net_errors.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 MultiBufferReader::MultiBufferReader( | |
| 14 MultiBuffer* multibuffer, | |
| 15 int64_t start, | |
| 16 int64_t end, | |
| 17 const base::Callback<void(int64_t, int64_t)>& progress_callback) | |
| 18 : multibuffer_(multibuffer), | |
| 19 end_(end == -1LL ? (1LL << (multibuffer->block_size_shift() + 30)) : end), | |
| 20 preload_high_(0), | |
| 21 preload_low_(0), | |
| 22 max_buffer_forward_(0), | |
| 23 max_buffer_backward_(0), | |
| 24 pos_(start), | |
| 25 preload_pos_(), | |
| 26 loading_(true), | |
| 27 current_wait_size_(0), | |
| 28 progress_callback_(progress_callback), | |
| 29 weak_factory_(this) { | |
| 30 DCHECK_GE(start, 0); | |
| 31 DCHECK_GE(end_, 0); | |
| 32 } | |
| 33 | |
| 34 MultiBufferReader::~MultiBufferReader() { | |
| 35 multibuffer_->RemoveReader(preload_pos_, this); | |
| 36 multibuffer_->IncrementMaxSize( | |
| 37 -block_ceil(max_buffer_forward_ + max_buffer_backward_)); | |
| 38 multibuffer_->PinRange(block(pos_ - max_buffer_backward_), | |
| 39 block_ceil(pos_ + max_buffer_forward_), -1); | |
| 40 multibuffer_->CleanupWriters(preload_pos_); | |
| 41 } | |
| 42 | |
| 43 MultiBuffer::BlockId MultiBufferReader::block(int64_t byte_pos) const { | |
| 44 return byte_pos >> multibuffer_->block_size_shift(); | |
| 45 } | |
| 46 | |
| 47 MultiBuffer::BlockId MultiBufferReader::block_ceil(int64_t byte_pos) const { | |
| 48 return block(byte_pos + (1LL << multibuffer_->block_size_shift()) - 1); | |
| 49 } | |
| 50 | |
| 51 void MultiBufferReader::Seek(int64_t pos) { | |
| 52 DCHECK_GE(pos, 0); | |
| 53 if (pos == pos_) | |
| 54 return; | |
| 55 // Use a rangemap to compute the diff in pinning. | |
| 56 RangeMap<MultiBuffer::BlockId, int32_t> tmp; | |
| 57 tmp.IncrementRange(block(pos_ - max_buffer_backward_), | |
| 58 block_ceil(pos_ + max_buffer_forward_), -1); | |
| 59 tmp.IncrementRange(block(pos - max_buffer_backward_), | |
| 60 block_ceil(pos + max_buffer_forward_), 1); | |
| 61 | |
| 62 multibuffer_->PinRanges(tmp); | |
| 63 | |
| 64 multibuffer_->RemoveReader(preload_pos_, this); | |
| 65 MultiBufferBlockId old_preload_pos = preload_pos_; | |
| 66 preload_pos_ = block(pos); | |
| 67 pos_ = pos; | |
| 68 IncrementPreloadPos(); | |
| 69 multibuffer_->CleanupWriters(old_preload_pos); | |
| 70 } | |
| 71 | |
| 72 void MultiBufferReader::SetMaxBuffer(int64_t backward, int64_t forward) { | |
| 73 // Safe, because we know this doesn't actually prune the cache right away. | |
| 74 multibuffer_->IncrementMaxSize( | |
| 75 -block_ceil(max_buffer_forward_ + max_buffer_backward_)); | |
| 76 // Use a rangemap to compute the diff in pinning. | |
| 77 RangeMap<MultiBuffer::BlockId, int32_t> tmp; | |
| 78 tmp.IncrementRange(block(pos_ - max_buffer_backward_), | |
| 79 block_ceil(pos_ + max_buffer_forward_), -1); | |
| 80 max_buffer_backward_ = backward; | |
| 81 max_buffer_forward_ = forward; | |
| 82 tmp.IncrementRange(block(pos_ - max_buffer_backward_), | |
| 83 block_ceil(pos_ + max_buffer_forward_), 1); | |
| 84 multibuffer_->PinRanges(tmp); | |
| 85 | |
| 86 multibuffer_->IncrementMaxSize( | |
| 87 block_ceil(max_buffer_forward_ + max_buffer_backward_)); | |
| 88 } | |
| 89 | |
| 90 int64_t MultiBufferReader::Available() const { | |
| 91 MultiBufferBlockId current_block = block(pos_); | |
| 92 int64_t unavailable_byte_pos = | |
| 93 static_cast<int64_t>(multibuffer_->FindNextUnavailable(block(pos_))) | |
| 94 << multibuffer_->block_size_shift(); | |
| 95 return std::max<int64_t>(0, unavailable_byte_pos - pos_); | |
| 96 } | |
| 97 | |
| 98 int64_t MultiBufferReader::TryRead(unsigned char* data, int64_t len) { | |
| 99 DCHECK_GT(len, 0); | |
| 100 current_wait_size_ = 0; | |
| 101 cb_.Reset(); | |
| 102 DCHECK_LE(pos_ + len, end_); | |
| 103 const MultiBuffer::DataMap& data_map = multibuffer_->map(); | |
| 104 MultiBuffer::DataMap::const_iterator i = data_map.find(block(pos_)); | |
| 105 int64_t p = pos_; | |
| 106 int64_t bytes_read = 0; | |
| 107 while (bytes_read < len) { | |
| 108 if (i == data_map.end()) | |
| 109 break; | |
| 110 if (i->first != block(p)) | |
| 111 break; | |
| 112 if (i->second->end_of_stream()) | |
| 113 break; | |
| 114 size_t offset = p & ((1LL << multibuffer_->block_size_shift()) - 1); | |
| 115 size_t tocopy = | |
| 116 std::min<size_t>(len - bytes_read, i->second->data_size() - offset); | |
| 117 memcpy(data, i->second->data() + offset, tocopy); | |
| 118 data += tocopy; | |
| 119 bytes_read += tocopy; | |
| 120 p += tocopy; | |
| 121 ++i; | |
| 122 } | |
| 123 Seek(p); | |
| 124 return bytes_read; | |
| 125 } | |
| 126 | |
| 127 int MultiBufferReader::Wait(int64_t len, base::Closure cb) { | |
| 128 DCHECK_LE(pos_ + len, end_); | |
| 129 DCHECK_NE(Available(), -1); | |
| 130 DCHECK_LE(len, max_buffer_forward_); | |
| 131 current_wait_size_ = len; | |
| 132 | |
| 133 cb_.Reset(); | |
| 134 IncrementPreloadPos(); | |
| 135 | |
| 136 if (Available() >= current_wait_size_) { | |
| 137 return net::OK; | |
| 138 } else { | |
| 139 cb_ = cb; | |
| 140 return net::ERR_IO_PENDING; | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void MultiBufferReader::SetPreload(int64_t preload_high, int64_t preload_low) { | |
| 145 DCHECK_GE(preload_high, preload_low); | |
| 146 multibuffer_->RemoveReader(preload_pos_, this); | |
| 147 preload_pos_ = block(pos_); | |
| 148 preload_high_ = preload_high; | |
| 149 preload_low_ = preload_low; | |
| 150 IncrementPreloadPos(); | |
| 151 } | |
| 152 | |
| 153 bool MultiBufferReader::IsLoading() const { | |
| 154 return loading_; | |
| 155 } | |
| 156 | |
| 157 void MultiBufferReader::CheckWait() { | |
| 158 if (!cb_.is_null() && | |
| 159 (Available() >= current_wait_size_ || Available() == -1)) { | |
| 160 base::MessageLoop::current()->PostTask( | |
| 161 FROM_HERE, | |
| 162 base::Bind(&MultiBufferReader::Call, weak_factory_.GetWeakPtr(), | |
| 163 base::ResetAndReturn(&cb_))); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void MultiBufferReader::Call(const base::Closure& cb) const { | |
| 168 cb.Run(); | |
| 169 } | |
| 170 | |
| 171 void MultiBufferReader::NotifyAvailableRange( | |
| 172 const Range<MultiBufferBlockId>& range) { | |
| 173 // Update end_ if we can. | |
| 174 if (range.end > range.begin) { | |
| 175 auto i = multibuffer_->map().find(range.end - 1); | |
| 176 DCHECK(i != multibuffer_->map().end()); | |
| 177 if (i->second->end_of_stream()) { | |
| 178 // This is an upper limit because the last-to-one block is allowed | |
| 179 // to be smaller than the rest of the blocks. | |
| 180 int64_t size_upper_limit = static_cast<int64_t>(range.end) | |
| 181 << multibuffer_->block_size_shift(); | |
| 182 end_ = std::min(end_, size_upper_limit); | |
| 183 } | |
| 184 } | |
| 185 IncrementPreloadPos(); | |
| 186 if (!progress_callback_.is_null()) { | |
| 187 progress_callback_.Run( | |
| 188 static_cast<int64_t>(range.begin) << multibuffer_->block_size_shift(), | |
| 189 static_cast<int64_t>(range.end) << multibuffer_->block_size_shift()); | |
| 190 // We may be destroyed, do not touch |this|. | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 void MultiBufferReader::IncrementPreloadPos() { | |
| 195 int64_t effective_preload = loading_ ? preload_high_ : preload_low_; | |
| 196 | |
| 197 loading_ = false; | |
| 198 if (preload_pos_ == MultiBuffer::BlockId()) { | |
| 199 preload_pos_ = block(pos_); | |
| 200 DCHECK_GE(preload_pos_, 0); | |
| 201 } | |
| 202 MultiBuffer::BlockId max_preload = block_ceil( | |
| 203 std::min(end_, pos_ + std::max(effective_preload, current_wait_size_))); | |
| 204 | |
| 205 multibuffer_->RemoveReader(preload_pos_, this); | |
| 206 | |
| 207 preload_pos_ = multibuffer_->FindNextUnavailable(preload_pos_); | |
|
liberato (no reviews please)
2015/10/29 18:44:00
isn't it possible that this retruns preload_pos_+1
hubbe
2015/10/29 21:45:06
Renamed function added comments as per offline dis
| |
| 208 DCHECK_GE(preload_pos_, 0); | |
| 209 | |
| 210 DVLOG(3) << "IncrementPreloadPos" | |
| 211 << " pp = " << preload_pos_ | |
| 212 << " block_ceil(end_) = " << block_ceil(end_) << " end_ = " << end_ | |
| 213 << " max_preload " << max_preload; | |
| 214 | |
| 215 if (preload_pos_ < block_ceil(end_)) { | |
| 216 if (preload_pos_ < max_preload) { | |
| 217 loading_ = true; | |
| 218 multibuffer_->AddReader(preload_pos_, this); | |
| 219 } else if (multibuffer_->Contains(preload_pos_ - 1)) { | |
| 220 --preload_pos_; | |
| 221 multibuffer_->AddReader(preload_pos_, this); | |
| 222 } | |
| 223 } | |
| 224 CheckWait(); | |
| 225 } | |
| 226 | |
| 227 } // namespace media | |
| OLD | NEW |