rtpmux: port to 0.11
[platform/upstream/gstreamer.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 gboolean
42 query_func (GstPad * pad, GstObject * noparent, GstQuery * query)
43 {
44   switch (GST_QUERY_TYPE (query)) {
45     case GST_QUERY_CAPS:
46     {
47       GstCaps **caps = g_object_get_data (G_OBJECT (pad), "caps");
48
49       fail_unless (caps != NULL && *caps != NULL);
50       gst_query_set_caps_result (query, *caps);
51       break;
52     }
53     default:
54       break;
55   }
56
57   return TRUE;
58 }
59
60 static gboolean
61 event_func (GstPad * pad, GstObject * noparent, GstEvent * event)
62 {
63   switch (GST_EVENT_TYPE (event)) {
64     case GST_EVENT_CAPS:
65     {
66       GstCaps *caps;
67       GstCaps **caps2 = g_object_get_data (G_OBJECT (pad), "caps");
68
69       gst_event_parse_caps (event, &caps);
70       fail_unless (caps2 != NULL && *caps2 != NULL);
71       fail_unless (gst_caps_is_equal (caps, *caps2));
72       break;
73     }
74     default:
75       gst_event_unref (event);
76       break;
77   }
78
79   return TRUE;
80 }
81
82 static void
83 test_basic (const gchar * elem_name, const gchar * sink2, int count,
84     check_cb cb)
85 {
86   GstElement *rtpmux = NULL;
87   GstPad *reqpad1 = NULL;
88   GstPad *reqpad2 = NULL;
89   GstPad *src1 = NULL;
90   GstPad *src2 = NULL;
91   GstPad *sink = NULL;
92   GstBuffer *inbuf = NULL;
93   GstCaps *src1caps = NULL;
94   GstCaps *src2caps = NULL;
95   GstCaps *sinkcaps = NULL;
96   GstCaps *caps;
97   GstSegment segment;
98   int i;
99
100   rtpmux = gst_check_setup_element (elem_name);
101
102   reqpad1 = gst_element_get_request_pad (rtpmux, "sink_1");
103   fail_unless (reqpad1 != NULL);
104   reqpad2 = gst_element_get_request_pad (rtpmux, sink2);
105   fail_unless (reqpad2 != NULL);
106   sink = gst_check_setup_sink_pad_by_name (rtpmux, &sinktemplate, "src");
107
108   src1 = gst_pad_new_from_static_template (&srctemplate, "src");
109   src2 = gst_pad_new_from_static_template (&srctemplate, "src");
110   fail_unless (gst_pad_link (src1, reqpad1) == GST_PAD_LINK_OK);
111   fail_unless (gst_pad_link (src2, reqpad2) == GST_PAD_LINK_OK);
112   gst_pad_set_query_function (src1, query_func);
113   gst_pad_set_query_function (src2, query_func);
114   gst_pad_set_query_function (sink, query_func);
115   gst_pad_set_event_function (sink, event_func);
116   g_object_set_data (G_OBJECT (src1), "caps", &src1caps);
117   g_object_set_data (G_OBJECT (src2), "caps", &src2caps);
118   g_object_set_data (G_OBJECT (sink), "caps", &sinkcaps);
119
120   src1caps = gst_caps_new_simple ("application/x-rtp",
121       "clock-rate", G_TYPE_INT, 1, "ssrc", G_TYPE_UINT, 11, NULL);
122   src2caps = gst_caps_new_simple ("application/x-rtp",
123       "clock-rate", G_TYPE_INT, 2, "ssrc", G_TYPE_UINT, 12, NULL);
124   sinkcaps = gst_caps_new_simple ("application/x-rtp",
125       "clock-rate", G_TYPE_INT, 3, "ssrc", G_TYPE_UINT, 13, NULL);
126
127   caps = gst_pad_peer_query_caps (src1, NULL);
128   fail_unless (gst_caps_is_empty (caps));
129   gst_caps_unref (caps);
130
131   gst_caps_set_simple (src2caps, "clock-rate", G_TYPE_INT, 3, NULL);
132   caps = gst_pad_peer_query_caps (src1, NULL);
133   fail_unless (gst_caps_is_equal (caps, sinkcaps));
134   gst_caps_unref (caps);
135
136   g_object_set (rtpmux, "seqnum-offset", 100, "timestamp-offset", 1000,
137       "ssrc", 55, NULL);
138
139   fail_unless (gst_element_set_state (rtpmux,
140           GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS);
141   gst_pad_set_active (sink, TRUE);
142   gst_pad_set_active (src1, TRUE);
143   gst_pad_set_active (src2, TRUE);
144
145   gst_caps_set_simple (sinkcaps,
146       "payload", G_TYPE_INT, 98, "seqnum-base", G_TYPE_UINT, 100,
147       "clock-base", G_TYPE_UINT, 1000, "ssrc", G_TYPE_UINT, 66, NULL);
148   caps = gst_caps_new_simple ("application/x-rtp",
149       "payload", G_TYPE_INT, 98, "clock-rate", G_TYPE_INT, 3,
150       "seqnum-base", G_TYPE_UINT, 56, "clock-base", G_TYPE_UINT, 57,
151       "ssrc", G_TYPE_UINT, 66, NULL);
152   fail_unless (gst_pad_set_caps (src1, caps));
153
154   gst_segment_init (&segment, GST_FORMAT_TIME);
155   segment.start = 100000;
156   fail_unless (gst_pad_push_event (src1, gst_event_new_segment (&segment)));
157   segment.start = 0;
158   fail_unless (gst_pad_push_event (src2, gst_event_new_segment (&segment)));
159
160
161   for (i = 0; i < count; i++) {
162     GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
163
164     inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
165     GST_BUFFER_PTS (inbuf) = i * 1000 + 100000;
166     GST_BUFFER_DURATION (inbuf) = 1000;
167
168     gst_rtp_buffer_map (inbuf, GST_MAP_WRITE, &rtpbuffer);
169
170     gst_rtp_buffer_set_version (&rtpbuffer, 2);
171     gst_rtp_buffer_set_payload_type (&rtpbuffer, 98);
172     gst_rtp_buffer_set_ssrc (&rtpbuffer, 44);
173     gst_rtp_buffer_set_timestamp (&rtpbuffer, 200 + i);
174     gst_rtp_buffer_set_seq (&rtpbuffer, 2000 + i);
175     gst_rtp_buffer_unmap (&rtpbuffer);
176     fail_unless (gst_pad_push (src1, inbuf) == GST_FLOW_OK);
177
178     if (buffers)
179       fail_unless (GST_BUFFER_PTS (buffers->data) == i * 1000, "%lld",
180           GST_BUFFER_PTS (buffers->data));
181
182     cb (src2, i);
183
184     g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
185     g_list_free (buffers);
186     buffers = NULL;
187   }
188
189
190   gst_pad_set_active (sink, FALSE);
191   gst_pad_set_active (src1, FALSE);
192   gst_pad_set_active (src2, FALSE);
193   fail_unless (gst_element_set_state (rtpmux,
194           GST_STATE_NULL) == GST_STATE_CHANGE_SUCCESS);
195   gst_check_teardown_pad_by_name (rtpmux, "src");
196   gst_object_unref (reqpad1);
197   gst_object_unref (reqpad2);
198   gst_check_teardown_pad_by_name (rtpmux, "sink_1");
199   gst_check_teardown_pad_by_name (rtpmux, sink2);
200   gst_element_release_request_pad (rtpmux, reqpad1);
201   gst_element_release_request_pad (rtpmux, reqpad2);
202
203   gst_caps_unref (caps);
204   gst_caps_replace (&src1caps, NULL);
205   gst_caps_replace (&src2caps, NULL);
206   gst_caps_replace (&sinkcaps, NULL);
207
208   gst_check_teardown_element (rtpmux);
209 }
210
211 static void
212 basic_check_cb (GstPad * pad, int i)
213 {
214   GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
215   fail_unless (buffers && g_list_length (buffers) == 1);
216
217   gst_rtp_buffer_map (buffers->data, GST_MAP_READ, &rtpbuffer);
218   fail_unless (gst_rtp_buffer_get_ssrc (&rtpbuffer) == 55);
219   fail_unless (gst_rtp_buffer_get_timestamp (&rtpbuffer) ==
220       200 - 57 + 1000 + i);
221   fail_unless (gst_rtp_buffer_get_seq (&rtpbuffer) == 100 + 1 + i);
222   gst_rtp_buffer_unmap (&rtpbuffer);
223 }
224
225
226 GST_START_TEST (test_rtpmux_basic)
227 {
228   test_basic ("rtpmux", "sink_2", 10, basic_check_cb);
229 }
230
231 GST_END_TEST;
232
233 GST_START_TEST (test_rtpdtmfmux_basic)
234 {
235   test_basic ("rtpdtmfmux", "sink_2", 10, basic_check_cb);
236 }
237
238 GST_END_TEST;
239
240 static void
241 lock_check_cb (GstPad * pad, int i)
242 {
243   GstBuffer *inbuf;
244
245   if (i % 2) {
246     fail_unless (buffers == NULL);
247   } else {
248     GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
249
250     fail_unless (buffers && g_list_length (buffers) == 1);
251     gst_rtp_buffer_map (buffers->data, GST_MAP_READ, &rtpbuffer);
252     fail_unless (gst_rtp_buffer_get_ssrc (&rtpbuffer) == 55);
253     fail_unless (gst_rtp_buffer_get_timestamp (&rtpbuffer) ==
254         200 - 57 + 1000 + i);
255     fail_unless (gst_rtp_buffer_get_seq (&rtpbuffer) == 100 + 1 + i);
256     gst_rtp_buffer_unmap (&rtpbuffer);
257
258     inbuf = gst_rtp_buffer_new_allocate (10, 0, 0);
259     GST_BUFFER_PTS (inbuf) = i * 1000 + 500;
260     GST_BUFFER_DURATION (inbuf) = 1000;
261     gst_rtp_buffer_map (inbuf, GST_MAP_WRITE, &rtpbuffer);
262     gst_rtp_buffer_set_version (&rtpbuffer, 2);
263     gst_rtp_buffer_set_payload_type (&rtpbuffer, 98);
264     gst_rtp_buffer_set_ssrc (&rtpbuffer, 44);
265     gst_rtp_buffer_set_timestamp (&rtpbuffer, 200 + i);
266     gst_rtp_buffer_set_seq (&rtpbuffer, 2000 + i);
267     gst_rtp_buffer_unmap (&rtpbuffer);
268     fail_unless (gst_pad_push (pad, inbuf) == GST_FLOW_OK);
269
270
271     g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
272     g_list_free (buffers);
273     buffers = NULL;
274   }
275 }
276
277 GST_START_TEST (test_rtpdtmfmux_lock)
278 {
279   test_basic ("rtpdtmfmux", "priority_sink_2", 10, lock_check_cb);
280 }
281
282 GST_END_TEST;
283
284 static Suite *
285 rtpmux_suite (void)
286 {
287   Suite *s = suite_create ("rtpmux");
288   TCase *tc_chain;
289
290   tc_chain = tcase_create ("rtpmux_basic");
291   tcase_add_test (tc_chain, test_rtpmux_basic);
292   suite_add_tcase (s, tc_chain);
293
294   tc_chain = tcase_create ("rtpdtmfmux_basic");
295   tcase_add_test (tc_chain, test_rtpdtmfmux_basic);
296   suite_add_tcase (s, tc_chain);
297
298   tc_chain = tcase_create ("rtpdtmfmux_lock");
299   tcase_add_test (tc_chain, test_rtpdtmfmux_lock);
300   suite_add_tcase (s, tc_chain);
301
302   return s;
303 }
304
305 GST_CHECK_MAIN (rtpmux)