From 3f9f8f7b3bd7a9d13a0592e5d3def206704c02d2 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 15 Jan 2024 15:05:38 -0600 Subject: [PATCH] Opt in for video encryption on platforms with fast AES implementations --- app/src/main/jni/moonlight-core/Android.mk | 4 ++- app/src/main/jni/moonlight-core/callbacks.c | 30 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/app/src/main/jni/moonlight-core/Android.mk b/app/src/main/jni/moonlight-core/Android.mk index 8a4a64e3..bf517ac3 100644 --- a/app/src/main/jni/moonlight-core/Android.mk +++ b/app/src/main/jni/moonlight-core/Android.mk @@ -55,7 +55,9 @@ endif LOCAL_LDLIBS := -llog -LOCAL_STATIC_LIBRARIES := libopus libssl libcrypto +LOCAL_STATIC_LIBRARIES := libopus libssl libcrypto cpufeatures LOCAL_LDFLAGS += -Wl,--exclude-libs,ALL include $(BUILD_SHARED_LIBRARY) + +$(call import-module,android/cpufeatures) \ No newline at end of file diff --git a/app/src/main/jni/moonlight-core/callbacks.c b/app/src/main/jni/moonlight-core/callbacks.c index b7e3fcfd..6b6e13ac 100644 --- a/app/src/main/jni/moonlight-core/callbacks.c +++ b/app/src/main/jni/moonlight-core/callbacks.c @@ -8,6 +8,8 @@ #include #include +#include + static OpusMSDecoder* Decoder; static OPUS_MULTISTREAM_CONFIGURATION OpusConfig; @@ -426,6 +428,29 @@ static CONNECTION_LISTENER_CALLBACKS BridgeConnListenerCallbacks = { .setControllerLED = BridgeClSetControllerLED, }; +static bool +hasFastAes() { + if (android_getCpuCount() <= 2) { + return false; + } + + switch (android_getCpuFamily()) { + case ANDROID_CPU_FAMILY_ARM: + return !!(android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_AES); + case ANDROID_CPU_FAMILY_ARM64: + return !!(android_getCpuFeatures() & ANDROID_CPU_ARM64_FEATURE_AES); + case ANDROID_CPU_FAMILY_X86: + case ANDROID_CPU_FAMILY_X86_64: + return !!(android_getCpuFeatures() & ANDROID_CPU_X86_FEATURE_AES_NI); + case ANDROID_CPU_FAMILY_MIPS: + case ANDROID_CPU_FAMILY_MIPS64: + return false; + default: + // Assume new architectures will all have crypto acceleration (RISC-V will) + return true; + } +} + JNIEXPORT jint JNICALL Java_com_limelight_nvstream_jni_MoonBridge_startConnection(JNIEnv *env, jclass clazz, jstring address, jstring appVersion, jstring gfeVersion, @@ -469,6 +494,11 @@ Java_com_limelight_nvstream_jni_MoonBridge_startConnection(JNIEnv *env, jclass c BridgeVideoRendererCallbacks.capabilities = videoCapabilities; + // Enable all encryption features if the platform has fast AES support + if (hasFastAes()) { + streamConfig.encryptionFlags = ENCFLG_ALL; + } + int ret = LiStartConnection(&serverInfo, &streamConfig, &BridgeConnListenerCallbacks,