Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / tests / check / elements / rtpmux.c
1 /* GStreamer
2  *
3  * unit test for rtpmux elements
4  *
5  * Copyright 2009 Collabora Ltd.
6  *  @author: Olivier Crete <olivier.crete@collabora.co.uk>
7  * Copyright 2009 Nokia Corp.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include <gst/check/gstcheck.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/gst.h>
28
29 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
30     GST_PAD_SINK,
31     GST_PAD_ALWAYS,
32     GST_STATIC_CAPS ("application/x-rtp"));
33
34 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
35     GST_PAD_SRC,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("application/x-rtp"));
38
39 typedef void (*check_cb) (GstPad * pad, int i);
40
41 static GstCaps *
42 getcaps_func (GstPad * pad)
43 {
44   GstCaps **caps = g_object_get_data (G_OBJECT (pad), "caps");
45
46   fail_unless (caps != NULL && *caps != NULL);
47
48   return gst_caps_ref (*caps);
49 }
50
51 static gboolean
52 setcaps_func (GstPad * pad, GstCaps * caps)
53 {
54   GstCaps **caps2 = g_object_get_data (G_OBJECT (pad), "caps");
55
56   fail_unless (caps2 != NULL && *caps2 != NULL);
57
58   fail_unless (gst_caps_is_equal (caps, *caps2));
59
60   return TRUE;
61 }
62
63 static gboolean
64 event_func (GstPad * pad, GstEvent * event)
65 {
66   gst_event_unref (event);
67
68   return TRUE;
69 }
70
71 static void
72 test_basic (const gchar * elem_name, const gchar * sink2, int count,
73     check_cb cb)
74 {
75   GstElement *rtpmux = NULL;
76   GstPad *reqpad1 = NULL;
77   GstPad *reqpad2 = NULL;
78   GstPad *src1 = NULL;
79   GstPad *src2 = NULL;
80   GstPad *sink = NULL;
81   GstBuffer *inbuf = NULL;
82   GstCaps *src1caps = NULL;
83   GstCaps *src2caps = NULL;
84   GstCaps *sinkcaps = NULL;
85   GstCaps *caps;
86   GstEvent *newsegment;
87   int i;
88
89   rtpmux = gst_check_setup_element (elem_name);
90
91   reqpad1 = gst_element_get_request_pad (rtpmux, "sink_1");
92   fail_unless (reqpad1 != NULL);
93   reqpad2 = gst_element_get_request_pad (rtpmux, sink2);
94   fail_unless (reqpad2 != NULL);
95   sink = gst_check_setup_sink_pad_by_name (rtpmux, &sinktemplate, "src");
96
97   src1 = gst_pad_new_from_static_template (&srctemplate, "src");
98   src2 = gst_pad_new_from_static_template (&srctemplate, "src");
99   fail_unless (gst_pad_link (src1, reqpad1) == GST_PAD_LINK_OK);
100   fail_unless (gst_pad_link (src2, reqpad2) == GST_PAD_LINK_OK);
101   gst_pad_set_getcaps_function (src1, getcaps_func);
102   gst_pad_set_getcaps_function (src2, getcaps_func);
103   gst_pad_set_getcaps_function (sink, getcaps_func);
104   gst_pad_set_setcaps_function (sink, setcaps_func);
105   gst_pad_set_event_function (sink, event_func);
106   g_object_set_data (G_OBJECT (src1), "caps", &src1caps);
107   g_object_set_data (G_OBJECT (src2), "caps", &src2caps);
108   g_object_set_data (G_OBJECT (sink), "caps", &sinkcaps);
109
110   src1caps = gst_caps_new_simple ("application/x-rtp",
111       "clock-rate", G_TYPE_INT, 1, "ssrc", G_TYPE_UINT, 11, NULL);
112   src2caps = gst_caps_new_simple ("application/x-rtp",
113       "clock-rate", G_TYPE_INT, 2, "ssrc", G_TYPE_UINT, 12, NULL);
114   sinkcaps = gst_caps_new_simple ("application/x-rtp",
115       "clock-rate", G_TYPE_INT, 3, "ssrc", G_TYPE_UINT, 13, NULL);
116
117   caps = gst_pad_peer_get_caps (src1);
118   fail_unless (gst_caps_is_empty (caps));
119   gst_caps_unref (caps);
120
121   gst_caps_set_simple (src2caps, "clock-rate", G_TYPE_INT, 3, NULL);
122   caps = gst_pad_peer_get_caps (src1);
123   fail_unless (gst_caps_is_equal (caps, sinkcaps));
124   gst_caps_unref (caps);
125
126   g_object_set (rtpmux, "seqnum-offset", 100, "timestamp-offset", 1000,
127       "ssrc", 55, NULL);
128
129   fail_unless (gst_element_set_state (rtpmux,
130           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
131   gst_pad_set_active (sink, TRUE);
132   gst_pad_set_active (src1, TRUE);
133   gst_pad_set_active (src2, TRUE);
134
135   gst_caps_set_simple (sinkcaps,
136       "payload", G_TYPE_INT, 98, "seqnum-base", G_TYPE_UINT, 100,
137       "clock-base", G_TYPE_UINT, 1000, "ssrc", G_TYPE_UINT, 66, NULL);
138   caps = gst_caps_new_simple ("application/x-rtp",
139       "payload", G_TYPE_INT, 98, "clock-rate", G_TYPE_INT, 3,
140       "seqnum-base", G_TYPE_UINT, 56, "clock-base", G_TYPE_UINT, 57,
141       "ssrc", G_TYPE_UINT, 66, NULL);
142   fail_unless (gst_pad_set_caps (src1, caps));
143
144   newsegment = gst_event_new_new_segment (FALSE, 1, GST_FORMAT_TIME, 100000,
145       -1, 0);
146   fail_unless (gst_pad_push_event (src1, newsegment));
147   newsegment = gst_event_new_new_segment (FALSE, 1, GST_FORMAT_TIME, 0, -1, 0);
148   fail_unless (gst_pad_push_event (src2, newsegment));
149
150   for (i = 0; i < count; i++) {
151     inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
152     GST_BUFFER_TIMESTAMP (inbuf) = i * 1000 + 100000;
153     GST_BUFFER_DURATION (inbuf) = 1000;
154     gst_buffer_set_caps (inbuf, caps);
155     gst_rtp_buffer_set_version (inbuf, 2);
156     gst_rtp_buffer_set_payload_type (inbuf, 98);
157     gst_rtp_buffer_set_ssrc (inbuf, 44);
158     gst_rtp_buffer_set_timestamp (inbuf, 200 + i);
159     gst_rtp_buffer_set_seq (inbuf, 2000 + i);
160     fail_unless (gst_pad_push (src1, inbuf) == GST_FLOW_OK);
161
162     if (buffers)
163       fail_unless (GST_BUFFER_TIMESTAMP (buffers->data) == i * 1000, "%lld",
164           GST_BUFFER_TIMESTAMP (buffers->data));
165
166     cb (src2, i);
167
168     g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
169     g_list_free (buffers);
170     buffers = NULL;
171   }
172
173
174   gst_pad_set_active (sink, FALSE);
175   gst_pad_set_active (src1, FALSE);
176   gst_pad_set_active (src2, FALSE);
177   fail_unless (gst_element_set_state (rtpmux,
178           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
179   gst_check_teardown_pad_by_name (rtpmux, "src");
180   gst_object_unref (reqpad1);
181   gst_object_unref (reqpad2);
182   gst_check_teardown_pad_by_name (rtpmux, "sink_1");
183   gst_check_teardown_pad_by_name (rtpmux, sink2);
184   gst_element_release_request_pad (rtpmux, reqpad1);
185   gst_element_release_request_pad (rtpmux, reqpad2);
186
187   gst_caps_unref (caps);
188   gst_caps_replace (&src1caps, NULL);
189   gst_caps_replace (&src2caps, NULL);
190   gst_caps_replace (&sinkcaps, NULL);
191
192   gst_check_teardown_element (rtpmux);
193 }
194
195 static void
196 basic_check_cb (GstPad * pad, int i)
197 {
198   fail_unless (buffers && g_list_length (buffers) == 1);
199   fail_unless (gst_rtp_buffer_get_ssrc (buffers->data) == 55);
200   fail_unless (gst_rtp_buffer_get_timestamp (buffers->data) ==
201       200 - 57 + 1000 + i);
202   fail_unless (gst_rtp_buffer_get_seq (buffers->data) == 100 + 1 + i);
203 }
204
205
206 GST_START_TEST (test_rtpmux_basic)
207 {
208   test_basic ("rtpmux", "sink_2", 10, basic_check_cb);
209 }
210
211 GST_END_TEST;
212
213 GST_START_TEST (test_rtpdtmfmux_basic)
214 {
215   test_basic ("rtpdtmfmux", "sink_2", 10, basic_check_cb);
216 }
217
218 GST_END_TEST;
219
220 static void
221 lock_check_cb (GstPad * pad, int i)
222 {
223   GstBuffer *inbuf;
224
225   if (i % 2) {
226     fail_unless (buffers == NULL);
227   } else {
228     fail_unless (buffers && g_list_length (buffers) == 1);
229     fail_unless (gst_rtp_buffer_get_ssrc (buffers->data) == 55);
230     fail_unless (gst_rtp_buffer_get_timestamp (buffers->data) ==
231         200 - 57 + 1000 + i);
232     fail_unless (gst_rtp_buffer_get_seq (buffers->data) == 100 + 1 + i);
233
234     inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
235     GST_BUFFER_TIMESTAMP (inbuf) = i * 1000 + 500;
236     GST_BUFFER_DURATION (inbuf) = 1000;
237     gst_rtp_buffer_set_version (inbuf, 2);
238     gst_rtp_buffer_set_payload_type (inbuf, 98);
239     gst_rtp_buffer_set_ssrc (inbuf, 44);
240     gst_rtp_buffer_set_timestamp (inbuf, 200 + i);
241     gst_rtp_buffer_set_seq (inbuf, 2000 + i);
242     fail_unless (gst_pad_push (pad, inbuf) == GST_FLOW_OK);
243
244
245     g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
246     g_list_free (buffers);
247     buffers = NULL;
248   }
249 }
250
251 GST_START_TEST (test_rtpdtmfmux_lock)
252 {
253   test_basic ("rtpdtmfmux", "priority_sink_2", 10, lock_check_cb);
254 }
255
256 GST_END_TEST;
257
258 static Suite *
259 rtpmux_suite (void)
260 {
261   Suite *s = suite_create ("rtpmux");
262   TCase *tc_chain;
263
264   tc_chain = tcase_create ("rtpmux_basic");
265   tcase_add_test (tc_chain, test_rtpmux_basic);
266   suite_add_tcase (s, tc_chain);
267
268   tc_chain = tcase_create ("rtpdtmfmux_basic");
269   tcase_add_test (tc_chain, test_rtpdtmfmux_basic);
270   suite_add_tcase (s, tc_chain);
271
272   tc_chain = tcase_create ("rtpdtmfmux_lock");
273   tcase_add_test (tc_chain, test_rtpdtmfmux_lock);
274   suite_add_tcase (s, tc_chain);
275
276   return s;
277 }
278
279 GST_CHECK_MAIN (rtpmux)