blob: 8e88b474812e6942f327036f2d7de8fbef00d00f [file] [log] [blame]
/*
* Copyright 2014 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.cts.util;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaCodecList;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import java.lang.reflect.Method;
import static java.lang.reflect.Modifier.isPublic;
import static java.lang.reflect.Modifier.isStatic;
import java.util.Map;
import android.util.Log;
import java.io.IOException;
public class MediaUtils {
final public static String TAG = "MediaUtils";
/**
* Finds test name (heuristically) and prints out standard skip message.
*
* Since it uses heuristics, this method has only been verified for media
* tests. This centralizes the way to signal a skipped test.
*/
public static void skipTest(String tag, String reason) {
int bestScore = -1;
String testName = "test???";
Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
StackTraceElement[] stack = entry.getValue();
for (int index = 0; index < stack.length; ++index) {
// method name must start with "test"
String methodName = stack[index].getMethodName();
if (!methodName.startsWith("test")) {
continue;
}
int score = 0;
// see if there is a public non-static void method that takes no argument
Class<?> clazz;
try {
clazz = Class.forName(stack[index].getClassName());
++score;
for (final Method method : clazz.getDeclaredMethods()) {
if (method.getName().equals(methodName)
&& isPublic(method.getModifiers())
&& !isStatic(method.getModifiers())
&& method.getParameterTypes().length == 0
&& method.getReturnType().equals(Void.TYPE)) {
++score;
break;
}
}
if (score == 1) {
// if we could read the class, but method is not public void, it is
// not a candidate
continue;
}
} catch (ClassNotFoundException e) {
}
// even if we cannot verify the method signature, there are signals in the stack
// usually test method is invoked by reflection
int depth = 1;
while (index + depth < stack.length
&& stack[index + depth].getMethodName().equals("invoke")
&& stack[index + depth].getClassName().equals(
"java.lang.reflect.Method")) {
++depth;
}
if (depth > 1) {
++score;
// and usually test method is run by runMethod method in android.test package
if (index + depth < stack.length) {
if (stack[index + depth].getClassName().startsWith("android.test.")) {
++score;
}
if (stack[index + depth].getMethodName().equals("runMethod")) {
++score;
}
}
}
if (score > bestScore) {
bestScore = score;
testName = methodName;
}
}
}
Log.i(tag, "SKIPPING " + testName + "(): " + reason);
}
/**
* Finds test name (heuristically) and prints out standard skip message.
*
* Since it uses heuristics, this method has only been verified for media
* tests. This centralizes the way to signal a skipped test.
*/
public static void skipTest(String reason) {
skipTest(TAG, reason);
}
/**
* return true iff all audio and video tracks are supported
*/
public static boolean hasCodecsForMedia(MediaExtractor ex) {
MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
for (int i = 0; i < ex.getTrackCount(); ++i) {
MediaFormat format = ex.getTrackFormat(i);
// only check for audio and video codecs
String mime = format.getString(MediaFormat.KEY_MIME).toLowerCase();
if (!mime.startsWith("audio/") && !mime.startsWith("video/")) {
continue;
}
if (mcl.findDecoderForFormat(format) == null) {
Log.i(TAG, "no decoder for " + format);
return false;
}
}
return true;
}
/**
* return true iff any track starting with mimePrefix is supported
*/
public static boolean hasCodecForMediaAndDomain(MediaExtractor ex, String mimePrefix) {
mimePrefix = mimePrefix.toLowerCase();
MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
for (int i = 0; i < ex.getTrackCount(); ++i) {
MediaFormat format = ex.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.toLowerCase().startsWith(mimePrefix)) {
if (mcl.findDecoderForFormat(format) == null) {
Log.i(TAG, "no decoder for " + format);
} else {
return true;
}
}
}
return false;
}
/**
* return true iff all audio and video tracks are supported
*/
public static boolean hasCodecsForResource(Context context, int resourceId) {
MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
try {
AssetFileDescriptor afd = null;
MediaExtractor ex = null;
try {
afd = context.getResources().openRawResourceFd(resourceId);
ex = new MediaExtractor();
ex.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
return hasCodecsForMedia(ex);
} finally {
if (ex != null) {
ex.release();
}
if (afd != null) {
afd.close();
}
}
} catch (IOException e) {
Log.i(TAG, "could not open resource");
}
return false;
}
/**
* return true iff any track starting with mimePrefix is supported
*/
public static boolean hasCodecForResourceAndDomain(
Context context, int resourceId, String mimePrefix) {
MediaCodecList mcl = new MediaCodecList(MediaCodecList.REGULAR_CODECS);
try {
AssetFileDescriptor afd = null;
MediaExtractor ex = null;
try {
afd = context.getResources().openRawResourceFd(resourceId);
ex = new MediaExtractor();
ex.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
return hasCodecForMediaAndDomain(ex, mimePrefix);
} finally {
if (ex != null) {
ex.release();
}
if (afd != null) {
afd.close();
}
}
} catch (IOException e) {
Log.i(TAG, "could not open resource");
}
return false;
}
}