Tizen 2.0 Release
[framework/multimedia/gstreamer-vaapi.git] / tests / output.c
1 /*
2  *  output.c - Video output helpers
3  *
4  *  Copyright (C) 2012 Intel Corporation
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public License
8  *  as published by the Free Software Foundation; either version 2.1
9  *  of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free
18  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301 USA
20  */
21
22 #include "config.h"
23 #include <string.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_WAYLAND
38 # include <gst/vaapi/gstvaapidisplay_wayland.h>
39 # include <gst/vaapi/gstvaapiwindow_wayland.h>
40 #endif
41 #include "output.h"
42
43 static const VideoOutputInfo *g_video_output;
44 static const VideoOutputInfo g_video_outputs[] = {
45     /* Video outputs are sorted in test order for automatic characterisation */
46 #if USE_WAYLAND
47     { "wayland",
48       gst_vaapi_display_wayland_new,
49       gst_vaapi_window_wayland_new
50     },
51 #endif
52 #if USE_X11
53     { "x11",
54       gst_vaapi_display_x11_new,
55       gst_vaapi_window_x11_new
56     },
57 #endif
58 #if USE_GLX
59     { "glx",
60       gst_vaapi_display_glx_new,
61       gst_vaapi_window_glx_new
62     },
63 #endif
64 #if USE_DRM
65     { "drm",
66       gst_vaapi_display_drm_new,
67       gst_vaapi_window_drm_new
68     },
69 #endif
70     { NULL, }
71 };
72
73 static gchar *g_output_name;
74 static gboolean g_list_outputs = FALSE;
75
76 static GOptionEntry g_options[] = {
77     { "list-outputs", 0,
78       0,
79       G_OPTION_ARG_NONE, &g_list_outputs,
80       "list video outputs", NULL },
81     { "output", 'o',
82       0,
83       G_OPTION_ARG_STRING, &g_output_name,
84       "video output name", NULL },
85     { NULL, }
86 };
87
88 static void
89 list_outputs(void)
90 {
91     const VideoOutputInfo *o;
92
93     g_print("Video outputs:");
94     for (o = g_video_outputs; o->name != NULL; o++)
95         g_print(" %s", o->name);
96     g_print("\n");
97 }
98
99 gboolean
100 video_output_init(int *argc, char *argv[], GOptionEntry *options)
101 {
102     GOptionContext *ctx;
103     gboolean success;
104
105 #if !GLIB_CHECK_VERSION(2,31,0)
106     if (!g_thread_supported())
107         g_thread_init(NULL);
108 #endif
109
110     ctx = g_option_context_new("- test options");
111     if (!ctx)
112         return FALSE;
113
114     g_option_context_add_group(ctx, gst_init_get_option_group());
115     g_option_context_add_main_entries(ctx, g_options, NULL);
116     if (options)
117         g_option_context_add_main_entries(ctx, options, NULL);
118     success = g_option_context_parse(ctx, argc, &argv, NULL);
119     g_option_context_free(ctx);
120
121     if (g_list_outputs) {
122         list_outputs();
123         exit(0);
124     }
125     return success;
126 }
127
128 void
129 video_output_exit(void)
130 {
131     g_free(g_output_name);
132     gst_deinit();
133 }
134
135 const VideoOutputInfo *
136 video_output_lookup(const gchar *output_name)
137 {
138     const VideoOutputInfo *o;
139
140     if (!output_name)
141         return NULL;
142
143     for (o = g_video_outputs; o->name != NULL; o++) {
144         if (g_ascii_strcasecmp(o->name, output_name) == 0)
145             return o;
146     }
147     return NULL;
148 }
149
150 GstVaapiDisplay *
151 video_output_create_display(const gchar *display_name)
152 {
153     const VideoOutputInfo *o = g_video_output;
154     GstVaapiDisplay *display = NULL;
155
156     if (!o) {
157         if (g_output_name)
158             o = video_output_lookup(g_output_name);
159         else {
160             for (o = g_video_outputs; o->name != NULL; o++) {
161                 display = o->create_display(display_name);
162                 if (display) {
163                     if (gst_vaapi_display_get_display(display))
164                         break;
165                     g_object_unref(display);
166                     display = NULL;
167                 }
168             }
169         }
170         if (!o || !o->name)
171             return NULL;
172         g_print("Using %s video output\n", o->name);
173         g_video_output = o;
174     }
175
176     if (!display)
177         display = o->create_display(display_name);
178     return display;
179 }
180
181 GstVaapiWindow *
182 video_output_create_window(GstVaapiDisplay *display, guint width, guint height)
183 {
184     if (!g_video_output)
185         return NULL;
186     return g_video_output->create_window(display, width, height);
187 }