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 #GstElement that can handle the buffers transported over @trans.
236 * It is possible that there are several managers available, use @option to
239 * @manager will contain an element name or #NULL when no manager is
240 * needed/available for @trans.
242 * Returns: #GST_RTSP_OK.
245 gst_rtsp_transport_get_manager (GstRTSPTransMode trans, const gchar ** manager,
250 g_return_val_if_fail (manager != NULL, GST_RTSP_EINVAL);
252 for (i = 0; transports[i].name; i++)
253 if (transports[i].mode == trans)
256 if (option < MAX_MANAGERS)
257 *manager = transports[i].manager[option];
265 parse_mode (GstRTSPTransport * transport, const gchar * str)
267 transport->mode_play = (strstr (str, "play") != NULL);
268 transport->mode_record = (strstr (str, "record") != NULL);
272 check_range (const gchar * str, gchar ** tmp, gint * range)
276 range_val = strtol (str, tmp, 10);
277 if (range_val >= G_MININT && range_val <= G_MAXINT) {
286 parse_range (const gchar * str, GstRTSPRange * range)
291 /* even though strtol() allows white space, plus and minus in front of
292 * the number, we do not allow it
294 if (g_ascii_isspace (*str) || *str == '+' || *str == '-')
297 minus = strstr (str, "-");
299 if (g_ascii_isspace (minus[1]) || minus[1] == '+' || minus[1] == '-')
302 if (!check_range (str, &tmp, &range->min) || str == tmp || tmp != minus)
305 if (!check_range (minus + 1, &tmp, &range->max) || (*tmp && *tmp != ';'))
308 if (!check_range (str, &tmp, &range->min) || str == tmp ||
309 (*tmp && *tmp != ';'))
326 range_as_text (const GstRTSPRange * range)
330 else if (range->max < 0)
331 return g_strdup_printf ("%d", range->min);
333 return g_strdup_printf ("%d-%d", range->min, range->max);
337 rtsp_transport_mode_as_text (const GstRTSPTransport * transport)
341 for (i = 0; transports[i].name; i++)
342 if (transports[i].mode == transport->trans)
343 return transports[i].name;
349 rtsp_transport_profile_as_text (const GstRTSPTransport * transport)
353 for (i = 0; profiles[i].name; i++)
354 if (profiles[i].profile == transport->profile)
355 return profiles[i].name;
361 rtsp_transport_ltrans_as_text (const GstRTSPTransport * transport)
365 /* need to special case GST_RTSP_LOWER_TRANS_UDP_MCAST */
366 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
369 for (i = 0; ltrans[i].name; i++)
370 if (ltrans[i].ltrans == transport->lower_transport)
371 return ltrans[i].name;
376 #define IS_VALID_PORT_RANGE(range) \
377 (range.min >= 0 && range.min < 65536 && range.max < 65536)
379 #define IS_VALID_INTERLEAVE_RANGE(range) \
380 (range.min >= 0 && range.min < 256 && range.max < 256)
383 * gst_rtsp_transport_parse:
384 * @str: a transport string
385 * @transport: a #GstRTSPTransport
387 * Parse the RTSP transport string @str into @transport.
389 * Returns: a #GstRTSPResult.
392 gst_rtsp_transport_parse (const gchar * str, GstRTSPTransport * transport)
394 gchar **split, *down, **transp = NULL;
395 guint transport_params = 0;
398 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
399 g_return_val_if_fail (str != NULL, GST_RTSP_EINVAL);
401 gst_rtsp_transport_init (transport);
403 /* case insensitive */
404 down = g_ascii_strdown (str, -1);
406 split = g_strsplit (down, ";", 0);
409 /* First field contains the transport/profile/lower_transport */
410 if (split[0] == NULL)
411 goto invalid_transport;
413 transp = g_strsplit (split[0], "/", 0);
415 if (transp[0] == NULL || transp[1] == NULL)
416 goto invalid_transport;
418 for (i = 0; transports[i].name; i++)
419 if (strcmp (transp[0], transports[i].name) == 0)
421 transport->trans = transports[i].mode;
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)
427 transport->profile = profiles[i].profile;
430 /* RDT has transport/lower_transport */
431 transport->profile = GST_RTSP_PROFILE_AVP;
435 if (transp[count] != NULL) {
436 for (i = 0; ltrans[i].name; i++)
437 if (strcmp (transp[count], ltrans[i].name) == 0)
439 transport->lower_transport = ltrans[i].ltrans;
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;
446 transport->lower_transport = GST_RTSP_LOWER_TRANS_UNKNOWN;
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;
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;
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;
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;
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);
517 /* unknown field... */
518 g_warning ("unknown transport field \"%s\"", split[i]);
526 unsupported_transport:
529 return GST_RTSP_ERROR;
535 return GST_RTSP_EINVAL;
540 * gst_rtsp_transport_as_text:
541 * @transport: a #GstRTSPTransport
543 * Convert @transport into a string that can be used to signal the transport in
544 * an RTSP SETUP response.
546 * Returns: a string describing the RTSP transport or #NULL when the transport
550 gst_rtsp_transport_as_text (GstRTSPTransport * transport)
556 g_return_val_if_fail (transport != NULL, NULL);
558 strs = g_ptr_array_new ();
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));
565 g_ptr_array_add (strs, g_strdup ("/"));
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));
571 if (transport->trans != GST_RTSP_TRANS_RTP ||
572 transport->profile != GST_RTSP_PROFILE_AVP ||
573 transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
574 g_ptr_array_add (strs, g_strdup ("/"));
576 if ((tmp = rtsp_transport_ltrans_as_text (transport)) == NULL)
577 goto invalid_transport;
579 g_ptr_array_add (strs, g_ascii_strup (tmp, -1));
583 * the order of the following parameters is the same as the one specified in
584 * RFC 2326 to please some weird RTSP clients that require it
587 /* add the unicast/multicast parameter */
588 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)
589 g_ptr_array_add (strs, g_strdup (";multicast"));
591 g_ptr_array_add (strs, g_strdup (";unicast"));
593 /* add the destination parameter */
594 if (transport->destination != NULL) {
595 g_ptr_array_add (strs, g_strdup (";destination="));
596 g_ptr_array_add (strs, g_strdup (transport->destination));
599 /* add the source parameter */
600 if (transport->source != NULL) {
601 g_ptr_array_add (strs, g_strdup (";source="));
602 g_ptr_array_add (strs, g_strdup (transport->source));
605 /* add the interleaved parameter */
606 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP &&
607 transport->interleaved.min >= 0) {
608 if (transport->interleaved.min < 256 && transport->interleaved.max < 256) {
609 g_ptr_array_add (strs, g_strdup (";interleaved="));
610 g_ptr_array_add (strs, range_as_text (&transport->interleaved));
612 goto invalid_transport;
615 /* add the append parameter */
616 if (transport->mode_record && transport->append)
617 g_ptr_array_add (strs, g_strdup (";append"));
619 /* add the ttl parameter */
620 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST &&
621 transport->ttl != 0) {
622 if (transport->ttl < 256) {
623 g_ptr_array_add (strs, g_strdup (";ttl="));
624 g_ptr_array_add (strs, g_strdup_printf ("%u", transport->ttl));
626 goto invalid_transport;
629 /* add the layers parameter */
630 if (transport->layers != 0) {
631 g_ptr_array_add (strs, g_strdup (";layers="));
632 g_ptr_array_add (strs, g_strdup_printf ("%u", transport->layers));
635 /* add the port parameter */
636 if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
637 if (transport->trans == GST_RTSP_TRANS_RTP && transport->port.min >= 0) {
638 if (transport->port.min < 65536 && transport->port.max < 65536) {
639 g_ptr_array_add (strs, g_strdup (";port="));
640 g_ptr_array_add (strs, range_as_text (&transport->port));
642 goto invalid_transport;
645 /* add the client_port parameter */
646 if (transport->trans == GST_RTSP_TRANS_RTP
647 && transport->client_port.min >= 0) {
648 if (transport->client_port.min < 65536
649 && transport->client_port.max < 65536) {
650 g_ptr_array_add (strs, g_strdup (";client_port="));
651 g_ptr_array_add (strs, range_as_text (&transport->client_port));
653 goto invalid_transport;
656 /* add the server_port parameter */
657 if (transport->trans == GST_RTSP_TRANS_RTP
658 && transport->server_port.min >= 0) {
659 if (transport->server_port.min < 65536
660 && transport->server_port.max < 65536) {
661 g_ptr_array_add (strs, g_strdup (";server_port="));
662 g_ptr_array_add (strs, range_as_text (&transport->server_port));
664 goto invalid_transport;
668 /* add the ssrc parameter */
669 if (transport->lower_transport != GST_RTSP_LOWER_TRANS_UDP_MCAST &&
670 transport->ssrc != 0) {
671 g_ptr_array_add (strs, g_strdup (";ssrc="));
672 g_ptr_array_add (strs, g_strdup_printf ("%08X", transport->ssrc));
675 /* add the mode parameter */
676 if (transport->mode_play && transport->mode_record)
677 g_ptr_array_add (strs, g_strdup (";mode=\"PLAY,RECORD\""));
678 else if (transport->mode_record)
679 g_ptr_array_add (strs, g_strdup (";mode=\"RECORD\""));
680 else if (transport->mode_play)
681 g_ptr_array_add (strs, g_strdup (";mode=\"PLAY\""));
683 /* add a terminating NULL */
684 g_ptr_array_add (strs, NULL);
686 res = g_strjoinv (NULL, (gchar **) strs->pdata);
687 g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
693 g_ptr_array_add (strs, NULL);
694 g_strfreev ((gchar **) g_ptr_array_free (strs, FALSE));
700 * gst_rtsp_transport_free:
701 * @transport: a #GstRTSPTransport
703 * Free the memory used by @transport.
705 * Returns: #GST_RTSP_OK.
708 gst_rtsp_transport_free (GstRTSPTransport * transport)
710 g_return_val_if_fail (transport != NULL, GST_RTSP_EINVAL);
712 gst_rtsp_transport_init (transport);