]> git.sesse.net Git - vlc/blob - modules/video_output/androidsurface.c
decklink output: black out video if no new pictures are coming
[vlc] / modules / video_output / androidsurface.c
1 /*****************************************************************************
2  * androidsurface.c: android video output using Surface Flinger
3  *****************************************************************************
4  * Copyright © 2011 VideoLAN
5  *
6  * Authors: Ming Hu <tewilove@gmail.com>
7  *          Ludovic Fauvet <etix@l0cal.com>
8  *          Sébastien Toque <xilasz@gmail.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_vout_display.h>
32 #include <vlc_picture_pool.h>
33
34 #include <dlfcn.h>
35
36 #ifndef ANDROID_SYM_S_LOCK
37 # define ANDROID_SYM_S_LOCK "_ZN7android7Surface4lockEPNS0_11SurfaceInfoEb"
38 #endif
39 #ifndef ANDROID_SYM_S_LOCK2
40 # define ANDROID_SYM_S_LOCK2 "_ZN7android7Surface4lockEPNS0_11SurfaceInfoEPNS_6RegionE"
41 #endif
42 #ifndef ANDROID_SYM_S_UNLOCK
43 # define ANDROID_SYM_S_UNLOCK "_ZN7android7Surface13unlockAndPostEv"
44 #endif
45
46 /*****************************************************************************
47  * Module descriptor
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.")
52
53 #define CFG_PREFIX "androidsurface-"
54
55 static int  Open (vlc_object_t *);
56 static void Close(vlc_object_t *);
57
58 vlc_module_begin()
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)
67 vlc_module_end()
68
69 /*****************************************************************************
70  * JNI prototypes
71  *****************************************************************************/
72
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);
76
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 *);
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87
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);
91
92 /* */
93 typedef struct _SurfaceInfo {
94     uint32_t    w;
95     uint32_t    h;
96     uint32_t    s;
97     uint32_t    usage;
98     uint32_t    format;
99     uint32_t*   bits;
100     uint32_t    reserved[2];
101 } SurfaceInfo;
102
103 /* */
104 struct vout_display_sys_t {
105     picture_pool_t *pool;
106     void *p_library;
107     Surface_lock s_lock;
108     Surface_lock2 s_lock2;
109     Surface_unlockAndPost s_unlockAndPost;
110
111     /* density */
112     int i_sar_num;
113     int i_sar_den;
114 };
115
116 struct picture_sys_t {
117     void *surf;
118     SurfaceInfo info;
119     vout_display_sys_t *sys;
120 };
121
122 static int  AndroidLockSurface(picture_t *);
123 static void AndroidUnlockSurface(picture_t *);
124
125 static vlc_mutex_t single_instance = VLC_STATIC_MUTEX;
126
127 static inline void *LoadSurface(const char *psz_lib, vout_display_sys_t *sys)
128 {
129     void *p_library = dlopen(psz_lib, RTLD_NOW);
130     if (!p_library)
131         return NULL;
132
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
138     if ((sys->s_lock || sys->s_lock2) && sys->s_unlockAndPost)
139         return p_library;
140
141     dlclose(p_library);
142     return NULL;
143 }
144
145 static void *InitLibrary(vout_display_sys_t *sys)
146 {
147     static const char *libs[] = {
148         "libsurfaceflinger_client.so",
149         "libgui.so",
150         "libui.so"
151     };
152
153     for (size_t i = 0; i < sizeof(libs) / sizeof(*libs); i++) {
154         void *lib = LoadSurface(libs[i], sys);
155         if (lib)
156             return lib;
157     }
158     return NULL;
159 }
160
161 static int Open(vlc_object_t *p_this)
162 {
163     vout_display_t *vd = (vout_display_t *)p_this;
164
165     /* */
166     if (vlc_mutex_trylock(&single_instance) != 0) {
167         msg_Err(vd, "Can't start more than one instance at a time");
168         return VLC_EGENERIC;
169     }
170
171     /* Allocate structure */
172     vout_display_sys_t *sys = (struct vout_display_sys_t*) calloc(1, sizeof(*sys));
173     if (!sys) {
174         vlc_mutex_unlock(&single_instance);
175         return VLC_ENOMEM;
176     }
177
178     /* */
179     sys->p_library = InitLibrary(sys);
180     if (!sys->p_library) {
181         free(sys);
182         msg_Err(vd, "Could not initialize libui.so/libgui.so/libsurfaceflinger_client.so!");
183         vlc_mutex_unlock(&single_instance);
184         return VLC_EGENERIC;
185     }
186
187     /* Setup chroma */
188     video_format_t fmt = vd->fmt;
189
190     char *psz_fcc = var_InheritString(vd, CFG_PREFIX "chroma");
191     if( psz_fcc ) {
192         fmt.i_chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, psz_fcc);
193         free(psz_fcc);
194     } else
195         fmt.i_chroma = VLC_CODEC_RGB32;
196
197     switch(fmt.i_chroma) {
198         case VLC_CODEC_YV12:
199             /* avoid swscale usage by asking for I420 instead since the
200              * vout already has code to swap the buffers */
201             fmt.i_chroma = VLC_CODEC_I420;
202         case VLC_CODEC_I420:
203             break;
204
205         case VLC_CODEC_RGB16:
206             fmt.i_bmask = 0x0000001f;
207             fmt.i_gmask = 0x000007e0;
208             fmt.i_rmask = 0x0000f800;
209             break;
210
211         case VLC_CODEC_RGB32:
212             fmt.i_rmask  = 0x000000ff;
213             fmt.i_gmask  = 0x0000ff00;
214             fmt.i_bmask  = 0x00ff0000;
215             break;
216
217         default:
218             return VLC_EGENERIC;
219     }
220     video_format_FixRgb(&fmt);
221
222     msg_Dbg(vd, "Pixel format %4.4s", (char*)&fmt.i_chroma);
223
224     /* Create the associated picture */
225     picture_sys_t picsys = malloc(sizeof(*picsys));
226     if (unlikely(picsys == NULL))
227         goto enomem;
228     picsys->sys = sys;
229
230     picture_resource_t resource = { .p_sys = picsys };
231     picture_t *picture = picture_NewFromResource(&fmt, &resource);
232     if (!picture) {
233         free(picsys);
234         goto enomem;
235     }
236
237     /* Wrap it into a picture pool */
238     picture_pool_configuration_t pool_cfg;
239     memset(&pool_cfg, 0, sizeof(pool_cfg));
240     pool_cfg.picture_count = 1;
241     pool_cfg.picture       = &picture;
242     pool_cfg.lock          = AndroidLockSurface;
243     pool_cfg.unlock        = AndroidUnlockSurface;
244
245     sys->pool = picture_pool_NewExtended(&pool_cfg);
246     if (!sys->pool) {
247         picture_Release(picture);
248         goto enomem;
249     }
250
251     /* Setup vout_display */
252     vd->sys     = sys;
253     vd->fmt     = fmt;
254     vd->pool    = Pool;
255     vd->display = Display;
256     vd->control = Control;
257     vd->prepare = NULL;
258     vd->manage  = NULL;
259
260     /* Fix initial state */
261     vout_display_SendEventFullscreen(vd, false);
262
263     sys->i_sar_num = vd->source.i_sar_num;
264     sys->i_sar_den = vd->source.i_sar_den;
265
266     return VLC_SUCCESS;
267
268 enomem:
269     dlclose(sys->p_library);
270     free(sys);
271     vlc_mutex_unlock(&single_instance);
272     return VLC_ENOMEM;
273 }
274
275 static void Close(vlc_object_t *p_this)
276 {
277     vout_display_t *vd = (vout_display_t *)p_this;
278     vout_display_sys_t *sys = vd->sys;
279
280     picture_pool_Delete(sys->pool);
281     dlclose(sys->p_library);
282     free(sys);
283     vlc_mutex_unlock(&single_instance);
284 }
285
286 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
287 {
288     VLC_UNUSED(count);
289
290     return vd->sys->pool;
291 }
292
293 #define ALIGN_16_PIXELS( x ) ( ( ( x ) + 15 ) / 16 * 16 )
294 static void SetupPictureYV12( SurfaceInfo* p_surfaceInfo, picture_t *p_picture )
295 {
296     /* according to document of android.graphics.ImageFormat.YV12 */
297     int i_stride = ALIGN_16_PIXELS( p_surfaceInfo->s );
298     int i_c_stride = ALIGN_16_PIXELS( i_stride / 2 );
299
300     p_picture->p->i_pitch = i_stride;
301
302     /* Fill chroma planes for planar YUV */
303     for( int n = 1; n < p_picture->i_planes; n++ )
304     {
305         const plane_t *o = &p_picture->p[n-1];
306         plane_t *p = &p_picture->p[n];
307
308         p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
309         p->i_pitch  = i_c_stride;
310         p->i_lines  = p_picture->format.i_height / 2;
311     }
312
313     if( vlc_fourcc_AreUVPlanesSwapped( p_picture->format.i_chroma,
314                                        VLC_CODEC_YV12 ) ) {
315         uint8_t *p_tmp = p_picture->p[1].p_pixels;
316         p_picture->p[1].p_pixels = p_picture->p[2].p_pixels;
317         p_picture->p[2].p_pixels = p_tmp;
318     }
319 }
320
321 static int  AndroidLockSurface(picture_t *picture)
322 {
323     picture_sys_t *picsys = picture->p_sys;
324     vout_display_sys_t *sys = picsys->sys;
325     SurfaceInfo *info;
326     uint32_t sw, sh;
327     void *surf;
328
329     sw = picture->p[0].i_visible_pitch / picture->p[0].i_pixel_pitch;
330     sh = picture->p[0].i_visible_lines;
331
332     picsys->surf = surf = jni_LockAndGetAndroidSurface();
333     info = &(picsys->info);
334
335     if (unlikely(!surf)) {
336         jni_UnlockAndroidSurface();
337         return VLC_EGENERIC;
338     }
339
340     if (sys->s_lock)
341         sys->s_lock(surf, info, 1);
342     else
343         sys->s_lock2(surf, info, NULL);
344
345     // For RGB (32 or 16) we need to align on 8 or 4 pixels, 16 pixels for YUV
346     int align_pixels = (16 / picture->p[0].i_pixel_pitch) - 1;
347     uint32_t aligned_width = (sw + align_pixels) & ~align_pixels;
348
349     if (info->w != aligned_width || info->h != sh) {
350         // input size doesn't match the surface size -> request a resize
351         jni_SetAndroidSurfaceSize(sw, sh, sys->i_sar_num, sys->i_sar_den);
352         sys->s_unlockAndPost(surf);
353         jni_UnlockAndroidSurface();
354         return VLC_EGENERIC;
355     }
356
357     picture->p[0].p_pixels = (uint8_t*)info->bits;
358     picture->p[0].i_lines = info->h;
359     picture->p[0].i_pitch = picture->p[0].i_pixel_pitch * info->s;
360
361     if (info->format == 0x32315659 /*ANDROID_IMAGE_FORMAT_YV12*/)
362         SetupPictureYV12(info, picture);
363
364     return VLC_SUCCESS;
365 }
366
367 static void AndroidUnlockSurface(picture_t *picture)
368 {
369     picture_sys_t *picsys = picture->p_sys;
370     vout_display_sys_t *sys = picsys->sys;
371
372     if (likely(picsys->surf))
373         sys->s_unlockAndPost(picsys->surf);
374     jni_UnlockAndroidSurface();
375 }
376
377 static void Display(vout_display_t *vd, picture_t *picture, subpicture_t *subpicture)
378 {
379     VLC_UNUSED(vd);
380     VLC_UNUSED(subpicture);
381
382     /* refcount lowers to 0, and pool_cfg.unlock is called */
383
384     picture_Release(picture);
385 }
386
387 static int Control(vout_display_t *vd, int query, va_list args)
388 {
389     VLC_UNUSED(args);
390
391     switch (query) {
392     case VOUT_DISPLAY_HIDE_MOUSE:
393         return VLC_SUCCESS;
394
395     default:
396         msg_Err(vd, "Unknown request in android vout display");
397
398     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
399     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
400     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
401     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
402     case VOUT_DISPLAY_CHANGE_ZOOM:
403     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
404     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
405     case VOUT_DISPLAY_GET_OPENGL:
406         return VLC_EGENERIC;
407     }
408 }