OSDN Git Service

LiveSession: re-buffer on under run to avoid stutter
authorRobert Shih <robertshih@google.com>
Sat, 30 Aug 2014 01:16:29 +0000 (18:16 -0700)
committerRobert Shih <robertshih@google.com>
Fri, 12 Sep 2014 19:20:46 +0000 (12:20 -0700)
Bug: 13742725
Change-Id: I7dad8876e18084c3c060d08190fa8a72fc2f5bad

media/libstagefright/httplive/LiveSession.cpp
media/libstagefright/httplive/LiveSession.h

index 83481bc..5f566ca 100644 (file)
@@ -81,6 +81,7 @@ LiveSession::LiveSession(
         mDiscontinuities.add(indexToType(i), new AnotherPacketSource(NULL /* meta */));
         mPacketSources.add(indexToType(i), new AnotherPacketSource(NULL /* meta */));
         mPacketSources2.add(indexToType(i), new AnotherPacketSource(NULL /* meta */));
+        mBuffering[i] = false;
     }
 }
 
@@ -133,8 +134,26 @@ status_t LiveSession::dequeueAccessUnit(
 
     sp<AnotherPacketSource> packetSource = mPacketSources.valueFor(stream);
 
+    ssize_t idx = typeToIndex(stream);
     if (!packetSource->hasBufferAvailable(&finalResult)) {
-        return finalResult == OK ? -EAGAIN : finalResult;
+        if (finalResult == OK) {
+            mBuffering[idx] = true;
+            return -EAGAIN;
+        } else {
+            return finalResult;
+        }
+    }
+
+    if (mBuffering[idx]) {
+        if (mSwitchInProgress
+                || packetSource->isFinished(0)
+                || packetSource->getEstimatedDurationUs() > 10000000ll) {
+            mBuffering[idx] = false;
+        }
+    }
+
+    if (mBuffering[idx]) {
+        return -EAGAIN;
     }
 
     // wait for counterpart
@@ -567,6 +586,21 @@ LiveSession::StreamType LiveSession::indexToType(int idx) {
     return (StreamType)(1 << idx);
 }
 
+// static
+ssize_t LiveSession::typeToIndex(int32_t type) {
+    switch (type) {
+        case STREAMTYPE_AUDIO:
+            return 0;
+        case STREAMTYPE_VIDEO:
+            return 1;
+        case STREAMTYPE_SUBTITLES:
+            return 2;
+        default:
+            return -1;
+    };
+    return -1;
+}
+
 void LiveSession::onConnect(const sp<AMessage> &msg) {
     AString url;
     CHECK(msg->findString("url", &url));
index 8a800da..26df543 100644 (file)
@@ -153,6 +153,7 @@ private:
     sp<IMediaHTTPService> mHTTPService;
 
     bool mInPreparationPhase;
+    bool mBuffering[kMaxStreams];
 
     sp<HTTPBase> mHTTPDataSource;
     KeyedVector<String8, String8> mExtraHeaders;
@@ -242,6 +243,7 @@ private:
 
     static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
     static StreamType indexToType(int idx);
+    static ssize_t typeToIndex(int32_t type);
 
     void changeConfiguration(
             int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false);