blob: ef29c483513c37dbd3db5fd6815881fdd764f243 [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
James Dong5b1b8a92011-05-25 19:37:03 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "HTTPBase"
19#include <utils/Log.h>
20
Marco Nelissen42057ce2019-09-23 12:15:57 -070021#include <datasource/HTTPBase.h>
Andreas Huber1156dc92011-03-08 15:59:28 -080022
Andreas Huberdab718b2011-07-13 15:45:01 -070023#include <media/stagefright/foundation/ADebug.h>
James Dong5b1b8a92011-05-25 19:37:03 -070024#include <media/stagefright/foundation/ALooper.h>
Andreas Huberdab718b2011-07-13 15:45:01 -070025
Andreas Huber1156dc92011-03-08 15:59:28 -080026#include <cutils/properties.h>
Andreas Huberdab718b2011-07-13 15:45:01 -070027#include <cutils/qtaguid.h>
Andreas Huber1156dc92011-03-08 15:59:28 -080028
29namespace android {
30
James Dong5b1b8a92011-05-25 19:37:03 -070031HTTPBase::HTTPBase()
32 : mNumBandwidthHistoryItems(0),
33 mTotalTransferTimeUs(0),
34 mTotalTransferBytes(0),
Lajos Molnaree4e1b12015-04-17 13:46:19 -070035 mMaxBandwidthHistoryItems(100),
James Dong5b1b8a92011-05-25 19:37:03 -070036 mPrevBandwidthMeasureTimeUs(0),
37 mPrevEstimatedBandWidthKbps(0),
Lajos Molnaree4e1b12015-04-17 13:46:19 -070038 mBandWidthCollectFreqMs(5000) {
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080039 mName = String8("HTTPBase(<disconnected>)");
Andreas Huberb7319a72013-05-29 14:20:52 -070040}
41
James Dong5b1b8a92011-05-25 19:37:03 -070042void HTTPBase::addBandwidthMeasurement(
43 size_t numBytes, int64_t delayUs) {
44 Mutex::Autolock autoLock(mLock);
45
46 BandwidthEntry entry;
47 entry.mDelayUs = delayUs;
48 entry.mNumBytes = numBytes;
49 mTotalTransferTimeUs += delayUs;
50 mTotalTransferBytes += numBytes;
51
52 mBandwidthHistory.push_back(entry);
Leena Winterrowda93fd2b2014-12-04 14:03:03 -080053 if (++mNumBandwidthHistoryItems > mMaxBandwidthHistoryItems) {
James Dong5b1b8a92011-05-25 19:37:03 -070054 BandwidthEntry *entry = &*mBandwidthHistory.begin();
55 mTotalTransferTimeUs -= entry->mDelayUs;
56 mTotalTransferBytes -= entry->mNumBytes;
57 mBandwidthHistory.erase(mBandwidthHistory.begin());
58 --mNumBandwidthHistoryItems;
59
60 int64_t timeNowUs = ALooper::GetNowUs();
61 if (timeNowUs - mPrevBandwidthMeasureTimeUs >=
62 mBandWidthCollectFreqMs * 1000LL) {
63
64 if (mPrevBandwidthMeasureTimeUs != 0) {
65 mPrevEstimatedBandWidthKbps =
66 (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs);
67 }
68 mPrevBandwidthMeasureTimeUs = timeNowUs;
69 }
70 }
71
72}
73
74bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) {
75 Mutex::Autolock autoLock(mLock);
76
Chong Zhang358e7172015-03-09 09:08:15 -070077 // Do not do bandwidth estimation if we don't have enough samples, or
78 // total bytes download are too small (<64K).
79 // Bandwidth estimation from these samples can often shoot up and cause
80 // unwanted bw adaption behaviors.
81 if (mNumBandwidthHistoryItems < 2 || mTotalTransferBytes < 65536) {
James Dong5b1b8a92011-05-25 19:37:03 -070082 return false;
83 }
84
85 *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs);
86
87 return true;
88}
89
90status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
91 Mutex::Autolock autoLock(mLock);
92 *kbps = mPrevEstimatedBandWidthKbps;
93 return OK;
94}
95
96status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) {
97 Mutex::Autolock autoLock(mLock);
98
99 if (freqMs < kMinBandwidthCollectFreqMs
100 || freqMs > kMaxBandwidthCollectFreqMs) {
101
Steve Block29357bc2012-01-06 19:20:56 +0000102 ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
James Dong5b1b8a92011-05-25 19:37:03 -0700103 return BAD_VALUE;
104 }
105
Steve Blockdf64d152012-01-04 20:05:49 +0000106 ALOGI("frequency set to %d ms", freqMs);
James Dong5b1b8a92011-05-25 19:37:03 -0700107 mBandWidthCollectFreqMs = freqMs;
108 return OK;
109}
110
Leena Winterrowda93fd2b2014-12-04 14:03:03 -0800111void HTTPBase::setBandwidthHistorySize(size_t numHistoryItems) {
112 mMaxBandwidthHistoryItems = numHistoryItems;
113}
114
Andreas Huber1156dc92011-03-08 15:59:28 -0800115} // namespace android