1 /* 2 * Copyright (C) 2011 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.app.Activity; 20 import android.app.Instrumentation; 21 import android.content.Intent; 22 import android.media.CamcorderProfile; 23 import android.media.MediaRecorder.AudioEncoder; 24 import android.media.MediaRecorder.VideoEncoder; 25 import android.os.Environment; 26 import android.test.suitebuilder.annotation.LargeTest; 27 import android.util.Log; 28 29 import java.io.BufferedWriter; 30 import java.io.File; 31 import java.io.FileWriter; 32 import java.io.Writer; 33 34 import android.test.InstrumentationTestCase; 35 36 /** 37 * Helper for implementing video playback stress test 38 */ 39 abstract class MediaPlayerStressTest extends InstrumentationTestCase { 40 protected static final String VIDEO_TOP_DIR = WorkDir.getMediaDirString(); 41 protected static final int REPEAT_NUMBER_FOR_SHORT_CLIPS = 2; 42 protected static final int REPEAT_NUMBER_FOR_LONG_CLIPS = 1; 43 protected static final int REPEAT_NUMBER_FOR_REPEATED_PLAYBACK = 20; 44 private static final String TAG = "MediaPlayerStressTest"; 45 // whether a video format is supported or not. 46 private final boolean mSupported; 47 48 /** 49 * construct a test case with check of whether the format is supported or not. 50 * @param quality 51 * @param videoCodec 52 * @param audioCodec 53 */ 54 protected MediaPlayerStressTest(int quality, int videoCodec, int audioCodec) { 55 mSupported = VideoPlayerCapability.formatSupported(quality, videoCodec, audioCodec); 56 } 57 58 protected MediaPlayerStressTest() { 59 mSupported = true; // supported if nothing specified 60 } 61 62 /** 63 * provides full path name of video clip for the given media number 64 * @param mediaNumber 65 * @return video file name 66 */ 67 abstract protected String getFullVideoClipName(int mediaNumber); 68 69 private int mTotalPlaybackError; 70 private int mTotalComplete; 71 private int mTotalInfoUnknown; 72 private int mTotalVideoTrackLagging; 73 private int mTotalBadInterleaving; 74 private int mTotalNotSeekable; 75 private int mTotalMetaDataUpdate; 76 77 private void writeTestOutput(String filename, Writer output) throws Exception{ 78 output.write("File Name: " + filename); 79 output.write(" Complete: " + CodecTest.mOnCompleteSuccess); 80 output.write(" Error: " + CodecTest.mPlaybackError); 81 output.write(" Unknown Info: " + CodecTest.mMediaInfoUnknownCount); 82 output.write(" Track Lagging: " + CodecTest.mMediaInfoVideoTrackLaggingCount); 83 output.write(" Bad Interleaving: " + CodecTest.mMediaInfoBadInterleavingCount); 84 output.write(" Not Seekable: " + CodecTest.mMediaInfoNotSeekableCount); 85 output.write(" Info Meta data update: " + CodecTest.mMediaInfoMetdataUpdateCount); 86 output.write("\n"); 87 } 88 89 private void writeTestSummary(Writer output) throws Exception{ 90 output.write("Total Result:\n"); 91 output.write("Total Complete: " + mTotalComplete + "\n"); 92 output.write("Total Error: " + mTotalPlaybackError + "\n"); 93 output.write("Total Unknown Info: " + mTotalInfoUnknown + "\n"); 94 output.write("Total Track Lagging: " + mTotalVideoTrackLagging + "\n" ); 95 output.write("Total Bad Interleaving: " + mTotalBadInterleaving + "\n"); 96 output.write("Total Not Seekable: " + mTotalNotSeekable + "\n"); 97 output.write("Total Info Meta data update: " + mTotalMetaDataUpdate + "\n"); 98 output.write("\n"); 99 } 100 101 private void updateTestResult(){ 102 if (CodecTest.mOnCompleteSuccess){ 103 mTotalComplete++; 104 } 105 else if (CodecTest.mPlaybackError){ 106 mTotalPlaybackError++; 107 } 108 mTotalInfoUnknown += CodecTest.mMediaInfoUnknownCount; 109 mTotalVideoTrackLagging += CodecTest.mMediaInfoVideoTrackLaggingCount; 110 mTotalBadInterleaving += CodecTest.mMediaInfoBadInterleavingCount; 111 mTotalNotSeekable += CodecTest.mMediaInfoNotSeekableCount; 112 mTotalMetaDataUpdate += CodecTest.mMediaInfoMetdataUpdateCount; 113 } 114 115 /** 116 * runs video playback test for the given mediaNumber 117 * @param mediaNumber number of media to be used for playback. 118 * This number is passed to getFullVideoClipName. 119 * @param repeatCounter repeat playback for the given number 120 * @throws Exception 121 */ 122 protected void doTestVideoPlayback(int mediaNumber, int repeatCounter) throws Exception { 123 if (!mSupported) { 124 return; 125 } 126 127 File playbackOutput = new File(WorkDir.getTopDir(), "PlaybackTestResult.txt"); 128 Writer output = new BufferedWriter(new FileWriter(playbackOutput, true)); 129 130 boolean testResult = true; 131 boolean onCompleteSuccess = false; 132 133 Instrumentation inst = getInstrumentation(); 134 Intent intent = new Intent(); 135 136 intent.setClass(inst.getTargetContext(), MediaFrameworkTest.class); 137 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 138 139 Activity act = inst.startActivitySync(intent); 140 141 String mediaName = getFullVideoClipName(mediaNumber); 142 for (int i = 0; i < repeatCounter; i++) { 143 Log.v(TAG, "start playing " + mediaName); 144 onCompleteSuccess = 145 CodecTest.playMediaSample(mediaName); 146 if (!onCompleteSuccess) { 147 //Don't fail the test right away, print out the failure file. 148 Log.v(TAG, "Failure File : " + mediaName); 149 testResult = false; 150 } 151 } 152 Thread.sleep(1000); 153 154 act.finish(); 155 //Write test result to an output file 156 writeTestOutput(mediaName, output); 157 //Get the summary 158 updateTestResult(); 159 160 writeTestSummary(output); 161 output.close(); 162 assertTrue("playback " + mediaName, testResult); 163 } 164 165 protected void doTestVideoPlaybackShort(int mediaNumber) throws Exception { 166 doTestVideoPlayback(mediaNumber, REPEAT_NUMBER_FOR_SHORT_CLIPS); 167 } 168 169 protected void doTestVideoPlaybackLong(int mediaNumber) throws Exception { 170 doTestVideoPlayback(mediaNumber, REPEAT_NUMBER_FOR_LONG_CLIPS); 171 } 172 173 protected void doTestVideoPlaybackRepeated(int mediaNumber) throws Exception { 174 doTestVideoPlayback(mediaNumber, REPEAT_NUMBER_FOR_REPEATED_PLAYBACK); 175 } 176 } 177