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>
36 #include <android/native_window.h>
38 #include <android/native_window_jni.h>
41 typedef ANativeWindow
* (*ptr_ANativeWindow_fromSurface
)(JNIEnv
*, jobject
);
42 typedef void (*ptr_ANativeWindow_release
)(ANativeWindow
*);
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
);
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
)
66 struct vout_window_sys_t
70 ptr_ANativeWindow_fromSurface s_winFromSurface
;
71 ptr_ANativeWindow_release s_winRelease
;
73 ANativeWindow
*window
;
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
);
86 p_sys
->s_winFromSurface
=
87 (ptr_ANativeWindow_fromSurface
)(dlsym(p_library
, "ANativeWindow_fromSurface"));
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
);
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
));
110 p_sys
->p_library
= InitLibrary(p_sys
);
111 if (p_sys
->p_library
== NULL
)
117 // Create the native window by first getting the Java surface.
118 jobject javaSurface
= jni_LockAndGetAndroidJavaSurface();
119 if (javaSurface
== NULL
)
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
)
132 wnd
->handle
.anativewindow
= p_sys
->window
;
133 wnd
->control
= Control
;
136 // Set the Java surface size.
137 jni_SetAndroidSurfaceSize(cfg
->width
, cfg
->height
, cfg
->width
, cfg
->height
, 1, 1);
142 dlclose(p_sys
->p_library
);
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.
163 static int Control(vout_window_t
*wnd
, int cmd
, va_list ap
)
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);
174 case VOUT_WINDOW_SET_STATE
:
175 case VOUT_WINDOW_SET_FULLSCREEN
:
178 msg_Err (wnd
, "request %d not implemented", cmd
);