From 9634665ed6eedebc2082f071255886ee3e051c85 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Wed, 19 Jan 2011 15:07:19 -0800 Subject: [PATCH] Prefill the cache before trying to instantiate the media extractor. The latter is an operation that otherwise could block on the datasource for a significant amount of time. During that time we'd be unable to abort the preparation phase without this prefill. Change-Id: Ia42496d88a11314386ea8797d665bf4e94871e30 related-to-bug: 3362836 --- media/libstagefright/AwesomePlayer.cpp | 33 +++++++++++++++++++++--- media/libstagefright/NuCachedSource2.cpp | 2 ++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp index 58c4a2e8591e8..11ac56ce296ee 100644 --- a/media/libstagefright/AwesomePlayer.cpp +++ b/media/libstagefright/AwesomePlayer.cpp @@ -58,6 +58,8 @@ namespace android { static int64_t kLowWaterMarkUs = 2000000ll; // 2secs static int64_t kHighWaterMarkUs = 10000000ll; // 10secs +static const size_t kLowWaterMarkBytes = 40000; +static const size_t kHighWaterMarkBytes = 200000; struct AwesomeEvent : public TimedEventQueue::Event { AwesomeEvent( @@ -610,9 +612,6 @@ void AwesomePlayer::onBufferingUpdate() { // We don't know the bitrate of the stream, use absolute size // limits to maintain the cache. - const size_t kLowWaterMarkBytes = 40000; - const size_t kHighWaterMarkBytes = 200000; - if ((mFlags & PLAYING) && !eos && (cachedDataRemaining < kLowWaterMarkBytes)) { LOGI("cache is running low (< %d) , pausing.", @@ -1535,6 +1534,34 @@ status_t AwesomePlayer::finishSetDataSource_l() { mConnectingDataSource.clear(); dataSource = mCachedSource; + + // We're going to prefill the cache before trying to instantiate + // the extractor below, as the latter is an operation that otherwise + // could block on the datasource for a significant amount of time. + // During that time we'd be unable to abort the preparation phase + // without this prefill. + + mLock.unlock(); + + for (;;) { + status_t finalStatus; + size_t cachedDataRemaining = + mCachedSource->approxDataRemaining(&finalStatus); + + if (finalStatus != OK || cachedDataRemaining >= kHighWaterMarkBytes + || (mFlags & PREPARE_CANCELLED)) { + break; + } + + usleep(200000); + } + + mLock.lock(); + + if (mFlags & PREPARE_CANCELLED) { + LOGI("Prepare cancelled while waiting for initial cache fill."); + return UNKNOWN_ERROR; + } } else if (!strncasecmp(mUri.string(), "httplive://", 11)) { String8 uri("http://"); uri.append(mUri.string() + 11); diff --git a/media/libstagefright/NuCachedSource2.cpp b/media/libstagefright/NuCachedSource2.cpp index 20f16555517a1..741aa1ca01246 100644 --- a/media/libstagefright/NuCachedSource2.cpp +++ b/media/libstagefright/NuCachedSource2.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +//#define LOG_NDEBUG 0 #define LOG_TAG "NuCachedSource2" #include @@ -487,4 +488,5 @@ void NuCachedSource2::getDrmInfo(DecryptHandle **handle, DrmManagerClient **clie String8 NuCachedSource2::getUri() { return mSource->getUri(); } + } // namespace android