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 gst_object_unref (srcpad);
167 cleanup_gdpdepay (gdpdepay);
172 GST_START_TEST (test_audio_in_one_buffer)
176 GstElement *gdpdepay;
177 GstBuffer *buffer, *inbuffer, *outbuffer;
179 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
180 guint header_len, payload_len;
183 gdpdepay = setup_gdpdepay ();
184 srcpad = gst_element_get_pad (gdpdepay, "src");
186 fail_unless (gst_element_set_state (gdpdepay,
187 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
188 "could not set to playing");
190 /* make sure no caps are set yet */
191 caps = gst_pad_get_caps (srcpad);
192 fail_unless (gst_caps_is_any (caps));
193 gst_caps_unref (caps);
194 fail_if (gst_pad_get_negotiated_caps (srcpad));
196 /* create caps and buffer packets and push them as one buffer */
197 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
198 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
201 buffer = gst_buffer_new_and_alloc (4);
202 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
203 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
205 payload_len = gst_dp_header_payload_length (caps_header);
207 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
208 payload_len + GST_BUFFER_SIZE (buffer));
209 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
210 i = GST_DP_HEADER_LENGTH;
211 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
213 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
214 i += GST_DP_HEADER_LENGTH;
215 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
216 GST_BUFFER_SIZE (buffer));
218 gst_caps_unref (caps);
219 gst_buffer_unref (buffer);
221 g_free (caps_header);
222 g_free (caps_payload);
226 gst_pad_push (mysrcpad, inbuffer);
228 /* the buffer is still queued */
229 fail_unless_equals_int (g_list_length (buffers), 1);
231 fail_unless (gst_element_set_state (gdpdepay,
232 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
234 gst_object_unref (srcpad);
235 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
236 cleanup_gdpdepay (gdpdepay);
241 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
244 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
248 setup_gdpdepay_streamheader ()
250 GstElement *gdpdepay;
252 GST_DEBUG ("setup_gdpdepay");
253 gdpdepay = gst_check_setup_element ("gdpdepay");
254 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
255 myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate, NULL);
260 /* this tests deserialization of a GDP stream where the serialized caps
261 * have a streamheader set */
262 GST_START_TEST (test_streamheader)
264 GstCaps *caps, *padcaps;
266 GstElement *gdpdepay;
267 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
269 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
270 guint header_len, payload_len;
272 GstStructure *structure;
273 GValue array = { 0 };
274 GValue value = { 0 };
276 gdpdepay = setup_gdpdepay_streamheader ();
277 srcpad = gst_element_get_pad (gdpdepay, "src");
278 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
280 fail_unless (gst_element_set_state (gdpdepay,
281 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
282 "could not set to playing");
284 /* make sure no caps are set yet */
285 caps = gst_pad_get_caps (srcpad);
286 fail_unless (gst_caps_is_any (caps));
287 gst_caps_unref (caps);
288 fail_if (gst_pad_get_negotiated_caps (srcpad));
290 /* create a streamheader buffer and the caps containing it */
291 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
292 structure = gst_caps_get_structure (caps, 0);
293 buffer = gst_buffer_new_and_alloc (4);
294 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
295 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
296 g_value_init (&array, GST_TYPE_ARRAY);
297 g_value_init (&value, GST_TYPE_BUFFER);
298 shbuffer = gst_buffer_copy (buffer);
299 gst_value_set_buffer (&value, shbuffer);
300 gst_buffer_unref (shbuffer);
301 gst_value_array_append_value (&array, &value);
302 g_value_unset (&value);
303 gst_structure_set_value (structure, "streamheader", &array);
304 g_value_unset (&array);
306 gst_buffer_set_caps (buffer, caps);
308 /* create GDP packets for the caps and the buffer, and put them in one
310 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
313 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
315 payload_len = gst_dp_header_payload_length (caps_header);
317 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
318 payload_len + GST_BUFFER_SIZE (buffer));
319 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
320 i = GST_DP_HEADER_LENGTH;
321 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
323 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
324 i += GST_DP_HEADER_LENGTH;
325 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
326 GST_BUFFER_SIZE (buffer));
328 gst_caps_unref (caps);
329 gst_buffer_unref (buffer);
331 g_free (caps_header);
332 g_free (caps_payload);
336 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
337 gst_pad_push (mysrcpad, inbuffer);
339 /* our only output buffer is the streamheader buffer */
340 fail_unless_equals_int (g_list_length (buffers), 1);
341 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
342 buffers = g_list_remove (buffers, outbuffer);
343 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
344 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
346 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
347 caps = gst_buffer_get_caps (outbuffer);
348 fail_if (caps == NULL);
349 fail_if (padcaps == NULL);
350 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
352 fail_unless (gst_caps_is_equal (padcaps, caps));
354 /* FIXME: get streamheader, compare data with buffer */
355 gst_buffer_unref (outbuffer);
356 gst_caps_unref (padcaps);
357 gst_caps_unref (caps);
360 fail_unless (gst_element_set_state (gdpdepay,
361 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
363 gst_object_unref (srcpad);
364 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
365 cleanup_gdpdepay (gdpdepay);
372 gdpdepay_suite (void)
374 Suite *s = suite_create ("gdpdepay");
375 TCase *tc_chain = tcase_create ("general");
377 suite_add_tcase (s, tc_chain);
378 tcase_add_test (tc_chain, test_audio_per_byte);
379 tcase_add_test (tc_chain, test_audio_in_one_buffer);
380 tcase_add_test (tc_chain, test_streamheader);
386 main (int argc, char **argv)
390 Suite *s = gdpdepay_suite ();
391 SRunner *sr = srunner_create (s);
393 gst_check_init (&argc, &argv);
395 srunner_run_all (sr, CK_NORMAL);
396 nf = srunner_ntests_failed (sr);