Skip to content

Commit 59797e6

Browse files
theandi666Android (Google) Code Review
authored andcommitted
Merge "Allow a system property "media.stagefright.cache-params" to override cache/prefetcher"
2 parents 1370987 + 594f0ba commit 59797e6

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

media/libstagefright/NuCachedSource2.cpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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

484491
ssize_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

media/libstagefright/include/NuCachedSource2.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ struct NuCachedSource2 : public DataSource {
6363
friend struct AHandlerReflector<NuCachedSource2>;
6464

6565
enum {
66-
kPageSize = 65536,
67-
kHighWaterThreshold = 20 * 1024 * 1024,
68-
kLowWaterThreshold = 4 * 1024 * 1024,
66+
kPageSize = 65536,
67+
kDefaultHighWaterThreshold = 20 * 1024 * 1024,
68+
kDefaultLowWaterThreshold = 4 * 1024 * 1024,
6969

7070
// Read data after a 15 sec timeout whether we're actively
7171
// fetching or not.
72-
kKeepAliveIntervalUs = 15000000,
72+
kDefaultKeepAliveIntervalUs = 15000000,
7373
};
7474

7575
enum {
@@ -99,6 +99,12 @@ struct NuCachedSource2 : public DataSource {
9999

100100
int32_t mNumRetriesLeft;
101101

102+
size_t mHighwaterThresholdBytes;
103+
size_t mLowwaterThresholdBytes;
104+
105+
// If the keep-alive interval is 0, keep-alives are disabled.
106+
int64_t mKeepAliveIntervalUs;
107+
102108
void onMessageReceived(const sp<AMessage> &msg);
103109
void onFetch();
104110
void onRead(const sp<AMessage> &msg);
@@ -112,6 +118,9 @@ struct NuCachedSource2 : public DataSource {
112118
void restartPrefetcherIfNecessary_l(
113119
bool ignoreLowWaterThreshold = false, bool force = false);
114120

121+
void updateCacheParamsFromSystemProperty();
122+
void updateCacheParamsFromString(const char *s);
123+
115124
DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
116125
};
117126

0 commit comments

Comments
 (0)