d268b66420e486b27867f2adcbc5c093fb1c8f4e
[platform/upstream/gstreamer.git] / tests / check / gst / rtspserver.c
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>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <gst/check/gstcheck.h>
24 #include <gst/sdp/gstsdpmessage.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 #include <gst/rtp/gstrtcpbuffer.h>
27
28 #include <stdio.h>
29 #include <netinet/in.h>
30
31 #include "rtsp-server.h"
32
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"
39
40 #define TEST_MOUNT_POINT  "/test"
41 #define TEST_PROTO        "RTP/AVP"
42 #define TEST_PROTO_TCP    "RTP/AVP/TCP"
43 #define TEST_ENCODING     "X-GST"
44 #define TEST_CLOCK_RATE   "90000"
45
46 /* tested rtsp server */
47 static GstRTSPServer *server = NULL;
48
49 /* tcp port that the test server listens for rtsp requests on */
50 static gint test_port = 0;
51
52 /* id of the server's source within the GMainContext */
53 static guint source_id;
54
55 /* iterate the default main loop until there are no events to dispatch */
56 static void
57 iterate (void)
58 {
59   while (g_main_context_iteration (NULL, FALSE)) {
60     GST_DEBUG ("iteration");
61   }
62 }
63
64 static void
65 get_client_ports_full (GstRTSPRange * range, GSocket ** rtp_socket,
66     GSocket ** rtcp_socket)
67 {
68   GSocket *rtp = NULL;
69   GSocket *rtcp = NULL;
70   gint rtp_port = 0;
71   gint rtcp_port;
72   GInetAddress *anyaddr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
73   GSocketAddress *sockaddr;
74   gboolean bound;
75
76   for (;;) {
77     if (rtp_port != 0)
78       rtp_port += 2;
79
80     rtp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
81         G_SOCKET_PROTOCOL_UDP, NULL);
82     fail_unless (rtp != NULL);
83
84     sockaddr = g_inet_socket_address_new (anyaddr, rtp_port);
85     fail_unless (sockaddr != NULL);
86     bound = g_socket_bind (rtp, sockaddr, FALSE, NULL);
87     g_object_unref (sockaddr);
88     if (!bound) {
89       g_object_unref (rtp);
90       continue;
91     }
92
93     sockaddr = g_socket_get_local_address (rtp, NULL);
94     fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
95     rtp_port =
96         g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
97     g_object_unref (sockaddr);
98
99     if (rtp_port % 2 != 0) {
100       rtp_port += 1;
101       g_object_unref (rtp);
102       continue;
103     }
104
105     rtcp_port = rtp_port + 1;
106
107     rtcp = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
108         G_SOCKET_PROTOCOL_UDP, NULL);
109     fail_unless (rtcp != NULL);
110
111     sockaddr = g_inet_socket_address_new (anyaddr, rtcp_port);
112     fail_unless (sockaddr != NULL);
113     bound = g_socket_bind (rtcp, sockaddr, FALSE, NULL);
114     g_object_unref (sockaddr);
115     if (!bound) {
116       g_object_unref (rtp);
117       g_object_unref (rtcp);
118       continue;
119     }
120
121     sockaddr = g_socket_get_local_address (rtcp, NULL);
122     fail_unless (sockaddr != NULL && G_IS_INET_SOCKET_ADDRESS (sockaddr));
123     fail_unless (rtcp_port ==
124         g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr)));
125     g_object_unref (sockaddr);
126
127     break;
128   }
129
130   range->min = rtp_port;
131   range->max = rtcp_port;
132   if (rtp_socket)
133     *rtp_socket = rtp;
134   else
135     g_object_unref (rtp);
136   if (rtcp_socket)
137     *rtcp_socket = rtcp;
138   else
139     g_object_unref (rtcp);
140   GST_DEBUG ("client_port=%d-%d", range->min, range->max);
141   g_object_unref (anyaddr);
142 }
143
144 /* get a free rtp/rtcp client port pair */
145 static void
146 get_client_ports (GstRTSPRange * range)
147 {
148   get_client_ports_full (range, NULL, NULL);
149 }
150
151 /* start the tested rtsp server */
152 static void
153 start_server (void)
154 {
155   GstRTSPMountPoints *mounts;
156   gchar *service;
157   GstRTSPMediaFactory *factory;
158
159   mounts = gst_rtsp_server_get_mount_points (server);
160
161   factory = gst_rtsp_media_factory_new ();
162
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);
167
168   /* set port to any */
169   gst_rtsp_server_set_service (server, "0");
170
171   /* attach to default main context */
172   source_id = gst_rtsp_server_attach (server, NULL);
173   fail_if (source_id == 0);
174
175   /* get port */
176   service = gst_rtsp_server_get_service (server);
177   test_port = atoi (service);
178   fail_unless (test_port != 0);
179   g_free (service);
180
181   GST_DEBUG ("rtsp server listening on port %d", test_port);
182 }
183
184 /* start the testing rtsp server for RECORD mode */
185 static GstRTSPMediaFactory *
186 start_record_server (const gchar * launch_line)
187 {
188   GstRTSPMediaFactory *factory;
189   GstRTSPMountPoints *mounts;
190   gchar *service;
191
192   mounts = gst_rtsp_server_get_mount_points (server);
193
194   factory = gst_rtsp_media_factory_new ();
195
196   gst_rtsp_media_factory_set_record (factory, TRUE);
197   gst_rtsp_media_factory_set_launch (factory, launch_line);
198   gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
199   g_object_unref (mounts);
200
201   /* set port to any */
202   gst_rtsp_server_set_service (server, "0");
203
204   /* attach to default main context */
205   source_id = gst_rtsp_server_attach (server, NULL);
206   fail_if (source_id == 0);
207
208   /* get port */
209   service = gst_rtsp_server_get_service (server);
210   test_port = atoi (service);
211   fail_unless (test_port != 0);
212   g_free (service);
213
214   GST_DEBUG ("rtsp server listening on port %d", test_port);
215   return factory;
216 }
217
218 /* stop the tested rtsp server */
219 static void
220 stop_server (void)
221 {
222   g_source_remove (source_id);
223   source_id = 0;
224
225   GST_DEBUG ("rtsp server stopped");
226 }
227
228 /* create an rtsp connection to the server on test_port */
229 static GstRTSPConnection *
230 connect_to_server (gint port, const gchar * mount_point)
231 {
232   GstRTSPConnection *conn = NULL;
233   gchar *address;
234   gchar *uri_string;
235   GstRTSPUrl *url = NULL;
236
237   address = gst_rtsp_server_get_address (server);
238   uri_string = g_strdup_printf ("rtsp://%s:%d%s", address, port, mount_point);
239   g_free (address);
240   fail_unless (gst_rtsp_url_parse (uri_string, &url) == GST_RTSP_OK);
241   g_free (uri_string);
242
243   fail_unless (gst_rtsp_connection_create (url, &conn) == GST_RTSP_OK);
244   gst_rtsp_url_free (url);
245
246   fail_unless (gst_rtsp_connection_connect (conn, NULL) == GST_RTSP_OK);
247
248   return conn;
249 }
250
251 /* create an rtsp request */
252 static GstRTSPMessage *
253 create_request (GstRTSPConnection * conn, GstRTSPMethod method,
254     const gchar * control)
255 {
256   GstRTSPMessage *request = NULL;
257   gchar *base_uri;
258   gchar *full_uri;
259
260   base_uri = gst_rtsp_url_get_request_uri (gst_rtsp_connection_get_url (conn));
261   full_uri = g_strdup_printf ("%s/%s", base_uri, control ? control : "");
262   g_free (base_uri);
263   if (gst_rtsp_message_new_request (&request, method, full_uri) != GST_RTSP_OK) {
264     GST_DEBUG ("failed to create request object");
265     g_free (full_uri);
266     return NULL;
267   }
268   g_free (full_uri);
269   return request;
270 }
271
272 /* send an rtsp request */
273 static gboolean
274 send_request (GstRTSPConnection * conn, GstRTSPMessage * request)
275 {
276   if (gst_rtsp_connection_send (conn, request, NULL) != GST_RTSP_OK) {
277     GST_DEBUG ("failed to send request");
278     return FALSE;
279   }
280   return TRUE;
281 }
282
283 /* read rtsp response. response must be freed by the caller */
284 static GstRTSPMessage *
285 read_response (GstRTSPConnection * conn)
286 {
287   GstRTSPMessage *response = NULL;
288
289   if (gst_rtsp_message_new (&response) != GST_RTSP_OK) {
290     GST_DEBUG ("failed to create response object");
291     return NULL;
292   }
293   if (gst_rtsp_connection_receive (conn, response, NULL) != GST_RTSP_OK) {
294     GST_DEBUG ("failed to read response");
295     gst_rtsp_message_free (response);
296     return NULL;
297   }
298   fail_unless (gst_rtsp_message_get_type (response) ==
299       GST_RTSP_MESSAGE_RESPONSE);
300   return response;
301 }
302
303 /* send an rtsp request and receive response. gchar** parameters are out
304  * parameters that have to be freed by the caller */
305 static GstRTSPStatusCode
306 do_request_full (GstRTSPConnection * conn, GstRTSPMethod method,
307     const gchar * control, const gchar * session_in, const gchar * transport_in,
308     const gchar * range_in, const gchar * require_in,
309     gchar ** content_type, gchar ** content_base, gchar ** body,
310     gchar ** session_out, gchar ** transport_out, gchar ** range_out,
311     gchar ** unsupported_out)
312 {
313   GstRTSPMessage *request;
314   GstRTSPMessage *response;
315   GstRTSPStatusCode code;
316   gchar *value;
317
318   /* create request */
319   request = create_request (conn, method, control);
320
321   /* add headers */
322   if (session_in) {
323     gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session_in);
324   }
325   if (transport_in) {
326     gst_rtsp_message_add_header (request, GST_RTSP_HDR_TRANSPORT, transport_in);
327   }
328   if (range_in) {
329     gst_rtsp_message_add_header (request, GST_RTSP_HDR_RANGE, range_in);
330   }
331   if (require_in) {
332     gst_rtsp_message_add_header (request, GST_RTSP_HDR_REQUIRE, require_in);
333   }
334
335   /* send request */
336   fail_unless (send_request (conn, request));
337   gst_rtsp_message_free (request);
338
339   iterate ();
340
341   /* read response */
342   response = read_response (conn);
343
344   /* check status line */
345   gst_rtsp_message_parse_response (response, &code, NULL, NULL);
346   if (code != GST_RTSP_STS_OK) {
347     if (unsupported_out != NULL && code == GST_RTSP_STS_OPTION_NOT_SUPPORTED) {
348       gst_rtsp_message_get_header (response, GST_RTSP_HDR_UNSUPPORTED,
349           &value, 0);
350       *unsupported_out = g_strdup (value);
351     }
352     gst_rtsp_message_free (response);
353     return code;
354   }
355
356   /* get information from response */
357   if (content_type) {
358     gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_TYPE,
359         &value, 0);
360     *content_type = g_strdup (value);
361   }
362   if (content_base) {
363     gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
364         &value, 0);
365     *content_base = g_strdup (value);
366   }
367   if (body) {
368     *body = g_malloc (response->body_size + 1);
369     strncpy (*body, (gchar *) response->body, response->body_size);
370   }
371   if (session_out) {
372     gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &value, 0);
373
374     value = g_strdup (value);
375
376     /* Remove the timeout */
377     if (value) {
378       char *pos = strchr (value, ';');
379       if (pos)
380         *pos = 0;
381     }
382     if (session_in) {
383       /* check that we got the same session back */
384       fail_unless (!g_strcmp0 (value, session_in));
385     }
386     *session_out = value;
387   }
388   if (transport_out) {
389     gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &value, 0);
390     *transport_out = g_strdup (value);
391   }
392   if (range_out) {
393     gst_rtsp_message_get_header (response, GST_RTSP_HDR_RANGE, &value, 0);
394     *range_out = g_strdup (value);
395   }
396
397   gst_rtsp_message_free (response);
398   return code;
399 }
400
401 /* send an rtsp request and receive response. gchar** parameters are out
402  * parameters that have to be freed by the caller */
403 static GstRTSPStatusCode
404 do_request (GstRTSPConnection * conn, GstRTSPMethod method,
405     const gchar * control, const gchar * session_in,
406     const gchar * transport_in, const gchar * range_in,
407     gchar ** content_type, gchar ** content_base, gchar ** body,
408     gchar ** session_out, gchar ** transport_out, gchar ** range_out)
409 {
410   return do_request_full (conn, method, control, session_in, transport_in,
411       range_in, NULL, content_type, content_base, body, session_out,
412       transport_out, range_out, NULL);
413 }
414
415 /* send an rtsp request with a method and a session, and receive response */
416 static GstRTSPStatusCode
417 do_simple_request (GstRTSPConnection * conn, GstRTSPMethod method,
418     const gchar * session)
419 {
420   return do_request (conn, method, NULL, session, NULL, NULL, NULL,
421       NULL, NULL, NULL, NULL, NULL);
422 }
423
424 /* send a DESCRIBE request and receive response. returns a received
425  * GstSDPMessage that must be freed by the caller */
426 static GstSDPMessage *
427 do_describe (GstRTSPConnection * conn, const gchar * mount_point)
428 {
429   GstSDPMessage *sdp_message;
430   gchar *content_type;
431   gchar *content_base;
432   gchar *body;
433   gchar *address;
434   gchar *expected_content_base;
435
436   /* send DESCRIBE request */
437   fail_unless (do_request (conn, GST_RTSP_DESCRIBE, NULL, NULL, NULL, NULL,
438           &content_type, &content_base, &body, NULL, NULL, NULL) ==
439       GST_RTSP_STS_OK);
440
441   /* check response values */
442   fail_unless (!g_strcmp0 (content_type, "application/sdp"));
443   address = gst_rtsp_server_get_address (server);
444   expected_content_base =
445       g_strdup_printf ("rtsp://%s:%d%s/", address, test_port, mount_point);
446   fail_unless (!g_strcmp0 (content_base, expected_content_base));
447
448   /* create sdp message */
449   fail_unless (gst_sdp_message_new (&sdp_message) == GST_SDP_OK);
450   fail_unless (gst_sdp_message_parse_buffer ((guint8 *) body,
451           strlen (body), sdp_message) == GST_SDP_OK);
452
453   /* clean up */
454   g_free (content_type);
455   g_free (content_base);
456   g_free (body);
457   g_free (address);
458   g_free (expected_content_base);
459
460   return sdp_message;
461 }
462
463 /* send a SETUP request and receive response. if *session is not NULL,
464  * it is used in the request. otherwise, *session is set to a returned
465  * session string that must be freed by the caller. the returned
466  * transport must be freed by the caller. */
467 static GstRTSPStatusCode
468 do_setup_full (GstRTSPConnection * conn, const gchar * control,
469     gboolean use_tcp_transport, const GstRTSPRange * client_ports,
470     const gchar * require, gchar ** session, GstRTSPTransport ** transport,
471     gchar ** unsupported)
472 {
473   GstRTSPStatusCode code;
474   gchar *session_in = NULL;
475   gchar *transport_string_in = NULL;
476   gchar **session_out = NULL;
477   gchar *transport_string_out = NULL;
478
479   /* prepare and send SETUP request */
480   if (session) {
481     if (*session) {
482       session_in = *session;
483     } else {
484       session_out = session;
485     }
486   }
487
488   if (use_tcp_transport) {
489     transport_string_in = g_strdup_printf (TEST_PROTO_TCP ";unicast");
490   } else {
491     transport_string_in =
492         g_strdup_printf (TEST_PROTO ";unicast;client_port=%d-%d",
493         client_ports->min, client_ports->max);
494   }
495   code =
496       do_request_full (conn, GST_RTSP_SETUP, control, session_in,
497       transport_string_in, NULL, require, NULL, NULL, NULL, session_out,
498       &transport_string_out, NULL, unsupported);
499   g_free (transport_string_in);
500
501   if (transport_string_out) {
502     /* create transport */
503     fail_unless (gst_rtsp_transport_new (transport) == GST_RTSP_OK);
504     fail_unless (gst_rtsp_transport_parse (transport_string_out,
505             *transport) == GST_RTSP_OK);
506     g_free (transport_string_out);
507   }
508   GST_INFO ("code=%d", code);
509   return code;
510 }
511
512 /* send a SETUP request and receive response. if *session is not NULL,
513  * it is used in the request. otherwise, *session is set to a returned
514  * session string that must be freed by the caller. the returned
515  * transport must be freed by the caller. */
516 static GstRTSPStatusCode
517 do_setup (GstRTSPConnection * conn, const gchar * control,
518     const GstRTSPRange * client_ports, gchar ** session,
519     GstRTSPTransport ** transport)
520 {
521   return do_setup_full (conn, control, FALSE, client_ports, NULL, session,
522       transport, NULL);
523 }
524
525 /* send a SETUP request and receive response. if *session is not NULL,
526  * it is used in the request. otherwise, *session is set to a returned
527  * session string that must be freed by the caller. the returned
528  * transport must be freed by the caller. */
529 static GstRTSPStatusCode
530 do_setup_tcp (GstRTSPConnection * conn, const gchar * control,
531     gchar ** session, GstRTSPTransport ** transport)
532 {
533   return do_setup_full (conn, control, TRUE, NULL, NULL, session, transport,
534       NULL);
535 }
536
537 /* fixture setup function */
538 static void
539 setup (void)
540 {
541   server = gst_rtsp_server_new ();
542 }
543
544 /* fixture clean-up function */
545 static void
546 teardown (void)
547 {
548   if (server) {
549     g_object_unref (server);
550     server = NULL;
551   }
552   test_port = 0;
553 }
554
555 GST_START_TEST (test_connect)
556 {
557   GstRTSPConnection *conn;
558
559   start_server ();
560
561   /* connect to server */
562   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
563
564   /* clean up */
565   gst_rtsp_connection_free (conn);
566   stop_server ();
567
568   /* iterate so the clean-up can finish */
569   iterate ();
570 }
571
572 GST_END_TEST;
573
574 GST_START_TEST (test_describe)
575 {
576   GstRTSPConnection *conn;
577   GstSDPMessage *sdp_message = NULL;
578   const GstSDPMedia *sdp_media;
579   gint32 format;
580   gchar *expected_rtpmap;
581   const gchar *rtpmap;
582   const gchar *control_video;
583   const gchar *control_audio;
584
585   start_server ();
586
587   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
588
589   /* send DESCRIBE request */
590   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
591
592   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
593
594   /* check video sdp */
595   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
596   fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
597   fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
598   sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
599       &format);
600   expected_rtpmap =
601       g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
602   rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
603   fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
604   g_free (expected_rtpmap);
605   control_video = gst_sdp_media_get_attribute_val (sdp_media, "control");
606   fail_unless (!g_strcmp0 (control_video, "stream=0"));
607
608   /* check audio sdp */
609   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
610   fail_unless (!g_strcmp0 (gst_sdp_media_get_proto (sdp_media), TEST_PROTO));
611   fail_unless (gst_sdp_media_formats_len (sdp_media) == 1);
612   sscanf (gst_sdp_media_get_format (sdp_media, 0), "%" G_GINT32_FORMAT,
613       &format);
614   expected_rtpmap =
615       g_strdup_printf ("%d " TEST_ENCODING "/" TEST_CLOCK_RATE, format);
616   rtpmap = gst_sdp_media_get_attribute_val (sdp_media, "rtpmap");
617   fail_unless (!g_strcmp0 (rtpmap, expected_rtpmap));
618   g_free (expected_rtpmap);
619   control_audio = gst_sdp_media_get_attribute_val (sdp_media, "control");
620   fail_unless (!g_strcmp0 (control_audio, "stream=1"));
621
622   /* clean up and iterate so the clean-up can finish */
623   gst_sdp_message_free (sdp_message);
624   gst_rtsp_connection_free (conn);
625   stop_server ();
626   iterate ();
627 }
628
629 GST_END_TEST;
630
631 GST_START_TEST (test_describe_record_media)
632 {
633   GstRTSPConnection *conn;
634
635   start_record_server ("( fakesink name=depay0 )");
636
637   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
638
639   /* send DESCRIBE request */
640   fail_unless_equals_int (do_request (conn, GST_RTSP_DESCRIBE, NULL, NULL, NULL,
641           NULL, NULL, NULL, NULL, NULL, NULL, NULL),
642       GST_RTSP_STS_METHOD_NOT_ALLOWED);
643
644   /* clean up and iterate so the clean-up can finish */
645   gst_rtsp_connection_free (conn);
646   stop_server ();
647   iterate ();
648 }
649
650 GST_END_TEST;
651
652 GST_START_TEST (test_describe_non_existing_mount_point)
653 {
654   GstRTSPConnection *conn;
655
656   start_server ();
657
658   /* send DESCRIBE request for a non-existing mount point
659    * and check that we get a 404 Not Found */
660   conn = connect_to_server (test_port, "/non-existing");
661   fail_unless (do_simple_request (conn, GST_RTSP_DESCRIBE, NULL)
662       == GST_RTSP_STS_NOT_FOUND);
663
664   /* clean up and iterate so the clean-up can finish */
665   gst_rtsp_connection_free (conn);
666   stop_server ();
667   iterate ();
668 }
669
670 GST_END_TEST;
671
672 GST_START_TEST (test_setup)
673 {
674   GstRTSPConnection *conn;
675   GstSDPMessage *sdp_message = NULL;
676   const GstSDPMedia *sdp_media;
677   const gchar *video_control;
678   const gchar *audio_control;
679   GstRTSPRange client_ports;
680   gchar *session = NULL;
681   GstRTSPTransport *video_transport = NULL;
682   GstRTSPTransport *audio_transport = NULL;
683
684   start_server ();
685
686   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
687
688   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
689
690   /* get control strings from DESCRIBE response */
691   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
692   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
693   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
694   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
695   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
696
697   get_client_ports (&client_ports);
698
699   /* send SETUP request for video */
700   fail_unless (do_setup (conn, video_control, &client_ports, &session,
701           &video_transport) == GST_RTSP_STS_OK);
702   GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
703
704   /* check response from SETUP */
705   fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
706   fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
707   fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
708   fail_unless (video_transport->mode_play);
709   gst_rtsp_transport_free (video_transport);
710
711   /* send SETUP request for audio */
712   fail_unless (do_setup (conn, audio_control, &client_ports, &session,
713           &audio_transport) == GST_RTSP_STS_OK);
714   GST_DEBUG ("set up audio %s with session '%s'", audio_control, session);
715
716   /* check response from SETUP */
717   fail_unless (audio_transport->trans == GST_RTSP_TRANS_RTP);
718   fail_unless (audio_transport->profile == GST_RTSP_PROFILE_AVP);
719   fail_unless (audio_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
720   fail_unless (audio_transport->mode_play);
721   gst_rtsp_transport_free (audio_transport);
722
723   /* send TEARDOWN request and check that we get 200 OK */
724   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
725           session) == GST_RTSP_STS_OK);
726
727   /* clean up and iterate so the clean-up can finish */
728   g_free (session);
729   gst_sdp_message_free (sdp_message);
730   gst_rtsp_connection_free (conn);
731   stop_server ();
732   iterate ();
733 }
734
735 GST_END_TEST;
736
737 GST_START_TEST (test_setup_tcp)
738 {
739   GstRTSPConnection *conn;
740   GstSDPMessage *sdp_message = NULL;
741   const GstSDPMedia *sdp_media;
742   const gchar *video_control;
743   const gchar *audio_control;
744   gchar *session = NULL;
745   GstRTSPTransport *video_transport = NULL;
746   GstRTSPTransport *audio_transport = NULL;
747
748   start_server ();
749
750   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
751
752   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
753
754   /* get control strings from DESCRIBE response */
755   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
756   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
757   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
758   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
759   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
760
761   /* send SETUP request for video */
762   fail_unless (do_setup_tcp (conn, video_control, &session,
763           &video_transport) == GST_RTSP_STS_OK);
764   GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
765
766   /* check response from SETUP */
767   fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
768   fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
769   fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP);
770   fail_unless (video_transport->mode_play);
771   gst_rtsp_transport_free (video_transport);
772
773   /* send SETUP request for audio */
774   fail_unless (do_setup_tcp (conn, audio_control, &session,
775           &audio_transport) == GST_RTSP_STS_OK);
776   GST_DEBUG ("set up audio %s with session '%s'", audio_control, session);
777
778   /* check response from SETUP */
779   fail_unless (audio_transport->trans == GST_RTSP_TRANS_RTP);
780   fail_unless (audio_transport->profile == GST_RTSP_PROFILE_AVP);
781   fail_unless (audio_transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP);
782   fail_unless (audio_transport->mode_play);
783   gst_rtsp_transport_free (audio_transport);
784
785   /* send TEARDOWN request and check that we get 200 OK */
786   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
787           session) == GST_RTSP_STS_OK);
788
789   /* clean up and iterate so the clean-up can finish */
790   g_free (session);
791   gst_sdp_message_free (sdp_message);
792   gst_rtsp_connection_free (conn);
793   stop_server ();
794   iterate ();
795 }
796
797 GST_END_TEST;
798
799 GST_START_TEST (test_setup_twice)
800 {
801   GstRTSPConnection *conn;
802   GstSDPMessage *sdp_message;
803   const GstSDPMedia *sdp_media;
804   const gchar *video_control;
805   GstRTSPRange client_ports;
806   GstRTSPTransport *video_transport = NULL;
807   gchar *session1 = NULL;
808   gchar *session2 = NULL;
809
810   start_server ();
811
812   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
813
814   /* we wan't more than one session for this connection */
815   gst_rtsp_connection_set_remember_session_id (conn, FALSE);
816
817   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
818
819   /* get the control url */
820   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
821   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
822   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
823
824   get_client_ports (&client_ports);
825
826   /* send SETUP request for one session */
827   fail_unless (do_setup (conn, video_control, &client_ports, &session1,
828           &video_transport) == GST_RTSP_STS_OK);
829   GST_DEBUG ("set up video %s, got session '%s'", video_control, session1);
830
831   /* check response from SETUP */
832   fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
833   fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
834   fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
835   fail_unless (video_transport->mode_play);
836   gst_rtsp_transport_free (video_transport);
837
838   /* send SETUP request for another session */
839   fail_unless (do_setup (conn, video_control, &client_ports, &session2,
840           &video_transport) == GST_RTSP_STS_OK);
841   GST_DEBUG ("set up video %s, got session '%s'", video_control, session2);
842
843   /* check response from SETUP */
844   fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
845   fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
846   fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
847   fail_unless (video_transport->mode_play);
848   gst_rtsp_transport_free (video_transport);
849
850   /* session can not be the same */
851   fail_unless (strcmp (session1, session2));
852
853   /* send TEARDOWN request for the first session */
854   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
855           session1) == GST_RTSP_STS_OK);
856
857   /* send TEARDOWN request for the second session */
858   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
859           session2) == GST_RTSP_STS_OK);
860
861   g_free (session1);
862   g_free (session2);
863   gst_sdp_message_free (sdp_message);
864   gst_rtsp_connection_free (conn);
865   stop_server ();
866   iterate ();
867 }
868
869 GST_END_TEST;
870
871 GST_START_TEST (test_setup_with_require_header)
872 {
873   GstRTSPConnection *conn;
874   GstSDPMessage *sdp_message = NULL;
875   const GstSDPMedia *sdp_media;
876   const gchar *video_control;
877   GstRTSPRange client_ports;
878   gchar *session = NULL;
879   gchar *unsupported = NULL;
880   GstRTSPTransport *video_transport = NULL;
881
882   start_server ();
883
884   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
885
886   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
887
888   /* get control strings from DESCRIBE response */
889   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
890   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
891   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
892
893   get_client_ports (&client_ports);
894
895   /* send SETUP request for video, with single Require header */
896   fail_unless_equals_int (do_setup_full (conn, video_control, FALSE,
897           &client_ports, "funky-feature", &session, &video_transport,
898           &unsupported), GST_RTSP_STS_OPTION_NOT_SUPPORTED);
899   fail_unless_equals_string (unsupported, "funky-feature");
900   g_free (unsupported);
901   unsupported = NULL;
902
903   /* send SETUP request for video, with multiple Require headers */
904   fail_unless_equals_int (do_setup_full (conn, video_control, FALSE,
905           &client_ports, "funky-feature, foo-bar, superburst", &session,
906           &video_transport, &unsupported), GST_RTSP_STS_OPTION_NOT_SUPPORTED);
907   fail_unless_equals_string (unsupported, "funky-feature, foo-bar, superburst");
908   g_free (unsupported);
909   unsupported = NULL;
910
911   /* ok, just do a normal setup then (make sure that still works) */
912   fail_unless_equals_int (do_setup (conn, video_control, &client_ports,
913           &session, &video_transport), GST_RTSP_STS_OK);
914
915   GST_DEBUG ("set up video %s, got session '%s'", video_control, session);
916
917   /* check response from SETUP */
918   fail_unless (video_transport->trans == GST_RTSP_TRANS_RTP);
919   fail_unless (video_transport->profile == GST_RTSP_PROFILE_AVP);
920   fail_unless (video_transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP);
921   fail_unless (video_transport->mode_play);
922   gst_rtsp_transport_free (video_transport);
923
924   /* send TEARDOWN request and check that we get 200 OK */
925   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
926           session) == GST_RTSP_STS_OK);
927
928   /* clean up and iterate so the clean-up can finish */
929   g_free (session);
930   gst_sdp_message_free (sdp_message);
931   gst_rtsp_connection_free (conn);
932   stop_server ();
933   iterate ();
934 }
935
936 GST_END_TEST;
937
938 GST_START_TEST (test_setup_non_existing_stream)
939 {
940   GstRTSPConnection *conn;
941   GstRTSPRange client_ports;
942
943   start_server ();
944
945   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
946
947   get_client_ports (&client_ports);
948
949   /* send SETUP request with a non-existing stream and check that we get a
950    * 404 Not Found */
951   fail_unless (do_setup (conn, "stream=7", &client_ports, NULL,
952           NULL) == GST_RTSP_STS_NOT_FOUND);
953
954   /* clean up and iterate so the clean-up can finish */
955   gst_rtsp_connection_free (conn);
956   stop_server ();
957   iterate ();
958 }
959
960 GST_END_TEST;
961
962 static void
963 receive_rtp (GSocket * socket, GSocketAddress ** addr)
964 {
965   GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
966
967   for (;;) {
968     gssize bytes;
969     GstMapInfo map = GST_MAP_INFO_INIT;
970     GstRTPBuffer rtpbuffer = GST_RTP_BUFFER_INIT;
971
972     gst_buffer_map (buffer, &map, GST_MAP_WRITE);
973     bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
974         map.maxsize, NULL, NULL);
975     fail_unless (bytes > 0);
976     gst_buffer_unmap (buffer, &map);
977     gst_buffer_set_size (buffer, bytes);
978
979     if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtpbuffer)) {
980       gst_rtp_buffer_unmap (&rtpbuffer);
981       break;
982     }
983
984     if (addr)
985       g_clear_object (addr);
986   }
987
988   gst_buffer_unref (buffer);
989 }
990
991 static void
992 receive_rtcp (GSocket * socket, GSocketAddress ** addr, GstRTCPType type)
993 {
994   GstBuffer *buffer = gst_buffer_new_allocate (NULL, 65536, NULL);
995
996   for (;;) {
997     gssize bytes;
998     GstMapInfo map = GST_MAP_INFO_INIT;
999
1000     gst_buffer_map (buffer, &map, GST_MAP_WRITE);
1001     bytes = g_socket_receive_from (socket, addr, (gchar *) map.data,
1002         map.maxsize, NULL, NULL);
1003     fail_unless (bytes > 0);
1004     gst_buffer_unmap (buffer, &map);
1005     gst_buffer_set_size (buffer, bytes);
1006
1007     if (gst_rtcp_buffer_validate (buffer)) {
1008       GstRTCPBuffer rtcpbuffer = GST_RTCP_BUFFER_INIT;
1009       GstRTCPPacket packet;
1010
1011       if (type) {
1012         fail_unless (gst_rtcp_buffer_map (buffer, GST_MAP_READ, &rtcpbuffer));
1013         fail_unless (gst_rtcp_buffer_get_first_packet (&rtcpbuffer, &packet));
1014         do {
1015           if (gst_rtcp_packet_get_type (&packet) == type) {
1016             gst_rtcp_buffer_unmap (&rtcpbuffer);
1017             goto done;
1018           }
1019         } while (gst_rtcp_packet_move_to_next (&packet));
1020         gst_rtcp_buffer_unmap (&rtcpbuffer);
1021       } else {
1022         break;
1023       }
1024     }
1025
1026     if (addr)
1027       g_clear_object (addr);
1028   }
1029
1030 done:
1031
1032   gst_buffer_unref (buffer);
1033 }
1034
1035 static void
1036 do_test_play (const gchar * range)
1037 {
1038   GstRTSPConnection *conn;
1039   GstSDPMessage *sdp_message = NULL;
1040   const GstSDPMedia *sdp_media;
1041   const gchar *video_control;
1042   const gchar *audio_control;
1043   GstRTSPRange client_port;
1044   gchar *session = NULL;
1045   GstRTSPTransport *video_transport = NULL;
1046   GstRTSPTransport *audio_transport = NULL;
1047   GSocket *rtp_socket, *rtcp_socket;
1048   gchar *range_out = NULL;
1049
1050   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1051
1052   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1053
1054   /* get control strings from DESCRIBE response */
1055   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1056   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1057   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1058   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1059   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1060
1061   get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
1062
1063   /* do SETUP for video and audio */
1064   fail_unless (do_setup (conn, video_control, &client_port, &session,
1065           &video_transport) == GST_RTSP_STS_OK);
1066   fail_unless (do_setup (conn, audio_control, &client_port, &session,
1067           &audio_transport) == GST_RTSP_STS_OK);
1068
1069   /* send PLAY request and check that we get 200 OK */
1070   fail_unless (do_request (conn, GST_RTSP_PLAY, NULL, session, NULL, range,
1071           NULL, NULL, NULL, NULL, NULL, &range_out) == GST_RTSP_STS_OK);
1072   if (range)
1073     fail_unless_equals_string (range, range_out);
1074   g_free (range_out);
1075
1076   receive_rtp (rtp_socket, NULL);
1077   receive_rtcp (rtcp_socket, NULL, 0);
1078
1079   /* send TEARDOWN request and check that we get 200 OK */
1080   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1081           session) == GST_RTSP_STS_OK);
1082
1083   /* FIXME: The rtsp-server always disconnects the transport before
1084    * sending the RTCP BYE
1085    * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
1086    */
1087
1088   /* clean up and iterate so the clean-up can finish */
1089   g_object_unref (rtp_socket);
1090   g_object_unref (rtcp_socket);
1091   g_free (session);
1092   gst_rtsp_transport_free (video_transport);
1093   gst_rtsp_transport_free (audio_transport);
1094   gst_sdp_message_free (sdp_message);
1095   gst_rtsp_connection_free (conn);
1096 }
1097
1098
1099 GST_START_TEST (test_play)
1100 {
1101   start_server ();
1102
1103   do_test_play (NULL);
1104
1105   stop_server ();
1106   iterate ();
1107 }
1108
1109 GST_END_TEST;
1110
1111 GST_START_TEST (test_play_without_session)
1112 {
1113   GstRTSPConnection *conn;
1114
1115   start_server ();
1116
1117   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1118
1119   /* send PLAY request without a session and check that we get a
1120    * 454 Session Not Found */
1121   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1122           NULL) == GST_RTSP_STS_SESSION_NOT_FOUND);
1123
1124   /* clean up and iterate so the clean-up can finish */
1125   gst_rtsp_connection_free (conn);
1126   stop_server ();
1127   iterate ();
1128 }
1129
1130 GST_END_TEST;
1131
1132 GST_START_TEST (test_bind_already_in_use)
1133 {
1134   GstRTSPServer *serv;
1135   GSocketService *service;
1136   GError *error = NULL;
1137   guint16 port;
1138   gchar *port_str;
1139
1140   serv = gst_rtsp_server_new ();
1141   service = g_socket_service_new ();
1142
1143   /* bind service to port */
1144   port =
1145       g_socket_listener_add_any_inet_port (G_SOCKET_LISTENER (service), NULL,
1146       &error);
1147   g_assert_no_error (error);
1148
1149   port_str = g_strdup_printf ("%d\n", port);
1150
1151   /* try to bind server to the same port */
1152   g_object_set (serv, "service", port_str, NULL);
1153   g_free (port_str);
1154
1155   /* attach to default main context */
1156   fail_unless (gst_rtsp_server_attach (serv, NULL) == 0);
1157
1158   /* cleanup */
1159   g_object_unref (serv);
1160   g_socket_service_stop (service);
1161   g_object_unref (service);
1162 }
1163
1164 GST_END_TEST;
1165
1166
1167 GST_START_TEST (test_play_multithreaded)
1168 {
1169   GstRTSPThreadPool *pool;
1170
1171   pool = gst_rtsp_server_get_thread_pool (server);
1172   gst_rtsp_thread_pool_set_max_threads (pool, 2);
1173   g_object_unref (pool);
1174
1175   start_server ();
1176
1177   do_test_play (NULL);
1178
1179   stop_server ();
1180   iterate ();
1181 }
1182
1183 GST_END_TEST;
1184
1185 enum
1186 {
1187   BLOCK_ME,
1188   BLOCKED,
1189   UNBLOCK
1190 };
1191
1192
1193 static void
1194 media_constructed_block (GstRTSPMediaFactory * factory,
1195     GstRTSPMedia * media, gpointer user_data)
1196 {
1197   gint *block_state = user_data;
1198
1199   g_mutex_lock (&check_mutex);
1200
1201   *block_state = BLOCKED;
1202   g_cond_broadcast (&check_cond);
1203
1204   while (*block_state != UNBLOCK)
1205     g_cond_wait (&check_cond, &check_mutex);
1206   g_mutex_unlock (&check_mutex);
1207 }
1208
1209
1210 GST_START_TEST (test_play_multithreaded_block_in_describe)
1211 {
1212   GstRTSPConnection *conn;
1213   GstRTSPMountPoints *mounts;
1214   GstRTSPMediaFactory *factory;
1215   gint block_state = BLOCK_ME;
1216   GstRTSPMessage *request;
1217   GstRTSPMessage *response;
1218   GstRTSPStatusCode code;
1219   GstRTSPThreadPool *pool;
1220
1221   pool = gst_rtsp_server_get_thread_pool (server);
1222   gst_rtsp_thread_pool_set_max_threads (pool, 2);
1223   g_object_unref (pool);
1224
1225   mounts = gst_rtsp_server_get_mount_points (server);
1226   fail_unless (mounts != NULL);
1227   factory = gst_rtsp_media_factory_new ();
1228   gst_rtsp_media_factory_set_launch (factory,
1229       "( " VIDEO_PIPELINE "  " AUDIO_PIPELINE " )");
1230   g_signal_connect (factory, "media-constructed",
1231       G_CALLBACK (media_constructed_block), &block_state);
1232   gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT "2", factory);
1233   g_object_unref (mounts);
1234
1235   start_server ();
1236
1237   conn = connect_to_server (test_port, TEST_MOUNT_POINT "2");
1238   iterate ();
1239
1240   /* do describe, it will not return now as we've blocked it */
1241   request = create_request (conn, GST_RTSP_DESCRIBE, NULL);
1242   fail_unless (send_request (conn, request));
1243   gst_rtsp_message_free (request);
1244
1245   g_mutex_lock (&check_mutex);
1246   while (block_state != BLOCKED)
1247     g_cond_wait (&check_cond, &check_mutex);
1248   g_mutex_unlock (&check_mutex);
1249
1250   /* Do a second connection while the first one is blocked */
1251   do_test_play (NULL);
1252
1253   /* Now unblock the describe */
1254   g_mutex_lock (&check_mutex);
1255   block_state = UNBLOCK;
1256   g_cond_broadcast (&check_cond);
1257   g_mutex_unlock (&check_mutex);
1258
1259   response = read_response (conn);
1260   gst_rtsp_message_parse_response (response, &code, NULL, NULL);
1261   fail_unless (code == GST_RTSP_STS_OK);
1262   gst_rtsp_message_free (response);
1263
1264
1265   gst_rtsp_connection_free (conn);
1266   stop_server ();
1267   iterate ();
1268
1269 }
1270
1271 GST_END_TEST;
1272
1273
1274 static void
1275 new_session_timeout_one (GstRTSPClient * client,
1276     GstRTSPSession * session, gpointer user_data)
1277 {
1278   gst_rtsp_session_set_timeout (session, 1);
1279
1280   g_signal_handlers_disconnect_by_func (client, new_session_timeout_one,
1281       user_data);
1282 }
1283
1284 static void
1285 session_connected_new_session_cb (GstRTSPServer * server,
1286     GstRTSPClient * client, gpointer user_data)
1287 {
1288
1289   g_signal_connect (client, "new-session", user_data, NULL);
1290 }
1291
1292 GST_START_TEST (test_play_multithreaded_timeout_client)
1293 {
1294   GstRTSPConnection *conn;
1295   GstSDPMessage *sdp_message = NULL;
1296   const GstSDPMedia *sdp_media;
1297   const gchar *video_control;
1298   const gchar *audio_control;
1299   GstRTSPRange client_port;
1300   gchar *session = NULL;
1301   GstRTSPTransport *video_transport = NULL;
1302   GstRTSPTransport *audio_transport = NULL;
1303   GstRTSPSessionPool *pool;
1304   GstRTSPThreadPool *thread_pool;
1305
1306   thread_pool = gst_rtsp_server_get_thread_pool (server);
1307   gst_rtsp_thread_pool_set_max_threads (thread_pool, 2);
1308   g_object_unref (thread_pool);
1309
1310   pool = gst_rtsp_server_get_session_pool (server);
1311   g_signal_connect (server, "client-connected",
1312       G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1313
1314   start_server ();
1315
1316
1317   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1318
1319   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1320
1321   /* get control strings from DESCRIBE response */
1322   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1323   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1324   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1325   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1326   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1327
1328   get_client_ports (&client_port);
1329
1330   /* do SETUP for video and audio */
1331   fail_unless (do_setup (conn, video_control, &client_port, &session,
1332           &video_transport) == GST_RTSP_STS_OK);
1333   fail_unless (do_setup (conn, audio_control, &client_port, &session,
1334           &audio_transport) == GST_RTSP_STS_OK);
1335
1336   fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1337
1338   /* send PLAY request and check that we get 200 OK */
1339   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1340           session) == GST_RTSP_STS_OK);
1341
1342   sleep (7);
1343
1344   fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1345   fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 0);
1346
1347   /* clean up and iterate so the clean-up can finish */
1348   g_object_unref (pool);
1349   g_free (session);
1350   gst_rtsp_transport_free (video_transport);
1351   gst_rtsp_transport_free (audio_transport);
1352   gst_sdp_message_free (sdp_message);
1353   gst_rtsp_connection_free (conn);
1354
1355   stop_server ();
1356   iterate ();
1357 }
1358
1359 GST_END_TEST;
1360
1361
1362 GST_START_TEST (test_play_multithreaded_timeout_session)
1363 {
1364   GstRTSPConnection *conn;
1365   GstSDPMessage *sdp_message = NULL;
1366   const GstSDPMedia *sdp_media;
1367   const gchar *video_control;
1368   const gchar *audio_control;
1369   GstRTSPRange client_port;
1370   gchar *session1 = NULL;
1371   gchar *session2 = NULL;
1372   GstRTSPTransport *video_transport = NULL;
1373   GstRTSPTransport *audio_transport = NULL;
1374   GstRTSPSessionPool *pool;
1375   GstRTSPThreadPool *thread_pool;
1376
1377   thread_pool = gst_rtsp_server_get_thread_pool (server);
1378   gst_rtsp_thread_pool_set_max_threads (thread_pool, 2);
1379   g_object_unref (thread_pool);
1380
1381   pool = gst_rtsp_server_get_session_pool (server);
1382   g_signal_connect (server, "client-connected",
1383       G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1384
1385   start_server ();
1386
1387
1388   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1389
1390   gst_rtsp_connection_set_remember_session_id (conn, FALSE);
1391
1392   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1393
1394   /* get control strings from DESCRIBE response */
1395   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1396   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1397   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1398   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1399   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1400
1401   get_client_ports (&client_port);
1402
1403   /* do SETUP for video and audio */
1404   fail_unless (do_setup (conn, video_control, &client_port, &session1,
1405           &video_transport) == GST_RTSP_STS_OK);
1406   fail_unless (do_setup (conn, audio_control, &client_port, &session2,
1407           &audio_transport) == GST_RTSP_STS_OK);
1408
1409   fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 2);
1410
1411   /* send PLAY request and check that we get 200 OK */
1412   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1413           session1) == GST_RTSP_STS_OK);
1414   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1415           session2) == GST_RTSP_STS_OK);
1416
1417   sleep (7);
1418
1419   fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1420
1421   /* send TEARDOWN request and check that we get 454 Session Not found */
1422   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1423           session1) == GST_RTSP_STS_SESSION_NOT_FOUND);
1424
1425   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1426           session2) == GST_RTSP_STS_OK);
1427
1428   /* clean up and iterate so the clean-up can finish */
1429   g_object_unref (pool);
1430   g_free (session1);
1431   g_free (session2);
1432   gst_rtsp_transport_free (video_transport);
1433   gst_rtsp_transport_free (audio_transport);
1434   gst_sdp_message_free (sdp_message);
1435   gst_rtsp_connection_free (conn);
1436
1437   stop_server ();
1438   iterate ();
1439 }
1440
1441 GST_END_TEST;
1442
1443
1444 GST_START_TEST (test_play_disconnect)
1445 {
1446   GstRTSPConnection *conn;
1447   GstSDPMessage *sdp_message = NULL;
1448   const GstSDPMedia *sdp_media;
1449   const gchar *video_control;
1450   const gchar *audio_control;
1451   GstRTSPRange client_port;
1452   gchar *session = NULL;
1453   GstRTSPTransport *video_transport = NULL;
1454   GstRTSPTransport *audio_transport = NULL;
1455   GstRTSPSessionPool *pool;
1456
1457   pool = gst_rtsp_server_get_session_pool (server);
1458   g_signal_connect (server, "client-connected",
1459       G_CALLBACK (session_connected_new_session_cb), new_session_timeout_one);
1460
1461   start_server ();
1462
1463   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1464
1465   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1466
1467   /* get control strings from DESCRIBE response */
1468   fail_unless (gst_sdp_message_medias_len (sdp_message) == 2);
1469   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1470   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1471   sdp_media = gst_sdp_message_get_media (sdp_message, 1);
1472   audio_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1473
1474   get_client_ports (&client_port);
1475
1476   /* do SETUP for video and audio */
1477   fail_unless (do_setup (conn, video_control, &client_port, &session,
1478           &video_transport) == GST_RTSP_STS_OK);
1479   fail_unless (do_setup (conn, audio_control, &client_port, &session,
1480           &audio_transport) == GST_RTSP_STS_OK);
1481
1482   fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1483
1484   /* send PLAY request and check that we get 200 OK */
1485   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1486           session) == GST_RTSP_STS_OK);
1487
1488   gst_rtsp_connection_free (conn);
1489
1490   sleep (7);
1491
1492   fail_unless (gst_rtsp_session_pool_get_n_sessions (pool) == 1);
1493   fail_unless (gst_rtsp_session_pool_cleanup (pool) == 1);
1494
1495
1496   /* clean up and iterate so the clean-up can finish */
1497   g_object_unref (pool);
1498   g_free (session);
1499   gst_rtsp_transport_free (video_transport);
1500   gst_rtsp_transport_free (audio_transport);
1501   gst_sdp_message_free (sdp_message);
1502
1503   stop_server ();
1504   iterate ();
1505 }
1506
1507 GST_END_TEST;
1508
1509 /* Only different with test_play is the specific ports selected */
1510
1511 GST_START_TEST (test_play_specific_server_port)
1512 {
1513   GstRTSPMountPoints *mounts;
1514   gchar *service;
1515   GstRTSPMediaFactory *factory;
1516   GstRTSPAddressPool *pool;
1517   GstRTSPConnection *conn;
1518   GstSDPMessage *sdp_message = NULL;
1519   const GstSDPMedia *sdp_media;
1520   const gchar *video_control;
1521   GstRTSPRange client_port;
1522   gchar *session = NULL;
1523   GstRTSPTransport *video_transport = NULL;
1524   GSocket *rtp_socket, *rtcp_socket;
1525   GSocketAddress *rtp_address, *rtcp_address;
1526   guint16 rtp_port, rtcp_port;
1527
1528   mounts = gst_rtsp_server_get_mount_points (server);
1529
1530   factory = gst_rtsp_media_factory_new ();
1531   pool = gst_rtsp_address_pool_new ();
1532   gst_rtsp_address_pool_add_range (pool, GST_RTSP_ADDRESS_POOL_ANY_IPV4,
1533       GST_RTSP_ADDRESS_POOL_ANY_IPV4, 7770, 7780, 0);
1534   gst_rtsp_media_factory_set_address_pool (factory, pool);
1535   g_object_unref (pool);
1536   gst_rtsp_media_factory_set_launch (factory, "( " VIDEO_PIPELINE " )");
1537   gst_rtsp_mount_points_add_factory (mounts, TEST_MOUNT_POINT, factory);
1538   g_object_unref (mounts);
1539
1540   /* set port to any */
1541   gst_rtsp_server_set_service (server, "0");
1542
1543   /* attach to default main context */
1544   source_id = gst_rtsp_server_attach (server, NULL);
1545   fail_if (source_id == 0);
1546
1547   /* get port */
1548   service = gst_rtsp_server_get_service (server);
1549   test_port = atoi (service);
1550   fail_unless (test_port != 0);
1551   g_free (service);
1552
1553   GST_DEBUG ("rtsp server listening on port %d", test_port);
1554
1555
1556   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1557
1558   sdp_message = do_describe (conn, TEST_MOUNT_POINT);
1559
1560   /* get control strings from DESCRIBE response */
1561   fail_unless (gst_sdp_message_medias_len (sdp_message) == 1);
1562   sdp_media = gst_sdp_message_get_media (sdp_message, 0);
1563   video_control = gst_sdp_media_get_attribute_val (sdp_media, "control");
1564
1565   get_client_ports_full (&client_port, &rtp_socket, &rtcp_socket);
1566
1567   /* do SETUP for video */
1568   fail_unless (do_setup (conn, video_control, &client_port, &session,
1569           &video_transport) == GST_RTSP_STS_OK);
1570
1571   /* send PLAY request and check that we get 200 OK */
1572   fail_unless (do_simple_request (conn, GST_RTSP_PLAY,
1573           session) == GST_RTSP_STS_OK);
1574
1575   receive_rtp (rtp_socket, &rtp_address);
1576   receive_rtcp (rtcp_socket, &rtcp_address, 0);
1577
1578   fail_unless (G_IS_INET_SOCKET_ADDRESS (rtp_address));
1579   fail_unless (G_IS_INET_SOCKET_ADDRESS (rtcp_address));
1580   rtp_port =
1581       g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_address));
1582   rtcp_port =
1583       g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtcp_address));
1584   fail_unless (rtp_port >= 7770 && rtp_port <= 7780 && rtp_port % 2 == 0);
1585   fail_unless (rtcp_port >= 7770 && rtcp_port <= 7780 && rtcp_port % 2 == 1);
1586   fail_unless (rtp_port + 1 == rtcp_port);
1587
1588   g_object_unref (rtp_address);
1589   g_object_unref (rtcp_address);
1590
1591   /* send TEARDOWN request and check that we get 200 OK */
1592   fail_unless (do_simple_request (conn, GST_RTSP_TEARDOWN,
1593           session) == GST_RTSP_STS_OK);
1594
1595   /* FIXME: The rtsp-server always disconnects the transport before
1596    * sending the RTCP BYE
1597    * receive_rtcp (rtcp_socket, NULL, GST_RTCP_TYPE_BYE);
1598    */
1599
1600   /* clean up and iterate so the clean-up can finish */
1601   g_object_unref (rtp_socket);
1602   g_object_unref (rtcp_socket);
1603   g_free (session);
1604   gst_rtsp_transport_free (video_transport);
1605   gst_sdp_message_free (sdp_message);
1606   gst_rtsp_connection_free (conn);
1607
1608
1609   stop_server ();
1610   iterate ();
1611 }
1612
1613 GST_END_TEST;
1614
1615
1616 GST_START_TEST (test_play_smpte_range)
1617 {
1618   start_server ();
1619
1620   do_test_play ("npt=5-");
1621   do_test_play ("smpte=0:00:00-");
1622   do_test_play ("smpte=1:00:00-");
1623   do_test_play ("smpte=1:00:03-");
1624   do_test_play ("clock=20120321T152256Z-");
1625
1626   stop_server ();
1627   iterate ();
1628 }
1629
1630 GST_END_TEST;
1631
1632 GST_START_TEST (test_announce_without_sdp)
1633 {
1634   GstRTSPConnection *conn;
1635   GstRTSPStatusCode status;
1636   GstRTSPMessage *request;
1637   GstRTSPMessage *response;
1638
1639   start_record_server ("( fakesink name=depay0 )");
1640
1641   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1642
1643   /* create and send ANNOUNCE request */
1644   request = create_request (conn, GST_RTSP_ANNOUNCE, NULL);
1645
1646   fail_unless (send_request (conn, request));
1647
1648   iterate ();
1649
1650   response = read_response (conn);
1651
1652   /* check response */
1653   gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1654   fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1655   gst_rtsp_message_free (response);
1656
1657   /* try again, this type with content-type, but still no SDP */
1658   gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1659       "application/sdp");
1660
1661   fail_unless (send_request (conn, request));
1662
1663   iterate ();
1664
1665   response = read_response (conn);
1666
1667   /* check response */
1668   gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1669   fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1670   gst_rtsp_message_free (response);
1671
1672   /* try again, this type with an unknown content-type */
1673   gst_rtsp_message_remove_header (request, GST_RTSP_HDR_CONTENT_TYPE, -1);
1674   gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1675       "application/x-something");
1676
1677   fail_unless (send_request (conn, request));
1678
1679   iterate ();
1680
1681   response = read_response (conn);
1682
1683   /* check response */
1684   gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1685   fail_unless_equals_int (status, GST_RTSP_STS_BAD_REQUEST);
1686   gst_rtsp_message_free (response);
1687
1688   /* clean up and iterate so the clean-up can finish */
1689   gst_rtsp_message_free (request);
1690   gst_rtsp_connection_free (conn);
1691   stop_server ();
1692   iterate ();
1693 }
1694
1695 GST_END_TEST;
1696
1697 static GstRTSPStatusCode
1698 do_announce (GstRTSPConnection * conn, GstSDPMessage * sdp)
1699 {
1700   GstRTSPMessage *request;
1701   GstRTSPMessage *response;
1702   GstRTSPStatusCode code;
1703   gchar *str;
1704
1705   /* create request */
1706   request = create_request (conn, GST_RTSP_ANNOUNCE, NULL);
1707
1708   gst_rtsp_message_add_header (request, GST_RTSP_HDR_CONTENT_TYPE,
1709       "application/sdp");
1710
1711   /* add SDP to the response body */
1712   str = gst_sdp_message_as_text (sdp);
1713   gst_rtsp_message_take_body (request, (guint8 *) str, strlen (str));
1714   gst_sdp_message_free (sdp);
1715
1716   /* send request */
1717   fail_unless (send_request (conn, request));
1718   gst_rtsp_message_free (request);
1719
1720   iterate ();
1721
1722   /* read response */
1723   response = read_response (conn);
1724
1725   /* check status line */
1726   gst_rtsp_message_parse_response (response, &code, NULL, NULL);
1727
1728   gst_rtsp_message_free (response);
1729   return code;
1730 }
1731
1732 static void
1733 media_constructed_cb (GstRTSPMediaFactory * mfactory, GstRTSPMedia * media,
1734     gpointer user_data)
1735 {
1736   GstElement **p_sink = user_data;
1737   GstElement *bin;
1738
1739   bin = gst_rtsp_media_get_element (media);
1740   *p_sink = gst_bin_get_by_name (GST_BIN (bin), "sink");
1741   GST_INFO ("media constructed!: %" GST_PTR_FORMAT, *p_sink);
1742 }
1743
1744 #define RECORD_N_BUFS 10
1745
1746 GST_START_TEST (test_record_tcp)
1747 {
1748   GstRTSPMediaFactory *mfactory;
1749   GstRTSPConnection *conn;
1750   GstRTSPStatusCode status;
1751   GstRTSPMessage *response;
1752   GstRTSPMessage *request;
1753   GstSDPMessage *sdp;
1754   GstRTSPResult rres;
1755   GSocketAddress *sa;
1756   GInetAddress *ia;
1757   GstElement *sink = NULL;
1758   GSocket *conn_socket;
1759   const gchar *proto;
1760   gchar *client_ip, *sess_id, *session = NULL;
1761   gint i;
1762
1763   mfactory =
1764       start_record_server ("( rtppcmadepay name=depay0 ! appsink name=sink )");
1765
1766   g_signal_connect (mfactory, "media-constructed",
1767       G_CALLBACK (media_constructed_cb), &sink);
1768
1769   conn = connect_to_server (test_port, TEST_MOUNT_POINT);
1770
1771   conn_socket = gst_rtsp_connection_get_read_socket (conn);
1772
1773   sa = g_socket_get_local_address (conn_socket, NULL);
1774   ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
1775   client_ip = g_inet_address_to_string (ia);
1776   if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6)
1777     proto = "IP6";
1778   else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
1779     proto = "IP4";
1780   else
1781     g_assert_not_reached ();
1782   g_object_unref (sa);
1783
1784   gst_sdp_message_new (&sdp);
1785
1786   /* some standard things first */
1787   gst_sdp_message_set_version (sdp, "0");
1788
1789   /* session ID doesn't have to be super-unique in this case */
1790   sess_id = g_strdup_printf ("%u", g_random_int ());
1791   gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
1792   g_free (sess_id);
1793   g_free (client_ip);
1794
1795   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1796   gst_sdp_message_set_information (sdp, "rtsp-server-test");
1797   gst_sdp_message_add_time (sdp, "0", "0", NULL);
1798   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1799
1800   /* add stream 0 */
1801   {
1802     GstSDPMedia *smedia;
1803
1804     gst_sdp_media_new (&smedia);
1805     gst_sdp_media_set_media (smedia, "audio");
1806     gst_sdp_media_add_format (smedia, "8");     /* pcma/alaw */
1807     gst_sdp_media_set_port_info (smedia, 0, 1);
1808     gst_sdp_media_set_proto (smedia, "RTP/AVP");
1809     gst_sdp_media_add_attribute (smedia, "rtpmap", "8 PCMA/8000");
1810     gst_sdp_message_add_media (sdp, smedia);
1811     gst_sdp_media_free (smedia);
1812   }
1813
1814   /* send ANNOUNCE request */
1815   status = do_announce (conn, sdp);
1816   fail_unless_equals_int (status, GST_RTSP_STS_OK);
1817
1818   /* create and send SETUP request */
1819   request = create_request (conn, GST_RTSP_SETUP, NULL);
1820   gst_rtsp_message_add_header (request, GST_RTSP_HDR_TRANSPORT,
1821       "RTP/AVP/TCP;interleaved=0;mode=record");
1822   fail_unless (send_request (conn, request));
1823   gst_rtsp_message_free (request);
1824   iterate ();
1825   response = read_response (conn);
1826   gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1827   fail_unless_equals_int (status, GST_RTSP_STS_OK);
1828
1829   rres =
1830       gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &session, 0);
1831   session = g_strdup (session);
1832   fail_unless_equals_int (rres, GST_RTSP_OK);
1833   gst_rtsp_message_free (response);
1834
1835   /* send RECORD */
1836   request = create_request (conn, GST_RTSP_RECORD, NULL);
1837   gst_rtsp_message_add_header (request, GST_RTSP_HDR_SESSION, session);
1838   fail_unless (send_request (conn, request));
1839   gst_rtsp_message_free (request);
1840   iterate ();
1841   response = read_response (conn);
1842   gst_rtsp_message_parse_response (response, &status, NULL, NULL);
1843   fail_unless_equals_int (status, GST_RTSP_STS_OK);
1844   gst_rtsp_message_free (response);
1845
1846   /* send some data */
1847   {
1848     GstElement *pipeline, *src, *enc, *pay, *sink;
1849
1850     pipeline = gst_pipeline_new ("send-pipeline");
1851     src = gst_element_factory_make ("audiotestsrc", NULL);
1852     g_object_set (src, "num-buffers", RECORD_N_BUFS,
1853         "samplesperbuffer", 1000, NULL);
1854     enc = gst_element_factory_make ("alawenc", NULL);
1855     pay = gst_element_factory_make ("rtppcmapay", NULL);
1856     sink = gst_element_factory_make ("appsink", NULL);
1857     fail_unless (pipeline && src && enc && pay && sink);
1858     gst_bin_add_many (GST_BIN (pipeline), src, enc, pay, sink, NULL);
1859     gst_element_link_many (src, enc, pay, sink, NULL);
1860     gst_element_set_state (pipeline, GST_STATE_PLAYING);
1861
1862     do {
1863       GstRTSPMessage *data_msg;
1864       GstMapInfo map = GST_MAP_INFO_INIT;
1865       GstRTSPResult rres;
1866       GstSample *sample = NULL;
1867       GstBuffer *buf;
1868
1869       g_signal_emit_by_name (G_OBJECT (sink), "pull-sample", &sample);
1870       if (sample == NULL)
1871         break;
1872       buf = gst_sample_get_buffer (sample);
1873       rres = gst_rtsp_message_new_data (&data_msg, 0);
1874       fail_unless_equals_int (rres, GST_RTSP_OK);
1875       gst_buffer_map (buf, &map, GST_MAP_READ);
1876       GST_INFO ("sending %u bytes of data on channel 0", (guint) map.size);
1877       GST_MEMDUMP ("data on channel 0", map.data, map.size);
1878       rres = gst_rtsp_message_set_body (data_msg, map.data, map.size);
1879       fail_unless_equals_int (rres, GST_RTSP_OK);
1880       gst_buffer_unmap (buf, &map);
1881       rres = gst_rtsp_connection_send (conn, data_msg, NULL);
1882       fail_unless_equals_int (rres, GST_RTSP_OK);
1883       gst_rtsp_message_free (data_msg);
1884       gst_sample_unref (sample);
1885     } while (TRUE);
1886
1887     gst_element_set_state (pipeline, GST_STATE_NULL);
1888     gst_object_unref (pipeline);
1889   }
1890
1891   /* check received data (we assume every buffer created by audiotestsrc and
1892    * subsequently encoded by mulawenc results in exactly one RTP packet) */
1893   for (i = 0; i < RECORD_N_BUFS; ++i) {
1894     GstSample *sample = NULL;
1895
1896     g_signal_emit_by_name (G_OBJECT (sink), "pull-sample", &sample);
1897     GST_INFO ("%2d recv sample: %p", i, sample);
1898     gst_sample_unref (sample);
1899   }
1900
1901   fail_unless_equals_int (GST_STATE (sink), GST_STATE_PLAYING);
1902
1903   /* clean up and iterate so the clean-up can finish */
1904   gst_rtsp_connection_free (conn);
1905   stop_server ();
1906   iterate ();
1907   g_free (session);
1908 }
1909
1910 GST_END_TEST;
1911
1912 static Suite *
1913 rtspserver_suite (void)
1914 {
1915   Suite *s = suite_create ("rtspserver");
1916   TCase *tc = tcase_create ("general");
1917
1918   suite_add_tcase (s, tc);
1919   tcase_add_checked_fixture (tc, setup, teardown);
1920   tcase_set_timeout (tc, 120);
1921   tcase_add_test (tc, test_connect);
1922   tcase_add_test (tc, test_describe);
1923   tcase_add_test (tc, test_describe_non_existing_mount_point);
1924   tcase_add_test (tc, test_describe_record_media);
1925   tcase_add_test (tc, test_setup);
1926   tcase_add_test (tc, test_setup_tcp);
1927   tcase_add_test (tc, test_setup_twice);
1928   tcase_add_test (tc, test_setup_with_require_header);
1929   tcase_add_test (tc, test_setup_non_existing_stream);
1930   tcase_add_test (tc, test_play);
1931   tcase_add_test (tc, test_play_without_session);
1932   tcase_add_test (tc, test_bind_already_in_use);
1933   tcase_add_test (tc, test_play_multithreaded);
1934   tcase_add_test (tc, test_play_multithreaded_block_in_describe);
1935   tcase_add_test (tc, test_play_multithreaded_timeout_client);
1936   tcase_add_test (tc, test_play_multithreaded_timeout_session);
1937   tcase_add_test (tc, test_play_disconnect);
1938   tcase_add_test (tc, test_play_specific_server_port);
1939   tcase_add_test (tc, test_play_smpte_range);
1940   tcase_add_test (tc, test_announce_without_sdp);
1941   tcase_add_test (tc, test_record_tcp);
1942   return s;
1943 }
1944
1945 GST_CHECK_MAIN (rtspserver);