Increase the size of the pages used in CachingDataSource (total amount of memory used remains the same) to compensate for reduced locality of audio/video data requests. Also fixes a mistaken trailing "\r\n" in the range header and better error handling on http connection.

Change-Id: Ic9a6ef204362bc9afdc61e081c76bc62e5ef92ad
related-to-bug: 2580785
diff --git a/include/media/stagefright/HTTPDataSource.h b/include/media/stagefright/HTTPDataSource.h
index b5d1e7a..83be475 100644
--- a/include/media/stagefright/HTTPDataSource.h
+++ b/include/media/stagefright/HTTPDataSource.h
@@ -54,7 +54,7 @@
 
 private:
     enum {
-        kBufferSize = 32 * 1024,
+        kBufferSize = 64 * 1024,
 
         // If we encounter a socket-read error we'll try reconnecting
         // and restarting the read for at most this many times.
diff --git a/media/libstagefright/AwesomePlayer.cpp b/media/libstagefright/AwesomePlayer.cpp
index e7022f4..63dfa67 100644
--- a/media/libstagefright/AwesomePlayer.cpp
+++ b/media/libstagefright/AwesomePlayer.cpp
@@ -1091,7 +1091,7 @@
         }
 
         dataSource = new CachingDataSource(
-                mConnectingDataSource, 32 * 1024, 20);
+                mConnectingDataSource, 64 * 1024, 10);
 
         mConnectingDataSource.clear();
     } else {
diff --git a/media/libstagefright/DataSource.cpp b/media/libstagefright/DataSource.cpp
index 284e3bc..86e4bfe 100644
--- a/media/libstagefright/DataSource.cpp
+++ b/media/libstagefright/DataSource.cpp
@@ -105,7 +105,7 @@
         if (httpSource->connect() != OK) {
             return NULL;
         }
-        source = new CachingDataSource(httpSource, 32 * 1024, 20);
+        source = new CachingDataSource(httpSource, 64 * 1024, 10);
     } else {
         // Assume it's a filename.
         source = new FileSource(uri);
diff --git a/media/libstagefright/HTTPDataSource.cpp b/media/libstagefright/HTTPDataSource.cpp
index 61ffa8d..48f8e7a 100644
--- a/media/libstagefright/HTTPDataSource.cpp
+++ b/media/libstagefright/HTTPDataSource.cpp
@@ -35,7 +35,8 @@
 // connected.
 static bool PerformRedirectIfNecessary(
         HTTPStream *http, const String8 &headers,
-        string *host, string *path, int *port) {
+        string *host, string *path, int *port,
+        status_t *result) {
     String8 request;
     request.append("GET ");
     request.append(path->c_str());
@@ -52,6 +53,8 @@
         err = http->receive_header(&http_status);
     }
 
+    *result = err;
+
     if (err != OK) {
         return false;
     }
@@ -181,6 +184,7 @@
          host.c_str(), port, path.c_str());
 
     int numRedirectsRemaining = 5;
+    status_t result;
     do {
         status_t err = mHttp->connect(host.c_str(), port);
 
@@ -194,9 +198,19 @@
 
             return err;
         }
-    } while (PerformRedirectIfNecessary(mHttp, mHeaders, &host, &path, &port)
+    } while (PerformRedirectIfNecessary(
+                mHttp, mHeaders, &host, &path, &port, &result)
              && numRedirectsRemaining-- > 0);
 
+    if (result != OK) {
+        // An error occurred while attempting to follow redirections/connect.
+        Mutex::Autolock autoLock(mStateLock);
+
+        mState = DISCONNECTED;
+
+        return result;
+    }
+
     string value;
     if (mHttp->find_header_value("Content-Length", &value)) {
         char *end;
@@ -282,7 +296,7 @@
 
     char range[128];
     if (offset > 0) {
-        sprintf(range, "Range: bytes=%d-\r\n\r\n", offset);
+        sprintf(range, "Range: bytes=%d-\r\n", offset);
     } else {
         range[0] = '\0';
     }
@@ -313,6 +327,7 @@
     }
 
     if ((http_status / 100) != 2) {
+        LOGE("HTTP request failed, http status = %d", http_status);
         return UNKNOWN_ERROR;
     }
 
@@ -349,6 +364,10 @@
 
         memcpy(data, (const char *)mBuffer + (offset - mBufferOffset), copy);
 
+        if (copy < size) {
+            LOGV("short read (1), returning %d vs. %d requested", copy, size);
+        }
+
         return copy;
     }