Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
android cpu abi implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
tanersener committed Nov 10, 2018
1 parent ce7e8f5 commit 7e821df
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
25 changes: 16 additions & 9 deletions android/app/src/main/cpp/mobileffmpeg_abidetect.c
Expand Up @@ -25,7 +25,8 @@ const char *abiDetectClassName = "com/arthenica/mobileffmpeg/AbiDetect";

/** Prototypes of native functions defined by this file. */
JNINativeMethod abiDetectMethods[] = {
{"getAbi", "()Ljava/lang/String;", (void*) Java_com_arthenica_mobileffmpeg_AbiDetect_getAbi}
{"getNativeAbi", "()Ljava/lang/String;", (void*) Java_com_arthenica_mobileffmpeg_AbiDetect_getNativeAbi},
{"getCpuAbi", "()Ljava/lang/String;", (void*) Java_com_arthenica_mobileffmpeg_AbiDetect_getCpuAbi}
};

/**
Expand All @@ -48,7 +49,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_FALSE;
}

if ((*env)->RegisterNatives(env, abiDetectClass, abiDetectMethods, 1) < 0) {
if ((*env)->RegisterNatives(env, abiDetectClass, abiDetectMethods, 2) < 0) {
LOGE("OnLoad failed to RegisterNatives for class %s.\n", abiDetectClassName);
return JNI_FALSE;
}
Expand All @@ -57,18 +58,16 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
}

/**
* Returns running ABI name.
* Returns loaded ABI name.
*
* \param env pointer to native method interface
* \param object reference to the class on which this method is invoked
* \return running ABI name as UTF string
* \return loaded ABI name as UTF string
*/
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getAbi(JNIEnv *env, jclass object) {
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getNativeAbi(JNIEnv *env, jclass object) {

#ifdef MOBILE_FFMPEG_ARM_V7A
return (*env)->NewStringUTF(env, "arm-v7a");
#elif MOBILE_FFMPEG_ARM_V7A_NEON
return (*env)->NewStringUTF(env, "arm-v7a-neon");
#elif MOBILE_FFMPEG_ARM64_V8A
return (*env)->NewStringUTF(env, "arm64-v8a");
#elif MOBILE_FFMPEG_X86
Expand All @@ -79,7 +78,16 @@ JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getAbi(JNIEn
return (*env)->NewStringUTF(env, "unknown");
#endif

/* OLD IMPLEMENTATION
}

/**
* Returns ABI name of the running cpu.
*
* \param env pointer to native method interface
* \param object reference to the class on which this method is invoked
* \return ABI name of the running cpu as UTF string
*/
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getCpuAbi(JNIEnv *env, jclass object) {
AndroidCpuFamily family = android_getCpuFamily();

if (family == ANDROID_CPU_FAMILY_ARM) {
Expand All @@ -104,5 +112,4 @@ JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getAbi(JNIEn
} else {
return (*env)->NewStringUTF(env, ABI_UNKNOWN);
}
*/
}
12 changes: 10 additions & 2 deletions android/app/src/main/cpp/mobileffmpeg_abidetect.h
Expand Up @@ -46,9 +46,17 @@

/*
* Class: com_arthenica_mobileffmpeg_AbiDetect
* Method: getAbi
* Method: getNativeAbi
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getAbi(JNIEnv *, jclass);
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getNativeAbi(JNIEnv *, jclass);

/*
* Class: com_arthenica_mobileffmpeg_AbiDetect
* Method: getCpuAbi
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_arthenica_mobileffmpeg_AbiDetect_getCpuAbi(JNIEnv *, jclass);


#endif /* MOBILE_FFMPEG_ABIDETECT_H */
Expand Up @@ -29,24 +29,51 @@
public class AbiDetect {

static {
armV7aNeonLoaded = false;
System.loadLibrary("mobileffmpeg-abidetect");

/* ALL LIBRARIES LOADED AT STARTUP */
Config.class.getName();
FFmpeg.class.getName();
}

private static boolean armV7aNeonLoaded;

/**
* Default constructor hidden.
*/
private AbiDetect() {
}

static void setArmV7aNeonLoaded(final boolean armV7aNeonLoaded) {
AbiDetect.armV7aNeonLoaded = armV7aNeonLoaded;
}

/**
* <p>Returns loaded ABI name.
*
* @return loaded ABI name
*/
public static String getAbi() {
if (armV7aNeonLoaded) {
return "arm-v7a-neon";
} else {
return getNativeAbi();
}
}

/**
* <p>Returns native loaded ABI name.
*
* @return native loaded ABI name
*/
private native static String getNativeAbi();

/**
* <p>Returns running ABI name.
* <p>Returns ABI name of the running cpu.
*
* @return running ABI name
* @return ABI name of the running cpu
*/
public native static String getAbi();
public native static String getCpuAbi();

}
Expand Up @@ -81,29 +81,28 @@ public class Config {
Log.i(Config.TAG, "Loading mobile-ffmpeg.");

/* ALL LIBRARIES LOADED AT STARTUP */
String abiName = AbiDetect.getAbi();
Abi abi = Abi.from(abiName);
Abi cpuAbi = Abi.from(AbiDetect.getCpuAbi());
FFmpeg.class.getName();

/*
* NEON supported arm-v7a library has a different name
*/
boolean nativeLibraryLoaded = false;
if (abi == Abi.ABI_ARMV7A_NEON) {
if (cpuAbi == Abi.ABI_ARMV7A_NEON) {
try {
System.loadLibrary("mobileffmpeg-armv7a-neon");
nativeLibraryLoaded = true;
AbiDetect.setArmV7aNeonLoaded(true);
} catch (final UnsatisfiedLinkError e) {
Log.i(Config.TAG, "NEON supported armeabi-v7a library not found. Loading default armeabi-v7a library.", e);
abi = Abi.ABI_ARMV7A;
}
}

if (!nativeLibraryLoaded) {
System.loadLibrary("mobileffmpeg");
}

Log.i(Config.TAG, String.format("Loaded mobile-ffmpeg-%s-%s-%s.", getPackageName(), abi.getName(), getVersion()));
Log.i(Config.TAG, String.format("Loaded mobile-ffmpeg-%s-%s-%s.", getPackageName(), AbiDetect.getAbi(), getVersion()));

/* NATIVE LOG LEVEL IS RECEIVED ONLY ON STARTUP */
activeLogLevel = Level.from(getNativeLogLevel());
Expand Down
16 changes: 15 additions & 1 deletion android/jni/Android.mk
Expand Up @@ -4,6 +4,20 @@ $(call import-add-path, $(LOCAL_PATH))
MY_ARM_MODE := arm
MY_PATH := ../app/src/main/cpp

# DEFINE ARCH FLAGS
ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
MY_ARCH_FLAGS := ARM_V7A
endif
ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
MY_ARCH_FLAGS := ARM64_V8A
endif
ifeq ($(TARGET_ARCH_ABI), x86)
MY_ARCH_FLAGS := X86
endif
ifeq ($(TARGET_ARCH_ABI), x86_64)
MY_ARCH_FLAGS := X86_64
endif

include $(CLEAR_VARS)
LOCAL_ARM_MODE := $(MY_ARM_MODE)
LOCAL_MODULE := cpufeatures
Expand All @@ -17,7 +31,7 @@ include $(CLEAR_VARS)
LOCAL_ARM_MODE := $(MY_ARM_MODE)
LOCAL_MODULE := mobileffmpeg-abidetect
LOCAL_SRC_FILES := $(MY_PATH)/mobileffmpeg_abidetect.c
LOCAL_CFLAGS := -Wall -Wextra -Werror -Wno-unused-parameter -I${LOCAL_PATH}/../../prebuilt/android-$(TARGET_ARCH)/ffmpeg/include -I$(NDK_ROOT)/sources/android/cpufeatures
LOCAL_CFLAGS := -Wall -Wextra -Werror -Wno-unused-parameter -I${LOCAL_PATH}/../../prebuilt/android-$(TARGET_ARCH)/ffmpeg/include -I$(NDK_ROOT)/sources/android/cpufeatures -DMOBILE_FFMPEG_${MY_ARCH_FLAGS}
LOCAL_LDLIBS := -llog -lz -landroid
LOCAL_SHARED_LIBRARIES := cpufeatures
include $(BUILD_SHARED_LIBRARY)
Expand Down

0 comments on commit 7e821df

Please sign in to comment.