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