]> git.sesse.net Git - vlc/blob - modules/video_output/android/nativewindow.c
Add missing rpath
[vlc] / modules / video_output / android / nativewindow.c
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
7  *
8  * Author: Adrien Maglo <magsoft@videolan.org>
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 <stdarg.h>
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout_window.h>
34
35 #include <dlfcn.h>
36 #include <jni.h>
37
38 #include "utils.h"
39
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);
46
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);
50
51 /*
52  * Module descriptor
53  */
54 vlc_module_begin()
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)
61 vlc_module_end()
62
63
64 struct vout_window_sys_t
65 {
66     void *p_library;
67     native_window_api_t native_window;
68
69     ANativeWindow *window;
70 };
71
72 /**
73  * Create an Android native window.
74  */
75 static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
76 {
77     if (cfg->type != VOUT_WINDOW_TYPE_INVALID
78      && cfg->type != VOUT_WINDOW_TYPE_ANDROID_NATIVE)
79         return VLC_EGENERIC;
80
81     vout_window_sys_t *p_sys = malloc(sizeof (*p_sys));
82     if (p_sys == NULL)
83         return VLC_ENOMEM;
84
85     p_sys->p_library = LoadNativeWindowAPI(&p_sys->native_window);
86     if (p_sys->p_library == NULL)
87     {
88         free(p_sys);
89         return VLC_EGENERIC;
90     }
91
92     // Create the native window by first getting the Java surface.
93     jobject javaSurface = jni_LockAndGetAndroidJavaSurface();
94     if (javaSurface == NULL)
95         goto error;
96
97     JNIEnv *p_env;
98     jni_attach_thread(&p_env, THREAD_NAME);
99     p_sys->window = p_sys->native_window.winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
100     jni_detach_thread();
101
102     jni_UnlockAndroidSurface();
103
104     if (p_sys->window == NULL)
105         goto error;
106
107     wnd->type = VOUT_WINDOW_TYPE_ANDROID_NATIVE;
108     wnd->handle.anativewindow = p_sys->window;
109     wnd->control = Control;
110     wnd->sys = p_sys;
111
112     // Set the Java surface size.
113     jni_SetSurfaceLayout(cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
114
115     return VLC_SUCCESS;
116
117 error:
118     dlclose(p_sys->p_library);
119     free(p_sys);
120     return VLC_EGENERIC;
121 }
122
123
124 /**
125  * Destroys the Android native window.
126  */
127 static void Close(vout_window_t *wnd)
128 {
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.
132     free (p_sys);
133 }
134
135
136 /**
137  * Window control.
138  */
139 static int Control(vout_window_t *wnd, int cmd, va_list ap)
140 {
141     switch (cmd)
142     {
143         case VOUT_WINDOW_SET_SIZE:
144         {
145             unsigned width = va_arg(ap, unsigned);
146             unsigned height = va_arg(ap, unsigned);
147             jni_SetSurfaceLayout(width, height, width, height, 1, 1);
148             break;
149         }
150         case VOUT_WINDOW_SET_STATE:
151         case VOUT_WINDOW_SET_FULLSCREEN:
152             return VLC_EGENERIC;
153         default:
154             msg_Err (wnd, "request %d not implemented", cmd);
155             return VLC_EGENERIC;
156     }
157     return VLC_SUCCESS;
158 }