1 /* 2 * Copyright 2015 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 package android.media.misc.cts; 17 18 import static org.junit.Assume.assumeTrue; 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.util.Log; 25 26 import junit.framework.Assert; 27 28 public class ResourceManagerStubActivity extends Activity { 29 // Define all the error codes specific to this test case here 30 // Test case was skipped as there aren't any supported decoder(s). 31 public static final int RESULT_CODE_NO_DECODER = Activity.RESULT_FIRST_USER + 1; 32 // Test case was skipped as there aren't any supported encoder(s). 33 public static final int RESULT_CODE_NO_ENCODER = Activity.RESULT_FIRST_USER + 2; 34 // Test case was skipped as the device doesn't have any camera available for recording. 35 public static final int RESULT_CODE_NO_CAMERA = Activity.RESULT_FIRST_USER + 3; 36 37 private static final String TAG = "ResourceManagerStubActivity"; 38 private final Object mFinishEvent = new Object(); 39 private int[] mRequestCodes = {0, 1}; 40 private int[] mResults = {RESULT_CANCELED, RESULT_CANCELED}; 41 private int mNumResults = 0; 42 private int mType1 = ResourceManagerTestActivityBase.TYPE_NONSECURE; 43 private int mType2 = ResourceManagerTestActivityBase.TYPE_NONSECURE; 44 private boolean mWaitForReclaim = true; 45 46 private static final String ERROR_INSUFFICIENT_RESOURCES = 47 "* Please check if the omx component is returning OMX_ErrorInsufficientResources " + 48 "properly when the codec failure is due to insufficient resource.\n"; 49 private static final String ERROR_SUPPORTS_MULTIPLE_SECURE_CODECS = 50 "* Please check if this platform supports multiple concurrent secure codec " + 51 "instances. If not, please add below setting in /etc/media_codecs.xml in order " + 52 "to pass the test:\n" + 53 " <Settings>\n" + 54 " <Setting name=\"supports-multiple-secure-codecs\" value=\"false\" />\n" + 55 " </Settings>\n"; 56 private static final String ERROR_SUPPORTS_SECURE_WITH_NON_SECURE_CODEC = 57 "* Please check if this platform supports co-exist of secure and non-secure codec. " + 58 "If not, please add below setting in /etc/media_codecs.xml in order to pass the " + 59 "test:\n" + 60 " <Settings>\n" + 61 " <Setting name=\"supports-secure-with-non-secure-codec\" value=\"false\" />\n" + 62 " </Settings>\n"; 63 64 @Override onCreate(Bundle savedInstanceState)65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 } 68 69 @Override onActivityResult(int requestCode, int resultCode, Intent data)70 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 71 Log.d(TAG, "Activity " + requestCode + " finished with resultCode " + resultCode); 72 mResults[requestCode] = resultCode; 73 if (++mNumResults == mResults.length) { 74 synchronized (mFinishEvent) { 75 mFinishEvent.notify(); 76 } 77 } 78 } 79 processActivityResults()80 private boolean processActivityResults() { 81 boolean result = true; 82 for (int i = 0; result && i < mResults.length; ++i) { 83 switch (mResults[i]) { 84 case RESULT_OK: 85 // Activity completed successfully. 86 break; 87 case RESULT_CODE_NO_DECODER: 88 assumeTrue("Test case was skipped as there aren't any supported decoder(s).", 89 false); 90 break; 91 case RESULT_CODE_NO_ENCODER: 92 assumeTrue("Test case was skipped as there aren't any supported encoder(s).", 93 false); 94 break; 95 case RESULT_CODE_NO_CAMERA: 96 assumeTrue("Test case was skipped as the device doesn't have any " 97 + "camera available for recording.", false); 98 break; 99 default: 100 Log.e(TAG, "Result from activity " + i + " is a fail."); 101 result = false; 102 break; 103 } 104 } 105 106 return result; 107 } 108 testReclaimResource(int type1, int type2, boolean highResolutionForActivity1, boolean highResolutionForActivity2)109 public void testReclaimResource(int type1, int type2, boolean highResolutionForActivity1, 110 boolean highResolutionForActivity2) throws InterruptedException { 111 mType1 = type1; 112 mType2 = type2; 113 if (type1 != ResourceManagerTestActivityBase.TYPE_MIX && type1 != type2) { 114 // in this case, activity2 may not need to reclaim codec from activity1. 115 mWaitForReclaim = false; 116 } else { 117 mWaitForReclaim = true; 118 } 119 Thread thread = new Thread() { 120 @Override 121 public void run() { 122 try { 123 Context context = getApplicationContext(); 124 Intent intent1 = new Intent(context, ResourceManagerTestActivity1.class); 125 intent1.putExtra("test-type", mType1); 126 intent1.putExtra("wait-for-reclaim", mWaitForReclaim); 127 intent1.putExtra("high-resolution", highResolutionForActivity1); 128 startActivityForResult(intent1, mRequestCodes[0]); 129 Thread.sleep(5000); // wait for process to launch and allocate all codecs. 130 131 Intent intent2 = new Intent(context, ResourceManagerTestActivity2.class); 132 intent2.putExtra("test-type", mType2); 133 intent2.putExtra("high-resolution", highResolutionForActivity2); 134 startActivityForResult(intent2, mRequestCodes[1]); 135 136 synchronized (mFinishEvent) { 137 mFinishEvent.wait(); 138 } 139 } catch (Exception e) { 140 Log.d(TAG, "testReclaimResource got exception " + e.toString()); 141 } 142 } 143 }; 144 thread.start(); 145 thread.join(20000 /* millis */); 146 System.gc(); 147 Thread.sleep(5000); // give the gc a chance to release test activities. 148 149 boolean result = processActivityResults(); 150 if (!result) { 151 String failMessage = "The potential reasons for the failure:\n"; 152 StringBuilder reasons = new StringBuilder(); 153 reasons.append(ERROR_INSUFFICIENT_RESOURCES); 154 if (mType1 != mType2) { 155 reasons.append(ERROR_SUPPORTS_SECURE_WITH_NON_SECURE_CODEC); 156 } 157 if (mType1 == ResourceManagerTestActivityBase.TYPE_MIX 158 && mType2 == ResourceManagerTestActivityBase.TYPE_SECURE) { 159 reasons.append(ERROR_SUPPORTS_MULTIPLE_SECURE_CODECS); 160 } 161 Assert.assertTrue(failMessage + reasons.toString(), result); 162 } 163 } 164 testVideoCodecReclaim(boolean highResolution, String mimeType)165 public void testVideoCodecReclaim(boolean highResolution, String mimeType) 166 throws InterruptedException { 167 Thread thread = new Thread() { 168 @Override 169 public void run() { 170 try { 171 Context context = getApplicationContext(); 172 173 // Start the transcoding activity first. 174 Log.d(TAG, "Starting ResourceManagerCodecActivity"); 175 Intent decoders = new Intent(context, ResourceManagerCodecActivity.class); 176 decoders.putExtra("high-resolution", highResolution); 177 decoders.putExtra("mime", mimeType); 178 startActivityForResult(decoders, mRequestCodes[0]); 179 // wait for ResourceManagerCodecActivity to launch and allocate all codecs. 180 Thread.sleep(5000); 181 182 Log.d(TAG, "Starting ResourceManagerRecorderActivity"); 183 // Start the Camera Recording next. 184 Intent recorder = new Intent(context, ResourceManagerRecorderActivity.class); 185 recorder.putExtra("high-resolution", highResolution); 186 recorder.putExtra("mime", mimeType); 187 startActivityForResult(recorder, mRequestCodes[1]); 188 189 synchronized (mFinishEvent) { 190 Log.d(TAG, "Waiting for both actvities to complete"); 191 mFinishEvent.wait(); 192 Log.d(TAG, "Both actvities completed"); 193 } 194 } catch (Exception e) { 195 Log.d(TAG, "testVideoCodecReclaim got exception " + e.toString()); 196 } 197 } 198 }; 199 200 thread.start(); 201 Log.i(TAG, "Started and waiting for Activities"); 202 thread.join(); 203 Log.i(TAG, "Activities completed"); 204 System.gc(); 205 // give the gc a chance to release test activities. 206 Thread.sleep(5000); 207 208 boolean result = processActivityResults(); 209 if (!result) { 210 String failMessage = "The potential reasons for the failure:\n"; 211 StringBuilder reasons = new StringBuilder(); 212 reasons.append(ERROR_INSUFFICIENT_RESOURCES); 213 Assert.assertTrue(failMessage + reasons.toString(), result); 214 } 215 } 216 } 217