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 | a9d0febdd0f0502ec4f29f77b742e11ceae8a30e (patch) | |
tree | 3b2e54ef6951b8adadd0640b5428f1aad5a18d83 /media/libstagefright/HTTPBase.cpp | |
parent | da188fbe4b4d7b91e1440afe5a1279d0fd7a91eb (diff) | |
download | frameworks_base-a9d0febdd0f0502ec4f29f77b742e11ceae8a30e.zip frameworks_base-a9d0febdd0f0502ec4f29f77b742e11ceae8a30e.tar.gz frameworks_base-a9d0febdd0f0502ec4f29f77b742e11ceae8a30e.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 |