NuCachedSource2.h revision 5994b4798b01f3dd340577c9ea9657f09093a770
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#ifndef NU_CACHED_SOURCE_2_H_ 18 19#define NU_CACHED_SOURCE_2_H_ 20 21#include <media/stagefright/foundation/ABase.h> 22#include <media/stagefright/foundation/AHandlerReflector.h> 23#include <media/stagefright/DataSource.h> 24 25namespace android { 26 27struct ALooper; 28struct PageCache; 29 30struct NuCachedSource2 : public DataSource { 31 NuCachedSource2(const sp<DataSource> &source); 32 33 virtual status_t initCheck() const; 34 35 virtual ssize_t readAt(off_t offset, void *data, size_t size); 36 37 virtual status_t getSize(off_t *size); 38 virtual uint32_t flags(); 39 40 //////////////////////////////////////////////////////////////////////////// 41 42 size_t cachedSize(); 43 size_t approxDataRemaining(bool *eos); 44 45protected: 46 virtual ~NuCachedSource2(); 47 48private: 49 friend struct AHandlerReflector<NuCachedSource2>; 50 51 enum { 52 kPageSize = 16384, 53 kHighWaterThreshold = 3 * 1024 * 1024, 54 kLowWaterThreshold = 512 * 1024, 55 }; 56 57 enum { 58 kWhatFetchMore = 'fetc', 59 kWhatRead = 'read', 60 }; 61 62 sp<DataSource> mSource; 63 sp<AHandlerReflector<NuCachedSource2> > mReflector; 64 sp<ALooper> mLooper; 65 66 Mutex mSerializer; 67 Mutex mLock; 68 Condition mCondition; 69 70 PageCache *mCache; 71 off_t mCacheOffset; 72 status_t mFinalStatus; 73 off_t mLastAccessPos; 74 sp<AMessage> mAsyncResult; 75 bool mFetching; 76 77 void onMessageReceived(const sp<AMessage> &msg); 78 void onFetch(); 79 void onRead(const sp<AMessage> &msg); 80 81 void fetchInternal(); 82 ssize_t readInternal(off_t offset, void *data, size_t size); 83 status_t seekInternal_l(off_t offset); 84 85 size_t approxDataRemaining_l(bool *eos); 86 void restartPrefetcherIfNecessary_l(); 87 88 DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2); 89}; 90 91} // namespace android 92 93#endif // NU_CACHED_SOURCE_2_H_ 94