2 * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.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-client.h>
27 test_response_200 (GstRTSPClient * client, GstRTSPMessage * response,
28 gboolean close, gpointer user_data)
30 GstRTSPStatusCode code;
32 GstRTSPVersion version;
34 fail_unless (gst_rtsp_message_get_type (response) ==
35 GST_RTSP_MESSAGE_RESPONSE);
37 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
40 fail_unless (code == GST_RTSP_STS_OK);
41 fail_unless (g_str_equal (reason, "OK"));
42 fail_unless (version == GST_RTSP_VERSION_1_0);
48 test_response_400 (GstRTSPClient * client, GstRTSPMessage * response,
49 gboolean close, gpointer user_data)
51 GstRTSPStatusCode code;
53 GstRTSPVersion version;
55 fail_unless (gst_rtsp_message_get_type (response) ==
56 GST_RTSP_MESSAGE_RESPONSE);
58 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
61 fail_unless (code == GST_RTSP_STS_BAD_REQUEST);
62 fail_unless (g_str_equal (reason, "Bad Request"));
63 fail_unless (version == GST_RTSP_VERSION_1_0);
69 test_response_404 (GstRTSPClient * client, GstRTSPMessage * response,
70 gboolean close, gpointer user_data)
72 GstRTSPStatusCode code;
74 GstRTSPVersion version;
76 fail_unless (gst_rtsp_message_get_type (response) ==
77 GST_RTSP_MESSAGE_RESPONSE);
79 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
82 fail_unless (code == GST_RTSP_STS_NOT_FOUND);
83 fail_unless (g_str_equal (reason, "Not Found"));
84 fail_unless (version == GST_RTSP_VERSION_1_0);
90 test_response_454 (GstRTSPClient * client, GstRTSPMessage * response,
91 gboolean close, gpointer user_data)
93 GstRTSPStatusCode code;
95 GstRTSPVersion version;
97 fail_unless (gst_rtsp_message_get_type (response) ==
98 GST_RTSP_MESSAGE_RESPONSE);
100 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
103 fail_unless (code == GST_RTSP_STS_SESSION_NOT_FOUND);
104 fail_unless (g_str_equal (reason, "Session Not Found"));
105 fail_unless (version == GST_RTSP_VERSION_1_0);
110 static GstRTSPClient *
113 GstRTSPClient *client;
114 GstRTSPSessionPool *session_pool;
115 GstRTSPMountPoints *mount_points;
116 GstRTSPMediaFactory *factory;
118 client = gst_rtsp_client_new ();
120 session_pool = gst_rtsp_session_pool_new ();
121 gst_rtsp_client_set_session_pool (client, session_pool);
123 mount_points = gst_rtsp_mount_points_new ();
124 factory = gst_rtsp_media_factory_new ();
125 gst_rtsp_media_factory_set_launch (factory,
126 "videotestsrc ! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96");
127 gst_rtsp_mount_points_add_factory (mount_points, "/test", factory);
128 gst_rtsp_client_set_mount_points (client, mount_points);
130 g_object_unref (mount_points);
131 g_object_unref (session_pool);
136 GST_START_TEST (test_request)
138 GstRTSPClient *client;
139 GstRTSPMessage request = { 0, };
142 client = gst_rtsp_client_new ();
144 /* OPTIONS with invalid url */
145 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
146 "foopy://padoop/") == GST_RTSP_OK);
147 str = g_strdup_printf ("%d", cseq);
148 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
151 gst_rtsp_client_set_send_func (client, test_response_400, NULL, NULL);
152 fail_unless (gst_rtsp_client_handle_message (client,
153 &request) == GST_RTSP_OK);
155 gst_rtsp_message_unset (&request);
157 /* OPTIONS with unknown session id */
158 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
159 "rtsp://localhost/test") == GST_RTSP_OK);
160 str = g_strdup_printf ("%d", cseq);
161 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
163 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SESSION, "foobar");
165 gst_rtsp_client_set_send_func (client, test_response_454, NULL, NULL);
166 fail_unless (gst_rtsp_client_handle_message (client,
167 &request) == GST_RTSP_OK);
169 gst_rtsp_message_unset (&request);
171 g_object_unref (client);
177 test_option_response_200 (GstRTSPClient * client, GstRTSPMessage * response,
178 gboolean close, gpointer user_data)
180 GstRTSPStatusCode code;
182 GstRTSPVersion version;
184 GstRTSPMethod methods;
186 fail_unless (gst_rtsp_message_get_type (response) ==
187 GST_RTSP_MESSAGE_RESPONSE);
189 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
192 fail_unless (code == GST_RTSP_STS_OK);
193 fail_unless (g_str_equal (reason, "OK"));
194 fail_unless (version == GST_RTSP_VERSION_1_0);
196 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
198 fail_unless (atoi (str) == cseq++);
200 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_PUBLIC, &str,
203 methods = gst_rtsp_options_from_text (str);
204 fail_if (methods == 0);
205 fail_unless (methods == (GST_RTSP_DESCRIBE |
210 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN));
215 GST_START_TEST (test_options)
217 GstRTSPClient *client;
218 GstRTSPMessage request = { 0, };
221 client = gst_rtsp_client_new ();
224 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
225 "rtsp://localhost/test") == GST_RTSP_OK);
226 str = g_strdup_printf ("%d", cseq);
227 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
230 gst_rtsp_client_set_send_func (client, test_option_response_200, NULL, NULL);
231 fail_unless (gst_rtsp_client_handle_message (client,
232 &request) == GST_RTSP_OK);
233 gst_rtsp_message_unset (&request);
235 g_object_unref (client);
240 GST_START_TEST (test_describe)
242 GstRTSPClient *client;
243 GstRTSPMessage request = { 0, };
245 GstRTSPUrl *uri_client;
248 client = gst_rtsp_client_new ();
250 /* simple DESCRIBE for non-existing url */
251 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
252 "rtsp://localhost/test") == GST_RTSP_OK);
253 str = g_strdup_printf ("%d", cseq);
254 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
257 gst_rtsp_client_set_send_func (client, test_response_404, NULL, NULL);
258 fail_unless (gst_rtsp_client_handle_message (client,
259 &request) == GST_RTSP_OK);
260 gst_rtsp_message_unset (&request);
262 uri_client = gst_rtsp_client_get_uri (client);
263 fail_unless (uri_client == NULL);
264 gst_rtsp_url_free (uri_client);
267 g_object_unref (client);
269 /* simple DESCRIBE for an existing url */
270 client = setup_client ();
271 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
272 "rtsp://localhost/test") == GST_RTSP_OK);
273 str = g_strdup_printf ("%d", cseq);
274 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
277 gst_rtsp_client_set_send_func (client, test_response_200, NULL, NULL);
278 fail_unless (gst_rtsp_client_handle_message (client,
279 &request) == GST_RTSP_OK);
280 gst_rtsp_message_unset (&request);
282 uri_client = gst_rtsp_client_get_uri (client);
283 fail_unless (uri_client != NULL);
284 uri_str = gst_rtsp_url_get_request_uri (uri_client);
285 gst_rtsp_url_free (uri_client);
286 fail_unless (g_strcmp0 (uri_str, "rtsp://localhost/test") == 0);
289 g_object_unref (client);
294 gchar *expected_transport = NULL;;
297 test_setup_response_200_multicast (GstRTSPClient * client,
298 GstRTSPMessage * response, gboolean close, gpointer user_data)
300 GstRTSPStatusCode code;
302 GstRTSPVersion version;
304 GstRTSPSessionPool *session_pool;
305 GstRTSPSession *session;
307 fail_unless (expected_transport != NULL);
309 fail_unless (gst_rtsp_message_get_type (response) ==
310 GST_RTSP_MESSAGE_RESPONSE);
312 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
315 fail_unless (code == GST_RTSP_STS_OK);
316 fail_unless (g_str_equal (reason, "OK"));
317 fail_unless (version == GST_RTSP_VERSION_1_0);
319 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
321 fail_unless (atoi (str) == cseq++);
323 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT,
324 &str, 0) == GST_RTSP_OK);
326 fail_unless (!strcmp (str, expected_transport));
328 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION,
329 &str, 0) == GST_RTSP_OK);
331 session_pool = gst_rtsp_client_get_session_pool (client);
332 fail_unless (session_pool != NULL);
334 fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 1);
335 session = gst_rtsp_session_pool_find (session_pool, str);
336 fail_unless (session != NULL);
337 g_object_unref (session);
339 g_object_unref (session_pool);
345 static GstRTSPClient *
346 setup_multicast_client (void)
348 GstRTSPClient *client;
349 GstRTSPSessionPool *session_pool;
350 GstRTSPMountPoints *mount_points;
351 GstRTSPMediaFactory *factory;
352 GstRTSPAddressPool *address_pool;
354 client = gst_rtsp_client_new ();
356 session_pool = gst_rtsp_session_pool_new ();
357 gst_rtsp_client_set_session_pool (client, session_pool);
359 mount_points = gst_rtsp_mount_points_new ();
360 factory = gst_rtsp_media_factory_new ();
361 gst_rtsp_media_factory_set_launch (factory,
362 "audiotestsrc ! audio/x-raw,rate=44100 ! audioconvert ! rtpL16pay name=pay0");
363 address_pool = gst_rtsp_address_pool_new ();
364 fail_unless (gst_rtsp_address_pool_add_range (address_pool,
365 "233.252.0.1", "233.252.0.1", 5000, 5010, 1));
366 gst_rtsp_media_factory_set_address_pool (factory, address_pool);
367 gst_rtsp_mount_points_add_factory (mount_points, "/test", factory);
368 gst_rtsp_client_set_mount_points (client, mount_points);
370 g_object_unref (mount_points);
371 g_object_unref (session_pool);
372 g_object_unref (address_pool);
377 GST_START_TEST (test_client_multicast_transport_404)
379 GstRTSPClient *client;
380 GstRTSPMessage request = { 0, };
383 client = setup_multicast_client ();
385 /* simple SETUP for non-existing url */
386 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
387 "rtsp://localhost/test2/stream=0") == GST_RTSP_OK);
388 str = g_strdup_printf ("%d", cseq);
389 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
390 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
391 "RTP/AVP;multicast");
393 gst_rtsp_client_set_send_func (client, test_response_404, NULL, NULL);
394 fail_unless (gst_rtsp_client_handle_message (client,
395 &request) == GST_RTSP_OK);
396 gst_rtsp_message_unset (&request);
398 g_object_unref (client);
403 GST_START_TEST (test_client_multicast_transport)
405 GstRTSPClient *client;
406 GstRTSPMessage request = { 0, };
409 client = setup_multicast_client ();
411 /* simple SETUP with a valid URI and multicast */
412 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
413 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
414 str = g_strdup_printf ("%d", cseq);
415 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
416 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
417 "RTP/AVP;multicast");
419 expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
420 "ttl=1;port=5000-5001;mode=\"PLAY\"";
421 gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
423 fail_unless (gst_rtsp_client_handle_message (client,
424 &request) == GST_RTSP_OK);
425 gst_rtsp_message_unset (&request);
426 expected_transport = NULL;
428 g_object_unref (client);
433 GST_START_TEST (test_client_multicast_ignore_transport_specific)
435 GstRTSPClient *client;
436 GstRTSPMessage request = { 0, };
439 client = setup_multicast_client ();
441 /* simple SETUP with a valid URI and multicast and a specific dest,
443 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
444 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
445 str = g_strdup_printf ("%d", cseq);
446 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
447 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
448 "RTP/AVP;multicast;destination=233.252.0.2;ttl=2;port=5001-5006;");
450 expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
451 "ttl=1;port=5000-5001;mode=\"PLAY\"";
452 gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
454 fail_unless (gst_rtsp_client_handle_message (client,
455 &request) == GST_RTSP_OK);
456 gst_rtsp_message_unset (&request);
457 expected_transport = NULL;
459 g_object_unref (client);
465 test_setup_response_461 (GstRTSPClient * client,
466 GstRTSPMessage * response, gboolean close, gpointer user_data)
468 GstRTSPStatusCode code;
470 GstRTSPVersion version;
473 fail_unless (expected_transport == NULL);
475 fail_unless (gst_rtsp_message_get_type (response) ==
476 GST_RTSP_MESSAGE_RESPONSE);
478 fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
481 fail_unless (code == GST_RTSP_STS_UNSUPPORTED_TRANSPORT);
482 fail_unless (g_str_equal (reason, "Unsupported transport"));
483 fail_unless (version == GST_RTSP_VERSION_1_0);
485 fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
487 fail_unless (atoi (str) == cseq++);
493 GST_START_TEST (test_client_multicast_invalid_transport_specific)
495 GstRTSPClient *client;
496 GstRTSPMessage request = { 0, };
498 GstRTSPSessionPool *session_pool;
500 client = setup_multicast_client ();
502 gst_rtsp_client_set_use_client_settings (client, TRUE);
503 fail_unless (gst_rtsp_client_get_use_client_settings (client));
506 /* simple SETUP with a valid URI and multicast, but an invalid ip */
507 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
508 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
509 str = g_strdup_printf ("%d", cseq);
510 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
511 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
512 "RTP/AVP;multicast;destination=233.252.0.2;ttl=1;port=5000-5001;");
514 gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
515 fail_unless (gst_rtsp_client_handle_message (client,
516 &request) == GST_RTSP_OK);
517 gst_rtsp_message_unset (&request);
519 session_pool = gst_rtsp_client_get_session_pool (client);
520 fail_unless (session_pool != NULL);
521 /* FIXME: There seems to be a leak of a session here ! */
522 /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
523 g_object_unref (session_pool);
527 /* simple SETUP with a valid URI and multicast, but an invalid prt */
528 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
529 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
530 str = g_strdup_printf ("%d", cseq);
531 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
532 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
533 "RTP/AVP;multicast;destination=233.252.0.1;ttl=1;port=6000-6001;");
535 gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
536 fail_unless (gst_rtsp_client_handle_message (client,
537 &request) == GST_RTSP_OK);
538 gst_rtsp_message_unset (&request);
540 session_pool = gst_rtsp_client_get_session_pool (client);
541 fail_unless (session_pool != NULL);
542 /* FIXME: There seems to be a leak of a session here ! */
543 /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
544 g_object_unref (session_pool);
548 /* simple SETUP with a valid URI and multicast, but an invalid ttl */
549 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
550 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
551 str = g_strdup_printf ("%d", cseq);
552 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
553 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
554 "RTP/AVP;multicast;destination=233.252.0.1;ttl=2;port=5000-5001;");
556 gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
557 fail_unless (gst_rtsp_client_handle_message (client,
558 &request) == GST_RTSP_OK);
559 gst_rtsp_message_unset (&request);
561 session_pool = gst_rtsp_client_get_session_pool (client);
562 fail_unless (session_pool != NULL);
563 /* FIXME: There seems to be a leak of a session here ! */
564 /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
565 g_object_unref (session_pool);
568 g_object_unref (client);
573 GST_START_TEST (test_client_multicast_transport_specific)
575 GstRTSPClient *client;
576 GstRTSPMessage request = { 0, };
578 GstRTSPSessionPool *session_pool;
580 client = setup_multicast_client ();
582 gst_rtsp_client_set_use_client_settings (client, TRUE);
583 fail_unless (gst_rtsp_client_get_use_client_settings (client));
585 expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
586 "ttl=1;port=5000-5001;mode=\"PLAY\"";
588 /* simple SETUP with a valid URI and multicast, but an invalid ip */
589 fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
590 "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
591 str = g_strdup_printf ("%d", cseq);
592 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
593 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
596 gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
598 fail_unless (gst_rtsp_client_handle_message (client,
599 &request) == GST_RTSP_OK);
600 gst_rtsp_message_unset (&request);
601 expected_transport = NULL;
603 gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
605 session_pool = gst_rtsp_client_get_session_pool (client);
606 fail_unless (session_pool != NULL);
607 fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 1);
608 g_object_unref (session_pool);
610 g_object_unref (client);
617 rtspclient_suite (void)
619 Suite *s = suite_create ("rtspclient");
620 TCase *tc = tcase_create ("general");
622 suite_add_tcase (s, tc);
623 tcase_set_timeout (tc, 20);
624 tcase_add_test (tc, test_request);
625 tcase_add_test (tc, test_options);
626 tcase_add_test (tc, test_describe);
627 tcase_add_test (tc, test_client_multicast_transport_404);
628 tcase_add_test (tc, test_client_multicast_transport);
629 tcase_add_test (tc, test_client_multicast_ignore_transport_specific);
630 tcase_add_test (tc, test_client_multicast_invalid_transport_specific);
631 tcase_add_test (tc, test_client_multicast_transport_specific);
636 GST_CHECK_MAIN (rtspclient);