mirror of
https://github.com/android/ndk-samples
synced 2025-11-05 23:20:53 +08:00
Migrate native-codec to RegisterNatives.
This commit is contained in:
@@ -19,4 +19,12 @@ android {
|
||||
path 'src/main/cpp/CMakeLists.txt'
|
||||
}
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
prefab true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation project(":base")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <base/macros.h>
|
||||
|
||||
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<void**>(&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<void*>(CreateStreamingMediaPlayer)},
|
||||
{"setPlayingStreamingMediaPlayer", "(Z)V",
|
||||
reinterpret_cast<void*>(SetPlayingStreamingMediaPlayer)},
|
||||
{"shutdown", "()V", reinterpret_cast<void*>(Shutdown)},
|
||||
{"setSurface", "(Landroid/view/Surface;)V",
|
||||
reinterpret_cast<void*>(SetSurface)},
|
||||
{"rewindStreamingMediaPlayer", "()V",
|
||||
reinterpret_cast<void*>(RewindStreamingMediaPlayer)},
|
||||
};
|
||||
int rc = env->RegisterNatives(c, methods, arraysize(methods));
|
||||
if (rc != JNI_OK) return rc;
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user