rtsp-sdp: add bandwidth line
[platform/upstream/gstreamer.git] / tests / check / gst / client.c
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/check/gstcheck.h>
21
22 #include <rtsp-client.h>
23
24 static gint cseq;
25
26 static gboolean
27 test_response_200 (GstRTSPClient * client, GstRTSPMessage * response,
28     gboolean close, gpointer user_data)
29 {
30   GstRTSPStatusCode code;
31   const gchar *reason;
32   GstRTSPVersion version;
33
34   fail_unless (gst_rtsp_message_get_type (response) ==
35       GST_RTSP_MESSAGE_RESPONSE);
36
37   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
38           &version)
39       == GST_RTSP_OK);
40   fail_unless (code == GST_RTSP_STS_OK);
41   fail_unless (g_str_equal (reason, "OK"));
42   fail_unless (version == GST_RTSP_VERSION_1_0);
43
44   return TRUE;
45 }
46
47 static gboolean
48 test_response_400 (GstRTSPClient * client, GstRTSPMessage * response,
49     gboolean close, gpointer user_data)
50 {
51   GstRTSPStatusCode code;
52   const gchar *reason;
53   GstRTSPVersion version;
54
55   fail_unless (gst_rtsp_message_get_type (response) ==
56       GST_RTSP_MESSAGE_RESPONSE);
57
58   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
59           &version)
60       == GST_RTSP_OK);
61   fail_unless (code == GST_RTSP_STS_BAD_REQUEST);
62   fail_unless (g_str_equal (reason, "Bad Request"));
63   fail_unless (version == GST_RTSP_VERSION_1_0);
64
65   return TRUE;
66 }
67
68 static gboolean
69 test_response_404 (GstRTSPClient * client, GstRTSPMessage * response,
70     gboolean close, gpointer user_data)
71 {
72   GstRTSPStatusCode code;
73   const gchar *reason;
74   GstRTSPVersion version;
75
76   fail_unless (gst_rtsp_message_get_type (response) ==
77       GST_RTSP_MESSAGE_RESPONSE);
78
79   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
80           &version)
81       == GST_RTSP_OK);
82   fail_unless (code == GST_RTSP_STS_NOT_FOUND);
83   fail_unless (g_str_equal (reason, "Not Found"));
84   fail_unless (version == GST_RTSP_VERSION_1_0);
85
86   return TRUE;
87 }
88
89 static gboolean
90 test_response_454 (GstRTSPClient * client, GstRTSPMessage * response,
91     gboolean close, gpointer user_data)
92 {
93   GstRTSPStatusCode code;
94   const gchar *reason;
95   GstRTSPVersion version;
96
97   fail_unless (gst_rtsp_message_get_type (response) ==
98       GST_RTSP_MESSAGE_RESPONSE);
99
100   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
101           &version)
102       == GST_RTSP_OK);
103   fail_unless (code == GST_RTSP_STS_SESSION_NOT_FOUND);
104   fail_unless (g_str_equal (reason, "Session Not Found"));
105   fail_unless (version == GST_RTSP_VERSION_1_0);
106
107   return TRUE;
108 }
109
110 static GstRTSPClient *
111 setup_client (const gchar * launch_line)
112 {
113   GstRTSPClient *client;
114   GstRTSPSessionPool *session_pool;
115   GstRTSPMountPoints *mount_points;
116   GstRTSPMediaFactory *factory;
117
118   client = gst_rtsp_client_new ();
119
120   session_pool = gst_rtsp_session_pool_new ();
121   gst_rtsp_client_set_session_pool (client, session_pool);
122
123   mount_points = gst_rtsp_mount_points_new ();
124   factory = gst_rtsp_media_factory_new ();
125   if (launch_line == NULL)
126     gst_rtsp_media_factory_set_launch (factory,
127         "videotestsrc ! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96");
128   else
129     gst_rtsp_media_factory_set_launch (factory, launch_line);
130
131   gst_rtsp_mount_points_add_factory (mount_points, "/test", factory);
132   gst_rtsp_client_set_mount_points (client, mount_points);
133
134   g_object_unref (mount_points);
135   g_object_unref (session_pool);
136
137   return client;
138 }
139
140 GST_START_TEST (test_request)
141 {
142   GstRTSPClient *client;
143   GstRTSPMessage request = { 0, };
144   gchar *str;
145
146   client = gst_rtsp_client_new ();
147
148   /* OPTIONS with invalid url */
149   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
150           "foopy://padoop/") == GST_RTSP_OK);
151   str = g_strdup_printf ("%d", cseq);
152   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
153   g_free (str);
154
155   gst_rtsp_client_set_send_func (client, test_response_400, NULL, NULL);
156   fail_unless (gst_rtsp_client_handle_message (client,
157           &request) == GST_RTSP_OK);
158
159   gst_rtsp_message_unset (&request);
160
161   /* OPTIONS with unknown session id */
162   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
163           "rtsp://localhost/test") == GST_RTSP_OK);
164   str = g_strdup_printf ("%d", cseq);
165   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
166   g_free (str);
167   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SESSION, "foobar");
168
169   gst_rtsp_client_set_send_func (client, test_response_454, NULL, NULL);
170   fail_unless (gst_rtsp_client_handle_message (client,
171           &request) == GST_RTSP_OK);
172
173   gst_rtsp_message_unset (&request);
174
175   g_object_unref (client);
176 }
177
178 GST_END_TEST;
179
180 static gboolean
181 test_option_response_200 (GstRTSPClient * client, GstRTSPMessage * response,
182     gboolean close, gpointer user_data)
183 {
184   GstRTSPStatusCode code;
185   const gchar *reason;
186   GstRTSPVersion version;
187   gchar *str;
188   GstRTSPMethod methods;
189
190   fail_unless (gst_rtsp_message_get_type (response) ==
191       GST_RTSP_MESSAGE_RESPONSE);
192
193   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
194           &version)
195       == GST_RTSP_OK);
196   fail_unless (code == GST_RTSP_STS_OK);
197   fail_unless (g_str_equal (reason, "OK"));
198   fail_unless (version == GST_RTSP_VERSION_1_0);
199
200   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
201           0) == GST_RTSP_OK);
202   fail_unless (atoi (str) == cseq++);
203
204   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_PUBLIC, &str,
205           0) == GST_RTSP_OK);
206
207   methods = gst_rtsp_options_from_text (str);
208   fail_if (methods == 0);
209   fail_unless (methods == (GST_RTSP_DESCRIBE |
210           GST_RTSP_OPTIONS |
211           GST_RTSP_PAUSE |
212           GST_RTSP_PLAY |
213           GST_RTSP_SETUP |
214           GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN));
215
216   return TRUE;
217 }
218
219 GST_START_TEST (test_options)
220 {
221   GstRTSPClient *client;
222   GstRTSPMessage request = { 0, };
223   gchar *str;
224
225   client = gst_rtsp_client_new ();
226
227   /* simple OPTIONS */
228   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
229           "rtsp://localhost/test") == GST_RTSP_OK);
230   str = g_strdup_printf ("%d", cseq);
231   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
232   g_free (str);
233
234   gst_rtsp_client_set_send_func (client, test_option_response_200, NULL, NULL);
235   fail_unless (gst_rtsp_client_handle_message (client,
236           &request) == GST_RTSP_OK);
237   gst_rtsp_message_unset (&request);
238
239   g_object_unref (client);
240 }
241
242 GST_END_TEST;
243
244 GST_START_TEST (test_describe)
245 {
246   GstRTSPClient *client;
247   GstRTSPMessage request = { 0, };
248   gchar *str;
249   GstRTSPUrl *uri_client;
250   gchar *uri_str;
251
252   client = gst_rtsp_client_new ();
253
254   /* simple DESCRIBE for non-existing url */
255   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
256           "rtsp://localhost/test") == GST_RTSP_OK);
257   str = g_strdup_printf ("%d", cseq);
258   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
259   g_free (str);
260
261   gst_rtsp_client_set_send_func (client, test_response_404, NULL, NULL);
262   fail_unless (gst_rtsp_client_handle_message (client,
263           &request) == GST_RTSP_OK);
264   gst_rtsp_message_unset (&request);
265
266   uri_client = gst_rtsp_client_get_uri (client);
267   fail_unless (uri_client == NULL);
268   gst_rtsp_url_free (uri_client);
269
270
271   g_object_unref (client);
272
273   /* simple DESCRIBE for an existing url */
274   client = setup_client (NULL);
275   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
276             "rtsp://localhost/test") == GST_RTSP_OK);
277   str = g_strdup_printf ("%d", cseq);
278   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
279   g_free (str);
280
281   gst_rtsp_client_set_send_func (client, test_response_200, NULL, NULL);
282   fail_unless (gst_rtsp_client_handle_message (client,
283           &request) == GST_RTSP_OK);
284   gst_rtsp_message_unset (&request);
285
286   uri_client = gst_rtsp_client_get_uri (client);
287   fail_unless (uri_client != NULL);
288   uri_str = gst_rtsp_url_get_request_uri (uri_client);
289   gst_rtsp_url_free (uri_client);
290   fail_unless (g_strcmp0 (uri_str, "rtsp://localhost/test") == 0);
291   g_free (uri_str);
292
293   g_object_unref (client);
294 }
295
296 GST_END_TEST;
297
298 gchar *expected_transport = NULL;;
299
300 static gboolean
301 test_setup_response_200_multicast (GstRTSPClient * client,
302     GstRTSPMessage * response, gboolean close, gpointer user_data)
303 {
304   GstRTSPStatusCode code;
305   const gchar *reason;
306   GstRTSPVersion version;
307   gchar *str;
308   GstRTSPSessionPool *session_pool;
309   GstRTSPSession *session;
310
311   fail_unless (expected_transport != NULL);
312
313   fail_unless (gst_rtsp_message_get_type (response) ==
314       GST_RTSP_MESSAGE_RESPONSE);
315
316   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
317           &version)
318       == GST_RTSP_OK);
319   fail_unless (code == GST_RTSP_STS_OK);
320   fail_unless (g_str_equal (reason, "OK"));
321   fail_unless (version == GST_RTSP_VERSION_1_0);
322
323   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
324           0) == GST_RTSP_OK);
325   fail_unless (atoi (str) == cseq++);
326
327   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT,
328           &str, 0) == GST_RTSP_OK);
329
330   fail_unless (!strcmp (str, expected_transport));
331
332   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION,
333           &str, 0) == GST_RTSP_OK);
334
335   session_pool = gst_rtsp_client_get_session_pool (client);
336   fail_unless (session_pool != NULL);
337
338   fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 1);
339   session = gst_rtsp_session_pool_find (session_pool, str);
340   fail_unless (session != NULL);
341   g_object_unref (session);
342
343   g_object_unref (session_pool);
344
345
346   return TRUE;
347 }
348
349 static GstRTSPClient *
350 setup_multicast_client (void)
351 {
352   GstRTSPClient *client;
353   GstRTSPSessionPool *session_pool;
354   GstRTSPMountPoints *mount_points;
355   GstRTSPMediaFactory *factory;
356   GstRTSPAddressPool *address_pool;
357
358   client = gst_rtsp_client_new ();
359
360   session_pool = gst_rtsp_session_pool_new ();
361   gst_rtsp_client_set_session_pool (client, session_pool);
362
363   mount_points = gst_rtsp_mount_points_new ();
364   factory = gst_rtsp_media_factory_new ();
365   gst_rtsp_media_factory_set_launch (factory,
366       "audiotestsrc ! audio/x-raw,rate=44100 ! audioconvert ! rtpL16pay name=pay0");
367   address_pool = gst_rtsp_address_pool_new ();
368   fail_unless (gst_rtsp_address_pool_add_range (address_pool,
369           "233.252.0.1", "233.252.0.1", 5000, 5010, 1));
370   gst_rtsp_media_factory_set_address_pool (factory, address_pool);
371   gst_rtsp_mount_points_add_factory (mount_points, "/test", factory);
372   gst_rtsp_client_set_mount_points (client, mount_points);
373
374   g_object_unref (mount_points);
375   g_object_unref (session_pool);
376   g_object_unref (address_pool);
377
378   return client;
379 }
380
381 GST_START_TEST (test_client_multicast_transport_404)
382 {
383   GstRTSPClient *client;
384   GstRTSPMessage request = { 0, };
385   gchar *str;
386
387   client = setup_multicast_client ();
388
389   /* simple SETUP for non-existing url */
390   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
391           "rtsp://localhost/test2/stream=0") == GST_RTSP_OK);
392   str = g_strdup_printf ("%d", cseq);
393   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
394   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
395       "RTP/AVP;multicast");
396
397   gst_rtsp_client_set_send_func (client, test_response_404, NULL, NULL);
398   fail_unless (gst_rtsp_client_handle_message (client,
399           &request) == GST_RTSP_OK);
400   gst_rtsp_message_unset (&request);
401
402   g_object_unref (client);
403 }
404
405 GST_END_TEST;
406
407 GST_START_TEST (test_client_multicast_transport)
408 {
409   GstRTSPClient *client;
410   GstRTSPMessage request = { 0, };
411   gchar *str;
412
413   client = setup_multicast_client ();
414
415   /* simple SETUP with a valid URI and multicast */
416   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
417           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
418   str = g_strdup_printf ("%d", cseq);
419   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
420   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
421       "RTP/AVP;multicast");
422
423   expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
424       "ttl=1;port=5000-5001;mode=\"PLAY\"";
425   gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
426       NULL, NULL);
427   fail_unless (gst_rtsp_client_handle_message (client,
428           &request) == GST_RTSP_OK);
429   gst_rtsp_message_unset (&request);
430   expected_transport = NULL;
431
432   g_object_unref (client);
433 }
434
435 GST_END_TEST;
436
437 GST_START_TEST (test_client_multicast_ignore_transport_specific)
438 {
439   GstRTSPClient *client;
440   GstRTSPMessage request = { 0, };
441   gchar *str;
442
443   client = setup_multicast_client ();
444
445   /* simple SETUP with a valid URI and multicast and a specific dest,
446    * but ignore it  */
447   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
448           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
449   str = g_strdup_printf ("%d", cseq);
450   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
451   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
452       "RTP/AVP;multicast;destination=233.252.0.2;ttl=2;port=5001-5006;");
453
454   expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
455       "ttl=1;port=5000-5001;mode=\"PLAY\"";
456   gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
457       NULL, NULL);
458   fail_unless (gst_rtsp_client_handle_message (client,
459           &request) == GST_RTSP_OK);
460   gst_rtsp_message_unset (&request);
461   expected_transport = NULL;
462
463   g_object_unref (client);
464 }
465
466 GST_END_TEST;
467
468 static gboolean
469 test_setup_response_461 (GstRTSPClient * client,
470     GstRTSPMessage * response, gboolean close, gpointer user_data)
471 {
472   GstRTSPStatusCode code;
473   const gchar *reason;
474   GstRTSPVersion version;
475   gchar *str;
476
477   fail_unless (expected_transport == NULL);
478
479   fail_unless (gst_rtsp_message_get_type (response) ==
480       GST_RTSP_MESSAGE_RESPONSE);
481
482   fail_unless (gst_rtsp_message_parse_response (response, &code, &reason,
483           &version)
484       == GST_RTSP_OK);
485   fail_unless (code == GST_RTSP_STS_UNSUPPORTED_TRANSPORT);
486   fail_unless (g_str_equal (reason, "Unsupported transport"));
487   fail_unless (version == GST_RTSP_VERSION_1_0);
488
489   fail_unless (gst_rtsp_message_get_header (response, GST_RTSP_HDR_CSEQ, &str,
490           0) == GST_RTSP_OK);
491   fail_unless (atoi (str) == cseq++);
492
493
494   return TRUE;
495 }
496
497 GST_START_TEST (test_client_multicast_invalid_transport_specific)
498 {
499   GstRTSPClient *client;
500   GstRTSPMessage request = { 0, };
501   gchar *str;
502   GstRTSPSessionPool *session_pool;
503
504   client = setup_multicast_client ();
505
506   gst_rtsp_client_set_use_client_settings (client, TRUE);
507   fail_unless (gst_rtsp_client_get_use_client_settings (client));
508
509
510   /* simple SETUP with a valid URI and multicast, but an invalid ip */
511   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
512           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
513   str = g_strdup_printf ("%d", cseq);
514   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
515   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
516       "RTP/AVP;multicast;destination=233.252.0.2;ttl=1;port=5000-5001;");
517
518   gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
519   fail_unless (gst_rtsp_client_handle_message (client,
520           &request) == GST_RTSP_OK);
521   gst_rtsp_message_unset (&request);
522
523   session_pool = gst_rtsp_client_get_session_pool (client);
524   fail_unless (session_pool != NULL);
525   /* FIXME: There seems to be a leak of a session here ! */
526   /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
527   g_object_unref (session_pool);
528
529
530
531   /* simple SETUP with a valid URI and multicast, but an invalid prt */
532   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
533           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
534   str = g_strdup_printf ("%d", cseq);
535   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
536   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
537       "RTP/AVP;multicast;destination=233.252.0.1;ttl=1;port=6000-6001;");
538
539   gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
540   fail_unless (gst_rtsp_client_handle_message (client,
541           &request) == GST_RTSP_OK);
542   gst_rtsp_message_unset (&request);
543
544   session_pool = gst_rtsp_client_get_session_pool (client);
545   fail_unless (session_pool != NULL);
546   /* FIXME: There seems to be a leak of a session here ! */
547   /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
548   g_object_unref (session_pool);
549
550
551
552   /* simple SETUP with a valid URI and multicast, but an invalid ttl */
553   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
554           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
555   str = g_strdup_printf ("%d", cseq);
556   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
557   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
558       "RTP/AVP;multicast;destination=233.252.0.1;ttl=2;port=5000-5001;");
559
560   gst_rtsp_client_set_send_func (client, test_setup_response_461, NULL, NULL);
561   fail_unless (gst_rtsp_client_handle_message (client,
562           &request) == GST_RTSP_OK);
563   gst_rtsp_message_unset (&request);
564
565   session_pool = gst_rtsp_client_get_session_pool (client);
566   fail_unless (session_pool != NULL);
567   /* FIXME: There seems to be a leak of a session here ! */
568   /* fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 0); */
569   g_object_unref (session_pool);
570
571
572   g_object_unref (client);
573 }
574
575 GST_END_TEST;
576
577 GST_START_TEST (test_client_multicast_transport_specific)
578 {
579   GstRTSPClient *client;
580   GstRTSPMessage request = { 0, };
581   gchar *str;
582   GstRTSPSessionPool *session_pool;
583
584   client = setup_multicast_client ();
585
586   gst_rtsp_client_set_use_client_settings (client, TRUE);
587   fail_unless (gst_rtsp_client_get_use_client_settings (client));
588
589   expected_transport = "RTP/AVP;multicast;destination=233.252.0.1;"
590       "ttl=1;port=5000-5001;mode=\"PLAY\"";
591
592   /* simple SETUP with a valid URI and multicast, but an invalid ip */
593   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
594           "rtsp://localhost/test/stream=0") == GST_RTSP_OK);
595   str = g_strdup_printf ("%d", cseq);
596   gst_rtsp_message_take_header (&request, GST_RTSP_HDR_CSEQ, str);
597   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT,
598       expected_transport);
599
600   gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
601       NULL, NULL);
602   fail_unless (gst_rtsp_client_handle_message (client,
603           &request) == GST_RTSP_OK);
604   gst_rtsp_message_unset (&request);
605   expected_transport = NULL;
606
607   gst_rtsp_client_set_send_func (client, test_setup_response_200_multicast,
608       NULL, NULL);
609   session_pool = gst_rtsp_client_get_session_pool (client);
610   fail_unless (session_pool != NULL);
611   fail_unless (gst_rtsp_session_pool_get_n_sessions (session_pool) == 1);
612   g_object_unref (session_pool);
613
614   g_object_unref (client);
615 }
616
617 GST_END_TEST;
618
619 static gboolean
620 test_response_sdp (GstRTSPClient * client, GstRTSPMessage * response,
621     gboolean close, gpointer user_data)
622 {
623   guint8 *data;
624   guint size;
625   GstSDPMessage *sdp_msg;
626   const GstSDPMedia *sdp_media;
627   const GstSDPBandwidth *bw;
628   gint bandwidth_val = GPOINTER_TO_INT (user_data);
629
630   fail_unless (gst_rtsp_message_get_body (response, &data, &size)
631       == GST_RTSP_OK);
632   gst_sdp_message_new (&sdp_msg);
633   fail_unless (gst_sdp_message_parse_buffer (data, size, sdp_msg)
634       == GST_SDP_OK);
635
636   /* session description */
637   /* v= */
638   fail_unless (gst_sdp_message_get_version (sdp_msg) != NULL);
639   /* o= */
640   fail_unless (gst_sdp_message_get_origin (sdp_msg) != NULL);
641   /* s= */
642   fail_unless (gst_sdp_message_get_session_name (sdp_msg) != NULL);
643   /* t=0 0 */
644   fail_unless (gst_sdp_message_times_len (sdp_msg) == 0);
645
646   /* verify number of medias */
647   fail_unless (gst_sdp_message_medias_len (sdp_msg) == 1);
648
649   /* media description */
650   sdp_media = gst_sdp_message_get_media (sdp_msg, 0);
651   fail_unless (sdp_media != NULL);
652
653   /* m= */
654   fail_unless (gst_sdp_media_get_media (sdp_media) != NULL);
655
656   /* media bandwidth */
657   if (bandwidth_val) {
658      fail_unless (gst_sdp_media_bandwidths_len (sdp_media) == 1);
659      bw = gst_sdp_media_get_bandwidth (sdp_media, 0);
660      fail_unless (bw != NULL);
661      fail_unless (g_strcmp0 (bw->bwtype, "AS") == 0);
662      fail_unless (bw->bandwidth == bandwidth_val);
663   } else {
664      fail_unless (gst_sdp_media_bandwidths_len (sdp_media) == 0);
665   }
666
667   gst_sdp_message_free (sdp_msg);
668
669   return TRUE;
670 }
671
672 static void
673 test_client_sdp (const gchar * launch_line, guint * bandwidth_val)
674 {
675   GstRTSPClient *client;
676   GstRTSPMessage request = { 0, };
677   gchar *str;
678
679   /* simple DESCRIBE for an existing url */
680   client = setup_client (launch_line);
681   fail_unless (gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
682             "rtsp://localhost/test") == GST_RTSP_OK);
683   str = g_strdup_printf ("%d", cseq);
684   gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CSEQ, str);
685   g_free (str);
686
687   gst_rtsp_client_set_send_func (client, test_response_sdp, (gpointer) bandwidth_val, NULL);
688   fail_unless (gst_rtsp_client_handle_message (client,
689           &request) == GST_RTSP_OK);
690   gst_rtsp_message_unset (&request);
691
692   g_object_unref (client);
693
694 }
695
696 GST_START_TEST (test_client_sdp_with_max_bitrate_tag)
697 {
698   test_client_sdp ("videotestsrc "
699       "! taginject tags=\"maximum-bitrate=(uint)50000000\" "
700       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (50000));
701
702
703   /* max-bitrate=0: no bandwidth line */
704   test_client_sdp ("videotestsrc "
705       "! taginject tags=\"maximum-bitrate=(uint)0\" "
706       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (0));
707 }
708
709 GST_END_TEST;
710
711 GST_START_TEST (test_client_sdp_with_bitrate_tag)
712 {
713   test_client_sdp ("videotestsrc "
714       "! taginject tags=\"bitrate=(uint)7000000\" "
715       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (7000));
716
717   /* bitrate=0: no bandwdith line */
718   test_client_sdp ("videotestsrc "
719       "! taginject tags=\"bitrate=(uint)0\" "
720       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (0));
721 }
722
723 GST_END_TEST;
724
725 GST_START_TEST (test_client_sdp_with_max_bitrate_and_bitrate_tags)
726 {
727   test_client_sdp ("videotestsrc "
728       "! taginject tags=\"bitrate=(uint)7000000,maximum-bitrate=(uint)50000000\" "
729       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (50000));
730
731   /* max-bitrate is zero: fallback to bitrate */
732   test_client_sdp ("videotestsrc "
733       "! taginject tags=\"bitrate=(uint)7000000,maximum-bitrate=(uint)0\" "
734       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (7000));
735
736   /* max-bitrate=bitrate=0o: no bandwidth line */
737   test_client_sdp ("videotestsrc "
738       "! taginject tags=\"bitrate=(uint)0,maximum-bitrate=(uint)0\" "
739       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", GUINT_TO_POINTER (0));
740 }
741
742 GST_END_TEST;
743
744 GST_START_TEST (test_client_sdp_with_no_bitrate_tags)
745 {
746   test_client_sdp ("videotestsrc "
747       "! video/x-raw,width=352,height=288 ! rtpgstpay name=pay0 pt=96", NULL);
748 }
749
750 GST_END_TEST;
751
752 static Suite *
753 rtspclient_suite (void)
754 {
755   Suite *s = suite_create ("rtspclient");
756   TCase *tc = tcase_create ("general");
757
758   suite_add_tcase (s, tc);
759   tcase_set_timeout (tc, 20);
760   tcase_add_test (tc, test_request);
761   tcase_add_test (tc, test_options);
762   tcase_add_test (tc, test_describe);
763   tcase_add_test (tc, test_client_multicast_transport_404);
764   tcase_add_test (tc, test_client_multicast_transport);
765   tcase_add_test (tc, test_client_multicast_ignore_transport_specific);
766   tcase_add_test (tc, test_client_multicast_invalid_transport_specific);
767   tcase_add_test (tc, test_client_multicast_transport_specific);
768   tcase_add_test (tc, test_client_sdp_with_max_bitrate_tag);
769   tcase_add_test (tc, test_client_sdp_with_bitrate_tag);
770   tcase_add_test (tc, test_client_sdp_with_max_bitrate_and_bitrate_tags);
771   tcase_add_test (tc, test_client_sdp_with_no_bitrate_tags);
772
773   return s;
774 }
775
776 GST_CHECK_MAIN (rtspclient);