• 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/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     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 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     NuCachedSource2(
76             const sp<DataSource> &source,
77             const char *cacheConfig,
78             bool disconnectAtHighwatermark);
79 
80     enum {
81         kPageSize                       = 65536,
82         kDefaultHighWaterThreshold      = 20 * 1024 * 1024,
83         kDefaultLowWaterThreshold       = 4 * 1024 * 1024,
84 
85         // Read data after a 15 sec timeout whether we're actively
86         // fetching or not.
87         kDefaultKeepAliveIntervalUs     = 15000000,
88     };
89 
90     enum {
91         kWhatFetchMore  = 'fetc',
92         kWhatRead       = 'read',
93     };
94 
95     enum {
96         kMaxNumRetries = 10,
97     };
98 
99     sp<DataSource> mSource;
100     sp<AHandlerReflector<NuCachedSource2> > mReflector;
101     sp<ALooper> mLooper;
102 
103     Mutex mSerializer;
104     mutable Mutex mLock;
105     Condition mCondition;
106 
107     PageCache *mCache;
108     off64_t mCacheOffset;
109     status_t mFinalStatus;
110     off64_t mLastAccessPos;
111     sp<AMessage> mAsyncResult;
112     bool mFetching;
113     bool mDisconnecting;
114     int64_t mLastFetchTimeUs;
115 
116     int32_t mNumRetriesLeft;
117 
118     size_t mHighwaterThresholdBytes;
119     size_t mLowwaterThresholdBytes;
120 
121     // If the keep-alive interval is 0, keep-alives are disabled.
122     int64_t mKeepAliveIntervalUs;
123 
124     bool mDisconnectAtHighwatermark;
125 
126     void onMessageReceived(const sp<AMessage> &msg);
127     void onFetch();
128     void onRead(const sp<AMessage> &msg);
129 
130     void fetchInternal();
131     ssize_t readInternal(off64_t offset, void *data, size_t size);
132     status_t seekInternal_l(off64_t offset);
133 
134     size_t approxDataRemaining_l(status_t *finalStatus) const;
135 
136     void restartPrefetcherIfNecessary_l(
137             bool ignoreLowWaterThreshold = false, bool force = false);
138 
139     void updateCacheParamsFromSystemProperty();
140     void updateCacheParamsFromString(const char *s);
141 
142     DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
143 };
144 
145 }  // namespace android
146 
147 #endif  // NU_CACHED_SOURCE_2_H_
148