Andreas Huber | 1156dc9 | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "HTTPBase" |
| 19 | #include <utils/Log.h> |
| 20 | |
Marco Nelissen | fa8be7d | 2019-09-23 12:15:57 -0700 | [diff] [blame] | 21 | #include <datasource/HTTPBase.h> |
Andreas Huber | 1156dc9 | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 22 | |
Andreas Huber | dab718b | 2011-07-13 15:45:01 -0700 | [diff] [blame] | 23 | #include <media/stagefright/foundation/ADebug.h> |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 24 | #include <media/stagefright/foundation/ALooper.h> |
Andreas Huber | dab718b | 2011-07-13 15:45:01 -0700 | [diff] [blame] | 25 | |
Andreas Huber | 1156dc9 | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Andreas Huber | dab718b | 2011-07-13 15:45:01 -0700 | [diff] [blame] | 27 | #include <cutils/qtaguid.h> |
Andreas Huber | 1156dc9 | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 31 | HTTPBase::HTTPBase() |
| 32 | : mNumBandwidthHistoryItems(0), |
| 33 | mTotalTransferTimeUs(0), |
| 34 | mTotalTransferBytes(0), |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 35 | mMaxBandwidthHistoryItems(100), |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 36 | mPrevBandwidthMeasureTimeUs(0), |
| 37 | mPrevEstimatedBandWidthKbps(0), |
Lajos Molnar | ee4e1b1 | 2015-04-17 13:46:19 -0700 | [diff] [blame] | 38 | mBandWidthCollectFreqMs(5000) { |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 39 | mName = String8("HTTPBase(<disconnected>)"); |
Andreas Huber | b7319a7 | 2013-05-29 14:20:52 -0700 | [diff] [blame] | 40 | } |
| 41 | |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 42 | void 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 Winterrowd | a93fd2b | 2014-12-04 14:03:03 -0800 | [diff] [blame] | 53 | if (++mNumBandwidthHistoryItems > mMaxBandwidthHistoryItems) { |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 54 | 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 | |
| 74 | bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) { |
| 75 | Mutex::Autolock autoLock(mLock); |
| 76 | |
Chong Zhang | 358e717 | 2015-03-09 09:08:15 -0700 | [diff] [blame] | 77 | // 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 Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
| 85 | *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) { |
| 91 | Mutex::Autolock autoLock(mLock); |
| 92 | *kbps = mPrevEstimatedBandWidthKbps; |
| 93 | return OK; |
| 94 | } |
| 95 | |
| 96 | status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) { |
| 97 | Mutex::Autolock autoLock(mLock); |
| 98 | |
| 99 | if (freqMs < kMinBandwidthCollectFreqMs |
| 100 | || freqMs > kMaxBandwidthCollectFreqMs) { |
| 101 | |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 102 | ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs); |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 103 | return BAD_VALUE; |
| 104 | } |
| 105 | |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 106 | ALOGI("frequency set to %d ms", freqMs); |
James Dong | 5b1b8a9 | 2011-05-25 19:37:03 -0700 | [diff] [blame] | 107 | mBandWidthCollectFreqMs = freqMs; |
| 108 | return OK; |
| 109 | } |
| 110 | |
Leena Winterrowd | a93fd2b | 2014-12-04 14:03:03 -0800 | [diff] [blame] | 111 | void HTTPBase::setBandwidthHistorySize(size_t numHistoryItems) { |
| 112 | mMaxBandwidthHistoryItems = numHistoryItems; |
| 113 | } |
| 114 | |
Andreas Huber | 1156dc9 | 2011-03-08 15:59:28 -0800 | [diff] [blame] | 115 | } // namespace android |