blob: 4c253add78c994b0b3f1380f1219153d79f78eb4 [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
Gloria Wang771b85d2010-11-09 15:06:51 -080047 virtual String8 getUri();
Andreas Huber6511c972011-03-30 11:15:27 -070048
49 virtual String8 getMIMEType() const;
50
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080051 virtual String8 toString() {
52 return mName;
53 }
54
Robert Shih3f657642018-09-27 12:22:23 -070055 status_t getAvailableSize(off64_t offset, off64_t *size);
Robert Shihed388ad2018-09-19 16:12:42 -070056
Andreas Huber5994b472010-06-10 11:09:36 -070057 ////////////////////////////////////////////////////////////////////////////
58
59 size_t cachedSize();
Andreas Hubera53d87c2012-04-19 16:25:20 -070060 size_t approxDataRemaining(status_t *finalStatus) const;
Andreas Huber5994b472010-06-10 11:09:36 -070061
Andreas Huber34ef0f32010-11-11 15:37:17 -080062 void resumeFetchingIfNecessary();
James Dongb33d2ac2011-06-01 15:27:20 -070063
64 // The following methods are supported only if the
65 // data source is HTTP-based; otherwise, ERROR_UNSUPPORTED
66 // is returned.
James Dong5b1b8a92011-05-25 19:37:03 -070067 status_t getEstimatedBandwidthKbps(int32_t *kbps);
James Dongb33d2ac2011-06-01 15:27:20 -070068 status_t setCacheStatCollectFreq(int32_t freqMs);
Andreas Huber34ef0f32010-11-11 15:37:17 -080069
Andreas Huber49c59812011-10-07 13:40:45 -070070 static void RemoveCacheSpecificHeaders(
71 KeyedVector<String8, String8> *headers,
72 String8 *cacheConfig,
73 bool *disconnectAtHighwatermark);
74
Andreas Huber5994b472010-06-10 11:09:36 -070075protected:
76 virtual ~NuCachedSource2();
77
78private:
79 friend struct AHandlerReflector<NuCachedSource2>;
80
Wonsik Kim316c3d92015-09-08 17:32:28 +090081 NuCachedSource2(
82 const sp<DataSource> &source,
83 const char *cacheConfig,
84 bool disconnectAtHighwatermark);
85
Andreas Huber5994b472010-06-10 11:09:36 -070086 enum {
Andreas Hubera045cb02011-10-05 14:32:17 -070087 kPageSize = 65536,
88 kDefaultHighWaterThreshold = 20 * 1024 * 1024,
89 kDefaultLowWaterThreshold = 4 * 1024 * 1024,
Andreas Hubera5273eb2010-06-22 08:57:34 -070090
91 // Read data after a 15 sec timeout whether we're actively
92 // fetching or not.
Andreas Hubera045cb02011-10-05 14:32:17 -070093 kDefaultKeepAliveIntervalUs = 15000000,
Andreas Huber5994b472010-06-10 11:09:36 -070094 };
95
96 enum {
97 kWhatFetchMore = 'fetc',
98 kWhatRead = 'read',
99 };
100
Andreas Huber0683eba2011-07-18 13:47:55 -0700101 enum {
102 kMaxNumRetries = 10,
103 };
104
Andreas Huber5994b472010-06-10 11:09:36 -0700105 sp<DataSource> mSource;
106 sp<AHandlerReflector<NuCachedSource2> > mReflector;
107 sp<ALooper> mLooper;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800108 String8 mName;
Andreas Huber5994b472010-06-10 11:09:36 -0700109
110 Mutex mSerializer;
Andreas Hubera53d87c2012-04-19 16:25:20 -0700111 mutable Mutex mLock;
Andreas Huber5994b472010-06-10 11:09:36 -0700112 Condition mCondition;
113
114 PageCache *mCache;
James Dongc7fc37a2010-11-16 14:04:54 -0800115 off64_t mCacheOffset;
Andreas Huber5994b472010-06-10 11:09:36 -0700116 status_t mFinalStatus;
James Dongc7fc37a2010-11-16 14:04:54 -0800117 off64_t mLastAccessPos;
Andreas Huber5994b472010-06-10 11:09:36 -0700118 sp<AMessage> mAsyncResult;
119 bool mFetching;
Chong Zhang48296b72014-09-14 14:28:45 -0700120 bool mDisconnecting;
Andreas Hubera5273eb2010-06-22 08:57:34 -0700121 int64_t mLastFetchTimeUs;
Andreas Huber5994b472010-06-10 11:09:36 -0700122
Andreas Huber0683eba2011-07-18 13:47:55 -0700123 int32_t mNumRetriesLeft;
124
Andreas Hubera045cb02011-10-05 14:32:17 -0700125 size_t mHighwaterThresholdBytes;
126 size_t mLowwaterThresholdBytes;
127
128 // If the keep-alive interval is 0, keep-alives are disabled.
129 int64_t mKeepAliveIntervalUs;
130
Andreas Huber49c59812011-10-07 13:40:45 -0700131 bool mDisconnectAtHighwatermark;
132
Andreas Huber5994b472010-06-10 11:09:36 -0700133 void onMessageReceived(const sp<AMessage> &msg);
134 void onFetch();
135 void onRead(const sp<AMessage> &msg);
Andreas Huber5994b472010-06-10 11:09:36 -0700136
137 void fetchInternal();
James Dongc7fc37a2010-11-16 14:04:54 -0800138 ssize_t readInternal(off64_t offset, void *data, size_t size);
139 status_t seekInternal_l(off64_t offset);
Andreas Huber5994b472010-06-10 11:09:36 -0700140
Robert Shih3f657642018-09-27 12:22:23 -0700141 size_t approxDataRemaining_l(off64_t offset, status_t *finalStatus) const;
Andreas Huber7bf84132011-04-19 10:04:08 -0700142
143 void restartPrefetcherIfNecessary_l(
144 bool ignoreLowWaterThreshold = false, bool force = false);
Andreas Huber5994b472010-06-10 11:09:36 -0700145
Andreas Hubera045cb02011-10-05 14:32:17 -0700146 void updateCacheParamsFromSystemProperty();
147 void updateCacheParamsFromString(const char *s);
148
Andreas Huber5994b472010-06-10 11:09:36 -0700149 DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
150};
151
152} // namespace android
153
154#endif // NU_CACHED_SOURCE_2_H_