From 5b1b8a93a07326f1cbc627f09e02988375189e0a Mon Sep 17 00:00:00 2001 From: James Dong Date: Wed, 25 May 2011 19:37:03 -0700 Subject: Send estimated bandwidth value as informational event when cache fetcher pauses o Application can make informed decision about the available network bandwidth when cache fetcher pauses. o Application can also adjust how frequently the bandwidth is estimated within a range from one second to one minute. Change-Id: I90068001343e79da1886de03c565537787e1580b --- media/libstagefright/HTTPBase.cpp | 79 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) (limited to 'media/libstagefright/HTTPBase.cpp') diff --git a/media/libstagefright/HTTPBase.cpp b/media/libstagefright/HTTPBase.cpp index 58b17a7..c0ae29d 100644 --- a/media/libstagefright/HTTPBase.cpp +++ b/media/libstagefright/HTTPBase.cpp @@ -14,6 +14,10 @@ * limitations under the License. */ +//#define LOG_NDEBUG 0 +#define LOG_TAG "HTTPBase" +#include + #include "include/HTTPBase.h" #if CHROMIUM_AVAILABLE @@ -22,11 +26,19 @@ #include "include/NuHTTPDataSource.h" +#include #include namespace android { -HTTPBase::HTTPBase() {} +HTTPBase::HTTPBase() + : mNumBandwidthHistoryItems(0), + mTotalTransferTimeUs(0), + mTotalTransferBytes(0), + mPrevBandwidthMeasureTimeUs(0), + mPrevEstimatedBandWidthKbps(0), + mBandWidthCollectFreqMs(5000) { +} // static sp HTTPBase::Create(uint32_t flags) { @@ -42,4 +54,69 @@ sp HTTPBase::Create(uint32_t flags) { } } +void HTTPBase::addBandwidthMeasurement( + size_t numBytes, int64_t delayUs) { + Mutex::Autolock autoLock(mLock); + + BandwidthEntry entry; + entry.mDelayUs = delayUs; + entry.mNumBytes = numBytes; + mTotalTransferTimeUs += delayUs; + mTotalTransferBytes += numBytes; + + mBandwidthHistory.push_back(entry); + if (++mNumBandwidthHistoryItems > 100) { + BandwidthEntry *entry = &*mBandwidthHistory.begin(); + mTotalTransferTimeUs -= entry->mDelayUs; + mTotalTransferBytes -= entry->mNumBytes; + mBandwidthHistory.erase(mBandwidthHistory.begin()); + --mNumBandwidthHistoryItems; + + int64_t timeNowUs = ALooper::GetNowUs(); + if (timeNowUs - mPrevBandwidthMeasureTimeUs >= + mBandWidthCollectFreqMs * 1000LL) { + + if (mPrevBandwidthMeasureTimeUs != 0) { + mPrevEstimatedBandWidthKbps = + (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs); + } + mPrevBandwidthMeasureTimeUs = timeNowUs; + } + } + +} + +bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) { + Mutex::Autolock autoLock(mLock); + + if (mNumBandwidthHistoryItems < 2) { + return false; + } + + *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs); + + return true; +} + +status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) { + Mutex::Autolock autoLock(mLock); + *kbps = mPrevEstimatedBandWidthKbps; + return OK; +} + +status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) { + Mutex::Autolock autoLock(mLock); + + if (freqMs < kMinBandwidthCollectFreqMs + || freqMs > kMaxBandwidthCollectFreqMs) { + + LOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs); + return BAD_VALUE; + } + + LOGI("frequency set to %d ms", freqMs); + mBandWidthCollectFreqMs = freqMs; + return OK; +} + } // namespace android -- cgit v1.1