3 * unit test for rtpmux elements
5 * Copyright 2009 Collabora Ltd.
6 * @author: Olivier Crete <olivier.crete@collabora.co.uk>
7 * Copyright 2009 Nokia Corp.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 #include <gst/check/gstcheck.h>
26 #include <gst/rtp/gstrtpbuffer.h>
29 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
32 GST_STATIC_CAPS ("application/x-rtp"));
34 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
37 GST_STATIC_CAPS ("application/x-rtp"));
39 typedef void (*check_cb) (GstPad * pad, int i);
42 query_func (GstPad * pad, GstObject * noparent, GstQuery * query)
44 switch (GST_QUERY_TYPE (query)) {
47 GstCaps **caps = g_object_get_data (G_OBJECT (pad), "caps");
49 fail_unless (caps != NULL && *caps != NULL);
50 gst_query_set_caps_result (query, *caps);
53 case GST_QUERY_ACCEPT_CAPS:
54 gst_query_set_accept_caps_result (query, TRUE);
64 event_func (GstPad * pad, GstObject * noparent, GstEvent * event)
66 switch (GST_EVENT_TYPE (event)) {
70 GstCaps **caps2 = g_object_get_data (G_OBJECT (pad), "caps");
72 gst_event_parse_caps (event, &caps);
73 fail_unless (caps2 != NULL && *caps2 != NULL);
74 fail_unless (gst_caps_is_fixed (caps));
75 fail_unless (gst_caps_is_fixed (*caps2));
76 fail_unless (gst_caps_is_equal_fixed (caps, *caps2));
83 gst_event_unref (event);
89 test_basic (const gchar * elem_name, const gchar * sink2, int count,
92 GstElement *rtpmux = NULL;
93 GstPad *reqpad1 = NULL;
94 GstPad *reqpad2 = NULL;
98 GstBuffer *inbuf = NULL;
99 GstCaps *src1caps = NULL;
100 GstCaps *src2caps = NULL;
101 GstCaps *sinkcaps = NULL;
106 rtpmux = gst_check_setup_element (elem_name);
108 reqpad1 = gst_element_get_request_pad (rtpmux, "sink_1");
109 fail_unless (reqpad1 != NULL);
110 reqpad2 = gst_element_get_request_pad (rtpmux, sink2);
111 fail_unless (reqpad2 != NULL);
112 sink = gst_check_setup_sink_pad_by_name (rtpmux, &sinktemplate, "src");
114 src1 = gst_pad_new_from_static_template (&srctemplate, "src");
115 src2 = gst_pad_new_from_static_template (&srctemplate, "src");
116 fail_unless (gst_pad_link (src1, reqpad1) == GST_PAD_LINK_OK);
117 fail_unless (gst_pad_link (src2, reqpad2) == GST_PAD_LINK_OK);
118 gst_pad_set_query_function (src1, query_func);
119 gst_pad_set_query_function (src2, query_func);
120 gst_pad_set_query_function (sink, query_func);
121 gst_pad_set_event_function (sink, event_func);
122 g_object_set_data (G_OBJECT (src1), "caps", &src1caps);
123 g_object_set_data (G_OBJECT (src2), "caps", &src2caps);
124 g_object_set_data (G_OBJECT (sink), "caps", &sinkcaps);
126 src1caps = gst_caps_new_simple ("application/x-rtp",
127 "clock-rate", G_TYPE_INT, 1, "ssrc", G_TYPE_UINT, 11, NULL);
128 src2caps = gst_caps_new_simple ("application/x-rtp",
129 "clock-rate", G_TYPE_INT, 2, "ssrc", G_TYPE_UINT, 12, NULL);
130 sinkcaps = gst_caps_new_simple ("application/x-rtp",
131 "clock-rate", G_TYPE_INT, 3, "ssrc", G_TYPE_UINT, 13, NULL);
133 caps = gst_pad_peer_query_caps (src1, NULL);
134 fail_unless (gst_caps_is_empty (caps));
135 gst_caps_unref (caps);
137 gst_caps_set_simple (src2caps, "clock-rate", G_TYPE_INT, 3, NULL);
138 caps = gst_pad_peer_query_caps (src1, NULL);
139 fail_unless (gst_caps_is_equal (caps, sinkcaps));
140 gst_caps_unref (caps);
142 g_object_set (rtpmux, "seqnum-offset", 100, "timestamp-offset", 1000,
145 fail_unless (gst_element_set_state (rtpmux,
146 GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
147 gst_pad_set_active (sink, TRUE);
148 gst_pad_set_active (src1, TRUE);
149 gst_pad_set_active (src2, TRUE);
151 gst_caps_set_simple (sinkcaps,
152 "payload", G_TYPE_INT, 98, "seqnum-base", G_TYPE_UINT, 100,
153 "clock-base", G_TYPE_UINT, 1000, "ssrc", G_TYPE_UINT, 66, NULL);
154 caps = gst_caps_new_simple ("application/x-rtp",
155 "payload", G_TYPE_INT, 98, "clock-rate", G_TYPE_INT, 3,
156 "seqnum-base", G_TYPE_UINT, 56, "clock-base", G_TYPE_UINT, 57,
157 "ssrc", G_TYPE_UINT, 66, NULL);
158 fail_unless (gst_pad_set_caps (src1, caps));
160 gst_segment_init (&segment, GST_FORMAT_TIME);
161 segment.start = 100000;
162 fail_unless (gst_pad_push_event (src1, gst_event_new_segment (&segment)));
164 fail_unless (gst_pad_push_event (src2, gst_event_new_segment (&segment)));
167 for (i = 0; i < count; i++) {
168 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
170 inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
171 GST_BUFFER_PTS (inbuf) = i * 1000 + 100000;
172 GST_BUFFER_DURATION (inbuf) = 1000;
174 gst_rtp_buffer_map (inbuf, GST_MAP_WRITE, &rtpbuffer);
176 gst_rtp_buffer_set_version (&rtpbuffer, 2);
177 gst_rtp_buffer_set_payload_type (&rtpbuffer, 98);
178 gst_rtp_buffer_set_ssrc (&rtpbuffer, 44);
179 gst_rtp_buffer_set_timestamp (&rtpbuffer, 200 + i);
180 gst_rtp_buffer_set_seq (&rtpbuffer, 2000 + i);
181 gst_rtp_buffer_unmap (&rtpbuffer);
182 fail_unless (gst_pad_push (src1, inbuf) == GST_FLOW_OK);
185 fail_unless (GST_BUFFER_PTS (buffers->data) == i * 1000, "%lld",
186 GST_BUFFER_PTS (buffers->data));
190 g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
191 g_list_free (buffers);
196 gst_pad_set_active (sink, FALSE);
197 gst_pad_set_active (src1, FALSE);
198 gst_pad_set_active (src2, FALSE);
199 fail_unless (gst_element_set_state (rtpmux,
200 GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
201 gst_check_teardown_pad_by_name (rtpmux, "src");
202 gst_object_unref (reqpad1);
203 gst_object_unref (reqpad2);
204 gst_check_teardown_pad_by_name (rtpmux, "sink_1");
205 gst_check_teardown_pad_by_name (rtpmux, sink2);
206 gst_element_release_request_pad (rtpmux, reqpad1);
207 gst_element_release_request_pad (rtpmux, reqpad2);
209 gst_caps_unref (caps);
210 gst_caps_replace (&src1caps, NULL);
211 gst_caps_replace (&src2caps, NULL);
212 gst_caps_replace (&sinkcaps, NULL);
214 gst_check_teardown_element (rtpmux);
218 basic_check_cb (GstPad * pad, int i)
220 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
221 fail_unless (buffers && g_list_length (buffers) == 1);
223 gst_rtp_buffer_map (buffers->data, GST_MAP_READ, &rtpbuffer);
224 fail_unless (gst_rtp_buffer_get_ssrc (&rtpbuffer) == 55);
225 fail_unless (gst_rtp_buffer_get_timestamp (&rtpbuffer) ==
226 200 - 57 + 1000 + i);
227 fail_unless (gst_rtp_buffer_get_seq (&rtpbuffer) == 100 + 1 + i);
228 gst_rtp_buffer_unmap (&rtpbuffer);
232 GST_START_TEST (test_rtpmux_basic)
234 test_basic ("rtpmux", "sink_2", 10, basic_check_cb);
239 GST_START_TEST (test_rtpdtmfmux_basic)
241 test_basic ("rtpdtmfmux", "sink_2", 10, basic_check_cb);
247 lock_check_cb (GstPad * pad, int i)
252 fail_unless (buffers == NULL);
254 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
256 fail_unless (buffers && g_list_length (buffers) == 1);
257 gst_rtp_buffer_map (buffers->data, GST_MAP_READ, &rtpbuffer);
258 fail_unless (gst_rtp_buffer_get_ssrc (&rtpbuffer) == 55);
259 fail_unless (gst_rtp_buffer_get_timestamp (&rtpbuffer) ==
260 200 - 57 + 1000 + i);
261 fail_unless (gst_rtp_buffer_get_seq (&rtpbuffer) == 100 + 1 + i);
262 gst_rtp_buffer_unmap (&rtpbuffer);
264 inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
265 GST_BUFFER_PTS (inbuf) = i * 1000 + 500;
266 GST_BUFFER_DURATION (inbuf) = 1000;
267 gst_rtp_buffer_map (inbuf, GST_MAP_WRITE, &rtpbuffer);
268 gst_rtp_buffer_set_version (&rtpbuffer, 2);
269 gst_rtp_buffer_set_payload_type (&rtpbuffer, 98);
270 gst_rtp_buffer_set_ssrc (&rtpbuffer, 44);
271 gst_rtp_buffer_set_timestamp (&rtpbuffer, 200 + i);
272 gst_rtp_buffer_set_seq (&rtpbuffer, 2000 + i);
273 gst_rtp_buffer_unmap (&rtpbuffer);
274 fail_unless (gst_pad_push (pad, inbuf) == GST_FLOW_OK);
277 g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
278 g_list_free (buffers);
283 GST_START_TEST (test_rtpdtmfmux_lock)
285 test_basic ("rtpdtmfmux", "priority_sink_2", 10, lock_check_cb);
293 Suite *s = suite_create ("rtpmux");
296 tc_chain = tcase_create ("rtpmux_basic");
297 tcase_add_test (tc_chain, test_rtpmux_basic);
298 suite_add_tcase (s, tc_chain);
300 tc_chain = tcase_create ("rtpdtmfmux_basic");
301 tcase_add_test (tc_chain, test_rtpdtmfmux_basic);
302 suite_add_tcase (s, tc_chain);
304 tc_chain = tcase_create ("rtpdtmfmux_lock");
305 tcase_add_test (tc_chain, test_rtpdtmfmux_lock);
306 suite_add_tcase (s, tc_chain);
311 GST_CHECK_MAIN (rtpmux)