Merge "If we never triggered a range request but know the content length make sure to not read more data than there could be, otherwise we'd block indefinitely if the server doesn't close the connection."
diff --git a/media/libstagefright/AMRExtractor.cpp b/media/libstagefright/AMRExtractor.cpp
index 3193d5e..2a16d26 100644
--- a/media/libstagefright/AMRExtractor.cpp
+++ b/media/libstagefright/AMRExtractor.cpp
@@ -225,27 +225,32 @@
         return ERROR_IO;
     }
 
-    MediaBuffer *buffer;
-    status_t err = mGroup->acquire_buffer(&buffer);
-    if (err != OK) {
-        return err;
-    }
-
     if (header & 0x83) {
         // Padding bits must be 0.
 
+        LOGE("padding bits must be 0, header is 0x%02x", header);
+
         return ERROR_MALFORMED;
     }
 
     unsigned FT = (header >> 3) & 0x0f;
 
     if (FT > 8 || (!mIsWide && FT > 7)) {
+
+        LOGE("illegal AMR frame type %d", FT);
+
         return ERROR_MALFORMED;
     }
 
     size_t frameSize = getFrameSize(mIsWide, FT);
     CHECK_EQ(frameSize, mFrameSize);
 
+    MediaBuffer *buffer;
+    status_t err = mGroup->acquire_buffer(&buffer);
+    if (err != OK) {
+        return err;
+    }
+
     n = mDataSource->readAt(mOffset, buffer->data(), frameSize);
 
     if (n != (ssize_t)frameSize) {
diff --git a/media/libstagefright/Prefetcher.cpp b/media/libstagefright/Prefetcher.cpp
index cb03979..76ca77b 100644
--- a/media/libstagefright/Prefetcher.cpp
+++ b/media/libstagefright/Prefetcher.cpp
@@ -128,46 +128,52 @@
 
 void Prefetcher::threadFunc() {
     for (;;) {
-        Mutex::Autolock autoLock(mLock);
-        if (mDone) {
-            break;
-        }
-        mCondition.waitRelative(mLock, 10000000ll);
+        sp<PrefetchedSource> minSource;
 
-        int64_t minCacheDurationUs = -1;
-        ssize_t minIndex = -1;
-        for (size_t i = 0; i < mSources.size(); ++i) {
-            sp<PrefetchedSource> source = mSources[i].promote();
+        {
+            Mutex::Autolock autoLock(mLock);
+            if (mDone) {
+                break;
+            }
+            mCondition.waitRelative(mLock, 10000000ll);
 
-            if (source == NULL) {
+            int64_t minCacheDurationUs = -1;
+            ssize_t minIndex = -1;
+            for (size_t i = 0; i < mSources.size(); ++i) {
+                sp<PrefetchedSource> source = mSources[i].promote();
+
+                if (source == NULL) {
+                    continue;
+                }
+
+                int64_t cacheDurationUs;
+                if (!source->getCacheDurationUs(&cacheDurationUs)) {
+                    continue;
+                }
+
+                if (cacheDurationUs >= kMaxCacheDurationUs) {
+                    continue;
+                }
+
+                if (minIndex < 0 || cacheDurationUs < minCacheDurationUs) {
+                    minCacheDurationUs = cacheDurationUs;
+                    minIndex = i;
+                    minSource = source;
+                }
+            }
+
+            if (minIndex < 0) {
                 continue;
             }
-
-            int64_t cacheDurationUs;
-            if (!source->getCacheDurationUs(&cacheDurationUs)) {
-                continue;
-            }
-
-            if (cacheDurationUs >= kMaxCacheDurationUs) {
-                continue;
-            }
-
-            if (minIndex < 0 || cacheDurationUs < minCacheDurationUs) {
-                minCacheDurationUs = cacheDurationUs;
-                minIndex = i;
-            }
         }
 
-        if (minIndex < 0) {
-            continue;
-        }
-
-        sp<PrefetchedSource> source = mSources[minIndex].promote();
-        if (source != NULL) {
-            source->cacheMore();
-        }
+        // Make sure not to hold the lock while calling into the source.
+        // The lock guards the list of sources, not the individual sources
+        // themselves.
+        minSource->cacheMore();
     }
 
+    Mutex::Autolock autoLock(mLock);
     for (size_t i = 0; i < mSources.size(); ++i) {
         sp<PrefetchedSource> source = mSources[i].promote();