Skip to content

Commit 3d69c88

Browse files
committed
chore: minor cleanup
1 parent 78401b6 commit 3d69c88

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

native/src/cpp/mpv/MPV.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,20 +580,22 @@ JNIEXPORT jobject JNICALL Java_dev_silenium_multimedia_core_mpv_MPVKt_createRend
580580
}
581581
const auto object = env->NewGlobalRef(self);
582582

583-
const auto glProcMethod = env->GetMethodID(env->GetObjectClass(self), "getGlProc", "(Ljava/lang/String;)J");
584-
if (glProcMethod == nullptr) {
585-
std::cerr << "Method not found: getGlProc" << std::endl;
586-
return mpvResultFailure(env, "GetMethodID", MPV_ERROR_GENERIC);
587-
}
588583
mpv_opengl_init_params gl_params{
589-
.get_proc_address = fnptr<void *(void *, const char *)>([=](void *ctx, const char *name) -> void * {
584+
.get_proc_address = fnptr<void *(void *, const char *)>([object, jvm](void *ctx, const char *name) -> void * {
590585
JNIEnv *jni_env;
591586
const auto res = jvm->AttachCurrentThread(reinterpret_cast<void **>(&jni_env), nullptr);
592587
if (res != JNI_OK) {
593588
std::cerr << "Failed to attach current thread" << std::endl;
594589
return nullptr;
595590
}
596591

592+
// TODO: Fix crash here during renderer disposal
593+
const auto glProcMethod = jni_env->GetMethodID(jni_env->GetObjectClass(object), "getGlProc", "(Ljava/lang/String;)J");
594+
if (glProcMethod == nullptr) {
595+
std::cerr << "Method not found: getGlProc" << std::endl;
596+
return nullptr;
597+
}
598+
597599
const auto nameStr = jni_env->NewStringUTF(name);
598600
const auto ret = jni_env->CallLongMethod(object, glProcMethod, nameStr);
599601
jni_env->DeleteLocalRef(nameStr);
@@ -609,22 +611,24 @@ JNIEXPORT jobject JNICALL Java_dev_silenium_multimedia_core_mpv_MPVKt_createRend
609611

610612
mpv_render_context *handle{nullptr};
611613
if (const auto ret = mpv_render_context_create(&handle, reinterpret_cast<mpv_handle *>(mpvHandle), params.data()); ret < 0) {
614+
env->DeleteGlobalRef(object);
612615
return mpvResultFailure(env, "mpv_render_context_create", ret);
613616
}
614-
const auto updateMethod = env->GetMethodID(env->GetObjectClass(self), "requestUpdate", "()V");
615-
if (updateMethod == nullptr) {
616-
std::cerr << "Method not found: requestUpdate" << std::endl;
617-
return mpvResultFailure(env, "GetMethodID", MPV_ERROR_GENERIC);
618-
}
619617
mpv_render_context_set_update_callback(
620618
handle,
621-
fnptr<void(void *)>([=](void *) {
619+
fnptr<void(void *)>([object, jvm](void *) {
622620
JNIEnv *jni_env;
623621
const auto res = jvm->AttachCurrentThread(reinterpret_cast<void **>(&jni_env), nullptr);
624622
if (res != JNI_OK) {
625623
std::cerr << "Failed to attach current thread" << std::endl;
626624
return;
627625
}
626+
const auto updateMethod = jni_env->GetMethodID(jni_env->GetObjectClass(object), "requestUpdate", "()V");
627+
if (updateMethod == nullptr) {
628+
std::cerr << "Method not found: requestUpdate" << std::endl;
629+
return;
630+
}
631+
std::cerr << "Requesting update: " << object << ", " << updateMethod << std::endl;
628632
jni_env->CallVoidMethod(object, updateMethod);
629633
jvm->DetachCurrentThread();
630634
}),

src/main/kotlin/dev/silenium/multimedia/compose/player/VideoSurface.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fun VideoSurface(
5656
val surfaceState = rememberGLSurfaceState()
5757
BoxWithConstraints(modifier = modifier) {
5858
GLSurfaceView(
59-
surface = player.surface ?: createGLSurface(player, surfaceState),
59+
surface = player.surface!!,
6060
modifier = Modifier.matchParentSize(),
6161
)
6262
if (showStats) {
@@ -79,11 +79,10 @@ fun rememberVideoPlayer(
7979
val player = remember { VideoPlayer() }
8080
val surface = createGLSurface(player, surfaceState, onInitialized)
8181

82-
LaunchedEffect(player, surface) {
82+
DisposableEffect(player, surface) {
8383
player.surface = surface
84-
}
85-
DisposableEffect(player) {
8684
onDispose {
85+
player.surface = null
8786
player.close()
8887
}
8988
}

src/main/kotlin/dev/silenium/multimedia/core/mpv/MPV.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ class MPV : NativeCleanable, MPVAsyncListener {
324324
}
325325
commandAsyncN(nativePointer.address, command, subscriptionId).onFailure {
326326
commandReplyCallbacks.remove(subscriptionId)
327-
logger.error("Failed to execute command $command", it)
327+
logger.error("Failed to execute command ${command.joinToString()}", it)
328328
continuation.resume(Result.failure(it))
329329
}
330330
}

0 commit comments

Comments
 (0)