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 17 //#define LOG_NDEBUG 0 18 #define LOG_TAG "HTTPBase" 19 #include <utils/Log.h> 20 21 #include "include/HTTPBase.h" 22 23 #include <media/stagefright/foundation/ADebug.h> 24 #include <media/stagefright/foundation/ALooper.h> 25 26 #include <cutils/properties.h> 27 #include <cutils/qtaguid.h> 28 29 #include <NetdClient.h> 30 31 namespace android { 32 33 HTTPBase::HTTPBase() 34 : mNumBandwidthHistoryItems(0), 35 mTotalTransferTimeUs(0), 36 mTotalTransferBytes(0), 37 mMaxBandwidthHistoryItems(100), 38 mPrevBandwidthMeasureTimeUs(0), 39 mPrevEstimatedBandWidthKbps(0), 40 mBandWidthCollectFreqMs(5000) { 41 mName = String8("HTTPBase(<disconnected>)"); 42 } 43 44 void HTTPBase::addBandwidthMeasurement( 45 size_t numBytes, int64_t delayUs) { 46 Mutex::Autolock autoLock(mLock); 47 48 BandwidthEntry entry; 49 entry.mDelayUs = delayUs; 50 entry.mNumBytes = numBytes; 51 mTotalTransferTimeUs += delayUs; 52 mTotalTransferBytes += numBytes; 53 54 mBandwidthHistory.push_back(entry); 55 if (++mNumBandwidthHistoryItems > mMaxBandwidthHistoryItems) { 56 BandwidthEntry *entry = &*mBandwidthHistory.begin(); 57 mTotalTransferTimeUs -= entry->mDelayUs; 58 mTotalTransferBytes -= entry->mNumBytes; 59 mBandwidthHistory.erase(mBandwidthHistory.begin()); 60 --mNumBandwidthHistoryItems; 61 62 int64_t timeNowUs = ALooper::GetNowUs(); 63 if (timeNowUs - mPrevBandwidthMeasureTimeUs >= 64 mBandWidthCollectFreqMs * 1000LL) { 65 66 if (mPrevBandwidthMeasureTimeUs != 0) { 67 mPrevEstimatedBandWidthKbps = 68 (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs); 69 } 70 mPrevBandwidthMeasureTimeUs = timeNowUs; 71 } 72 } 73 74 } 75 76 bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) { 77 Mutex::Autolock autoLock(mLock); 78 79 // Do not do bandwidth estimation if we don't have enough samples, or 80 // total bytes download are too small (<64K). 81 // Bandwidth estimation from these samples can often shoot up and cause 82 // unwanted bw adaption behaviors. 83 if (mNumBandwidthHistoryItems < 2 || mTotalTransferBytes < 65536) { 84 return false; 85 } 86 87 *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs); 88 89 return true; 90 } 91 92 status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) { 93 Mutex::Autolock autoLock(mLock); 94 *kbps = mPrevEstimatedBandWidthKbps; 95 return OK; 96 } 97 98 status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) { 99 Mutex::Autolock autoLock(mLock); 100 101 if (freqMs < kMinBandwidthCollectFreqMs 102 || freqMs > kMaxBandwidthCollectFreqMs) { 103 104 ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs); 105 return BAD_VALUE; 106 } 107 108 ALOGI("frequency set to %d ms", freqMs); 109 mBandWidthCollectFreqMs = freqMs; 110 return OK; 111 } 112 113 void HTTPBase::setBandwidthHistorySize(size_t numHistoryItems) { 114 mMaxBandwidthHistoryItems = numHistoryItems; 115 } 116 117 // static 118 void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) { 119 int res = qtaguid_tagSocket(sockfd, kTag, uid); 120 if (res != 0) { 121 ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid()); 122 } 123 } 124 125 // static 126 void HTTPBase::UnRegisterSocketUserTag(int sockfd) { 127 int res = qtaguid_untagSocket(sockfd); 128 if (res != 0) { 129 ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid()); 130 } 131 } 132 133 // static 134 void HTTPBase::RegisterSocketUserMark(int sockfd, uid_t uid) { 135 setNetworkForUser(uid, sockfd); 136 } 137 138 // static 139 void HTTPBase::UnRegisterSocketUserMark(int sockfd) { 140 RegisterSocketUserMark(sockfd, geteuid()); 141 } 142 143 } // namespace android 144