blob: 596efb88efaf008639f1447f5eb8c20fdbc09bbd [file] [log] [blame]
Andreas Huber5994b472010-06-10 11:09:36 -07001/*
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
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070021#include <media/DataSource.h>
Andreas Huber5994b472010-06-10 11:09:36 -070022#include <media/stagefright/foundation/ABase.h>
23#include <media/stagefright/foundation/AHandlerReflector.h>
Andreas Huber5994b472010-06-10 11:09:36 -070024
25namespace android {
26
27struct ALooper;
28struct PageCache;
29
30struct NuCachedSource2 : public DataSource {
Wonsik Kim316c3d92015-09-08 17:32:28 +090031 static sp<NuCachedSource2> Create(
Andreas Huber49c59812011-10-07 13:40:45 -070032 const sp<DataSource> &source,
33 const char *cacheConfig = NULL,
34 bool disconnectAtHighwatermark = false);
Andreas Huber5994b472010-06-10 11:09:36 -070035
36 virtual status_t initCheck() const;
37
James Dongc7fc37a2010-11-16 14:04:54 -080038 virtual ssize_t readAt(off64_t offset, void *data, size_t size);
Andreas Huber5994b472010-06-10 11:09:36 -070039
Robert Shih03244032018-10-02 14:36:32 -070040 virtual void close();
41
Chong Zhang48296b72014-09-14 14:28:45 -070042 virtual void disconnect();
43
James Dongc7fc37a2010-11-16 14:04:54 -080044 virtual status_t getSize(off64_t *size);
Andreas Huber5994b472010-06-10 11:09:36 -070045 virtual uint32_t flags();
46
James Dong9d2f3862012-01-10 08:24:37 -080047 virtual sp<DecryptHandle> DrmInitialization(const char* mime);
Gloria Wang771b85d2010-11-09 15:06:51 -080048 virtual String8 getUri();
Andreas Huber6511c972011-03-30 11:15:27 -070049
50 virtual String8 getMIMEType() const;
51
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080052 virtual String8 toString() {
53 return mName;
54 }
55
Robert Shih3f657642018-09-27 12:22:23 -070056 status_t getAvailableSize(off64_t offset, off64_t *size);
Robert Shihed388ad2018-09-19 16:12:42 -070057
Andreas Huber5994b472010-06-10 11:09:36 -070058 ////////////////////////////////////////////////////////////////////////////
59
60 size_t cachedSize();
Andreas Hubera53d87c2012-04-19 16:25:20 -070061 size_t approxDataRemaining(status_t *finalStatus) const;
Andreas Huber5994b472010-06-10 11:09:36 -070062
Andreas Huber34ef0f32010-11-11 15:37:17 -080063 void resumeFetchingIfNecessary();
James Dongb33d2ac2011-06-01 15:27:20 -070064
65 // The following methods are supported only if the
66 // data source is HTTP-based; otherwise, ERROR_UNSUPPORTED
67 // is returned.
James Dong5b1b8a92011-05-25 19:37:03 -070068 status_t getEstimatedBandwidthKbps(int32_t *kbps);
James Dongb33d2ac2011-06-01 15:27:20 -070069 status_t setCacheStatCollectFreq(int32_t freqMs);
Andreas Huber34ef0f32010-11-11 15:37:17 -080070
Andreas Huber49c59812011-10-07 13:40:45 -070071 static void RemoveCacheSpecificHeaders(
72 KeyedVector<String8, String8> *headers,
73 String8 *cacheConfig,
74 bool *disconnectAtHighwatermark);
75
Andreas Huber5994b472010-06-10 11:09:36 -070076protected:
77 virtual ~NuCachedSource2();
78
79private:
80 friend struct AHandlerReflector<NuCachedSource2>;
81
Wonsik Kim316c3d92015-09-08 17:32:28 +090082 NuCachedSource2(
83 const sp<DataSource> &source,
84 const char *cacheConfig,
85 bool disconnectAtHighwatermark);
86
Andreas Huber5994b472010-06-10 11:09:36 -070087 enum {
Andreas Hubera045cb02011-10-05 14:32:17 -070088 kPageSize = 65536,
89 kDefaultHighWaterThreshold = 20 * 1024 * 1024,
90 kDefaultLowWaterThreshold = 4 * 1024 * 1024,
Andreas Hubera5273eb2010-06-22 08:57:34 -070091
92 // Read data after a 15 sec timeout whether we're actively
93 // fetching or not.
Andreas Hubera045cb02011-10-05 14:32:17 -070094 kDefaultKeepAliveIntervalUs = 15000000,
Andreas Huber5994b472010-06-10 11:09:36 -070095 };
96
97 enum {
98 kWhatFetchMore = 'fetc',
99 kWhatRead = 'read',
100 };
101
Andreas Huber0683eba2011-07-18 13:47:55 -0700102 enum {
103 kMaxNumRetries = 10,
104 };
105
Andreas Huber5994b472010-06-10 11:09:36 -0700106 sp<DataSource> mSource;
107 sp<AHandlerReflector<NuCachedSource2> > mReflector;
108 sp<ALooper> mLooper;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800109 String8 mName;
Andreas Huber5994b472010-06-10 11:09:36 -0700110
111 Mutex mSerializer;
Andreas Hubera53d87c2012-04-19 16:25:20 -0700112 mutable Mutex mLock;
Andreas Huber5994b472010-06-10 11:09:36 -0700113 Condition mCondition;
114
115 PageCache *mCache;
James Dongc7fc37a2010-11-16 14:04:54 -0800116 off64_t mCacheOffset;
Andreas Huber5994b472010-06-10 11:09:36 -0700117 status_t mFinalStatus;
James Dongc7fc37a2010-11-16 14:04:54 -0800118 off64_t mLastAccessPos;
Andreas Huber5994b472010-06-10 11:09:36 -0700119 sp<AMessage> mAsyncResult;
120 bool mFetching;
Chong Zhang48296b72014-09-14 14:28:45 -0700121 bool mDisconnecting;
Andreas Hubera5273eb2010-06-22 08:57:34 -0700122 int64_t mLastFetchTimeUs;
Andreas Huber5994b472010-06-10 11:09:36 -0700123
Andreas Huber0683eba2011-07-18 13:47:55 -0700124 int32_t mNumRetriesLeft;
125
Andreas Hubera045cb02011-10-05 14:32:17 -0700126 size_t mHighwaterThresholdBytes;
127 size_t mLowwaterThresholdBytes;
128
129 // If the keep-alive interval is 0, keep-alives are disabled.
130 int64_t mKeepAliveIntervalUs;
131
Andreas Huber49c59812011-10-07 13:40:45 -0700132 bool mDisconnectAtHighwatermark;
133
Andreas Huber5994b472010-06-10 11:09:36 -0700134 void onMessageReceived(const sp<AMessage> &msg);
135 void onFetch();
136 void onRead(const sp<AMessage> &msg);
Andreas Huber5994b472010-06-10 11:09:36 -0700137
138 void fetchInternal();
James Dongc7fc37a2010-11-16 14:04:54 -0800139 ssize_t readInternal(off64_t offset, void *data, size_t size);
140 status_t seekInternal_l(off64_t offset);
Andreas Huber5994b472010-06-10 11:09:36 -0700141
Robert Shih3f657642018-09-27 12:22:23 -0700142 size_t approxDataRemaining_l(off64_t offset, status_t *finalStatus) const;
Andreas Huber7bf84132011-04-19 10:04:08 -0700143
144 void restartPrefetcherIfNecessary_l(
145 bool ignoreLowWaterThreshold = false, bool force = false);
Andreas Huber5994b472010-06-10 11:09:36 -0700146
Andreas Hubera045cb02011-10-05 14:32:17 -0700147 void updateCacheParamsFromSystemProperty();
148 void updateCacheParamsFromString(const char *s);
149
Andreas Huber5994b472010-06-10 11:09:36 -0700150 DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
151};
152
153} // namespace android
154
155#endif // NU_CACHED_SOURCE_2_H_