3 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include <gst/check/gstcheck.h>
25 #include <gst/dataprotocol/dataprotocol.h>
27 GList *buffers = NULL;
28 gboolean have_eos = FALSE;
30 /* For ease of programming we use globals to keep refs for our floating
31 * src and sink pads we create; otherwise we always have to do get_pad,
32 * get_peer, and then remove references in every test function */
33 GstPad *mysrcpad, *mysinkpad, *myshsinkpad;
35 #define AUDIO_CAPS_TEMPLATE_STRING \
37 "rate = (int) [ 1, MAX ], " \
38 "channels = (int) [ 1, 8 ], " \
39 "endianness = (int) BYTE_ORDER, " \
40 "width = (int) {8, 16}, " \
41 "depth = (int) {8, 16}, " \
42 "signed = (boolean) true"
44 #define AUDIO_CAPS_STRING \
46 "rate = (int) 1000, " \
47 "channels = (int) 2, " \
48 "endianness = (int) BYTE_ORDER, " \
49 "width = (int) 16, " \
50 "depth = (int) 16, " \
51 "signed = (boolean) true"
54 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
57 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
59 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-gdp")
65 /* takes over reference for outcaps */
71 GST_DEBUG ("setup_gdpdepay");
72 gdpdepay = gst_check_setup_element ("gdpdepay");
73 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
74 mysinkpad = gst_check_setup_sink_pad (gdpdepay, &sinktemplate, NULL);
80 cleanup_gdpdepay (GstElement * gdpdepay)
82 GST_DEBUG ("cleanup_gdpdepay");
84 gst_check_teardown_src_pad (gdpdepay);
85 gst_check_teardown_sink_pad (gdpdepay);
86 gst_check_teardown_element (gdpdepay);
90 gdpdepay_push_per_byte (gchar * reason, guint8 * bytes, guint length)
95 for (i = 0; i < length; ++i) {
96 inbuffer = gst_buffer_new_and_alloc (1);
97 GST_BUFFER_DATA (inbuffer)[0] = bytes[i];
98 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
99 "%s: failed pushing byte buffer", reason);
103 GST_START_TEST (test_audio_per_byte)
107 GstElement *gdpdepay;
108 GstBuffer *buffer, *inbuffer, *outbuffer;
110 guint8 *header, *payload;
114 gdpdepay = setup_gdpdepay ();
115 srcpad = gst_element_get_pad (gdpdepay, "src");
117 fail_unless (gst_element_set_state (gdpdepay,
118 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
119 "could not set to playing");
121 caps = gst_pad_get_caps (srcpad);
122 fail_unless (gst_caps_is_any (caps));
123 gst_caps_unref (caps);
124 fail_if (gst_pad_get_negotiated_caps (srcpad));
126 /* create caps and buffer packets and push them */
127 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
128 fail_unless (gst_dp_packet_from_caps (caps, 0, &len, &header, &payload));
129 gst_caps_unref (caps);
130 gdpdepay_push_per_byte ("caps header", header, len);
131 fail_unless_equals_int (g_list_length (buffers), 0);
132 gdpdepay_push_per_byte ("caps payload", payload,
133 gst_dp_header_payload_length (header));
134 fail_unless_equals_int (g_list_length (buffers), 0);
135 caps = gst_pad_get_caps (srcpad);
136 fail_if (gst_caps_is_any (caps));
137 gst_caps_unref (caps);
142 buffer = gst_buffer_new_and_alloc (4);
143 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
144 GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
145 GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
146 fail_unless (gst_dp_header_from_buffer (buffer, 0, &len, &header));
147 gdpdepay_push_per_byte ("buffer header", header, len);
148 fail_unless_equals_int (g_list_length (buffers), 0);
149 gdpdepay_push_per_byte ("buffer payload", GST_BUFFER_DATA (buffer),
150 gst_dp_header_payload_length (header));
152 gst_buffer_unref (buffer);
154 fail_unless_equals_int (g_list_length (buffers), 1);
155 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
156 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
157 fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
159 buffers = g_list_remove (buffers, outbuffer);
160 gst_buffer_unref (outbuffer);
162 fail_unless (gst_element_set_state (gdpdepay,
163 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
165 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
166 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
167 g_list_free (buffers);
169 gst_object_unref (srcpad);
170 cleanup_gdpdepay (gdpdepay);
175 GST_START_TEST (test_audio_in_one_buffer)
179 GstElement *gdpdepay;
180 GstBuffer *buffer, *inbuffer, *outbuffer;
182 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
183 guint header_len, payload_len;
186 gdpdepay = setup_gdpdepay ();
187 srcpad = gst_element_get_pad (gdpdepay, "src");
189 fail_unless (gst_element_set_state (gdpdepay,
190 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
191 "could not set to playing");
193 /* make sure no caps are set yet */
194 caps = gst_pad_get_caps (srcpad);
195 fail_unless (gst_caps_is_any (caps));
196 gst_caps_unref (caps);
197 fail_if (gst_pad_get_negotiated_caps (srcpad));
199 /* create caps and buffer packets and push them as one buffer */
200 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
201 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
204 buffer = gst_buffer_new_and_alloc (4);
205 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
206 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
208 payload_len = gst_dp_header_payload_length (caps_header);
210 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
211 payload_len + GST_BUFFER_SIZE (buffer));
212 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
213 i = GST_DP_HEADER_LENGTH;
214 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
216 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
217 i += GST_DP_HEADER_LENGTH;
218 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
219 GST_BUFFER_SIZE (buffer));
221 gst_caps_unref (caps);
222 gst_buffer_unref (buffer);
224 g_free (caps_header);
225 g_free (caps_payload);
229 gst_pad_push (mysrcpad, inbuffer);
231 /* the buffer is still queued */
232 fail_unless_equals_int (g_list_length (buffers), 1);
234 fail_unless (gst_element_set_state (gdpdepay,
235 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
237 gst_object_unref (srcpad);
238 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
239 g_list_free (buffers);
241 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
242 cleanup_gdpdepay (gdpdepay);
247 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
250 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
254 setup_gdpdepay_streamheader ()
256 GstElement *gdpdepay;
258 GST_DEBUG ("setup_gdpdepay");
259 gdpdepay = gst_check_setup_element ("gdpdepay");
260 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
261 myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate, NULL);
266 /* this tests deserialization of a GDP stream where the serialized caps
267 * have a streamheader set */
268 GST_START_TEST (test_streamheader)
270 GstCaps *caps, *padcaps;
272 GstElement *gdpdepay;
273 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
275 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
276 guint header_len, payload_len;
278 GstStructure *structure;
279 GValue array = { 0 };
280 GValue value = { 0 };
282 gdpdepay = setup_gdpdepay_streamheader ();
283 srcpad = gst_element_get_pad (gdpdepay, "src");
284 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
286 fail_unless (gst_element_set_state (gdpdepay,
287 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
288 "could not set to playing");
290 /* make sure no caps are set yet */
291 caps = gst_pad_get_caps (srcpad);
292 fail_unless (gst_caps_is_any (caps));
293 gst_caps_unref (caps);
294 fail_if (gst_pad_get_negotiated_caps (srcpad));
296 /* create a streamheader buffer and the caps containing it */
297 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
298 structure = gst_caps_get_structure (caps, 0);
299 buffer = gst_buffer_new_and_alloc (4);
300 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
301 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
302 g_value_init (&array, GST_TYPE_ARRAY);
303 g_value_init (&value, GST_TYPE_BUFFER);
304 shbuffer = gst_buffer_copy (buffer);
305 gst_value_set_buffer (&value, shbuffer);
306 gst_buffer_unref (shbuffer);
307 gst_value_array_append_value (&array, &value);
308 g_value_unset (&value);
309 gst_structure_set_value (structure, "streamheader", &array);
310 g_value_unset (&array);
312 gst_buffer_set_caps (buffer, caps);
314 /* create GDP packets for the caps and the buffer, and put them in one
316 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
319 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
321 payload_len = gst_dp_header_payload_length (caps_header);
323 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
324 payload_len + GST_BUFFER_SIZE (buffer));
325 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
326 i = GST_DP_HEADER_LENGTH;
327 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
329 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
330 i += GST_DP_HEADER_LENGTH;
331 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
332 GST_BUFFER_SIZE (buffer));
334 gst_caps_unref (caps);
335 gst_buffer_unref (buffer);
337 g_free (caps_header);
338 g_free (caps_payload);
342 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
343 gst_pad_push (mysrcpad, inbuffer);
345 /* our only output buffer is the streamheader buffer */
346 fail_unless_equals_int (g_list_length (buffers), 1);
347 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
348 buffers = g_list_remove (buffers, outbuffer);
349 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
350 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
352 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
353 caps = gst_buffer_get_caps (outbuffer);
354 fail_if (caps == NULL);
355 fail_if (padcaps == NULL);
356 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
358 fail_unless (gst_caps_is_equal (padcaps, caps));
360 /* FIXME: get streamheader, compare data with buffer */
361 gst_buffer_unref (outbuffer);
362 gst_caps_unref (padcaps);
363 gst_caps_unref (caps);
366 fail_unless (gst_element_set_state (gdpdepay,
367 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
369 gst_object_unref (srcpad);
370 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
371 g_list_free (buffers);
373 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
374 cleanup_gdpdepay (gdpdepay);
381 gdpdepay_suite (void)
383 Suite *s = suite_create ("gdpdepay");
384 TCase *tc_chain = tcase_create ("general");
386 suite_add_tcase (s, tc_chain);
387 tcase_add_test (tc_chain, test_audio_per_byte);
388 tcase_add_test (tc_chain, test_audio_in_one_buffer);
389 tcase_add_test (tc_chain, test_streamheader);
395 main (int argc, char **argv)
399 Suite *s = gdpdepay_suite ();
400 SRunner *sr = srunner_create (s);
402 gst_check_init (&argc, &argv);
404 srunner_run_all (sr, CK_NORMAL);
405 nf = srunner_ntests_failed (sr);