2 * Copyright (C) 2013 Axis Communications AB <dev-gstreamer at axis dot com>
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.
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.
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.
20 #include <gst/check/gstcheck.h>
22 #include <rtsp-stream.h>
23 #include <rtsp-address-pool.h>
26 get_sockets (GstRTSPLowerTrans lower_transport, GSocketFamily socket_family)
30 GstRTSPStream *stream;
33 GstRTSPAddressPool *pool;
37 GstRTSPTransport *transport;
39 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
40 fail_unless (srcpad != NULL);
41 gst_pad_set_active (srcpad, TRUE);
42 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
43 fail_unless (pay != NULL);
44 stream = gst_rtsp_stream_new (0, pay, srcpad);
45 fail_unless (stream != NULL);
46 gst_object_unref (pay);
47 gst_object_unref (srcpad);
48 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
49 fail_unless (rtpbin != NULL);
50 bin = GST_BIN (gst_bin_new ("testbin"));
51 fail_unless (bin != NULL);
52 fail_unless (gst_bin_add (bin, rtpbin));
54 /* configure address pool for IPv4 and IPv6 unicast addresses */
55 pool = gst_rtsp_address_pool_new ();
56 fail_unless (gst_rtsp_address_pool_add_range (pool,
57 GST_RTSP_ADDRESS_POOL_ANY_IPV4, GST_RTSP_ADDRESS_POOL_ANY_IPV4, 50000,
59 fail_unless (gst_rtsp_address_pool_add_range (pool,
60 GST_RTSP_ADDRESS_POOL_ANY_IPV6, GST_RTSP_ADDRESS_POOL_ANY_IPV6, 50000,
62 fail_unless (gst_rtsp_address_pool_add_range (pool, "233.252.0.0",
63 "233.252.0.0", 50000, 60000, 1));
64 fail_unless (gst_rtsp_address_pool_add_range (pool, "FF11:DB8::1",
65 "FF11:DB8::1", 50000, 60000, 1));
66 gst_rtsp_stream_set_address_pool (stream, pool);
68 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
70 /* allocate udp ports first */
71 fail_unless (gst_rtsp_transport_new (&transport) == GST_RTSP_OK);
72 transport->lower_transport = lower_transport;
74 /* no ports allocated, complete stream should fail */
75 fail_if (gst_rtsp_stream_complete_stream (stream, transport));
78 fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream,
79 socket_family, transport, FALSE));
81 fail_unless (gst_rtsp_stream_complete_stream (stream, transport));
82 fail_unless (gst_rtsp_transport_free (transport) == GST_RTSP_OK);
84 if (lower_transport == GST_RTSP_LOWER_TRANS_UDP)
85 socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV4);
87 socket = gst_rtsp_stream_get_rtp_multicast_socket (stream,
88 G_SOCKET_FAMILY_IPV4);
89 have_ipv4 = (socket != NULL);
91 fail_unless (g_socket_get_fd (socket) >= 0);
92 g_object_unref (socket);
95 if (lower_transport == GST_RTSP_LOWER_TRANS_UDP)
96 socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV4);
98 socket = gst_rtsp_stream_get_rtcp_multicast_socket (stream,
99 G_SOCKET_FAMILY_IPV4);
101 fail_unless (socket != NULL);
102 fail_unless (g_socket_get_fd (socket) >= 0);
103 g_object_unref (socket);
105 fail_unless (socket == NULL);
108 if (lower_transport == GST_RTSP_LOWER_TRANS_UDP)
109 socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV6);
111 socket = gst_rtsp_stream_get_rtp_multicast_socket (stream,
112 G_SOCKET_FAMILY_IPV6);
113 have_ipv6 = (socket != NULL);
115 fail_unless (g_socket_get_fd (socket) >= 0);
116 g_object_unref (socket);
119 if (lower_transport == GST_RTSP_LOWER_TRANS_UDP)
120 socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV6);
122 socket = gst_rtsp_stream_get_rtcp_multicast_socket (stream,
123 G_SOCKET_FAMILY_IPV6);
125 fail_unless (socket != NULL);
126 fail_unless (g_socket_get_fd (socket) >= 0);
127 g_object_unref (socket);
129 fail_unless (socket == NULL);
132 /* check that at least one family is available */
133 fail_unless (have_ipv4 || have_ipv6);
135 g_object_unref (pool);
137 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
139 gst_object_unref (bin);
140 gst_object_unref (stream);
143 GST_START_TEST (test_get_sockets_udp)
145 get_sockets (GST_RTSP_LOWER_TRANS_UDP, G_SOCKET_FAMILY_IPV4);
146 get_sockets (GST_RTSP_LOWER_TRANS_UDP, G_SOCKET_FAMILY_IPV6);
151 GST_START_TEST (test_get_sockets_mcast)
153 get_sockets (GST_RTSP_LOWER_TRANS_UDP_MCAST, G_SOCKET_FAMILY_IPV4);
154 get_sockets (GST_RTSP_LOWER_TRANS_UDP_MCAST, G_SOCKET_FAMILY_IPV6);
159 /* The purpose of this test is to make sure that it's not possible to allocate
160 * multicast UDP ports if the address pool does not contain multicast UDP
162 GST_START_TEST (test_allocate_udp_ports_fail)
166 GstRTSPStream *stream;
169 GstRTSPAddressPool *pool;
170 GstRTSPTransport *transport;
172 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
173 fail_unless (srcpad != NULL);
174 gst_pad_set_active (srcpad, TRUE);
175 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
176 fail_unless (pay != NULL);
177 stream = gst_rtsp_stream_new (0, pay, srcpad);
178 fail_unless (stream != NULL);
179 gst_object_unref (pay);
180 gst_object_unref (srcpad);
181 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
182 fail_unless (rtpbin != NULL);
183 bin = GST_BIN (gst_bin_new ("testbin"));
184 fail_unless (bin != NULL);
185 fail_unless (gst_bin_add (bin, rtpbin));
187 pool = gst_rtsp_address_pool_new ();
188 fail_unless (gst_rtsp_address_pool_add_range (pool, "192.168.1.1",
189 "192.168.1.1", 6000, 6001, 0));
190 gst_rtsp_stream_set_address_pool (stream, pool);
192 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
194 fail_unless (gst_rtsp_transport_new (&transport) == GST_RTSP_OK);
195 transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
196 fail_if (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
198 fail_unless (gst_rtsp_transport_free (transport) == GST_RTSP_OK);
200 g_object_unref (pool);
201 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
202 gst_object_unref (bin);
203 gst_object_unref (stream);
208 GST_START_TEST (test_get_multicast_address)
212 GstRTSPStream *stream;
213 GstRTSPAddressPool *pool;
214 GstRTSPAddress *addr1;
215 GstRTSPAddress *addr2;
217 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
218 fail_unless (srcpad != NULL);
219 gst_pad_set_active (srcpad, TRUE);
220 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
221 fail_unless (pay != NULL);
222 stream = gst_rtsp_stream_new (0, pay, srcpad);
223 fail_unless (stream != NULL);
224 gst_object_unref (pay);
225 gst_object_unref (srcpad);
227 pool = gst_rtsp_address_pool_new ();
228 fail_unless (gst_rtsp_address_pool_add_range (pool,
229 "233.252.0.0", "233.252.0.0", 5100, 5101, 1));
230 fail_unless (gst_rtsp_address_pool_add_range (pool,
231 "FF11:DB8::1", "FF11:DB8::1", 5102, 5103, 1));
232 gst_rtsp_stream_set_address_pool (stream, pool);
234 addr1 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
235 fail_unless (addr1 != NULL);
236 fail_unless_equals_string (addr1->address, "233.252.0.0");
237 fail_unless_equals_int (addr1->port, 5100);
238 fail_unless_equals_int (addr1->n_ports, 2);
240 addr2 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
241 fail_unless (addr2 != NULL);
242 fail_unless_equals_string (addr2->address, "233.252.0.0");
243 fail_unless_equals_int (addr2->port, 5100);
244 fail_unless_equals_int (addr2->n_ports, 2);
246 gst_rtsp_address_free (addr1);
247 gst_rtsp_address_free (addr2);
249 addr1 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
250 fail_unless (addr1 != NULL);
251 fail_unless (!g_ascii_strcasecmp (addr1->address, "FF11:DB8::1"));
252 fail_unless_equals_int (addr1->port, 5102);
253 fail_unless_equals_int (addr1->n_ports, 2);
255 addr2 = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
256 fail_unless (addr2 != NULL);
257 fail_unless (!g_ascii_strcasecmp (addr2->address, "FF11:DB8::1"));
258 fail_unless_equals_int (addr2->port, 5102);
259 fail_unless_equals_int (addr2->n_ports, 2);
261 gst_rtsp_address_free (addr1);
262 gst_rtsp_address_free (addr2);
264 g_object_unref (pool);
266 gst_object_unref (stream);
271 /* test case: address pool only contains multicast addresses,
272 * but the client is requesting unicast udp */
273 GST_START_TEST (test_multicast_address_and_unicast_udp)
277 GstRTSPStream *stream;
280 GstRTSPAddressPool *pool;
282 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
283 fail_unless (srcpad != NULL);
284 gst_pad_set_active (srcpad, TRUE);
285 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
286 fail_unless (pay != NULL);
287 stream = gst_rtsp_stream_new (0, pay, srcpad);
288 fail_unless (stream != NULL);
289 gst_object_unref (pay);
290 gst_object_unref (srcpad);
291 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
292 fail_unless (rtpbin != NULL);
293 bin = GST_BIN (gst_bin_new ("testbin"));
294 fail_unless (bin != NULL);
295 fail_unless (gst_bin_add (bin, rtpbin));
297 pool = gst_rtsp_address_pool_new ();
298 /* add a multicast addres to the address pool */
299 fail_unless (gst_rtsp_address_pool_add_range (pool,
300 "233.252.0.0", "233.252.0.0", 5200, 5201, 1));
301 gst_rtsp_stream_set_address_pool (stream, pool);
303 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
305 g_object_unref (pool);
306 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
307 gst_object_unref (bin);
308 gst_object_unref (stream);
313 GST_START_TEST (test_allocate_udp_ports_multicast)
317 GstRTSPStream *stream;
320 GstRTSPAddressPool *pool;
321 GstRTSPAddress *addr;
323 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
324 fail_unless (srcpad != NULL);
325 gst_pad_set_active (srcpad, TRUE);
326 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
327 fail_unless (pay != NULL);
328 stream = gst_rtsp_stream_new (0, pay, srcpad);
329 fail_unless (stream != NULL);
330 gst_object_unref (pay);
331 gst_object_unref (srcpad);
332 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
333 fail_unless (rtpbin != NULL);
334 bin = GST_BIN (gst_bin_new ("testbin"));
335 fail_unless (bin != NULL);
336 fail_unless (gst_bin_add (bin, rtpbin));
338 pool = gst_rtsp_address_pool_new ();
339 /* add multicast addresses to the address pool */
340 fail_unless (gst_rtsp_address_pool_add_range (pool,
341 "233.252.0.1", "233.252.0.1", 6000, 6001, 1));
342 fail_unless (gst_rtsp_address_pool_add_range (pool,
343 "FF11:DB8::1", "FF11:DB8::1", 6002, 6003, 1));
344 gst_rtsp_stream_set_address_pool (stream, pool);
346 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
348 /* check the multicast address and ports for IPv4 */
349 addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
350 fail_unless (addr != NULL);
351 fail_unless_equals_string (addr->address, "233.252.0.1");
352 fail_unless_equals_int (addr->port, 6000);
353 fail_unless_equals_int (addr->n_ports, 2);
354 gst_rtsp_address_free (addr);
356 /* check the multicast address and ports for IPv6 */
357 addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
358 fail_unless (addr != NULL);
359 fail_unless (!g_ascii_strcasecmp (addr->address, "FF11:DB8::1"));
360 fail_unless_equals_int (addr->port, 6002);
361 fail_unless_equals_int (addr->n_ports, 2);
362 gst_rtsp_address_free (addr);
364 g_object_unref (pool);
365 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
366 gst_object_unref (bin);
367 gst_object_unref (stream);
372 GST_START_TEST (test_allocate_udp_ports_client_settings)
376 GstRTSPStream *stream;
379 GstRTSPAddressPool *pool;
380 GstRTSPAddress *addr;
382 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
383 fail_unless (srcpad != NULL);
384 gst_pad_set_active (srcpad, TRUE);
385 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
386 fail_unless (pay != NULL);
387 stream = gst_rtsp_stream_new (0, pay, srcpad);
388 fail_unless (stream != NULL);
389 gst_object_unref (pay);
390 gst_object_unref (srcpad);
391 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
392 fail_unless (rtpbin != NULL);
393 bin = GST_BIN (gst_bin_new ("testbin"));
394 fail_unless (bin != NULL);
395 fail_unless (gst_bin_add (bin, rtpbin));
397 pool = gst_rtsp_address_pool_new ();
398 /* add multicast addresses to the address pool */
399 fail_unless (gst_rtsp_address_pool_add_range (pool,
400 "233.252.0.1", "233.252.0.1", 6000, 6001, 1));
401 fail_unless (gst_rtsp_address_pool_add_range (pool,
402 "FF11:DB7::1", "FF11:DB7::1", 6004, 6005, 1));
403 /* multicast address specified by the client */
404 fail_unless (gst_rtsp_address_pool_add_range (pool,
405 "233.252.0.2", "233.252.0.2", 6002, 6003, 1));
406 fail_unless (gst_rtsp_address_pool_add_range (pool,
407 "FF11:DB8::1", "FF11:DB8::1", 6006, 6007, 1));
408 gst_rtsp_stream_set_address_pool (stream, pool);
410 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
412 /* Reserve IPV4 mcast address */
413 addr = gst_rtsp_stream_reserve_address (stream, "233.252.0.2", 6002, 2, 1);
414 fail_unless (addr != NULL);
415 gst_rtsp_address_free (addr);
417 /* verify that the multicast address and ports correspond to the requested client
418 * transport information for IPv4 */
419 addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV4);
420 fail_unless (addr != NULL);
421 fail_unless_equals_string (addr->address, "233.252.0.2");
422 fail_unless_equals_int (addr->port, 6002);
423 fail_unless_equals_int (addr->n_ports, 2);
424 gst_rtsp_address_free (addr);
426 /* Reserve IPV6 mcast address */
427 addr = gst_rtsp_stream_reserve_address (stream, "FF11:DB8::1", 6006, 2, 1);
428 fail_unless (addr != NULL);
429 gst_rtsp_address_free (addr);
431 /* verify that the multicast address and ports correspond to the requested client
432 * transport information for IPv6 */
433 addr = gst_rtsp_stream_get_multicast_address (stream, G_SOCKET_FAMILY_IPV6);
434 fail_unless (addr != NULL);
435 fail_unless (!g_ascii_strcasecmp (addr->address, "FF11:DB8::1"));
436 fail_unless_equals_int (addr->port, 6006);
437 fail_unless_equals_int (addr->n_ports, 2);
438 gst_rtsp_address_free (addr);
440 g_object_unref (pool);
441 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
442 gst_object_unref (bin);
443 gst_object_unref (stream);
448 GST_START_TEST (test_tcp_transport)
452 GstRTSPStream *stream;
455 GstRTSPRange server_port;
457 srcpad = gst_pad_new ("testsrcpad", GST_PAD_SRC);
458 fail_unless (srcpad != NULL);
459 gst_pad_set_active (srcpad, TRUE);
460 pay = gst_element_factory_make ("rtpgstpay", "testpayloader");
461 fail_unless (pay != NULL);
462 stream = gst_rtsp_stream_new (0, pay, srcpad);
463 fail_unless (stream != NULL);
464 gst_object_unref (pay);
465 gst_object_unref (srcpad);
466 rtpbin = gst_element_factory_make ("rtpbin", "testrtpbin");
467 fail_unless (rtpbin != NULL);
468 bin = GST_BIN (gst_bin_new ("testbin"));
469 fail_unless (bin != NULL);
470 fail_unless (gst_bin_add (bin, rtpbin));
473 gst_rtsp_stream_set_protocols (stream, GST_RTSP_LOWER_TRANS_TCP);
474 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
476 /* port that the server will use to receive RTCP makes only sense in the UDP
477 * case so verify that the received server port is 0 in the TCP case */
478 gst_rtsp_stream_get_server_port (stream, &server_port, G_SOCKET_FAMILY_IPV4);
479 fail_unless_equals_int (server_port.min, 0);
480 fail_unless_equals_int (server_port.max, 0);
482 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
483 gst_object_unref (bin);
484 gst_object_unref (stream);
490 rtspstream_suite (void)
492 Suite *s = suite_create ("rtspstream");
493 TCase *tc = tcase_create ("general");
495 suite_add_tcase (s, tc);
496 tcase_add_test (tc, test_get_sockets_udp);
497 tcase_add_test (tc, test_get_sockets_mcast);
498 tcase_add_test (tc, test_allocate_udp_ports_fail);
499 tcase_add_test (tc, test_get_multicast_address);
500 tcase_add_test (tc, test_multicast_address_and_unicast_udp);
501 tcase_add_test (tc, test_allocate_udp_ports_multicast);
502 tcase_add_test (tc, test_allocate_udp_ports_client_settings);
503 tcase_add_test (tc, test_tcp_transport);
508 GST_CHECK_MAIN (rtspstream);