2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45 * @short_description: handling RTSP urls
49 * Provides helper functions to handle RTSP urls.
53 * Last reviewed on 2007-07-25 (0.10.14)
59 #include "gstrtspurl.h"
62 register_rtsp_url_type (GType * id)
64 *id = g_boxed_type_register_static ("GstRTSPUrl",
65 (GBoxedCopyFunc) gst_rtsp_url_copy, (GBoxedFreeFunc) gst_rtsp_url_free);
69 gst_rtsp_url_get_type (void)
72 static GOnce once = G_ONCE_INIT;
74 g_once (&once, (GThreadFunc) register_rtsp_url_type, &id);
79 #define RTSP_PROTO "rtsp://"
80 #define RTSP_PROTO_LEN 7
81 #define RTSPU_PROTO "rtspu://"
82 #define RTSPU_PROTO_LEN 8
83 #define RTSPT_PROTO "rtspt://"
84 #define RTSPT_PROTO_LEN 8
85 #define RTSPH_PROTO "rtsph://"
86 #define RTSPH_PROTO_LEN 8
88 /* format is rtsp[u]://[user:passwd@]host[:port]/abspath[?query] */
92 * @urlstr: the url string to parse
93 * @url: location to hold the result.
95 * Parse the RTSP @urlstr into a newly allocated #GstRTSPUrl. Free after usage
96 * with gst_rtsp_url_free().
98 * Returns: a #GstRTSPResult.
101 gst_rtsp_url_parse (const gchar * urlstr, GstRTSPUrl ** url)
104 gchar *p, *delim, *at, *col;
106 g_return_val_if_fail (urlstr != NULL, GST_RTSP_EINVAL);
107 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
109 res = g_new0 (GstRTSPUrl, 1);
111 p = (gchar *) urlstr;
112 if (g_str_has_prefix (p, RTSP_PROTO)) {
114 GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP |
115 GST_RTSP_LOWER_TRANS_UDP_MCAST;
117 } else if (g_str_has_prefix (p, RTSPU_PROTO)) {
118 res->transports = GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST;
119 p += RTSPU_PROTO_LEN;
120 } else if (g_str_has_prefix (p, RTSPT_PROTO)) {
121 res->transports = GST_RTSP_LOWER_TRANS_TCP;
122 p += RTSPT_PROTO_LEN;
123 } else if (g_str_has_prefix (p, RTSPH_PROTO)) {
124 res->transports = GST_RTSP_LOWER_TRANS_HTTP | GST_RTSP_LOWER_TRANS_TCP;
125 p += RTSPH_PROTO_LEN;
129 delim = strpbrk (p, "/?");
130 at = strchr (p, '@');
132 if (at && delim && at > delim)
136 col = strchr (p, ':');
138 /* must have a ':' and it must be before the '@' */
139 if (col == NULL || col > at)
142 res->user = g_strndup (p, col - p);
144 res->passwd = g_strndup (col, at - col);
150 col = strchr (p, ':');
151 /* we have a ':' and a delimiter but the ':' is after the delimiter, it's
152 * not really part of the hostname */
153 if (col && delim && col >= delim)
157 res->host = g_strndup (p, col - p);
159 res->port = strtoul (p, (char **) &p, 10);
163 /* no port specified, set to 0. _get_port() will return the default port. */
166 res->host = g_strdup (p);
169 res->host = g_strndup (p, delim - p);
174 if (p && *p == '/') {
175 delim = strchr (p, '?');
177 res->abspath = g_strdup (p);
180 res->abspath = g_strndup (p, delim - p);
184 res->abspath = g_strdup ("/");
188 res->query = g_strdup (p + 1);
197 gst_rtsp_url_free (res);
198 return GST_RTSP_EINVAL;
204 * @url: a #GstRTSPUrl
206 * Make a copy of @url.
208 * Returns: a copy of @url. Free with gst_rtsp_url_free () after usage.
213 gst_rtsp_url_copy (const GstRTSPUrl * url)
217 g_return_val_if_fail (url != NULL, NULL);
219 res = g_new0 (GstRTSPUrl, 1);
221 res->transports = url->transports;
222 res->family = url->family;
223 res->user = g_strdup (url->user);
224 res->passwd = g_strdup (url->passwd);
225 res->host = g_strdup (url->host);
226 res->port = url->port;
227 res->abspath = g_strdup (url->abspath);
228 res->query = g_strdup (url->query);
235 * @url: a #GstRTSPUrl
237 * Free the memory used by @url.
240 gst_rtsp_url_free (GstRTSPUrl * url)
246 g_free (url->passwd);
248 g_free (url->abspath);
254 * gst_rtsp_url_set_port:
255 * @url: a #GstRTSPUrl
258 * Set the port number in @url to @port.
260 * Returns: #GST_RTSP_OK.
263 gst_rtsp_url_set_port (GstRTSPUrl * url, guint16 port)
265 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
273 * gst_rtsp_url_get_port:
274 * @url: a #GstRTSPUrl
275 * @port: location to hold the port
277 * Get the port number of @url.
279 * Returns: #GST_RTSP_OK.
282 gst_rtsp_url_get_port (const GstRTSPUrl * url, guint16 * port)
284 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL);
285 g_return_val_if_fail (port != NULL, GST_RTSP_EINVAL);
287 /* if a port was specified, use that else use the default port. */
291 *port = GST_RTSP_DEFAULT_PORT;
297 * gst_rtsp_url_get_request_uri:
298 * @url: a #GstRTSPUrl
300 * Get a newly allocated string describing the request URI for @url.
302 * Returns: a string with the request URI. g_free() after usage.
305 gst_rtsp_url_get_request_uri (const GstRTSPUrl * url)
309 g_return_val_if_fail (url != NULL, NULL);
311 if (url->port != 0) {
312 uri = g_strdup_printf ("rtsp://%s:%u%s%s%s", url->host, url->port,
313 url->abspath, url->query ? "?" : "", url->query ? url->query : "");
315 uri = g_strdup_printf ("rtsp://%s%s%s%s", url->host, url->abspath,
316 url->query ? "?" : "", url->query ? url->query : "");