Vout: add a new window provider to create android native windows.
[vlc/gmpfix.git] / modules / video_output / android / nativewindow.c
blob674162b7d33b2ba9fd35e58dee07f682d1790986
1 /**
2 * @file androidnativewindow.c
3 * @brief Android native window provider module for VLC media player
4 */
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
29 #include <stdarg.h>
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_window.h>
35 #include <dlfcn.h>
36 #include <android/native_window.h>
37 #include <jni.h>
38 #include <android/native_window_jni.h>
41 typedef ANativeWindow* (*ptr_ANativeWindow_fromSurface)(JNIEnv*, jobject);
42 typedef void (*ptr_ANativeWindow_release)(ANativeWindow*);
44 extern JavaVM *myVm;
45 extern jobject jni_LockAndGetAndroidJavaSurface();
46 extern void jni_UnlockAndroidSurface();
47 extern void jni_SetAndroidSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
49 static int Open(vout_window_t *, const vout_window_cfg_t *);
50 static void Close(vout_window_t *);
51 static int Control(vout_window_t *, int, va_list ap);
54 * Module descriptor
56 vlc_module_begin()
57 set_shortname(N_("ANativeWindow"))
58 set_description(N_("Android native window"))
59 set_category(CAT_VIDEO)
60 set_subcategory(SUBCAT_VIDEO_VOUT)
61 set_capability("vout window anative", 10)
62 set_callbacks(Open, Close)
63 vlc_module_end()
66 struct vout_window_sys_t
68 void *p_library;
70 ptr_ANativeWindow_fromSurface s_winFromSurface;
71 ptr_ANativeWindow_release s_winRelease;
73 ANativeWindow *window;
77 /**
78 * Dynamically load the libandroid library and the needed symbols.
80 static void *InitLibrary(vout_window_sys_t *p_sys)
82 void *p_library = dlopen("libandroid.so", RTLD_NOW);
83 if (!p_library)
84 return NULL;
86 p_sys->s_winFromSurface =
87 (ptr_ANativeWindow_fromSurface)(dlsym(p_library, "ANativeWindow_fromSurface"));
88 p_sys->s_winRelease =
89 (ptr_ANativeWindow_release)(dlsym(p_library, "ANativeWindow_release"));
91 if (p_sys->s_winFromSurface == NULL || p_sys->s_winRelease == NULL)
93 dlclose(p_sys->p_library);
94 return NULL;
97 return p_library;
102 * Create an Android native window.
104 static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
106 vout_window_sys_t *p_sys = malloc(sizeof (*p_sys));
107 if (p_sys == NULL)
108 return VLC_ENOMEM;
110 p_sys->p_library = InitLibrary(p_sys);
111 if (p_sys->p_library == NULL)
113 free(p_sys);
114 return VLC_EGENERIC;
117 // Create the native window by first getting the Java surface.
118 jobject javaSurface = jni_LockAndGetAndroidJavaSurface();
119 if (javaSurface == NULL)
120 goto error;
122 JNIEnv *p_env;
123 (*myVm)->AttachCurrentThread(myVm, &p_env, NULL);
124 p_sys->window = p_sys->s_winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
125 (*myVm)->DetachCurrentThread(myVm);
127 jni_UnlockAndroidSurface();
129 if (p_sys->window == NULL)
130 goto error;
132 wnd->handle.anativewindow = p_sys->window;
133 wnd->control = Control;
134 wnd->sys = p_sys;
136 // Set the Java surface size.
137 jni_SetAndroidSurfaceSize(cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
139 return VLC_SUCCESS;
141 error:
142 dlclose(p_sys->p_library);
143 free(p_sys);
144 return VLC_EGENERIC;
149 * Destroys the Android native window.
151 static void Close(vout_window_t *wnd)
153 vout_window_sys_t *p_sys = wnd->sys;
154 p_sys->s_winRelease(p_sys->window); // Release the native window.
155 dlclose(p_sys->p_library); // Close the library.
156 free (p_sys);
161 * Window control.
163 static int Control(vout_window_t *wnd, int cmd, va_list ap)
165 switch (cmd)
167 case VOUT_WINDOW_SET_SIZE:
169 unsigned width = va_arg(ap, unsigned);
170 unsigned height = va_arg(ap, unsigned);
171 jni_SetAndroidSurfaceSize(width, height, width, height, 1, 1);
172 break;
174 case VOUT_WINDOW_SET_STATE:
175 case VOUT_WINDOW_SET_FULLSCREEN:
176 return VLC_EGENERIC;
177 default:
178 msg_Err (wnd, "request %d not implemented", cmd);
179 return VLC_EGENERIC;
181 return VLC_SUCCESS;