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, const guint8 * bytes,
103 for (i = 0; i < length; ++i) {
104 inbuffer = gst_buffer_new_and_alloc (1);
105 gst_buffer_fill (inbuffer, 0, &bytes[i], 1);
106 fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK,
107 "%s: failed pushing byte buffer", reason);
111 GST_START_TEST (test_audio_per_byte)
115 GstElement *gdpdepay;
116 GstBuffer *buffer, *outbuffer;
117 guint8 *header, *payload;
121 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
123 gdpdepay = setup_gdpdepay ();
124 srcpad = gst_element_get_static_pad (gdpdepay, "src");
126 fail_unless (gst_element_set_state (gdpdepay,
127 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
128 "could not set to playing");
130 caps = gst_pad_get_caps (srcpad);
131 fail_unless (gst_caps_is_any (caps));
132 gst_caps_unref (caps);
133 fail_if (gst_pad_get_negotiated_caps (srcpad));
135 /* create caps and buffer packets and push them */
136 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
137 fail_unless (pk->packet_from_caps (caps, 0, &len, &header, &payload));
138 gst_caps_unref (caps);
139 gdpdepay_push_per_byte ("caps header", header, len);
140 fail_unless_equals_int (g_list_length (buffers), 0);
141 gdpdepay_push_per_byte ("caps payload", payload,
142 gst_dp_header_payload_length (header));
143 fail_unless_equals_int (g_list_length (buffers), 0);
144 caps = gst_pad_get_caps (srcpad);
145 fail_if (gst_caps_is_any (caps));
146 gst_caps_unref (caps);
151 buffer = gst_buffer_new_and_alloc (4);
152 gst_buffer_fill (buffer, 0, "f00d", 4);
153 GST_BUFFER_TIMESTAMP (buffer) = GST_SECOND;
154 GST_BUFFER_DURATION (buffer) = GST_SECOND / 10;
155 fail_unless (pk->header_from_buffer (buffer, 0, &len, &header));
156 gdpdepay_push_per_byte ("buffer header", header, len);
157 fail_unless_equals_int (g_list_length (buffers), 0);
158 gdpdepay_push_per_byte ("buffer payload", (const guint8 *) "f00d",
159 gst_dp_header_payload_length (header));
161 gst_buffer_unref (buffer);
163 fail_unless_equals_int (g_list_length (buffers), 1);
164 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
165 fail_unless_equals_uint64 (GST_BUFFER_TIMESTAMP (outbuffer), GST_SECOND);
166 fail_unless_equals_uint64 (GST_BUFFER_DURATION (outbuffer), GST_SECOND / 10);
168 buffers = g_list_remove (buffers, outbuffer);
169 gst_buffer_unref (outbuffer);
171 fail_unless (gst_element_set_state (gdpdepay,
172 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
174 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
175 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
176 g_list_free (buffers);
178 gst_object_unref (srcpad);
179 cleanup_gdpdepay (gdpdepay);
181 gst_dp_packetizer_free (pk);
186 GST_START_TEST (test_audio_in_one_buffer)
190 GstElement *gdpdepay;
191 GstBuffer *buffer, *inbuffer;
192 guint8 *caps_header, *caps_payload, *buf_header;
193 guint header_len, payload_len;
197 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
199 gdpdepay = setup_gdpdepay ();
200 srcpad = gst_element_get_static_pad (gdpdepay, "src");
202 fail_unless (gst_element_set_state (gdpdepay,
203 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
204 "could not set to playing");
206 /* make sure no caps are set yet */
207 caps = gst_pad_get_caps (srcpad);
208 fail_unless (gst_caps_is_any (caps));
209 gst_caps_unref (caps);
210 fail_if (gst_pad_get_negotiated_caps (srcpad));
212 /* create caps and buffer packets and push them as one buffer */
213 caps = gst_caps_from_string (AUDIO_CAPS_STRING);
214 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
217 buffer = gst_buffer_new_and_alloc (4);
218 gst_buffer_fill (buffer, 0, "f00d", 4);
219 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
221 payload_len = gst_dp_header_payload_length (caps_header);
223 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
224 payload_len + gst_buffer_get_size (buffer));
225 gst_buffer_fill (inbuffer, 0, caps_header, GST_DP_HEADER_LENGTH);
226 i = GST_DP_HEADER_LENGTH;
227 gst_buffer_fill (inbuffer, i, caps_payload, payload_len);
229 gst_buffer_fill (inbuffer, i, buf_header, GST_DP_HEADER_LENGTH);
230 i += GST_DP_HEADER_LENGTH;
231 gst_buffer_fill (inbuffer, i, "f00d", 4);
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, *data;
292 guint header_len, payload_len;
294 GstStructure *structure;
295 GValue array = { 0 };
296 GValue value = { 0 };
299 pk = gst_dp_packetizer_new (GST_DP_VERSION_1_0);
301 gdpdepay = setup_gdpdepay_streamheader ();
302 srcpad = gst_element_get_static_pad (gdpdepay, "src");
303 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
305 fail_unless (gst_element_set_state (gdpdepay,
306 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS,
307 "could not set to playing");
309 /* make sure no caps are set yet */
310 caps = gst_pad_get_caps (srcpad);
311 fail_unless (gst_caps_is_any (caps));
312 gst_caps_unref (caps);
313 fail_if (gst_pad_get_negotiated_caps (srcpad));
315 /* create a streamheader buffer and the caps containing it */
316 caps = gst_caps_from_string ("application/x-gst-test-streamheader");
317 structure = gst_caps_get_structure (caps, 0);
318 buffer = gst_buffer_new_and_alloc (4);
319 gst_buffer_fill (buffer, 0, "f00d", 4);
320 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_IN_CAPS);
321 g_value_init (&array, GST_TYPE_ARRAY);
322 g_value_init (&value, GST_TYPE_BUFFER);
323 shbuffer = gst_buffer_copy (buffer);
324 gst_value_set_buffer (&value, shbuffer);
325 gst_buffer_unref (shbuffer);
326 gst_value_array_append_value (&array, &value);
327 g_value_unset (&value);
328 gst_structure_set_value (structure, "streamheader", &array);
329 g_value_unset (&array);
331 gst_buffer_set_caps (buffer, caps);
333 /* create GDP packets for the caps and the buffer, and put them in one
335 fail_unless (pk->packet_from_caps (caps, 0, &header_len, &caps_header,
338 fail_unless (pk->header_from_buffer (buffer, 0, &header_len, &buf_header));
340 payload_len = gst_dp_header_payload_length (caps_header);
342 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
343 inbuffer = gst_buffer_new_and_alloc (2 * GST_DP_HEADER_LENGTH +
345 gst_buffer_fill (inbuffer, 0, caps_header, GST_DP_HEADER_LENGTH);
346 i = GST_DP_HEADER_LENGTH;
347 gst_buffer_fill (inbuffer, i, caps_payload, payload_len);
349 gst_buffer_fill (inbuffer, i, buf_header, GST_DP_HEADER_LENGTH);
350 i += GST_DP_HEADER_LENGTH;
351 gst_buffer_fill (inbuffer, i, data, size);
352 gst_buffer_unmap (buffer, data, size);
354 gst_caps_unref (caps);
355 gst_buffer_unref (buffer);
357 g_free (caps_header);
358 g_free (caps_payload);
362 ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1);
363 gst_pad_push (mysrcpad, inbuffer);
365 /* our only output buffer is the streamheader buffer */
366 fail_unless_equals_int (g_list_length (buffers), 1);
367 fail_if ((outbuffer = (GstBuffer *) buffers->data) == NULL);
368 buffers = g_list_remove (buffers, outbuffer);
369 ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1);
370 fail_unless (GST_BUFFER_FLAG_IS_SET (outbuffer, GST_BUFFER_FLAG_IN_CAPS));
372 padcaps = gst_pad_get_negotiated_caps (myshsinkpad);
373 caps = gst_buffer_get_caps (outbuffer);
374 fail_if (caps == NULL);
375 fail_if (padcaps == NULL);
376 GST_DEBUG ("caps: %" GST_PTR_FORMAT ", padcaps: %" GST_PTR_FORMAT, caps,
378 fail_unless (gst_caps_is_equal (padcaps, caps));
380 /* FIXME: get streamheader, compare data with buffer */
381 gst_buffer_unref (outbuffer);
382 gst_caps_unref (padcaps);
383 gst_caps_unref (caps);
386 fail_unless (gst_element_set_state (gdpdepay,
387 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS, "could not set to null");
389 gst_object_unref (srcpad);
390 g_list_foreach (buffers, (GFunc) gst_mini_object_unref, NULL);
391 g_list_free (buffers);
393 ASSERT_OBJECT_REFCOUNT (gdpdepay, "gdpdepay", 1);
394 cleanup_gdpdepay (gdpdepay);
396 gst_dp_packetizer_free (pk);
402 gdpdepay_suite (void)
404 Suite *s = suite_create ("gdpdepay");
405 TCase *tc_chain = tcase_create ("general");
407 suite_add_tcase (s, tc_chain);
408 tcase_add_test (tc_chain, test_audio_per_byte);
409 tcase_add_test (tc_chain, test_audio_in_one_buffer);
410 tcase_add_test (tc_chain, test_streamheader);
415 GST_CHECK_MAIN (gdpdepay);