NuCachedSource2.h revision 0683eba6b35c396c21f10e926709f2f8fc05f090
15994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber/*
25994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * Copyright (C) 2010 The Android Open Source Project
35994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber *
45994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
55994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * you may not use this file except in compliance with the License.
65994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * You may obtain a copy of the License at
75994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber *
85994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
95994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber *
105994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * Unless required by applicable law or agreed to in writing, software
115994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
125994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * See the License for the specific language governing permissions and
145994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber * limitations under the License.
155994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber */
165994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
175994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#ifndef NU_CACHED_SOURCE_2_H_
185994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
195994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#define NU_CACHED_SOURCE_2_H_
205994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
215994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#include <media/stagefright/foundation/ABase.h>
225994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#include <media/stagefright/foundation/AHandlerReflector.h>
235994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#include <media/stagefright/DataSource.h>
245994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
255994b4798b01f3dd340577c9ea9657f09093a770Andreas Hubernamespace android {
265994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
275994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberstruct ALooper;
285994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberstruct PageCache;
295994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
305994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberstruct NuCachedSource2 : public DataSource {
315994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    NuCachedSource2(const sp<DataSource> &source);
325994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
335994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual status_t initCheck() const;
345994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
35c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    virtual ssize_t readAt(off64_t offset, void *data, size_t size);
365994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
37c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    virtual status_t getSize(off64_t *size);
385994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual uint32_t flags();
395994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
40b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    virtual sp<DecryptHandle> DrmInitialization();
41b5ce361d19e69fe156f7188c9ee0f4734b259874Gloria Wang    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client);
42771b85d9245a24273497792a2515d88d31c99e1eGloria Wang    virtual String8 getUri();
436511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber
446511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber    virtual String8 getMIMEType() const;
456511c9755c3a3360ba869772600c7aae048a7ffcAndreas Huber
465994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    ////////////////////////////////////////////////////////////////////////////
475994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
485994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    size_t cachedSize();
491bd233ce7e1aa7730bc18d46ffd57791391738cdBryan Mawhinney    size_t approxDataRemaining(status_t *finalStatus);
505994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
5134ef0f32c8fc0186236a27e07405328cc1f7c56dAndreas Huber    void resumeFetchingIfNecessary();
52b33d2ac90cfce0fe6db8c3e979e7ae2bbfc28163James Dong
53b33d2ac90cfce0fe6db8c3e979e7ae2bbfc28163James Dong    // The following methods are supported only if the
54b33d2ac90cfce0fe6db8c3e979e7ae2bbfc28163James Dong    // data source is HTTP-based; otherwise, ERROR_UNSUPPORTED
55b33d2ac90cfce0fe6db8c3e979e7ae2bbfc28163James Dong    // is returned.
565b1b8a93a07326f1cbc627f09e02988375189e0aJames Dong    status_t getEstimatedBandwidthKbps(int32_t *kbps);
57b33d2ac90cfce0fe6db8c3e979e7ae2bbfc28163James Dong    status_t setCacheStatCollectFreq(int32_t freqMs);
5834ef0f32c8fc0186236a27e07405328cc1f7c56dAndreas Huber
595994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberprotected:
605994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual ~NuCachedSource2();
615994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
625994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberprivate:
635994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    friend struct AHandlerReflector<NuCachedSource2>;
645994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
655994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    enum {
669235bc896538e906e162aa5f9e11f8af04c29664Andreas Huber        kPageSize            = 65536,
67145e68fc778275963189b02a1adcbe27cce4d769Andreas Huber        kHighWaterThreshold  = 20 * 1024 * 1024,
68145e68fc778275963189b02a1adcbe27cce4d769Andreas Huber        kLowWaterThreshold   = 4 * 1024 * 1024,
69a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber
70a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        // Read data after a 15 sec timeout whether we're actively
71a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        // fetching or not.
72a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        kKeepAliveIntervalUs = 15000000,
735994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    };
745994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
755994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    enum {
765994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber        kWhatFetchMore  = 'fetc',
775994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber        kWhatRead       = 'read',
785994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    };
795994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
800683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber    enum {
810683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber        kMaxNumRetries = 10,
820683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber    };
830683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber
845994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<DataSource> mSource;
855994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<AHandlerReflector<NuCachedSource2> > mReflector;
865994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<ALooper> mLooper;
875994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
885994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Mutex mSerializer;
895994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Mutex mLock;
905994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Condition mCondition;
915994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
925994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    PageCache *mCache;
93c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t mCacheOffset;
945994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    status_t mFinalStatus;
95c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    off64_t mLastAccessPos;
965994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<AMessage> mAsyncResult;
975994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    bool mFetching;
98a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber    int64_t mLastFetchTimeUs;
995994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1000683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber    int32_t mNumRetriesLeft;
1010683eba6b35c396c21f10e926709f2f8fc05f090Andreas Huber
1025994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onMessageReceived(const sp<AMessage> &msg);
1035994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onFetch();
1045994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onRead(const sp<AMessage> &msg);
1055994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1065994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void fetchInternal();
107c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    ssize_t readInternal(off64_t offset, void *data, size_t size);
108c7fc37a3dab9bd1f96713649f351b5990e6316ffJames Dong    status_t seekInternal_l(off64_t offset);
1095994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1101bd233ce7e1aa7730bc18d46ffd57791391738cdBryan Mawhinney    size_t approxDataRemaining_l(status_t *finalStatus);
1117bf8413f91fc072452f315a91618aeef2574d420Andreas Huber
1127bf8413f91fc072452f315a91618aeef2574d420Andreas Huber    void restartPrefetcherIfNecessary_l(
1137bf8413f91fc072452f315a91618aeef2574d420Andreas Huber            bool ignoreLowWaterThreshold = false, bool force = false);
1145994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1155994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
1165994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber};
1175994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1185994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber}  // namespace android
1195994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1205994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#endif  // NU_CACHED_SOURCE_2_H_
121