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