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>
25 GST_START_TEST (test_get_sockets)
29 GstRTSPStream *stream;
32 GstRTSPAddressPool *pool;
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));
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);
61 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
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,
68 socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV4);
69 have_ipv4 = (socket != NULL);
71 fail_unless (g_socket_get_fd (socket) >= 0);
72 g_object_unref (socket);
75 socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV4);
77 fail_unless (socket != NULL);
78 fail_unless (g_socket_get_fd (socket) >= 0);
79 g_object_unref (socket);
81 fail_unless (socket == NULL);
84 socket = gst_rtsp_stream_get_rtp_socket (stream, G_SOCKET_FAMILY_IPV6);
85 have_ipv6 = (socket != NULL);
87 fail_unless (g_socket_get_fd (socket) >= 0);
88 g_object_unref (socket);
91 socket = gst_rtsp_stream_get_rtcp_socket (stream, G_SOCKET_FAMILY_IPV6);
93 fail_unless (socket != NULL);
94 fail_unless (g_socket_get_fd (socket) >= 0);
95 g_object_unref (socket);
97 fail_unless (socket == NULL);
100 /* check that at least one family is available */
101 fail_unless (have_ipv4 || have_ipv6);
103 gst_rtsp_transport_free (tr);
104 g_object_unref (pool);
106 fail_unless (gst_rtsp_stream_leave_bin (stream, bin, rtpbin));
108 gst_object_unref (bin);
109 gst_object_unref (stream);
114 GST_START_TEST (test_allocate_udp_ports_fail)
118 GstRTSPStream *stream;
121 GstRTSPAddressPool *pool;
122 GstRTSPTransport *tr;
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));
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);
144 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
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,
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);
160 GST_START_TEST (test_get_multicast_address)
164 GstRTSPStream *stream;
165 GstRTSPAddressPool *pool;
166 GstRTSPAddress *addr1;
167 GstRTSPAddress *addr2;
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);
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);
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);
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);
198 gst_rtsp_address_free (addr1);
199 gst_rtsp_address_free (addr2);
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);
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);
213 gst_rtsp_address_free (addr1);
214 gst_rtsp_address_free (addr2);
216 g_object_unref (pool);
218 gst_object_unref (stream);
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)
229 GstRTSPStream *stream;
232 GstRTSPAddressPool *pool;
233 GstRTSPTransport *tr;
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));
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);
256 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
258 gst_rtsp_transport_new (&tr);
260 tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
261 fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
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);
273 GST_START_TEST (test_allocate_udp_ports_multicast)
277 GstRTSPStream *stream;
280 GstRTSPAddressPool *pool;
281 GstRTSPTransport *tr;
282 GstRTSPAddress *addr;
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));
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);
307 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
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,
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);
323 /* allocate upd multicast ports for IPv6 */
324 fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV6,
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);
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);
344 GST_START_TEST (test_allocate_udp_ports_client_settings)
348 GstRTSPStream *stream;
351 GstRTSPAddressPool *pool;
352 GstRTSPTransport *tr;
353 GstRTSPAddress *addr;
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));
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);
383 fail_unless (gst_rtsp_stream_join_bin (stream, bin, rtpbin, GST_STATE_NULL));
385 /* client transport settings for IPv4 */
386 gst_rtsp_transport_new (&tr);
387 tr->destination = g_strdup ("233.252.0.2");
390 tr->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
391 fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV4,
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);
403 /* client transport settings for IPv6 */
404 g_free (tr->destination);
405 tr->destination = g_strdup ("FF11:DB8::1");
408 fail_unless (gst_rtsp_stream_allocate_udp_sockets (stream, G_SOCKET_FAMILY_IPV6,
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);
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);
430 rtspstream_suite (void)
432 Suite *s = suite_create ("rtspstream");
433 TCase *tc = tcase_create ("general");
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);
446 GST_CHECK_MAIN (rtspstream);