1 /* GStreamer unit test for GstRTSPServer
2 * Copyright (C) 2012 Axis Communications <dev-gstreamer at axis dot com>
3 * @author David Svensson Fors <davidsf at axis dot com>
4 * Copyright (C) 2015 Centricular Ltd
5 * @author Tim-Philipp Müller <tim@centricular.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #include <gst/check/gstcheck.h>
24 #include <gst/sdp/gstsdpmessage.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/rtp/gstrtcpbuffer.h>
29 #include <netinet/in.h>
31 #include "rtsp-server.h"
33 #define VIDEO_PIPELINE "videotestsrc ! " \
34 "video/x-raw,width=352,height=288 ! " \
35 "rtpgstpay name=pay0 pt=96"
36 #define AUDIO_PIPELINE "audiotestsrc ! " \
37 "audio/x-raw,rate=8000 ! " \
38 "rtpgstpay name=pay1 pt=97"
40 #define TEST_MOUNT_POINT "/test"
41 #define TEST_PROTO "RTP/AVP"
42 #define TEST_ENCODING "X-GST"
43 #define TEST_CLOCK_RATE "90000"
45 /* tested rtsp server */
46 static GstRTSPServer *server = NULL;
48 /* tcp port that the test server listens for rtsp requests on */
49 static gint test_port = 0;
51 /* id of the server's source within the GMainContext */
52 static guint source_id;
54 /* iterate the default main loop until there are no events to dispatch */
58 while (g_main_context_iteration (NULL, FALSE)) {
59 GST_DEBUG ("iteration");
64 get_client_ports_full (GstRTSPRange * range, GSocket ** rtp_socket,
65 GSocket ** rtcp_socket)
71 GInetAddress *anyaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
72 GSocketAddress *sockaddr;
79 rtp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
80 G_SOCKET_PROTOCOL_UDP, NULL);
81 fail_unless (rtp != NULL);
83 sockaddr = g_inet_socket_address_new (anyaddr, rtp_port);
84 fail_unless (sockaddr != NULL);
85 bound = g_socket_bind (rtp, sockaddr, FALSE, NULL);
86 g_object_unref (sockaddr);
92 sockaddr = g_socket_get_local_address (rtp, NULL);
93 fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
95 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
96 g_object_unref (sockaddr);
98 if (rtp_port % 2 != 0) {
100 g_object_unref (rtp);
104 rtcp_port = rtp_port + 1;
106 rtcp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
107 G_SOCKET_PROTOCOL_UDP, NULL);
108 fail_unless (rtcp != NULL);
110 sockaddr = g_inet_socket_address_new (anyaddr, rtcp_port);
111 fail_unless (sockaddr != NULL);
112 bound = g_socket_bind (rtcp, sockaddr, FALSE, NULL);
113 g_object_unref (sockaddr);
115 g_object_unref (rtp);
116 g_object_unref (rtcp);
120 sockaddr = g_socket_get_local_address (rtcp, NULL);
121 fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
122 fail_unless (rtcp_port ==
123 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr)));
124 g_object_unref (sockaddr);
129 range->min = rtp_port;
130 range->max = rtcp_port;
134 g_object_unref (rtp);
138 g_object_unref (rtcp);
139 GST_DEBUG ("client_port=%d-%d", range->min, range->max);
140 g_object_unref (anyaddr);
143 /* get a free rtp/rtcp client port pair */
145 get_client_ports (GstRTSPRange * range)
147 get_client_ports_full (range, NULL, NULL);
150 /* start the tested rtsp server */
154 GstRTSPMountPoints *mounts;
156 GstRTSPMediaFactory *factory;
157 GstRTSPAddressPool *pool;
159 mounts = gst_rtsp_server_get_mount_points (server);
161 factory = gst_rtsp_media_factory_new ();
163 gst_rtsp_media_factory_set_launch (factory,
164 "( " VIDEO_PIPELINE " " AUDIO_PIPELINE " )");
165 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
166 g_object_unref (mounts);
168 /* use an address pool for multicast */
169 pool = gst_rtsp_address_pool_new ();
170 gst_rtsp_address_pool_add_range (pool,
171 "224.3.0.0", "224.3.0.10", 5000, 5010, 16);
172 gst_rtsp_media_factory_set_address_pool (factory, pool);
173 gst_object_unref (pool);
175 /* set port to any */
176 gst_rtsp_server_set_service (server, "0");
178 /* attach to default main context */
179 source_id = gst_rtsp_server_attach (server, NULL);
180 fail_if (source_id == 0);
183 service = gst_rtsp_server_get_service (server);
184 test_port = atoi (service);
185 fail_unless (test_port != 0);
188 GST_DEBUG ("rtsp server listening on port %d", test_port);
191 /* start the testing rtsp server for RECORD mode */
192 static GstRTSPMediaFactory *
193 start_record_server (const gchar * launch_line)
195 GstRTSPMediaFactory *factory;
196 GstRTSPMountPoints *mounts;
199 mounts = gst_rtsp_server_get_mount_points (server);
201 factory = gst_rtsp_media_factory_new ();
203 gst_rtsp_media_factory_set_transport_mode (factory,
204 GST_RTSP_TRANSPORT_MODE_RECORD);
205 gst_rtsp_media_factory_set_launch (factory, launch_line);
206 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
207 g_object_unref (mounts);
209 /* set port to any */
210 gst_rtsp_server_set_service (server, "0");
212 /* attach to default main context */
213 source_id = gst_rtsp_server_attach (server, NULL);
214 fail_if (source_id == 0);
217 service = gst_rtsp_server_get_service (server);
218 test_port = atoi (service);
219 fail_unless (test_port != 0);
222 GST_DEBUG ("rtsp server listening on port %d", test_port);
226 /* stop the tested rtsp server */
230 g_source_remove (source_id);
233 GST_DEBUG ("rtsp server stopped");
236 /* create an rtsp connection to the server on test_port */
237 static GstRTSPConnection *
238 connect_to_server (gint port, const gchar * mount_point)
240 GstRTSPConnection *conn = NULL;
243 GstRTSPUrl *url = NULL;
245 address = gst_rtsp_server_get_address (server);
246 uri_string = g_strdup_printf ("rtsp://%s:%d%s", address, port, mount_point);
248 fail_unless (gst_rtsp_url_parse (uri_string, &url) == GST_RTSP_OK);
251 fail_unless (gst_rtsp_connection_create (url, &conn) == GST_RTSP_OK);
252 gst_rtsp_url_free (url);
254 fail_unless (gst_rtsp_connection_connect (conn, NULL) == GST_RTSP_OK);
259 /* create an rtsp request */
260 static GstRTSPMessage *
261 create_request (GstRTSPConnection * conn, GstRTSPMethod method,
262 const gchar * control)
264 GstRTSPMessage *request = NULL;
268 base_uri = gst_rtsp_url_get_request_uri (gst_rtsp_connection_get_url (conn));
269 full_uri = g_strdup_printf ("%s/%s", base_uri, control ? control : "");
271 if (gst_rtsp_message_new_request (&request, method, full_uri) != GST_RTSP_OK) {
272 GST_DEBUG ("failed to create request object");
280 /* send an rtsp request */
282 send_request (GstRTSPConnection * conn, GstRTSPMessage * request)
284 if (gst_rtsp_connection_send (conn, request, NULL) != GST_RTSP_OK) {
285 GST_DEBUG ("failed to send request");
291 /* read rtsp response. response must be freed by the caller */
292 static GstRTSPMessage *
293 read_response (GstRTSPConnection * conn)
295 GstRTSPMessage *response = NULL;
297 if (gst_rtsp_message_new (&response) != GST_RTSP_OK) {
298 GST_DEBUG ("failed to create response object");
301 if (gst_rtsp_connection_receive (conn, response, NULL) != GST_RTSP_OK) {
302 GST_DEBUG ("failed to read response");
303 gst_rtsp_message_free (response);
306 fail_unless (gst_rtsp_message_get_type (response) ==
307 GST_RTSP_MESSAGE_RESPONSE);
311 /* send an rtsp request and receive response. gchar** parameters are out
312 * parameters that have to be freed by the caller */
313 static GstRTSPStatusCode
314 do_request_full (GstRTSPConnection * conn, GstRTSPMethod method,
315 const gchar * control, const gchar * session_in, const gchar * transport_in,
316 const gchar * range_in, const gchar * require_in,
317 gchar ** content_type, gchar ** content_base, gchar ** body,
318 gchar ** session_out, gchar ** transport_out, gchar ** range_out,
319 gchar ** unsupported_out)
321 GstRTSPMessage *request;
322 GstRTSPMessage *response;
323 GstRTSPStatusCode code;
327 request = create_request (conn, method, control);
331 gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session_in);
334 gst_rtsp_message_add_header (request, GST_RTSP_HDR_TRANSPORT, transport_in);
337 gst_rtsp_message_add_header (request, GST_RTSP_HDR_RANGE, range_in);
340 gst_rtsp_message_add_header (request, GST_RTSP_HDR_REQUIRE, require_in);
344 fail_unless (send_request (conn, request));
345 gst_rtsp_message_free (request);
350 response = read_response (conn);
352 /* check status line */
353 gst_rtsp_message_parse_response (response, &code, NULL, NULL);
354 if (code != GST_RTSP_STS_OK) {
355 if (unsupported_out != NULL && code == GST_RTSP_STS_OPTION_NOT_SUPPORTED) {
356 gst_rtsp_message_get_header (response, GST_RTSP_HDR_UNSUPPORTED,
358 *unsupported_out = g_strdup (value);
360 gst_rtsp_message_free (response);
364 /* get information from response */
366 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_TYPE,
368 *content_type = g_strdup (value);
371 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
373 *content_base = g_strdup (value);
376 *body = g_malloc (response->body_size + 1);
377 strncpy (*body, (gchar *) response->body, response->body_size);
380 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &value, 0);
382 value = g_strdup (value);
384 /* Remove the timeout */
386 char *pos = strchr (value, ';');
391 /* check that we got the same session back */
392 fail_unless (!g_strcmp0 (value, session_in));
394 *session_out = value;
397 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &value, 0);
398 *transport_out = g_strdup (value);
401 gst_rtsp_message_get_header (response, GST_RTSP_HDR_RANGE, &value, 0);
402 *range_out = g_strdup (value);
405 gst_rtsp_message_free (response);
409 /* send an rtsp request and receive response. gchar** parameters are out
410 * parameters that have to be freed by the caller */
411 static GstRTSPStatusCode
412 do_request (GstRTSPConnection * conn, GstRTSPMethod method,
413 const gchar * control, const gchar * session_in,
414 const gchar * transport_in, const gchar * range_in,
415 gchar ** content_type, gchar ** content_base, gchar ** body,
416 gchar ** session_out, gchar ** transport_out, gchar ** range_out)
418 return do_request_full (conn, method, control, session_in, transport_in,
419 range_in, NULL, content_type, content_base, body, session_out,
420 transport_out, range_out, NULL);
423 /* send an rtsp request with a method and a session, and receive response */
424 static GstRTSPStatusCode
425 do_simple_request (GstRTSPConnection * conn, GstRTSPMethod method,
426 const gchar * session)
428 return do_request (conn, method, NULL, session, NULL, NULL, NULL,
429 NULL, NULL, NULL, NULL, NULL);
432 /* send a DESCRIBE request and receive response. returns a received
433 * GstSDPMessage that must be freed by the caller */
434 static GstSDPMessage *
435 do_describe (GstRTSPConnection * conn, const gchar * mount_point)
437 GstSDPMessage *sdp_message;
438 gchar *content_type = NULL;
439 gchar *content_base = NULL;
442 gchar *expected_content_base;
444 /* send DESCRIBE request */
445 fail_unless (do_request (conn, GST_RTSP_DESCRIBE, NULL, NULL, NULL, NULL,
446 &content_type, &content_base, &body, NULL, NULL, NULL) ==
449 /* check response values */
450 fail_unless (!g_strcmp0 (content_type, "application/sdp"));
451 address = gst_rtsp_server_get_address (server);
452 expected_content_base =
453 g_strdup_printf ("rtsp://%s:%d%s/", address, test_port, mount_point);
454 fail_unless (!g_strcmp0 (content_base, expected_content_base));
456 /* create sdp message */
457 fail_unless (gst_sdp_message_new (&sdp_message) == GST_SDP_OK);
458 fail_unless (gst_sdp_message_parse_buffer ((guint8 *) body,
459 strlen (body), sdp_message) == GST_SDP_OK);
462 g_free (content_type);
463 g_free (content_base);
466 g_free (expected_content_base);
471 /* send a SETUP request and receive response. if *session is not NULL,
472 * it is used in the request. otherwise, *session is set to a returned
473 * session string that must be freed by the caller. the returned
474 * transport must be freed by the caller. */
475 static GstRTSPStatusCode
476 do_setup_full (GstRTSPConnection * conn, const gchar * control,
477 GstRTSPLowerTrans lower_transport, const GstRTSPRange * client_ports,
478 const gchar * require, gchar ** session, GstRTSPTransport ** transport,
479 gchar ** unsupported)
481 GstRTSPStatusCode code;
482 gchar *session_in = NULL;
483 GString *transport_string_in = NULL;
484 gchar **session_out = NULL;
485 gchar *transport_string_out = NULL;
487 /* prepare and send SETUP request */
490 session_in = *session;
492 session_out = session;
496 transport_string_in = g_string_new (TEST_PROTO);
497 switch (lower_transport) {
498 case GST_RTSP_LOWER_TRANS_UDP:
499 transport_string_in =
500 g_string_append (transport_string_in, "/UDP;unicast");
502 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
503 transport_string_in =
504 g_string_append (transport_string_in, "/UDP;multicast");
506 case GST_RTSP_LOWER_TRANS_TCP:
507 transport_string_in =
508 g_string_append (transport_string_in, "/TCP;unicast");
511 g_assert_not_reached ();
516 g_string_append_printf (transport_string_in, ";client_port=%d-%d",
517 client_ports->min, client_ports->max);
521 do_request_full (conn, GST_RTSP_SETUP, control, session_in,
522 transport_string_in->str, NULL, require, NULL, NULL, NULL, session_out,
523 &transport_string_out, NULL, unsupported);
524 g_string_free (transport_string_in, TRUE);
526 if (transport_string_out) {
527 /* create transport */
528 fail_unless (gst_rtsp_transport_new (transport) == GST_RTSP_OK);
529 fail_unless (gst_rtsp_transport_parse (transport_string_out,
530 *transport) == GST_RTSP_OK);
531 g_free (transport_string_out);
533 GST_INFO ("code=%d", code);
537 /* send a SETUP request and receive response. if *session is not NULL,
538 * it is used in the request. otherwise, *session is set to a returned
539 * session string that must be freed by the caller. the returned
540 * transport must be freed by the caller. */
541 static GstRTSPStatusCode
542 do_setup (GstRTSPConnection * conn, const gchar * control,
543 const GstRTSPRange * client_ports, gchar ** session,
544 GstRTSPTransport ** transport)
546 return do_setup_full (conn, control, GST_RTSP_LOWER_TRANS_UDP, client_ports,
547 NULL, session, transport, NULL);
550 /* fixture setup function */
554 server = gst_rtsp_server_new ();
557 /* fixture clean-up function */
562 g_object_unref (server);
568 GST_START_TEST (test_connect)
570 GstRTSPConnection *conn;
574 /* connect to server */
575 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
578 gst_rtsp_connection_free (conn);
581 /* iterate so the clean-up can finish */
587 GST_START_TEST (test_describe)
589 GstRTSPConnection *conn;
590 GstSDPMessage *sdp_message = NULL;
591 const GstSDPMedia *sdp_media;
593 gchar *expected_rtpmap;
595 const gchar *control_video;
596 const gchar *control_audio;
600 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
602 /* send DESCRIBE request */
603 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
605 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
607 /* check video sdp */
608 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
609 fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
610 fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
611 sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
614 g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
615 rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
616 fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
617 g_free (expected_rtpmap);
618 control_video = gst_sdp_media_get_attribute_val (sdp_media, "control");
619 fail_unless (!g_strcmp0 (control_video, "stream=0"));
621 /* check audio sdp */
622 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
623 fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
624 fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
625 sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
628 g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
629 rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
630 fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
631 g_free (expected_rtpmap);
632 control_audio = gst_sdp_media_get_attribute_val (sdp_media, "control");
633 fail_unless (!g_strcmp0 (control_audio, "stream=1"));
635 /* clean up and iterate so the clean-up can finish */
636 gst_sdp_message_free (sdp_message);
637 gst_rtsp_connection_free (conn);
644 GST_START_TEST (test_describe_record_media)
646 GstRTSPConnection *conn;
648 start_record_server ("( fakesink name=depay0 )");
650 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
652 /* send DESCRIBE request */
653 fail_unless_equals_int (do_request (conn, GST_RTSP_DESCRIBE, NULL, NULL, NULL,
654 NULL, NULL, NULL, NULL, NULL, NULL, NULL),
655 GST_RTSP_STS_METHOD_NOT_ALLOWED);
657 /* clean up and iterate so the clean-up can finish */
658 gst_rtsp_connection_free (conn);
665 GST_START_TEST (test_describe_non_existing_mount_point)
667 GstRTSPConnection *conn;
671 /* send DESCRIBE request for a non-existing mount point
672 * and check that we get a 404 Not Found */
673 conn = connect_to_server (test_port, "/non-existing");
674 fail_unless (do_simple_request (conn, GST_RTSP_DESCRIBE, NULL)
675 == GST_RTSP_STS_NOT_FOUND);
677 /* clean up and iterate so the clean-up can finish */
678 gst_rtsp_connection_free (conn);
686 do_test_setup (GstRTSPLowerTrans lower_transport)
688 GstRTSPConnection *conn;
689 GstSDPMessage *sdp_message = NULL;
690 const GstSDPMedia *sdp_media;
691 const gchar *video_control;
692 const gchar *audio_control;
693 GstRTSPRange client_ports = { 0 };
694 gchar *session = NULL;
695 GstRTSPTransport *video_transport = NULL;
696 GstRTSPTransport *audio_transport = NULL;
700 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
702 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
704 /* get control strings from DESCRIBE response */
705 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
706 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
707 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
708 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
709 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
711 get_client_ports (&client_ports);
713 /* send SETUP request for video */
714 fail_unless (do_setup_full (conn, video_control, lower_transport,
715 &client_ports, NULL, &session, &video_transport,
716 NULL) == GST_RTSP_STS_OK);
717 GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
719 /* check response from SETUP */
720 fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
721 fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
722 fail_unless (video_transport->lower_transport == lower_transport);
723 fail_unless (video_transport->mode_play);
724 gst_rtsp_transport_free (video_transport);
726 /* send SETUP request for audio */
727 fail_unless (do_setup_full (conn, audio_control, lower_transport,
728 &client_ports, NULL, &session, &audio_transport,
729 NULL) == GST_RTSP_STS_OK);
730 GST_DEBUG ("set up audio %s with session '%s'", audio_control, session);
732 /* check response from SETUP */
733 fail_unless (audio_transport->trans == GST_RTSP_TRANS_RTP);
734 fail_unless (audio_transport->profile == GST_RTSP_PROFILE_AVP);
735 fail_unless (audio_transport->lower_transport == lower_transport);
736 fail_unless (audio_transport->mode_play);
737 gst_rtsp_transport_free (audio_transport);
739 /* send TEARDOWN request and check that we get 200 OK */
740 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
741 session) == GST_RTSP_STS_OK);
743 /* clean up and iterate so the clean-up can finish */
745 gst_sdp_message_free (sdp_message);
746 gst_rtsp_connection_free (conn);
751 GST_START_TEST (test_setup_udp)
753 do_test_setup (GST_RTSP_LOWER_TRANS_UDP);
758 GST_START_TEST (test_setup_tcp)
760 do_test_setup (GST_RTSP_LOWER_TRANS_TCP);
765 GST_START_TEST (test_setup_udp_mcast)
767 do_test_setup (GST_RTSP_LOWER_TRANS_UDP_MCAST);
772 GST_START_TEST (test_setup_twice)
774 GstRTSPConnection *conn;
775 GstSDPMessage *sdp_message;
776 const GstSDPMedia *sdp_media;
777 const gchar *video_control;
778 GstRTSPRange client_ports;
779 GstRTSPTransport *video_transport = NULL;
780 gchar *session1 = NULL;
781 gchar *session2 = NULL;
785 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
787 /* we wan't more than one session for this connection */
788 gst_rtsp_connection_set_remember_session_id (conn, FALSE);
790 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
792 /* get the control url */
793 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
794 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
795 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
797 get_client_ports (&client_ports);
799 /* send SETUP request for one session */
800 fail_unless (do_setup (conn, video_control, &client_ports, &session1,
801 &video_transport) == GST_RTSP_STS_OK);
802 GST_DEBUG ("set up video %s, got session '%s'", video_control, session1);
804 /* check response from SETUP */
805 fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
806 fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
807 fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
808 fail_unless (video_transport->mode_play);
809 gst_rtsp_transport_free (video_transport);
811 /* send SETUP request for another session */
812 fail_unless (do_setup (conn, video_control, &client_ports, &session2,
813 &video_transport) == GST_RTSP_STS_OK);
814 GST_DEBUG ("set up video %s, got session '%s'", video_control, session2);
816 /* check response from SETUP */
817 fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
818 fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
819 fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
820 fail_unless (video_transport->mode_play);
821 gst_rtsp_transport_free (video_transport);
823 /* session can not be the same */
824 fail_unless (strcmp (session1, session2));
826 /* send TEARDOWN request for the first session */
827 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
828 session1) == GST_RTSP_STS_OK);
830 /* send TEARDOWN request for the second session */
831 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
832 session2) == GST_RTSP_STS_OK);
836 gst_sdp_message_free (sdp_message);
837 gst_rtsp_connection_free (conn);
844 GST_START_TEST (test_setup_with_require_header)
846 GstRTSPConnection *conn;
847 GstSDPMessage *sdp_message = NULL;
848 const GstSDPMedia *sdp_media;
849 const gchar *video_control;
850 GstRTSPRange client_ports;
851 gchar *session = NULL;
852 gchar *unsupported = NULL;
853 GstRTSPTransport *video_transport = NULL;
857 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
859 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
861 /* get control strings from DESCRIBE response */
862 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
863 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
864 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
866 get_client_ports (&client_ports);
868 /* send SETUP request for video, with single Require header */
869 fail_unless_equals_int (do_setup_full (conn, video_control,
870 GST_RTSP_LOWER_TRANS_UDP, &client_ports, "funky-feature", &session,
871 &video_transport, &unsupported), GST_RTSP_STS_OPTION_NOT_SUPPORTED);
872 fail_unless_equals_string (unsupported, "funky-feature");
873 g_free (unsupported);
876 /* send SETUP request for video, with multiple Require headers */
877 fail_unless_equals_int (do_setup_full (conn, video_control,
878 GST_RTSP_LOWER_TRANS_UDP, &client_ports,
879 "funky-feature, foo-bar, superburst", &session, &video_transport,
880 &unsupported), GST_RTSP_STS_OPTION_NOT_SUPPORTED);
881 fail_unless_equals_string (unsupported, "funky-feature, foo-bar, superburst");
882 g_free (unsupported);
885 /* ok, just do a normal setup then (make sure that still works) */
886 fail_unless_equals_int (do_setup (conn, video_control, &client_ports,
887 &session, &video_transport), GST_RTSP_STS_OK);
889 GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
891 /* check response from SETUP */
892 fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
893 fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
894 fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
895 fail_unless (video_transport->mode_play);
896 gst_rtsp_transport_free (video_transport);
898 /* send TEARDOWN request and check that we get 200 OK */
899 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
900 session) == GST_RTSP_STS_OK);
902 /* clean up and iterate so the clean-up can finish */
904 gst_sdp_message_free (sdp_message);
905 gst_rtsp_connection_free (conn);
912 GST_START_TEST (test_setup_non_existing_stream)
914 GstRTSPConnection *conn;
915 GstRTSPRange client_ports;
919 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
921 get_client_ports (&client_ports);
923 /* send SETUP request with a non-existing stream and check that we get a
925 fail_unless (do_setup (conn, "stream=7", &client_ports, NULL,
926 NULL) == GST_RTSP_STS_NOT_FOUND);
928 /* clean up and iterate so the clean-up can finish */
929 gst_rtsp_connection_free (conn);
937 receive_rtp (GSocket * socket, GSocketAddress ** addr)
939 GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
943 GstMapInfo map = GST_MAP_INFO_INIT;
944 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
946 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
947 bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
948 map.maxsize, NULL, NULL);
949 fail_unless (bytes > 0);
950 gst_buffer_unmap (buffer, &map);
951 gst_buffer_set_size (buffer, bytes);
953 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer)) {
954 gst_rtp_buffer_unmap (&rtpbuffer);
959 g_clear_object (addr);
962 gst_buffer_unref (buffer);
966 receive_rtcp (GSocket * socket, GSocketAddress ** addr, GstRTCPType type)
968 GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
972 GstMapInfo map = GST_MAP_INFO_INIT;
974 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
975 bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
976 map.maxsize, NULL, NULL);
977 fail_unless (bytes > 0);
978 gst_buffer_unmap (buffer, &map);
979 gst_buffer_set_size (buffer, bytes);
981 if (gst_rtcp_buffer_validate (buffer)) {
982 GstRTCPBuffer rtcpbuffer = GST_RTCP_BUFFER_INIT;
983 GstRTCPPacket packet;
986 fail_unless (gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcpbuffer));
987 fail_unless (gst_rtcp_buffer_get_first_packet (&rtcpbuffer, &packet));
989 if (gst_rtcp_packet_get_type (&packet) == type) {
990 gst_rtcp_buffer_unmap (&rtcpbuffer);
993 } while (gst_rtcp_packet_move_to_next (&packet));
994 gst_rtcp_buffer_unmap (&rtcpbuffer);
1001 g_clear_object (addr);
1006 gst_buffer_unref (buffer);
1010 do_test_play_full (const gchar * range, GstRTSPLowerTrans lower_transport)
1012 GstRTSPConnection *conn;
1013 GstSDPMessage *sdp_message = NULL;
1014 const GstSDPMedia *sdp_media;
1015 const gchar *video_control;
1016 const gchar *audio_control;
1017 GstRTSPRange client_port;
1018 gchar *session = NULL;
1019 GstRTSPTransport *video_transport = NULL;
1020 GstRTSPTransport *audio_transport = NULL;
1021 GSocket *rtp_socket, *rtcp_socket;
1022 gchar *range_out = NULL;
1024 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1026 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1028 /* get control strings from DESCRIBE response */
1029 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1030 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1031 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1032 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1033 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1035 get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
1037 /* do SETUP for video and audio */
1038 fail_unless (do_setup_full (conn, video_control, lower_transport,
1039 &client_port, NULL, &session, &video_transport,
1040 NULL) == GST_RTSP_STS_OK);
1041 fail_unless (do_setup_full (conn, audio_control, lower_transport,
1042 &client_port, NULL, &session, &audio_transport,
1043 NULL) == GST_RTSP_STS_OK);
1045 /* send PLAY request and check that we get 200 OK */
1046 fail_unless (do_request (conn, GST_RTSP_PLAY, NULL, session, NULL, range,
1047 NULL, NULL, NULL, NULL, NULL, &range_out) == GST_RTSP_STS_OK);
1049 fail_unless_equals_string (range, range_out);
1052 receive_rtp (rtp_socket, NULL);
1053 receive_rtcp (rtcp_socket, NULL, 0);
1055 /* send TEARDOWN request and check that we get 200 OK */
1056 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1057 session) == GST_RTSP_STS_OK);
1059 /* FIXME: The rtsp-server always disconnects the transport before
1060 * sending the RTCP BYE
1061 * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
1064 /* clean up and iterate so the clean-up can finish */
1065 g_object_unref (rtp_socket);
1066 g_object_unref (rtcp_socket);
1068 gst_rtsp_transport_free (video_transport);
1069 gst_rtsp_transport_free (audio_transport);
1070 gst_sdp_message_free (sdp_message);
1071 gst_rtsp_connection_free (conn);
1075 do_test_play (const gchar * range)
1077 do_test_play_full (range, GST_RTSP_LOWER_TRANS_UDP);
1080 GST_START_TEST (test_play)
1084 do_test_play (NULL);
1092 GST_START_TEST (test_play_without_session)
1094 GstRTSPConnection *conn;
1098 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1100 /* send PLAY request without a session and check that we get a
1101 * 454 Session Not Found */
1102 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1103 NULL) == GST_RTSP_STS_SESSION_NOT_FOUND);
1105 /* clean up and iterate so the clean-up can finish */
1106 gst_rtsp_connection_free (conn);
1113 GST_START_TEST (test_bind_already_in_use)
1115 GstRTSPServer *serv;
1116 GSocketService *service;
1117 GError *error = NULL;
1121 serv = gst_rtsp_server_new ();
1122 service = g_socket_service_new ();
1124 /* bind service to port */
1126 g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL,
1128 g_assert_no_error (error);
1130 port_str = g_strdup_printf ("%d\n", port);
1132 /* try to bind server to the same port */
1133 g_object_set (serv, "service", port_str, NULL);
1136 /* attach to default main context */
1137 fail_unless (gst_rtsp_server_attach (serv, NULL) == 0);
1140 g_object_unref (serv);
1141 g_socket_service_stop (service);
1142 g_object_unref (service);
1148 GST_START_TEST (test_play_multithreaded)
1150 GstRTSPThreadPool *pool;
1152 pool = gst_rtsp_server_get_thread_pool (server);
1153 gst_rtsp_thread_pool_set_max_threads (pool, 2);
1154 g_object_unref (pool);
1158 do_test_play (NULL);
1175 media_constructed_block (GstRTSPMediaFactory * factory,
1176 GstRTSPMedia * media, gpointer user_data)
1178 gint *block_state = user_data;
1180 g_mutex_lock (&check_mutex);
1182 *block_state = BLOCKED;
1183 g_cond_broadcast (&check_cond);
1185 while (*block_state != UNBLOCK)
1186 g_cond_wait (&check_cond, &check_mutex);
1187 g_mutex_unlock (&check_mutex);
1191 GST_START_TEST (test_play_multithreaded_block_in_describe)
1193 GstRTSPConnection *conn;
1194 GstRTSPMountPoints *mounts;
1195 GstRTSPMediaFactory *factory;
1196 gint block_state = BLOCK_ME;
1197 GstRTSPMessage *request;
1198 GstRTSPMessage *response;
1199 GstRTSPStatusCode code;
1200 GstRTSPThreadPool *pool;
1202 pool = gst_rtsp_server_get_thread_pool (server);
1203 gst_rtsp_thread_pool_set_max_threads (pool, 2);
1204 g_object_unref (pool);
1206 mounts = gst_rtsp_server_get_mount_points (server);
1207 fail_unless (mounts != NULL);
1208 factory = gst_rtsp_media_factory_new ();
1209 gst_rtsp_media_factory_set_launch (factory,
1210 "( " VIDEO_PIPELINE " " AUDIO_PIPELINE " )");
1211 g_signal_connect (factory, "media-constructed",
1212 G_CALLBACK (media_constructed_block), &block_state);
1213 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT "2", factory);
1214 g_object_unref (mounts);
1218 conn = connect_to_server (test_port, TEST_MOUNT_POINT "2");
1221 /* do describe, it will not return now as we've blocked it */
1222 request = create_request (conn, GST_RTSP_DESCRIBE, NULL);
1223 fail_unless (send_request (conn, request));
1224 gst_rtsp_message_free (request);
1226 g_mutex_lock (&check_mutex);
1227 while (block_state != BLOCKED)
1228 g_cond_wait (&check_cond, &check_mutex);
1229 g_mutex_unlock (&check_mutex);
1231 /* Do a second connection while the first one is blocked */
1232 do_test_play (NULL);
1234 /* Now unblock the describe */
1235 g_mutex_lock (&check_mutex);
1236 block_state = UNBLOCK;
1237 g_cond_broadcast (&check_cond);
1238 g_mutex_unlock (&check_mutex);
1240 response = read_response (conn);
1241 gst_rtsp_message_parse_response (response, &code, NULL, NULL);
1242 fail_unless (code == GST_RTSP_STS_OK);
1243 gst_rtsp_message_free (response);
1246 gst_rtsp_connection_free (conn);
1256 new_session_timeout_one (GstRTSPClient * client,
1257 GstRTSPSession * session, gpointer user_data)
1259 gst_rtsp_session_set_timeout (session, 1);
1261 g_signal_handlers_disconnect_by_func (client, new_session_timeout_one,
1266 session_connected_new_session_cb (GstRTSPServer * server,
1267 GstRTSPClient * client, gpointer user_data)
1270 g_signal_connect (client, "new-session", user_data, NULL);
1273 GST_START_TEST (test_play_multithreaded_timeout_client)
1275 GstRTSPConnection *conn;
1276 GstSDPMessage *sdp_message = NULL;
1277 const GstSDPMedia *sdp_media;
1278 const gchar *video_control;
1279 const gchar *audio_control;
1280 GstRTSPRange client_port;
1281 gchar *session = NULL;
1282 GstRTSPTransport *video_transport = NULL;
1283 GstRTSPTransport *audio_transport = NULL;
1284 GstRTSPSessionPool *pool;
1285 GstRTSPThreadPool *thread_pool;
1287 thread_pool = gst_rtsp_server_get_thread_pool (server);
1288 gst_rtsp_thread_pool_set_max_threads (thread_pool, 2);
1289 g_object_unref (thread_pool);
1291 pool = gst_rtsp_server_get_session_pool (server);
1292 g_signal_connect (server, "client-connected",
1293 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1298 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1300 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1302 /* get control strings from DESCRIBE response */
1303 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1304 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1305 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1306 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1307 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1309 get_client_ports (&client_port);
1311 /* do SETUP for video and audio */
1312 fail_unless (do_setup_full (conn, video_control, GST_RTSP_LOWER_TRANS_UDP,
1313 &client_port, NULL, &session, &video_transport,
1314 NULL) == GST_RTSP_STS_OK);
1315 fail_unless (do_setup_full (conn, audio_control, GST_RTSP_LOWER_TRANS_UDP,
1316 &client_port, NULL, &session, &audio_transport,
1317 NULL) == GST_RTSP_STS_OK);
1319 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1321 /* send PLAY request and check that we get 200 OK */
1322 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1323 session) == GST_RTSP_STS_OK);
1327 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1328 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 0);
1330 /* clean up and iterate so the clean-up can finish */
1331 g_object_unref (pool);
1333 gst_rtsp_transport_free (video_transport);
1334 gst_rtsp_transport_free (audio_transport);
1335 gst_sdp_message_free (sdp_message);
1336 gst_rtsp_connection_free (conn);
1345 GST_START_TEST (test_play_multithreaded_timeout_session)
1347 GstRTSPConnection *conn;
1348 GstSDPMessage *sdp_message = NULL;
1349 const GstSDPMedia *sdp_media;
1350 const gchar *video_control;
1351 const gchar *audio_control;
1352 GstRTSPRange client_port;
1353 gchar *session1 = NULL;
1354 gchar *session2 = NULL;
1355 GstRTSPTransport *video_transport = NULL;
1356 GstRTSPTransport *audio_transport = NULL;
1357 GstRTSPSessionPool *pool;
1358 GstRTSPThreadPool *thread_pool;
1360 thread_pool = gst_rtsp_server_get_thread_pool (server);
1361 gst_rtsp_thread_pool_set_max_threads (thread_pool, 2);
1362 g_object_unref (thread_pool);
1364 pool = gst_rtsp_server_get_session_pool (server);
1365 g_signal_connect (server, "client-connected",
1366 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1371 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1373 gst_rtsp_connection_set_remember_session_id (conn, FALSE);
1375 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1377 /* get control strings from DESCRIBE response */
1378 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1379 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1380 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1381 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1382 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1384 get_client_ports (&client_port);
1386 /* do SETUP for video and audio */
1387 fail_unless (do_setup (conn, video_control, &client_port, &session1,
1388 &video_transport) == GST_RTSP_STS_OK);
1389 fail_unless (do_setup (conn, audio_control, &client_port, &session2,
1390 &audio_transport) == GST_RTSP_STS_OK);
1392 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 2);
1394 /* send PLAY request and check that we get 200 OK */
1395 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1396 session1) == GST_RTSP_STS_OK);
1397 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1398 session2) == GST_RTSP_STS_OK);
1402 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1404 /* send TEARDOWN request and check that we get 454 Session Not found */
1405 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1406 session1) == GST_RTSP_STS_SESSION_NOT_FOUND);
1408 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1409 session2) == GST_RTSP_STS_OK);
1411 /* clean up and iterate so the clean-up can finish */
1412 g_object_unref (pool);
1415 gst_rtsp_transport_free (video_transport);
1416 gst_rtsp_transport_free (audio_transport);
1417 gst_sdp_message_free (sdp_message);
1418 gst_rtsp_connection_free (conn);
1427 GST_START_TEST (test_play_disconnect)
1429 GstRTSPConnection *conn;
1430 GstSDPMessage *sdp_message = NULL;
1431 const GstSDPMedia *sdp_media;
1432 const gchar *video_control;
1433 const gchar *audio_control;
1434 GstRTSPRange client_port;
1435 gchar *session = NULL;
1436 GstRTSPTransport *video_transport = NULL;
1437 GstRTSPTransport *audio_transport = NULL;
1438 GstRTSPSessionPool *pool;
1440 pool = gst_rtsp_server_get_session_pool (server);
1441 g_signal_connect (server, "client-connected",
1442 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1446 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1448 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1450 /* get control strings from DESCRIBE response */
1451 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1452 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1453 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1454 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1455 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1457 get_client_ports (&client_port);
1459 /* do SETUP for video and audio */
1460 fail_unless (do_setup (conn, video_control, &client_port, &session,
1461 &video_transport) == GST_RTSP_STS_OK);
1462 fail_unless (do_setup (conn, audio_control, &client_port, &session,
1463 &audio_transport) == GST_RTSP_STS_OK);
1465 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1467 /* send PLAY request and check that we get 200 OK */
1468 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1469 session) == GST_RTSP_STS_OK);
1471 gst_rtsp_connection_free (conn);
1475 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1476 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1479 /* clean up and iterate so the clean-up can finish */
1480 g_object_unref (pool);
1482 gst_rtsp_transport_free (video_transport);
1483 gst_rtsp_transport_free (audio_transport);
1484 gst_sdp_message_free (sdp_message);
1492 /* Only different with test_play is the specific ports selected */
1494 GST_START_TEST (test_play_specific_server_port)
1496 GstRTSPMountPoints *mounts;
1498 GstRTSPMediaFactory *factory;
1499 GstRTSPAddressPool *pool;
1500 GstRTSPConnection *conn;
1501 GstSDPMessage *sdp_message = NULL;
1502 const GstSDPMedia *sdp_media;
1503 const gchar *video_control;
1504 GstRTSPRange client_port;
1505 gchar *session = NULL;
1506 GstRTSPTransport *video_transport = NULL;
1507 GSocket *rtp_socket, *rtcp_socket;
1508 GSocketAddress *rtp_address, *rtcp_address;
1509 guint16 rtp_port, rtcp_port;
1511 mounts = gst_rtsp_server_get_mount_points (server);
1513 factory = gst_rtsp_media_factory_new ();
1514 pool = gst_rtsp_address_pool_new ();
1515 gst_rtsp_address_pool_add_range (pool, GST_RTSP_ADDRESS_POOL_ANY_IPV4,
1516 GST_RTSP_ADDRESS_POOL_ANY_IPV4, 7770, 7780, 0);
1517 gst_rtsp_media_factory_set_address_pool (factory, pool);
1518 g_object_unref (pool);
1519 gst_rtsp_media_factory_set_launch (factory, "( " VIDEO_PIPELINE " )");
1520 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
1521 g_object_unref (mounts);
1523 /* set port to any */
1524 gst_rtsp_server_set_service (server, "0");
1526 /* attach to default main context */
1527 source_id = gst_rtsp_server_attach (server, NULL);
1528 fail_if (source_id == 0);
1531 service = gst_rtsp_server_get_service (server);
1532 test_port = atoi (service);
1533 fail_unless (test_port != 0);
1536 GST_DEBUG ("rtsp server listening on port %d", test_port);
1539 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1541 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1543 /* get control strings from DESCRIBE response */
1544 fail_unless (gst_sdp_message_medias_len (sdp_message) == 1);
1545 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1546 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1548 get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
1550 /* do SETUP for video */
1551 fail_unless (do_setup (conn, video_control, &client_port, &session,
1552 &video_transport) == GST_RTSP_STS_OK);
1554 /* send PLAY request and check that we get 200 OK */
1555 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1556 session) == GST_RTSP_STS_OK);
1558 receive_rtp (rtp_socket, &rtp_address);
1559 receive_rtcp (rtcp_socket, &rtcp_address, 0);
1561 fail_unless (G_IS_INET_SOCKET_ADDRESS (rtp_address));
1562 fail_unless (G_IS_INET_SOCKET_ADDRESS (rtcp_address));
1564 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_address));
1566 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtcp_address));
1567 fail_unless (rtp_port >= 7770 && rtp_port <= 7780 && rtp_port % 2 == 0);
1568 fail_unless (rtcp_port >= 7770 && rtcp_port <= 7780 && rtcp_port % 2 == 1);
1569 fail_unless (rtp_port + 1 == rtcp_port);
1571 g_object_unref (rtp_address);
1572 g_object_unref (rtcp_address);
1574 /* send TEARDOWN request and check that we get 200 OK */
1575 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1576 session) == GST_RTSP_STS_OK);
1578 /* FIXME: The rtsp-server always disconnects the transport before
1579 * sending the RTCP BYE
1580 * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
1583 /* clean up and iterate so the clean-up can finish */
1584 g_object_unref (rtp_socket);
1585 g_object_unref (rtcp_socket);
1587 gst_rtsp_transport_free (video_transport);
1588 gst_sdp_message_free (sdp_message);
1589 gst_rtsp_connection_free (conn);
1599 GST_START_TEST (test_play_smpte_range)
1603 do_test_play ("npt=5-");
1604 do_test_play ("smpte=0:00:00-");
1605 do_test_play ("smpte=1:00:00-");
1606 do_test_play ("smpte=1:00:03-");
1607 do_test_play ("clock=20120321T152256Z-");
1615 GST_START_TEST (test_announce_without_sdp)
1617 GstRTSPConnection *conn;
1618 GstRTSPStatusCode status;
1619 GstRTSPMessage *request;
1620 GstRTSPMessage *response;
1622 start_record_server ("( fakesink name=depay0 )");
1624 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1626 /* create and send ANNOUNCE request */
1627 request = create_request (conn, GST_RTSP_ANNOUNCE, NULL);
1629 fail_unless (send_request (conn, request));
1633 response = read_response (conn);
1635 /* check response */
1636 gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1637 fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1638 gst_rtsp_message_free (response);
1640 /* try again, this type with content-type, but still no SDP */
1641 gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1644 fail_unless (send_request (conn, request));
1648 response = read_response (conn);
1650 /* check response */
1651 gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1652 fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1653 gst_rtsp_message_free (response);
1655 /* try again, this type with an unknown content-type */
1656 gst_rtsp_message_remove_header (request, GST_RTSP_HDR_CONTENT_TYPE, -1);
1657 gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1658 "application/x-something");
1660 fail_unless (send_request (conn, request));
1664 response = read_response (conn);
1666 /* check response */
1667 gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1668 fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1669 gst_rtsp_message_free (response);
1671 /* clean up and iterate so the clean-up can finish */
1672 gst_rtsp_message_free (request);
1673 gst_rtsp_connection_free (conn);
1680 static GstRTSPStatusCode
1681 do_announce (GstRTSPConnection * conn, GstSDPMessage * sdp)
1683 GstRTSPMessage *request;
1684 GstRTSPMessage *response;
1685 GstRTSPStatusCode code;
1688 /* create request */
1689 request = create_request (conn, GST_RTSP_ANNOUNCE, NULL);
1691 gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1694 /* add SDP to the response body */
1695 str = gst_sdp_message_as_text (sdp);
1696 gst_rtsp_message_take_body (request, (guint8 *) str, strlen (str));
1697 gst_sdp_message_free (sdp);
1700 fail_unless (send_request (conn, request));
1701 gst_rtsp_message_free (request);
1706 response = read_response (conn);
1708 /* check status line */
1709 gst_rtsp_message_parse_response (response, &code, NULL, NULL);
1711 gst_rtsp_message_free (response);
1716 media_constructed_cb (GstRTSPMediaFactory * mfactory, GstRTSPMedia * media,
1719 GstElement **p_sink = user_data;
1722 bin = gst_rtsp_media_get_element (media);
1723 *p_sink = gst_bin_get_by_name (GST_BIN (bin), "sink");
1724 GST_INFO ("media constructed!: %" GST_PTR_FORMAT, *p_sink);
1727 #define RECORD_N_BUFS 10
1729 GST_START_TEST (test_record_tcp)
1731 GstRTSPMediaFactory *mfactory;
1732 GstRTSPConnection *conn;
1733 GstRTSPStatusCode status;
1734 GstRTSPMessage *response;
1735 GstRTSPMessage *request;
1740 GstElement *server_sink = NULL;
1741 GSocket *conn_socket;
1743 gchar *client_ip, *sess_id, *session = NULL;
1747 start_record_server ("( rtppcmadepay name=depay0 ! appsink name=sink )");
1749 g_signal_connect (mfactory, "media-constructed",
1750 G_CALLBACK (media_constructed_cb), &server_sink);
1752 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1754 conn_socket = gst_rtsp_connection_get_read_socket (conn);
1756 sa = g_socket_get_local_address (conn_socket, NULL);
1757 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
1758 client_ip = g_inet_address_to_string (ia);
1759 if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6)
1761 else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
1764 g_assert_not_reached ();
1765 g_object_unref (sa);
1767 gst_sdp_message_new (&sdp);
1769 /* some standard things first */
1770 gst_sdp_message_set_version (sdp, "0");
1772 /* session ID doesn't have to be super-unique in this case */
1773 sess_id = g_strdup_printf ("%u", g_random_int ());
1774 gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
1778 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1779 gst_sdp_message_set_information (sdp, "rtsp-server-test");
1780 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1781 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1785 GstSDPMedia *smedia;
1787 gst_sdp_media_new (&smedia);
1788 gst_sdp_media_set_media (smedia, "audio");
1789 gst_sdp_media_add_format (smedia, "8"); /* pcma/alaw */
1790 gst_sdp_media_set_port_info (smedia, 0, 1);
1791 gst_sdp_media_set_proto (smedia, "RTP/AVP");
1792 gst_sdp_media_add_attribute (smedia, "rtpmap", "8 PCMA/8000");
1793 gst_sdp_message_add_media (sdp, smedia);
1794 gst_sdp_media_free (smedia);
1797 /* send ANNOUNCE request */
1798 status = do_announce (conn, sdp);
1799 fail_unless_equals_int (status, GST_RTSP_STS_OK);
1801 /* create and send SETUP request */
1802 request = create_request (conn, GST_RTSP_SETUP, NULL);
1803 gst_rtsp_message_add_header (request, GST_RTSP_HDR_TRANSPORT,
1804 "RTP/AVP/TCP;interleaved=0;mode=record");
1805 fail_unless (send_request (conn, request));
1806 gst_rtsp_message_free (request);
1808 response = read_response (conn);
1809 gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1810 fail_unless_equals_int (status, GST_RTSP_STS_OK);
1813 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &session, 0);
1814 session = g_strdup (session);
1815 fail_unless_equals_int (rres, GST_RTSP_OK);
1816 gst_rtsp_message_free (response);
1819 request = create_request (conn, GST_RTSP_RECORD, NULL);
1820 gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session);
1821 fail_unless (send_request (conn, request));
1822 gst_rtsp_message_free (request);
1824 response = read_response (conn);
1825 gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1826 fail_unless_equals_int (status, GST_RTSP_STS_OK);
1827 gst_rtsp_message_free (response);
1829 /* send some data */
1831 GstElement *pipeline, *src, *enc, *pay, *sink;
1833 pipeline = gst_pipeline_new ("send-pipeline");
1834 src = gst_element_factory_make ("audiotestsrc", NULL);
1835 g_object_set (src, "num-buffers", RECORD_N_BUFS,
1836 "samplesperbuffer", 1000, NULL);
1837 enc = gst_element_factory_make ("alawenc", NULL);
1838 pay = gst_element_factory_make ("rtppcmapay", NULL);
1839 sink = gst_element_factory_make ("appsink", NULL);
1840 fail_unless (pipeline && src && enc && pay && sink);
1841 gst_bin_add_many (GST_BIN (pipeline), src, enc, pay, sink, NULL);
1842 gst_element_link_many (src, enc, pay, sink, NULL);
1843 gst_element_set_state (pipeline, GST_STATE_PLAYING);
1846 GstRTSPMessage *data_msg;
1847 GstMapInfo map = GST_MAP_INFO_INIT;
1849 GstSample *sample = NULL;
1852 g_signal_emit_by_name (G_OBJECT (sink), "pull-sample", &sample);
1855 buf = gst_sample_get_buffer (sample);
1856 rres = gst_rtsp_message_new_data (&data_msg, 0);
1857 fail_unless_equals_int (rres, GST_RTSP_OK);
1858 gst_buffer_map (buf, &map, GST_MAP_READ);
1859 GST_INFO ("sending %u bytes of data on channel 0", (guint) map.size);
1860 GST_MEMDUMP ("data on channel 0", map.data, map.size);
1861 rres = gst_rtsp_message_set_body (data_msg, map.data, map.size);
1862 fail_unless_equals_int (rres, GST_RTSP_OK);
1863 gst_buffer_unmap (buf, &map);
1864 rres = gst_rtsp_connection_send (conn, data_msg, NULL);
1865 fail_unless_equals_int (rres, GST_RTSP_OK);
1866 gst_rtsp_message_free (data_msg);
1867 gst_sample_unref (sample);
1870 gst_element_set_state (pipeline, GST_STATE_NULL);
1871 gst_object_unref (pipeline);
1874 /* check received data (we assume every buffer created by audiotestsrc and
1875 * subsequently encoded by mulawenc results in exactly one RTP packet) */
1876 for (i = 0; i < RECORD_N_BUFS; ++i) {
1877 GstSample *sample = NULL;
1879 g_signal_emit_by_name (G_OBJECT (server_sink), "pull-sample", &sample);
1880 GST_INFO ("%2d recv sample: %p", i, sample);
1881 gst_sample_unref (sample);
1884 fail_unless_equals_int (GST_STATE (server_sink), GST_STATE_PLAYING);
1886 /* clean up and iterate so the clean-up can finish */
1887 gst_rtsp_connection_free (conn);
1896 rtspserver_suite (void)
1898 Suite *s = suite_create ("rtspserver");
1899 TCase *tc = tcase_create ("general");
1901 suite_add_tcase (s, tc);
1902 tcase_add_checked_fixture (tc, setup, teardown);
1903 tcase_set_timeout (tc, 120);
1904 tcase_add_test (tc, test_connect);
1905 tcase_add_test (tc, test_describe);
1906 tcase_add_test (tc, test_describe_non_existing_mount_point);
1907 tcase_add_test (tc, test_describe_record_media);
1908 tcase_add_test (tc, test_setup_udp);
1909 tcase_add_test (tc, test_setup_tcp);
1910 tcase_add_test (tc, test_setup_udp_mcast);
1911 tcase_add_test (tc, test_setup_twice);
1912 tcase_add_test (tc, test_setup_with_require_header);
1913 tcase_add_test (tc, test_setup_non_existing_stream);
1914 tcase_add_test (tc, test_play);
1915 tcase_add_test (tc, test_play_without_session);
1916 tcase_add_test (tc, test_bind_already_in_use);
1917 tcase_add_test (tc, test_play_multithreaded);
1918 tcase_add_test (tc, test_play_multithreaded_block_in_describe);
1919 tcase_add_test (tc, test_play_multithreaded_timeout_client);
1920 tcase_add_test (tc, test_play_multithreaded_timeout_session);
1921 tcase_add_test (tc, test_play_disconnect);
1922 tcase_add_test (tc, test_play_specific_server_port);
1923 tcase_add_test (tc, test_play_smpte_range);
1924 tcase_add_test (tc, test_announce_without_sdp);
1925 tcase_add_test (tc, test_record_tcp);
1929 GST_CHECK_MAIN (rtspserver);