Merging gst-editing-services
[platform/upstream/gstreamer.git] / subprojects / gstreamer-vaapi / tests / internal / output.c
1 /*
2  *  output.c - Video output helpers
3  *
4  *  Copyright (C) 2012-2013 Intel Corporation
5  *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22
23 #include "gst/vaapi/sysdeps.h"
24 #include <gst/gst.h>
25 #if USE_DRM
26 # include <gst/vaapi/gstvaapidisplay_drm.h>
27 # include <gst/vaapi/gstvaapiwindow_drm.h>
28 #endif
29 #if USE_X11
30 # include <gst/vaapi/gstvaapidisplay_x11.h>
31 # include <gst/vaapi/gstvaapiwindow_x11.h>
32 #endif
33 #if USE_GLX
34 # include <gst/vaapi/gstvaapidisplay_glx.h>
35 # include <gst/vaapi/gstvaapiwindow_glx.h>
36 #endif
37 #if USE_EGL
38 # include <gst/vaapi/gstvaapidisplay_egl.h>
39 # include <gst/vaapi/gstvaapiwindow_egl.h>
40 #endif
41 #if USE_WAYLAND
42 # include <gst/vaapi/gstvaapidisplay_wayland.h>
43 # include <gst/vaapi/gstvaapiwindow_wayland.h>
44 #endif
45 #include "output.h"
46
47 static const VideoOutputInfo *g_video_output;
48 static const VideoOutputInfo g_video_outputs[] = {
49   /* Video outputs are sorted in test order for automatic characterisation */
50 #if USE_WAYLAND
51   {"wayland",
52         gst_vaapi_display_wayland_new,
53       gst_vaapi_window_wayland_new},
54 #endif
55 #if USE_X11
56   {"x11",
57         gst_vaapi_display_x11_new,
58       gst_vaapi_window_x11_new},
59 #endif
60 #if USE_GLX
61   {"glx",
62         gst_vaapi_display_glx_new,
63       gst_vaapi_window_glx_new},
64 #endif
65 #if USE_DRM
66   {"drm",
67         gst_vaapi_display_drm_new,
68       gst_vaapi_window_drm_new},
69 #endif
70   {NULL,}
71 };
72
73 static gchar *g_output_name;
74 static gboolean g_list_outputs = FALSE;
75 static gboolean g_fullscreen = FALSE;
76
77 static gboolean g_egl_mode = FALSE;
78 static guint g_gles_version;
79
80 static GOptionEntry g_options[] = {
81   {"list-outputs", 0,
82         0,
83         G_OPTION_ARG_NONE, &g_list_outputs,
84       "list video outputs", NULL},
85   {"output", 'o',
86         0,
87         G_OPTION_ARG_STRING, &g_output_name,
88       "video output name", NULL},
89   {"fullscreen", 'f',
90         0,
91         G_OPTION_ARG_NONE, &g_fullscreen,
92       "fullscreen mode", NULL},
93   {"egl", 0,
94         0,
95         G_OPTION_ARG_NONE, &g_egl_mode,
96       "enable EGL rendering", NULL},
97   {"gles-version", 0,
98         0,
99         G_OPTION_ARG_INT, &g_gles_version,
100       "OpenGL|ES version (in --egl mode)", NULL},
101   {NULL,}
102 };
103
104 static void
105 list_outputs (void)
106 {
107   const VideoOutputInfo *o;
108
109   g_print ("Video outputs:");
110   for (o = g_video_outputs; o->name != NULL; o++)
111     g_print (" %s", o->name);
112   g_print ("\n");
113 }
114
115 gboolean
116 video_output_init (int *argc, char *argv[], GOptionEntry * options)
117 {
118   GOptionContext *ctx;
119   gboolean success;
120
121   ctx = g_option_context_new ("- test options");
122   if (!ctx)
123     return FALSE;
124
125   g_option_context_add_group (ctx, gst_init_get_option_group ());
126   g_option_context_add_main_entries (ctx, g_options, NULL);
127   if (options)
128     g_option_context_add_main_entries (ctx, options, NULL);
129   success = g_option_context_parse (ctx, argc, &argv, NULL);
130   g_option_context_free (ctx);
131
132   if (g_list_outputs) {
133     list_outputs ();
134     exit (0);
135   }
136   return success;
137 }
138
139 void
140 video_output_exit (void)
141 {
142   g_free (g_output_name);
143   gst_deinit ();
144 }
145
146 const VideoOutputInfo *
147 video_output_lookup (const gchar * output_name)
148 {
149   const VideoOutputInfo *o;
150
151   if (!output_name)
152     return NULL;
153
154   for (o = g_video_outputs; o->name != NULL; o++) {
155     if (g_ascii_strcasecmp (o->name, output_name) == 0)
156       return o;
157   }
158   return NULL;
159 }
160
161 GstVaapiDisplay *
162 video_output_create_display (const gchar * display_name)
163 {
164   const VideoOutputInfo *o = g_video_output;
165   GstVaapiDisplay *egl_display, *display = NULL;
166
167   if (!o) {
168     if (g_output_name)
169       o = video_output_lookup (g_output_name);
170     else {
171       for (o = g_video_outputs; o->name != NULL; o++) {
172         display = o->create_display (display_name);
173         if (display) {
174           if (gst_vaapi_display_get_display (display))
175             break;
176           gst_object_unref (display);
177           display = NULL;
178         }
179       }
180     }
181     if (!o || !o->name)
182       return NULL;
183     g_print ("Using %s video output\n", o->name);
184     g_video_output = o;
185   }
186
187   if (!display)
188     display = o->create_display (display_name);
189
190   if (g_egl_mode) {
191 #if USE_EGL
192     egl_display = gst_vaapi_display_egl_new (display, g_gles_version);
193 #else
194     egl_display = NULL;
195     g_print ("error: unsupported EGL renderering mode\n");
196 #endif
197     gst_object_unref (display);
198     if (!egl_display)
199       return NULL;
200     display = egl_display;
201   }
202   return display;
203 }
204
205 GstVaapiWindow *
206 video_output_create_window (GstVaapiDisplay * display, guint width,
207     guint height)
208 {
209   GstVaapiWindow *window;
210
211   if (!g_video_output)
212     return NULL;
213
214 #if USE_EGL
215   if (g_egl_mode)
216     window = gst_vaapi_window_egl_new (display, width, height);
217   else
218 #endif
219     window = g_video_output->create_window (display, width, height);
220   if (!window)
221     return NULL;
222
223   /* Force fullscreen mode, should this be requested by the user */
224   if (g_fullscreen)
225     gst_vaapi_window_set_fullscreen (window, TRUE);
226   return window;
227 }