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