NuCachedSource2.h revision 771b85d9245a24273497792a2515d88d31c99e1e
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(off_t offset, void *data, size_t size);
36
37    virtual status_t getSize(off_t *size);
38    virtual uint32_t flags();
39
40    virtual DecryptHandle* DrmInitialization(DrmManagerClient *client);
41    virtual void getDrmInfo(DecryptHandle **handle, DrmManagerClient **client);
42    virtual String8 getUri();
43    ////////////////////////////////////////////////////////////////////////////
44
45    size_t cachedSize();
46    size_t approxDataRemaining(bool *eos);
47
48    void suspend();
49    void clearCacheAndResume();
50
51    void resumeFetchingIfNecessary();
52
53protected:
54    virtual ~NuCachedSource2();
55
56private:
57    friend struct AHandlerReflector<NuCachedSource2>;
58
59    enum {
60        kPageSize            = 65536,
61        kHighWaterThreshold  = 5 * 1024 * 1024,
62        kLowWaterThreshold   = 1024 * 1024,
63
64        // Read data after a 15 sec timeout whether we're actively
65        // fetching or not.
66        kKeepAliveIntervalUs = 15000000,
67    };
68
69    enum {
70        kWhatFetchMore  = 'fetc',
71        kWhatRead       = 'read',
72        kWhatSuspend    = 'susp',
73    };
74
75    sp<DataSource> mSource;
76    sp<AHandlerReflector<NuCachedSource2> > mReflector;
77    sp<ALooper> mLooper;
78
79    Mutex mSerializer;
80    Mutex mLock;
81    Condition mCondition;
82
83    PageCache *mCache;
84    off_t mCacheOffset;
85    status_t mFinalStatus;
86    off_t mLastAccessPos;
87    sp<AMessage> mAsyncResult;
88    bool mFetching;
89    int64_t mLastFetchTimeUs;
90    bool mSuspended;
91
92    void onMessageReceived(const sp<AMessage> &msg);
93    void onFetch();
94    void onRead(const sp<AMessage> &msg);
95    void onSuspend();
96
97    void fetchInternal();
98    ssize_t readInternal(off_t offset, void *data, size_t size);
99    status_t seekInternal_l(off_t offset);
100
101    size_t approxDataRemaining_l(bool *eos);
102    void restartPrefetcherIfNecessary_l(bool ignoreLowWaterThreshold = false);
103
104    DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
105};
106
107}  // namespace android
108
109#endif  // NU_CACHED_SOURCE_2_H_
110