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