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 gboolean have_eos = FALSE;
29 /* For ease of programming we use globals to keep refs for our floating
30 * src and sink pads we create; otherwise we always have to do get_pad,
31 * get_peer, and then remove references in every test function */
32 GstPad *mysrcpad, *mysinkpad, *myshsinkpad;
34 #define AUDIO_CAPS_TEMPLATE_STRING \
36 "rate = (int) [ 1, MAX ], " \
37 "channels = (int) [ 1, 8 ], " \
38 "endianness = (int) BYTE_ORDER, " \
39 "width = (int) {8, 16}, " \
40 "depth = (int) {8, 16}, " \
41 "signed = (boolean) true"
43 #define AUDIO_CAPS_STRING \
45 "rate = (int) 1000, " \
46 "channels = (int) 2, " \
47 "endianness = (int) BYTE_ORDER, " \
48 "width = (int) 16, " \
49 "depth = (int) 16, " \
50 "signed = (boolean) true"
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
58 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 GST_STATIC_CAPS ("application/x-gdp")
64 /* takes over reference for outcaps */
70 GST_DEBUG ("setup_gdpdepay");
71 gdpdepay = gst_check_setup_element ("gdpdepay");
72 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
73 mysinkpad = gst_check_setup_sink_pad (gdpdepay, &sinktemplate, NULL);
79 cleanup_gdpdepay (GstElement * gdpdepay)
81 GST_DEBUG ("cleanup_gdpdepay");
83 gst_check_teardown_src_pad (gdpdepay);
84 gst_check_teardown_sink_pad (gdpdepay);
85 gst_check_teardown_element (gdpdepay);
89 gdpdepay_push_per_byte (gchar * reason, guint8 * bytes, guint length)
94 for (i = 0; i < length; ++i) {
95 inbuffer = gst_buffer_new_and_alloc (1);
96 GST_BUFFER_DATA (inbuffer)[0] = bytes[i];
97 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
98 "%s: failed pushing byte buffer", reason);
102 GST_START_TEST (test_audio_per_byte)
106 GstElement *gdpdepay;
107 GstBuffer *buffer, *inbuffer, *outbuffer;
109 guint8 *header, *payload;
113 gdpdepay = setup_gdpdepay ();
114 srcpad = gst_element_get_pad (gdpdepay, "src");
116 fail_unless (gst_element_set_state (gdpdepay,
117 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
118 "could not set to playing");
120 caps = gst_pad_get_caps (srcpad);
121 fail_unless (gst_caps_is_any (caps));
122 gst_caps_unref (caps);
123 fail_if (gst_pad_get_negotiated_caps (srcpad));
125 /* create caps and buffer packets and push them */
126 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
127 fail_unless (gst_dp_packet_from_caps (caps, 0, &len, &header, &payload));
128 gst_caps_unref (caps);
129 gdpdepay_push_per_byte ("caps header", header, len);
130 fail_unless_equals_int (g_list_length (buffers), 0);
131 gdpdepay_push_per_byte ("caps payload", payload,
132 gst_dp_header_payload_length (header));
133 fail_unless_equals_int (g_list_length (buffers), 0);
134 caps = gst_pad_get_caps (srcpad);
135 fail_if (gst_caps_is_any (caps));
136 gst_caps_unref (caps);
141 buffer = gst_buffer_new_and_alloc (4);
142 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
143 GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
144 GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
145 fail_unless (gst_dp_header_from_buffer (buffer, 0, &len, &header));
146 gdpdepay_push_per_byte ("buffer header", header, len);
147 fail_unless_equals_int (g_list_length (buffers), 0);
148 gdpdepay_push_per_byte ("buffer payload", GST_BUFFER_DATA (buffer),
149 gst_dp_header_payload_length (header));
151 gst_buffer_unref (buffer);
153 fail_unless_equals_int (g_list_length (buffers), 1);
154 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
155 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
156 fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
158 buffers = g_list_remove (buffers, outbuffer);
159 gst_buffer_unref (outbuffer);
161 fail_unless (gst_element_set_state (gdpdepay,
162 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
164 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
165 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
166 g_list_free (buffers);
168 gst_object_unref (srcpad);
169 cleanup_gdpdepay (gdpdepay);
174 GST_START_TEST (test_audio_in_one_buffer)
178 GstElement *gdpdepay;
179 GstBuffer *buffer, *inbuffer, *outbuffer;
181 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
182 guint header_len, payload_len;
185 gdpdepay = setup_gdpdepay ();
186 srcpad = gst_element_get_pad (gdpdepay, "src");
188 fail_unless (gst_element_set_state (gdpdepay,
189 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
190 "could not set to playing");
192 /* make sure no caps are set yet */
193 caps = gst_pad_get_caps (srcpad);
194 fail_unless (gst_caps_is_any (caps));
195 gst_caps_unref (caps);
196 fail_if (gst_pad_get_negotiated_caps (srcpad));
198 /* create caps and buffer packets and push them as one buffer */
199 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
200 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
203 buffer = gst_buffer_new_and_alloc (4);
204 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
205 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
207 payload_len = gst_dp_header_payload_length (caps_header);
209 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
210 payload_len + GST_BUFFER_SIZE (buffer));
211 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
212 i = GST_DP_HEADER_LENGTH;
213 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
215 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
216 i += GST_DP_HEADER_LENGTH;
217 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
218 GST_BUFFER_SIZE (buffer));
220 gst_caps_unref (caps);
221 gst_buffer_unref (buffer);
223 g_free (caps_header);
224 g_free (caps_payload);
228 gst_pad_push (mysrcpad, inbuffer);
230 /* the buffer is still queued */
231 fail_unless_equals_int (g_list_length (buffers), 1);
233 fail_unless (gst_element_set_state (gdpdepay,
234 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
236 gst_object_unref (srcpad);
237 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
238 g_list_free (buffers);
240 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
241 cleanup_gdpdepay (gdpdepay);
246 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
249 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
253 setup_gdpdepay_streamheader ()
255 GstElement *gdpdepay;
257 GST_DEBUG ("setup_gdpdepay");
258 gdpdepay = gst_check_setup_element ("gdpdepay");
259 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
260 myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate, NULL);
265 /* this tests deserialization of a GDP stream where the serialized caps
266 * have a streamheader set */
267 GST_START_TEST (test_streamheader)
269 GstCaps *caps, *padcaps;
271 GstElement *gdpdepay;
272 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
274 guint8 *caps_header, *caps_payload, *buf_header, *buf_payload;
275 guint header_len, payload_len;
277 GstStructure *structure;
278 GValue array = { 0 };
279 GValue value = { 0 };
281 gdpdepay = setup_gdpdepay_streamheader ();
282 srcpad = gst_element_get_pad (gdpdepay, "src");
283 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
285 fail_unless (gst_element_set_state (gdpdepay,
286 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
287 "could not set to playing");
289 /* make sure no caps are set yet */
290 caps = gst_pad_get_caps (srcpad);
291 fail_unless (gst_caps_is_any (caps));
292 gst_caps_unref (caps);
293 fail_if (gst_pad_get_negotiated_caps (srcpad));
295 /* create a streamheader buffer and the caps containing it */
296 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
297 structure = gst_caps_get_structure (caps, 0);
298 buffer = gst_buffer_new_and_alloc (4);
299 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
300 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
301 g_value_init (&array, GST_TYPE_ARRAY);
302 g_value_init (&value, GST_TYPE_BUFFER);
303 shbuffer = gst_buffer_copy (buffer);
304 gst_value_set_buffer (&value, shbuffer);
305 gst_buffer_unref (shbuffer);
306 gst_value_array_append_value (&array, &value);
307 g_value_unset (&value);
308 gst_structure_set_value (structure, "streamheader", &array);
309 g_value_unset (&array);
311 gst_buffer_set_caps (buffer, caps);
313 /* create GDP packets for the caps and the buffer, and put them in one
315 fail_unless (gst_dp_packet_from_caps (caps, 0, &header_len, &caps_header,
318 fail_unless (gst_dp_header_from_buffer (buffer, 0, &header_len, &buf_header));
320 payload_len = gst_dp_header_payload_length (caps_header);
322 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
323 payload_len + GST_BUFFER_SIZE (buffer));
324 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
325 i = GST_DP_HEADER_LENGTH;
326 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
328 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
329 i += GST_DP_HEADER_LENGTH;
330 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
331 GST_BUFFER_SIZE (buffer));
333 gst_caps_unref (caps);
334 gst_buffer_unref (buffer);
336 g_free (caps_header);
337 g_free (caps_payload);
341 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
342 gst_pad_push (mysrcpad, inbuffer);
344 /* our only output buffer is the streamheader buffer */
345 fail_unless_equals_int (g_list_length (buffers), 1);
346 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
347 buffers = g_list_remove (buffers, outbuffer);
348 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
349 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
351 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
352 caps = gst_buffer_get_caps (outbuffer);
353 fail_if (caps == NULL);
354 fail_if (padcaps == NULL);
355 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
357 fail_unless (gst_caps_is_equal (padcaps, caps));
359 /* FIXME: get streamheader, compare data with buffer */
360 gst_buffer_unref (outbuffer);
361 gst_caps_unref (padcaps);
362 gst_caps_unref (caps);
365 fail_unless (gst_element_set_state (gdpdepay,
366 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
368 gst_object_unref (srcpad);
369 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
370 g_list_free (buffers);
372 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
373 cleanup_gdpdepay (gdpdepay);
380 gdpdepay_suite (void)
382 Suite *s = suite_create ("gdpdepay");
383 TCase *tc_chain = tcase_create ("general");
385 suite_add_tcase (s, tc_chain);
386 tcase_add_test (tc_chain, test_audio_per_byte);
387 tcase_add_test (tc_chain, test_audio_in_one_buffer);
388 tcase_add_test (tc_chain, test_streamheader);
394 main (int argc, char **argv)
398 Suite *s = gdpdepay_suite ();
399 SRunner *sr = srunner_create (s);
401 gst_check_init (&argc, &argv);
403 srunner_run_all (sr, CK_NORMAL);
404 nf = srunner_ntests_failed (sr);