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