2019-09-23 11:49:36 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libavcodec/jni.h>
|
|
|
|
#include <android/native_window_jni.h>
|
|
|
|
|
|
|
|
#include "android_common.h"
|
|
|
|
#include "common/msg.h"
|
2020-03-12 20:32:51 +00:00
|
|
|
#include "misc/jni.h"
|
2019-09-23 11:49:36 +00:00
|
|
|
#include "options/m_config.h"
|
|
|
|
#include "vo.h"
|
|
|
|
|
|
|
|
struct android_opts {
|
|
|
|
struct m_geometry surface_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct android_opts
|
|
|
|
const struct m_sub_options android_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"android-surface-size", OPT_SIZE_BOX(surface_size),
|
|
|
|
.flags = UPDATE_VO_RESIZE},
|
2019-09-23 11:49:36 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct android_opts),
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct vo_android_state {
|
|
|
|
struct mp_log *log;
|
|
|
|
ANativeWindow *native_window;
|
|
|
|
};
|
|
|
|
|
|
|
|
int vo_android_init(struct vo *vo)
|
|
|
|
{
|
|
|
|
vo->android = talloc_zero(vo, struct vo_android_state);
|
|
|
|
struct vo_android_state *ctx = vo->android;
|
|
|
|
|
|
|
|
*ctx = (struct vo_android_state){
|
|
|
|
.log = mp_log_new(ctx, vo->log, "android"),
|
|
|
|
};
|
|
|
|
|
2020-03-12 20:32:51 +00:00
|
|
|
JNIEnv *env = MP_JNI_GET_ENV(ctx);
|
|
|
|
if (!env) {
|
|
|
|
MP_FATAL(ctx, "Could not attach java VM.\n");
|
|
|
|
goto fail;
|
2019-09-23 11:49:36 +00:00
|
|
|
}
|
2020-03-12 20:32:51 +00:00
|
|
|
|
|
|
|
jobject surface = (jobject)(intptr_t)vo->opts->WinID;
|
2019-09-23 11:49:36 +00:00
|
|
|
ctx->native_window = ANativeWindow_fromSurface(env, surface);
|
2020-03-12 20:32:51 +00:00
|
|
|
if (!ctx->native_window) {
|
|
|
|
MP_FATAL(ctx, "Failed to create ANativeWindow\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-09-23 11:49:36 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
fail:
|
|
|
|
talloc_free(ctx);
|
|
|
|
vo->android = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vo_android_uninit(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_android_state *ctx = vo->android;
|
|
|
|
if (!ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctx->native_window)
|
|
|
|
ANativeWindow_release(ctx->native_window);
|
|
|
|
|
|
|
|
talloc_free(ctx);
|
|
|
|
vo->android = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ANativeWindow *vo_android_native_window(struct vo *vo)
|
|
|
|
{
|
|
|
|
struct vo_android_state *ctx = vo->android;
|
|
|
|
return ctx->native_window;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vo_android_surface_size(struct vo *vo, int *out_w, int *out_h)
|
|
|
|
{
|
|
|
|
struct vo_android_state *ctx = vo->android;
|
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
struct android_opts *opts = mp_get_config_group(tmp, vo->global, &android_conf);
|
|
|
|
|
|
|
|
int w = opts->surface_size.w, h = opts->surface_size.h;
|
|
|
|
if (!w)
|
|
|
|
w = ANativeWindow_getWidth(ctx->native_window);
|
|
|
|
if (!h)
|
|
|
|
h = ANativeWindow_getHeight(ctx->native_window);
|
|
|
|
|
|
|
|
talloc_free(tmp);
|
|
|
|
if (w <= 0 || h <= 0) {
|
|
|
|
MP_ERR(ctx, "Failed to get height and width.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out_w = w;
|
|
|
|
*out_h = h;
|
|
|
|
return true;
|
|
|
|
}
|