diff options
author | James Dong <jdong@google.com> | 2011-05-25 19:37:03 -0700 |
---|---|---|
committer | James Dong <jdong@google.com> | 2011-05-31 15:23:25 -0700 |
commit | 5b1b8a93a07326f1cbc627f09e02988375189e0a (patch) | |
tree | 1da7ba8c401fb96b4de5fab790fba8e51eea78f6 /media/libstagefright/HTTPBase.cpp | |
parent | 65580f9adf6c4d98449ad0716488f9fe3869aa5a (diff) | |
download | frameworks_av-5b1b8a93a07326f1cbc627f09e02988375189e0a.zip frameworks_av-5b1b8a93a07326f1cbc627f09e02988375189e0a.tar.gz frameworks_av-5b1b8a93a07326f1cbc627f09e02988375189e0a.tar.bz2 |
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
Diffstat (limited to 'media/libstagefright/HTTPBase.cpp')
-rw-r--r-- | media/libstagefright/HTTPBase.cpp | 79 |
1 files changed, 78 insertions, 1 deletions
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 <utils/Log.h> + #include "include/HTTPBase.h" #if CHROMIUM_AVAILABLE @@ -22,11 +26,19 @@ #include "include/NuHTTPDataSource.h" +#include <media/stagefright/foundation/ALooper.h> #include <cutils/properties.h> namespace android { -HTTPBase::HTTPBase() {} +HTTPBase::HTTPBase() + : mNumBandwidthHistoryItems(0), + mTotalTransferTimeUs(0), + mTotalTransferBytes(0), + mPrevBandwidthMeasureTimeUs(0), + mPrevEstimatedBandWidthKbps(0), + mBandWidthCollectFreqMs(5000) { +} // static sp<HTTPBase> HTTPBase::Create(uint32_t flags) { @@ -42,4 +54,69 @@ sp<HTTPBase> 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 |