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