Merge "media: cts for media resource manager." into mnc-dev
diff --git a/tests/tests/media/AndroidManifest.xml b/tests/tests/media/AndroidManifest.xml
index e913f05..e4b6f6b 100644
--- a/tests/tests/media/AndroidManifest.xml
+++ b/tests/tests/media/AndroidManifest.xml
@@ -52,6 +52,21 @@
<category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
</intent-filter>
</activity>
+ <activity android:name="android.media.cts.ResourceManagerStubActivity"
+ android:label="ResourceManagerStubActivity">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" />
+ </intent-filter>
+ </activity>
+ <activity android:name="android.media.cts.ResourceManagerTestActivity1"
+ android:label="ResourceManagerTestActivity1"
+ android:process=":mediaCodecTestProcess1">
+ </activity>
+ <activity android:name="android.media.cts.ResourceManagerTestActivity2"
+ android:label="ResourceManagerTestActivity2"
+ android:process=":mediaCodecTestProcess2">
+ </activity>
<activity android:name="android.media.cts.RingtonePickerActivity"
android:label="RingtonePickerActivity">
<intent-filter>
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java b/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java
new file mode 100644
index 0000000..214ced4
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.media.cts;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import junit.framework.Assert;
+
+public class ResourceManagerStubActivity extends Activity {
+ private static final String TAG = "ResourceManagerStubActivity";
+ private final Object mFinishEvent = new Object();
+ private int[] mRequestCodes = {0, 1};
+ private boolean[] mResults = {false, false};
+ private int mNumResults = 0;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+ Log.d(TAG, "Activity " + requestCode + " finished.");
+ mResults[requestCode] = (resultCode == RESULT_OK);
+ if (++mNumResults == mResults.length) {
+ synchronized (mFinishEvent) {
+ mFinishEvent.notify();
+ }
+ }
+ }
+
+ public boolean testReclaimResource() throws InterruptedException {
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ try {
+ Context context = getApplicationContext();
+ Intent intent1 = new Intent(context, ResourceManagerTestActivity1.class);
+ startActivityForResult(intent1, mRequestCodes[0]);
+ Thread.sleep(2000); // wait for process to launch.
+
+ Intent intent2 = new Intent(context, ResourceManagerTestActivity2.class);
+ startActivityForResult(intent2, mRequestCodes[1]);
+
+ synchronized (mFinishEvent) {
+ mFinishEvent.wait();
+ }
+ } catch(Exception e) {
+ Log.d(TAG, "testReclaimResource got exception " + e.toString());
+ }
+ }
+ };
+ thread.start();
+ thread.join(10000);
+
+ for (int i = 0; i < mResults.length; ++i) {
+ Assert.assertTrue("Result from activity " + i + " is a fail.", mResults[i]);
+ }
+ return true;
+ }
+}
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTest.java b/tests/tests/media/src/android/media/cts/ResourceManagerTest.java
new file mode 100644
index 0000000..5170aac
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.media.cts;
+
+import android.os.Bundle;
+import android.test.ActivityInstrumentationTestCase2;
+
+public class ResourceManagerTest
+ extends ActivityInstrumentationTestCase2<ResourceManagerStubActivity> {
+
+ public ResourceManagerTest() {
+ super("com.android.cts.media", ResourceManagerStubActivity.class);
+ }
+
+ public void testReclaimResource() throws Exception {
+ Bundle extras = new Bundle();
+ ResourceManagerStubActivity activity = launchActivity(
+ "com.android.cts.media", ResourceManagerStubActivity.class, extras);
+ activity.testReclaimResource();
+ activity.finish();
+ }
+}
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java
new file mode 100644
index 0000000..aff3f03
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.media.cts;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+public class ResourceManagerTestActivity1 extends ResourceManagerTestActivityBase {
+ private static final int MAX_INSTANCES = 32;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ TAG = "ResourceManagerTestActivity1";
+
+ Log.d(TAG, "onCreate called.");
+ super.onCreate(savedInstanceState);
+ moveTaskToBack(true);
+
+ if (allocateCodecs(MAX_INSTANCES) == MAX_INSTANCES) {
+ // haven't reached the limit with MAX_INSTANCES, report RESULT_OK directly and
+ // skip additional test.
+ setResult(Activity.RESULT_OK);
+ finish();
+ }
+ useCodecs();
+ }
+}
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity2.java b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity2.java
new file mode 100644
index 0000000..f4c57f5
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity2.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.media.cts;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+
+public class ResourceManagerTestActivity2 extends ResourceManagerTestActivityBase {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ TAG = "ResourceManagerTestActivity2";
+
+ Log.d(TAG, "onCreate called.");
+ super.onCreate(savedInstanceState);
+
+ if (allocateCodecs(1) == 1) {
+ setResult(Activity.RESULT_OK);
+ finish();
+ }
+ }
+}
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java
new file mode 100644
index 0000000..9c48fc4
--- /dev/null
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.media.cts;
+
+import android.app.Activity;
+import android.media.MediaCodec;
+import android.media.MediaCodecInfo;
+import android.media.MediaCodecInfo.CodecCapabilities;
+import android.media.MediaCodecInfo.VideoCapabilities;
+import android.media.MediaCodecList;
+import android.media.MediaFormat;
+import android.os.Bundle;
+import android.util.Log;
+import java.io.IOException;
+import java.util.Vector;
+
+public class ResourceManagerTestActivityBase extends Activity {
+ protected String TAG;
+ private static final int IFRAME_INTERVAL = 10; // 10 seconds between I-frames
+ private static final String MIME = MediaFormat.MIMETYPE_VIDEO_AVC;
+
+ private Vector<MediaCodec> mCodecs = new Vector<MediaCodec>();
+
+ private class TestCodecCallback extends MediaCodec.Callback {
+ @Override
+ public void onInputBufferAvailable(MediaCodec codec, int index) {
+ Log.d(TAG, "onInputBufferAvailable " + codec.toString());
+ }
+
+ @Override
+ public void onOutputBufferAvailable(
+ MediaCodec codec, int index, MediaCodec.BufferInfo info) {
+ Log.d(TAG, "onOutputBufferAvailable " + codec.toString());
+ }
+
+ @Override
+ public void onError(MediaCodec codec, MediaCodec.CodecException e) {
+ Log.d(TAG, "onError " + codec.toString() + " errorCode " + e.getErrorCode());
+ }
+
+ @Override
+ public void onOutputFormatChanged(MediaCodec codec, MediaFormat format) {
+ Log.d(TAG, "onOutputFormatChanged " + codec.toString());
+ }
+ }
+
+ private MediaCodec.Callback mCallback = new TestCodecCallback();
+
+ private static MediaFormat getTestFormat(VideoCapabilities vcaps) {
+ int maxWidth = vcaps.getSupportedWidths().getUpper();
+ int maxHeight = vcaps.getSupportedHeightsFor(maxWidth).getUpper();
+ int maxBitrate = vcaps.getBitrateRange().getUpper();
+ int maxFramerate = vcaps.getSupportedFrameRatesFor(maxWidth, maxHeight)
+ .getUpper().intValue();
+
+ MediaFormat format = MediaFormat.createVideoFormat(MIME, maxWidth, maxHeight);
+ format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
+ CodecCapabilities.COLOR_FormatYUV420Flexible);
+ format.setInteger(MediaFormat.KEY_BIT_RATE, maxBitrate);
+ format.setInteger(MediaFormat.KEY_FRAME_RATE, maxFramerate);
+ format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
+ return format;
+ }
+
+ private MediaCodecInfo getTestCodecInfo() {
+ // Use avc decoder for testing.
+ boolean isEncoder = false;
+
+ MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
+ for (MediaCodecInfo info : mcl.getCodecInfos()) {
+ if (info.isEncoder() != isEncoder) {
+ continue;
+ }
+ CodecCapabilities caps;
+ try {
+ caps = info.getCapabilitiesForType(MIME);
+ } catch (IllegalArgumentException e) {
+ // mime is not supported
+ continue;
+ }
+ return info;
+ }
+
+ return null;
+ }
+
+ protected int allocateCodecs(int max) {
+ MediaCodecInfo info = getTestCodecInfo();
+ if (info == null) {
+ // skip the test
+ return 0;
+ }
+
+ String name = info.getName();
+ VideoCapabilities vcaps = info.getCapabilitiesForType(MIME).getVideoCapabilities();
+ MediaFormat format = getTestFormat(vcaps);
+ for (int i = 0; i < max; ++i) {
+ try {
+ Log.d(TAG, "Create codec " + name + " #" + i);
+ MediaCodec codec = MediaCodec.createByCodecName(name);
+ codec.setCallback(mCallback);
+ Log.d(TAG, "Configure codec " + format);
+ codec.configure(format, null, null, 0);
+ Log.d(TAG, "Start codec " + format);
+ codec.start();
+ mCodecs.add(codec);
+ } catch (IllegalArgumentException e) {
+ Log.d(TAG, "IllegalArgumentException " + e.getMessage());
+ break;
+ } catch (IOException e) {
+ Log.d(TAG, "IOException " + e.getMessage());
+ break;
+ } catch (MediaCodec.CodecException e) {
+ Log.d(TAG, "CodecException 0x" + Integer.toHexString(e.getErrorCode()));
+ break;
+ }
+ }
+
+ return mCodecs.size();
+ }
+
+ private void doUseCodecs() {
+ int current = 0;
+ try {
+ for (current = 0; current < mCodecs.size(); ++current) {
+ mCodecs.get(current).getName();
+ }
+ } catch (MediaCodec.CodecException e) {
+ Log.d(TAG, "useCodecs got CodecException 0x" + Integer.toHexString(e.getErrorCode()));
+ if (e.getErrorCode() == MediaCodec.CodecException.ERROR_RECLAIMED) {
+ Log.d(TAG, "Remove codec " + current + " from the list");
+ mCodecs.remove(current);
+ setResult(Activity.RESULT_OK);
+ finish();
+ }
+ return;
+ }
+ }
+
+ private Thread mWorkerThread;
+ protected void useCodecs() {
+ mWorkerThread = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (true) {
+ doUseCodecs();
+ }
+ }
+ });
+ mWorkerThread.start();
+ }
+
+ @Override
+ protected void onDestroy() {
+ Log.d(TAG, "onDestroy called.");
+ super.onDestroy();
+
+ for (int i = 0; i < mCodecs.size(); ++i) {
+ mCodecs.get(i).release();
+ }
+ }
+}