auto import from //branches/cupcake/...@137197
diff --git a/media/libmedia/ToneGenerator.cpp b/media/libmedia/ToneGenerator.cpp
index 5416629..8560593 100644
--- a/media/libmedia/ToneGenerator.cpp
+++ b/media/libmedia/ToneGenerator.cpp
@@ -182,8 +182,9 @@
             mLock.lock();
             if (mState == TONE_STARTING) {
                 LOGV("Wait for start callback");
-                if (mWaitCbkCond.waitRelative(mLock, seconds(1)) != NO_ERROR) {
-                    LOGE("--- Immediate start timed out");
+                status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
+                if (lStatus != NO_ERROR) {
+                    LOGE("--- Immediate start timed out, status %d", lStatus);
                     mState = TONE_IDLE;
                     lResult = false;
                 }
@@ -195,13 +196,14 @@
         LOGV("Delayed start\n");
 
         mState = TONE_RESTARTING;
-        if (mWaitCbkCond.waitRelative(mLock, seconds(1)) == NO_ERROR) {
+        status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
+        if (lStatus == NO_ERROR) {
             if (mState != TONE_IDLE) {
                 lResult = true;
             }
             LOGV("cond received");
         } else {
-            LOGE("--- Delayed start timed out");
+            LOGE("--- Delayed start timed out, status %d", lStatus);
             mState = TONE_IDLE;
         }
     }
@@ -368,6 +370,8 @@
             break;
         default:
             LOGV("Extra Cbk");
+            // Force loop exit
+            lNumSmp = 0;
             goto audioCallback_EndLoop;
         }
         
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaNames.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaNames.java
index 5e9c488..07b43bb 100755
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaNames.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaNames.java
@@ -494,4 +494,18 @@
     "/sdcard/media_api/video_stress/h263/mpeg4_QVGA.3gp",
     "/sdcard/media_api/video_stress/h263/mpeg4_SQVGA.mp4"
   };
+  
+  //Streaming test files
+  public static final String STREAM_H264_480_360_1411k = 
+      "http://sridharg.googlejunta.com/yslau/stress_media/h264_regular.mp4"; 
+  public static final String STREAM_WMV = 
+      "http://sridharg.googlejunta.com/yslau/stress_media/bugs.wmv"; 
+  public static final String STREAM_H263_176x144_325k = 
+      "http://sridharg.googlejunta.com/yslau/stress_media/h263_regular.3gp";
+  public static final String STREAM_H264_352x288_1536k = 
+      "http://sridharg.googlejunta.com/yslau/stress_media/h264_highBitRate.mp4";
+  public static final String STREAM_MP3= 
+      "http://sridharg.googlejunta.com/yslau/stress_media/mp3_regular.mp3";
+  public static final String STREAM_MPEG4_QVGA_128k = 
+      "http://sridharg.googlejunta.com/yslau/stress_media/mpeg4_qvga_24fps.3gp";
 }
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/CodecTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/CodecTest.java
index 0e88719..caba47c 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/CodecTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/CodecTest.java
@@ -28,6 +28,7 @@
 import android.media.MediaMetadataRetriever;
 import android.media.MediaPlayer;
 import android.media.MediaRecorder;
+import android.os.Looper;
 import android.os.SystemClock;
 import android.util.Log;
 
@@ -39,6 +40,16 @@
  */  
 public class CodecTest {    
     private static String TAG = "MediaPlayerApiTest";
+    private static MediaPlayer mMediaPlayer;
+    private MediaPlayer.OnPreparedListener mOnPreparedListener;
+    
+    private static int WAIT_FOR_COMMAND_TO_COMPLETE = 10000;  //10 seconds max.
+    private static boolean mInitialized = false;
+    private static Looper mLooper = null;
+    private static final Object lock = new Object();
+    private static final Object prepareDone = new Object();
+    private static boolean onPrepareSuccess = false;
+    
 
     public static String printCpuInfo(){      
         String cm = "dumpsys cpuinfo";
@@ -573,5 +584,89 @@
 
         return true;
     }
+    
+    /*
+     * Initializes the message looper so that the mediaPlayer object can 
+     * receive the callback messages.
+     */
+    private static void initializeMessageLooper() {
+        Log.v(TAG, "start looper");
+        new Thread() {
+            @Override
+            public void run() {
+                // Set up a looper to be used by camera.
+                Looper.prepare();
+                Log.v(TAG, "start loopRun");
+                // Save the looper so that we can terminate this thread 
+                // after we are done with it.
+                mLooper = Looper.myLooper();                
+                mMediaPlayer = new MediaPlayer();                                
+                synchronized (lock) {
+                    mInitialized = true;
+                    lock.notify();
+                }
+                Looper.loop();  // Blocks forever until Looper.quit() is called.
+                Log.v(TAG, "initializeMessageLooper: quit.");
+            }
+        }.start();
+    }
+    
+    /*
+     * Terminates the message looper thread.
+     */
+    private static void terminateMessageLooper() {
+        mLooper.quit();
+        mMediaPlayer.release();
+    }
+    
+    static MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
+        public void onPrepared(MediaPlayer mp) {
+            synchronized (prepareDone) {
+                Log.v(TAG, "notify the prepare callback");
+                prepareDone.notify();
+                onPrepareSuccess = true;
+            }
+        }
+    };
+   
+    public static boolean prepareAsyncCallback(String filePath) throws Exception {
+        int videoWidth = 0;
+        int videoHeight = 0;
+        boolean checkVideoDimension = false;
+        
+        initializeMessageLooper();
+        synchronized (lock) {
+            try {
+                lock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
+            } catch(Exception e) {
+                Log.v(TAG, "looper was interrupted.");
+                return false;
+            }
+        }
+        try{
+            mMediaPlayer.setOnPreparedListener(mPreparedListener);
+            mMediaPlayer.setDataSource(filePath);
+            mMediaPlayer.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder());
+            mMediaPlayer.prepareAsync(); 
+            synchronized (prepareDone) {
+                try {
+                    prepareDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
+                    Log.v(TAG, "setPreview done");
+                } catch (Exception e) {
+                    Log.v(TAG, "wait was interrupted.");
+                }
+            }
+            videoWidth = mMediaPlayer.getVideoWidth();
+            videoHeight = mMediaPlayer.getVideoHeight();
+            
+            terminateMessageLooper();
+        }catch (Exception e){
+            Log.v(TAG,e.getMessage());
+        }      
+       return onPrepareSuccess;
+    }
+    
+    
+    
 }
 
diff --git a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPlayerApiTest.java b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPlayerApiTest.java
index dd94164..ee6a727 100644
--- a/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPlayerApiTest.java
+++ b/media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/functional/MediaPlayerApiTest.java
@@ -52,7 +52,8 @@
         return true;
     }   
     
-   
+
+    
     //Audio    
     //Wait for PV bugs for MP3 duration
     @MediumTest
@@ -410,8 +411,6 @@
       assertTrue("Play mp3 from resources", mp3Resources);         
     }
     
-    //Bug# 1422662
-    @Suppress
     @MediumTest
     public void testPrepareAsyncReset() throws Exception {
       boolean isReset = CodecTest.prepareAsyncReset(MediaNames.STREAM_LARGE_MP3);
@@ -429,5 +428,19 @@
         boolean isLooping = CodecTest.isLoopingAfterReset(MediaNames.AMR);
         assertTrue("isLooping after reset", isLooping);
     }
+    
+    @LargeTest
+    public void testLocalMp3PrepareAsyncCallback() throws Exception {
+        boolean onPrepareSuccess = 
+            CodecTest.prepareAsyncCallback(MediaNames.VIDEO_H263_AMR);
+        assertTrue("LocalMp3prepareAsyncCallback", onPrepareSuccess);
+    }
+    
+    @LargeTest
+    public void testStreamPrepareAsyncCallback() throws Exception {
+        boolean onPrepareSuccess = 
+            CodecTest.prepareAsyncCallback(MediaNames.STREAM_H264_480_360_1411k);
+        assertTrue("StreamH264PrepareAsyncCallback", onPrepareSuccess);
+    }
 }