Skip to content

Commit 0eea8cc

Browse files
committed
fix: throw Java exception instead of exit(1) on fatal errors
Replace die() → exit(1) with die() → ThrowNew(RuntimeException) so that mpv initialization failures are reported as catchable Java exceptions instead of killing the entire Android process. All call sites now return after die() since ThrowNew only sets a pending exception flag without unwinding the stack.
1 parent f48aa82 commit 0eea8cc

File tree

7 files changed

+47
-18
lines changed

7 files changed

+47
-18
lines changed

libmpv/src/main/cpp/event.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ static void sendLogMessageToJava(JNIEnv *env, mpv_event_log_message *msg) {
6969
void *event_thread(void *arg) {
7070
JNIEnv *env = NULL;
7171
acquire_jni_env(g_vm, &env);
72-
if (!env)
73-
die("failed to acquire java env");
72+
if (!env) {
73+
ALOGE("failed to acquire java env");
74+
return NULL;
75+
}
7476

7577
while (true) {
7678
mpv_event *mp_event;

libmpv/src/main/cpp/globals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

3+
#include <jni.h>
34
#include <atomic>
5+
#include <mpv/client.h>
46

57
extern JavaVM *g_vm;
68
extern mpv_handle *g_mpv;

libmpv/src/main/cpp/log.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include "log.h"
2-
3-
#include <cstdlib>
2+
#include "globals.h"
3+
#include "jni_utils.h"
44

55
void die(const char *msg)
66
{
77
ALOGE("%s", msg);
8-
exit(1);
8+
JNIEnv *env = nullptr;
9+
if (g_vm && acquire_jni_env(g_vm, &env) && env) {
10+
jclass cls = env->FindClass("java/lang/RuntimeException");
11+
if (cls) env->ThrowNew(cls, msg);
12+
}
913
}

libmpv/src/main/cpp/log.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
#define ALOGV(...) (void)0
1313
#endif
1414

15-
__attribute__((noreturn)) void die(const char *msg);
15+
void die(const char *msg);
1616

1717
#define CHECK_MPV_INIT() do { \
18-
if (__builtin_expect(!g_mpv, 0)) \
18+
if (__builtin_expect(!g_mpv, 0)) { \
1919
die("libmpv is not initialized"); \
20-
} while (0)
20+
return; \
21+
} } while (0)
22+
23+
#define CHECK_MPV_INIT_RET(val) do { \
24+
if (__builtin_expect(!g_mpv, 0)) { \
25+
die("libmpv is not initialized"); \
26+
return val; \
27+
} } while (0)

libmpv/src/main/cpp/main.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,36 @@ static void prepare_environment(JNIEnv *env, jobject appctx) {
4949
jni_func(void, create, jobject appctx) {
5050
prepare_environment(env, appctx);
5151

52-
if (g_mpv)
52+
if (g_mpv) {
5353
die("mpv is already initialized");
54+
return;
55+
}
5456

5557
g_mpv = mpv_create();
56-
if (!g_mpv)
58+
if (!g_mpv) {
5759
die("context init failed");
60+
return;
61+
}
5862

5963
mpv_request_log_messages(g_mpv, "v");
6064
}
6165

6266
jni_func(void, init) {
63-
if (!g_mpv)
67+
if (!g_mpv) {
6468
die("mpv is not created");
69+
return;
70+
}
6571

66-
if (mpv_initialize(g_mpv) < 0)
72+
if (mpv_initialize(g_mpv) < 0) {
6773
die("mpv init failed");
74+
return;
75+
}
6876

6977
g_event_thread_request_exit = false;
70-
if (pthread_create(&event_thread_id, NULL, event_thread, NULL) != 0)
78+
if (pthread_create(&event_thread_id, NULL, event_thread, NULL) != 0) {
7179
die("thread create failed");
80+
return;
81+
}
7282
pthread_setname_np(event_thread_id, "event_thread");
7383
}
7484

@@ -92,8 +102,10 @@ jni_func(void, command, jobjectArray jarray) {
92102

93103
const char *arguments[128] = { 0 };
94104
int len = env->GetArrayLength(jarray);
95-
if (len >= ARRAYLEN(arguments))
105+
if (len >= ARRAYLEN(arguments)) {
96106
die("too many command arguments");
107+
return;
108+
}
97109

98110
for (int i = 0; i < len; ++i)
99111
arguments[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(jarray, i), NULL);

libmpv/src/main/cpp/property.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" {
2323
}
2424

2525
jni_func(jint, setOptionString, jstring joption, jstring jvalue) {
26-
CHECK_MPV_INIT();
26+
CHECK_MPV_INIT_RET(0);
2727

2828
const char *option = env->GetStringUTFChars(joption, NULL);
2929
const char *value = env->GetStringUTFChars(jvalue, NULL);
@@ -37,7 +37,7 @@ jni_func(jint, setOptionString, jstring joption, jstring jvalue) {
3737
}
3838

3939
static int common_get_property(JNIEnv *env, jstring jproperty, mpv_format format, void *output) {
40-
CHECK_MPV_INIT();
40+
CHECK_MPV_INIT_RET(-1);
4141

4242
const char *prop = env->GetStringUTFChars(jproperty, NULL);
4343
int result = mpv_get_property(g_mpv, prop, format, output);
@@ -49,7 +49,7 @@ static int common_get_property(JNIEnv *env, jstring jproperty, mpv_format format
4949
}
5050

5151
static int common_set_property(JNIEnv *env, jstring jproperty, mpv_format format, void *value) {
52-
CHECK_MPV_INIT();
52+
CHECK_MPV_INIT_RET(-1);
5353

5454
const char *prop = env->GetStringUTFChars(jproperty, NULL);
5555
int result = mpv_set_property(g_mpv, prop, format, value);

libmpv/src/main/cpp/render.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ jni_func(void, attachSurface, jobject surface_) {
1717
CHECK_MPV_INIT();
1818

1919
surface = env->NewGlobalRef(surface_);
20-
if (!surface)
20+
if (!surface) {
2121
die("invalid surface provided");
22+
return;
23+
}
2224
int64_t wid = reinterpret_cast<intptr_t>(surface);
2325
int result = mpv_set_option(g_mpv, "wid", MPV_FORMAT_INT64, &wid);
2426
if (result < 0)

0 commit comments

Comments
 (0)