diff options
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 |