NuCachedSource2.h revision 34ef0f32c8fc0186236a27e07405328cc1f7c56d
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
355994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual ssize_t readAt(off_t offset, void *data, size_t size);
365994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
375994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual status_t getSize(off_t *size);
385994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual uint32_t flags();
395994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
40b371426ce4cf2fa6d8c3d1903b61322feb165d35Gloria Wang    virtual DecryptHandle* DrmInitialization(DrmManagerClient *client);
41b371426ce4cf2fa6d8c3d1903b61322feb165d35Gloria Wang    virtual void getDrmInfo(DecryptHandle **handle, DrmManagerClient **client);
425994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    ////////////////////////////////////////////////////////////////////////////
435994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
445994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    size_t cachedSize();
455994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    size_t approxDataRemaining(bool *eos);
465994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
47bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    void suspend();
48bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    void clearCacheAndResume();
49bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber
5034ef0f32c8fc0186236a27e07405328cc1f7c56dAndreas Huber    void resumeFetchingIfNecessary();
5134ef0f32c8fc0186236a27e07405328cc1f7c56dAndreas Huber
525994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberprotected:
535994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    virtual ~NuCachedSource2();
545994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
555994b4798b01f3dd340577c9ea9657f09093a770Andreas Huberprivate:
565994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    friend struct AHandlerReflector<NuCachedSource2>;
575994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
585994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    enum {
599235bc896538e906e162aa5f9e11f8af04c29664Andreas Huber        kPageSize            = 65536,
609235bc896538e906e162aa5f9e11f8af04c29664Andreas Huber        kHighWaterThreshold  = 5 * 1024 * 1024,
61a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        kLowWaterThreshold   = 512 * 1024,
62a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber
63a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        // Read data after a 15 sec timeout whether we're actively
64a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        // fetching or not.
65a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber        kKeepAliveIntervalUs = 15000000,
665994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    };
675994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
685994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    enum {
695994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber        kWhatFetchMore  = 'fetc',
705994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber        kWhatRead       = 'read',
71bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber        kWhatSuspend    = 'susp',
725994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    };
735994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
745994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<DataSource> mSource;
755994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<AHandlerReflector<NuCachedSource2> > mReflector;
765994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<ALooper> mLooper;
775994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
785994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Mutex mSerializer;
795994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Mutex mLock;
805994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    Condition mCondition;
815994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
825994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    PageCache *mCache;
835994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    off_t mCacheOffset;
845994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    status_t mFinalStatus;
855994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    off_t mLastAccessPos;
865994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    sp<AMessage> mAsyncResult;
875994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    bool mFetching;
88a5273ebd1746368662a597643d6701a5046d5c7bAndreas Huber    int64_t mLastFetchTimeUs;
89bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    bool mSuspended;
905994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
915994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onMessageReceived(const sp<AMessage> &msg);
925994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onFetch();
935994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void onRead(const sp<AMessage> &msg);
94bff07d0b22a5ee2d9f044f6cb5e4be1532017ab0Andreas Huber    void onSuspend();
955994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
965994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    void fetchInternal();
975994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    ssize_t readInternal(off_t offset, void *data, size_t size);
985994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    status_t seekInternal_l(off_t offset);
995994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1005994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    size_t approxDataRemaining_l(bool *eos);
10134ef0f32c8fc0186236a27e07405328cc1f7c56dAndreas Huber    void restartPrefetcherIfNecessary_l(bool ignoreLowWaterThreshold = false);
1025994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1035994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber    DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
1045994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber};
1055994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1065994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber}  // namespace android
1075994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber
1085994b4798b01f3dd340577c9ea9657f09093a770Andreas Huber#endif  // NU_CACHED_SOURCE_2_H_
109