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 /* For ease of programming we use globals to keep refs for our floating
29 * src and sink pads we create; otherwise we always have to do get_pad,
30 * get_peer, and then remove references in every test function */
31 static GstPad *mysrcpad, *mysinkpad, *myshsinkpad;
33 #define AUDIO_CAPS_TEMPLATE_STRING \
35 "rate = (int) [ 1, MAX ], " \
36 "channels = (int) [ 1, 8 ], " \
37 "endianness = (int) BYTE_ORDER, " \
38 "width = (int) {8, 16}, " \
39 "depth = (int) {8, 16}, " \
40 "signed = (boolean) true"
42 #define AUDIO_CAPS_STRING \
44 "rate = (int) 1000, " \
45 "channels = (int) 2, " \
46 "endianness = (int) BYTE_ORDER, " \
47 "width = (int) 16, " \
48 "depth = (int) 16, " \
49 "signed = (boolean) true"
52 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_STATIC_CAPS (AUDIO_CAPS_TEMPLATE_STRING)
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
60 GST_STATIC_CAPS ("application/x-gdp")
63 /* takes over reference for outcaps */
69 GST_DEBUG ("setup_gdpdepay");
70 gdpdepay = gst_check_setup_element ("gdpdepay");
71 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
72 mysinkpad = gst_check_setup_sink_pad (gdpdepay, &sinktemplate, NULL);
73 gst_pad_set_active (mysrcpad, TRUE);
74 gst_pad_set_active (mysinkpad, TRUE);
80 cleanup_gdpdepay (GstElement * gdpdepay)
82 GST_DEBUG ("cleanup_gdpdepay");
84 gst_pad_set_active (mysrcpad, FALSE);
86 gst_pad_set_active (mysinkpad, FALSE);
88 gst_pad_set_active (myshsinkpad, FALSE);
89 gst_check_teardown_src_pad (gdpdepay);
90 gst_check_teardown_sink_pad (gdpdepay);
91 gst_check_teardown_element (gdpdepay);
97 gdpdepay_push_per_byte (const gchar * reason, guint8 * bytes, guint length)
102 for (i = 0; i < length; ++i) {
103 inbuffer = gst_buffer_new_and_alloc (1);
104 GST_BUFFER_DATA (inbuffer)[0] = bytes[i];
105 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
106 "%s: failed pushing byte buffer", reason);
110 GST_START_TEST (test_audio_per_byte)
114 GstElement *gdpdepay;
115 GstBuffer *buffer, *outbuffer;
116 guint8 *header, *payload;
120 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
122 gdpdepay = setup_gdpdepay ();
123 srcpad = gst_element_get_static_pad (gdpdepay, "src");
125 fail_unless (gst_element_set_state (gdpdepay,
126 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
127 "could not set to playing");
129 caps = gst_pad_get_caps (srcpad);
130 fail_unless (gst_caps_is_any (caps));
131 gst_caps_unref (caps);
132 fail_if (gst_pad_get_negotiated_caps (srcpad));
134 /* create caps and buffer packets and push them */
135 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
136 fail_unless (pk->packet_from_caps (caps, 0, &len, &header, &payload));
137 gst_caps_unref (caps);
138 gdpdepay_push_per_byte ("caps header", header, len);
139 fail_unless_equals_int (g_list_length (buffers), 0);
140 gdpdepay_push_per_byte ("caps payload", payload,
141 gst_dp_header_payload_length (header));
142 fail_unless_equals_int (g_list_length (buffers), 0);
143 caps = gst_pad_get_caps (srcpad);
144 fail_if (gst_caps_is_any (caps));
145 gst_caps_unref (caps);
150 buffer = gst_buffer_new_and_alloc (4);
151 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
152 GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
153 GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
154 fail_unless (pk->header_from_buffer (buffer, 0, &len, &header));
155 gdpdepay_push_per_byte ("buffer header", header, len);
156 fail_unless_equals_int (g_list_length (buffers), 0);
157 gdpdepay_push_per_byte ("buffer payload", GST_BUFFER_DATA (buffer),
158 gst_dp_header_payload_length (header));
160 gst_buffer_unref (buffer);
162 fail_unless_equals_int (g_list_length (buffers), 1);
163 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
164 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
165 fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
167 buffers = g_list_remove (buffers, outbuffer);
168 gst_buffer_unref (outbuffer);
170 fail_unless (gst_element_set_state (gdpdepay,
171 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
173 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
174 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
175 g_list_free (buffers);
177 gst_object_unref (srcpad);
178 cleanup_gdpdepay (gdpdepay);
180 gst_dp_packetizer_free (pk);
185 GST_START_TEST (test_audio_in_one_buffer)
189 GstElement *gdpdepay;
190 GstBuffer *buffer, *inbuffer;
191 guint8 *caps_header, *caps_payload, *buf_header;
192 guint header_len, payload_len;
196 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
198 gdpdepay = setup_gdpdepay ();
199 srcpad = gst_element_get_static_pad (gdpdepay, "src");
201 fail_unless (gst_element_set_state (gdpdepay,
202 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
203 "could not set to playing");
205 /* make sure no caps are set yet */
206 caps = gst_pad_get_caps (srcpad);
207 fail_unless (gst_caps_is_any (caps));
208 gst_caps_unref (caps);
209 fail_if (gst_pad_get_negotiated_caps (srcpad));
211 /* create caps and buffer packets and push them as one buffer */
212 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
213 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
216 buffer = gst_buffer_new_and_alloc (4);
217 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
218 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
220 payload_len = gst_dp_header_payload_length (caps_header);
222 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
223 payload_len + GST_BUFFER_SIZE (buffer));
224 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
225 i = GST_DP_HEADER_LENGTH;
226 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
228 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
229 i += GST_DP_HEADER_LENGTH;
230 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
231 GST_BUFFER_SIZE (buffer));
233 gst_caps_unref (caps);
234 gst_buffer_unref (buffer);
236 g_free (caps_header);
237 g_free (caps_payload);
241 gst_pad_push (mysrcpad, inbuffer);
243 /* the buffer is still queued */
244 fail_unless_equals_int (g_list_length (buffers), 1);
246 fail_unless (gst_element_set_state (gdpdepay,
247 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
249 gst_object_unref (srcpad);
250 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
251 g_list_free (buffers);
253 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
254 cleanup_gdpdepay (gdpdepay);
256 gst_dp_packetizer_free (pk);
261 static GstStaticPadTemplate shsinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
264 GST_STATIC_CAPS ("application/x-gst-test-streamheader")
268 setup_gdpdepay_streamheader (void)
270 GstElement *gdpdepay;
272 GST_DEBUG ("setup_gdpdepay");
273 gdpdepay = gst_check_setup_element ("gdpdepay");
274 mysrcpad = gst_check_setup_src_pad (gdpdepay, &srctemplate, NULL);
275 myshsinkpad = gst_check_setup_sink_pad (gdpdepay, &shsinktemplate, NULL);
276 gst_pad_set_active (mysrcpad, TRUE);
277 gst_pad_set_active (myshsinkpad, TRUE);
282 /* this tests deserialization of a GDP stream where the serialized caps
283 * have a streamheader set */
284 GST_START_TEST (test_streamheader)
286 GstCaps *caps, *padcaps;
288 GstElement *gdpdepay;
289 GstBuffer *buffer, *inbuffer, *outbuffer, *shbuffer;
290 guint8 *caps_header, *caps_payload, *buf_header;
291 guint header_len, payload_len;
293 GstStructure *structure;
294 GValue array = { 0 };
295 GValue value = { 0 };
298 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
300 gdpdepay = setup_gdpdepay_streamheader ();
301 srcpad = gst_element_get_static_pad (gdpdepay, "src");
302 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
304 fail_unless (gst_element_set_state (gdpdepay,
305 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
306 "could not set to playing");
308 /* make sure no caps are set yet */
309 caps = gst_pad_get_caps (srcpad);
310 fail_unless (gst_caps_is_any (caps));
311 gst_caps_unref (caps);
312 fail_if (gst_pad_get_negotiated_caps (srcpad));
314 /* create a streamheader buffer and the caps containing it */
315 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
316 structure = gst_caps_get_structure (caps, 0);
317 buffer = gst_buffer_new_and_alloc (4);
318 memcpy (GST_BUFFER_DATA (buffer), "f00d", 4);
319 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
320 g_value_init (&array, GST_TYPE_ARRAY);
321 g_value_init (&value, GST_TYPE_BUFFER);
322 shbuffer = gst_buffer_copy (buffer);
323 gst_value_set_buffer (&value, shbuffer);
324 gst_buffer_unref (shbuffer);
325 gst_value_array_append_value (&array, &value);
326 g_value_unset (&value);
327 gst_structure_set_value (structure, "streamheader", &array);
328 g_value_unset (&array);
330 gst_buffer_set_caps (buffer, caps);
332 /* create GDP packets for the caps and the buffer, and put them in one
334 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
337 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
339 payload_len = gst_dp_header_payload_length (caps_header);
341 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
342 payload_len + GST_BUFFER_SIZE (buffer));
343 memcpy (GST_BUFFER_DATA (inbuffer), caps_header, GST_DP_HEADER_LENGTH);
344 i = GST_DP_HEADER_LENGTH;
345 memcpy (GST_BUFFER_DATA (inbuffer) + i, caps_payload, payload_len);
347 memcpy (GST_BUFFER_DATA (inbuffer) + i, buf_header, GST_DP_HEADER_LENGTH);
348 i += GST_DP_HEADER_LENGTH;
349 memcpy (GST_BUFFER_DATA (inbuffer) + i, GST_BUFFER_DATA (buffer),
350 GST_BUFFER_SIZE (buffer));
352 gst_caps_unref (caps);
353 gst_buffer_unref (buffer);
355 g_free (caps_header);
356 g_free (caps_payload);
360 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
361 gst_pad_push (mysrcpad, inbuffer);
363 /* our only output buffer is the streamheader buffer */
364 fail_unless_equals_int (g_list_length (buffers), 1);
365 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
366 buffers = g_list_remove (buffers, outbuffer);
367 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
368 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
370 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
371 caps = gst_buffer_get_caps (outbuffer);
372 fail_if (caps == NULL);
373 fail_if (padcaps == NULL);
374 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
376 fail_unless (gst_caps_is_equal (padcaps, caps));
378 /* FIXME: get streamheader, compare data with buffer */
379 gst_buffer_unref (outbuffer);
380 gst_caps_unref (padcaps);
381 gst_caps_unref (caps);
384 fail_unless (gst_element_set_state (gdpdepay,
385 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
387 gst_object_unref (srcpad);
388 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
389 g_list_free (buffers);
391 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
392 cleanup_gdpdepay (gdpdepay);
394 gst_dp_packetizer_free (pk);
400 gdpdepay_suite (void)
402 Suite *s = suite_create ("gdpdepay");
403 TCase *tc_chain = tcase_create ("general");
405 suite_add_tcase (s, tc_chain);
406 tcase_add_test (tc_chain, test_audio_per_byte);
407 tcase_add_test (tc_chain, test_audio_in_one_buffer);
408 tcase_add_test (tc_chain, test_streamheader);
413 GST_CHECK_MAIN (gdpdepay);