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 MediaMetadataRetriever mMediaMetadataRetriever = new MediaMetadataRetriever(); 511 try { 512 mMediaMetadataRetriever.setDataSource(filePath); 513 } catch(Exception e) { 514 e.printStackTrace(); 515 return false; 516 } 517 Bitmap outThumbnail = mMediaMetadataRetriever.getFrameAtTime(-1); 518 519 //Verify the thumbnail 520 Bitmap goldenBitmap = mBitmapFactory.decodeFile(goldenPath); 521 outputWidth = outThumbnail.getWidth(); 522 outputHeight = outThumbnail.getHeight(); 523 goldenHeight = goldenBitmap.getHeight(); 524 goldenWidth = goldenBitmap.getWidth(); 525 526 //check the image dimension 527 if ((outputWidth != goldenWidth) || (outputHeight != goldenHeight)) 528 return false; 529 530 // Check half line of pixel 531 int x = goldenHeight / 2; 532 for (int j = 1; j < goldenWidth / 2; j++) { 533 if (goldenBitmap.getPixel(x, j) != outThumbnail.getPixel(x, j)) { 534 Log.v(TAG, "pixel = " + goldenBitmap.getPixel(x, j)); 535 return false; 536 } 537 } 538 } catch (Exception e) { 539 Log.v(TAG, e.toString()); 540 return false; 541 } 542 return true; 543 } 544 545 //Load midi file from resources resourcesPlayback(AssetFileDescriptor afd, int expectedDuration)546 public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration) { 547 int duration = 0; 548 try { 549 MediaPlayer mp = new MediaPlayer(); 550 mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength()); 551 mp.prepare(); 552 mp.start(); 553 duration = mp.getDuration(); 554 Thread.sleep(5000); 555 mp.release(); 556 } catch (Exception e) { 557 Log.v(TAG,e.getMessage()); 558 } 559 if (duration > expectedDuration) 560 return true; 561 else 562 return false; 563 } 564 prepareAsyncReset(String filePath)565 public static boolean prepareAsyncReset(String filePath) { 566 //preparesAsync 567 try { 568 MediaPlayer mp = new MediaPlayer(); 569 mp.setDataSource(filePath); 570 mp.prepareAsync(); 571 mp.reset(); 572 mp.release(); 573 } catch (Exception e) { 574 Log.v(TAG,e.getMessage()); 575 return false; 576 } 577 return true; 578 } 579 580 isLooping(String filePath)581 public static boolean isLooping(String filePath) { 582 MediaPlayer mp = null; 583 584 try { 585 mp = new MediaPlayer(); 586 if (mp.isLooping()) { 587 Log.v(TAG, "MediaPlayer.isLooping() returned true after ctor"); 588 return false; 589 } 590 mp.setDataSource(filePath); 591 mp.prepare(); 592 593 mp.setLooping(true); 594 if (!mp.isLooping()) { 595 Log.v(TAG, "MediaPlayer.isLooping() returned false after setLooping(true)"); 596 return false; 597 } 598 599 mp.setLooping(false); 600 if (mp.isLooping()) { 601 Log.v(TAG, "MediaPlayer.isLooping() returned true after setLooping(false)"); 602 return false; 603 } 604 } catch (Exception e) { 605 Log.v(TAG, "Exception : " + e.toString()); 606 return false; 607 } finally { 608 if (mp != null) 609 mp.release(); 610 } 611 612 return true; 613 } 614 isLoopingAfterReset(String filePath)615 public static boolean isLoopingAfterReset(String filePath) { 616 MediaPlayer mp = null; 617 try { 618 mp = new MediaPlayer(); 619 mp.setDataSource(filePath); 620 mp.prepare(); 621 622 mp.setLooping(true); 623 mp.reset(); 624 if (mp.isLooping()) { 625 Log.v(TAG, "MediaPlayer.isLooping() returned true after reset()"); 626 return false; 627 } 628 } catch (Exception e){ 629 Log.v(TAG, "Exception : " + e.toString()); 630 return false; 631 } finally { 632 if (mp != null) 633 mp.release(); 634 } 635 636 return true; 637 } 638 639 /* 640 * Initializes the message looper so that the mediaPlayer object can 641 * receive the callback messages. 642 */ initializeMessageLooper()643 private static void initializeMessageLooper() { 644 Log.v(TAG, "start looper"); 645 new Thread() { 646 @Override 647 public void run() { 648 // Set up a looper to be used by camera. 649 Looper.prepare(); 650 Log.v(TAG, "start loopRun"); 651 // Save the looper so that we can terminate this thread 652 // after we are done with it. 653 mLooper = Looper.myLooper(); 654 mMediaPlayer = new MediaPlayer(); 655 synchronized (mLock) { 656 mInitialized = true; 657 mLock.notify(); 658 } 659 Looper.loop(); // Blocks forever until Looper.quit() is called. 660 Log.v(TAG, "initializeMessageLooper: quit."); 661 } 662 }.start(); 663 } 664 665 /* 666 * Terminates the message looper thread. 667 */ terminateMessageLooper()668 private static void terminateMessageLooper() { 669 mLooper.quit(); 670 mMediaPlayer.release(); 671 } 672 673 static MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() { 674 public void onPrepared(MediaPlayer mp) { 675 synchronized (mPrepareDone) { 676 if(mPrepareReset) { 677 Log.v(TAG, "call Reset"); 678 mMediaPlayer.reset(); 679 } 680 Log.v(TAG, "notify the prepare callback"); 681 mPrepareDone.notify(); 682 mOnPrepareSuccess = true; 683 } 684 } 685 }; 686 prepareAsyncCallback(String filePath, boolean reset)687 public static boolean prepareAsyncCallback(String filePath, boolean reset) throws Exception { 688 //Added the PrepareReset flag which allow us to switch to different 689 //test case. 690 if (reset) { 691 mPrepareReset = true; 692 } 693 694 synchronized (mLock) { 695 initializeMessageLooper(); 696 try { 697 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 698 } catch(Exception e) { 699 Log.v(TAG, "looper was interrupted."); 700 return false; 701 } 702 } 703 try{ 704 mMediaPlayer.setOnPreparedListener(mPreparedListener); 705 mMediaPlayer.setDataSource(filePath); 706 mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder()); 707 mMediaPlayer.prepareAsync(); 708 synchronized (mPrepareDone) { 709 try { 710 mPrepareDone.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 711 } catch (Exception e) { 712 Log.v(TAG, "wait was interrupted."); 713 } 714 } 715 terminateMessageLooper(); 716 }catch (Exception e) { 717 Log.v(TAG,e.getMessage()); 718 } 719 return mOnPrepareSuccess; 720 } 721 722 static MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() { 723 public void onCompletion(MediaPlayer mp) { 724 synchronized (mOnCompletion) { 725 Log.v(TAG, "notify the completion callback"); 726 mOnCompletion.notify(); 727 mOnCompleteSuccess = true; 728 } 729 } 730 }; 731 732 static MediaPlayer.OnErrorListener mOnErrorListener = new MediaPlayer.OnErrorListener() { 733 public boolean onError(MediaPlayer mp, int framework_err, int impl_err) { 734 Log.v(TAG, "playback error"); 735 mPlaybackError = true; 736 mp.reset(); 737 synchronized (mOnCompletion) { 738 Log.v(TAG, "notify the completion callback"); 739 mOnCompletion.notify(); 740 mOnCompleteSuccess = false; 741 } 742 return true; 743 } 744 }; 745 746 static MediaPlayer.OnInfoListener mInfoListener = new MediaPlayer.OnInfoListener() { 747 public boolean onInfo(MediaPlayer mp, int what, int extra) { 748 switch (what) { 749 case MediaPlayer.MEDIA_INFO_UNKNOWN: 750 mMediaInfoUnknownCount++; 751 break; 752 case MediaPlayer.MEDIA_INFO_VIDEO_TRACK_LAGGING: 753 mMediaInfoVideoTrackLaggingCount++; 754 break; 755 case MediaPlayer.MEDIA_INFO_BAD_INTERLEAVING: 756 mMediaInfoBadInterleavingCount++; 757 break; 758 case MediaPlayer.MEDIA_INFO_NOT_SEEKABLE: 759 mMediaInfoNotSeekableCount++; 760 break; 761 case MediaPlayer.MEDIA_INFO_METADATA_UPDATE: 762 mMediaInfoMetdataUpdateCount++; 763 break; 764 } 765 return true; 766 } 767 }; 768 playMediaSample(String fileName)769 public static boolean playMediaSample(String fileName) throws Exception { 770 int duration = 0; 771 int curPosition = 0; 772 int nextPosition = 0; 773 int waittime = 0; 774 mOnCompleteSuccess = false; 775 mMediaInfoUnknownCount = 0; 776 mMediaInfoVideoTrackLaggingCount = 0; 777 mMediaInfoBadInterleavingCount = 0; 778 mMediaInfoNotSeekableCount = 0; 779 mMediaInfoMetdataUpdateCount = 0; 780 mPlaybackError = false; 781 782 initializeMessageLooper(); 783 synchronized (mLock) { 784 try { 785 mLock.wait(WAIT_FOR_COMMAND_TO_COMPLETE); 786 } catch(Exception e) { 787 Log.v(TAG, "looper was interrupted."); 788 return false; 789 } 790 } 791 try { 792 mMediaPlayer.setOnCompletionListener(mCompletionListener); 793 mMediaPlayer.setOnErrorListener(mOnErrorListener); 794 mMediaPlayer.setOnInfoListener(mInfoListener); 795 Log.v(TAG, "playMediaSample: sample file name " + fileName); 796 mMediaPlayer.setDataSource(fileName); 797 mMediaPlayer.setDisplay(MediaFrameworkTest.getSurfaceView().getHolder()); 798 mMediaPlayer.prepare(); 799 duration = mMediaPlayer.getDuration(); 800 Log.v(TAG, "duration of media " + duration); 801 // start to play 802 mMediaPlayer.start(); 803 waittime = duration - mMediaPlayer.getCurrentPosition(); 804 synchronized(mOnCompletion) { 805 try { 806 mOnCompletion.wait(waittime + PLAYBACK_SETTLE_TIME_MS); 807 } catch (Exception e) { 808 Log.v(TAG, "playMediaSamples are interrupted"); 809 return false; 810 } 811 } 812 if (!mOnCompleteSuccess && !mPlaybackError) { 813 Log.e(TAG, "wait timed-out without onCompletion notification"); 814 } 815 terminateMessageLooper(); 816 } catch (Exception e) { 817 Log.v(TAG, "playMediaSample Exception:" + e.getMessage()); 818 } 819 return mOnCompleteSuccess; 820 } 821 } 822