Andreas Huber | b206319 | 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 | |
| 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 | |
| 25 | namespace android { |
| 26 | |
| 27 | struct ALooper; |
| 28 | struct PageCache; |
| 29 | |
| 30 | struct NuCachedSource2 : public DataSource { |
| 31 | NuCachedSource2(const sp<DataSource> &source); |
| 32 | |
| 33 | virtual status_t initCheck() const; |
| 34 | |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 35 | virtual ssize_t readAt(off64_t offset, void *data, size_t size); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 36 | |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 37 | virtual status_t getSize(off64_t *size); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 38 | virtual uint32_t flags(); |
| 39 | |
Gloria Wang | ae77527 | 2011-02-24 16:40:57 -0800 | [diff] [blame] | 40 | virtual sp<DecryptHandle> DrmInitialization(); |
| 41 | virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client); |
Gloria Wang | 43cd12d | 2010-11-09 15:06:51 -0800 | [diff] [blame] | 42 | virtual String8 getUri(); |
Andreas Huber | a2e57ca | 2011-03-30 11:15:27 -0700 | [diff] [blame] | 43 | |
| 44 | virtual String8 getMIMEType() const; |
| 45 | |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 46 | //////////////////////////////////////////////////////////////////////////// |
| 47 | |
| 48 | size_t cachedSize(); |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 49 | size_t approxDataRemaining(status_t *finalStatus); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 50 | |
Andreas Huber | 10b920c | 2010-11-11 15:37:17 -0800 | [diff] [blame] | 51 | void resumeFetchingIfNecessary(); |
| 52 | |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 53 | protected: |
| 54 | virtual ~NuCachedSource2(); |
| 55 | |
| 56 | private: |
| 57 | friend struct AHandlerReflector<NuCachedSource2>; |
| 58 | |
| 59 | enum { |
Andreas Huber | 909a8cf | 2010-06-25 10:44:24 -0700 | [diff] [blame] | 60 | kPageSize = 65536, |
Andreas Huber | 52c7832 | 2011-01-11 15:05:28 -0800 | [diff] [blame] | 61 | kHighWaterThreshold = 20 * 1024 * 1024, |
| 62 | kLowWaterThreshold = 4 * 1024 * 1024, |
Andreas Huber | a4867540 | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 63 | |
| 64 | // Read data after a 15 sec timeout whether we're actively |
| 65 | // fetching or not. |
| 66 | kKeepAliveIntervalUs = 15000000, |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | enum { |
| 70 | kWhatFetchMore = 'fetc', |
| 71 | kWhatRead = 'read', |
| 72 | }; |
| 73 | |
| 74 | sp<DataSource> mSource; |
| 75 | sp<AHandlerReflector<NuCachedSource2> > mReflector; |
| 76 | sp<ALooper> mLooper; |
| 77 | |
| 78 | Mutex mSerializer; |
| 79 | Mutex mLock; |
| 80 | Condition mCondition; |
| 81 | |
| 82 | PageCache *mCache; |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 83 | off64_t mCacheOffset; |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 84 | status_t mFinalStatus; |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 85 | off64_t mLastAccessPos; |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 86 | sp<AMessage> mAsyncResult; |
| 87 | bool mFetching; |
Andreas Huber | a4867540 | 2010-06-22 08:57:34 -0700 | [diff] [blame] | 88 | int64_t mLastFetchTimeUs; |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 89 | |
| 90 | void onMessageReceived(const sp<AMessage> &msg); |
| 91 | void onFetch(); |
| 92 | void onRead(const sp<AMessage> &msg); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 93 | |
| 94 | void fetchInternal(); |
James Dong | b1262a8 | 2010-11-16 14:04:54 -0800 | [diff] [blame] | 95 | ssize_t readInternal(off64_t offset, void *data, size_t size); |
| 96 | status_t seekInternal_l(off64_t offset); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 97 | |
Bryan Mawhinney | e26275b | 2011-01-18 19:12:21 +0000 | [diff] [blame] | 98 | size_t approxDataRemaining_l(status_t *finalStatus); |
Andreas Huber | bb32a94 | 2011-04-19 10:04:08 -0700 | [diff] [blame] | 99 | |
| 100 | void restartPrefetcherIfNecessary_l( |
| 101 | bool ignoreLowWaterThreshold = false, bool force = false); |
Andreas Huber | b206319 | 2010-06-10 11:09:36 -0700 | [diff] [blame] | 102 | |
| 103 | DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2); |
| 104 | }; |
| 105 | |
| 106 | } // namespace android |
| 107 | |
| 108 | #endif // NU_CACHED_SOURCE_2_H_ |