0fb7e59c4f02359e0c67fa692b443dfa1d67e3cd
[platform/upstream/gstreamer.git] / tests / check / gst / stream.c
1 /* GStreamer
2  * Copyright (C) 2013 Axis Communications AB <dev-gstreamer at axis dot com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 #include <rtsp-stream.h>
23 #include <rtsp-address-pool.h>
24
25 GST_START_TEST (test_get_sockets)
26 {
27   GstPad *srcpad;
28   GstElement *pay;
29   GstRTSPStream *stream;
30   GstBin *bin;
31   GstElement *rtpbin;
32   GstRTSPAddressPool *pool;
33   GSocket *socket;
34   gboolean have_ipv4;
35   gboolean have_ipv6;
36   GstRTSPTransport *tr;
37
38   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
39   fail_unless (srcpad != NULL);
40   gst_pad_set_active (srcpad, TRUE);
41   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
42   fail_unless (pay != NULL);
43   stream = gst_rtsp_stream_new (0, pay, srcpad);
44   fail_unless (stream != NULL);
45   gst_object_unref (pay);
46   gst_object_unref (srcpad);
47   rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
48   fail_unless (rtpbin != NULL);
49   bin = GST_BIN (gst_bin_new ("testbin"));
50   fail_unless (bin != NULL);
51   fail_unless (gst_bin_add (bin, rtpbin));
52
53   /* configure address pool for IPv4 and IPv6 unicast addresses */
54   pool = gst_rtsp_address_pool_new ();
55   fail_unless (gst_rtsp_address_pool_add_range (pool, GST_RTSP_ADDRESS_POOL_ANY_IPV4,
56         GST_RTSP_ADDRESS_POOL_ANY_IPV4, 50000, 60000, 0));
57   fail_unless (gst_rtsp_address_pool_add_range (pool, GST_RTSP_ADDRESS_POOL_ANY_IPV6,
58         GST_RTSP_ADDRESS_POOL_ANY_IPV6, 50000, 60000, 0));
59   gst_rtsp_stream_set_address_pool (stream, pool);
60
61   fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
62
63   gst_rtsp_transport_new (&tr);
64   tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
65   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
66         tr, FALSE));
67
68   socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV4);
69   have_ipv4 = (socket != NULL);
70   if (have_ipv4) {
71     fail_unless (g_socket_get_fd (socket) >= 0);
72     g_object_unref (socket);
73   }
74
75   socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV4);
76   if (have_ipv4) {
77     fail_unless (socket != NULL);
78     fail_unless (g_socket_get_fd (socket) >= 0);
79     g_object_unref (socket);
80   } else {
81     fail_unless (socket == NULL);
82   }
83
84   socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV6);
85   have_ipv6 = (socket != NULL);
86   if (have_ipv6) {
87     fail_unless (g_socket_get_fd (socket) >= 0);
88     g_object_unref (socket);
89   }
90
91   socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV6);
92   if (have_ipv6) {
93     fail_unless (socket != NULL);
94     fail_unless (g_socket_get_fd (socket) >= 0);
95     g_object_unref (socket);
96   } else {
97     fail_unless (socket == NULL);
98   }
99
100   /* check that at least one family is available */
101   fail_unless (have_ipv4 || have_ipv6);
102
103   gst_rtsp_transport_free (tr);
104   g_object_unref (pool);
105
106   fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
107
108   gst_object_unref (bin);
109   gst_object_unref (stream);
110 }
111
112 GST_END_TEST;
113
114 GST_START_TEST (test_allocate_udp_ports_fail)
115 {
116   GstPad *srcpad;
117   GstElement *pay;
118   GstRTSPStream *stream;
119   GstBin *bin;
120   GstElement *rtpbin;
121   GstRTSPAddressPool *pool;
122   GstRTSPTransport *tr;
123
124   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
125   fail_unless (srcpad != NULL);
126   gst_pad_set_active (srcpad, TRUE);
127   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
128   fail_unless (pay != NULL);
129   stream = gst_rtsp_stream_new (0, pay, srcpad);
130   fail_unless (stream != NULL);
131   gst_object_unref (pay);
132   gst_object_unref (srcpad);
133   rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
134   fail_unless (rtpbin != NULL);
135   bin = GST_BIN (gst_bin_new ("testbin"));
136   fail_unless (bin != NULL);
137   fail_unless (gst_bin_add (bin, rtpbin));
138
139   pool = gst_rtsp_address_pool_new ();
140   fail_unless (gst_rtsp_address_pool_add_range (pool, "192.168.1.1",
141         "192.168.1.1", 6000, 6001, 0));
142   gst_rtsp_stream_set_address_pool (stream, pool);
143
144   fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
145
146   gst_rtsp_transport_new (&tr);
147   tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
148   fail_if (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
149         tr, FALSE));
150
151   gst_rtsp_transport_free (tr);
152   g_object_unref (pool);
153   fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
154   gst_object_unref (bin);
155   gst_object_unref (stream);
156 }
157
158 GST_END_TEST;
159
160 GST_START_TEST (test_get_multicast_address)
161 {
162   GstPad *srcpad;
163   GstElement *pay;
164   GstRTSPStream *stream;
165   GstRTSPAddressPool *pool;
166   GstRTSPAddress *addr1;
167   GstRTSPAddress *addr2;
168
169   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
170   fail_unless (srcpad != NULL);
171   gst_pad_set_active (srcpad, TRUE);
172   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
173   fail_unless (pay != NULL);
174   stream = gst_rtsp_stream_new (0, pay, srcpad);
175   fail_unless (stream != NULL);
176   gst_object_unref (pay);
177   gst_object_unref (srcpad);
178
179   pool = gst_rtsp_address_pool_new ();
180   fail_unless (gst_rtsp_address_pool_add_range (pool,
181           "233.252.0.0", "233.252.0.0", 5000, 5001, 1));
182   fail_unless (gst_rtsp_address_pool_add_range (pool,
183           "FF11:DB8::1", "FF11:DB8::1", 5002, 5003, 1));
184   gst_rtsp_stream_set_address_pool (stream, pool);
185
186   addr1 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
187   fail_unless (addr1 != NULL);
188   fail_unless_equals_string (addr1->address, "233.252.0.0");
189   fail_unless_equals_int (addr1->port, 5000);
190   fail_unless_equals_int (addr1->n_ports, 2);
191
192   addr2 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
193   fail_unless (addr2 != NULL);
194   fail_unless_equals_string (addr2->address, "233.252.0.0");
195   fail_unless_equals_int (addr2->port, 5000);
196   fail_unless_equals_int (addr2->n_ports, 2);
197
198   gst_rtsp_address_free (addr1);
199   gst_rtsp_address_free (addr2);
200
201   addr1 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
202   fail_unless (addr1 != NULL);
203   fail_unless (!g_ascii_strcasecmp (addr1->address, "FF11:DB8::1"));
204   fail_unless_equals_int (addr1->port, 5002);
205   fail_unless_equals_int (addr1->n_ports, 2);
206
207   addr2 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
208   fail_unless (addr2 != NULL);
209   fail_unless (!g_ascii_strcasecmp (addr2->address, "FF11:DB8::1"));
210   fail_unless_equals_int (addr2->port, 5002);
211   fail_unless_equals_int (addr2->n_ports, 2);
212
213   gst_rtsp_address_free (addr1);
214   gst_rtsp_address_free (addr2);
215
216   g_object_unref (pool);
217
218   gst_object_unref (stream);
219 }
220
221 GST_END_TEST;
222
223 /*  test case: address pool only contains multicast addresses,
224  *  but the client is requesting unicast udp */
225 GST_START_TEST (test_multicast_address_and_unicast_udp)
226 {
227   GstPad *srcpad;
228   GstElement *pay;
229   GstRTSPStream *stream;
230   GstBin *bin;
231   GstElement *rtpbin;
232   GstRTSPAddressPool *pool;
233   GstRTSPTransport *tr;
234
235   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
236   fail_unless (srcpad != NULL);
237   gst_pad_set_active (srcpad, TRUE);
238   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
239   fail_unless (pay != NULL);
240   stream = gst_rtsp_stream_new (0, pay, srcpad);
241   fail_unless (stream != NULL);
242   gst_object_unref (pay);
243   gst_object_unref (srcpad);
244   rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
245   fail_unless (rtpbin != NULL);
246   bin = GST_BIN (gst_bin_new ("testbin"));
247   fail_unless (bin != NULL);
248   fail_unless (gst_bin_add (bin, rtpbin));
249
250   pool = gst_rtsp_address_pool_new ();
251   /* add a multicast addres to the address pool */
252   fail_unless (gst_rtsp_address_pool_add_range (pool,
253           "233.252.0.0", "233.252.0.0", 5000, 5001, 1));
254   gst_rtsp_stream_set_address_pool (stream, pool);
255
256   fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
257
258   gst_rtsp_transport_new (&tr);
259   /* unicast udp */
260   tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
261   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
262         tr, FALSE));
263
264   gst_rtsp_transport_free (tr);
265   g_object_unref (pool);
266   fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
267   gst_object_unref (bin);
268   gst_object_unref (stream);
269 }
270
271 GST_END_TEST;
272
273 GST_START_TEST (test_allocate_udp_ports_multicast)
274 {
275   GstPad *srcpad;
276   GstElement *pay;
277   GstRTSPStream *stream;
278   GstBin *bin;
279   GstElement *rtpbin;
280   GstRTSPAddressPool *pool;
281   GstRTSPTransport *tr;
282   GstRTSPAddress *addr;
283
284   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
285   fail_unless (srcpad != NULL);
286   gst_pad_set_active (srcpad, TRUE);
287   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
288   fail_unless (pay != NULL);
289   stream = gst_rtsp_stream_new (0, pay, srcpad);
290   fail_unless (stream != NULL);
291   gst_object_unref (pay);
292   gst_object_unref (srcpad);
293   rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
294   fail_unless (rtpbin != NULL);
295   bin = GST_BIN (gst_bin_new ("testbin"));
296   fail_unless (bin != NULL);
297   fail_unless (gst_bin_add (bin, rtpbin));
298
299   pool = gst_rtsp_address_pool_new ();
300   /* add multicast addresses to the address pool */
301   fail_unless (gst_rtsp_address_pool_add_range (pool,
302           "233.252.0.1", "233.252.0.1", 6000, 6001, 1));
303   fail_unless (gst_rtsp_address_pool_add_range (pool,
304           "FF11:DB8::1", "FF11:DB8::1", 6002, 6003, 1));
305   gst_rtsp_stream_set_address_pool (stream, pool);
306
307   fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
308
309   /* allocate udp multicast ports for IPv4 */
310   gst_rtsp_transport_new (&tr);
311   tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
312   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
313         tr, FALSE));
314
315   /* check the multicast address and ports for IPv4 */
316   addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
317   fail_unless (addr != NULL);
318   fail_unless_equals_string (addr->address, "233.252.0.1");
319   fail_unless_equals_int (addr->port, 6000);
320   fail_unless_equals_int (addr->n_ports, 2);
321   gst_rtsp_address_free (addr);
322
323   /* allocate upd multicast ports for IPv6 */
324   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV6,
325         tr, FALSE));
326
327   /* check the multicast address and ports for IPv6 */
328   addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
329   fail_unless (addr != NULL);
330   fail_unless (!g_ascii_strcasecmp (addr->address, "FF11:DB8::1"));
331   fail_unless_equals_int (addr->port, 6002);
332   fail_unless_equals_int (addr->n_ports, 2);
333   gst_rtsp_address_free (addr);
334
335   gst_rtsp_transport_free (tr);
336   g_object_unref (pool);
337   fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
338   gst_object_unref (bin);
339   gst_object_unref (stream);
340 }
341
342 GST_END_TEST;
343
344 GST_START_TEST (test_allocate_udp_ports_client_settings)
345 {
346   GstPad *srcpad;
347   GstElement *pay;
348   GstRTSPStream *stream;
349   GstBin *bin;
350   GstElement *rtpbin;
351   GstRTSPAddressPool *pool;
352   GstRTSPTransport *tr;
353   GstRTSPAddress *addr;
354
355   srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
356   fail_unless (srcpad != NULL);
357   gst_pad_set_active (srcpad, TRUE);
358   pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
359   fail_unless (pay != NULL);
360   stream = gst_rtsp_stream_new (0, pay, srcpad);
361   fail_unless (stream != NULL);
362   gst_object_unref (pay);
363   gst_object_unref (srcpad);
364   rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
365   fail_unless (rtpbin != NULL);
366   bin = GST_BIN (gst_bin_new ("testbin"));
367   fail_unless (bin != NULL);
368   fail_unless (gst_bin_add (bin, rtpbin));
369
370   pool = gst_rtsp_address_pool_new ();
371   /* add multicast addresses to the address pool */
372   fail_unless (gst_rtsp_address_pool_add_range (pool,
373           "233.252.0.1", "233.252.0.1", 6000, 6001, 1));
374   fail_unless (gst_rtsp_address_pool_add_range (pool,
375           "FF11:DB7::1", "FF11:DB7::1", 6004, 6005, 1));
376   /* multicast address specified by the client */
377   fail_unless (gst_rtsp_address_pool_add_range (pool,
378           "233.252.0.2", "233.252.0.2", 6002, 6003, 1));
379   fail_unless (gst_rtsp_address_pool_add_range (pool,
380           "FF11:DB8::1", "FF11:DB8::1", 6006, 6007, 1));
381   gst_rtsp_stream_set_address_pool (stream, pool);
382
383   fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
384
385   /* client transport settings for IPv4 */
386   gst_rtsp_transport_new (&tr);
387   tr->destination = g_strdup ("233.252.0.2");
388   tr->port.min = 6002;
389   tr->port.max = 6003;
390   tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
391   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
392         tr, FALSE));
393
394   /* verify that the multicast address and ports correspond to the requested client
395    * transport information for IPv4 */
396   addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
397   fail_unless (addr != NULL);
398   fail_unless_equals_string (addr->address, "233.252.0.2");
399   fail_unless_equals_int (addr->port, 6002);
400   fail_unless_equals_int (addr->n_ports, 2);
401   gst_rtsp_address_free (addr);
402
403   /* client transport settings for IPv6 */
404   g_free (tr->destination);
405   tr->destination = g_strdup ("FF11:DB8::1");
406   tr->port.min = 6006;
407   tr->port.max = 6007;
408   fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV6,
409         tr, FALSE));
410
411   /* verify that the multicast address and ports correspond to the requested client
412    * transport information for IPv6 */
413   addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
414   fail_unless (addr != NULL);
415   fail_unless (!g_ascii_strcasecmp (addr->address, "FF11:DB8::1"));
416   fail_unless_equals_int (addr->port, 6006);
417   fail_unless_equals_int (addr->n_ports, 2);
418   gst_rtsp_address_free (addr);
419
420   gst_rtsp_transport_free (tr);
421   g_object_unref (pool);
422   fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
423   gst_object_unref (bin);
424   gst_object_unref (stream);
425 }
426
427 GST_END_TEST;
428
429 static Suite *
430 rtspstream_suite (void)
431 {
432   Suite *s = suite_create ("rtspstream");
433   TCase *tc = tcase_create ("general");
434
435   suite_add_tcase (s, tc);
436   tcase_add_test (tc, test_get_sockets);
437   tcase_add_test (tc, test_allocate_udp_ports_fail);
438   tcase_add_test (tc, test_get_multicast_address);
439   tcase_add_test (tc, test_multicast_address_and_unicast_udp);
440   tcase_add_test (tc, test_allocate_udp_ports_multicast);
441   tcase_add_test (tc, test_allocate_udp_ports_client_settings);
442
443   return s;
444 }
445
446 GST_CHECK_MAIN (rtspstream);