2 * @file androidnativewindow.c
3 * @brief Android native window provider module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2013 VLC authors and VideoLAN
8 * Author: Adrien Maglo <magsoft@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_window.h>
40 #define THREAD_NAME "ANativeWindow"
41 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
42 extern void jni_detach_thread();
43 extern jobject jni_LockAndGetAndroidJavaSurface();
44 extern void jni_UnlockAndroidSurface();
45 extern void jni_SetSurfaceLayout(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
47 static int Open(vout_window_t *, const vout_window_cfg_t *);
48 static void Close(vout_window_t *);
49 static int Control(vout_window_t *, int, va_list ap);
55 set_shortname(N_("ANativeWindow"))
56 set_description(N_("Android native window"))
57 set_category(CAT_VIDEO)
58 set_subcategory(SUBCAT_VIDEO_VOUT)
59 set_capability("vout window", 0)
60 set_callbacks(Open, Close)
64 struct vout_window_sys_t
67 native_window_api_t native_window;
69 ANativeWindow *window;
73 * Create an Android native window.
75 static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
77 if (cfg->type != VOUT_WINDOW_TYPE_INVALID
78 && cfg->type != VOUT_WINDOW_TYPE_ANDROID_NATIVE)
81 vout_window_sys_t *p_sys = malloc(sizeof (*p_sys));
85 p_sys->p_library = LoadNativeWindowAPI(&p_sys->native_window);
86 if (p_sys->p_library == NULL)
92 // Create the native window by first getting the Java surface.
93 jobject javaSurface = jni_LockAndGetAndroidJavaSurface();
94 if (javaSurface == NULL)
98 jni_attach_thread(&p_env, THREAD_NAME);
99 p_sys->window = p_sys->native_window.winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
102 jni_UnlockAndroidSurface();
104 if (p_sys->window == NULL)
107 wnd->type = VOUT_WINDOW_TYPE_ANDROID_NATIVE;
108 wnd->handle.anativewindow = p_sys->window;
109 wnd->control = Control;
112 // Set the Java surface size.
113 jni_SetSurfaceLayout(cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
118 dlclose(p_sys->p_library);
125 * Destroys the Android native window.
127 static void Close(vout_window_t *wnd)
129 vout_window_sys_t *p_sys = wnd->sys;
130 p_sys->native_window.winRelease(p_sys->window); // Release the native window.
131 dlclose(p_sys->p_library); // Close the library.
139 static int Control(vout_window_t *wnd, int cmd, va_list ap)
143 case VOUT_WINDOW_SET_SIZE:
145 unsigned width = va_arg(ap, unsigned);
146 unsigned height = va_arg(ap, unsigned);
147 jni_SetSurfaceLayout(width, height, width, height, 1, 1);
150 case VOUT_WINDOW_SET_STATE:
151 case VOUT_WINDOW_SET_FULLSCREEN:
154 msg_Err (wnd, "request %d not implemented", cmd);