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/DataSource.h> 22 #include <media/stagefright/foundation/ABase.h> 23 #include <media/stagefright/foundation/AHandlerReflector.h> 24 25 namespace android { 26 27 struct ALooper; 28 struct PageCache; 29 30 struct NuCachedSource2 : public DataSource { 31 static sp<NuCachedSource2> Create( 32 const sp<DataSource> &source, 33 const char *cacheConfig = NULL, 34 bool disconnectAtHighwatermark = false); 35 36 virtual status_t initCheck() const; 37 38 virtual ssize_t readAt(off64_t offset, void *data, size_t size); 39 40 virtual void close(); 41 42 virtual void disconnect(); 43 44 virtual status_t getSize(off64_t *size); 45 virtual uint32_t flags(); 46 47 virtual sp<DecryptHandle> DrmInitialization(const char* mime); 48 virtual String8 getUri(); 49 50 virtual String8 getMIMEType() const; 51 52 virtual String8 toString() { 53 return mName; 54 } 55 56 status_t getAvailableSize(off64_t offset, off64_t *size); 57 58 //////////////////////////////////////////////////////////////////////////// 59 60 size_t cachedSize(); 61 size_t approxDataRemaining(status_t *finalStatus) const; 62 63 void resumeFetchingIfNecessary(); 64 65 // The following methods are supported only if the 66 // data source is HTTP-based; otherwise, ERROR_UNSUPPORTED 67 // is returned. 68 status_t getEstimatedBandwidthKbps(int32_t *kbps); 69 status_t setCacheStatCollectFreq(int32_t freqMs); 70 71 static void RemoveCacheSpecificHeaders( 72 KeyedVector<String8, String8> *headers, 73 String8 *cacheConfig, 74 bool *disconnectAtHighwatermark); 75 76 protected: 77 virtual ~NuCachedSource2(); 78 79 private: 80 friend struct AHandlerReflector<NuCachedSource2>; 81 82 NuCachedSource2( 83 const sp<DataSource> &source, 84 const char *cacheConfig, 85 bool disconnectAtHighwatermark); 86 87 enum { 88 kPageSize = 65536, 89 kDefaultHighWaterThreshold = 20 * 1024 * 1024, 90 kDefaultLowWaterThreshold = 4 * 1024 * 1024, 91 92 // Read data after a 15 sec timeout whether we're actively 93 // fetching or not. 94 kDefaultKeepAliveIntervalUs = 15000000, 95 }; 96 97 enum { 98 kWhatFetchMore = 'fetc', 99 kWhatRead = 'read', 100 }; 101 102 enum { 103 kMaxNumRetries = 10, 104 }; 105 106 sp<DataSource> mSource; 107 sp<AHandlerReflector<NuCachedSource2> > mReflector; 108 sp<ALooper> mLooper; 109 String8 mName; 110 111 Mutex mSerializer; 112 mutable Mutex mLock; 113 Condition mCondition; 114 115 PageCache *mCache; 116 off64_t mCacheOffset; 117 status_t mFinalStatus; 118 off64_t mLastAccessPos; 119 sp<AMessage> mAsyncResult; 120 bool mFetching; 121 bool mDisconnecting; 122 int64_t mLastFetchTimeUs; 123 124 int32_t mNumRetriesLeft; 125 126 size_t mHighwaterThresholdBytes; 127 size_t mLowwaterThresholdBytes; 128 129 // If the keep-alive interval is 0, keep-alives are disabled. 130 int64_t mKeepAliveIntervalUs; 131 132 bool mDisconnectAtHighwatermark; 133 134 void onMessageReceived(const sp<AMessage> &msg); 135 void onFetch(); 136 void onRead(const sp<AMessage> &msg); 137 138 void fetchInternal(); 139 ssize_t readInternal(off64_t offset, void *data, size_t size); 140 status_t seekInternal_l(off64_t offset); 141 142 size_t approxDataRemaining_l(off64_t offset, status_t *finalStatus) const; 143 144 void restartPrefetcherIfNecessary_l( 145 bool ignoreLowWaterThreshold = false, bool force = false); 146 147 void updateCacheParamsFromSystemProperty(); 148 void updateCacheParamsFromString(const char *s); 149 150 DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2); 151 }; 152 153 } // namespace android 154 155 #endif // NU_CACHED_SOURCE_2_H_ 156