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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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  * @title: GstRTSPRange
47  * @short_description: dealing with RTSP transports
48  *
49  * Provides helper functions to deal with RTSP transport strings.
50  */
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
54 
55 #include <string.h>
56 #include <stdlib.h>
57 
58 #include "gstrtsptransport.h"
59 #include "gstrtsp-enumtypes.h"
60 
61 #define MAX_MANAGERS	2
62 
63 typedef enum
64 {
65   RTSP_TRANSPORT_DELIVERY = 1 << 0,     /* multicast | unicast */
66   RTSP_TRANSPORT_DESTINATION = 1 << 1,
67   RTSP_TRANSPORT_SOURCE = 1 << 2,
68   RTSP_TRANSPORT_INTERLEAVED = 1 << 3,
69   RTSP_TRANSPORT_APPEND = 1 << 4,
70   RTSP_TRANSPORT_TTL = 1 << 5,
71   RTSP_TRANSPORT_LAYERS = 1 << 6,
72   RTSP_TRANSPORT_PORT = 1 << 7,
73   RTSP_TRANSPORT_CLIENT_PORT = 1 << 8,
74   RTSP_TRANSPORT_SERVER_PORT = 1 << 9,
75   RTSP_TRANSPORT_SSRC = 1 << 10,
76   RTSP_TRANSPORT_MODE = 1 << 11,
77 } RTSPTransportParameter;
78 
79 typedef struct
80 {
81   const gchar *name;
82   const GstRTSPTransMode mode;
83   const GstRTSPProfile profile;
84   const GstRTSPLowerTrans ltrans;
85   const gchar *media_type;
86   const gchar *manager[MAX_MANAGERS];
87 } GstRTSPTransMap;
88 
89 static const GstRTSPTransMap transports[] = {
90   {"rtp", GST_RTSP_TRANS_RTP, GST_RTSP_PROFILE_AVP,
91         GST_RTSP_LOWER_TRANS_UDP_MCAST, "application/x-rtp",
92       {"rtpbin", "rtpdec"}},
93   {"srtp", GST_RTSP_TRANS_RTP, GST_RTSP_PROFILE_SAVP,
94         GST_RTSP_LOWER_TRANS_UDP_MCAST, "application/x-srtp",
95       {"rtpbin", "rtpdec"}},
96   {"rtpf", GST_RTSP_TRANS_RTP, GST_RTSP_PROFILE_AVPF,
97         GST_RTSP_LOWER_TRANS_UDP_MCAST, "application/x-rtp",
98       {"rtpbin", "rtpdec"}},
99   {"srtpf", GST_RTSP_TRANS_RTP, GST_RTSP_PROFILE_SAVPF,
100         GST_RTSP_LOWER_TRANS_UDP_MCAST, "application/x-srtp",
101       {"rtpbin", "rtpdec"}},
102   {"x-real-rdt", GST_RTSP_TRANS_RDT, GST_RTSP_PROFILE_AVP,
103         GST_RTSP_LOWER_TRANS_UNKNOWN, "application/x-rdt",
104       {"rdtmanager", NULL}},
105   {"x-pn-tng", GST_RTSP_TRANS_RDT, GST_RTSP_PROFILE_AVP,
106         GST_RTSP_LOWER_TRANS_UNKNOWN, "application/x-rdt",
107       {"rdtmanager", NULL}},
108   {NULL, GST_RTSP_TRANS_UNKNOWN, GST_RTSP_PROFILE_UNKNOWN,
109       GST_RTSP_LOWER_TRANS_UNKNOWN, NULL, {NULL, NULL}}
110 };
111 
112 typedef struct
113 {
114   const gchar *name;
115   const GstRTSPProfile profile;
116 } RTSPProfileMap;
117 
118 static const RTSPProfileMap profiles[] = {
119   {"avp", GST_RTSP_PROFILE_AVP},
120   {"savp", GST_RTSP_PROFILE_SAVP},
121   {"avpf", GST_RTSP_PROFILE_AVPF},
122   {"savpf", GST_RTSP_PROFILE_SAVPF},
123   {NULL, GST_RTSP_PROFILE_UNKNOWN}
124 };
125 
126 typedef struct
127 {
128   const gchar *name;
129   const GstRTSPLowerTrans ltrans;
130 } RTSPLTransMap;
131 
132 static const RTSPLTransMap ltrans[] = {
133   {"udp", GST_RTSP_LOWER_TRANS_UDP},
134   {"mcast", GST_RTSP_LOWER_TRANS_UDP_MCAST},
135   {"tcp", GST_RTSP_LOWER_TRANS_TCP},
136   {NULL, GST_RTSP_LOWER_TRANS_UNKNOWN}
137 };
138 
139 #define RTSP_TRANSPORT_PARAMETER_IS_UNIQUE(param) \
140 G_STMT_START {                                    \
141   if ((transport_params & (param)) != 0)          \
142     goto invalid_transport;                       \
143   transport_params |= (param);                    \
144 } G_STMT_END
145 
146 /**
147  * gst_rtsp_transport_new:
148  * @transport: location to hold the new #GstRTSPTransport
149  *
150  * Allocate a new initialized #GstRTSPTransport. Use gst_rtsp_transport_free()
151  * after usage.
152  *
153  * Returns: a #GstRTSPResult.
154  */
155 GstRTSPResult
gst_rtsp_transport_new(GstRTSPTransport ** transport)156 gst_rtsp_transport_new (GstRTSPTransport ** transport)
157 {
158   GstRTSPTransport *trans;
159 
160   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
161 
162   trans = g_new0 (GstRTSPTransport, 1);
163 
164   *transport = trans;
165 
166   return gst_rtsp_transport_init (trans);
167 }
168 
169 /**
170  * gst_rtsp_transport_init:
171  * @transport: a #GstRTSPTransport
172  *
173  * Initialize @transport so that it can be used.
174  *
175  * Returns: #GST_RTSP_OK.
176  */
177 GstRTSPResult
gst_rtsp_transport_init(GstRTSPTransport * transport)178 gst_rtsp_transport_init (GstRTSPTransport * transport)
179 {
180   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
181 
182   g_free (transport->destination);
183   g_free (transport->source);
184 
185   memset (transport, 0, sizeof (GstRTSPTransport));
186 
187   transport->trans = GST_RTSP_TRANS_RTP;
188   transport->profile = GST_RTSP_PROFILE_AVP;
189   transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
190   transport->mode_play = TRUE;
191   transport->mode_record = FALSE;
192   transport->interleaved.min = -1;
193   transport->interleaved.max = -1;
194   transport->port.min = -1;
195   transport->port.max = -1;
196   transport->client_port.min = -1;
197   transport->client_port.max = -1;
198   transport->server_port.min = -1;
199   transport->server_port.max = -1;
200 
201   return GST_RTSP_OK;
202 }
203 
204 #ifndef GST_REMOVE_DEPRECATED
205 /**
206  * gst_rtsp_transport_get_mime:
207  * @trans: a #GstRTSPTransMode
208  * @mime: location to hold the result
209  *
210  * Get the mime type of the transport mode @trans. This mime type is typically
211  * used to generate #GstCaps events.
212  *
213  * Deprecated: This functions only deals with the GstRTSPTransMode and only
214  *    returns the mime type for #GST_RTSP_PROFILE_AVP. Use
215  *    gst_rtsp_transport_get_media_type() instead.
216  *
217  * Returns: #GST_RTSP_OK.
218  */
219 GstRTSPResult
gst_rtsp_transport_get_mime(GstRTSPTransMode trans,const gchar ** mime)220 gst_rtsp_transport_get_mime (GstRTSPTransMode trans, const gchar ** mime)
221 {
222   gint i;
223 
224   g_return_val_if_fail (mime != NULL, GST_RTSP_EINVAL);
225 
226   for (i = 0; transports[i].name; i++)
227     if (transports[i].mode == trans
228         && transports[i].profile == GST_RTSP_PROFILE_AVP)
229       break;
230   *mime = transports[i].media_type;
231 
232   return GST_RTSP_OK;
233 }
234 #endif
235 
236 /**
237  * gst_rtsp_transport_get_media_type:
238  * @transport: a #GstRTSPTransport
239  * @media_type: (out) (transfer none): media type of @transport
240  *
241  * Get the media type of @transport. This media type is typically
242  * used to generate #GstCaps events.
243  *
244  * Since: 1.4
245  *
246  * Returns: #GST_RTSP_OK.
247  */
248 GstRTSPResult
gst_rtsp_transport_get_media_type(GstRTSPTransport * transport,const gchar ** media_type)249 gst_rtsp_transport_get_media_type (GstRTSPTransport * transport,
250     const gchar ** media_type)
251 {
252   gint i;
253 
254   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
255   g_return_val_if_fail (media_type != NULL, GST_RTSP_EINVAL);
256 
257   for (i = 0; transports[i].name; i++)
258     if (transports[i].mode == transport->trans
259         && transports[i].profile == transport->profile)
260       break;
261   *media_type = transports[i].media_type;
262 
263   return GST_RTSP_OK;
264 }
265 
266 static GstRTSPLowerTrans
get_default_lower_trans(GstRTSPTransport * transport)267 get_default_lower_trans (GstRTSPTransport * transport)
268 {
269   gint i;
270 
271   for (i = 0; transports[i].name; i++)
272     if (transports[i].mode == transport->trans
273         && transports[i].profile == transport->profile)
274       break;
275 
276   return transports[i].ltrans;
277 }
278 
279 /**
280  * gst_rtsp_transport_get_manager:
281  * @trans: a #GstRTSPTransMode
282  * @manager: (out) (nullable) (transfer none): location to hold the result
283  * @option: option index.
284  *
285  * Get the #GstElement that can handle the buffers transported over @trans.
286  *
287  * It is possible that there are several managers available, use @option to
288  * selected one.
289  *
290  * @manager will contain an element name or %NULL when no manager is
291  * needed/available for @trans.
292  *
293  * Returns: #GST_RTSP_OK.
294  */
295 GstRTSPResult
gst_rtsp_transport_get_manager(GstRTSPTransMode trans,const gchar ** manager,guint option)296 gst_rtsp_transport_get_manager (GstRTSPTransMode trans, const gchar ** manager,
297     guint option)
298 {
299   gint i;
300 
301   g_return_val_if_fail (manager != NULL, GST_RTSP_EINVAL);
302 
303   for (i = 0; transports[i].name; i++)
304     if (transports[i].mode == trans)
305       break;
306 
307   if (option < MAX_MANAGERS)
308     *manager = transports[i].manager[option];
309   else
310     *manager = NULL;
311 
312   return GST_RTSP_OK;
313 }
314 
315 static void
parse_mode(GstRTSPTransport * transport,const gchar * str)316 parse_mode (GstRTSPTransport * transport, const gchar * str)
317 {
318   transport->mode_play = (strstr (str, "play") != NULL);
319   transport->mode_record = (strstr (str, "record") != NULL);
320 }
321 
322 static gboolean
check_range(const gchar * str,gchar ** tmp,gint * range)323 check_range (const gchar * str, gchar ** tmp, gint * range)
324 {
325   glong range_val;
326 
327   range_val = strtol (str, tmp, 10);
328   if (range_val >= G_MININT && range_val <= G_MAXINT) {
329     *range = range_val;
330     return TRUE;
331   } else {
332     return FALSE;
333   }
334 }
335 
336 static gboolean
parse_range(const gchar * str,GstRTSPRange * range)337 parse_range (const gchar * str, GstRTSPRange * range)
338 {
339   gchar *minus;
340   gchar *tmp;
341 
342   /* even though strtol() allows white space, plus and minus in front of
343    * the number, we do not allow it
344    */
345   if (g_ascii_isspace (*str) || *str == '+' || *str == '-')
346     goto invalid_range;
347 
348   minus = strstr (str, "-");
349   if (minus) {
350     if (g_ascii_isspace (minus[1]) || minus[1] == '+' || minus[1] == '-')
351       goto invalid_range;
352 
353     if (!check_range (str, &tmp, &range->min) || str == tmp || tmp != minus)
354       goto invalid_range;
355 
356     if (!check_range (minus + 1, &tmp, &range->max) || (*tmp && *tmp != ';'))
357       goto invalid_range;
358   } else {
359     if (!check_range (str, &tmp, &range->min) || str == tmp ||
360         (*tmp && *tmp != ';'))
361       goto invalid_range;
362 
363     range->max = -1;
364   }
365 
366   return TRUE;
367 
368 invalid_range:
369   {
370     range->min = -1;
371     range->max = -1;
372     return FALSE;
373   }
374 }
375 
376 static gchar *
range_as_text(const GstRTSPRange * range)377 range_as_text (const GstRTSPRange * range)
378 {
379   if (range->min < 0)
380     return NULL;
381   else if (range->max < 0)
382     return g_strdup_printf ("%d", range->min);
383   else
384     return g_strdup_printf ("%d-%d", range->min, range->max);
385 }
386 
387 static const gchar *
rtsp_transport_mode_as_text(const GstRTSPTransport * transport)388 rtsp_transport_mode_as_text (const GstRTSPTransport * transport)
389 {
390   gint i;
391 
392   for (i = 0; transports[i].name; i++)
393     if (transports[i].mode == transport->trans)
394       return transports[i].name;
395 
396   return NULL;
397 }
398 
399 static const gchar *
rtsp_transport_profile_as_text(const GstRTSPTransport * transport)400 rtsp_transport_profile_as_text (const GstRTSPTransport * transport)
401 {
402   gint i;
403 
404   for (i = 0; profiles[i].name; i++)
405     if (profiles[i].profile == transport->profile)
406       return profiles[i].name;
407 
408   return NULL;
409 }
410 
411 static const gchar *
rtsp_transport_ltrans_as_text(const GstRTSPTransport * transport)412 rtsp_transport_ltrans_as_text (const GstRTSPTransport * transport)
413 {
414   gint i;
415 
416   /* need to special case GST_RTSP_LOWER_TRANS_UDP_MCAST */
417   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
418     return "udp";
419 
420   for (i = 0; ltrans[i].name; i++)
421     if (ltrans[i].ltrans == transport->lower_transport)
422       return ltrans[i].name;
423 
424   return NULL;
425 }
426 
427 #define IS_VALID_PORT_RANGE(range) \
428     (range.min >= 0 && range.min < 65536 && range.max < 65536)
429 
430 #define IS_VALID_INTERLEAVE_RANGE(range) \
431     (range.min >= 0 && range.min < 256 && range.max < 256)
432 
433 /**
434  * gst_rtsp_transport_parse:
435  * @str: a transport string
436  * @transport: a #GstRTSPTransport
437  *
438  * Parse the RTSP transport string @str into @transport.
439  *
440  * Returns: a #GstRTSPResult.
441  */
442 GstRTSPResult
gst_rtsp_transport_parse(const gchar * str,GstRTSPTransport * transport)443 gst_rtsp_transport_parse (const gchar * str, GstRTSPTransport * transport)
444 {
445   gchar **split, *down, **transp = NULL;
446   guint transport_params = 0;
447   gint i, count;
448 
449   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
450   g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
451 
452   gst_rtsp_transport_init (transport);
453 
454   /* case insensitive */
455   down = g_ascii_strdown (str, -1);
456 
457   split = g_strsplit (down, ";", 0);
458   g_free (down);
459 
460   /* First field contains the transport/profile/lower_transport */
461   if (split[0] == NULL)
462     goto invalid_transport;
463 
464   transp = g_strsplit (split[0], "/", 0);
465 
466   if (transp[0] == NULL || transp[1] == NULL)
467     goto invalid_transport;
468 
469   for (i = 0; transports[i].name; i++)
470     if (strcmp (transp[0], transports[i].name) == 0)
471       break;
472   transport->trans = transports[i].mode;
473 
474   if (transport->trans != GST_RTSP_TRANS_RDT) {
475     for (i = 0; profiles[i].name; i++)
476       if (strcmp (transp[1], profiles[i].name) == 0)
477         break;
478     transport->profile = profiles[i].profile;
479     count = 2;
480   } else {
481     /* RDT has transport/lower_transport */
482     transport->profile = GST_RTSP_PROFILE_AVP;
483     count = 1;
484   }
485 
486   if (transp[count] != NULL) {
487     for (i = 0; ltrans[i].name; i++)
488       if (strcmp (transp[count], ltrans[i].name) == 0)
489         break;
490     transport->lower_transport = ltrans[i].ltrans;
491   } else {
492     /* specifying the lower transport is optional */
493     transport->lower_transport = get_default_lower_trans (transport);
494   }
495 
496   g_strfreev (transp);
497   transp = NULL;
498 
499   if (transport->trans == GST_RTSP_TRANS_UNKNOWN ||
500       transport->profile == GST_RTSP_PROFILE_UNKNOWN ||
501       transport->lower_transport == GST_RTSP_LOWER_TRANS_UNKNOWN)
502     goto unsupported_transport;
503 
504   i = 1;
505   while (split[i]) {
506     if (strcmp (split[i], "multicast") == 0) {
507       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
508       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP)
509         goto invalid_transport;
510       transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP_MCAST;
511     } else if (strcmp (split[i], "unicast") == 0) {
512       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DELIVERY);
513       if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
514         transport->lower_transport = GST_RTSP_LOWER_TRANS_UDP;
515     } else if (g_str_has_prefix (split[i], "destination=")) {
516       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_DESTINATION);
517       transport->destination = g_strdup (split[i] + 12);
518     } else if (g_str_has_prefix (split[i], "source=")) {
519       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SOURCE);
520       transport->source = g_strdup (split[i] + 7);
521     } else if (g_str_has_prefix (split[i], "layers=")) {
522       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_LAYERS);
523       transport->layers = strtoul (split[i] + 7, NULL, 10);
524     } else if (g_str_has_prefix (split[i], "mode=")) {
525       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_MODE);
526       parse_mode (transport, split[i] + 5);
527       if (!transport->mode_play && !transport->mode_record)
528         goto invalid_transport;
529     } else if (strcmp (split[i], "append") == 0) {
530       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_APPEND);
531       transport->append = TRUE;
532     } else if (g_str_has_prefix (split[i], "interleaved=")) {
533       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_INTERLEAVED);
534       parse_range (split[i] + 12, &transport->interleaved);
535       if (!IS_VALID_INTERLEAVE_RANGE (transport->interleaved))
536         goto invalid_transport;
537     } else if (g_str_has_prefix (split[i], "ttl=")) {
538       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_TTL);
539       transport->ttl = strtoul (split[i] + 4, NULL, 10);
540       if (transport->ttl >= 256)
541         goto invalid_transport;
542     } else if (g_str_has_prefix (split[i], "port=")) {
543       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_PORT);
544       if (parse_range (split[i] + 5, &transport->port)) {
545         if (!IS_VALID_PORT_RANGE (transport->port))
546           goto invalid_transport;
547       }
548     } else if (g_str_has_prefix (split[i], "client_port=")) {
549       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_CLIENT_PORT);
550       if (parse_range (split[i] + 12, &transport->client_port)) {
551         if (!IS_VALID_PORT_RANGE (transport->client_port))
552           goto invalid_transport;
553       }
554     } else if (g_str_has_prefix (split[i], "server_port=")) {
555       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SERVER_PORT);
556       if (parse_range (split[i] + 12, &transport->server_port)) {
557         if (!IS_VALID_PORT_RANGE (transport->server_port))
558           goto invalid_transport;
559       }
560     } else if (g_str_has_prefix (split[i], "ssrc=")) {
561       RTSP_TRANSPORT_PARAMETER_IS_UNIQUE (RTSP_TRANSPORT_SSRC);
562       transport->ssrc = strtoul (split[i] + 5, NULL, 16);
563     } else {
564       /* unknown field... */
565       if (strlen (split[i]) > 0) {
566         g_warning ("unknown transport field \"%s\"", split[i]);
567       }
568     }
569     i++;
570   }
571   g_strfreev (split);
572 
573   return GST_RTSP_OK;
574 
575 unsupported_transport:
576   {
577     g_strfreev (split);
578     return GST_RTSP_ERROR;
579   }
580 invalid_transport:
581   {
582     g_strfreev (transp);
583     g_strfreev (split);
584     return GST_RTSP_EINVAL;
585   }
586 }
587 
588 /**
589  * gst_rtsp_transport_as_text:
590  * @transport: a #GstRTSPTransport
591  *
592  * Convert @transport into a string that can be used to signal the transport in
593  * an RTSP SETUP response.
594  *
595  * Returns: a string describing the RTSP transport or %NULL when the transport
596  * is invalid.
597  */
598 gchar *
gst_rtsp_transport_as_text(GstRTSPTransport * transport)599 gst_rtsp_transport_as_text (GstRTSPTransport * transport)
600 {
601   GPtrArray *strs;
602   gchar *res;
603   const gchar *tmp;
604 
605   g_return_val_if_fail (transport != NULL, NULL);
606 
607   strs = g_ptr_array_new ();
608 
609   /* add the transport specifier */
610   if ((tmp = rtsp_transport_mode_as_text (transport)) == NULL)
611     goto invalid_transport;
612   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
613 
614   g_ptr_array_add (strs, g_strdup ("/"));
615 
616   if ((tmp = rtsp_transport_profile_as_text (transport)) == NULL)
617     goto invalid_transport;
618   g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
619 
620   if (transport->trans != GST_RTSP_TRANS_RTP ||
621       (transport->profile != GST_RTSP_PROFILE_AVP &&
622           transport->profile != GST_RTSP_PROFILE_SAVP &&
623           transport->profile != GST_RTSP_PROFILE_AVPF &&
624           transport->profile != GST_RTSP_PROFILE_SAVPF) ||
625       transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
626     g_ptr_array_add (strs, g_strdup ("/"));
627 
628     if ((tmp = rtsp_transport_ltrans_as_text (transport)) == NULL)
629       goto invalid_transport;
630 
631     g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
632   }
633 
634   /*
635    * the order of the following parameters is the same as the one specified in
636    * RFC 2326 to please some weird RTSP clients that require it
637    */
638 
639   /* add the unicast/multicast parameter */
640   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
641     g_ptr_array_add (strs, g_strdup (";multicast"));
642   else
643     g_ptr_array_add (strs, g_strdup (";unicast"));
644 
645   /* add the destination parameter */
646   if (transport->destination != NULL) {
647     g_ptr_array_add (strs, g_strdup (";destination="));
648     g_ptr_array_add (strs, g_strdup (transport->destination));
649   }
650 
651   /* add the source parameter */
652   if (transport->source != NULL) {
653     g_ptr_array_add (strs, g_strdup (";source="));
654     g_ptr_array_add (strs, g_strdup (transport->source));
655   }
656 
657   /* add the interleaved parameter */
658   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP &&
659       transport->interleaved.min >= 0) {
660     if (transport->interleaved.min < 256 && transport->interleaved.max < 256) {
661       g_ptr_array_add (strs, g_strdup (";interleaved="));
662       g_ptr_array_add (strs, range_as_text (&transport->interleaved));
663     } else
664       goto invalid_transport;
665   }
666 
667   /* add the append parameter */
668   if (transport->mode_record && transport->append)
669     g_ptr_array_add (strs, g_strdup (";append"));
670 
671   /* add the ttl parameter */
672   if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST &&
673       transport->ttl != 0) {
674     if (transport->ttl < 256) {
675       g_ptr_array_add (strs, g_strdup (";ttl="));
676       g_ptr_array_add (strs, g_strdup_printf ("%u", transport->ttl));
677     } else
678       goto invalid_transport;
679   }
680 
681   /* add the layers parameter */
682   if (transport->layers != 0) {
683     g_ptr_array_add (strs, g_strdup (";layers="));
684     g_ptr_array_add (strs, g_strdup_printf ("%u", transport->layers));
685   }
686 
687   /* add the port parameter */
688   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
689     if (transport->trans == GST_RTSP_TRANS_RTP && transport->port.min >= 0) {
690       if (transport->port.min < 65536 && transport->port.max < 65536) {
691         g_ptr_array_add (strs, g_strdup (";port="));
692         g_ptr_array_add (strs, range_as_text (&transport->port));
693       } else
694         goto invalid_transport;
695     }
696 
697     /* add the client_port parameter */
698     if (transport->trans == GST_RTSP_TRANS_RTP
699         && transport->client_port.min >= 0) {
700       if (transport->client_port.min < 65536
701           && transport->client_port.max < 65536) {
702         g_ptr_array_add (strs, g_strdup (";client_port="));
703         g_ptr_array_add (strs, range_as_text (&transport->client_port));
704       } else
705         goto invalid_transport;
706     }
707 
708     /* add the server_port parameter */
709     if (transport->trans == GST_RTSP_TRANS_RTP
710         && transport->server_port.min >= 0) {
711       if (transport->server_port.min < 65536
712           && transport->server_port.max < 65536) {
713         g_ptr_array_add (strs, g_strdup (";server_port="));
714         g_ptr_array_add (strs, range_as_text (&transport->server_port));
715       } else
716         goto invalid_transport;
717     }
718   }
719 
720   /* add the ssrc parameter */
721   if (transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST &&
722       transport->ssrc != 0) {
723     g_ptr_array_add (strs, g_strdup (";ssrc="));
724     g_ptr_array_add (strs, g_strdup_printf ("%08X", transport->ssrc));
725   }
726 
727   /* add the mode parameter */
728   if (transport->mode_play && transport->mode_record)
729     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY,RECORD\""));
730   else if (transport->mode_record)
731     g_ptr_array_add (strs, g_strdup (";mode=\"RECORD\""));
732   else if (transport->mode_play)
733     g_ptr_array_add (strs, g_strdup (";mode=\"PLAY\""));
734 
735   /* add a terminating NULL */
736   g_ptr_array_add (strs, NULL);
737 
738   res = g_strjoinv (NULL, (gchar **) strs->pdata);
739   g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
740 
741   return res;
742 
743 invalid_transport:
744   {
745     g_ptr_array_add (strs, NULL);
746     g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
747     return NULL;
748   }
749 }
750 
751 /**
752  * gst_rtsp_transport_free:
753  * @transport: a #GstRTSPTransport
754  *
755  * Free the memory used by @transport.
756  *
757  * Returns: #GST_RTSP_OK.
758  */
759 GstRTSPResult
gst_rtsp_transport_free(GstRTSPTransport * transport)760 gst_rtsp_transport_free (GstRTSPTransport * transport)
761 {
762   g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
763 
764   gst_rtsp_transport_init (transport);
765   g_free (transport);
766 
767   return GST_RTSP_OK;
768 }
769