Fix a bug on subtitle (SRT).

Bug: 6375542

Change-Id: Ic5dd5a1826b9f78ccbbddc4dec33b0e915b9329f
diff --git a/media/libstagefright/timedtext/TimedTextSRTSource.cpp b/media/libstagefright/timedtext/TimedTextSRTSource.cpp
index 7b1f7f6..1f5d037 100644
--- a/media/libstagefright/timedtext/TimedTextSRTSource.cpp
+++ b/media/libstagefright/timedtext/TimedTextSRTSource.cpp
@@ -212,6 +212,9 @@
 status_t TimedTextSRTSource::getText(
         const MediaSource::ReadOptions *options,
         AString *text, int64_t *startTimeUs, int64_t *endTimeUs) {
+    if (mTextVector.size() == 0) {
+        return ERROR_END_OF_STREAM;
+    }
     text->clear();
     int64_t seekTimeUs;
     MediaSource::ReadOptions::SeekMode mode;
@@ -225,31 +228,38 @@
             mIndex = 0;
         } else {
             // binary search
-            ssize_t low = 0;
-            ssize_t high = mTextVector.size() - 1;
-            ssize_t mid = 0;
+            size_t low = 0;
+            size_t high = mTextVector.size() - 1;
+            size_t mid = 0;
             int64_t currTimeUs;
 
             while (low <= high) {
                 mid = low + (high - low)/2;
                 currTimeUs = mTextVector.keyAt(mid);
-                const int diff = currTimeUs - seekTimeUs;
+                const int64_t diffTime = currTimeUs - seekTimeUs;
 
-                if (diff == 0) {
+                if (diffTime == 0) {
                     break;
-                } else if (diff < 0) {
+                } else if (diffTime < 0) {
                     low = mid + 1;
                 } else {
                     if ((high == mid + 1)
                         && (seekTimeUs < mTextVector.keyAt(high))) {
                         break;
                     }
+                    if (mid < 1) {
+                        break;
+                    }
                     high = mid - 1;
                 }
             }
             mIndex = mid;
         }
     }
+
+    if (mIndex >= mTextVector.size()) {
+        return ERROR_END_OF_STREAM;
+    }
     const TextInfo &info = mTextVector.valueAt(mIndex);
     *startTimeUs = mTextVector.keyAt(mIndex);
     *endTimeUs = info.endTimeUs;
diff --git a/media/libstagefright/timedtext/TimedTextSRTSource.h b/media/libstagefright/timedtext/TimedTextSRTSource.h
index e1371b8..9eeab39 100644
--- a/media/libstagefright/timedtext/TimedTextSRTSource.h
+++ b/media/libstagefright/timedtext/TimedTextSRTSource.h
@@ -56,7 +56,7 @@
         int textLen;
     };
 
-    int mIndex;
+    size_t mIndex;
     KeyedVector<int64_t, TextInfo> mTextVector;
 
     void reset();