forked from mpv-android/mpv-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
100 lines (74 loc) · 2.27 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <locale.h>
#include <atomic>
#include <mpv/client.h>
#include <pthread.h>
extern "C" {
#include <libavcodec/jni.h>
}
#include "log.h"
#include "jni_utils.h"
#include "event.h"
#define ARRAYLEN(a) (sizeof(a)/sizeof(a[0]))
extern "C" {
jni_func(void, create, jobject appctx);
jni_func(void, init);
jni_func(void, destroy);
jni_func(void, command, jobjectArray jarray);
};
JavaVM *g_vm;
mpv_handle *g_mpv;
std::atomic<bool> g_event_thread_request_exit(false);
static pthread_t event_thread_id;
static void prepare_environment(JNIEnv *env, jobject appctx) {
setlocale(LC_NUMERIC, "C");
if (!env->GetJavaVM(&g_vm) && g_vm)
av_jni_set_java_vm(g_vm, NULL);
init_methods_cache(env);
}
jni_func(void, create, jobject appctx) {
prepare_environment(env, appctx);
if (g_mpv)
die("mpv is already initialized");
g_mpv = mpv_create();
if (!g_mpv)
die("context init failed");
mpv_request_log_messages(g_mpv, "v");
}
jni_func(void, init) {
if (!g_mpv)
die("mpv is not created");
if (mpv_initialize(g_mpv) < 0)
die("mpv init failed");
#ifdef __aarch64__
ALOGV("You're using the 64-bit build of mpv!");
#endif
g_event_thread_request_exit = false;
pthread_create(&event_thread_id, NULL, event_thread, NULL);
}
jni_func(void, destroy) {
if (!g_mpv)
die("mpv destroy called but it's already destroyed");
// poke event thread and wait for it to exit
g_event_thread_request_exit = true;
mpv_wakeup(g_mpv);
pthread_join(event_thread_id, NULL);
mpv_terminate_destroy(g_mpv);
g_mpv = NULL;
}
jni_func(void, command, jobjectArray jarray) {
const char *arguments[128] = { 0 };
int len = env->GetArrayLength(jarray);
if (!g_mpv)
die("Cannot run command: mpv is not initialized");
if (len >= ARRAYLEN(arguments))
die("Cannot run command: too many arguments");
for (int i = 0; i < len; ++i)
arguments[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(jarray, i), NULL);
mpv_command(g_mpv, arguments);
for (int i = 0; i < len; ++i)
env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(jarray, i), arguments[i]);
}