diff --git a/native-codec/app/build.gradle b/native-codec/app/build.gradle index b179b0e4..d16d2734 100644 --- a/native-codec/app/build.gradle +++ b/native-codec/app/build.gradle @@ -19,4 +19,12 @@ android { path 'src/main/cpp/CMakeLists.txt' } } + + buildFeatures { + prefab true + } +} + +dependencies { + implementation project(":base") } diff --git a/native-codec/app/src/main/cpp/CMakeLists.txt b/native-codec/app/src/main/cpp/CMakeLists.txt index 68d7464d..a5eafea4 100644 --- a/native-codec/app/src/main/cpp/CMakeLists.txt +++ b/native-codec/app/src/main/cpp/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.22.1) project(NativeCodec LANGUAGES CXX) include(AppLibrary) +find_package(base CONFIG REQUIRED) add_app_library(native-codec-jni SHARED looper.cpp @@ -9,6 +10,8 @@ add_app_library(native-codec-jni SHARED ) target_link_libraries(native-codec-jni + PRIVATE + base::base android log mediandk diff --git a/native-codec/app/src/main/cpp/native-codec-jni.cpp b/native-codec/app/src/main/cpp/native-codec-jni.cpp index 07fcf2da..595dec88 100644 --- a/native-codec/app/src/main/cpp/native-codec-jni.cpp +++ b/native-codec/app/src/main/cpp/native-codec-jni.cpp @@ -49,6 +49,7 @@ #include #include #include +#include typedef struct { int fd; @@ -196,10 +197,8 @@ void mylooper::handle(int what, void* obj) { } } -extern "C" { - -jboolean Java_com_example_nativecodec_NativeCodec_createStreamingMediaPlayer( - JNIEnv* env, jclass, jobject assetMgr, jstring filename) { +jboolean CreateStreamingMediaPlayer(JNIEnv* env, jclass, jobject assetMgr, + jstring filename) { LOGV("@@@ create"); // convert Java string to UTF-8 @@ -268,8 +267,7 @@ jboolean Java_com_example_nativecodec_NativeCodec_createStreamingMediaPlayer( } // set the playing state for the streaming media player -void Java_com_example_nativecodec_NativeCodec_setPlayingStreamingMediaPlayer( - JNIEnv*, jclass, jboolean isPlaying) { +void SetPlayingStreamingMediaPlayer(JNIEnv*, jclass, jboolean isPlaying) { LOGV("@@@ playpause: %d", isPlaying); if (mlooper) { if (isPlaying) { @@ -281,7 +279,7 @@ void Java_com_example_nativecodec_NativeCodec_setPlayingStreamingMediaPlayer( } // shut down the native media system -void Java_com_example_nativecodec_NativeCodec_shutdown(JNIEnv*, jclass) { +void Shutdown(JNIEnv*, jclass) { LOGV("@@@ shutdown"); if (mlooper) { mlooper->post(kMsgDecodeDone, &data, true /* flush */); @@ -296,8 +294,7 @@ void Java_com_example_nativecodec_NativeCodec_shutdown(JNIEnv*, jclass) { } // set the surface -void Java_com_example_nativecodec_NativeCodec_setSurface(JNIEnv* env, jclass, - jobject surface) { +void SetSurface(JNIEnv* env, jclass, jobject surface) { // obtain a native window from a Java surface if (data.window) { ANativeWindow_release(data.window); @@ -308,11 +305,37 @@ void Java_com_example_nativecodec_NativeCodec_setSurface(JNIEnv* env, jclass, } // rewind the streaming media player -void Java_com_example_nativecodec_NativeCodec_rewindStreamingMediaPlayer( - JNIEnv*, jclass) { +void RewindStreamingMediaPlayer(JNIEnv*, jclass) { LOGV("@@@ rewind"); if (mlooper) { mlooper->post(kMsgSeek, &data); } } + +extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* _Nonnull vm, + void* _Nullable) { + JNIEnv* env; + if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) { + return JNI_ERR; + } + + jclass c = env->FindClass("com/example/nativecodec/NativeCodec"); + if (c == nullptr) return JNI_ERR; + + static const JNINativeMethod methods[] = { + {"createStreamingMediaPlayer", + "(Landroid/content/res/AssetManager;Ljava/lang/String;)Z", + reinterpret_cast(CreateStreamingMediaPlayer)}, + {"setPlayingStreamingMediaPlayer", "(Z)V", + reinterpret_cast(SetPlayingStreamingMediaPlayer)}, + {"shutdown", "()V", reinterpret_cast(Shutdown)}, + {"setSurface", "(Landroid/view/Surface;)V", + reinterpret_cast(SetSurface)}, + {"rewindStreamingMediaPlayer", "()V", + reinterpret_cast(RewindStreamingMediaPlayer)}, + }; + int rc = env->RegisterNatives(c, methods, arraysize(methods)); + if (rc != JNI_OK) return rc; + + return JNI_VERSION_1_6; }