NuCachedSource2.cpp revision a5273ebd1746368662a597643d6701a5046d5c7b
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
17#define LOG_TAG "NuCachedSource2"
18#include <utils/Log.h>
19
20#include "include/NuCachedSource2.h"
21
22#include <media/stagefright/foundation/ADebug.h>
23#include <media/stagefright/foundation/AMessage.h>
24#include <media/stagefright/MediaErrors.h>
25
26namespace android {
27
28struct PageCache {
29    PageCache(size_t pageSize);
30    ~PageCache();
31
32    struct Page {
33        void *mData;
34        size_t mSize;
35    };
36
37    Page *acquirePage();
38    void releasePage(Page *page);
39
40    void appendPage(Page *page);
41    size_t releaseFromStart(size_t maxBytes);
42
43    size_t totalSize() const {
44        return mTotalSize;
45    }
46
47    void copy(size_t from, void *data, size_t size);
48
49private:
50    size_t mPageSize;
51    size_t mTotalSize;
52
53    List<Page *> mActivePages;
54    List<Page *> mFreePages;
55
56    void freePages(List<Page *> *list);
57
58    DISALLOW_EVIL_CONSTRUCTORS(PageCache);
59};
60
61PageCache::PageCache(size_t pageSize)
62    : mPageSize(pageSize),
63      mTotalSize(0) {
64}
65
66PageCache::~PageCache() {
67    freePages(&mActivePages);
68    freePages(&mFreePages);
69}
70
71void PageCache::freePages(List<Page *> *list) {
72    List<Page *>::iterator it = list->begin();
73    while (it != list->end()) {
74        Page *page = *it;
75
76        free(page->mData);
77        delete page;
78        page = NULL;
79
80        ++it;
81    }
82}
83
84PageCache::Page *PageCache::acquirePage() {
85    if (!mFreePages.empty()) {
86        List<Page *>::iterator it = mFreePages.begin();
87        Page *page = *it;
88        mFreePages.erase(it);
89
90        return page;
91    }
92
93    Page *page = new Page;
94    page->mData = malloc(mPageSize);
95    page->mSize = 0;
96
97    return page;
98}
99
100void PageCache::releasePage(Page *page) {
101    page->mSize = 0;
102    mFreePages.push_back(page);
103}
104
105void PageCache::appendPage(Page *page) {
106    mTotalSize += page->mSize;
107    mActivePages.push_back(page);
108}
109
110size_t PageCache::releaseFromStart(size_t maxBytes) {
111    size_t bytesReleased = 0;
112
113    while (maxBytes > 0 && !mActivePages.empty()) {
114        List<Page *>::iterator it = mActivePages.begin();
115
116        Page *page = *it;
117
118        if (maxBytes < page->mSize) {
119            break;
120        }
121
122        mActivePages.erase(it);
123
124        maxBytes -= page->mSize;
125        bytesReleased += page->mSize;
126
127        releasePage(page);
128    }
129
130    mTotalSize -= bytesReleased;
131    return bytesReleased;
132}
133
134void PageCache::copy(size_t from, void *data, size_t size) {
135    LOG(VERBOSE) << "copy from " << from << " size " << size;
136
137    CHECK_LE(from + size, mTotalSize);
138
139    size_t offset = 0;
140    List<Page *>::iterator it = mActivePages.begin();
141    while (from >= offset + (*it)->mSize) {
142        offset += (*it)->mSize;
143        ++it;
144    }
145
146    size_t delta = from - offset;
147    size_t avail = (*it)->mSize - delta;
148
149    if (avail >= size) {
150        memcpy(data, (const uint8_t *)(*it)->mData + delta, size);
151        return;
152    }
153
154    memcpy(data, (const uint8_t *)(*it)->mData + delta, avail);
155    ++it;
156    data = (uint8_t *)data + avail;
157    size -= avail;
158
159    while (size > 0) {
160        size_t copy = (*it)->mSize;
161        if (copy > size) {
162            copy = size;
163        }
164        memcpy(data, (*it)->mData, copy);
165        data = (uint8_t *)data + copy;
166        size -= copy;
167        ++it;
168    }
169}
170
171////////////////////////////////////////////////////////////////////////////////
172
173NuCachedSource2::NuCachedSource2(const sp<DataSource> &source)
174    : mSource(source),
175      mReflector(new AHandlerReflector<NuCachedSource2>(this)),
176      mLooper(new ALooper),
177      mCache(new PageCache(kPageSize)),
178      mCacheOffset(0),
179      mFinalStatus(OK),
180      mLastAccessPos(0),
181      mFetching(true),
182      mLastFetchTimeUs(-1) {
183    mLooper->registerHandler(mReflector);
184    mLooper->start();
185
186    Mutex::Autolock autoLock(mLock);
187    (new AMessage(kWhatFetchMore, mReflector->id()))->post();
188}
189
190NuCachedSource2::~NuCachedSource2() {
191    mLooper->stop();
192    mLooper->unregisterHandler(mReflector->id());
193
194    delete mCache;
195    mCache = NULL;
196}
197
198status_t NuCachedSource2::initCheck() const {
199    return mSource->initCheck();
200}
201
202status_t NuCachedSource2::getSize(off_t *size) {
203    return mSource->getSize(size);
204}
205
206uint32_t NuCachedSource2::flags() {
207    return mSource->flags();
208}
209
210void NuCachedSource2::onMessageReceived(const sp<AMessage> &msg) {
211    switch (msg->what()) {
212        case kWhatFetchMore:
213        {
214            onFetch();
215            break;
216        }
217
218        case kWhatRead:
219        {
220            onRead(msg);
221            break;
222        }
223
224        default:
225            TRESPASS();
226    }
227}
228
229void NuCachedSource2::fetchInternal() {
230    LOG(VERBOSE) << "fetchInternal";
231
232    CHECK_EQ(mFinalStatus, (status_t)OK);
233
234    PageCache::Page *page = mCache->acquirePage();
235
236    ssize_t n = mSource->readAt(
237            mCacheOffset + mCache->totalSize(), page->mData, kPageSize);
238
239    Mutex::Autolock autoLock(mLock);
240
241    if (n < 0) {
242        LOG(ERROR) << "source returned error " << n;
243        mFinalStatus = n;
244        mCache->releasePage(page);
245    } else if (n == 0) {
246        LOG(INFO) << "ERROR_END_OF_STREAM";
247        mFinalStatus = ERROR_END_OF_STREAM;
248        mCache->releasePage(page);
249    } else {
250        page->mSize = n;
251        mCache->appendPage(page);
252    }
253}
254
255void NuCachedSource2::onFetch() {
256    LOG(VERBOSE) << "onFetch";
257
258    if (mFinalStatus != OK) {
259        LOG(VERBOSE) << "EOS reached, done prefetching for now";
260        mFetching = false;
261    }
262
263    bool keepAlive =
264        !mFetching
265            && mFinalStatus == OK
266            && ALooper::GetNowUs() >= mLastFetchTimeUs + kKeepAliveIntervalUs;
267
268    if (mFetching || keepAlive) {
269        if (keepAlive) {
270            LOG(INFO) << "Keep alive";
271        }
272
273        fetchInternal();
274
275        mLastFetchTimeUs = ALooper::GetNowUs();
276
277        if (mFetching && mCache->totalSize() >= kHighWaterThreshold) {
278            LOG(INFO) << "Cache full, done prefetching for now";
279            mFetching = false;
280        }
281    } else {
282        Mutex::Autolock autoLock(mLock);
283        restartPrefetcherIfNecessary_l();
284    }
285
286    (new AMessage(kWhatFetchMore, mReflector->id()))->post(
287            mFetching ? 0 : 100000ll);
288}
289
290void NuCachedSource2::onRead(const sp<AMessage> &msg) {
291    LOG(VERBOSE) << "onRead";
292
293    int64_t offset;
294    CHECK(msg->findInt64("offset", &offset));
295
296    void *data;
297    CHECK(msg->findPointer("data", &data));
298
299    size_t size;
300    CHECK(msg->findSize("size", &size));
301
302    ssize_t result = readInternal(offset, data, size);
303
304    if (result == -EAGAIN) {
305        msg->post(50000);
306        return;
307    }
308
309    Mutex::Autolock autoLock(mLock);
310
311    CHECK(mAsyncResult == NULL);
312
313    mAsyncResult = new AMessage;
314    mAsyncResult->setInt32("result", result);
315
316    mCondition.signal();
317}
318
319void NuCachedSource2::restartPrefetcherIfNecessary_l() {
320    static const size_t kGrayArea = 256 * 1024;
321
322    if (mFetching || mFinalStatus != OK) {
323        return;
324    }
325
326    if (mCacheOffset + mCache->totalSize() - mLastAccessPos
327            >= kLowWaterThreshold) {
328        return;
329    }
330
331    size_t maxBytes = mLastAccessPos - mCacheOffset;
332    if (maxBytes < kGrayArea) {
333        return;
334    }
335
336    maxBytes -= kGrayArea;
337
338    size_t actualBytes = mCache->releaseFromStart(maxBytes);
339    mCacheOffset += actualBytes;
340
341    LOG(INFO) << "restarting prefetcher, totalSize = " << mCache->totalSize();
342    mFetching = true;
343}
344
345ssize_t NuCachedSource2::readAt(off_t offset, void *data, size_t size) {
346    Mutex::Autolock autoSerializer(mSerializer);
347
348    LOG(VERBOSE) << "readAt offset " << offset << " size " << size;
349
350    Mutex::Autolock autoLock(mLock);
351
352    // If the request can be completely satisfied from the cache, do so.
353
354    if (offset >= mCacheOffset
355            && offset + size <= mCacheOffset + mCache->totalSize()) {
356        size_t delta = offset - mCacheOffset;
357        mCache->copy(delta, data, size);
358
359        mLastAccessPos = offset + size;
360
361        return size;
362    }
363
364    sp<AMessage> msg = new AMessage(kWhatRead, mReflector->id());
365    msg->setInt64("offset", offset);
366    msg->setPointer("data", data);
367    msg->setSize("size", size);
368
369    CHECK(mAsyncResult == NULL);
370    msg->post();
371
372    while (mAsyncResult == NULL) {
373        mCondition.wait(mLock);
374    }
375
376    int32_t result;
377    CHECK(mAsyncResult->findInt32("result", &result));
378
379    mAsyncResult.clear();
380
381    if (result > 0) {
382        mLastAccessPos = offset + result;
383    }
384
385    return (ssize_t)result;
386}
387
388size_t NuCachedSource2::cachedSize() {
389    Mutex::Autolock autoLock(mLock);
390    return mCacheOffset + mCache->totalSize();
391}
392
393size_t NuCachedSource2::approxDataRemaining(bool *eos) {
394    Mutex::Autolock autoLock(mLock);
395    return approxDataRemaining_l(eos);
396}
397
398size_t NuCachedSource2::approxDataRemaining_l(bool *eos) {
399    *eos = (mFinalStatus != OK);
400    off_t lastBytePosCached = mCacheOffset + mCache->totalSize();
401    if (mLastAccessPos < lastBytePosCached) {
402        return lastBytePosCached - mLastAccessPos;
403    }
404    return 0;
405}
406
407ssize_t NuCachedSource2::readInternal(off_t offset, void *data, size_t size) {
408    LOG(VERBOSE) << "readInternal offset " << offset << " size " << size;
409
410    Mutex::Autolock autoLock(mLock);
411
412    if (offset < mCacheOffset
413            || offset >= (off_t)(mCacheOffset + mCache->totalSize())) {
414        static const off_t kPadding = 32768;
415
416        // In the presence of multiple decoded streams, once of them will
417        // trigger this seek request, the other one will request data "nearby"
418        // soon, adjust the seek position so that that subsequent request
419        // does not trigger another seek.
420        off_t seekOffset = (offset > kPadding) ? offset - kPadding : 0;
421
422        seekInternal_l(seekOffset);
423    }
424
425    size_t delta = offset - mCacheOffset;
426
427    if (mFinalStatus != OK) {
428        if (delta >= mCache->totalSize()) {
429            return mFinalStatus;
430        }
431
432        size_t avail = mCache->totalSize() - delta;
433        mCache->copy(delta, data, avail);
434
435        return avail;
436    }
437
438    if (offset + size <= mCacheOffset + mCache->totalSize()) {
439        mCache->copy(delta, data, size);
440
441        return size;
442    }
443
444    LOG(VERBOSE) << "deferring read";
445
446    return -EAGAIN;
447}
448
449status_t NuCachedSource2::seekInternal_l(off_t offset) {
450    mLastAccessPos = offset;
451
452    if (offset >= mCacheOffset
453            && offset <= (off_t)(mCacheOffset + mCache->totalSize())) {
454        return OK;
455    }
456
457    LOG(INFO) << "new range: offset= " << offset;
458
459    mCacheOffset = offset;
460
461    size_t totalSize = mCache->totalSize();
462    CHECK_EQ(mCache->releaseFromStart(totalSize), totalSize);
463
464    mFinalStatus = OK;
465    mFetching = true;
466
467    return OK;
468}
469
470}  // namespace android
471
472