dd76bc7391e35313323fc796c755d8d345c493e3
[profile/ivi/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_X11
26 # include <gst/vaapi/gstvaapidisplay_x11.h>
27 # include <gst/vaapi/gstvaapiwindow_x11.h>
28 #endif
29 #include "output.h"
30
31 static const VideoOutputInfo *g_video_output;
32 static const VideoOutputInfo g_video_outputs[] = {
33     /* Video outputs are sorted in test order for automatic characterisation */
34 #if USE_X11
35     { "x11",
36       gst_vaapi_display_x11_new,
37       gst_vaapi_window_x11_new
38     },
39 #endif
40     { NULL, }
41 };
42
43 static gchar *g_output_name;
44 static gboolean g_list_outputs = FALSE;
45
46 static GOptionEntry g_options[] = {
47     { "list-outputs", 0,
48       0,
49       G_OPTION_ARG_NONE, &g_list_outputs,
50       "list video outputs", NULL },
51     { "output", 'o',
52       0,
53       G_OPTION_ARG_STRING, &g_output_name,
54       "video output name", NULL },
55     { NULL, }
56 };
57
58 static void
59 list_outputs(void)
60 {
61     const VideoOutputInfo *o;
62
63     g_print("Video outputs:");
64     for (o = g_video_outputs; o->name != NULL; o++)
65         g_print(" %s", o->name);
66     g_print("\n");
67 }
68
69 gboolean
70 video_output_init(int *argc, char *argv[], GOptionEntry *options)
71 {
72     GOptionContext *ctx;
73     gboolean success;
74
75 #if !GLIB_CHECK_VERSION(2,31,0)
76     if (!g_thread_supported())
77         g_thread_init(NULL);
78 #endif
79
80     ctx = g_option_context_new("- test options");
81     if (!ctx)
82         return FALSE;
83
84     g_option_context_add_group(ctx, gst_init_get_option_group());
85     g_option_context_add_main_entries(ctx, g_options, NULL);
86     if (options)
87         g_option_context_add_main_entries(ctx, options, NULL);
88     success = g_option_context_parse(ctx, argc, &argv, NULL);
89     g_option_context_free(ctx);
90
91     if (g_list_outputs) {
92         list_outputs();
93         exit(0);
94     }
95     return success;
96 }
97
98 void
99 video_output_exit(void)
100 {
101     g_free(g_output_name);
102     gst_deinit();
103 }
104
105 const VideoOutputInfo *
106 video_output_lookup(const gchar *output_name)
107 {
108     const VideoOutputInfo *o;
109
110     if (!output_name)
111         return NULL;
112
113     for (o = g_video_outputs; o->name != NULL; o++) {
114         if (g_ascii_strcasecmp(o->name, output_name) == 0)
115             return o;
116     }
117     return NULL;
118 }
119
120 GstVaapiDisplay *
121 video_output_create_display(const gchar *display_name)
122 {
123     const VideoOutputInfo *o = g_video_output;
124     GstVaapiDisplay *display = NULL;
125
126     if (!o) {
127         if (g_output_name)
128             o = video_output_lookup(g_output_name);
129         else {
130             for (o = g_video_outputs; o->name != NULL; o++) {
131                 display = o->create_display(display_name);
132                 if (display) {
133                     if (gst_vaapi_display_get_display(display))
134                         break;
135                     g_object_unref(display);
136                     display = NULL;
137                 }
138             }
139         }
140         if (!o || !o->name)
141             return NULL;
142         g_print("Using %s video output\n", o->name);
143         g_video_output = o;
144     }
145
146     if (!display)
147         display = o->create_display(display_name);
148     return display;
149 }
150
151 GstVaapiWindow *
152 video_output_create_window(GstVaapiDisplay *display, guint width, guint height)
153 {
154     if (!g_video_output)
155         return NULL;
156     return g_video_output->create_window(display, width, height);
157 }