2121#include " include/NuCachedSource2.h"
2222#include " include/HTTPBase.h"
2323
24+ #include < cutils/properties.h>
2425#include < media/stagefright/foundation/ADebug.h>
2526#include < media/stagefright/foundation/AMessage.h>
2627#include < media/stagefright/MediaErrors.h>
@@ -186,7 +187,12 @@ NuCachedSource2::NuCachedSource2(const sp<DataSource> &source)
186187 mLastAccessPos(0 ),
187188 mFetching(true ),
188189 mLastFetchTimeUs(-1 ),
189- mNumRetriesLeft(kMaxNumRetries ) {
190+ mNumRetriesLeft(kMaxNumRetries ),
191+ mHighwaterThresholdBytes(kDefaultHighWaterThreshold ),
192+ mLowwaterThresholdBytes(kDefaultLowWaterThreshold ),
193+ mKeepAliveIntervalUs(kDefaultKeepAliveIntervalUs ) {
194+ updateCacheParamsFromSystemProperty ();
195+
190196 mLooper ->setName (" NuCachedSource2" );
191197 mLooper ->registerHandler (mReflector );
192198 mLooper ->start ();
@@ -318,7 +324,8 @@ void NuCachedSource2::onFetch() {
318324 bool keepAlive =
319325 !mFetching
320326 && mFinalStatus == OK
321- && ALooper::GetNowUs () >= mLastFetchTimeUs + kKeepAliveIntervalUs ;
327+ && mKeepAliveIntervalUs > 0
328+ && ALooper::GetNowUs () >= mLastFetchTimeUs + mKeepAliveIntervalUs ;
322329
323330 if (mFetching || keepAlive) {
324331 if (keepAlive) {
@@ -329,7 +336,7 @@ void NuCachedSource2::onFetch() {
329336
330337 mLastFetchTimeUs = ALooper::GetNowUs ();
331338
332- if (mFetching && mCache ->totalSize () >= kHighWaterThreshold ) {
339+ if (mFetching && mCache ->totalSize () >= mHighwaterThresholdBytes ) {
333340 LOGI (" Cache full, done prefetching for now" );
334341 mFetching = false ;
335342 }
@@ -392,7 +399,7 @@ void NuCachedSource2::restartPrefetcherIfNecessary_l(
392399
393400 if (!ignoreLowWaterThreshold && !force
394401 && mCacheOffset + mCache ->totalSize () - mLastAccessPos
395- >= kLowWaterThreshold ) {
402+ >= mLowwaterThresholdBytes ) {
396403 return ;
397404 }
398405
@@ -482,7 +489,7 @@ size_t NuCachedSource2::approxDataRemaining_l(status_t *finalStatus) {
482489}
483490
484491ssize_t NuCachedSource2::readInternal (off64_t offset, void *data, size_t size) {
485- CHECK_LE (size, (size_t )kHighWaterThreshold );
492+ CHECK_LE (size, (size_t )mHighwaterThresholdBytes );
486493
487494 LOGV (" readInternal offset %lld size %d" , offset, size);
488495
@@ -580,4 +587,44 @@ String8 NuCachedSource2::getMIMEType() const {
580587 return mSource ->getMIMEType ();
581588}
582589
590+ void NuCachedSource2::updateCacheParamsFromSystemProperty () {
591+ char value[PROPERTY_VALUE_MAX];
592+ if (!property_get (" media.stagefright.cache-params" , value, NULL )) {
593+ return ;
594+ }
595+
596+ updateCacheParamsFromString (value);
597+ }
598+
599+ void NuCachedSource2::updateCacheParamsFromString (const char *s) {
600+ ssize_t lowwaterMarkKb, highwaterMarkKb;
601+ unsigned keepAliveSecs;
602+
603+ if (sscanf (s, " %ld/%ld/%u" ,
604+ &lowwaterMarkKb, &highwaterMarkKb, &keepAliveSecs) != 3
605+ || lowwaterMarkKb >= highwaterMarkKb) {
606+ LOGE (" Failed to parse cache parameters from '%s'." , s);
607+ return ;
608+ }
609+
610+ if (lowwaterMarkKb >= 0 ) {
611+ mLowwaterThresholdBytes = lowwaterMarkKb * 1024 ;
612+ } else {
613+ mLowwaterThresholdBytes = kDefaultLowWaterThreshold ;
614+ }
615+
616+ if (highwaterMarkKb >= 0 ) {
617+ mHighwaterThresholdBytes = highwaterMarkKb * 1024 ;
618+ } else {
619+ mHighwaterThresholdBytes = kDefaultHighWaterThreshold ;
620+ }
621+
622+ mKeepAliveIntervalUs = keepAliveSecs * 1000000ll ;
623+
624+ LOGV (" lowwater = %d bytes, highwater = %d bytes, keepalive = %lld us" ,
625+ mLowwaterThresholdBytes ,
626+ mHighwaterThresholdBytes ,
627+ mKeepAliveIntervalUs );
628+ }
629+
583630} // namespace android
0 commit comments