]> git.sesse.net Git - vlc/blob - modules/video_output/android/nativewindow.c
Macosx video output: remove trailing spaces
[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 extern JavaVM *myVm;
41 extern jobject jni_LockAndGetAndroidJavaSurface();
42 extern void jni_UnlockAndroidSurface();
43 extern void  jni_SetAndroidSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
44
45 static int Open(vout_window_t *, const vout_window_cfg_t *);
46 static void Close(vout_window_t *);
47 static int Control(vout_window_t *, int, va_list ap);
48
49 /*
50  * Module descriptor
51  */
52 vlc_module_begin()
53     set_shortname(N_("ANativeWindow"))
54     set_description(N_("Android native window"))
55     set_category(CAT_VIDEO)
56     set_subcategory(SUBCAT_VIDEO_VOUT)
57     set_capability("vout window anative", 10)
58     set_callbacks(Open, Close)
59 vlc_module_end()
60
61
62 struct vout_window_sys_t
63 {
64     void *p_library;
65     native_window_api_t native_window;
66
67     ANativeWindow *window;
68 };
69
70 /**
71  * Create an Android native window.
72  */
73 static int Open(vout_window_t *wnd, const vout_window_cfg_t *cfg)
74 {
75     vout_window_sys_t *p_sys = malloc(sizeof (*p_sys));
76     if (p_sys == NULL)
77         return VLC_ENOMEM;
78
79     p_sys->p_library = LoadNativeWindowAPI(&p_sys->native_window);
80     if (p_sys->p_library == NULL)
81     {
82         free(p_sys);
83         return VLC_EGENERIC;
84     }
85
86     // Create the native window by first getting the Java surface.
87     jobject javaSurface = jni_LockAndGetAndroidJavaSurface();
88     if (javaSurface == NULL)
89         goto error;
90
91     JNIEnv *p_env;
92     (*myVm)->AttachCurrentThread(myVm, &p_env, NULL);
93     p_sys->window = p_sys->native_window.winFromSurface(p_env, javaSurface); // ANativeWindow_fromSurface call.
94     (*myVm)->DetachCurrentThread(myVm);
95
96     jni_UnlockAndroidSurface();
97
98     if (p_sys->window == NULL)
99         goto error;
100
101     wnd->handle.anativewindow = p_sys->window;
102     wnd->control = Control;
103     wnd->sys = p_sys;
104
105     // Set the Java surface size.
106     jni_SetAndroidSurfaceSize(cfg->width, cfg->height, cfg->width, cfg->height, 1, 1);
107
108     return VLC_SUCCESS;
109
110 error:
111     dlclose(p_sys->p_library);
112     free(p_sys);
113     return VLC_EGENERIC;
114 }
115
116
117 /**
118  * Destroys the Android native window.
119  */
120 static void Close(vout_window_t *wnd)
121 {
122     vout_window_sys_t *p_sys = wnd->sys;
123     p_sys->native_window.winRelease(p_sys->window); // Release the native window.
124     dlclose(p_sys->p_library); // Close the library.
125     free (p_sys);
126 }
127
128
129 /**
130  * Window control.
131  */
132 static int Control(vout_window_t *wnd, int cmd, va_list ap)
133 {
134     switch (cmd)
135     {
136         case VOUT_WINDOW_SET_SIZE:
137         {
138             unsigned width = va_arg(ap, unsigned);
139             unsigned height = va_arg(ap, unsigned);
140             jni_SetAndroidSurfaceSize(width, height, width, height, 1, 1);
141             break;
142         }
143         case VOUT_WINDOW_SET_STATE:
144         case VOUT_WINDOW_SET_FULLSCREEN:
145             return VLC_EGENERIC;
146         default:
147             msg_Err (wnd, "request %d not implemented", cmd);
148             return VLC_EGENERIC;
149     }
150     return VLC_SUCCESS;
151 }