2 * Copyright (C) <2005,2006,2007> Wim Taymans <wim@fluendo.com>
3 * <2007> Peter Kjellerstedt <pkj at axis com>
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.
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.
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.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
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:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
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
45 * SECTION:gstrtsptransport
46 * @short_description: dealing with RTSP transports
48 * Provides helper functions to deal with RTSP transport strings.
50 * Last reviewed on 2007-07-25 (0.10.14)
56 #include "gstrtsptransport.h"
58 #define MAX_MANAGERS 2
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;
79 const GstRTSPTransMode mode;
80 const gchar *gst_mime;
81 const gchar *manager[MAX_MANAGERS];
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}}
94 const GstRTSPProfile profile;
97 static const RTSPProfileMap profiles[] = {
98 {"avp", GST_RTSP_PROFILE_AVP},
99 {"savp", GST_RTSP_PROFILE_SAVP},
100 {NULL, GST_RTSP_PROFILE_UNKNOWN}
106 const GstRTSPLowerTrans ltrans;
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}
118 gst_rtsp_lower_trans_get_type (void)
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",
125 {GST_RTSP_LOWER_TRANS_TCP, "GST_RTSP_LOWER_TRANS_TCP", "tcp"},
126 {GST_RTSP_LOWER_TRANS_HTTP, "GST_RTSP_LOWER_TRANS_HTTP", "http"},
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);
135 return (GType) rtsp_lower_trans_type;
138 #define RTSP_TRANSPORT_PARAMETER_IS_UNIQUE(param) \
140 if ((transport_params & (param)) != 0) \
141 goto invalid_transport; \
142 transport_params |= (param); \
146 * gst_rtsp_transport_new:
147 * @transport: location to hold the new #GstRTSPTransport
149 * Allocate a new initialized #GstRTSPTransport. Use gst_rtsp_transport_free()
152 * Returns: a #GstRTSPResult.
155 gst_rtsp_transport_new (GstRTSPTransport ** transport)
157 GstRTSPTransport *trans;
159 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
161 trans = g_new0 (GstRTSPTransport, 1);
165 return gst_rtsp_transport_init (trans);
169 * gst_rtsp_transport_init:
170 * @transport: a #GstRTSPTransport
172 * Initialize @transport so that it can be used.
174 * Returns: #GST_RTSP_OK.
177 gst_rtsp_transport_init (GstRTSPTransport * transport)
179 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
181 g_free (transport->destination);
182 g_free (transport->source);
184 memset (transport, 0, sizeof (GstRTSPTransport));
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;
204 * gst_rtsp_transport_get_mime:
205 * @trans: a #GstRTSPTransMode
206 * @mime: location to hold the result
208 * Get the mime type of the transport mode @trans. This mime type is typically
209 * used to generate #GstCaps on buffers.
211 * Returns: #GST_RTSP_OK.
214 gst_rtsp_transport_get_mime (GstRTSPTransMode trans, const gchar ** mime)
218 g_return_val_if_fail (mime != NULL, GST_RTSP_EINVAL);
220 for (i = 0; transports[i].name; i++)
221 if (transports[i].mode == trans)
223 *mime = transports[i].gst_mime;
229 * gst_rtsp_transport_get_manager:
230 * @trans: a #GstRTSPTransMode
231 * @manager: location to hold the result
232 * @option: option index.
234 * Get the #GStreamer element that can handle the buffers transported over
237 * It is possible that there are several managers available, use @option to
240 * @manager will contain an element name or #NULL when no manager is
241 * needed/available for @trans.
243 * Returns: #GST_RTSP_OK.
246 gst_rtsp_transport_get_manager (GstRTSPTransMode trans, const gchar ** manager,
251 g_return_val_if_fail (manager != NULL, GST_RTSP_EINVAL);
253 for (i = 0; transports[i].name; i++)
254 if (transports[i].mode == trans)
257 if (option < MAX_MANAGERS)
258 *manager = transports[i].manager[option];
266 parse_mode (GstRTSPTransport * transport, const gchar * str)
268 transport->mode_play = (strstr (str, "play") != NULL);
269 transport->mode_record = (strstr (str, "record") != NULL);
273 check_range (const gchar * str, gchar ** tmp, gint * range)
277 range_val = strtol (str, tmp, 10);
278 if (range_val >= G_MININT && range_val <= G_MAXINT) {
287 parse_range (const gchar * str, GstRTSPRange * range)
292 /* even though strtol() allows white space, plus and minus in front of
293 * the number, we do not allow it
295 if (g_ascii_isspace (*str) || *str == '+' || *str == '-')
298 minus = strstr (str, "-");
300 if (g_ascii_isspace (minus[1]) || minus[1] == '+' || minus[1] == '-')
303 if (!check_range (str, &tmp, &range->min) || str == tmp || tmp != minus)
306 if (!check_range (minus + 1, &tmp, &range->max) || (*tmp && *tmp != ';'))
309 if (!check_range (str, &tmp, &range->min) || str == tmp ||
310 (*tmp && *tmp != ';'))
327 range_as_text (const GstRTSPRange * range)
331 else if (range->max < 0)
332 return g_strdup_printf ("%d", range->min);
334 return g_strdup_printf ("%d-%d", range->min, range->max);
338 rtsp_transport_mode_as_text (const GstRTSPTransport * transport)
342 for (i = 0; transports[i].name; i++)
343 if (transports[i].mode == transport->trans)
344 return transports[i].name;
350 rtsp_transport_profile_as_text (const GstRTSPTransport * transport)
354 for (i = 0; profiles[i].name; i++)
355 if (profiles[i].profile == transport->profile)
356 return profiles[i].name;
362 rtsp_transport_ltrans_as_text (const GstRTSPTransport * transport)
366 /* need to special case GST_RTSP_LOWER_TRANS_UDP_MCAST */
367 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
370 for (i = 0; ltrans[i].name; i++)
371 if (ltrans[i].ltrans == transport->lower_transport)
372 return ltrans[i].name;
377 #define IS_VALID_PORT_RANGE(range) \
378 (range.min >= 0 && range.min < 65536 && range.max < 65536)
380 #define IS_VALID_INTERLEAVE_RANGE(range) \
381 (range.min >= 0 && range.min < 256 && range.max < 256)
384 * gst_rtsp_transport_parse:
385 * @str: a transport string
386 * @transport: a #GstRTSPTransport
388 * Parse the RTSP transport string @str into @transport.
390 * Returns: a #GstRTSPResult.
393 gst_rtsp_transport_parse (const gchar * str, GstRTSPTransport * transport)
395 gchar **split, *down, **transp = NULL;
396 guint transport_params = 0;
399 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
400 g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
402 gst_rtsp_transport_init (transport);
404 /* case insensitive */
405 down = g_ascii_strdown (str, -1);
407 split = g_strsplit (down, ";", 0);
410 /* First field contains the transport/profile/lower_transport */
411 if (split[0] == NULL)
412 goto invalid_transport;
414 transp = g_strsplit (split[0], "/", 0);
416 if (transp[0] == NULL || transp[1] == NULL)
417 goto invalid_transport;
419 for (i = 0; transports[i].name; i++)
420 if (strcmp (transp[0], transports[i].name) == 0)
422 transport->trans = transports[i].mode;
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)
428 transport->profile = profiles[i].profile;
431 /* RDT has transport/lower_transport */
432 transport->profile = GST_RTSP_PROFILE_AVP;
436 if (transp[count] != NULL) {
437 for (i = 0; ltrans[i].name; i++)
438 if (strcmp (transp[count], ltrans[i].name) == 0)
440 transport->lower_transport = ltrans[i].ltrans;
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;
447 transport->lower_transport = GST_RTSP_LOWER_TRANS_UNKNOWN;
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;
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;
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;
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;
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);
518 /* unknown field... */
519 g_warning ("unknown transport field \"%s\"", split[i]);
527 unsupported_transport:
530 return GST_RTSP_ERROR;
536 return GST_RTSP_EINVAL;
541 * gst_rtsp_transport_as_text:
542 * @transport: a #GstRTSPTransport
544 * Convert @transport into a string that can be used to signal the transport in
545 * an RTSP SETUP response.
547 * Returns: a string describing the RTSP transport or #NULL when the transport
551 gst_rtsp_transport_as_text (GstRTSPTransport * transport)
557 g_return_val_if_fail (transport != NULL, NULL);
559 strs = g_ptr_array_new ();
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));
566 g_ptr_array_add (strs, g_strdup ("/"));
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));
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 ("/"));
577 if ((tmp = rtsp_transport_ltrans_as_text (transport)) == NULL)
578 goto invalid_transport;
580 g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
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
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"));
592 g_ptr_array_add (strs, g_strdup (";unicast"));
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));
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));
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));
613 goto invalid_transport;
616 /* add the append parameter */
617 if (transport->mode_record && transport->append)
618 g_ptr_array_add (strs, g_strdup (";append"));
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));
627 goto invalid_transport;
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));
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));
643 goto invalid_transport;
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));
654 goto invalid_transport;
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));
665 goto invalid_transport;
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));
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\""));
684 /* add a terminating NULL */
685 g_ptr_array_add (strs, NULL);
687 res = g_strjoinv (NULL, (gchar **) strs->pdata);
688 g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
694 g_ptr_array_add (strs, NULL);
695 g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
701 * gst_rtsp_transport_free:
702 * @transport: a #GstRTSPTransport
704 * Free the memory used by @transport.
706 * Returns: #GST_RTSP_OK.
709 gst_rtsp_transport_free (GstRTSPTransport * transport)
711 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
713 gst_rtsp_transport_init (transport);