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.
25 #include <gst/check/gstcheck.h>
26 #include <gst/dataprotocol/dataprotocol.h>
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 #ifndef HAVE_CPU_PPC64 /* Test known to fail on PPC64. See #348114 */
268 /* this tests deserialization of a GDP stream where the serialized caps
269 * have a streamheader set */
270 GST_START_TEST (test_streamheader)
272 GstCaps *caps, *padcaps;
274 GstElement *gdpdepay;
275 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
277 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
278 guint header_len, payload_len;
280 GstStructure *structure;
281 GValue array = { 0 };
282 GValue value = { 0 };
284 gdpdepay = setup_gdpdepay_streamheader ();
285 srcpad = gst_element_get_pad (gdpdepay, "src");
286 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
288 fail_unless (gst_element_set_state (gdpdepay,
289 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
290 "could not set to playing");
292 /* make sure no caps are set yet */
293 caps = gst_pad_get_caps (srcpad);
294 fail_unless (gst_caps_is_any (caps));
295 gst_caps_unref (caps);
296 fail_if (gst_pad_get_negotiated_caps (srcpad));
298 /* create a streamheader buffer and the caps containing it */
299 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
300 structure = gst_caps_get_structure (caps, 0);
301 buffer = gst_buffer_new_and_alloc (4);
302 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
303 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
304 g_value_init (&array, GST_TYPE_ARRAY);
305 g_value_init (&value, GST_TYPE_BUFFER);
306 shbuffer = gst_buffer_copy (buffer);
307 gst_value_set_buffer (&value, shbuffer);
308 gst_buffer_unref (shbuffer);
309 gst_value_array_append_value (&array, &value);
310 g_value_unset (&value);
311 gst_structure_set_value (structure, "streamheader", &array);
312 g_value_unset (&array);
314 gst_buffer_set_caps (buffer, caps);
316 /* create GDP packets for the caps and the buffer, and put them in one
318 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
321 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
323 payload_len = gst_dp_header_payload_length (caps_header);
325 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
326 payload_len + GST_BUFFER_SIZE (buffer));
327 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
328 i = GST_DP_HEADER_LENGTH;
329 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
331 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
332 i += GST_DP_HEADER_LENGTH;
333 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
334 GST_BUFFER_SIZE (buffer));
336 gst_caps_unref (caps);
337 gst_buffer_unref (buffer);
339 g_free (caps_header);
340 g_free (caps_payload);
344 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
345 gst_pad_push (mysrcpad, inbuffer);
347 /* our only output buffer is the streamheader buffer */
348 fail_unless_equals_int (g_list_length (buffers), 1);
349 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
350 buffers = g_list_remove (buffers, outbuffer);
351 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
352 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
354 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
355 caps = gst_buffer_get_caps (outbuffer);
356 fail_if (caps == NULL);
357 fail_if (padcaps == NULL);
358 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
360 fail_unless (gst_caps_is_equal (padcaps, caps));
362 /* FIXME: get streamheader, compare data with buffer */
363 gst_buffer_unref (outbuffer);
364 gst_caps_unref (padcaps);
365 gst_caps_unref (caps);
368 fail_unless (gst_element_set_state (gdpdepay,
369 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
371 gst_object_unref (srcpad);
372 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
373 g_list_free (buffers);
375 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
376 cleanup_gdpdepay (gdpdepay);
381 #endif /* ifndef HAVE_CPU_PPC64 */
384 gdpdepay_suite (void)
386 Suite *s = suite_create ("gdpdepay");
387 TCase *tc_chain = tcase_create ("general");
389 suite_add_tcase (s, tc_chain);
390 tcase_add_test (tc_chain, test_audio_per_byte);
391 tcase_add_test (tc_chain, test_audio_in_one_buffer);
392 #ifndef HAVE_CPU_PPC64 /* Test known to fail on PPC64. See #348114 */
393 tcase_add_test (tc_chain, test_streamheader);
399 main (int argc, char **argv)
403 Suite *s = gdpdepay_suite ();
404 SRunner *sr = srunner_create (s);
406 gst_check_init (&argc, &argv);
408 srunner_run_all (sr, CK_NORMAL);
409 nf = srunner_ntests_failed (sr);