Make sure a read restarts the prefetcher if necessary.

Change-Id: I87cac0e61e4dce7987ddf29c32f51e1672d1bbed
related-to-bug: 4286618
diff --git a/media/libstagefright/NuCachedSource2.cpp b/media/libstagefright/NuCachedSource2.cpp
index c1aa46e..dc86885 100644
--- a/media/libstagefright/NuCachedSource2.cpp
+++ b/media/libstagefright/NuCachedSource2.cpp
@@ -323,25 +323,28 @@
 }
 
 void NuCachedSource2::restartPrefetcherIfNecessary_l(
-        bool ignoreLowWaterThreshold) {
+        bool ignoreLowWaterThreshold, bool force) {
     static const size_t kGrayArea = 1024 * 1024;
 
     if (mFetching || mFinalStatus != OK) {
         return;
     }
 
-    if (!ignoreLowWaterThreshold
+    if (!ignoreLowWaterThreshold && !force
             && mCacheOffset + mCache->totalSize() - mLastAccessPos
                 >= kLowWaterThreshold) {
         return;
     }
 
     size_t maxBytes = mLastAccessPos - mCacheOffset;
-    if (maxBytes < kGrayArea) {
-        return;
-    }
 
-    maxBytes -= kGrayArea;
+    if (!force) {
+        if (maxBytes < kGrayArea) {
+            return;
+        }
+
+        maxBytes -= kGrayArea;
+    }
 
     size_t actualBytes = mCache->releaseFromStart(maxBytes);
     mCacheOffset += actualBytes;
@@ -413,10 +416,19 @@
 }
 
 ssize_t NuCachedSource2::readInternal(off64_t offset, void *data, size_t size) {
+    CHECK_LE(size, (size_t)kHighWaterThreshold);
+
     LOGV("readInternal offset %lld size %d", offset, size);
 
     Mutex::Autolock autoLock(mLock);
 
+    if (!mFetching) {
+        mLastAccessPos = offset;
+        restartPrefetcherIfNecessary_l(
+                false, // ignoreLowWaterThreshold
+                true); // force
+    }
+
     if (offset < mCacheOffset
             || offset >= (off64_t)(mCacheOffset + mCache->totalSize())) {
         static const off64_t kPadding = 256 * 1024;
diff --git a/media/libstagefright/include/NuCachedSource2.h b/media/libstagefright/include/NuCachedSource2.h
index 2128682..ed3e265 100644
--- a/media/libstagefright/include/NuCachedSource2.h
+++ b/media/libstagefright/include/NuCachedSource2.h
@@ -96,7 +96,9 @@
     status_t seekInternal_l(off64_t offset);
 
     size_t approxDataRemaining_l(status_t *finalStatus);
-    void restartPrefetcherIfNecessary_l(bool ignoreLowWaterThreshold = false);
+
+    void restartPrefetcherIfNecessary_l(
+            bool ignoreLowWaterThreshold = false, bool force = false);
 
     DISALLOW_EVIL_CONSTRUCTORS(NuCachedSource2);
 };