1 /* GStreamer unit test for the gdkpixbufsink element
2 * Copyright (C) 2008 Tim-Philipp Müller <tim centricular net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <gst/check/gstcheck.h>
25 #include <gst/video/video.h>
26
27 #include <gdk-pixbuf/gdk-pixbuf.h>
28
29 #define CAPS_RGB "video/x-raw, format=RGB"
30 #define CAPS_RGBA "video/x-raw, format=RGBA"
31 #define WxH ",width=(int)319,height=(int)241"
32
33 #define N_BUFFERS 5
34
35 typedef struct
36 {
37 GstElement *pipe;
38 GstElement *src;
39 GstElement *filter;
40 GstElement *sink;
41 } GstGdkPixbufSinkTestContext;
42
43 static void
gdkpixbufsink_init_test_context(GstGdkPixbufSinkTestContext * ctx,const gchar * filter_caps_string,gint num_buffers)44 gdkpixbufsink_init_test_context (GstGdkPixbufSinkTestContext * ctx,
45 const gchar * filter_caps_string, gint num_buffers)
46 {
47 GstCaps *caps;
48
49 fail_unless (ctx != NULL);
50
51 ctx->pipe = gst_pipeline_new ("pipeline");
52 fail_unless (ctx->pipe != NULL);
53 ctx->src = gst_element_factory_make ("videotestsrc", "src");
54 fail_unless (ctx->src != NULL, "Failed to create videotestsrc element");
55 ctx->filter = gst_element_factory_make ("capsfilter", "filter");
56 fail_unless (ctx->filter != NULL, "Failed to create capsfilter element");
57 ctx->sink = gst_element_factory_make ("gdkpixbufsink", "sink");
58 fail_unless (ctx->sink != NULL, "Failed to create gdkpixbufsink element");
59
60 caps = gst_caps_from_string (filter_caps_string);
61 fail_unless (caps != NULL);
62 g_object_set (ctx->filter, "caps", caps, NULL);
63 gst_caps_unref (caps);
64
65 if (num_buffers > 0)
66 g_object_set (ctx->src, "num-buffers", num_buffers, NULL);
67
68 fail_unless (gst_bin_add (GST_BIN (ctx->pipe), ctx->src));
69 fail_unless (gst_bin_add (GST_BIN (ctx->pipe), ctx->filter));
70 fail_unless (gst_bin_add (GST_BIN (ctx->pipe), ctx->sink));
71 fail_unless (gst_element_link (ctx->src, ctx->filter));
72 fail_unless (gst_element_link (ctx->filter, ctx->sink));
73 }
74
75 static void
gdkpixbufsink_unset_test_context(GstGdkPixbufSinkTestContext * ctx)76 gdkpixbufsink_unset_test_context (GstGdkPixbufSinkTestContext * ctx)
77 {
78 gst_element_set_state (ctx->pipe, GST_STATE_NULL);
79 gst_object_unref (ctx->pipe);
80 memset (ctx, 0, sizeof (GstGdkPixbufSinkTestContext));
81 }
82
83 static gboolean
check_last_pixbuf(GstGdkPixbufSinkTestContext * ctx,gpointer pixbuf)84 check_last_pixbuf (GstGdkPixbufSinkTestContext * ctx, gpointer pixbuf)
85 {
86 gpointer last_pb = NULL;
87 gboolean ret;
88
89 g_object_get (ctx->sink, "last-pixbuf", &last_pb, NULL);
90
91 ret = (last_pb == pixbuf);
92
93 if (last_pb)
94 g_object_unref (last_pb);
95
96 return ret;
97 }
98
99 /* doesn't return a ref to the pixbuf */
100 static GdkPixbuf *
check_message_pixbuf(GstMessage * msg,const gchar * name,gint channels,gboolean has_alpha)101 check_message_pixbuf (GstMessage * msg, const gchar * name, gint channels,
102 gboolean has_alpha)
103 {
104 GdkPixbuf *pixbuf;
105 const GstStructure *s;
106
107 fail_unless (gst_message_get_structure (msg) != NULL);
108
109 s = gst_message_get_structure (msg);
110 fail_unless_equals_string (gst_structure_get_name (s), name);
111
112 fail_unless (gst_structure_has_field (s, "pixbuf"));
113 fail_unless (gst_structure_has_field_typed (s, "pixel-aspect-ratio",
114 GST_TYPE_FRACTION));
115 pixbuf =
116 GDK_PIXBUF (g_value_get_object (gst_structure_get_value (s, "pixbuf")));
117 fail_unless (GDK_IS_PIXBUF (pixbuf));
118 fail_unless_equals_int (gdk_pixbuf_get_n_channels (pixbuf), channels);
119 fail_unless_equals_int (gdk_pixbuf_get_has_alpha (pixbuf), has_alpha);
120 fail_unless_equals_int (gdk_pixbuf_get_width (pixbuf), 319);
121 fail_unless_equals_int (gdk_pixbuf_get_height (pixbuf), 241);
122
123 return pixbuf;
124 }
125
GST_START_TEST(test_rgb)126 GST_START_TEST (test_rgb)
127 {
128 GstGdkPixbufSinkTestContext ctx;
129 GstMessage *msg;
130 GdkPixbuf *pixbuf;
131 GstBus *bus;
132 gint i;
133
134 gdkpixbufsink_init_test_context (&ctx, CAPS_RGB WxH, N_BUFFERS);
135
136 fail_unless (check_last_pixbuf (&ctx, NULL));
137
138 /* start prerolling */
139 fail_unless_equals_int (gst_element_set_state (ctx.pipe, GST_STATE_PAUSED),
140 GST_STATE_CHANGE_ASYNC);
141
142 /* wait until prerolled */
143 fail_unless_equals_int (gst_element_get_state (ctx.pipe, NULL, NULL, -1),
144 GST_STATE_CHANGE_SUCCESS);
145
146 bus = gst_element_get_bus (ctx.pipe);
147
148 /* find element message from our gdkpixbufsink, ignore element messages from
149 * other elemements (which just seems prudent to do, we don't expect any) */
150 while (1) {
151 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_ELEMENT);
152 fail_if (msg == NULL, "Expected element message from gdkpixbufsink");
153
154 if (msg->src == GST_OBJECT (ctx.sink))
155 break;
156 }
157
158 pixbuf = check_message_pixbuf (msg, "preroll-pixbuf", 3, FALSE);
159 fail_unless (check_last_pixbuf (&ctx, pixbuf));
160 gst_message_unref (msg);
161 pixbuf = NULL;
162
163 /* and go! */
164 fail_unless_equals_int (gst_element_set_state (ctx.pipe, GST_STATE_PLAYING),
165 GST_STATE_CHANGE_SUCCESS);
166
167 /* This is racy, supposed to make sure locking and refcounting works at
168 * least to some extent */
169 for (i = 0; i < 10000; ++i) {
170 gpointer obj = NULL;
171
172 g_object_get (ctx.sink, "last-pixbuf", &obj, NULL);
173 fail_unless (obj == NULL || GDK_IS_PIXBUF (obj));
174 if (obj)
175 g_object_unref (obj);
176 }
177
178 /* there should be as many pixbuf messages as buffers */
179 for (i = 0; i < N_BUFFERS; ++i) {
180 /* find element message from our gdkpixbufsink, ignore element messages from
181 * other elemements (which just seems prudent to do, we don't expect any) */
182 while (1) {
183 msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_ELEMENT);
184 fail_if (msg == NULL, "Expected element message from gdkpixbufsink");
185 if (msg->src == GST_OBJECT (ctx.sink))
186 break;
187 }
188
189 pixbuf = check_message_pixbuf (msg, "pixbuf", 3, FALSE);
190 gst_message_unref (msg);
191 }
192
193 /* note: we don't hold a ref to pixbuf any longer here, but it should be ok */
194 fail_unless (check_last_pixbuf (&ctx, pixbuf));
195 pixbuf = NULL;
196
197 gdkpixbufsink_unset_test_context (&ctx);
198 gst_object_unref (bus);
199 }
200
201 GST_END_TEST;
202
GST_START_TEST(test_rgba)203 GST_START_TEST (test_rgba)
204 {
205 GstGdkPixbufSinkTestContext ctx;
206 GstMessage *msg;
207 GdkPixbuf *pixbuf;
208 GstBus *bus;
209 gint i;
210
211 gdkpixbufsink_init_test_context (&ctx, CAPS_RGBA WxH, N_BUFFERS);
212
213 fail_unless (check_last_pixbuf (&ctx, NULL));
214
215 /* start prerolling */
216 fail_unless_equals_int (gst_element_set_state (ctx.pipe, GST_STATE_PAUSED),
217 GST_STATE_CHANGE_ASYNC);
218
219 /* wait until prerolled */
220 fail_unless_equals_int (gst_element_get_state (ctx.pipe, NULL, NULL, -1),
221 GST_STATE_CHANGE_SUCCESS);
222
223 bus = gst_element_get_bus (ctx.pipe);
224
225 /* find element message from our gdkpixbufsink, ignore element messages from
226 * other elemements (which just seems prudent to do, we don't expect any) */
227 while (1) {
228 msg = gst_bus_pop_filtered (bus, GST_MESSAGE_ELEMENT);
229 fail_if (msg == NULL, "Expected element message from gdkpixbufsink");
230
231 if (msg->src == GST_OBJECT (ctx.sink))
232 break;
233 }
234
235 pixbuf = check_message_pixbuf (msg, "preroll-pixbuf", 4, TRUE);
236 fail_unless (check_last_pixbuf (&ctx, pixbuf));
237 gst_message_unref (msg);
238 pixbuf = NULL;
239
240 /* and go! */
241 fail_unless_equals_int (gst_element_set_state (ctx.pipe, GST_STATE_PLAYING),
242 GST_STATE_CHANGE_SUCCESS);
243
244 /* This is racy, supposed to make sure locking and refcounting works at
245 * least to some extent */
246 for (i = 0; i < 10000; ++i) {
247 gpointer obj = NULL;
248
249 g_object_get (ctx.sink, "last-pixbuf", &obj, NULL);
250 fail_unless (obj == NULL || GDK_IS_PIXBUF (obj));
251 if (obj)
252 g_object_unref (obj);
253 }
254
255 /* there should be as many pixbuf messages as buffers */
256 for (i = 0; i < N_BUFFERS; ++i) {
257 /* find element message from our gdkpixbufsink, ignore element messages from
258 * other elemements (which just seems prudent to do, we don't expect any) */
259 while (1) {
260 msg = gst_bus_timed_pop_filtered (bus, -1, GST_MESSAGE_ELEMENT);
261 fail_if (msg == NULL, "Expected element message from gdkpixbufsink");
262 if (msg->src == GST_OBJECT (ctx.sink))
263 break;
264 }
265
266 pixbuf = check_message_pixbuf (msg, "pixbuf", 4, TRUE);
267 gst_message_unref (msg);
268 }
269
270 /* note: we don't hold a ref to pixbuf any longer here, but it should be ok */
271 fail_unless (check_last_pixbuf (&ctx, pixbuf));
272 pixbuf = NULL;
273
274 gdkpixbufsink_unset_test_context (&ctx);
275 gst_object_unref (bus);
276 }
277
278 GST_END_TEST;
279
280 static Suite *
gdkpixbufsink_suite(void)281 gdkpixbufsink_suite (void)
282 {
283 Suite *s = suite_create ("gdkpixbufsink");
284 TCase *tc_chain = tcase_create ("general");
285
286 suite_add_tcase (s, tc_chain);
287 tcase_add_test (tc_chain, test_rgb);
288 tcase_add_test (tc_chain, test_rgba);
289
290 return s;
291 }
292
293 GST_CHECK_MAIN (gdkpixbufsink);
294