From 50a8a55841a05669e988d05b3a138968c2026fc6 Mon Sep 17 00:00:00 2001 From: lifujun <814084764@qq.com> Date: Thu, 9 Apr 2020 14:45:52 +0800 Subject: [PATCH] feat(android): add global setting --- .../source/premierlibrary/CMakeLists.txt | 1 + .../player/CicadaPlayerGlobalSettings.java | 27 ++++++++ .../premierlibrary/src/main/jni/JniLoader.cpp | 13 +++- .../main/jni/player/JavaGlobalSettings.cpp | 66 +++++++++++++++++++ .../src/main/jni/player/JavaGlobalSettings.h | 24 +++++++ 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 platform/Android/source/premierlibrary/src/main/java/com/cicada/player/CicadaPlayerGlobalSettings.java create mode 100644 platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.cpp create mode 100644 platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.h diff --git a/platform/Android/source/premierlibrary/CMakeLists.txt b/platform/Android/source/premierlibrary/CMakeLists.txt index 1e6c2588f..fb97fe949 100644 --- a/platform/Android/source/premierlibrary/CMakeLists.txt +++ b/platform/Android/source/premierlibrary/CMakeLists.txt @@ -36,6 +36,7 @@ add_library(CicadaPlayer SHARED src/main/jni/player/NativeBase.cpp src/main/jni/player/JavaTrackInfo.cpp src/main/jni/player/JavaMediaInfo.cpp + src/main/jni/player/JavaGlobalSettings.cpp src/main/jni/utils/JavaLogger.cpp ) diff --git a/platform/Android/source/premierlibrary/src/main/java/com/cicada/player/CicadaPlayerGlobalSettings.java b/platform/Android/source/premierlibrary/src/main/java/com/cicada/player/CicadaPlayerGlobalSettings.java new file mode 100644 index 000000000..5bd8a5055 --- /dev/null +++ b/platform/Android/source/premierlibrary/src/main/java/com/cicada/player/CicadaPlayerGlobalSettings.java @@ -0,0 +1,27 @@ +package com.cicada.player; + + +public class CicadaPlayerGlobalSettings { + + static { + System.loadLibrary("alivcffmpeg"); + System.loadLibrary("CicadaPlayer"); + } + + /** + * 设置域名对应的解析ip + * @param host 域名,需指定端口(http默认端口80,https默认端口443)。例如player.alicdn.com:80 + * @param ip 相应的ip,设置为空字符串清空设定。 + */ + /**** + * Set a DNS ip to resolve the host. + * @param host The host. Need to specify the port(http defualt port is 80,https default port is 443). E.g player.alicdn.com:80 + * @param ip The ip address, set as empty string to clear the setting. + */ + public static void setDNSResolve(String host, String ip){ + nSetDNSResolve(host, ip); + } + + private static native void nSetDNSResolve(String host, String ip); + +} diff --git a/platform/Android/source/premierlibrary/src/main/jni/JniLoader.cpp b/platform/Android/source/premierlibrary/src/main/jni/JniLoader.cpp index 9b4e6df10..f5c9756f2 100644 --- a/platform/Android/source/premierlibrary/src/main/jni/JniLoader.cpp +++ b/platform/Android/source/premierlibrary/src/main/jni/JniLoader.cpp @@ -11,6 +11,7 @@ #include "player/JavaCacheConfig.h" #include "player/JavaMediaInfo.h" #include "player/JavaTrackInfo.h" +#include "player/JavaGlobalSettings.h" #include "player/NativeBase.h" using namespace Cicada; @@ -25,16 +26,23 @@ int initJavaInfo(JNIEnv *env) JavaPlayerConfig::init(env); AndroidVSync::init(env); DecoderSurface::init(env); + JavaGlobalSettings::init(env); int result = NativeBase::registerMethod(env); if (result == JNI_FALSE) { - return false; + return JNI_FALSE; } result = JavaLogger::registerMethod(env); if (result == JNI_FALSE) { - return false; + return JNI_FALSE; + } + + result = JavaGlobalSettings::registerMethod(env); + + if (result == JNI_FALSE) { + return JNI_FALSE; } return JNI_TRUE; @@ -50,6 +58,7 @@ void unInitJavaInfo(JNIEnv *env) AndroidVSync::unInit(env); DecoderSurface::unInit(env); JavaLogger::unInit(env); + JavaGlobalSettings::unInit(env); } extern "C" diff --git a/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.cpp b/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.cpp new file mode 100644 index 000000000..180469b6c --- /dev/null +++ b/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.cpp @@ -0,0 +1,66 @@ +// +// Created by lifujun on 2020/4/9. +// + +#include +#include +#include +#include "JavaGlobalSettings.h" + +static char *globalSettingsPath = (char *) ("com/cicada/player/CicadaPlayerGlobalSettings"); +static jclass gj_GlobalSettings_Class = nullptr; + +void JavaGlobalSettings::init(JNIEnv *pEnv) +{ + if (gj_GlobalSettings_Class == nullptr) { + FindClass cls(pEnv, globalSettingsPath); + gj_GlobalSettings_Class = (jclass) pEnv->NewGlobalRef(cls.getClass()); + } +} + +void JavaGlobalSettings::unInit(JNIEnv *pEnv) +{ + if (gj_GlobalSettings_Class != nullptr) { + pEnv->DeleteGlobalRef(gj_GlobalSettings_Class); + gj_GlobalSettings_Class = nullptr; + } +} + +void JavaGlobalSettings::java_setDNSResolve(JNIEnv *mEnv, jclass clazz, jstring jhost, jstring jip) +{ + GetStringUTFChars hostStr(mEnv, jhost); + char *hostChars = hostStr.getChars(); + + if (hostChars == nullptr || strlen(hostChars) == 0) { + return; + } + + Cicada::globalSettings::getSetting()->removeResolve(hostChars, ""); + GetStringUTFChars ipStr(mEnv, jip); + char *ipChars = ipStr.getChars(); + + if (ipChars != nullptr && strlen(ipChars) > 0) { + Cicada::globalSettings::getSetting()->addResolve(hostChars, ipChars); + } +} + + +static JNINativeMethod globalSettings_method_table[] = { + {"nSetDNSResolve", "(Ljava/lang/String;Ljava/lang/String;)V", (void *) JavaGlobalSettings::java_setDNSResolve}, +}; + +int JavaGlobalSettings::registerMethod(JNIEnv *pEnv) +{ + if (gj_GlobalSettings_Class == nullptr) { + return JNI_FALSE; + } + + if (pEnv->RegisterNatives(gj_GlobalSettings_Class, globalSettings_method_table, + sizeof(globalSettings_method_table) / sizeof(JNINativeMethod)) < 0) { + return JNI_FALSE; + } + + return JNI_TRUE; +} + + diff --git a/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.h b/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.h new file mode 100644 index 000000000..7b5feb98d --- /dev/null +++ b/platform/Android/source/premierlibrary/src/main/jni/player/JavaGlobalSettings.h @@ -0,0 +1,24 @@ +// +// Created by lifujun on 2020/4/9. +// + +#ifndef SOURCE_JAVAGLOBALSETTINGS_H +#define SOURCE_JAVAGLOBALSETTINGS_H + + +#include + +class JavaGlobalSettings { + +public: + static void init(JNIEnv *pEnv); + + static void unInit(JNIEnv *pEnv); + + static int registerMethod(JNIEnv *pEnv); + + static void java_setDNSResolve(JNIEnv *mEnv, jclass clazz, jstring host, jstring ip); +}; + + +#endif //SOURCE_JAVAGLOBALSETTINGS_H