blob: 656e85e29c8d13a3a7781740a17ed3e966e6cb55 [file] [log] [blame]
Andreas Huber1156dc92011-03-08 15:59:28 -08001/*
2 * Copyright (C) 2011 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 HTTP_BASE_H_
18
19#define HTTP_BASE_H_
20
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070021#include <media/DataSource.h>
Andreas Huber1156dc92011-03-08 15:59:28 -080022#include <media/stagefright/foundation/ABase.h>
James Dong5b1b8a92011-05-25 19:37:03 -070023#include <media/stagefright/MediaErrors.h>
Marco Nelissendab79b32019-11-18 08:25:47 -080024#include <utils/KeyedVector.h>
Dongwon Kang49ce6712018-01-24 10:16:25 -080025#include <utils/List.h>
James Dong5b1b8a92011-05-25 19:37:03 -070026#include <utils/threads.h>
Andreas Huber1156dc92011-03-08 15:59:28 -080027
28namespace android {
29
30struct HTTPBase : public DataSource {
31 enum Flags {
32 // Don't log any URLs.
33 kFlagIncognito = 1
34 };
35
36 HTTPBase();
37
38 virtual status_t connect(
39 const char *uri,
40 const KeyedVector<String8, String8> *headers = NULL,
41 off64_t offset = 0) = 0;
42
43 virtual void disconnect() = 0;
44
45 // Returns true if bandwidth could successfully be estimated,
46 // false otherwise.
James Dong5b1b8a92011-05-25 19:37:03 -070047 virtual bool estimateBandwidth(int32_t *bandwidth_bps);
48
49 virtual status_t getEstimatedBandwidthKbps(int32_t *kbps);
50
51 virtual status_t setBandwidthStatCollectFreq(int32_t freqMs);
Andreas Huber1156dc92011-03-08 15:59:28 -080052
Leena Winterrowda93fd2b2014-12-04 14:03:03 -080053 virtual void setBandwidthHistorySize(size_t numHistoryItems);
54
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080055 virtual String8 toString() {
56 return mName;
57 }
58
James Dong5b1b8a92011-05-25 19:37:03 -070059protected:
Leena Winterrowda93fd2b2014-12-04 14:03:03 -080060 virtual void addBandwidthMeasurement(size_t numBytes, int64_t delayUs);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080061 String8 mName;
James Dong5b1b8a92011-05-25 19:37:03 -070062
Andreas Huber1156dc92011-03-08 15:59:28 -080063private:
James Dong5b1b8a92011-05-25 19:37:03 -070064 struct BandwidthEntry {
65 int64_t mDelayUs;
66 size_t mNumBytes;
67 };
68
69 Mutex mLock;
70
71 List<BandwidthEntry> mBandwidthHistory;
72 size_t mNumBandwidthHistoryItems;
73 int64_t mTotalTransferTimeUs;
74 size_t mTotalTransferBytes;
Leena Winterrowda93fd2b2014-12-04 14:03:03 -080075 size_t mMaxBandwidthHistoryItems;
James Dong5b1b8a92011-05-25 19:37:03 -070076
77 enum {
78 kMinBandwidthCollectFreqMs = 1000, // 1 second
79 kMaxBandwidthCollectFreqMs = 60000, // one minute
80 };
81
82 int64_t mPrevBandwidthMeasureTimeUs;
83 int32_t mPrevEstimatedBandWidthKbps;
84 int32_t mBandWidthCollectFreqMs;
85
Andreas Huber1156dc92011-03-08 15:59:28 -080086 DISALLOW_EVIL_CONSTRUCTORS(HTTPBase);
87};
88
89} // namespace android
90
91#endif // HTTP_BASE_H_