1 /*****************************************************************************
2 * androidsurface.c: android video output using Surface Flinger
3 *****************************************************************************
4 * Copyright © 2011 VideoLAN
6 * Authors: Ming Hu <tewilove@gmail.com>
7 * Ludovic Fauvet <etix@l0cal.com>
8 * Sébastien Toque <xilasz@gmail.com>
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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_vout_display.h>
32 #include <vlc_picture_pool.h>
36 #ifndef ANDROID_SYM_S_LOCK
37 # define ANDROID_SYM_S_LOCK "_ZN7android7Surface4lockEPNS0_11SurfaceInfoEb"
39 #ifndef ANDROID_SYM_S_LOCK2
40 # define ANDROID_SYM_S_LOCK2 "_ZN7android7Surface4lockEPNS0_11SurfaceInfoEPNS_6RegionE"
42 #ifndef ANDROID_SYM_S_UNLOCK
43 # define ANDROID_SYM_S_UNLOCK "_ZN7android7Surface13unlockAndPostEv"
46 /*****************************************************************************
48 *****************************************************************************/
49 #define CHROMA_TEXT N_("Chroma used")
50 #define CHROMA_LONGTEXT N_(\
51 "Force use of a specific chroma for output. Default is RGB32.")
53 #define CFG_PREFIX "androidsurface-"
55 static int Open (vlc_object_t *);
56 static void Close(vlc_object_t *);
59 set_category(CAT_VIDEO)
60 set_subcategory(SUBCAT_VIDEO_VOUT)
61 set_shortname("AndroidSurface")
62 set_description(N_("Android Surface video output"))
63 set_capability("vout display", 155)
64 add_shortcut("androidsurface", "android")
65 add_string(CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true)
66 set_callbacks(Open, Close)
69 /*****************************************************************************
71 *****************************************************************************/
73 extern void *jni_LockAndGetAndroidSurface();
74 extern void jni_UnlockAndroidSurface();
75 extern void jni_SetAndroidSurfaceSize(int width, int height, int sar_num, int sar_den);
77 // _ZN7android7Surface4lockEPNS0_11SurfaceInfoEb
78 typedef void (*Surface_lock)(void *, void *, int);
79 // _ZN7android7Surface4lockEPNS0_11SurfaceInfoEPNS_6RegionE
80 typedef void (*Surface_lock2)(void *, void *, void *);
81 // _ZN7android7Surface13unlockAndPostEv
82 typedef void (*Surface_unlockAndPost)(void *);
84 /*****************************************************************************
86 *****************************************************************************/
88 static picture_pool_t *Pool (vout_display_t *, unsigned);
89 static void Display(vout_display_t *, picture_t *, subpicture_t *);
90 static int Control(vout_display_t *, int, va_list);
93 typedef struct _SurfaceInfo {
100 uint32_t reserved[2];
104 struct vout_display_sys_t {
105 picture_pool_t *pool;
108 Surface_lock2 s_lock2;
109 Surface_unlockAndPost s_unlockAndPost;
111 picture_resource_t resource;
122 vout_display_sys_t *sys;
125 static int AndroidLockSurface(picture_t *);
126 static void AndroidUnlockSurface(picture_t *);
128 static vlc_mutex_t single_instance = VLC_STATIC_MUTEX;
130 static inline void *LoadSurface(const char *psz_lib, vout_display_sys_t *sys) {
131 void *p_library = dlopen(psz_lib, RTLD_NOW);
133 sys->s_lock = (Surface_lock)(dlsym(p_library, ANDROID_SYM_S_LOCK));
134 sys->s_lock2 = (Surface_lock2)(dlsym(p_library, ANDROID_SYM_S_LOCK2));
135 sys->s_unlockAndPost =
136 (Surface_unlockAndPost)(dlsym(p_library, ANDROID_SYM_S_UNLOCK));
137 if ((sys->s_lock || sys->s_lock2) && sys->s_unlockAndPost) {
145 static void *InitLibrary(vout_display_sys_t *sys) {
147 if ((p_library = LoadSurface("libsurfaceflinger_client.so", sys)))
149 if ((p_library = LoadSurface("libgui.so", sys)))
151 return LoadSurface("libui.so", sys);
154 static int Open(vlc_object_t *p_this) {
155 vout_display_t *vd = (vout_display_t *)p_this;
156 vout_display_sys_t *sys;
160 if (vlc_mutex_trylock(&single_instance) != 0) {
161 msg_Err(vd, "Can't start more than one instance at a time");
165 /* Allocate structure */
166 sys = (struct vout_display_sys_t*) calloc(1, sizeof(*sys));
168 vlc_mutex_unlock(&single_instance);
173 sys->p_library = p_library = InitLibrary(sys);
176 msg_Err(vd, "Could not initialize libui.so/libgui.so/libsurfaceflinger_client.so!");
177 vlc_mutex_unlock(&single_instance);
182 video_format_t fmt = vd->fmt;
184 char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
186 fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, psz_fcc);
189 fmt.i_chroma = VLC_CODEC_RGB32;
191 switch(fmt.i_chroma) {
193 /* avoid swscale usage by asking for I420 instead since the
194 * vout already has code to swap the buffers */
195 fmt.i_chroma = VLC_CODEC_I420;
199 case VLC_CODEC_RGB16:
200 fmt.i_bmask = 0x0000001f;
201 fmt.i_gmask = 0x000007e0;
202 fmt.i_rmask = 0x0000f800;
205 case VLC_CODEC_RGB32:
206 fmt.i_rmask = 0x000000ff;
207 fmt.i_gmask = 0x0000ff00;
208 fmt.i_bmask = 0x00ff0000;
214 video_format_FixRgb(&fmt);
216 msg_Dbg(vd, "Pixel format %4.4s", (char*)&fmt.i_chroma);
218 /* Create the associated picture */
219 picture_resource_t *rsc = &sys->resource;
220 rsc->p_sys = malloc(sizeof(*rsc->p_sys));
223 rsc->p_sys->sys = sys;
225 for (int i = 0; i < PICTURE_PLANE_MAX; i++) {
226 rsc->p[i].p_pixels = NULL;
227 rsc->p[i].i_pitch = 0;
228 rsc->p[i].i_lines = 0;
230 picture_t *picture = picture_NewFromResource(&fmt, rsc);
234 /* Wrap it into a picture pool */
235 picture_pool_configuration_t pool_cfg;
236 memset(&pool_cfg, 0, sizeof(pool_cfg));
237 pool_cfg.picture_count = 1;
238 pool_cfg.picture = &picture;
239 pool_cfg.lock = AndroidLockSurface;
240 pool_cfg.unlock = AndroidUnlockSurface;
242 sys->pool = picture_pool_NewExtended(&pool_cfg);
244 picture_Release(picture);
248 /* Setup vout_display */
252 vd->display = Display;
253 vd->control = Control;
257 /* Fix initial state */
258 vout_display_SendEventFullscreen(vd, false);
260 sys->i_sar_num = vd->source.i_sar_num;
261 sys->i_sar_den = vd->source.i_sar_den;
269 vlc_mutex_unlock(&single_instance);
273 static void Close(vlc_object_t *p_this) {
274 vout_display_t *vd = (vout_display_t *)p_this;
275 vout_display_sys_t *sys = vd->sys;
277 picture_pool_Delete(sys->pool);
278 dlclose(sys->p_library);
280 vlc_mutex_unlock(&single_instance);
283 static picture_pool_t *Pool(vout_display_t *vd, unsigned count) {
284 vout_display_sys_t *sys = vd->sys;
289 #define ALIGN_16_PIXELS( x ) ( ( ( x ) + 15 ) / 16 * 16 )
290 static void SetupPictureYV12( SurfaceInfo* p_surfaceInfo, picture_t *p_picture )
292 /* according to document of android.graphics.ImageFormat.YV12 */
293 int i_stride = ALIGN_16_PIXELS( p_surfaceInfo->s );
294 int i_c_stride = ALIGN_16_PIXELS( i_stride / 2 );
296 p_picture->p->i_pitch = i_stride;
298 /* Fill chroma planes for planar YUV */
299 for( int n = 1; n < p_picture->i_planes; n++ )
301 const plane_t *o = &p_picture->p[n-1];
302 plane_t *p = &p_picture->p[n];
304 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
305 p->i_pitch = i_c_stride;
306 p->i_lines = p_picture->format.i_height / 2;
309 if( vlc_fourcc_AreUVPlanesSwapped( p_picture->format.i_chroma,
311 uint8_t *p_tmp = p_picture->p[1].p_pixels;
312 p_picture->p[1].p_pixels = p_picture->p[2].p_pixels;
313 p_picture->p[2].p_pixels = p_tmp;
317 static int AndroidLockSurface(picture_t *picture) {
318 picture_sys_t *picsys = picture->p_sys;
319 vout_display_sys_t *sys = picsys->sys;
324 sw = picture->p[0].i_visible_pitch / picture->p[0].i_pixel_pitch;
325 sh = picture->p[0].i_visible_lines;
327 picsys->surf = surf = jni_LockAndGetAndroidSurface();
328 info = &(picsys->info);
330 if (unlikely(!surf)) {
331 jni_UnlockAndroidSurface();
336 sys->s_lock(surf, info, 1);
338 sys->s_lock2(surf, info, NULL);
340 // input size doesn't match the surface size,
342 if (info->w != sw || info->h != sh) {
343 jni_SetAndroidSurfaceSize(sw, sh, sys->i_sar_num, sys->i_sar_den);
344 sys->s_unlockAndPost(surf);
345 jni_UnlockAndroidSurface();
349 picture->p[0].p_pixels = (uint8_t*)info->bits;
350 picture->p[0].i_lines = info->h;
351 picture->p[0].i_pitch = picture->p[0].i_pixel_pitch * info->s;
353 if (info->format == 0x32315659 /*ANDROID_IMAGE_FORMAT_YV12*/)
354 SetupPictureYV12(info, picture);
359 static void AndroidUnlockSurface(picture_t *picture) {
360 picture_sys_t *picsys = picture->p_sys;
361 vout_display_sys_t *sys = picsys->sys;
363 if (likely(picsys->surf))
364 sys->s_unlockAndPost(picsys->surf);
365 jni_UnlockAndroidSurface();
368 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture) {
370 VLC_UNUSED(subpicture);
371 picture_Release(picture);
374 static int Control(vout_display_t *vd, int query, va_list args) {
378 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
379 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
380 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
381 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
382 case VOUT_DISPLAY_CHANGE_ZOOM:
383 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
384 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
385 case VOUT_DISPLAY_GET_OPENGL:
387 case VOUT_DISPLAY_HIDE_MOUSE:
390 msg_Err(vd, "Unknown request in android vout display");