de3085ed314e2800b4333d334f506182de5a6e21
[framework/multimedia/gst-plugins-base0.10.git] / gst-libs / gst / rtsp / gstrtsptransport.c
1 /* GStreamer
2  * Copyright (C) <2005,2006,2007> Wim Taymans <wim@fluendo.com>
3  *               <2007> Peter Kjellerstedt  <pkj at axis com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 /*
21  * Unless otherwise indicated, Source Code is licensed under MIT license.
22  * See further explanation attached in License Statement (distributed in the file
23  * LICENSE).
24  *
25  * Permission is hereby granted, free of charge, to any person obtaining a copy of
26  * this software and associated documentation files (the "Software"), to deal in
27  * the Software without restriction, including without limitation the rights to
28  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29  * of the Software, and to permit persons to whom the Software is furnished to do
30  * so, subject to the following conditions:
31  *
32  * The above copyright notice and this permission notice shall be included in all
33  * copies or substantial portions of the Software.
34  *
35  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41  * SOFTWARE.
42  */
43
44 /**
45  * SECTION:gstrtsptransport
46  * @short_description: dealing with RTSP transports
47  *  
48  * Provides helper functions to deal with RTSP transport strings.
49  *  
50  * Last reviewed on 2007-07-25 (0.10.14)
51  */
52
53 #include <string.h>
54 #include <stdlib.h>
55
56 #include "gstrtsptransport.h"
57
58 #define MAX_MANAGERS    2
59
60 typedef enum
61 {
62   RTSP_TRANSPORT_DELIVERY = 1 << 0,     /* multicast | unicast */
63   RTSP_TRANSPORT_DESTINATION = 1 << 1,
64   RTSP_TRANSPORT_SOURCE = 1 << 2,
65   RTSP_TRANSPORT_INTERLEAVED = 1 << 3,
66   RTSP_TRANSPORT_APPEND = 1 << 4,
67   RTSP_TRANSPORT_TTL = 1 << 5,
68   RTSP_TRANSPORT_LAYERS = 1 << 6,
69   RTSP_TRANSPORT_PORT = 1 << 7,
70   RTSP_TRANSPORT_CLIENT_PORT = 1 << 8,
71   RTSP_TRANSPORT_SERVER_PORT = 1 << 9,
72   RTSP_TRANSPORT_SSRC = 1 << 10,
73   RTSP_TRANSPORT_MODE = 1 << 11,
74 } RTSPTransportParameter;
75
76 typedef struct
77 {
78   const gchar *name;
79   const GstRTSPTransMode mode;
80   const gchar *gst_mime;
81   const gchar *manager[MAX_MANAGERS];
82 } GstRTSPTransMap;
83
84 static const GstRTSPTransMap transports[] = {
85   {"rtp", GST_RTSP_TRANS_RTP, "application/x-rtp", {"gstrtpbin", "rtpdec"}},
86   {"x-real-rdt", GST_RTSP_TRANS_RDT, "application/x-rdt", {"rdtmanager", NULL}},
87   {"x-pn-tng", GST_RTSP_TRANS_RDT, "application/x-rdt", {"rdtmanager", NULL}},
88   {NULL, GST_RTSP_TRANS_UNKNOWN, NULL, {NULL, NULL}}
89 };
90
91 typedef struct
92 {
93   const gchar *name;
94   const GstRTSPProfile profile;
95 } RTSPProfileMap;
96
97 static const RTSPProfileMap profiles[] = {
98   {"avp", GST_RTSP_PROFILE_AVP},
99   {"savp", GST_RTSP_PROFILE_SAVP},
100   {NULL, GST_RTSP_PROFILE_UNKNOWN}
101 };
102
103 typedef struct
104 {
105   const gchar *name;
106   const GstRTSPLowerTrans ltrans;
107 } RTSPLTransMap;
108
109 static const RTSPLTransMap ltrans[] = {
110   {"udp", GST_RTSP_LOWER_TRANS_UDP},
111   {"mcast", GST_RTSP_LOWER_TRANS_UDP_MCAST},
112   {"tcp", GST_RTSP_LOWER_TRANS_TCP},
113   {NULL, GST_RTSP_LOWER_TRANS_UNKNOWN}
114 };
115
116
117 GType
118 gst_rtsp_lower_trans_get_type (void)
119 {
120   static volatile gsize rtsp_lower_trans_type = 0;
121   static const GFlagsValue rtsp_lower_trans[] = {
122     {GST_RTSP_LOWER_TRANS_UDP, "GST_RTSP_LOWER_TRANS_UDP", "udp-unicast"},
123     {GST_RTSP_LOWER_TRANS_UDP_MCAST, "GST_RTSP_LOWER_TRANS_UDP_MCAST",
124         "udp-multicast"},
125     {GST_RTSP_LOWER_TRANS_TCP, "GST_RTSP_LOWER_TRANS_TCP", "tcp"},
126     {GST_RTSP_LOWER_TRANS_HTTP, "GST_RTSP_LOWER_TRANS_HTTP", "http"},
127     {0, NULL, NULL},
128   };
129
130   if (g_once_init_enter (&rtsp_lower_trans_type)) {
131     GType tmp = g_flags_register_static ("GstRTSPLowerTrans", rtsp_lower_trans);
132     g_once_init_leave (&rtsp_lower_trans_type, tmp);
133   }
134
135   return (GType) rtsp_lower_trans_type;
136 }
137
138 #define RTSP_TRANSPORT_PARAMETER_IS_UNIQUE(param) \
139 G_STMT_START {                                    \
140   if ((transport_params & (param)) != 0)          \
141     goto invalid_transport;                       \
142   transport_params |= (param);                    \
143 } G_STMT_END
144
145 /**
146  * gst_rtsp_transport_new:
147  * @transport: location to hold the new #GstRTSPTransport
148  *
149  * Allocate a new initialized #GstRTSPTransport. Use gst_rtsp_transport_free()
150  * after usage.
151  *
152  * Returns: a #GstRTSPResult. 
153  */
154 GstRTSPResult
155 gst_rtsp_transport_new (GstRTSPTransport ** transport)
156 {
157   GstRTSPTransport *trans;
158
159   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
160
161   trans = g_new0 (GstRTSPTransport, 1);
162
163   *transport = trans;
164
165   return gst_rtsp_transport_init (trans);
166 }
167
168 /**
169  * gst_rtsp_transport_init:
170  * @transport: a #GstRTSPTransport
171  *
172  * Initialize @transport so that it can be used.
173  *
174  * Returns: #GST_RTSP_OK. 
175  */
176 GstRTSPResult
177 gst_rtsp_transport_init (GstRTSPTransport * transport)
178 {
179   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
180
181   g_free (transport->destination);
182   g_free (transport->source);
183
184   memset (transport, 0, sizeof (GstRTSPTransport));
185
186   transport->trans = GST_RTSP_TRANS_RTP;
187   transport->profile = GST_RTSP_PROFILE_AVP;
188   transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
189   transport->mode_play = TRUE;
190   transport->mode_record = FALSE;
191   transport->interleaved.min = -1;
192   transport->interleaved.max = -1;
193   transport->port.min = -1;
194   transport->port.max = -1;
195   transport->client_port.min = -1;
196   transport->client_port.max = -1;
197   transport->server_port.min = -1;
198   transport->server_port.max = -1;
199
200   return GST_RTSP_OK;
201 }
202
203 /**
204  * gst_rtsp_transport_get_mime:
205  * @trans: a #GstRTSPTransMode
206  * @mime: location to hold the result
207  *
208  * Get the mime type of the transport mode @trans. This mime type is typically
209  * used to generate #GstCaps on buffers.
210  *
211  * Returns: #GST_RTSP_OK. 
212  */
213 GstRTSPResult
214 gst_rtsp_transport_get_mime (GstRTSPTransMode trans, const gchar ** mime)
215 {
216   gint i;
217
218   g_return_val_if_fail (mime != NULL, GST_RTSP_EINVAL);
219
220   for (i = 0; transports[i].name; i++)
221     if (transports[i].mode == trans)
222       break;
223   *mime = transports[i].gst_mime;
224
225   return GST_RTSP_OK;
226 }
227
228 /**
229  * gst_rtsp_transport_get_manager:
230  * @trans: a #GstRTSPTransMode
231  * @manager: location to hold the result
232  * @option: option index.
233  *
234  * Get the #GStreamer element that can handle the buffers transported over
235  * @trans.
236  *
237  * It is possible that there are several managers available, use @option to
238  * selected one.
239  *
240  * @manager will contain an element name or #NULL when no manager is
241  * needed/available for @trans.
242  *
243  * Returns: #GST_RTSP_OK. 
244  */
245 GstRTSPResult
246 gst_rtsp_transport_get_manager (GstRTSPTransMode trans, const gchar ** manager,
247     guint option)
248 {
249   gint i;
250
251   g_return_val_if_fail (manager != NULL, GST_RTSP_EINVAL);
252
253   for (i = 0; transports[i].name; i++)
254     if (transports[i].mode == trans)
255       break;
256
257   if (option < MAX_MANAGERS)
258     *manager = transports[i].manager[option];
259   else
260     *manager = NULL;
261
262   return GST_RTSP_OK;
263 }
264
265 static void
266 parse_mode (GstRTSPTransport * transport, const gchar * str)
267 {
268   transport->mode_play = (strstr (str, "play") != NULL);
269   transport->mode_record = (strstr (str, "record") != NULL);
270 }
271
272 static gboolean
273 check_range (const gchar * str, gchar ** tmp, gint * range)
274 {
275   glong range_val;
276
277   range_val = strtol (str, tmp, 10);
278   if (range_val >= G_MININT && range_val <= G_MAXINT) {
279     *range = range_val;
280     return TRUE;
281   } else {
282     return FALSE;
283   }
284 }
285
286 static gboolean
287 parse_range (const gchar * str, GstRTSPRange * range)
288 {
289   gchar *minus;
290   gchar *tmp;
291
292   /* even though strtol() allows white space, plus and minus in front of
293    * the number, we do not allow it
294    */
295   if (g_ascii_isspace (*str) || *str == '+' || *str == '-')
296     goto invalid_range;
297
298   minus = strstr (str, "-");
299   if (minus) {
300     if (g_ascii_isspace (minus[1]) || minus[1] == '+' || minus[1] == '-')
301       goto invalid_range;
302
303     if (!check_range (str, &tmp, &range->min) || str == tmp || tmp != minus)
304       goto invalid_range;
305
306     if (!check_range (minus + 1, &tmp, &range->max) || (*tmp && *tmp != ';'))
307       goto invalid_range;
308   } else {
309     if (!check_range (str, &tmp, &range->min) || str == tmp ||
310         (*tmp && *tmp != ';'))
311       goto invalid_range;
312
313     range->max = -1;
314   }
315
316   return TRUE;
317
318 invalid_range:
319   {
320     range->min = -1;
321     range->max = -1;
322     return FALSE;
323   }
324 }
325
326 static gchar *
327 range_as_text (const GstRTSPRange * range)
328 {
329   if (range->min < 0)
330     return NULL;
331   else if (range->max < 0)
332     return g_strdup_printf ("%d", range->min);
333   else
334     return g_strdup_printf ("%d-%d", range->min, range->max);
335 }
336
337 static const gchar *
338 rtsp_transport_mode_as_text (const GstRTSPTransport * transport)
339 {
340   gint i;
341
342   for (i = 0; transports[i].name; i++)
343     if (transports[i].mode == transport->trans)
344       return transports[i].name;
345
346   return NULL;
347 }
348
349 static const gchar *
350 rtsp_transport_profile_as_text (const GstRTSPTransport * transport)
351 {
352   gint i;
353
354   for (i = 0; profiles[i].name; i++)
355     if (profiles[i].profile == transport->profile)
356       return profiles[i].name;
357
358   return NULL;
359 }
360
361 static const gchar *
362 rtsp_transport_ltrans_as_text (const GstRTSPTransport * transport)
363 {
364   gint i;
365
366   /* need to special case GST_RTSP_LOWER_TRANS_UDP_MCAST */
367   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
368     return "udp";
369
370   for (i = 0; ltrans[i].name; i++)
371     if (ltrans[i].ltrans == transport->lower_transport)
372       return ltrans[i].name;
373
374   return NULL;
375 }
376
377 #define IS_VALID_PORT_RANGE(range) \
378     (range.min >= 0 && range.min < 65536 && range.max < 65536)
379
380 #define IS_VALID_INTERLEAVE_RANGE(range) \
381     (range.min >= 0 && range.min < 256 && range.max < 256)
382
383 /**
384  * gst_rtsp_transport_parse:
385  * @str: a transport string
386  * @transport: a #GstRTSPTransport
387  *
388  * Parse the RTSP transport string @str into @transport.
389  *
390  * Returns: a #GstRTSPResult.
391  */
392 GstRTSPResult
393 gst_rtsp_transport_parse (const gchar * str, GstRTSPTransport * transport)
394 {
395   gchar **split, *down, **transp = NULL;
396   guint transport_params = 0;
397   gint i, count;
398
399   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
400   g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
401
402   gst_rtsp_transport_init (transport);
403
404   /* case insensitive */
405   down = g_ascii_strdown (str, -1);
406
407   split = g_strsplit (down, ";", 0);
408   g_free (down);
409
410   /* First field contains the transport/profile/lower_transport */
411   if (split[0] == NULL)
412     goto invalid_transport;
413
414   transp = g_strsplit (split[0], "/", 0);
415
416   if (transp[0] == NULL || transp[1] == NULL)
417     goto invalid_transport;
418
419   for (i = 0; transports[i].name; i++)
420     if (strcmp (transp[0], transports[i].name) == 0)
421       break;
422   transport->trans = transports[i].mode;
423
424   if (transport->trans != GST_RTSP_TRANS_RDT) {
425     for (i = 0; profiles[i].name; i++)
426       if (strcmp (transp[1], profiles[i].name) == 0)
427         break;
428     transport->profile = profiles[i].profile;
429     count = 2;
430   } else {
431     /* RDT has transport/lower_transport */
432     transport->profile = GST_RTSP_PROFILE_AVP;
433     count = 1;
434   }
435
436   if (transp[count] != NULL) {
437     for (i = 0; ltrans[i].name; i++)
438       if (strcmp (transp[count], ltrans[i].name) == 0)
439         break;
440     transport->lower_transport = ltrans[i].ltrans;
441   } else {
442     /* specifying the lower transport is optional */
443     if (transport->trans == GST_RTSP_TRANS_RTP &&
444         transport->profile == GST_RTSP_PROFILE_AVP)
445       transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
446     else
447       transport->lower_transport = GST_RTSP_LOWER_TRANS_UNKNOWN;
448   }
449
450   g_strfreev (transp);
451   transp = NULL;
452
453   if (transport->trans == GST_RTSP_TRANS_UNKNOWN ||
454       transport->profile == GST_RTSP_PROFILE_UNKNOWN ||
455       transport->lower_transport == GST_RTSP_LOWER_TRANS_UNKNOWN)
456     goto unsupported_transport;
457
458   i = 1;
459   while (split[i]) {
460     if (strcmp (split[i], "multicast") == 0) {
461       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
462       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP)
463         goto invalid_transport;
464       transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
465     } else if (strcmp (split[i], "unicast") == 0) {
466       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
467       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
468         transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
469     } else if (g_str_has_prefix (split[i], "destination=")) {
470       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DESTINATION);
471       transport->destination = g_strdup (split[i] + 12);
472     } else if (g_str_has_prefix (split[i], "source=")) {
473       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SOURCE);
474       transport->source = g_strdup (split[i] + 7);
475     } else if (g_str_has_prefix (split[i], "layers=")) {
476       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_LAYERS);
477       transport->layers = strtoul (split[i] + 7, NULL, 10);
478     } else if (g_str_has_prefix (split[i], "mode=")) {
479       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_MODE);
480       parse_mode (transport, split[i] + 5);
481       if (!transport->mode_play && !transport->mode_record)
482         goto invalid_transport;
483     } else if (strcmp (split[i], "append") == 0) {
484       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_APPEND);
485       transport->append = TRUE;
486     } else if (g_str_has_prefix (split[i], "interleaved=")) {
487       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_INTERLEAVED);
488       parse_range (split[i] + 12, &transport->interleaved);
489       if (!IS_VALID_INTERLEAVE_RANGE (transport->interleaved))
490         goto invalid_transport;
491     } else if (g_str_has_prefix (split[i], "ttl=")) {
492       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_TTL);
493       transport->ttl = strtoul (split[i] + 4, NULL, 10);
494       if (transport->ttl >= 256)
495         goto invalid_transport;
496     } else if (g_str_has_prefix (split[i], "port=")) {
497       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_PORT);
498       if (parse_range (split[i] + 5, &transport->port)) {
499         if (!IS_VALID_PORT_RANGE (transport->port))
500           goto invalid_transport;
501       }
502     } else if (g_str_has_prefix (split[i], "client_port=")) {
503       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_CLIENT_PORT);
504       if (parse_range (split[i] + 12, &transport->client_port)) {
505         if (!IS_VALID_PORT_RANGE (transport->client_port))
506           goto invalid_transport;
507       }
508     } else if (g_str_has_prefix (split[i], "server_port=")) {
509       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SERVER_PORT);
510       if (parse_range (split[i] + 12, &transport->server_port)) {
511         if (!IS_VALID_PORT_RANGE (transport->server_port))
512           goto invalid_transport;
513       }
514     } else if (g_str_has_prefix (split[i], "ssrc=")) {
515       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SSRC);
516       transport->ssrc = strtoul (split[i] + 5, NULL, 16);
517     } else {
518       /* unknown field... */
519       g_warning ("unknown transport field \"%s\"", split[i]);
520     }
521     i++;
522   }
523   g_strfreev (split);
524
525   return GST_RTSP_OK;
526
527 unsupported_transport:
528   {
529     g_strfreev (split);
530     return GST_RTSP_ERROR;
531   }
532 invalid_transport:
533   {
534     g_strfreev (transp);
535     g_strfreev (split);
536     return GST_RTSP_EINVAL;
537   }
538 }
539
540 /**
541  * gst_rtsp_transport_as_text:
542  * @transport: a #GstRTSPTransport
543  *
544  * Convert @transport into a string that can be used to signal the transport in
545  * an RTSP SETUP response.
546  *
547  * Returns: a string describing the RTSP transport or #NULL when the transport
548  * is invalid.
549  */
550 gchar *
551 gst_rtsp_transport_as_text (GstRTSPTransport * transport)
552 {
553   GPtrArray *strs;
554   gchar *res;
555   const gchar *tmp;
556
557   g_return_val_if_fail (transport != NULL, NULL);
558
559   strs = g_ptr_array_new ();
560
561   /* add the transport specifier */
562   if ((tmp = rtsp_transport_mode_as_text (transport)) == NULL)
563     goto invalid_transport;
564   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
565
566   g_ptr_array_add (strs, g_strdup ("/"));
567
568   if ((tmp = rtsp_transport_profile_as_text (transport)) == NULL)
569     goto invalid_transport;
570   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
571
572   if (transport->trans != GST_RTSP_TRANS_RTP ||
573       transport->profile != GST_RTSP_PROFILE_AVP ||
574       transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
575     g_ptr_array_add (strs, g_strdup ("/"));
576
577     if ((tmp = rtsp_transport_ltrans_as_text (transport)) == NULL)
578       goto invalid_transport;
579
580     g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
581   }
582
583   /*
584    * the order of the following parameters is the same as the one specified in
585    * RFC 2326 to please some weird RTSP clients that require it
586    */
587
588   /* add the unicast/multicast parameter */
589   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
590     g_ptr_array_add (strs, g_strdup (";multicast"));
591   else
592     g_ptr_array_add (strs, g_strdup (";unicast"));
593
594   /* add the destination parameter */
595   if (transport->destination != NULL) {
596     g_ptr_array_add (strs, g_strdup (";destination="));
597     g_ptr_array_add (strs, g_strdup (transport->destination));
598   }
599
600   /* add the source parameter */
601   if (transport->source != NULL) {
602     g_ptr_array_add (strs, g_strdup (";source="));
603     g_ptr_array_add (strs, g_strdup (transport->source));
604   }
605
606   /* add the interleaved parameter */
607   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP &&
608       transport->interleaved.min >= 0) {
609     if (transport->interleaved.min < 256 && transport->interleaved.max < 256) {
610       g_ptr_array_add (strs, g_strdup (";interleaved="));
611       g_ptr_array_add (strs, range_as_text (&transport->interleaved));
612     } else
613       goto invalid_transport;
614   }
615
616   /* add the append parameter */
617   if (transport->mode_record && transport->append)
618     g_ptr_array_add (strs, g_strdup (";append"));
619
620   /* add the ttl parameter */
621   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST &&
622       transport->ttl != 0) {
623     if (transport->ttl < 256) {
624       g_ptr_array_add (strs, g_strdup (";ttl="));
625       g_ptr_array_add (strs, g_strdup_printf ("%u", transport->ttl));
626     } else
627       goto invalid_transport;
628   }
629
630   /* add the layers parameter */
631   if (transport->layers != 0) {
632     g_ptr_array_add (strs, g_strdup (";layers="));
633     g_ptr_array_add (strs, g_strdup_printf ("%u", transport->layers));
634   }
635
636   /* add the port parameter */
637   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
638     if (transport->trans == GST_RTSP_TRANS_RTP && transport->port.min >= 0) {
639       if (transport->port.min < 65536 && transport->port.max < 65536) {
640         g_ptr_array_add (strs, g_strdup (";port="));
641         g_ptr_array_add (strs, range_as_text (&transport->port));
642       } else
643         goto invalid_transport;
644     }
645
646     /* add the client_port parameter */
647     if (transport->trans == GST_RTSP_TRANS_RTP
648         && transport->client_port.min >= 0) {
649       if (transport->client_port.min < 65536
650           && transport->client_port.max < 65536) {
651         g_ptr_array_add (strs, g_strdup (";client_port="));
652         g_ptr_array_add (strs, range_as_text (&transport->client_port));
653       } else
654         goto invalid_transport;
655     }
656
657     /* add the server_port parameter */
658     if (transport->trans == GST_RTSP_TRANS_RTP
659         && transport->server_port.min >= 0) {
660       if (transport->server_port.min < 65536
661           && transport->server_port.max < 65536) {
662         g_ptr_array_add (strs, g_strdup (";server_port="));
663         g_ptr_array_add (strs, range_as_text (&transport->server_port));
664       } else
665         goto invalid_transport;
666     }
667   }
668
669   /* add the ssrc parameter */
670   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST &&
671       transport->ssrc != 0) {
672     g_ptr_array_add (strs, g_strdup (";ssrc="));
673     g_ptr_array_add (strs, g_strdup_printf ("%08X", transport->ssrc));
674   }
675
676   /* add the mode parameter */
677   if (transport->mode_play && transport->mode_record)
678     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY,RECORD\""));
679   else if (transport->mode_record)
680     g_ptr_array_add (strs, g_strdup (";mode=\"RECORD\""));
681   else if (transport->mode_play)
682     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY\""));
683
684   /* add a terminating NULL */
685   g_ptr_array_add (strs, NULL);
686
687   res = g_strjoinv (NULL, (gchar **) strs->pdata);
688   g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
689
690   return res;
691
692 invalid_transport:
693   {
694     g_ptr_array_add (strs, NULL);
695     g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
696     return NULL;
697   }
698 }
699
700 /**
701  * gst_rtsp_transport_free:
702  * @transport: a #GstRTSPTransport
703  *
704  * Free the memory used by @transport.
705  *
706  * Returns: #GST_RTSP_OK.
707  */
708 GstRTSPResult
709 gst_rtsp_transport_free (GstRTSPTransport * transport)
710 {
711   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
712
713   gst_rtsp_transport_init (transport);
714   g_free (transport);
715
716   return GST_RTSP_OK;
717 }