Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
| 18 | |
Andreas Huber | ac05c31 | 2011-01-19 15:07:19 -0800 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 20 | #define LOG_TAG "NuCachedSource2" |
| 21 | #include <utils/Log.h> |
| 22 | |
Marco Nelissen | 42057ce | 2019-09-23 12:15:57 -0700 | [diff] [blame^] | 23 | #include <datasource/NuCachedSource2.h> |
| 24 | #include <datasource/HTTPBase.h> |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 25 | |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 27 | #include <media/stagefright/foundation/ADebug.h> |
| 28 | #include <media/stagefright/foundation/AMessage.h> |
| 29 | #include <media/stagefright/MediaErrors.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | struct PageCache { |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 34 | explicit PageCache(size_t pageSize); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 35 | ~PageCache(); |
| 36 | |
| 37 | struct Page { |
| 38 | void *mData; |
| 39 | size_t mSize; |
| 40 | }; |
| 41 | |
| 42 | Page *acquirePage(); |
| 43 | void releasePage(Page *page); |
| 44 | |
| 45 | void appendPage(Page *page); |
| 46 | size_t releaseFromStart(size_t maxBytes); |
| 47 | |
| 48 | size_t totalSize() const { |
| 49 | return mTotalSize; |
| 50 | } |
| 51 | |
| 52 | void copy(size_t from, void *data, size_t size); |
| 53 | |
| 54 | private: |
| 55 | size_t mPageSize; |
| 56 | size_t mTotalSize; |
| 57 | |
| 58 | List<Page *> mActivePages; |
| 59 | List<Page *> mFreePages; |
| 60 | |
| 61 | void freePages(List<Page *> *list); |
| 62 | |
| 63 | DISALLOW_EVIL_CONSTRUCTORS(PageCache); |
| 64 | }; |
| 65 | |
| 66 | PageCache::PageCache(size_t pageSize) |
| 67 | : mPageSize(pageSize), |
| 68 | mTotalSize(0) { |
| 69 | } |
| 70 | |
| 71 | PageCache::~PageCache() { |
| 72 | freePages(&mActivePages); |
| 73 | freePages(&mFreePages); |
| 74 | } |
| 75 | |
| 76 | void PageCache::freePages(List<Page *> *list) { |
| 77 | List<Page *>::iterator it = list->begin(); |
| 78 | while (it != list->end()) { |
| 79 | Page *page = *it; |
| 80 | |
| 81 | free(page->mData); |
| 82 | delete page; |
| 83 | page = NULL; |
| 84 | |
| 85 | ++it; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | PageCache::Page *PageCache::acquirePage() { |
| 90 | if (!mFreePages.empty()) { |
| 91 | List<Page *>::iterator it = mFreePages.begin(); |
| 92 | Page *page = *it; |
| 93 | mFreePages.erase(it); |
| 94 | |
| 95 | return page; |
| 96 | } |
| 97 | |
| 98 | Page *page = new Page; |
| 99 | page->mData = malloc(mPageSize); |
| 100 | page->mSize = 0; |
| 101 | |
| 102 | return page; |
| 103 | } |
| 104 | |
| 105 | void PageCache::releasePage(Page *page) { |
| 106 | page->mSize = 0; |
| 107 | mFreePages.push_back(page); |
| 108 | } |
| 109 | |
| 110 | void PageCache::appendPage(Page *page) { |
| 111 | mTotalSize += page->mSize; |
| 112 | mActivePages.push_back(page); |
| 113 | } |
| 114 | |
| 115 | size_t PageCache::releaseFromStart(size_t maxBytes) { |
| 116 | size_t bytesReleased = 0; |
| 117 | |
| 118 | while (maxBytes > 0 && !mActivePages.empty()) { |
| 119 | List<Page *>::iterator it = mActivePages.begin(); |
| 120 | |
| 121 | Page *page = *it; |
| 122 | |
| 123 | if (maxBytes < page->mSize) { |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | mActivePages.erase(it); |
| 128 | |
| 129 | maxBytes -= page->mSize; |
| 130 | bytesReleased += page->mSize; |
| 131 | |
| 132 | releasePage(page); |
| 133 | } |
| 134 | |
| 135 | mTotalSize -= bytesReleased; |
| 136 | return bytesReleased; |
| 137 | } |
| 138 | |
| 139 | void PageCache::copy(size_t from, void *data, size_t size) { |
Mark Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 140 | ALOGV("copy from %zu size %zu", from, size); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 141 | |
Andreas Huber | 3109629 | 2011-03-21 14:16:03 -0700 | [diff] [blame] | 142 | if (size == 0) { |
| 143 | return; |
| 144 | } |
| 145 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 146 | CHECK_LE(from + size, mTotalSize); |
| 147 | |
| 148 | size_t offset = 0; |
| 149 | List<Page *>::iterator it = mActivePages.begin(); |
| 150 | while (from >= offset + (*it)->mSize) { |
| 151 | offset += (*it)->mSize; |
| 152 | ++it; |
| 153 | } |
| 154 | |
| 155 | size_t delta = from - offset; |
| 156 | size_t avail = (*it)->mSize - delta; |
| 157 | |
| 158 | if (avail >= size) { |
| 159 | memcpy(data, (const uint8_t *)(*it)->mData + delta, size); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | memcpy(data, (const uint8_t *)(*it)->mData + delta, avail); |
| 164 | ++it; |
| 165 | data = (uint8_t *)data + avail; |
| 166 | size -= avail; |
| 167 | |
| 168 | while (size > 0) { |
| 169 | size_t copy = (*it)->mSize; |
| 170 | if (copy > size) { |
| 171 | copy = size; |
| 172 | } |
| 173 | memcpy(data, (*it)->mData, copy); |
| 174 | data = (uint8_t *)data + copy; |
| 175 | size -= copy; |
| 176 | ++it; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | //////////////////////////////////////////////////////////////////////////////// |
| 181 | |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 182 | NuCachedSource2::NuCachedSource2( |
| 183 | const sp<DataSource> &source, |
| 184 | const char *cacheConfig, |
| 185 | bool disconnectAtHighwatermark) |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 186 | : mSource(source), |
| 187 | mReflector(new AHandlerReflector<NuCachedSource2>(this)), |
| 188 | mLooper(new ALooper), |
| 189 | mCache(new PageCache(kPageSize)), |
| 190 | mCacheOffset(0), |
| 191 | mFinalStatus(OK), |
| 192 | mLastAccessPos(0), |
Andreas Huber | a5273eb | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 193 | mFetching(true), |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 194 | mDisconnecting(false), |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 195 | mLastFetchTimeUs(-1), |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 196 | mNumRetriesLeft(kMaxNumRetries), |
| 197 | mHighwaterThresholdBytes(kDefaultHighWaterThreshold), |
| 198 | mLowwaterThresholdBytes(kDefaultLowWaterThreshold), |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 199 | mKeepAliveIntervalUs(kDefaultKeepAliveIntervalUs), |
| 200 | mDisconnectAtHighwatermark(disconnectAtHighwatermark) { |
| 201 | // We are NOT going to support disconnect-at-highwatermark indefinitely |
| 202 | // and we are not guaranteeing support for client-specified cache |
| 203 | // parameters. Both of these are temporary measures to solve a specific |
| 204 | // problem that will be solved in a better way going forward. |
| 205 | |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 206 | updateCacheParamsFromSystemProperty(); |
| 207 | |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 208 | if (cacheConfig != NULL) { |
| 209 | updateCacheParamsFromString(cacheConfig); |
| 210 | } |
| 211 | |
| 212 | if (mDisconnectAtHighwatermark) { |
| 213 | // Makes no sense to disconnect and do keep-alives... |
| 214 | mKeepAliveIntervalUs = 0; |
| 215 | } |
| 216 | |
Andreas Huber | a814c1f | 2010-08-27 15:21:07 -0700 | [diff] [blame] | 217 | mLooper->setName("NuCachedSource2"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 218 | mLooper->registerHandler(mReflector); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 219 | |
| 220 | // Since it may not be obvious why our looper thread needs to be |
| 221 | // able to call into java since it doesn't appear to do so at all... |
| 222 | // IMediaHTTPConnection may be (and most likely is) implemented in JAVA |
| 223 | // and a local JAVA IBinder will call directly into JNI methods. |
| 224 | // So whenever we call DataSource::readAt it may end up in a call to |
| 225 | // IMediaHTTPConnection::readAt and therefore call back into JAVA. |
| 226 | mLooper->start(false /* runOnCallingThread */, true /* canCallJava */); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 227 | |
| 228 | mName = String8::format("NuCachedSource2(%s)", mSource->toString().string()); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | NuCachedSource2::~NuCachedSource2() { |
| 232 | mLooper->stop(); |
| 233 | mLooper->unregisterHandler(mReflector->id()); |
| 234 | |
| 235 | delete mCache; |
| 236 | mCache = NULL; |
| 237 | } |
| 238 | |
Wonsik Kim | 316c3d9 | 2015-09-08 17:32:28 +0900 | [diff] [blame] | 239 | // static |
| 240 | sp<NuCachedSource2> NuCachedSource2::Create( |
| 241 | const sp<DataSource> &source, |
| 242 | const char *cacheConfig, |
| 243 | bool disconnectAtHighwatermark) { |
| 244 | sp<NuCachedSource2> instance = new NuCachedSource2( |
| 245 | source, cacheConfig, disconnectAtHighwatermark); |
| 246 | Mutex::Autolock autoLock(instance->mLock); |
| 247 | (new AMessage(kWhatFetchMore, instance->mReflector))->post(); |
| 248 | return instance; |
| 249 | } |
| 250 | |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 251 | status_t NuCachedSource2::getEstimatedBandwidthKbps(int32_t *kbps) { |
James Dong | b33d2ac | 2011-06-01 15:27:20 -0700 | [diff] [blame] | 252 | if (mSource->flags() & kIsHTTPBasedSource) { |
| 253 | HTTPBase* source = static_cast<HTTPBase *>(mSource.get()); |
| 254 | return source->getEstimatedBandwidthKbps(kbps); |
| 255 | } |
| 256 | return ERROR_UNSUPPORTED; |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Robert Shih | 0324403 | 2018-10-02 14:36:32 -0700 | [diff] [blame] | 259 | void NuCachedSource2::close() { |
| 260 | disconnect(); |
| 261 | } |
| 262 | |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 263 | void NuCachedSource2::disconnect() { |
| 264 | if (mSource->flags() & kIsHTTPBasedSource) { |
| 265 | ALOGV("disconnecting HTTPBasedSource"); |
| 266 | |
| 267 | { |
| 268 | Mutex::Autolock autoLock(mLock); |
| 269 | // set mDisconnecting to true, if a fetch returns after |
| 270 | // this, the source will be marked as EOS. |
| 271 | mDisconnecting = true; |
Chong Zhang | 9f3d1cf | 2014-09-23 22:22:30 -0700 | [diff] [blame] | 272 | |
| 273 | // explicitly signal mCondition so that the pending readAt() |
| 274 | // will immediately return |
| 275 | mCondition.signal(); |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // explicitly disconnect from the source, to allow any |
| 279 | // pending reads to return more promptly |
| 280 | static_cast<HTTPBase *>(mSource.get())->disconnect(); |
| 281 | } |
| 282 | } |
| 283 | |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 284 | status_t NuCachedSource2::setCacheStatCollectFreq(int32_t freqMs) { |
James Dong | b33d2ac | 2011-06-01 15:27:20 -0700 | [diff] [blame] | 285 | if (mSource->flags() & kIsHTTPBasedSource) { |
| 286 | HTTPBase *source = static_cast<HTTPBase *>(mSource.get()); |
| 287 | return source->setBandwidthStatCollectFreq(freqMs); |
| 288 | } |
| 289 | return ERROR_UNSUPPORTED; |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 292 | status_t NuCachedSource2::initCheck() const { |
| 293 | return mSource->initCheck(); |
| 294 | } |
| 295 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 296 | status_t NuCachedSource2::getSize(off64_t *size) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 297 | return mSource->getSize(size); |
| 298 | } |
| 299 | |
| 300 | uint32_t NuCachedSource2::flags() { |
James Dong | b33d2ac | 2011-06-01 15:27:20 -0700 | [diff] [blame] | 301 | // Remove HTTP related flags since NuCachedSource2 is not HTTP-based. |
| 302 | uint32_t flags = mSource->flags() & ~(kWantsPrefetching | kIsHTTPBasedSource); |
| 303 | return (flags | kIsCachingDataSource); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void NuCachedSource2::onMessageReceived(const sp<AMessage> &msg) { |
| 307 | switch (msg->what()) { |
| 308 | case kWhatFetchMore: |
| 309 | { |
| 310 | onFetch(); |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | case kWhatRead: |
| 315 | { |
| 316 | onRead(msg); |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | default: |
| 321 | TRESPASS(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void NuCachedSource2::fetchInternal() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 326 | ALOGV("fetchInternal"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 327 | |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 328 | bool reconnect = false; |
| 329 | |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 330 | { |
| 331 | Mutex::Autolock autoLock(mLock); |
| 332 | CHECK(mFinalStatus == OK || mNumRetriesLeft > 0); |
| 333 | |
| 334 | if (mFinalStatus != OK) { |
| 335 | --mNumRetriesLeft; |
| 336 | |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 337 | reconnect = true; |
| 338 | } |
| 339 | } |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 340 | |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 341 | if (reconnect) { |
| 342 | status_t err = |
| 343 | mSource->reconnectAtOffset(mCacheOffset + mCache->totalSize()); |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 344 | |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 345 | Mutex::Autolock autoLock(mLock); |
| 346 | |
Chong Zhang | 9f3d1cf | 2014-09-23 22:22:30 -0700 | [diff] [blame] | 347 | if (mDisconnecting) { |
| 348 | mNumRetriesLeft = 0; |
| 349 | mFinalStatus = ERROR_END_OF_STREAM; |
| 350 | return; |
| 351 | } else if (err == ERROR_UNSUPPORTED || err == -EPIPE) { |
Andreas Huber | a7607a7 | 2012-08-28 09:48:40 -0700 | [diff] [blame] | 352 | // These are errors that are not likely to go away even if we |
| 353 | // retry, i.e. the server doesn't support range requests or similar. |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 354 | mNumRetriesLeft = 0; |
| 355 | return; |
| 356 | } else if (err != OK) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 357 | ALOGI("The attempt to reconnect failed, %d retries remaining", |
Andreas Huber | 95c4d60 | 2011-10-17 15:49:01 -0700 | [diff] [blame] | 358 | mNumRetriesLeft); |
| 359 | |
| 360 | return; |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 363 | |
| 364 | PageCache::Page *page = mCache->acquirePage(); |
| 365 | |
| 366 | ssize_t n = mSource->readAt( |
| 367 | mCacheOffset + mCache->totalSize(), page->mData, kPageSize); |
| 368 | |
| 369 | Mutex::Autolock autoLock(mLock); |
| 370 | |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 371 | if (n == 0 || mDisconnecting) { |
Chong Zhang | efbb619 | 2015-01-30 17:13:27 -0800 | [diff] [blame] | 372 | ALOGI("caching reached eos."); |
Chong Zhang | 48296b7 | 2014-09-14 14:28:45 -0700 | [diff] [blame] | 373 | |
| 374 | mNumRetriesLeft = 0; |
| 375 | mFinalStatus = ERROR_END_OF_STREAM; |
| 376 | |
| 377 | mCache->releasePage(page); |
| 378 | } else if (n < 0) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 379 | mFinalStatus = n; |
Andreas Huber | a7607a7 | 2012-08-28 09:48:40 -0700 | [diff] [blame] | 380 | if (n == ERROR_UNSUPPORTED || n == -EPIPE) { |
| 381 | // These are errors that are not likely to go away even if we |
| 382 | // retry, i.e. the server doesn't support range requests or similar. |
| 383 | mNumRetriesLeft = 0; |
| 384 | } |
| 385 | |
Mark Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 386 | ALOGE("source returned error %zd, %d retries left", n, mNumRetriesLeft); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 387 | mCache->releasePage(page); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 388 | } else { |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 389 | if (mFinalStatus != OK) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 390 | ALOGI("retrying a previously failed read succeeded."); |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 391 | } |
| 392 | mNumRetriesLeft = kMaxNumRetries; |
| 393 | mFinalStatus = OK; |
| 394 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 395 | page->mSize = n; |
| 396 | mCache->appendPage(page); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void NuCachedSource2::onFetch() { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 401 | ALOGV("onFetch"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 402 | |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 403 | if (mFinalStatus != OK && mNumRetriesLeft == 0) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 404 | ALOGV("EOS reached, done prefetching for now"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 405 | mFetching = false; |
| 406 | } |
| 407 | |
Andreas Huber | a5273eb | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 408 | bool keepAlive = |
| 409 | !mFetching |
| 410 | && mFinalStatus == OK |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 411 | && mKeepAliveIntervalUs > 0 |
| 412 | && ALooper::GetNowUs() >= mLastFetchTimeUs + mKeepAliveIntervalUs; |
Andreas Huber | a5273eb | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 413 | |
| 414 | if (mFetching || keepAlive) { |
| 415 | if (keepAlive) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 416 | ALOGI("Keep alive"); |
Andreas Huber | a5273eb | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 419 | fetchInternal(); |
| 420 | |
Andreas Huber | a5273eb | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 421 | mLastFetchTimeUs = ALooper::GetNowUs(); |
| 422 | |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 423 | if (mFetching && mCache->totalSize() >= mHighwaterThresholdBytes) { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 424 | ALOGI("Cache full, done prefetching for now"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 425 | mFetching = false; |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 426 | |
| 427 | if (mDisconnectAtHighwatermark |
| 428 | && (mSource->flags() & DataSource::kIsHTTPBasedSource)) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 429 | ALOGV("Disconnecting at high watermark"); |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 430 | static_cast<HTTPBase *>(mSource.get())->disconnect(); |
Bryan Mawhinney | 40a4e14 | 2012-01-18 13:40:07 +0000 | [diff] [blame] | 431 | mFinalStatus = -EAGAIN; |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 432 | } |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 433 | } |
Andreas Huber | a44153c | 2010-12-03 16:12:25 -0800 | [diff] [blame] | 434 | } else { |
Andreas Huber | d17875a | 2010-06-11 14:14:52 -0700 | [diff] [blame] | 435 | Mutex::Autolock autoLock(mLock); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 436 | restartPrefetcherIfNecessary_l(); |
| 437 | } |
| 438 | |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 439 | int64_t delayUs; |
| 440 | if (mFetching) { |
| 441 | if (mFinalStatus != OK && mNumRetriesLeft > 0) { |
| 442 | // We failed this time and will try again in 3 seconds. |
Chih-Hung Hsieh | 3794b24 | 2018-12-11 13:55:06 -0800 | [diff] [blame] | 443 | delayUs = 3000000LL; |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 444 | } else { |
| 445 | delayUs = 0; |
| 446 | } |
| 447 | } else { |
Chih-Hung Hsieh | 3794b24 | 2018-12-11 13:55:06 -0800 | [diff] [blame] | 448 | delayUs = 100000LL; |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 451 | (new AMessage(kWhatFetchMore, mReflector))->post(delayUs); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void NuCachedSource2::onRead(const sp<AMessage> &msg) { |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 455 | ALOGV("onRead"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 456 | |
| 457 | int64_t offset; |
| 458 | CHECK(msg->findInt64("offset", &offset)); |
| 459 | |
| 460 | void *data; |
| 461 | CHECK(msg->findPointer("data", &data)); |
| 462 | |
| 463 | size_t size; |
| 464 | CHECK(msg->findSize("size", &size)); |
| 465 | |
| 466 | ssize_t result = readInternal(offset, data, size); |
| 467 | |
| 468 | if (result == -EAGAIN) { |
| 469 | msg->post(50000); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | Mutex::Autolock autoLock(mLock); |
Robert Shih | 4f17dad | 2014-09-30 14:17:38 -0700 | [diff] [blame] | 474 | if (mDisconnecting) { |
| 475 | mCondition.signal(); |
| 476 | return; |
| 477 | } |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 478 | |
| 479 | CHECK(mAsyncResult == NULL); |
| 480 | |
| 481 | mAsyncResult = new AMessage; |
| 482 | mAsyncResult->setInt32("result", result); |
| 483 | |
| 484 | mCondition.signal(); |
| 485 | } |
| 486 | |
Andreas Huber | 34ef0f3 | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 487 | void NuCachedSource2::restartPrefetcherIfNecessary_l( |
Andreas Huber | 7bf8413 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 488 | bool ignoreLowWaterThreshold, bool force) { |
James Dong | 6ee9458 | 2011-01-14 15:15:12 -0800 | [diff] [blame] | 489 | static const size_t kGrayArea = 1024 * 1024; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 490 | |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 491 | if (mFetching || (mFinalStatus != OK && mNumRetriesLeft == 0)) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
Andreas Huber | 7bf8413 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 495 | if (!ignoreLowWaterThreshold && !force |
Andreas Huber | 34ef0f3 | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 496 | && mCacheOffset + mCache->totalSize() - mLastAccessPos |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 497 | >= mLowwaterThresholdBytes) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 498 | return; |
| 499 | } |
| 500 | |
| 501 | size_t maxBytes = mLastAccessPos - mCacheOffset; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 502 | |
Andreas Huber | 7bf8413 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 503 | if (!force) { |
| 504 | if (maxBytes < kGrayArea) { |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | maxBytes -= kGrayArea; |
| 509 | } |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 510 | |
| 511 | size_t actualBytes = mCache->releaseFromStart(maxBytes); |
| 512 | mCacheOffset += actualBytes; |
| 513 | |
Mark Salyzyn | a5750e0 | 2014-06-18 16:34:45 -0700 | [diff] [blame] | 514 | ALOGI("restarting prefetcher, totalSize = %zu", mCache->totalSize()); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 515 | mFetching = true; |
| 516 | } |
| 517 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 518 | ssize_t NuCachedSource2::readAt(off64_t offset, void *data, size_t size) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 519 | Mutex::Autolock autoSerializer(mSerializer); |
| 520 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 521 | ALOGV("readAt offset %lld, size %zu", (long long)offset, size); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 522 | |
| 523 | Mutex::Autolock autoLock(mLock); |
Robert Shih | 4f17dad | 2014-09-30 14:17:38 -0700 | [diff] [blame] | 524 | if (mDisconnecting) { |
| 525 | return ERROR_END_OF_STREAM; |
| 526 | } |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 527 | |
| 528 | // If the request can be completely satisfied from the cache, do so. |
| 529 | |
| 530 | if (offset >= mCacheOffset |
| 531 | && offset + size <= mCacheOffset + mCache->totalSize()) { |
| 532 | size_t delta = offset - mCacheOffset; |
| 533 | mCache->copy(delta, data, size); |
| 534 | |
| 535 | mLastAccessPos = offset + size; |
| 536 | |
| 537 | return size; |
| 538 | } |
| 539 | |
Lajos Molnar | 1d15ab5 | 2015-03-04 16:46:34 -0800 | [diff] [blame] | 540 | sp<AMessage> msg = new AMessage(kWhatRead, mReflector); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 541 | msg->setInt64("offset", offset); |
| 542 | msg->setPointer("data", data); |
| 543 | msg->setSize("size", size); |
| 544 | |
| 545 | CHECK(mAsyncResult == NULL); |
| 546 | msg->post(); |
| 547 | |
Chong Zhang | 9f3d1cf | 2014-09-23 22:22:30 -0700 | [diff] [blame] | 548 | while (mAsyncResult == NULL && !mDisconnecting) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 549 | mCondition.wait(mLock); |
| 550 | } |
| 551 | |
Chong Zhang | 9f3d1cf | 2014-09-23 22:22:30 -0700 | [diff] [blame] | 552 | if (mDisconnecting) { |
Robert Shih | 4f17dad | 2014-09-30 14:17:38 -0700 | [diff] [blame] | 553 | mAsyncResult.clear(); |
Chong Zhang | 9f3d1cf | 2014-09-23 22:22:30 -0700 | [diff] [blame] | 554 | return ERROR_END_OF_STREAM; |
| 555 | } |
| 556 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 557 | int32_t result; |
| 558 | CHECK(mAsyncResult->findInt32("result", &result)); |
| 559 | |
| 560 | mAsyncResult.clear(); |
| 561 | |
| 562 | if (result > 0) { |
| 563 | mLastAccessPos = offset + result; |
| 564 | } |
| 565 | |
| 566 | return (ssize_t)result; |
| 567 | } |
| 568 | |
| 569 | size_t NuCachedSource2::cachedSize() { |
| 570 | Mutex::Autolock autoLock(mLock); |
| 571 | return mCacheOffset + mCache->totalSize(); |
| 572 | } |
| 573 | |
Robert Shih | 3f65764 | 2018-09-27 12:22:23 -0700 | [diff] [blame] | 574 | status_t NuCachedSource2::getAvailableSize(off64_t offset, off64_t *size) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 575 | Mutex::Autolock autoLock(mLock); |
Robert Shih | 3f65764 | 2018-09-27 12:22:23 -0700 | [diff] [blame] | 576 | status_t finalStatus = UNKNOWN_ERROR; |
| 577 | *size = approxDataRemaining_l(offset, &finalStatus); |
| 578 | return finalStatus; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Robert Shih | 3f65764 | 2018-09-27 12:22:23 -0700 | [diff] [blame] | 581 | size_t NuCachedSource2::approxDataRemaining(status_t *finalStatus) const { |
| 582 | Mutex::Autolock autoLock(mLock); |
| 583 | return approxDataRemaining_l(mLastAccessPos, finalStatus); |
| 584 | } |
| 585 | |
| 586 | size_t NuCachedSource2::approxDataRemaining_l(off64_t offset, status_t *finalStatus) const { |
Bryan Mawhinney | 1bd233c | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 587 | *finalStatus = mFinalStatus; |
Andreas Huber | 0683eba | 2011-07-18 13:47:55 -0700 | [diff] [blame] | 588 | |
| 589 | if (mFinalStatus != OK && mNumRetriesLeft > 0) { |
| 590 | // Pretend that everything is fine until we're out of retries. |
| 591 | *finalStatus = OK; |
| 592 | } |
| 593 | |
Robert Shih | 3f65764 | 2018-09-27 12:22:23 -0700 | [diff] [blame] | 594 | offset = offset >= 0 ? offset : mLastAccessPos; |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 595 | off64_t lastBytePosCached = mCacheOffset + mCache->totalSize(); |
Robert Shih | 3f65764 | 2018-09-27 12:22:23 -0700 | [diff] [blame] | 596 | if (offset < lastBytePosCached) { |
| 597 | return lastBytePosCached - offset; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 602 | ssize_t NuCachedSource2::readInternal(off64_t offset, void *data, size_t size) { |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 603 | CHECK_LE(size, (size_t)mHighwaterThresholdBytes); |
Andreas Huber | 7bf8413 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 604 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 605 | ALOGV("readInternal offset %lld size %zu", (long long)offset, size); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 606 | |
| 607 | Mutex::Autolock autoLock(mLock); |
| 608 | |
Chong Zhang | 2c878cf | 2015-05-19 10:56:40 -0700 | [diff] [blame] | 609 | // If we're disconnecting, return EOS and don't access *data pointer. |
| 610 | // data could be on the stack of the caller to NuCachedSource2::readAt(), |
| 611 | // which may have exited already. |
| 612 | if (mDisconnecting) { |
| 613 | return ERROR_END_OF_STREAM; |
| 614 | } |
| 615 | |
Andreas Huber | 7bf8413 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 616 | if (!mFetching) { |
| 617 | mLastAccessPos = offset; |
| 618 | restartPrefetcherIfNecessary_l( |
| 619 | false, // ignoreLowWaterThreshold |
| 620 | true); // force |
| 621 | } |
| 622 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 623 | if (offset < mCacheOffset |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 624 | || offset >= (off64_t)(mCacheOffset + mCache->totalSize())) { |
James Dong | 6ee9458 | 2011-01-14 15:15:12 -0800 | [diff] [blame] | 625 | static const off64_t kPadding = 256 * 1024; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 626 | |
| 627 | // In the presence of multiple decoded streams, once of them will |
| 628 | // trigger this seek request, the other one will request data "nearby" |
| 629 | // soon, adjust the seek position so that that subsequent request |
| 630 | // does not trigger another seek. |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 631 | off64_t seekOffset = (offset > kPadding) ? offset - kPadding : 0; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 632 | |
| 633 | seekInternal_l(seekOffset); |
| 634 | } |
| 635 | |
| 636 | size_t delta = offset - mCacheOffset; |
| 637 | |
Bryan Mawhinney | 40a4e14 | 2012-01-18 13:40:07 +0000 | [diff] [blame] | 638 | if (mFinalStatus != OK && mNumRetriesLeft == 0) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 639 | if (delta >= mCache->totalSize()) { |
| 640 | return mFinalStatus; |
| 641 | } |
| 642 | |
Andreas Huber | 6f5aae1 | 2010-06-11 09:22:48 -0700 | [diff] [blame] | 643 | size_t avail = mCache->totalSize() - delta; |
Andreas Huber | 6780297 | 2011-05-04 11:43:43 -0700 | [diff] [blame] | 644 | |
| 645 | if (avail > size) { |
| 646 | avail = size; |
| 647 | } |
| 648 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 649 | mCache->copy(delta, data, avail); |
| 650 | |
| 651 | return avail; |
| 652 | } |
| 653 | |
| 654 | if (offset + size <= mCacheOffset + mCache->totalSize()) { |
| 655 | mCache->copy(delta, data, size); |
| 656 | |
| 657 | return size; |
| 658 | } |
| 659 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 660 | ALOGV("deferring read"); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 661 | |
| 662 | return -EAGAIN; |
| 663 | } |
| 664 | |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 665 | status_t NuCachedSource2::seekInternal_l(off64_t offset) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 666 | mLastAccessPos = offset; |
| 667 | |
| 668 | if (offset >= mCacheOffset |
James Dong | c7fc37a | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 669 | && offset <= (off64_t)(mCacheOffset + mCache->totalSize())) { |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 670 | return OK; |
| 671 | } |
| 672 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 673 | ALOGI("new range: offset= %lld", (long long)offset); |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 674 | |
| 675 | mCacheOffset = offset; |
| 676 | |
| 677 | size_t totalSize = mCache->totalSize(); |
| 678 | CHECK_EQ(mCache->releaseFromStart(totalSize), totalSize); |
| 679 | |
Bryan Mawhinney | 40a4e14 | 2012-01-18 13:40:07 +0000 | [diff] [blame] | 680 | mNumRetriesLeft = kMaxNumRetries; |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 681 | mFetching = true; |
| 682 | |
| 683 | return OK; |
| 684 | } |
| 685 | |
Andreas Huber | 34ef0f3 | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 686 | void NuCachedSource2::resumeFetchingIfNecessary() { |
| 687 | Mutex::Autolock autoLock(mLock); |
| 688 | |
| 689 | restartPrefetcherIfNecessary_l(true /* ignore low water threshold */); |
| 690 | } |
| 691 | |
James Dong | 9d2f386 | 2012-01-10 08:24:37 -0800 | [diff] [blame] | 692 | sp<DecryptHandle> NuCachedSource2::DrmInitialization(const char* mime) { |
| 693 | return mSource->DrmInitialization(mime); |
Gloria Wang | b371426 | 2010-11-01 15:53:16 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Gloria Wang | 771b85d | 2010-11-09 15:06:51 -0800 | [diff] [blame] | 696 | String8 NuCachedSource2::getUri() { |
| 697 | return mSource->getUri(); |
| 698 | } |
Andreas Huber | ac05c31 | 2011-01-19 15:07:19 -0800 | [diff] [blame] | 699 | |
Andreas Huber | 6511c97 | 2011-03-30 11:15:27 -0700 | [diff] [blame] | 700 | String8 NuCachedSource2::getMIMEType() const { |
| 701 | return mSource->getMIMEType(); |
| 702 | } |
| 703 | |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 704 | void NuCachedSource2::updateCacheParamsFromSystemProperty() { |
| 705 | char value[PROPERTY_VALUE_MAX]; |
| 706 | if (!property_get("media.stagefright.cache-params", value, NULL)) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | updateCacheParamsFromString(value); |
| 711 | } |
| 712 | |
| 713 | void NuCachedSource2::updateCacheParamsFromString(const char *s) { |
| 714 | ssize_t lowwaterMarkKb, highwaterMarkKb; |
Andreas Huber | 0b8cd8b | 2011-10-07 10:00:38 -0700 | [diff] [blame] | 715 | int keepAliveSecs; |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 716 | |
Mark Salyzyn | db43b34 | 2014-04-04 14:47:28 -0700 | [diff] [blame] | 717 | if (sscanf(s, "%zd/%zd/%d", |
Andreas Huber | 0b8cd8b | 2011-10-07 10:00:38 -0700 | [diff] [blame] | 718 | &lowwaterMarkKb, &highwaterMarkKb, &keepAliveSecs) != 3) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 719 | ALOGE("Failed to parse cache parameters from '%s'.", s); |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
| 723 | if (lowwaterMarkKb >= 0) { |
| 724 | mLowwaterThresholdBytes = lowwaterMarkKb * 1024; |
| 725 | } else { |
| 726 | mLowwaterThresholdBytes = kDefaultLowWaterThreshold; |
| 727 | } |
| 728 | |
| 729 | if (highwaterMarkKb >= 0) { |
| 730 | mHighwaterThresholdBytes = highwaterMarkKb * 1024; |
| 731 | } else { |
| 732 | mHighwaterThresholdBytes = kDefaultHighWaterThreshold; |
| 733 | } |
| 734 | |
Andreas Huber | 0b8cd8b | 2011-10-07 10:00:38 -0700 | [diff] [blame] | 735 | if (mLowwaterThresholdBytes >= mHighwaterThresholdBytes) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 736 | ALOGE("Illegal low/highwater marks specified, reverting to defaults."); |
Andreas Huber | 0b8cd8b | 2011-10-07 10:00:38 -0700 | [diff] [blame] | 737 | |
| 738 | mLowwaterThresholdBytes = kDefaultLowWaterThreshold; |
| 739 | mHighwaterThresholdBytes = kDefaultHighWaterThreshold; |
| 740 | } |
| 741 | |
| 742 | if (keepAliveSecs >= 0) { |
Chih-Hung Hsieh | 3794b24 | 2018-12-11 13:55:06 -0800 | [diff] [blame] | 743 | mKeepAliveIntervalUs = keepAliveSecs * 1000000LL; |
Andreas Huber | 0b8cd8b | 2011-10-07 10:00:38 -0700 | [diff] [blame] | 744 | } else { |
| 745 | mKeepAliveIntervalUs = kDefaultKeepAliveIntervalUs; |
| 746 | } |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 747 | |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 748 | ALOGV("lowwater = %zu bytes, highwater = %zu bytes, keepalive = %lld us", |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 749 | mLowwaterThresholdBytes, |
| 750 | mHighwaterThresholdBytes, |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 751 | (long long)mKeepAliveIntervalUs); |
Andreas Huber | a045cb0 | 2011-10-05 14:32:17 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 754 | // static |
| 755 | void NuCachedSource2::RemoveCacheSpecificHeaders( |
| 756 | KeyedVector<String8, String8> *headers, |
| 757 | String8 *cacheConfig, |
| 758 | bool *disconnectAtHighwatermark) { |
| 759 | *cacheConfig = String8(); |
| 760 | *disconnectAtHighwatermark = false; |
| 761 | |
| 762 | if (headers == NULL) { |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | ssize_t index; |
| 767 | if ((index = headers->indexOfKey(String8("x-cache-config"))) >= 0) { |
| 768 | *cacheConfig = headers->valueAt(index); |
| 769 | |
| 770 | headers->removeItemsAt(index); |
| 771 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 772 | ALOGV("Using special cache config '%s'", cacheConfig->string()); |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if ((index = headers->indexOfKey( |
| 776 | String8("x-disconnect-at-highwatermark"))) >= 0) { |
| 777 | *disconnectAtHighwatermark = true; |
| 778 | headers->removeItemsAt(index); |
| 779 | |
Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 780 | ALOGV("Client requested disconnection at highwater mark"); |
Andreas Huber | 49c5981 | 2011-10-07 13:40:45 -0700 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Andreas Huber | 5994b47 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 784 | } // namespace android |