Merge change I8134d6ed into eclair

* changes:
  DO NOT MERGE: Instead of inserting semi-random delays after submission to surface flinger, delay releasing buffers to the next display time to avoid flickering.
diff --git a/include/media/stagefright/MediaPlayerImpl.h b/include/media/stagefright/MediaPlayerImpl.h
index 53a2088..7adf836 100644
--- a/include/media/stagefright/MediaPlayerImpl.h
+++ b/include/media/stagefright/MediaPlayerImpl.h
@@ -112,7 +112,9 @@
 
     MediaSource *makeShoutcastSource(const char *path);
 
-    void displayOrDiscardFrame(MediaBuffer *buffer, int64_t pts_us);
+    void displayOrDiscardFrame(
+            MediaBuffer **lastBuffer, MediaBuffer *buffer, int64_t pts_us);
+
     void populateISurface();
     void depopulateISurface();
     void sendFrameToISurface(MediaBuffer *buffer);
diff --git a/media/libstagefright/MediaPlayerImpl.cpp b/media/libstagefright/MediaPlayerImpl.cpp
index 622ea7e..8300422 100644
--- a/media/libstagefright/MediaPlayerImpl.cpp
+++ b/media/libstagefright/MediaPlayerImpl.cpp
@@ -40,6 +40,13 @@
 
 namespace android {
 
+static void releaseBufferIfNonNULL(MediaBuffer **buffer) {
+    if (*buffer) {
+        (*buffer)->release();
+        *buffer = NULL;
+    }
+}
+
 MediaPlayerImpl::MediaPlayerImpl(const char *uri)
     : mInitCheck(NO_INIT),
       mTimeSource(NULL),
@@ -51,7 +58,7 @@
       mPlaying(false),
       mPaused(false),
       mSeeking(false) {
-    LOGI("MediaPlayerImpl(%s)", uri);
+    LOGV("MediaPlayerImpl(%s)", uri);
     DataSource::RegisterDefaultSniffers();
 
     status_t err = mClient.connect();
@@ -103,7 +110,7 @@
       mPlaying(false),
       mPaused(false),
       mSeeking(false) {
-    LOGI("MediaPlayerImpl(%d, %lld, %lld)", fd, offset, length);
+    LOGV("MediaPlayerImpl(%d, %lld, %lld)", fd, offset, length);
     DataSource::RegisterDefaultSniffers();
 
     status_t err = mClient.connect();
@@ -140,7 +147,7 @@
 }
 
 void MediaPlayerImpl::play() {
-    LOGI("play");
+    LOGV("play");
 
     if (mPlaying) {
         if (mPaused) {
@@ -229,6 +236,8 @@
     bool firstFrame = true;
     bool eof = false;
 
+    MediaBuffer *lastBuffer = NULL;
+
     status_t err = mVideoDecoder->start();
     CHECK_EQ(err, OK);
 
@@ -241,7 +250,9 @@
         {
             Mutex::Autolock autoLock(mLock);
             if (mSeeking) {
-                LOGI("seek-options to %lld", mSeekTimeUs);
+                releaseBufferIfNonNULL(&lastBuffer);
+
+                LOGV("seek-options to %lld", mSeekTimeUs);
                 options.setSeekTo(mSeekTimeUs);
 
                 mSeeking = false;
@@ -301,19 +312,21 @@
             firstFrame = false;
         }
 
-        displayOrDiscardFrame(buffer, pts_us);
+        displayOrDiscardFrame(&lastBuffer, buffer, pts_us);
     }
 
+    releaseBufferIfNonNULL(&lastBuffer);
+
     mVideoDecoder->stop();
 }
 
 void MediaPlayerImpl::displayOrDiscardFrame(
+        MediaBuffer **lastBuffer,
         MediaBuffer *buffer, int64_t pts_us) {
     for (;;) {
         if (!mPlaying || mPaused) {
-            buffer->release();
-            buffer = NULL;
-
+            releaseBufferIfNonNULL(lastBuffer);
+            *lastBuffer = buffer;
             return;
         }
 
@@ -332,15 +345,13 @@
         if (delay_us < -15000) {
             // We're late.
 
-            LOGI("we're late by %lld ms, dropping a frame\n",
+            LOGV("we're late by %lld ms, dropping a frame\n",
                  -delay_us / 1000);
 
-            buffer->release();
-            buffer = NULL;
+            releaseBufferIfNonNULL(lastBuffer);
+            *lastBuffer = buffer;
             return;
         } else if (delay_us > 100000) {
-            LOGI("we're much too early (by %lld ms)\n",
-                 delay_us / 1000);
             usleep(100000);
             continue;
         } else if (delay_us > 0) {
@@ -352,13 +363,14 @@
 
     {
         Mutex::Autolock autoLock(mLock);
+
         if (mVideoRenderer.get() != NULL) {
             sendFrameToISurface(buffer);
         }
     }
 
-    buffer->release();
-    buffer = NULL;
+    releaseBufferIfNonNULL(lastBuffer);
+    *lastBuffer = buffer;
 }
 
 void MediaPlayerImpl::init() {
@@ -410,7 +422,7 @@
 }
 
 void MediaPlayerImpl::setAudioSource(const sp<MediaSource> &source) {
-    LOGI("setAudioSource");
+    LOGV("setAudioSource");
     mAudioSource = source;
 
     sp<MetaData> meta = source->getFormat();
@@ -420,7 +432,7 @@
 }
 
 void MediaPlayerImpl::setVideoSource(const sp<MediaSource> &source) {
-    LOGI("setVideoSource");
+    LOGV("setVideoSource");
     mVideoSource = source;
 
     sp<MetaData> meta = source->getFormat();
@@ -441,7 +453,7 @@
 }
 
 void MediaPlayerImpl::setSurface(const sp<Surface> &surface) {
-    LOGI("setSurface %p", surface.get());
+    LOGV("setSurface %p", surface.get());
     Mutex::Autolock autoLock(mLock);
 
     depopulateISurface();
@@ -455,7 +467,7 @@
 }
 
 void MediaPlayerImpl::setISurface(const sp<ISurface> &isurface) {
-    LOGI("setISurface %p", isurface.get());
+    LOGV("setISurface %p", isurface.get());
     Mutex::Autolock autoLock(mLock);
 
     depopulateISurface();
@@ -499,7 +511,7 @@
         host = string(host, 0, colon - host.c_str());
     }
 
-    LOGI("Connecting to host '%s', port %d, path '%s'",
+    LOGV("Connecting to host '%s', port %d, path '%s'",
          host.c_str(), port, path.c_str());
 
     HTTPStream *http = new HTTPStream;
@@ -533,7 +545,7 @@
 
             http->disconnect();
 
-            LOGI("Redirecting to %s\n", location.c_str());
+            LOGV("Redirecting to %s\n", location.c_str());
 
             host = string(location, 0, slashPos);
 
@@ -588,7 +600,7 @@
 }
 
 status_t MediaPlayerImpl::seekTo(int64_t time) {
-    LOGI("seekTo %lld", time);
+    LOGV("seekTo %lld", time);
 
     if (mPaused) {
         return UNKNOWN_ERROR;
@@ -651,7 +663,7 @@
 
 void MediaPlayerImpl::setAudioSink(
         const sp<MediaPlayerBase::AudioSink> &audioSink) {
-    LOGI("setAudioSink %p", audioSink.get());
+    LOGV("setAudioSink %p", audioSink.get());
     mAudioSink = audioSink;
 }