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