3 * unit test for GstRTSPServer
5 * Copyright (C) 2012 Axis Communications <dev-gstreamer at axis dot com>
6 * @author David Svensson Fors <davidsf at axis dot com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include <gst/check/gstcheck.h>
25 #include <gst/sdp/gstsdpmessage.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/rtp/gstrtcpbuffer.h>
30 #include <netinet/in.h>
32 #include "rtsp-server.h"
34 #define VIDEO_PIPELINE "videotestsrc ! " \
35 "video/x-raw,width=352,height=288 ! " \
36 "rtpgstpay name=pay0 pt=96"
37 #define AUDIO_PIPELINE "audiotestsrc ! " \
38 "audio/x-raw,rate=8000 ! " \
39 "rtpgstpay name=pay1 pt=97"
41 #define TEST_MOUNT_POINT "/test"
42 #define TEST_PROTO "RTP/AVP"
43 #define TEST_ENCODING "X-GST"
44 #define TEST_CLOCK_RATE "90000"
46 /* tested rtsp server */
47 static GstRTSPServer *server = NULL;
49 /* tcp port that the test server listens for rtsp requests on */
50 static gint test_port = 0;
52 /* id of the server's source within the GMainContext */
53 static guint source_id;
55 /* iterate the default main loop until there are no events to dispatch */
59 while (g_main_context_iteration (NULL, FALSE)) {
60 GST_DEBUG ("iteration");
64 /* returns an unused port that can be used by the test */
66 get_unused_port (gint type)
69 struct sockaddr_in addr;
74 fail_unless ((sock = socket (AF_INET, type, 0)) > 0);
76 /* pass port 0 to bind, which will bind to any free port */
77 memset (&addr, 0, sizeof addr);
78 addr.sin_family = AF_INET;
79 addr.sin_addr.s_addr = INADDR_ANY;
80 addr.sin_port = htons (0);
81 fail_unless (bind (sock, (struct sockaddr *) &addr, sizeof addr) == 0);
83 /* ask what port was bound using getsockname */
84 addr_len = sizeof addr;
85 memset (&addr, 0, addr_len);
86 fail_unless (getsockname (sock, (struct sockaddr *) &addr, &addr_len) == 0);
87 port = ntohs (addr.sin_port);
89 /* close the socket so the port gets unbound again (and can be used by the
97 get_client_ports_full (GstRTSPRange * range, GSocket ** rtp_socket,
98 GSocket ** rtcp_socket)
101 GSocket *rtcp = NULL;
104 GInetAddress *anyaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
105 GSocketAddress *sockaddr;
112 rtp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
113 G_SOCKET_PROTOCOL_UDP, NULL);
114 fail_unless (rtp != NULL);
116 sockaddr = g_inet_socket_address_new (anyaddr, rtp_port);
117 fail_unless (sockaddr != NULL);
118 bound = g_socket_bind (rtp, sockaddr, FALSE, NULL);
119 g_object_unref (sockaddr);
121 g_object_unref (rtp);
125 sockaddr = g_socket_get_local_address (rtp, NULL);
126 fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
128 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
129 g_object_unref (sockaddr);
131 if (rtp_port % 2 != 0) {
133 g_object_unref (rtp);
137 rtcp_port = rtp_port + 1;
139 rtcp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
140 G_SOCKET_PROTOCOL_UDP, NULL);
141 fail_unless (rtcp != NULL);
143 sockaddr = g_inet_socket_address_new (anyaddr, rtcp_port);
144 fail_unless (sockaddr != NULL);
145 bound = g_socket_bind (rtcp, sockaddr, FALSE, NULL);
146 g_object_unref (sockaddr);
148 g_object_unref (rtp);
149 g_object_unref (rtcp);
153 sockaddr = g_socket_get_local_address (rtcp, NULL);
154 fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
155 fail_unless (rtcp_port ==
156 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr)));
157 g_object_unref (sockaddr);
162 range->min = rtp_port;
163 range->max = rtcp_port;
167 g_object_unref (rtp);
171 g_object_unref (rtcp);
172 GST_DEBUG ("client_port=%d-%d", range->min, range->max);
173 g_object_unref (anyaddr);
176 /* get a free rtp/rtcp client port pair */
178 get_client_ports (GstRTSPRange * range)
180 get_client_ports_full (range, NULL, NULL);
183 /* start the tested rtsp server */
187 GstRTSPMountPoints *mounts;
189 GstRTSPMediaFactory *factory;
191 mounts = gst_rtsp_server_get_mount_points (server);
193 factory = gst_rtsp_media_factory_new ();
195 gst_rtsp_media_factory_set_launch (factory,
196 "( " VIDEO_PIPELINE " " AUDIO_PIPELINE " )");
197 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
198 g_object_unref (mounts);
201 test_port = get_unused_port (SOCK_STREAM);
202 service = g_strdup_printf ("%d", test_port);
203 gst_rtsp_server_set_service (server, service);
206 /* attach to default main context */
207 source_id = gst_rtsp_server_attach (server, NULL);
208 fail_if (source_id == 0);
210 GST_DEBUG ("rtsp server listening on port %d", test_port);
213 /* stop the tested rtsp server */
217 g_source_remove (source_id);
220 GST_DEBUG ("rtsp server stopped");
223 /* create an rtsp connection to the server on test_port */
224 static GstRTSPConnection *
225 connect_to_server (gint port, const gchar * mount_point)
227 GstRTSPConnection *conn = NULL;
230 GstRTSPUrl *url = NULL;
232 address = gst_rtsp_server_get_address (server);
233 uri_string = g_strdup_printf ("rtsp://%s:%d%s", address, port, mount_point);
235 gst_rtsp_url_parse (uri_string, &url);
238 fail_unless (gst_rtsp_connection_create (url, &conn) == GST_RTSP_OK);
239 gst_rtsp_url_free (url);
241 fail_unless (gst_rtsp_connection_connect (conn, NULL) == GST_RTSP_OK);
246 /* create an rtsp request */
247 static GstRTSPMessage *
248 create_request (GstRTSPConnection * conn, GstRTSPMethod method,
249 const gchar * control)
251 GstRTSPMessage *request = NULL;
255 base_uri = gst_rtsp_url_get_request_uri (gst_rtsp_connection_get_url (conn));
256 full_uri = g_strdup_printf ("%s/%s", base_uri, control ? control : "");
258 if (gst_rtsp_message_new_request (&request, method, full_uri) != GST_RTSP_OK) {
259 GST_DEBUG ("failed to create request object");
267 /* send an rtsp request */
269 send_request (GstRTSPConnection * conn, GstRTSPMessage * request)
271 if (gst_rtsp_connection_send (conn, request, NULL) != GST_RTSP_OK) {
272 GST_DEBUG ("failed to send request");
278 /* read rtsp response. response must be freed by the caller */
279 static GstRTSPMessage *
280 read_response (GstRTSPConnection * conn)
282 GstRTSPMessage *response = NULL;
284 if (gst_rtsp_message_new (&response) != GST_RTSP_OK) {
285 GST_DEBUG ("failed to create response object");
288 if (gst_rtsp_connection_receive (conn, response, NULL) != GST_RTSP_OK) {
289 GST_DEBUG ("failed to read response");
290 gst_rtsp_message_free (response);
293 fail_unless (gst_rtsp_message_get_type (response) ==
294 GST_RTSP_MESSAGE_RESPONSE);
298 /* send an rtsp request and receive response. gchar** parameters are out
299 * parameters that have to be freed by the caller */
300 static GstRTSPStatusCode
301 do_request (GstRTSPConnection * conn, GstRTSPMethod method,
302 const gchar * control, const gchar * session_in, const gchar * transport_in,
303 const gchar * range_in,
304 gchar ** content_type, gchar ** content_base, gchar ** body,
305 gchar ** session_out, gchar ** transport_out, gchar ** range_out)
307 GstRTSPMessage *request;
308 GstRTSPMessage *response;
309 GstRTSPStatusCode code;
313 request = create_request (conn, method, control);
317 gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session_in);
320 gst_rtsp_message_add_header (request, GST_RTSP_HDR_TRANSPORT, transport_in);
323 gst_rtsp_message_add_header (request, GST_RTSP_HDR_RANGE, range_in);
327 fail_unless (send_request (conn, request));
328 gst_rtsp_message_free (request);
333 response = read_response (conn);
335 /* check status line */
336 gst_rtsp_message_parse_response (response, &code, NULL, NULL);
337 if (code != GST_RTSP_STS_OK) {
338 gst_rtsp_message_free (response);
342 /* get information from response */
344 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_TYPE,
346 *content_type = g_strdup (value);
349 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
351 *content_base = g_strdup (value);
354 *body = g_malloc (response->body_size + 1);
355 strncpy (*body, (gchar *) response->body, response->body_size);
358 gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &value, 0);
360 value = g_strdup (value);
362 /* Remove the timeout */
364 char *pos = strchr (value, ';');
369 /* check that we got the same session back */
370 fail_unless (!g_strcmp0 (value, session_in));
372 *session_out = value;
375 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &value, 0);
376 *transport_out = g_strdup (value);
379 gst_rtsp_message_get_header (response, GST_RTSP_HDR_RANGE, &value, 0);
380 *range_out = g_strdup (value);
383 gst_rtsp_message_free (response);
387 /* send an rtsp request with a method and a session, and receive response */
388 static GstRTSPStatusCode
389 do_simple_request (GstRTSPConnection * conn, GstRTSPMethod method,
390 const gchar * session)
392 return do_request (conn, method, NULL, session, NULL, NULL, NULL,
393 NULL, NULL, NULL, NULL, NULL);
396 /* send a DESCRIBE request and receive response. returns a received
397 * GstSDPMessage that must be freed by the caller */
398 static GstSDPMessage *
399 do_describe (GstRTSPConnection * conn, const gchar * mount_point)
401 GstSDPMessage *sdp_message;
406 gchar *expected_content_base;
408 /* send DESCRIBE request */
409 fail_unless (do_request (conn, GST_RTSP_DESCRIBE, NULL, NULL, NULL, NULL,
410 &content_type, &content_base, &body, NULL, NULL, NULL) ==
413 /* check response values */
414 fail_unless (!g_strcmp0 (content_type, "application/sdp"));
415 address = gst_rtsp_server_get_address (server);
416 expected_content_base =
417 g_strdup_printf ("rtsp://%s:%d%s/", address, test_port, mount_point);
418 fail_unless (!g_strcmp0 (content_base, expected_content_base));
420 /* create sdp message */
421 fail_unless (gst_sdp_message_new (&sdp_message) == GST_SDP_OK);
422 fail_unless (gst_sdp_message_parse_buffer ((guint8 *) body,
423 strlen (body), sdp_message) == GST_SDP_OK);
426 g_free (content_type);
427 g_free (content_base);
430 g_free (expected_content_base);
435 /* send a SETUP request and receive response. if *session is not NULL,
436 * it is used in the request. otherwise, *session is set to a returned
437 * session string that must be freed by the caller. the returned
438 * transport must be freed by the caller. */
439 static GstRTSPStatusCode
440 do_setup (GstRTSPConnection * conn, const gchar * control,
441 const GstRTSPRange * client_ports, gchar ** session,
442 GstRTSPTransport ** transport)
444 GstRTSPStatusCode code;
445 gchar *session_in = NULL;
446 gchar *transport_string_in = NULL;
447 gchar **session_out = NULL;
448 gchar *transport_string_out = NULL;
450 /* prepare and send SETUP request */
453 session_in = *session;
455 session_out = session;
458 transport_string_in =
459 g_strdup_printf (TEST_PROTO ";unicast;client_port=%d-%d",
460 client_ports->min, client_ports->max);
462 do_request (conn, GST_RTSP_SETUP, control, session_in,
463 transport_string_in, NULL, NULL, NULL, NULL, session_out,
464 &transport_string_out, NULL);
465 g_free (transport_string_in);
467 if (transport_string_out) {
468 /* create transport */
469 fail_unless (gst_rtsp_transport_new (transport) == GST_RTSP_OK);
470 fail_unless (gst_rtsp_transport_parse (transport_string_out,
471 *transport) == GST_RTSP_OK);
472 g_free (transport_string_out);
478 /* fixture setup function */
482 server = gst_rtsp_server_new ();
485 /* fixture clean-up function */
490 g_object_unref (server);
496 GST_START_TEST (test_connect)
498 GstRTSPConnection *conn;
502 /* connect to server */
503 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
506 gst_rtsp_connection_free (conn);
509 /* iterate so the clean-up can finish */
515 GST_START_TEST (test_describe)
517 GstRTSPConnection *conn;
518 GstSDPMessage *sdp_message = NULL;
519 const GstSDPMedia *sdp_media;
521 gchar *expected_rtpmap;
523 const gchar *control_video;
524 const gchar *control_audio;
528 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
530 /* send DESCRIBE request */
531 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
533 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
535 /* check video sdp */
536 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
537 fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
538 fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
539 sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
542 g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
543 rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
544 fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
545 g_free (expected_rtpmap);
546 control_video = gst_sdp_media_get_attribute_val (sdp_media, "control");
547 fail_unless (!g_strcmp0 (control_video, "stream=0"));
549 /* check audio sdp */
550 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
551 fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
552 fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
553 sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
556 g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
557 rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
558 fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
559 g_free (expected_rtpmap);
560 control_audio = gst_sdp_media_get_attribute_val (sdp_media, "control");
561 fail_unless (!g_strcmp0 (control_audio, "stream=1"));
563 /* clean up and iterate so the clean-up can finish */
564 gst_sdp_message_free (sdp_message);
565 gst_rtsp_connection_free (conn);
572 GST_START_TEST (test_describe_non_existing_mount_point)
574 GstRTSPConnection *conn;
578 /* send DESCRIBE request for a non-existing mount point
579 * and check that we get a 404 Not Found */
580 conn = connect_to_server (test_port, "/non-existing");
581 fail_unless (do_simple_request (conn, GST_RTSP_DESCRIBE, NULL)
582 == GST_RTSP_STS_NOT_FOUND);
584 /* clean up and iterate so the clean-up can finish */
585 gst_rtsp_connection_free (conn);
592 GST_START_TEST (test_setup)
594 GstRTSPConnection *conn;
595 GstSDPMessage *sdp_message = NULL;
596 const GstSDPMedia *sdp_media;
597 const gchar *video_control;
598 const gchar *audio_control;
599 GstRTSPRange client_ports;
600 gchar *session = NULL;
601 GstRTSPTransport *video_transport = NULL;
602 GstRTSPTransport *audio_transport = NULL;
606 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
608 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
610 /* get control strings from DESCRIBE response */
611 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
612 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
613 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
614 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
615 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
617 get_client_ports (&client_ports);
619 /* send SETUP request for video */
620 fail_unless (do_setup (conn, video_control, &client_ports, &session,
621 &video_transport) == GST_RTSP_STS_OK);
622 GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
624 /* check response from SETUP */
625 fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
626 fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
627 fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
628 fail_unless (video_transport->mode_play);
629 gst_rtsp_transport_free (video_transport);
631 /* send SETUP request for audio */
632 fail_unless (do_setup (conn, audio_control, &client_ports, &session,
633 &audio_transport) == GST_RTSP_STS_OK);
634 GST_DEBUG ("set up audio %s with session '%s'", audio_control, session);
636 /* check response from SETUP */
637 fail_unless (audio_transport->trans == GST_RTSP_TRANS_RTP);
638 fail_unless (audio_transport->profile == GST_RTSP_PROFILE_AVP);
639 fail_unless (audio_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
640 fail_unless (audio_transport->mode_play);
641 gst_rtsp_transport_free (audio_transport);
643 /* clean up and iterate so the clean-up can finish */
645 gst_sdp_message_free (sdp_message);
646 gst_rtsp_connection_free (conn);
653 GST_START_TEST (test_setup_non_existing_stream)
655 GstRTSPConnection *conn;
656 GstRTSPRange client_ports;
660 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
662 get_client_ports (&client_ports);
664 /* send SETUP request with a non-existing stream and check that we get a
666 fail_unless (do_setup (conn, "stream=7", &client_ports, NULL,
667 NULL) == GST_RTSP_STS_NOT_FOUND);
669 /* clean up and iterate so the clean-up can finish */
670 gst_rtsp_connection_free (conn);
674 /* need to unref the server here, otherwise threads will remain
675 * and teardown won't be run */
676 g_object_unref (server);
683 receive_rtp (GSocket * socket, GSocketAddress ** addr)
685 GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
689 GstMapInfo map = GST_MAP_INFO_INIT;
690 GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
692 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
693 bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
694 map.maxsize, NULL, NULL);
695 fail_unless (bytes > 0);
696 gst_buffer_unmap (buffer, &map);
697 gst_buffer_set_size (buffer, bytes);
699 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer)) {
700 gst_rtp_buffer_unmap (&rtpbuffer);
705 g_clear_object (addr);
708 gst_buffer_unref (buffer);
712 receive_rtcp (GSocket * socket, GSocketAddress ** addr, GstRTCPType type)
714 GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
718 GstMapInfo map = GST_MAP_INFO_INIT;
720 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
721 bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
722 map.maxsize, NULL, NULL);
723 fail_unless (bytes > 0);
724 gst_buffer_unmap (buffer, &map);
725 gst_buffer_set_size (buffer, bytes);
727 if (gst_rtcp_buffer_validate (buffer)) {
728 GstRTCPBuffer rtcpbuffer = GST_RTCP_BUFFER_INIT;
729 GstRTCPPacket packet;
732 fail_unless (gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcpbuffer));
733 fail_unless (gst_rtcp_buffer_get_first_packet (&rtcpbuffer, &packet));
735 if (gst_rtcp_packet_get_type (&packet) == type) {
736 gst_rtcp_buffer_unmap (&rtcpbuffer);
739 } while (gst_rtcp_packet_move_to_next (&packet));
740 gst_rtcp_buffer_unmap (&rtcpbuffer);
747 g_clear_object (addr);
752 gst_buffer_unref (buffer);
756 do_test_play (const gchar * range)
758 GstRTSPConnection *conn;
759 GstSDPMessage *sdp_message = NULL;
760 const GstSDPMedia *sdp_media;
761 const gchar *video_control;
762 const gchar *audio_control;
763 GstRTSPRange client_port;
764 gchar *session = NULL;
765 GstRTSPTransport *video_transport = NULL;
766 GstRTSPTransport *audio_transport = NULL;
767 GSocket *rtp_socket, *rtcp_socket;
768 gchar *range_out = NULL;
770 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
772 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
774 /* get control strings from DESCRIBE response */
775 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
776 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
777 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
778 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
779 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
781 get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
783 /* do SETUP for video and audio */
784 fail_unless (do_setup (conn, video_control, &client_port, &session,
785 &video_transport) == GST_RTSP_STS_OK);
786 fail_unless (do_setup (conn, audio_control, &client_port, &session,
787 &audio_transport) == GST_RTSP_STS_OK);
789 /* send PLAY request and check that we get 200 OK */
790 fail_unless (do_request (conn, GST_RTSP_PLAY, NULL, session, NULL, range,
791 NULL, NULL, NULL, NULL, NULL, &range_out) == GST_RTSP_STS_OK);
793 fail_unless_equals_string (range, range_out);
796 receive_rtp (rtp_socket, NULL);
797 receive_rtcp (rtcp_socket, NULL, 0);
799 /* send TEARDOWN request and check that we get 200 OK */
800 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
801 session) == GST_RTSP_STS_OK);
803 /* FIXME: The rtsp-server always disconnects the transport before
804 * sending the RTCP BYE
805 * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
808 /* clean up and iterate so the clean-up can finish */
809 g_object_unref (rtp_socket);
810 g_object_unref (rtcp_socket);
812 gst_rtsp_transport_free (video_transport);
813 gst_rtsp_transport_free (audio_transport);
814 gst_sdp_message_free (sdp_message);
815 gst_rtsp_connection_free (conn);
819 GST_START_TEST (test_play)
831 GST_START_TEST (test_play_without_session)
833 GstRTSPConnection *conn;
837 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
839 /* send PLAY request without a session and check that we get a
840 * 454 Session Not Found */
841 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
842 NULL) == GST_RTSP_STS_SESSION_NOT_FOUND);
844 /* clean up and iterate so the clean-up can finish */
845 gst_rtsp_connection_free (conn);
852 GST_START_TEST (test_bind_already_in_use)
855 GSocketService *service;
856 GError *error = NULL;
860 serv = gst_rtsp_server_new ();
861 service = g_socket_service_new ();
863 /* bind service to port */
865 g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL,
867 g_assert_no_error (error);
869 port_str = g_strdup_printf ("%d\n", port);
871 /* try to bind server to the same port */
872 g_object_set (serv, "service", port_str, NULL);
875 /* attach to default main context */
876 fail_unless (gst_rtsp_server_attach (serv, NULL) == 0);
879 g_object_unref (serv);
880 g_socket_service_stop (service);
881 g_object_unref (service);
887 GST_START_TEST (test_play_multithreaded)
889 gst_rtsp_server_set_max_threads (server, 2);
910 media_constructed_block (GstRTSPMediaFactory * factory,
911 GstRTSPMedia * media, gpointer user_data)
913 gint *block_state = user_data;
915 g_mutex_lock (&check_mutex);
917 *block_state = BLOCKED;
918 g_cond_broadcast (&check_cond);
920 while (*block_state != UNBLOCK)
921 g_cond_wait (&check_cond, &check_mutex);
922 g_mutex_unlock (&check_mutex);
926 GST_START_TEST (test_play_multithreaded_block_in_describe)
928 GstRTSPConnection *conn;
929 GstRTSPMountPoints *mounts;
930 GstRTSPMediaFactory *factory;
931 gint block_state = BLOCK_ME;
932 GstRTSPMessage *request;
933 GstRTSPMessage *response;
934 GstRTSPStatusCode code;
936 gst_rtsp_server_set_max_threads (server, 2);
938 mounts = gst_rtsp_server_get_mount_points (server);
939 fail_unless (mounts != NULL);
940 factory = gst_rtsp_media_factory_new ();
941 gst_rtsp_media_factory_set_launch (factory,
942 "( " VIDEO_PIPELINE " " AUDIO_PIPELINE " )");
943 g_signal_connect (factory, "media-constructed",
944 G_CALLBACK (media_constructed_block), &block_state);
945 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT "2", factory);
946 g_object_unref (mounts);
950 conn = connect_to_server (test_port, TEST_MOUNT_POINT "2");
953 /* do describe, it will not return now as we've blocked it */
954 request = create_request (conn, GST_RTSP_DESCRIBE, NULL);
955 fail_unless (send_request (conn, request));
956 gst_rtsp_message_free (request);
958 g_mutex_lock (&check_mutex);
959 while (block_state != BLOCKED)
960 g_cond_wait (&check_cond, &check_mutex);
961 g_mutex_unlock (&check_mutex);
963 /* Do a second connection while the first one is blocked */
966 /* Now unblock the describe */
967 g_mutex_lock (&check_mutex);
968 block_state = UNBLOCK;
969 g_cond_broadcast (&check_cond);
970 g_mutex_unlock (&check_mutex);
972 response = read_response (conn);
973 gst_rtsp_message_parse_response (response, &code, NULL, NULL);
974 fail_unless (code == GST_RTSP_STS_OK);
975 gst_rtsp_message_free (response);
978 gst_rtsp_connection_free (conn);
988 new_session_timeout_one (GstRTSPClient * client,
989 GstRTSPSession * session, gpointer user_data)
991 gst_rtsp_session_set_timeout (session, 1);
993 g_signal_handlers_disconnect_by_func (client, new_session_timeout_one,
998 session_connected_new_session_cb (GstRTSPServer * server,
999 GstRTSPClient * client, gpointer user_data)
1002 g_signal_connect (client, "new-session", user_data, NULL);
1005 GST_START_TEST (test_play_multithreaded_timeout_client)
1007 GstRTSPConnection *conn;
1008 GstSDPMessage *sdp_message = NULL;
1009 const GstSDPMedia *sdp_media;
1010 const gchar *video_control;
1011 const gchar *audio_control;
1012 GstRTSPRange client_port;
1013 gchar *session = NULL;
1014 GstRTSPTransport *video_transport = NULL;
1015 GstRTSPTransport *audio_transport = NULL;
1016 GstRTSPSessionPool *pool;
1017 GstRTSPMessage *request;
1018 GstRTSPMessage *response;
1020 gst_rtsp_server_set_max_threads (server, 2);
1021 pool = gst_rtsp_server_get_session_pool (server);
1022 g_signal_connect (server, "client-connected",
1023 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1028 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1030 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1032 /* get control strings from DESCRIBE response */
1033 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1034 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1035 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1036 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1037 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1039 get_client_ports (&client_port);
1041 /* do SETUP for video and audio */
1042 fail_unless (do_setup (conn, video_control, &client_port, &session,
1043 &video_transport) == GST_RTSP_STS_OK);
1044 fail_unless (do_setup (conn, audio_control, &client_port, &session,
1045 &audio_transport) == GST_RTSP_STS_OK);
1047 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1049 /* send PLAY request and check that we get 200 OK */
1050 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1051 session) == GST_RTSP_STS_OK);
1055 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1058 /* send TEARDOWN request and check that we get 454 Session Not found */
1059 request = create_request (conn, GST_RTSP_TEARDOWN, NULL);
1060 gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session);
1061 fail_unless (send_request (conn, request));
1062 gst_rtsp_message_free (request);
1064 fail_unless (gst_rtsp_message_new (&response) == GST_RTSP_OK);
1065 fail_unless (gst_rtsp_connection_receive (conn, response, NULL) ==
1067 fail_unless (errno == ECONNRESET);
1068 gst_rtsp_message_free (response);
1070 /* clean up and iterate so the clean-up can finish */
1071 g_object_unref (pool);
1073 gst_rtsp_transport_free (video_transport);
1074 gst_rtsp_transport_free (audio_transport);
1075 gst_sdp_message_free (sdp_message);
1076 gst_rtsp_connection_free (conn);
1085 GST_START_TEST (test_play_multithreaded_timeout_session)
1087 GstRTSPConnection *conn;
1088 GstSDPMessage *sdp_message = NULL;
1089 const GstSDPMedia *sdp_media;
1090 const gchar *video_control;
1091 const gchar *audio_control;
1092 GstRTSPRange client_port;
1093 gchar *session1 = NULL;
1094 gchar *session2 = NULL;
1095 GstRTSPTransport *video_transport = NULL;
1096 GstRTSPTransport *audio_transport = NULL;
1097 GstRTSPSessionPool *pool;
1099 gst_rtsp_server_set_max_threads (server, 2);
1100 pool = gst_rtsp_server_get_session_pool (server);
1101 g_signal_connect (server, "client-connected",
1102 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1107 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1109 gst_rtsp_connection_set_remember_session_id (conn, FALSE);
1111 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1113 /* get control strings from DESCRIBE response */
1114 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1115 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1116 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1117 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1118 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1120 get_client_ports (&client_port);
1122 /* do SETUP for video and audio */
1123 fail_unless (do_setup (conn, video_control, &client_port, &session1,
1124 &video_transport) == GST_RTSP_STS_OK);
1125 fail_unless (do_setup (conn, audio_control, &client_port, &session2,
1126 &audio_transport) == GST_RTSP_STS_OK);
1128 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 2);
1130 /* send PLAY request and check that we get 200 OK */
1131 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1132 session1) == GST_RTSP_STS_OK);
1133 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1134 session2) == GST_RTSP_STS_OK);
1138 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1140 /* send TEARDOWN request and check that we get 454 Session Not found */
1141 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1142 session1) == GST_RTSP_STS_SESSION_NOT_FOUND);
1144 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1145 session2) == GST_RTSP_STS_OK);
1147 /* clean up and iterate so the clean-up can finish */
1148 g_object_unref (pool);
1151 gst_rtsp_transport_free (video_transport);
1152 gst_rtsp_transport_free (audio_transport);
1153 gst_sdp_message_free (sdp_message);
1154 gst_rtsp_connection_free (conn);
1163 GST_START_TEST (test_play_disconnect)
1165 GstRTSPConnection *conn;
1166 GstSDPMessage *sdp_message = NULL;
1167 const GstSDPMedia *sdp_media;
1168 const gchar *video_control;
1169 const gchar *audio_control;
1170 GstRTSPRange client_port;
1171 gchar *session = NULL;
1172 GstRTSPTransport *video_transport = NULL;
1173 GstRTSPTransport *audio_transport = NULL;
1174 GstRTSPSessionPool *pool;
1176 pool = gst_rtsp_server_get_session_pool (server);
1177 g_signal_connect (server, "client-connected",
1178 G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1182 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1184 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1186 /* get control strings from DESCRIBE response */
1187 fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1188 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1189 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1190 sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1191 audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1193 get_client_ports (&client_port);
1195 /* do SETUP for video and audio */
1196 fail_unless (do_setup (conn, video_control, &client_port, &session,
1197 &video_transport) == GST_RTSP_STS_OK);
1198 fail_unless (do_setup (conn, audio_control, &client_port, &session,
1199 &audio_transport) == GST_RTSP_STS_OK);
1201 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1203 /* send PLAY request and check that we get 200 OK */
1204 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1205 session) == GST_RTSP_STS_OK);
1207 gst_rtsp_connection_free (conn);
1211 fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1212 fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1215 /* clean up and iterate so the clean-up can finish */
1216 g_object_unref (pool);
1218 gst_rtsp_transport_free (video_transport);
1219 gst_rtsp_transport_free (audio_transport);
1220 gst_sdp_message_free (sdp_message);
1228 /* Only different with test_play is the specific ports selected */
1230 GST_START_TEST (test_play_specific_server_port)
1232 GstRTSPMountPoints *mounts;
1234 GstRTSPMediaFactory *factory;
1235 GstRTSPAddressPool *pool;
1236 GstRTSPConnection *conn;
1237 GstSDPMessage *sdp_message = NULL;
1238 const GstSDPMedia *sdp_media;
1239 const gchar *video_control;
1240 GstRTSPRange client_port;
1241 gchar *session = NULL;
1242 GstRTSPTransport *video_transport = NULL;
1243 GSocket *rtp_socket, *rtcp_socket;
1244 GSocketAddress *rtp_address, *rtcp_address;
1245 guint16 rtp_port, rtcp_port;
1247 mounts = gst_rtsp_server_get_mount_points (server);
1249 factory = gst_rtsp_media_factory_new ();
1250 pool = gst_rtsp_address_pool_new ();
1251 gst_rtsp_address_pool_add_range_unicast (pool, GST_RTSP_ADDRESS_POOL_ANY_IPV4,
1252 GST_RTSP_ADDRESS_POOL_ANY_IPV4, 7770, 7780);
1253 gst_rtsp_media_factory_set_address_pool (factory, pool);
1254 g_object_unref (pool);
1255 gst_rtsp_media_factory_set_launch (factory, "( " VIDEO_PIPELINE " )");
1256 gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
1257 g_object_unref (mounts);
1260 test_port = get_unused_port (SOCK_STREAM);
1261 service = g_strdup_printf ("%d", test_port);
1262 gst_rtsp_server_set_service (server, service);
1265 /* attach to default main context */
1266 source_id = gst_rtsp_server_attach (server, NULL);
1267 fail_if (source_id == 0);
1269 GST_DEBUG ("rtsp server listening on port %d", test_port);
1272 conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1274 sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1276 /* get control strings from DESCRIBE response */
1277 fail_unless (gst_sdp_message_medias_len (sdp_message) == 1);
1278 sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1279 video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1281 get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
1283 /* do SETUP for video */
1284 fail_unless (do_setup (conn, video_control, &client_port, &session,
1285 &video_transport) == GST_RTSP_STS_OK);
1287 /* send PLAY request and check that we get 200 OK */
1288 fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1289 session) == GST_RTSP_STS_OK);
1291 receive_rtp (rtp_socket, &rtp_address);
1292 receive_rtcp (rtcp_socket, &rtcp_address, 0);
1294 fail_unless (G_IS_INET_SOCKET_ADDRESS (rtp_address));
1295 fail_unless (G_IS_INET_SOCKET_ADDRESS (rtcp_address));
1297 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_address));
1299 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtcp_address));
1300 fail_unless (rtp_port >= 7770 && rtp_port <= 7780 && rtp_port % 2 == 0);
1301 fail_unless (rtcp_port >= 7770 && rtcp_port <= 7780 && rtcp_port % 2 == 1);
1302 fail_unless (rtp_port + 1 == rtcp_port);
1304 g_object_unref (rtp_address);
1305 g_object_unref (rtcp_address);
1307 /* send TEARDOWN request and check that we get 200 OK */
1308 fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1309 session) == GST_RTSP_STS_OK);
1311 /* FIXME: The rtsp-server always disconnects the transport before
1312 * sending the RTCP BYE
1313 * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
1316 /* clean up and iterate so the clean-up can finish */
1317 g_object_unref (rtp_socket);
1318 g_object_unref (rtcp_socket);
1320 gst_rtsp_transport_free (video_transport);
1321 gst_sdp_message_free (sdp_message);
1322 gst_rtsp_connection_free (conn);
1332 GST_START_TEST (test_play_smpte_range)
1336 do_test_play ("npt=5-");
1337 do_test_play ("smpte=0:00:00-");
1338 do_test_play ("smpte=1:00:00-");
1339 do_test_play ("smpte=1:00:03-");
1340 do_test_play ("clock=20120321T152256Z-");
1350 rtspserver_suite (void)
1352 Suite *s = suite_create ("rtspserver");
1353 TCase *tc = tcase_create ("general");
1355 suite_add_tcase (s, tc);
1356 tcase_add_checked_fixture (tc, setup, teardown);
1357 tcase_set_timeout (tc, 20);
1358 tcase_add_test (tc, test_connect);
1359 tcase_add_test (tc, test_describe);
1360 tcase_add_test (tc, test_describe_non_existing_mount_point);
1361 tcase_add_test (tc, test_setup);
1362 tcase_add_test (tc, test_setup_non_existing_stream);
1363 tcase_add_test (tc, test_play);
1364 tcase_add_test (tc, test_play_without_session);
1365 tcase_add_test (tc, test_bind_already_in_use);
1366 tcase_add_test (tc, test_play_multithreaded);
1367 tcase_add_test (tc, test_play_multithreaded_block_in_describe);
1368 tcase_add_test (tc, test_play_multithreaded_timeout_client);
1369 tcase_add_test (tc, test_play_multithreaded_timeout_session);
1370 tcase_add_test (tc, test_play_disconnect);
1371 tcase_add_test (tc, test_play_specific_server_port);
1372 tcase_add_test (tc, test_play_smpte_range);
1376 GST_CHECK_MAIN (rtspserver);