• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.mediastress.cts;
18 
19 import android.content.res.AssetFileDescriptor;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapFactory;
22 import android.media.MediaMetadataRetriever;
23 import android.media.MediaPlayer;
24 import android.media.MediaRecorder;
25 import android.os.Looper;
26 import android.os.SystemClock;
27 import android.util.Log;
28 
29 import java.io.IOException;
30 import java.io.InputStream;
31 
32 /**
33  * Junit / Instrumentation test case for the media player api
34  */
35 public class CodecTest {
36     private static String TAG = "CodecTest";
37     private static MediaPlayer mMediaPlayer;
38     private MediaPlayer.OnPreparedListener mOnPreparedListener;
39 
40     private static int WAIT_FOR_COMMAND_TO_COMPLETE = 60000;  //1 min max.
41     private static boolean mInitialized = false;
42     private static boolean mPrepareReset = false;
43     private static Looper mLooper = null;
44     private static final Object mLock = new Object();
45     private static final Object mPrepareDone = new Object();
46     private static final Object mVideoSizeChanged = new Object();
47     private static final Object mOnCompletion = new Object();
48     private static boolean mOnPrepareSuccess = false;
49     private static final long PAUSE_WAIT_TIME = 3000;
50     private static final long WAIT_TIME = 2000;
51     private static final int SEEK_TIME = 10000;
52     private static final int PLAYBACK_SETTLE_TIME_MS = 5000;
53 
54     public static boolean mOnCompleteSuccess = false;
55     public static boolean mPlaybackError = false;
56     public static int mMediaInfoUnknownCount = 0;
57     public static int mMediaInfoVideoTrackLaggingCount = 0;
58     public static int mMediaInfoBadInterleavingCount = 0;
59     public static int mMediaInfoNotSeekableCount = 0;
60     public static int mMediaInfoMetdataUpdateCount = 0;
61 
printCpuInfo()62     public static String printCpuInfo() {
63         String cm = "dumpsys cpuinfo";
64         String cpuinfo = null;
65         int ch;
66         try {
67             Process  p = Runtime.getRuntime().exec(cm);
68             InputStream in = p.getInputStream();
69             StringBuffer sb = new StringBuffer(512);
70             while ( ( ch = in.read() ) != -1 ) {
71                 sb.append((char) ch);
72             }
73             cpuinfo = sb.toString();
74         } catch (IOException e) {
75             Log.v(TAG, e.toString());
76         }
77         return cpuinfo;
78     }
79 
80 
getDuration(String filePath)81     public static int getDuration(String filePath) {
82         Log.v(TAG, "getDuration - " + filePath);
83         MediaPlayer mp = new MediaPlayer();
84         try {
85             mp.setDataSource(filePath);
86             mp.prepare();
87         } catch (Exception e) {
88             Log.v(TAG, e.toString());
89         }
90         int duration = mp.getDuration();
91         Log.v(TAG, "Duration " + duration);
92         mp.release();
93         Log.v(TAG, "release");
94         return duration;
95     }
96 
getCurrentPosition(String filePath)97     public static boolean getCurrentPosition(String filePath) {
98         Log.v(TAG, "GetCurrentPosition - " + filePath);
99         int currentPosition = 0;
100         long t1=0;
101         long t2 =0;
102         MediaPlayer mp = new MediaPlayer();
103         try {
104             mp.setDataSource(filePath);
105             Log.v(TAG, "start playback");
106             mp.prepare();
107             mp.start();
108             t1=SystemClock.uptimeMillis();
109             Thread.sleep(10000);
110             mp.pause();
111             Thread.sleep(PAUSE_WAIT_TIME);
112             t2=SystemClock.uptimeMillis();
113         } catch (Exception e) {
114             Log.v(TAG, e.toString());
115         }
116         currentPosition = mp.getCurrentPosition();
117         mp.stop();
118         mp.release();
119         Log.v(TAG, "mp currentPositon = " + currentPosition + " play duration = " + (t2-t1));
120 
121         if ((currentPosition < ((t2-t1) *1.2)) && (currentPosition > 0))
122             return true;
123         else
124             return false;
125     }
126 
seekTo(String filePath)127     public static boolean seekTo(String filePath) {
128         Log.v(TAG, "seekTo " + filePath);
129         int currentPosition = 0;
130         MediaPlayer mp = new MediaPlayer();
131         try {
132             mp.setDataSource(filePath);
133             mp.prepare();
134             mp.start();
135             mp.seekTo(SEEK_TIME);
136             Thread.sleep(WAIT_TIME);
137             currentPosition = mp.getCurrentPosition();
138         } catch (Exception e) {
139             Log.v(TAG, e.getMessage());
140         }
141         mp.stop();
142         mp.release();
143         Log.v(TAG, "CurrentPosition = " + currentPosition);
144         //The currentposition should be at least greater than the 80% of seek time
145         if ((currentPosition > SEEK_TIME *0.8))
146             return true;
147         else
148             return false;
149     }
150 
setLooping(String filePath)151     public static boolean setLooping(String filePath) {
152         int currentPosition = 0;
153         int duration = 0;
154         long t1 =0;
155         long t2 =0;
156         Log.v (TAG, "SetLooping - " + filePath);
157         MediaPlayer mp = new MediaPlayer();
158         try {
159             mp.setDataSource(filePath);
160             mp.prepare();
161             duration = mp.getDuration();
162             Log.v(TAG, "setLooping duration " + duration);
163             mp.setLooping(true);
164             mp.start();
165             Thread.sleep(5000);
166             mp.seekTo(duration - 5000);
167             t1=SystemClock.uptimeMillis();
168             Thread.sleep(20000);
169             t2=SystemClock.uptimeMillis();
170             Log.v(TAG, "pause");
171             //Bug# 1106852 - IllegalStateException will be thrown if pause is called
172             //in here
173             //mp.pause();
174             currentPosition = mp.getCurrentPosition();
175             Log.v(TAG, "looping position " + currentPosition + "duration = " + (t2-t1));
176         } catch (Exception e) {
177             Log.v(TAG, "Exception : " + e.toString());
178         }
179         mp.stop();
180         mp.release();
181         //The current position should be within 20% of the sleep time
182         //and should be greater than zero.
183         if ((currentPosition < ((t2-t1-5000)*1.2)) && currentPosition > 0)
184             return true;
185         else
186             return false;
187     }
188 
pause(String filePath)189     public static boolean pause(String filePath) throws Exception {
190         Log.v(TAG, "pause - " + filePath);
191         boolean misPlaying = true;
192         boolean pauseResult = false;
193         long t1=0;
194         long t2=0;
195         MediaPlayer mp = new MediaPlayer();
196         mp.setDataSource(filePath);
197         mp.prepare();
198         int duration = mp.getDuration();
199         mp.start();
200         t1=SystemClock.uptimeMillis();
201         Thread.sleep(5000);
202         mp.pause();
203         Thread.sleep(PAUSE_WAIT_TIME);
204         t2=SystemClock.uptimeMillis();
205         misPlaying = mp.isPlaying();
206         int curPosition = mp.getCurrentPosition();
207         Log.v(TAG, filePath + " pause currentPositon " + curPosition);
208         Log.v(TAG, "isPlaying "+ misPlaying + " wait time " + (t2 - t1) );
209         String cpuinfo = printCpuInfo();
210         Log.v(TAG, cpuinfo);
211         if ((curPosition>0) && (curPosition < ((t2-t1) * 1.3)) && (misPlaying == false))
212             pauseResult = true;
213         mp.stop();
214         mp.release();
215         return pauseResult;
216     }
217 
prepareStopRelease(String filePath)218     public static void prepareStopRelease(String filePath) throws Exception {
219         Log.v(TAG, "prepareStopRelease" + filePath);
220         MediaPlayer mp = new MediaPlayer();
221         mp.setDataSource(filePath);
222         mp.prepare();
223         mp.stop();
224         mp.release();
225     }
226 
preparePauseRelease(String filePath)227     public static void preparePauseRelease(String filePath) throws Exception {
228         Log.v(TAG, "preparePauseRelease" + filePath);
229         MediaPlayer mp = new MediaPlayer();
230         mp.setDataSource(filePath);
231         mp.prepare();
232         mp.pause();
233         mp.release();
234     }
235 
236     static MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener =
237         new MediaPlayer.OnVideoSizeChangedListener() {
238             public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
239                 synchronized (mVideoSizeChanged) {
240                     Log.v(TAG, "sizechanged notification received ...");
241                     mVideoSizeChanged.notify();
242                 }
243             }
244     };
245 
246     //Register the videoSizeChanged listener
videoHeight(String filePath)247     public static int videoHeight(String filePath) throws Exception {
248         Log.v(TAG, "videoHeight - " + filePath);
249         int videoHeight = 0;
250         synchronized (mLock) {
251             initializeMessageLooper();
252             try {
253                 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
254             } catch(Exception e) {
255                 Log.v(TAG, "looper was interrupted.");
256                 return 0;
257             }
258         }
259         try {
260             mMediaPlayer.setDataSource(filePath);
261             mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder());
262             mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
263             synchronized (mVideoSizeChanged) {
264                 try {
265                     mMediaPlayer.prepare();
266                     mMediaPlayer.start();
267                     mVideoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
268                 } catch (Exception e) {
269                     Log.v(TAG, "wait was interrupted");
270                 }
271             }
272             videoHeight = mMediaPlayer.getVideoHeight();
273             terminateMessageLooper();
274         } catch (Exception e) {
275             Log.e(TAG, e.getMessage());
276         }
277 
278         return videoHeight;
279     }
280 
281     //Register the videoSizeChanged listener
videoWidth(String filePath)282     public static int videoWidth(String filePath) throws Exception {
283         Log.v(TAG, "videoWidth - " + filePath);
284         int videoWidth = 0;
285 
286         synchronized (mLock) {
287             initializeMessageLooper();
288             try {
289                 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
290             } catch(Exception e) {
291                 Log.v(TAG, "looper was interrupted.");
292                 return 0;
293             }
294         }
295         try {
296             mMediaPlayer.setDataSource(filePath);
297             mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder());
298             mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
299             synchronized (mVideoSizeChanged) {
300                 try {
301                     mMediaPlayer.prepare();
302                     mMediaPlayer.start();
303                     mVideoSizeChanged.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
304                 } catch (Exception e) {
305                     Log.v(TAG, "wait was interrupted");
306                 }
307             }
308             videoWidth = mMediaPlayer.getVideoWidth();
309             terminateMessageLooper();
310         } catch (Exception e) {
311             Log.e(TAG, e.getMessage());
312         }
313         return videoWidth;
314     }
315 
316     //This also test the streaming video which may take a long
317     //time to start the playback.
videoSeekTo(String filePath)318     public static boolean videoSeekTo(String filePath) throws Exception {
319         Log.v(TAG, "videoSeekTo - " + filePath);
320         int currentPosition = 0;
321         int duration = 0;
322         boolean videoResult = false;
323         MediaPlayer mp = new MediaPlayer();
324         mp.setDataSource(filePath);
325         mp.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder());
326         mp.prepare();
327         mp.start();
328 
329         Thread.sleep(5000);
330         duration = mp.getDuration();
331         Log.v(TAG, "video duration " + duration);
332         mp.pause();
333         Thread.sleep(PAUSE_WAIT_TIME);
334         mp.seekTo(duration - 20000 );
335         mp.start();
336         Thread.sleep(1000);
337         mp.pause();
338         Thread.sleep(PAUSE_WAIT_TIME);
339         mp.seekTo(duration/2);
340         mp.start();
341         Thread.sleep(10000);
342         currentPosition = mp.getCurrentPosition();
343         Log.v(TAG, "video currentPosition " + currentPosition);
344         mp.release();
345         if (currentPosition > (duration /2 )*0.9)
346             return true;
347         else
348             return false;
349 
350     }
351 
seekToEnd(String filePath)352     public static boolean seekToEnd(String filePath) {
353         Log.v(TAG, "seekToEnd - " + filePath);
354         int duration = 0;
355         int currentPosition = 0;
356         boolean isPlaying = false;
357         MediaPlayer mp = new MediaPlayer();
358         try {
359             mp.setDataSource(filePath);
360             Log.v(TAG, "start playback");
361             mp.prepare();
362             duration = mp.getDuration();
363             mp.seekTo(duration - 3000);
364             mp.start();
365             Thread.sleep(6000);
366         } catch (Exception e) {}
367         isPlaying = mp.isPlaying();
368         currentPosition = mp.getCurrentPosition();
369         Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
370         mp.stop();
371         mp.release();
372         Log.v(TAG, "duration = " + duration);
373         if (currentPosition < 0.9 * duration || isPlaying)
374             return false;
375         else
376             return true;
377     }
378 
shortMediaStop(String filePath)379     public static boolean shortMediaStop(String filePath) {
380         Log.v(TAG, "shortMediaStop - " + filePath);
381         //This test is only for the short media file
382         int duration = 0;
383         int currentPosition = 0;
384         boolean isPlaying = false;
385         MediaPlayer mp = new MediaPlayer();
386         try {
387             mp.setDataSource(filePath);
388             Log.v(TAG, "start playback");
389             mp.prepare();
390             duration = mp.getDuration();
391             mp.start();
392             Thread.sleep(10000);
393         } catch (Exception e) {}
394         isPlaying = mp.isPlaying();
395         currentPosition = mp.getCurrentPosition();
396         Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
397         mp.stop();
398         mp.release();
399         Log.v(TAG, "duration = " + duration);
400         if (currentPosition > duration || isPlaying)
401             return false;
402         else
403             return true;
404     }
405 
playToEnd(String filePath)406     public static boolean playToEnd(String filePath) {
407         Log.v(TAG, "shortMediaStop - " + filePath);
408         //This test is only for the short media file
409         int duration = 200000;
410         int updateDuration = 0;
411         int currentPosition = 0;
412         boolean isPlaying = false;
413         MediaPlayer mp = new MediaPlayer();
414         try {
415             Thread.sleep(5000);
416             mp.setDataSource(filePath);
417             Log.v(TAG, "start playback");
418             mp.prepare();
419             //duration = mp.getDuration();
420             mp.start();
421             Thread.sleep(50000);
422         } catch (Exception e){}
423         isPlaying = mp.isPlaying();
424         currentPosition = mp.getCurrentPosition();
425         //updateDuration = mp.getDuration();
426         Log.v(TAG, "seekToEnd currentPosition= " + currentPosition + " isPlaying = " + isPlaying);
427         mp.stop();
428         mp.release();
429         //Log.v(TAG, "duration = " + duration);
430         //Log.v(TAG, "Update duration = " + updateDuration);
431         if (currentPosition > duration || isPlaying)
432             return false;
433         else
434             return true;
435     }
436 
seektoBeforeStart(String filePath)437     public static boolean seektoBeforeStart(String filePath){
438         Log.v(TAG, "seektoBeforeStart - " + filePath);
439         //This test is only for the short media file
440         int duration = 0;
441         int currentPosition = 0;
442 
443         MediaPlayer mp = new MediaPlayer();
444         try {
445             mp.setDataSource(filePath);
446             mp.prepare();
447             duration = mp.getDuration();
448             mp.seekTo(duration - 10000);
449             mp.start();
450             currentPosition=mp.getCurrentPosition();
451             mp.stop();
452             mp.release();
453         } catch (Exception e) {}
454         if (currentPosition < duration/2)
455             return false;
456         else
457             return true;
458     }
459 
mediaRecorderRecord(String filePath)460     public static boolean mediaRecorderRecord(String filePath){
461         Log.v(TAG, "SoundRecording - " + filePath);
462         //This test is only for the short media file
463         int duration = 0;
464         try {
465             MediaRecorder mRecorder = new MediaRecorder();
466             mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
467             mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
468             mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
469             mRecorder.setOutputFile(filePath);
470             mRecorder.prepare();
471             mRecorder.start();
472             Thread.sleep(500);
473             mRecorder.stop();
474             Log.v(TAG, "sound recorded");
475             mRecorder.release();
476         } catch (Exception e) {
477             Log.v(TAG, e.toString());
478         }
479 
480         //Verify the recorded file
481         MediaPlayer mp = new MediaPlayer();
482         try {
483             mp.setDataSource(filePath);
484             mp.prepare();
485             duration = mp.getDuration();
486             Log.v(TAG,"Duration " + duration);
487             mp.release();
488         } catch (Exception e) {}
489         //Check the record media file length is greate than zero
490         if (duration > 0)
491             return true;
492         else
493             return false;
494 
495     }
496 
497     //Test for mediaMeta Data Thumbnail
getThumbnail(String filePath, String goldenPath)498     public static boolean getThumbnail(String filePath, String goldenPath) {
499         Log.v(TAG, "getThumbnail - " + filePath);
500 
501         int goldenHeight = 0;
502         int goldenWidth = 0;
503         int outputWidth = 0;
504         int outputHeight = 0;
505 
506         //This test is only for the short media file
507         try {
508             BitmapFactory mBitmapFactory = new BitmapFactory();
509 
510             Bitmap outThumbnail;
511             try (MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever()) {
512                 try {
513                     mediaMetadataRetriever.setDataSource(filePath);
514                 } catch (Exception e) {
515                     e.printStackTrace();
516                     return false;
517                 }
518                 outThumbnail = mediaMetadataRetriever.getFrameAtTime(-1);
519             }
520 
521             //Verify the thumbnail
522             Bitmap goldenBitmap = mBitmapFactory.decodeFile(goldenPath);
523             outputWidth = outThumbnail.getWidth();
524             outputHeight = outThumbnail.getHeight();
525             goldenHeight = goldenBitmap.getHeight();
526             goldenWidth = goldenBitmap.getWidth();
527 
528             //check the image dimension
529             if ((outputWidth != goldenWidth) || (outputHeight != goldenHeight))
530                 return false;
531 
532             // Check half line of pixel
533             int x = goldenHeight / 2;
534             for (int j = 1; j < goldenWidth / 2; j++) {
535                 if (goldenBitmap.getPixel(x, j) != outThumbnail.getPixel(x, j)) {
536                     Log.v(TAG, "pixel = " + goldenBitmap.getPixel(x, j));
537                     return false;
538                 }
539            }
540         } catch (Exception e) {
541             Log.v(TAG, e.toString());
542             return false;
543         }
544         return true;
545     }
546 
547     //Load midi file from resources
resourcesPlayback(AssetFileDescriptor afd, int expectedDuration)548     public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration) {
549         int duration = 0;
550         try {
551             MediaPlayer mp = new MediaPlayer();
552             mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
553             mp.prepare();
554             mp.start();
555             duration = mp.getDuration();
556             Thread.sleep(5000);
557             mp.release();
558         } catch (Exception e) {
559             Log.v(TAG,e.getMessage());
560         }
561         if (duration > expectedDuration)
562             return true;
563         else
564             return false;
565     }
566 
prepareAsyncReset(String filePath)567     public static boolean prepareAsyncReset(String filePath) {
568         //preparesAsync
569         try {
570             MediaPlayer mp = new MediaPlayer();
571             mp.setDataSource(filePath);
572             mp.prepareAsync();
573             mp.reset();
574             mp.release();
575         } catch (Exception e) {
576             Log.v(TAG,e.getMessage());
577             return false;
578         }
579         return true;
580     }
581 
582 
isLooping(String filePath)583     public static boolean isLooping(String filePath) {
584         MediaPlayer mp = null;
585 
586         try {
587             mp = new MediaPlayer();
588             if (mp.isLooping()) {
589                 Log.v(TAG, "MediaPlayer.isLooping() returned true after ctor");
590                 return false;
591             }
592             mp.setDataSource(filePath);
593             mp.prepare();
594 
595             mp.setLooping(true);
596             if (!mp.isLooping()) {
597                 Log.v(TAG, "MediaPlayer.isLooping() returned false after setLooping(true)");
598                 return false;
599             }
600 
601             mp.setLooping(false);
602             if (mp.isLooping()) {
603                 Log.v(TAG, "MediaPlayer.isLooping() returned true after setLooping(false)");
604                 return false;
605             }
606         } catch (Exception e) {
607             Log.v(TAG, "Exception : " + e.toString());
608             return false;
609         } finally {
610             if (mp != null)
611                 mp.release();
612         }
613 
614         return true;
615     }
616 
isLoopingAfterReset(String filePath)617     public static boolean isLoopingAfterReset(String filePath) {
618         MediaPlayer mp = null;
619         try {
620             mp = new MediaPlayer();
621             mp.setDataSource(filePath);
622             mp.prepare();
623 
624             mp.setLooping(true);
625             mp.reset();
626             if (mp.isLooping()) {
627                 Log.v(TAG, "MediaPlayer.isLooping() returned true after reset()");
628                 return false;
629             }
630         } catch (Exception e){
631             Log.v(TAG, "Exception : " + e.toString());
632             return false;
633         } finally {
634             if (mp != null)
635                 mp.release();
636         }
637 
638         return true;
639     }
640 
641     /*
642      * Initializes the message looper so that the mediaPlayer object can
643      * receive the callback messages.
644      */
initializeMessageLooper()645     private static void initializeMessageLooper() {
646         Log.v(TAG, "start looper");
647         new Thread() {
648             @Override
649             public void run() {
650                 // Set up a looper to be used by camera.
651                 Looper.prepare();
652                 Log.v(TAG, "start loopRun");
653                 // Save the looper so that we can terminate this thread
654                 // after we are done with it.
655                 mLooper = Looper.myLooper();
656                 mMediaPlayer = new MediaPlayer();
657                 synchronized (mLock) {
658                     mInitialized = true;
659                     mLock.notify();
660                 }
661                 Looper.loop();  // Blocks forever until Looper.quit() is called.
662                 Log.v(TAG, "initializeMessageLooper: quit.");
663             }
664         }.start();
665     }
666 
667     /*
668      * Terminates the message looper thread.
669      */
terminateMessageLooper()670     private static void terminateMessageLooper() {
671         mLooper.quit();
672         mMediaPlayer.release();
673     }
674 
675     static MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
676         public void onPrepared(MediaPlayer mp) {
677             synchronized (mPrepareDone) {
678                 if(mPrepareReset) {
679                     Log.v(TAG, "call Reset");
680                     mMediaPlayer.reset();
681                 }
682                 Log.v(TAG, "notify the prepare callback");
683                 mPrepareDone.notify();
684                 mOnPrepareSuccess = true;
685             }
686         }
687     };
688 
prepareAsyncCallback(String filePath, boolean reset)689     public static boolean prepareAsyncCallback(String filePath, boolean reset) throws Exception {
690         //Added the PrepareReset flag which allow us to switch to different
691         //test case.
692         if (reset) {
693             mPrepareReset = true;
694         }
695 
696         synchronized (mLock) {
697             initializeMessageLooper();
698             try {
699                 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
700             } catch(Exception e) {
701                 Log.v(TAG, "looper was interrupted.");
702                 return false;
703             }
704         }
705         try{
706             mMediaPlayer.setOnPreparedListener(mPreparedListener);
707             mMediaPlayer.setDataSource(filePath);
708             mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder());
709             mMediaPlayer.prepareAsync();
710             synchronized (mPrepareDone) {
711                 try {
712                     mPrepareDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
713                 } catch (Exception e) {
714                     Log.v(TAG, "wait was interrupted.");
715                 }
716             }
717             terminateMessageLooper();
718         }catch (Exception e) {
719             Log.v(TAG,e.getMessage());
720         }
721        return mOnPrepareSuccess;
722     }
723 
724     static MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
725         public void onCompletion(MediaPlayer mp) {
726             synchronized (mOnCompletion) {
727                 Log.v(TAG, "notify the completion callback");
728                 mOnCompletion.notify();
729                 mOnCompleteSuccess = true;
730             }
731         }
732     };
733 
734     static MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() {
735         public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
736             Log.v(TAG, "playback error");
737             mPlaybackError = true;
738             mp.reset();
739             synchronized (mOnCompletion) {
740                 Log.v(TAG, "notify the completion callback");
741                 mOnCompletion.notify();
742                 mOnCompleteSuccess = false;
743             }
744             return true;
745         }
746     };
747 
748     static MediaPlayer.OnInfoListener mInfoListener = new MediaPlayer.OnInfoListener() {
749         public boolean onInfo(MediaPlayer mp, int what, int extra) {
750             switch (what) {
751                 case MediaPlayer.MEDIA_INFO_UNKNOWN:
752                     mMediaInfoUnknownCount++;
753                     break;
754                 case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING:
755                     mMediaInfoVideoTrackLaggingCount++;
756                     break;
757                 case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING:
758                     mMediaInfoBadInterleavingCount++;
759                     break;
760                 case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE:
761                     mMediaInfoNotSeekableCount++;
762                     break;
763                 case MediaPlayer.MEDIA_INFO_METADATA_UPDATE:
764                     mMediaInfoMetdataUpdateCount++;
765                     break;
766             }
767             return true;
768         }
769     };
770 
playMediaSample(String fileName)771     public static boolean playMediaSample(String fileName) throws Exception {
772         int duration = 0;
773         int curPosition = 0;
774         int nextPosition = 0;
775         int waittime = 0;
776         mOnCompleteSuccess = false;
777         mMediaInfoUnknownCount = 0;
778         mMediaInfoVideoTrackLaggingCount = 0;
779         mMediaInfoBadInterleavingCount = 0;
780         mMediaInfoNotSeekableCount = 0;
781         mMediaInfoMetdataUpdateCount = 0;
782         mPlaybackError = false;
783 
784         initializeMessageLooper();
785         synchronized (mLock) {
786             try {
787                 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE);
788             } catch(Exception e) {
789                 Log.v(TAG, "looper was interrupted.");
790                 return false;
791             }
792         }
793         try {
794             mMediaPlayer.setOnCompletionListener(mCompletionListener);
795             mMediaPlayer.setOnErrorListener(mOnErrorListener);
796             mMediaPlayer.setOnInfoListener(mInfoListener);
797             Log.v(TAG, "playMediaSample: sample file name " + fileName);
798             mMediaPlayer.setDataSource(fileName);
799             mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder());
800             mMediaPlayer.prepare();
801             duration = mMediaPlayer.getDuration();
802             Log.v(TAG, "duration of media " + duration);
803             // start to play
804             mMediaPlayer.start();
805             waittime = duration - mMediaPlayer.getCurrentPosition();
806             synchronized(mOnCompletion) {
807                 try {
808                     mOnCompletion.wait(waittime + PLAYBACK_SETTLE_TIME_MS);
809                 } catch (Exception e) {
810                     Log.v(TAG, "playMediaSamples are interrupted");
811                     return false;
812                 }
813             }
814             if (!mOnCompleteSuccess && !mPlaybackError) {
815                 Log.e(TAG, "wait timed-out without onCompletion notification");
816             }
817             terminateMessageLooper();
818         } catch (Exception e) {
819             Log.v(TAG, "playMediaSample Exception:" + e.getMessage());
820         }
821         return mOnCompleteSuccess;
822     }
823 }
824