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