JNIEnv *ff_jni_attach_env(int *attached, void *log_ctx) { int ret = 0; JNIEnv *env = NULL; *attached = 0; pthread_mutex_lock(&lock); if (java_vm == NULL && (java_vm = av_jni_get_java_vm(log_ctx)) == NULL) { av_log(log_ctx, AV_LOG_INFO, "Retrieving current Java virtual machine using Android JniInvocation wrapper\n"); if (check_jni_invocation(log_ctx) == 0) { if ((java_vm = get_java_vm(NULL, log_ctx)) != NULL || (java_vm = get_java_vm("libdvm.so", log_ctx)) != NULL || (java_vm = get_java_vm("libart.so", log_ctx)) != NULL) { av_log(log_ctx, AV_LOG_INFO, "Found Java virtual machine using Android JniInvocation wrapper\n"); } } } pthread_mutex_unlock(&lock); if (!java_vm) { av_log(log_ctx, AV_LOG_ERROR, "Could not retrieve a Java virtual machine\n"); return NULL; } ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6); switch(ret) { case JNI_EDETACHED: if ((*java_vm)->AttachCurrentThread(java_vm, &env, NULL) != 0) { av_log(log_ctx, AV_LOG_ERROR, "Failed to attach the JNI environment to the current thread\n"); env = NULL; } else { *attached = 1; } break; case JNI_OK: break; case JNI_EVERSION: av_log(log_ctx, AV_LOG_ERROR, "The specified JNI version is not supported\n"); break; default: av_log(log_ctx, AV_LOG_ERROR, "Failed to get the JNI environment attached to this thread"); break; } return env; }
JNIEnv *ff_jni_get_env(void *log_ctx) { int ret = 0; JNIEnv *env = NULL; pthread_mutex_lock(&lock); if (java_vm == NULL) { java_vm = av_jni_get_java_vm(log_ctx); } if (!java_vm) { av_log(log_ctx, AV_LOG_ERROR, "No Java virtual machine has been registered\n"); goto done; } pthread_once(&once, jni_create_pthread_key); if ((env = pthread_getspecific(current_env)) != NULL) { goto done; } ret = (*java_vm)->GetEnv(java_vm, (void **)&env, JNI_VERSION_1_6); switch(ret) { case JNI_EDETACHED: if ((*java_vm)->AttachCurrentThread(java_vm, &env, NULL) != 0) { av_log(log_ctx, AV_LOG_ERROR, "Failed to attach the JNI environment to the current thread\n"); env = NULL; } else { pthread_setspecific(current_env, env); } break; case JNI_OK: break; case JNI_EVERSION: av_log(log_ctx, AV_LOG_ERROR, "The specified JNI version is not supported\n"); break; default: av_log(log_ctx, AV_LOG_ERROR, "Failed to get the JNI environment attached to this thread"); break; } done: pthread_mutex_unlock(&lock); return env; }
static bool android_init(struct ra_ctx *ctx) { struct priv *p = ctx->priv = talloc_zero(ctx, struct priv); jobject surface = (jobject)(intptr_t)ctx->vo->opts->WinID; JavaVM *vm = (JavaVM *)av_jni_get_java_vm(NULL); JNIEnv *env; int ret = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6); if (ret == JNI_EDETACHED) { if ((*vm)->AttachCurrentThread(vm, &env, NULL) != 0) { MP_FATAL(ctx, "Could not attach java VM.\n"); goto fail; } } p->native_window = ANativeWindow_fromSurface(env, surface); (*vm)->DetachCurrentThread(vm); p->egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (!eglInitialize(p->egl_display, NULL, NULL)) { MP_FATAL(ctx, "EGL failed to initialize.\n"); goto fail; } EGLConfig config; if (!mpegl_create_context(ctx, p->egl_display, &p->egl_context, &config)) goto fail; EGLint format; eglGetConfigAttrib(p->egl_display, config, EGL_NATIVE_VISUAL_ID, &format); ANativeWindow_setBuffersGeometry(p->native_window, 0, 0, format); p->egl_surface = eglCreateWindowSurface(p->egl_display, config, (EGLNativeWindowType)p->native_window, NULL); if (p->egl_surface == EGL_NO_SURFACE) { MP_FATAL(ctx, "Could not create EGL surface!\n"); goto fail; } if (!eglMakeCurrent(p->egl_display, p->egl_surface, p->egl_surface, p->egl_context)) { MP_FATAL(ctx, "Failed to set context!\n"); goto fail; } if (!eglQuerySurface(p->egl_display, p->egl_surface, EGL_WIDTH, &p->w) || !eglQuerySurface(p->egl_display, p->egl_surface, EGL_HEIGHT, &p->h)) { MP_FATAL(ctx, "Failed to get height and width!\n"); goto fail; } mpegl_load_functions(&p->gl, ctx->log); struct ra_gl_ctx_params params = { .swap_buffers = android_swap_buffers, }; if (!ra_gl_ctx_init(ctx, &p->gl, params)) goto fail; return true; fail: android_uninit(ctx); return false; }