mirror of
https://github.com/android/ndk-samples
synced 2025-11-06 23:55:35 +08:00
Migrate native-codec to RegisterNatives.
This commit is contained in:
@@ -19,4 +19,12 @@ android {
|
|||||||
path 'src/main/cpp/CMakeLists.txt'
|
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)
|
project(NativeCodec LANGUAGES CXX)
|
||||||
|
|
||||||
include(AppLibrary)
|
include(AppLibrary)
|
||||||
|
find_package(base CONFIG REQUIRED)
|
||||||
|
|
||||||
add_app_library(native-codec-jni SHARED
|
add_app_library(native-codec-jni SHARED
|
||||||
looper.cpp
|
looper.cpp
|
||||||
@@ -9,6 +10,8 @@ add_app_library(native-codec-jni SHARED
|
|||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(native-codec-jni
|
target_link_libraries(native-codec-jni
|
||||||
|
PRIVATE
|
||||||
|
base::base
|
||||||
android
|
android
|
||||||
log
|
log
|
||||||
mediandk
|
mediandk
|
||||||
|
|||||||
@@ -49,6 +49,7 @@
|
|||||||
#include <android/asset_manager.h>
|
#include <android/asset_manager.h>
|
||||||
#include <android/asset_manager_jni.h>
|
#include <android/asset_manager_jni.h>
|
||||||
#include <android/native_window_jni.h>
|
#include <android/native_window_jni.h>
|
||||||
|
#include <base/macros.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int fd;
|
int fd;
|
||||||
@@ -196,10 +197,8 @@ void mylooper::handle(int what, void* obj) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
jboolean CreateStreamingMediaPlayer(JNIEnv* env, jclass, jobject assetMgr,
|
||||||
|
jstring filename) {
|
||||||
jboolean Java_com_example_nativecodec_NativeCodec_createStreamingMediaPlayer(
|
|
||||||
JNIEnv* env, jclass, jobject assetMgr, jstring filename) {
|
|
||||||
LOGV("@@@ create");
|
LOGV("@@@ create");
|
||||||
|
|
||||||
// convert Java string to UTF-8
|
// 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
|
// set the playing state for the streaming media player
|
||||||
void Java_com_example_nativecodec_NativeCodec_setPlayingStreamingMediaPlayer(
|
void SetPlayingStreamingMediaPlayer(JNIEnv*, jclass, jboolean isPlaying) {
|
||||||
JNIEnv*, jclass, jboolean isPlaying) {
|
|
||||||
LOGV("@@@ playpause: %d", isPlaying);
|
LOGV("@@@ playpause: %d", isPlaying);
|
||||||
if (mlooper) {
|
if (mlooper) {
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
@@ -281,7 +279,7 @@ void Java_com_example_nativecodec_NativeCodec_setPlayingStreamingMediaPlayer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// shut down the native media system
|
// shut down the native media system
|
||||||
void Java_com_example_nativecodec_NativeCodec_shutdown(JNIEnv*, jclass) {
|
void Shutdown(JNIEnv*, jclass) {
|
||||||
LOGV("@@@ shutdown");
|
LOGV("@@@ shutdown");
|
||||||
if (mlooper) {
|
if (mlooper) {
|
||||||
mlooper->post(kMsgDecodeDone, &data, true /* flush */);
|
mlooper->post(kMsgDecodeDone, &data, true /* flush */);
|
||||||
@@ -296,8 +294,7 @@ void Java_com_example_nativecodec_NativeCodec_shutdown(JNIEnv*, jclass) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set the surface
|
// set the surface
|
||||||
void Java_com_example_nativecodec_NativeCodec_setSurface(JNIEnv* env, jclass,
|
void SetSurface(JNIEnv* env, jclass, jobject surface) {
|
||||||
jobject surface) {
|
|
||||||
// obtain a native window from a Java surface
|
// obtain a native window from a Java surface
|
||||||
if (data.window) {
|
if (data.window) {
|
||||||
ANativeWindow_release(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
|
// rewind the streaming media player
|
||||||
void Java_com_example_nativecodec_NativeCodec_rewindStreamingMediaPlayer(
|
void RewindStreamingMediaPlayer(JNIEnv*, jclass) {
|
||||||
JNIEnv*, jclass) {
|
|
||||||
LOGV("@@@ rewind");
|
LOGV("@@@ rewind");
|
||||||
if (mlooper) {
|
if (mlooper) {
|
||||||
mlooper->post(kMsgSeek, &data);
|
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