MediaPlayerStressTest.java revision 147a0c270b5a6078218496b17df9eab70b358b67
1/* 2 * Copyright (C) 2009 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 17package com.android.mediaframeworktest.stress; 18 19import com.android.mediaframeworktest.MediaFrameworkTest; 20 21import android.hardware.Camera; 22import android.media.MediaPlayer; 23import android.media.MediaRecorder; 24import android.test.ActivityInstrumentationTestCase2; 25import android.test.suitebuilder.annotation.LargeTest; 26import android.util.Log; 27import android.view.SurfaceHolder; 28 29import com.android.mediaframeworktest.MediaNames; 30 31import java.util.Random; 32 33/** 34 * Junit / Instrumentation test case for the media player 35 */ 36public class MediaPlayerStressTest extends ActivityInstrumentationTestCase2<MediaFrameworkTest> { 37 private String TAG = "MediaPlayerStressTest"; 38 private MediaRecorder mRecorder; 39 private Camera mCamera; 40 41 private static final int NUMBER_OF_RANDOM_REPOSITION_AND_PLAY = 10; 42 private static final int NUMBER_OF_RANDOM_REPOSITION_AND_PLAY_SHORT = 5; 43 private static final int NUMBER_OF_STRESS_LOOPS = 1000; 44 private static final int PLAYBACK_END_TOLERANCE = 5000; 45 private static final int WAIT_UNTIL_PLAYBACK_FINISH = 515000 ; 46 47 public MediaPlayerStressTest() { 48 super("com.android.mediaframeworktest", MediaFrameworkTest.class); 49 } 50 51 protected void setUp() throws Exception { 52 getActivity(); 53 super.setUp(); 54 } 55 56 @LargeTest 57 public void testStressHWDecoderRelease() throws Exception { 58 SurfaceHolder mSurfaceHolder; 59 long randomseed = System.currentTimeMillis(); 60 Random generator = new Random(randomseed); 61 Log.v(TAG, "Random seed: " + randomseed); 62 int video_duration = MediaNames.STREAM_H264_480_360_1411k_DURATION; 63 int random_play_time; 64 65 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 66 try { 67 for (int i = 0; i < NUMBER_OF_STRESS_LOOPS; i++) { 68 MediaPlayer mp = new MediaPlayer(); 69 mp.setDataSource(MediaNames.STREAM_H264_480_360_1411k); 70 mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 71 mp.prepare(); 72 mp.start(); 73 // seek and play 74 for (int j = 0; j < generator.nextInt(10); j++) { 75 random_play_time = 76 generator.nextInt(MediaNames.STREAM_H264_480_360_1411k_DURATION / 2); 77 Log.v(TAG, "Play time = " + random_play_time); 78 Thread.sleep(random_play_time); 79 int seek_time = MediaNames.STREAM_H264_480_360_1411k_DURATION / 2; 80 Log.v(TAG, "Seek time = " + seek_time); 81 mp.seekTo(seek_time); 82 } 83 mp.release(); 84 } 85 86 } catch (Exception e) { 87 Log.v(TAG, e.toString()); 88 } 89 } 90 91 @LargeTest 92 public void testStressGetCurrentPosition() throws Exception { 93 SurfaceHolder mSurfaceHolder; 94 long randomseed = System.currentTimeMillis(); 95 Random generator = new Random(randomseed); 96 Log.v(TAG, "Random seed: " + randomseed); 97 int video_duration = MediaNames.VIDEO_H263_AAC_DURATION; 98 int random_play_time = 0; 99 int random_seek_time = 0; 100 int random_no_of_seek = 0; 101 102 mSurfaceHolder = MediaFrameworkTest.mSurfaceView.getHolder(); 103 try { 104 for (int i = 0; i < NUMBER_OF_STRESS_LOOPS; i++) { 105 MediaPlayer mp = new MediaPlayer(); 106 mp.setDataSource(MediaNames.VIDEO_H263_AMR); 107 mp.setDisplay(MediaFrameworkTest.mSurfaceView.getHolder()); 108 mp.prepare(); 109 mp.start(); 110 random_no_of_seek = generator.nextInt(10); 111 // make sure the seek at least run once. 112 if (random_no_of_seek == 0) { 113 random_no_of_seek = 1; 114 } 115 // Random seek and play 116 for (int j = 0; j < random_no_of_seek; j++) { 117 random_play_time = 118 generator.nextInt(video_duration / 2); 119 Log.v(TAG, "Play time = " + random_play_time); 120 Thread.sleep(random_play_time); 121 random_seek_time = 122 generator.nextInt(video_duration / 2); 123 Log.v(TAG, "Seek time = " + random_seek_time); 124 mp.seekTo(random_seek_time); 125 } 126 //wait until the movie finish and check the current position 127 //Make sure the wait time is long enough 128 long wait_until_playback_finish = video_duration - random_seek_time + PLAYBACK_END_TOLERANCE * 2; 129 Thread.sleep(wait_until_playback_finish); 130 Log.v(TAG, "CurrentPosition = " + mp.getCurrentPosition()); 131 if ( mp.isPlaying() || mp.getCurrentPosition() > (video_duration + PLAYBACK_END_TOLERANCE)){ 132 assertTrue("Current PlayTime greater than duration", false); 133 } 134 mp.release(); 135 } 136 137 } catch (Exception e) { 138 Log.v(TAG, e.toString()); 139 } 140 } 141} 142 143