am 7ff94577: am 9909b948: Merge "Various fixes to improve resilience of the rtsp stack against spurious errors instead of asserting." into gingerbread

Merge commit '7ff945775210c60e6f113fb00903449cbb05c68a'

* commit '7ff945775210c60e6f113fb00903449cbb05c68a':
  Various fixes to improve resilience of the rtsp stack against spurious errors instead of asserting.
diff --git a/media/libstagefright/rtsp/APacketSource.cpp b/media/libstagefright/rtsp/APacketSource.cpp
index b63798f..b3e86eb 100644
--- a/media/libstagefright/rtsp/APacketSource.cpp
+++ b/media/libstagefright/rtsp/APacketSource.cpp
@@ -764,7 +764,11 @@
     int64_t lastTimeUs;
     CHECK(last->meta()->findInt64("timeUs", &lastTimeUs));
 
-    CHECK_GE(lastTimeUs, firstTimeUs);
+    if (lastTimeUs < firstTimeUs) {
+        LOG(ERROR) << "Huh? Time moving backwards? "
+                   << firstTimeUs << " > " << lastTimeUs;
+        return 0;
+    }
 
     return lastTimeUs - firstTimeUs;
 }
diff --git a/media/libstagefright/rtsp/ARTSPConnection.cpp b/media/libstagefright/rtsp/ARTSPConnection.cpp
index fd6aa62..dcff64c 100644
--- a/media/libstagefright/rtsp/ARTSPConnection.cpp
+++ b/media/libstagefright/rtsp/ARTSPConnection.cpp
@@ -175,19 +175,38 @@
 
     mState = CONNECTING;
 
-    mSocket = socket(AF_INET, SOCK_STREAM, 0);
-
-    MakeSocketBlocking(mSocket, false);
-
     AString url;
     CHECK(msg->findString("url", &url));
 
+    sp<AMessage> reply;
+    CHECK(msg->findMessage("reply", &reply));
+
     AString host, path;
     unsigned port;
-    CHECK(ParseURL(url.c_str(), &host, &port, &path));
+    if (!ParseURL(url.c_str(), &host, &port, &path)) {
+        LOG(ERROR) << "Malformed rtsp url " << url;
+
+        reply->setInt32("result", ERROR_MALFORMED);
+        reply->post();
+
+        mState = DISCONNECTED;
+        return;
+    }
 
     struct hostent *ent = gethostbyname(host.c_str());
-    CHECK(ent != NULL);
+    if (ent == NULL) {
+        LOG(ERROR) << "Unknown host " << host;
+
+        reply->setInt32("result", -ENOENT);
+        reply->post();
+
+        mState = DISCONNECTED;
+        return;
+    }
+
+    mSocket = socket(AF_INET, SOCK_STREAM, 0);
+
+    MakeSocketBlocking(mSocket, false);
 
     struct sockaddr_in remote;
     memset(remote.sin_zero, 0, sizeof(remote.sin_zero));
@@ -198,9 +217,6 @@
     int err = ::connect(
             mSocket, (const struct sockaddr *)&remote, sizeof(remote));
 
-    sp<AMessage> reply;
-    CHECK(msg->findMessage("reply", &reply));
-
     reply->setInt32("server-ip", ntohl(remote.sin_addr.s_addr));
 
     if (err < 0) {
@@ -337,13 +353,20 @@
 
         if (n == 0) {
             // Server closed the connection.
-            TRESPASS();
+            LOG(ERROR) << "Server unexpectedly closed the connection.";
+
+            reply->setInt32("result", ERROR_IO);
+            reply->post();
+            return;
         } else if (n < 0) {
             if (errno == EINTR) {
                 continue;
             }
 
-            TRESPASS();
+            LOG(ERROR) << "Error sending rtsp request.";
+            reply->setInt32("result", -errno);
+            reply->post();
+            return;
         }
 
         numBytesSent += (size_t)n;
@@ -415,13 +438,15 @@
         ssize_t n = recv(mSocket, (uint8_t *)data + offset, size - offset, 0);
         if (n == 0) {
             // Server closed the connection.
+            LOG(ERROR) << "Server unexpectedly closed the connection.";
             return ERROR_IO;
         } else if (n < 0) {
             if (errno == EINTR) {
                 continue;
             }
 
-            TRESPASS();
+            LOG(ERROR) << "Error reading rtsp response.";
+            return -errno;
         }
 
         offset += (size_t)n;
diff --git a/media/libstagefright/rtsp/MyHandler.h b/media/libstagefright/rtsp/MyHandler.h
index 0aefd58..a3864f1 100644
--- a/media/libstagefright/rtsp/MyHandler.h
+++ b/media/libstagefright/rtsp/MyHandler.h
@@ -209,22 +209,24 @@
                                 response->mContent->data(),
                                 response->mContent->size());
 
-                        CHECK(mSessionDesc->isValid());
-
-                        ssize_t i = response->mHeaders.indexOfKey("content-base");
-                        if (i >= 0) {
-                            mBaseURL = response->mHeaders.valueAt(i);
+                        if (!mSessionDesc->isValid()) {
+                            result = ERROR_MALFORMED;
                         } else {
-                            i = response->mHeaders.indexOfKey("content-location");
+                            ssize_t i = response->mHeaders.indexOfKey("content-base");
                             if (i >= 0) {
                                 mBaseURL = response->mHeaders.valueAt(i);
                             } else {
-                                mBaseURL = mSessionURL;
+                                i = response->mHeaders.indexOfKey("content-location");
+                                if (i >= 0) {
+                                    mBaseURL = response->mHeaders.valueAt(i);
+                                } else {
+                                    mBaseURL = mSessionURL;
+                                }
                             }
-                        }
 
-                        CHECK_GT(mSessionDesc->countTracks(), 1u);
-                        setupTrack(1);
+                            CHECK_GT(mSessionDesc->countTracks(), 1u);
+                            setupTrack(1);
+                        }
                     }
                 }
 
@@ -333,13 +335,17 @@
                     sp<ARTSPResponse> response =
                         static_cast<ARTSPResponse *>(obj.get());
 
-                    CHECK_EQ(response->mStatusCode, 200u);
+                    if (response->mStatusCode != 200) {
+                        result = UNKNOWN_ERROR;
+                    } else {
+                        parsePlayResponse(response);
 
-                    parsePlayResponse(response);
+                        sp<AMessage> timeout = new AMessage('tiou', id());
+                        timeout->post(kStartupTimeoutUs);
+                    }
+                }
 
-                    sp<AMessage> timeout = new AMessage('tiou', id());
-                    timeout->post(kStartupTimeoutUs);
-                } else {
+                if (result != OK) {
                     sp<AMessage> reply = new AMessage('disc', id());
                     mConn->disconnect(reply);
                 }
@@ -477,6 +483,11 @@
 
                 uint32_t seqNum = (uint32_t)accessUnit->int32Data();
 
+                if (mSeekPending) {
+                    LOG(INFO) << "we're seeking, dropping stale packet.";
+                    break;
+                }
+
                 if (seqNum < track->mFirstSeqNumInSegment) {
                     LOG(INFO) << "dropping stale access-unit "
                               << "(" << seqNum << " < "
@@ -600,18 +611,26 @@
                 LOG(INFO) << "PLAY completed with result "
                      << result << " (" << strerror(-result) << ")";
 
-                CHECK_EQ(result, (status_t)OK);
+                if (result == OK) {
+                    sp<RefBase> obj;
+                    CHECK(msg->findObject("response", &obj));
+                    sp<ARTSPResponse> response =
+                        static_cast<ARTSPResponse *>(obj.get());
 
-                sp<RefBase> obj;
-                CHECK(msg->findObject("response", &obj));
-                sp<ARTSPResponse> response =
-                    static_cast<ARTSPResponse *>(obj.get());
+                    if (response->mStatusCode != 200) {
+                        result = UNKNOWN_ERROR;
+                    } else {
+                        parsePlayResponse(response);
 
-                CHECK_EQ(response->mStatusCode, 200u);
+                        LOG(INFO) << "seek completed.";
+                    }
+                }
 
-                parsePlayResponse(response);
+                if (result != OK) {
+                    LOG(ERROR) << "seek failed, aborting.";
+                    (new AMessage('abor', id()))->post();
+                }
 
-                LOG(INFO) << "seek completed.";
                 mSeekPending = false;
                 break;
             }