tizen 2.3 release
[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 #GstElement that can handle the buffers transported over @trans.
235  *
236  * It is possible that there are several managers available, use @option to
237  * selected one.
238  *
239  * @manager will contain an element name or #NULL when no manager is
240  * needed/available for @trans.
241  *
242  * Returns: #GST_RTSP_OK. 
243  */
244 GstRTSPResult
245 gst_rtsp_transport_get_manager (GstRTSPTransMode trans, const gchar ** manager,
246     guint option)
247 {
248   gint i;
249
250   g_return_val_if_fail (manager != NULL, GST_RTSP_EINVAL);
251
252   for (i = 0; transports[i].name; i++)
253     if (transports[i].mode == trans)
254       break;
255
256   if (option < MAX_MANAGERS)
257     *manager = transports[i].manager[option];
258   else
259     *manager = NULL;
260
261   return GST_RTSP_OK;
262 }
263
264 static void
265 parse_mode (GstRTSPTransport * transport, const gchar * str)
266 {
267   transport->mode_play = (strstr (str, "play") != NULL);
268   transport->mode_record = (strstr (str, "record") != NULL);
269 }
270
271 static gboolean
272 check_range (const gchar * str, gchar ** tmp, gint * range)
273 {
274   glong range_val;
275
276   range_val = strtol (str, tmp, 10);
277   if (range_val >= G_MININT && range_val <= G_MAXINT) {
278     *range = range_val;
279     return TRUE;
280   } else {
281     return FALSE;
282   }
283 }
284
285 static gboolean
286 parse_range (const gchar * str, GstRTSPRange * range)
287 {
288   gchar *minus;
289   gchar *tmp;
290
291   /* even though strtol() allows white space, plus and minus in front of
292    * the number, we do not allow it
293    */
294   if (g_ascii_isspace (*str) || *str == '+' || *str == '-')
295     goto invalid_range;
296
297   minus = strstr (str, "-");
298   if (minus) {
299     if (g_ascii_isspace (minus[1]) || minus[1] == '+' || minus[1] == '-')
300       goto invalid_range;
301
302     if (!check_range (str, &tmp, &range->min) || str == tmp || tmp != minus)
303       goto invalid_range;
304
305     if (!check_range (minus + 1, &tmp, &range->max) || (*tmp && *tmp != ';'))
306       goto invalid_range;
307   } else {
308     if (!check_range (str, &tmp, &range->min) || str == tmp ||
309         (*tmp && *tmp != ';'))
310       goto invalid_range;
311
312     range->max = -1;
313   }
314
315   return TRUE;
316
317 invalid_range:
318   {
319     range->min = -1;
320     range->max = -1;
321     return FALSE;
322   }
323 }
324
325 static gchar *
326 range_as_text (const GstRTSPRange * range)
327 {
328   if (range->min < 0)
329     return NULL;
330   else if (range->max < 0)
331     return g_strdup_printf ("%d", range->min);
332   else
333     return g_strdup_printf ("%d-%d", range->min, range->max);
334 }
335
336 static const gchar *
337 rtsp_transport_mode_as_text (const GstRTSPTransport * transport)
338 {
339   gint i;
340
341   for (i = 0; transports[i].name; i++)
342     if (transports[i].mode == transport->trans)
343       return transports[i].name;
344
345   return NULL;
346 }
347
348 static const gchar *
349 rtsp_transport_profile_as_text (const GstRTSPTransport * transport)
350 {
351   gint i;
352
353   for (i = 0; profiles[i].name; i++)
354     if (profiles[i].profile == transport->profile)
355       return profiles[i].name;
356
357   return NULL;
358 }
359
360 static const gchar *
361 rtsp_transport_ltrans_as_text (const GstRTSPTransport * transport)
362 {
363   gint i;
364
365   /* need to special case GST_RTSP_LOWER_TRANS_UDP_MCAST */
366   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
367     return "udp";
368
369   for (i = 0; ltrans[i].name; i++)
370     if (ltrans[i].ltrans == transport->lower_transport)
371       return ltrans[i].name;
372
373   return NULL;
374 }
375
376 #define IS_VALID_PORT_RANGE(range) \
377     (range.min >= 0 && range.min < 65536 && range.max < 65536)
378
379 #define IS_VALID_INTERLEAVE_RANGE(range) \
380     (range.min >= 0 && range.min < 256 && range.max < 256)
381
382 /**
383  * gst_rtsp_transport_parse:
384  * @str: a transport string
385  * @transport: a #GstRTSPTransport
386  *
387  * Parse the RTSP transport string @str into @transport.
388  *
389  * Returns: a #GstRTSPResult.
390  */
391 GstRTSPResult
392 gst_rtsp_transport_parse (const gchar * str, GstRTSPTransport * transport)
393 {
394   gchar **split, *down, **transp = NULL;
395   guint transport_params = 0;
396   gint i, count;
397
398   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
399   g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
400
401   gst_rtsp_transport_init (transport);
402
403   /* case insensitive */
404   down = g_ascii_strdown (str, -1);
405
406   split = g_strsplit (down, ";", 0);
407   g_free (down);
408
409   /* First field contains the transport/profile/lower_transport */
410   if (split[0] == NULL)
411     goto invalid_transport;
412
413   transp = g_strsplit (split[0], "/", 0);
414
415   if (transp[0] == NULL || transp[1] == NULL)
416     goto invalid_transport;
417
418   for (i = 0; transports[i].name; i++)
419     if (strcmp (transp[0], transports[i].name) == 0)
420       break;
421   transport->trans = transports[i].mode;
422
423   if (transport->trans != GST_RTSP_TRANS_RDT) {
424     for (i = 0; profiles[i].name; i++)
425       if (strcmp (transp[1], profiles[i].name) == 0)
426         break;
427     transport->profile = profiles[i].profile;
428     count = 2;
429   } else {
430     /* RDT has transport/lower_transport */
431     transport->profile = GST_RTSP_PROFILE_AVP;
432     count = 1;
433   }
434
435   if (transp[count] != NULL) {
436     for (i = 0; ltrans[i].name; i++)
437       if (strcmp (transp[count], ltrans[i].name) == 0)
438         break;
439     transport->lower_transport = ltrans[i].ltrans;
440   } else {
441     /* specifying the lower transport is optional */
442     if (transport->trans == GST_RTSP_TRANS_RTP &&
443         transport->profile == GST_RTSP_PROFILE_AVP)
444       transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
445     else
446       transport->lower_transport = GST_RTSP_LOWER_TRANS_UNKNOWN;
447   }
448
449   g_strfreev (transp);
450   transp = NULL;
451
452   if (transport->trans == GST_RTSP_TRANS_UNKNOWN ||
453       transport->profile == GST_RTSP_PROFILE_UNKNOWN ||
454       transport->lower_transport == GST_RTSP_LOWER_TRANS_UNKNOWN)
455     goto unsupported_transport;
456
457   i = 1;
458   while (split[i]) {
459     if (strcmp (split[i], "multicast") == 0) {
460       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
461       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP)
462         goto invalid_transport;
463       transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
464     } else if (strcmp (split[i], "unicast") == 0) {
465       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
466       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
467         transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
468     } else if (g_str_has_prefix (split[i], "destination=")) {
469       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DESTINATION);
470       transport->destination = g_strdup (split[i] + 12);
471     } else if (g_str_has_prefix (split[i], "source=")) {
472       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SOURCE);
473       transport->source = g_strdup (split[i] + 7);
474     } else if (g_str_has_prefix (split[i], "layers=")) {
475       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_LAYERS);
476       transport->layers = strtoul (split[i] + 7, NULL, 10);
477     } else if (g_str_has_prefix (split[i], "mode=")) {
478       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_MODE);
479       parse_mode (transport, split[i] + 5);
480       if (!transport->mode_play && !transport->mode_record)
481         goto invalid_transport;
482     } else if (strcmp (split[i], "append") == 0) {
483       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_APPEND);
484       transport->append = TRUE;
485     } else if (g_str_has_prefix (split[i], "interleaved=")) {
486       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_INTERLEAVED);
487       parse_range (split[i] + 12, &transport->interleaved);
488       if (!IS_VALID_INTERLEAVE_RANGE (transport->interleaved))
489         goto invalid_transport;
490     } else if (g_str_has_prefix (split[i], "ttl=")) {
491       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_TTL);
492       transport->ttl = strtoul (split[i] + 4, NULL, 10);
493       if (transport->ttl >= 256)
494         goto invalid_transport;
495     } else if (g_str_has_prefix (split[i], "port=")) {
496       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_PORT);
497       if (parse_range (split[i] + 5, &transport->port)) {
498         if (!IS_VALID_PORT_RANGE (transport->port))
499           goto invalid_transport;
500       }
501     } else if (g_str_has_prefix (split[i], "client_port=")) {
502       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_CLIENT_PORT);
503       if (parse_range (split[i] + 12, &transport->client_port)) {
504         if (!IS_VALID_PORT_RANGE (transport->client_port))
505           goto invalid_transport;
506       }
507     } else if (g_str_has_prefix (split[i], "server_port=")) {
508       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SERVER_PORT);
509       if (parse_range (split[i] + 12, &transport->server_port)) {
510         if (!IS_VALID_PORT_RANGE (transport->server_port))
511           goto invalid_transport;
512       }
513     } else if (g_str_has_prefix (split[i], "ssrc=")) {
514       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SSRC);
515       transport->ssrc = strtoul (split[i] + 5, NULL, 16);
516     } else {
517       /* unknown field... */
518       g_warning ("unknown transport field \"%s\"", split[i]);
519     }
520     i++;
521   }
522   g_strfreev (split);
523
524   return GST_RTSP_OK;
525
526 unsupported_transport:
527   {
528     g_strfreev (split);
529     return GST_RTSP_ERROR;
530   }
531 invalid_transport:
532   {
533     g_strfreev (transp);
534     g_strfreev (split);
535     return GST_RTSP_EINVAL;
536   }
537 }
538
539 /**
540  * gst_rtsp_transport_as_text:
541  * @transport: a #GstRTSPTransport
542  *
543  * Convert @transport into a string that can be used to signal the transport in
544  * an RTSP SETUP response.
545  *
546  * Returns: a string describing the RTSP transport or #NULL when the transport
547  * is invalid.
548  */
549 gchar *
550 gst_rtsp_transport_as_text (GstRTSPTransport * transport)
551 {
552   GPtrArray *strs;
553   gchar *res;
554   const gchar *tmp;
555
556   g_return_val_if_fail (transport != NULL, NULL);
557
558   strs = g_ptr_array_new ();
559
560   /* add the transport specifier */
561   if ((tmp = rtsp_transport_mode_as_text (transport)) == NULL)
562     goto invalid_transport;
563   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
564
565   g_ptr_array_add (strs, g_strdup ("/"));
566
567   if ((tmp = rtsp_transport_profile_as_text (transport)) == NULL)
568     goto invalid_transport;
569   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
570
571   g_printf("\n %d transport->lower_transport=%d \n", __LINE__, transport->lower_transport);
572   g_ptr_array_add (strs, g_strdup ("/"));
573   if ((tmp = rtsp_transport_ltrans_as_text (transport)) == NULL)
574     goto invalid_transport;
575   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
576
577   /*
578    * the order of the following parameters is the same as the one specified in
579    * RFC 2326 to please some weird RTSP clients that require it
580    */
581
582   /* add the unicast/multicast parameter */
583   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
584     g_ptr_array_add (strs, g_strdup (";multicast"));
585   else
586     g_ptr_array_add (strs, g_strdup (";unicast"));
587
588   /* add the destination parameter */
589   if (transport->destination != NULL) {
590     g_ptr_array_add (strs, g_strdup (";destination="));
591     g_ptr_array_add (strs, g_strdup (transport->destination));
592   }
593
594   /* add the source parameter */
595   if (transport->source != NULL) {
596     g_ptr_array_add (strs, g_strdup (";source="));
597     g_ptr_array_add (strs, g_strdup (transport->source));
598   }
599
600   /* add the interleaved parameter */
601   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP &&
602       transport->interleaved.min >= 0) {
603     if (transport->interleaved.min < 256 && transport->interleaved.max < 256) {
604       g_ptr_array_add (strs, g_strdup (";interleaved="));
605       g_ptr_array_add (strs, range_as_text (&transport->interleaved));
606     } else
607       goto invalid_transport;
608   }
609
610   /* add the append parameter */
611   if (transport->mode_record && transport->append)
612     g_ptr_array_add (strs, g_strdup (";append"));
613
614   /* add the ttl parameter */
615   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST &&
616       transport->ttl != 0) {
617     if (transport->ttl < 256) {
618       g_ptr_array_add (strs, g_strdup (";ttl="));
619       g_ptr_array_add (strs, g_strdup_printf ("%u", transport->ttl));
620     } else
621       goto invalid_transport;
622   }
623
624   /* add the layers parameter */
625   if (transport->layers != 0) {
626     g_ptr_array_add (strs, g_strdup (";layers="));
627     g_ptr_array_add (strs, g_strdup_printf ("%u", transport->layers));
628   }
629
630   /* add the port parameter */
631   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
632     if (transport->trans == GST_RTSP_TRANS_RTP && transport->port.min >= 0) {
633       if (transport->port.min < 65536 && transport->port.max < 65536) {
634         g_ptr_array_add (strs, g_strdup (";port="));
635         g_ptr_array_add (strs, range_as_text (&transport->port));
636       } else
637         goto invalid_transport;
638     }
639
640     /* add the client_port parameter */
641     if (transport->trans == GST_RTSP_TRANS_RTP
642         && transport->client_port.min >= 0) {
643       if (transport->client_port.min < 65536
644           && transport->client_port.max < 65536) {
645         g_ptr_array_add (strs, g_strdup (";client_port="));
646         g_ptr_array_add (strs, range_as_text (&transport->client_port));
647       } else
648         goto invalid_transport;
649     }
650
651     /* add the server_port parameter */
652     if (transport->trans == GST_RTSP_TRANS_RTP
653         && transport->server_port.min >= 0) {
654       if (transport->server_port.min < 65536
655           && transport->server_port.max < 65536) {
656         g_ptr_array_add (strs, g_strdup (";server_port="));
657         g_ptr_array_add (strs, range_as_text (&transport->server_port));
658       } else
659         goto invalid_transport;
660     }
661   }
662
663   /* add the ssrc parameter */
664   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST &&
665       transport->ssrc != 0) {
666     g_ptr_array_add (strs, g_strdup (";ssrc="));
667     g_ptr_array_add (strs, g_strdup_printf ("%08X", transport->ssrc));
668   }
669
670   /* add the mode parameter */
671   if (transport->mode_play && transport->mode_record)
672     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY,RECORD\""));
673   else if (transport->mode_record)
674     g_ptr_array_add (strs, g_strdup (";mode=\"RECORD\""));
675   else if (transport->mode_play)
676     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY\""));
677
678   /* add a terminating NULL */
679   g_ptr_array_add (strs, NULL);
680
681   res = g_strjoinv (NULL, (gchar **) strs->pdata);
682   g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
683
684   return res;
685
686 invalid_transport:
687   {
688     g_ptr_array_add (strs, NULL);
689     g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
690     return NULL;
691   }
692 }
693
694 /**
695  * gst_rtsp_transport_free:
696  * @transport: a #GstRTSPTransport
697  *
698  * Free the memory used by @transport.
699  *
700  * Returns: #GST_RTSP_OK.
701  */
702 GstRTSPResult
703 gst_rtsp_transport_free (GstRTSPTransport * transport)
704 {
705   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
706
707   gst_rtsp_transport_init (transport);
708   g_free (transport);
709
710   return GST_RTSP_OK;
711 }