|
1 /* GStreamer |
|
2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com> |
|
3 * |
|
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. |
|
8 * |
|
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. |
|
13 * |
|
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. |
|
18 */ |
|
19 /* |
|
20 * Unless otherwise indicated, Source Code is licensed under MIT license. |
|
21 * See further explanation attached in License Statement (distributed in the file |
|
22 * LICENSE). |
|
23 * |
|
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: |
|
30 * |
|
31 * The above copyright notice and this permission notice shall be included in all |
|
32 * copies or substantial portions of the Software. |
|
33 * |
|
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 |
|
40 * SOFTWARE. |
|
41 */ |
|
42 |
|
43 /** |
|
44 * SECTION:gstrtspurl |
|
45 * @short_description: handling RTSP urls |
|
46 * |
|
47 * <refsect2> |
|
48 * <para> |
|
49 * Provides helper functions to handle RTSP urls. |
|
50 * </para> |
|
51 * </refsect2> |
|
52 * |
|
53 * Last reviewed on 2007-07-25 (0.10.14) |
|
54 */ |
|
55 |
|
56 #include <stdlib.h> |
|
57 #include <string.h> |
|
58 |
|
59 #include "gstrtspurl.h" |
|
60 |
|
61 #define RTSP_PROTO "rtsp://" |
|
62 #define RTSP_PROTO_LEN 7 |
|
63 #define RTSPU_PROTO "rtspu://" |
|
64 #define RTSPU_PROTO_LEN 8 |
|
65 #define RTSPT_PROTO "rtspt://" |
|
66 #define RTSPT_PROTO_LEN 8 |
|
67 |
|
68 /* format is rtsp[u]://[user:passwd@]host[:port]/abspath[?query] */ |
|
69 |
|
70 /** |
|
71 * gst_rtsp_url_parse: |
|
72 * @urlstr: the url string to parse |
|
73 * @url: location to hold the result. |
|
74 * |
|
75 * Parse the RTSP @urlstr into a newly allocated #GstRTSPUrl. Free after usage |
|
76 * with gst_rtsp_url_free(). |
|
77 * |
|
78 * Returns: a #GstRTSPResult. |
|
79 */ |
|
80 GstRTSPResult |
|
81 gst_rtsp_url_parse (const gchar * urlstr, GstRTSPUrl ** url) |
|
82 { |
|
83 GstRTSPUrl *res; |
|
84 gchar *p, *delim, *at, *col; |
|
85 |
|
86 g_return_val_if_fail (urlstr != NULL, GST_RTSP_EINVAL); |
|
87 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL); |
|
88 |
|
89 res = g_new0 (GstRTSPUrl, 1); |
|
90 |
|
91 p = (gchar *) urlstr; |
|
92 if (g_str_has_prefix (p, RTSP_PROTO)) { |
|
93 res->transports = |
|
94 GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_UDP | |
|
95 GST_RTSP_LOWER_TRANS_UDP_MCAST; |
|
96 p += RTSP_PROTO_LEN; |
|
97 } else if (g_str_has_prefix (p, RTSPU_PROTO)) { |
|
98 res->transports = GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST; |
|
99 p += RTSPU_PROTO_LEN; |
|
100 } else if (g_str_has_prefix (p, RTSPT_PROTO)) { |
|
101 res->transports = GST_RTSP_LOWER_TRANS_TCP; |
|
102 p += RTSPT_PROTO_LEN; |
|
103 } else |
|
104 goto invalid; |
|
105 |
|
106 delim = strpbrk (p, "/?"); |
|
107 at = strchr (p, '@'); |
|
108 |
|
109 if (at && delim && at > delim) |
|
110 at = NULL; |
|
111 |
|
112 if (at) { |
|
113 col = strchr (p, ':'); |
|
114 |
|
115 /* must have a ':' and it must be before the '@' */ |
|
116 if (col == NULL || col > at) |
|
117 goto invalid; |
|
118 |
|
119 res->user = g_strndup (p, col - p); |
|
120 col++; |
|
121 res->passwd = g_strndup (col, at - col); |
|
122 |
|
123 /* move to host */ |
|
124 p = at + 1; |
|
125 } |
|
126 |
|
127 col = strchr (p, ':'); |
|
128 /* we have a ':' and a delimiter but the ':' is after the delimiter, it's |
|
129 * not really part of the hostname */ |
|
130 if (col && delim && col >= delim) |
|
131 col = NULL; |
|
132 |
|
133 if (col) { |
|
134 res->host = g_strndup (p, col - p); |
|
135 p = col + 1; |
|
136 res->port = strtoul (p, (char **) &p, 10); |
|
137 if (delim) |
|
138 p = delim; |
|
139 } else { |
|
140 /* no port specified, set to 0. _get_port() will return the default port. */ |
|
141 res->port = 0; |
|
142 if (!delim) { |
|
143 res->host = g_strdup (p); |
|
144 p = NULL; |
|
145 } else { |
|
146 res->host = g_strndup (p, delim - p); |
|
147 p = delim; |
|
148 } |
|
149 } |
|
150 |
|
151 if (p && *p == '/') { |
|
152 delim = strchr (p, '?'); |
|
153 if (!delim) { |
|
154 res->abspath = g_strdup (p); |
|
155 p = NULL; |
|
156 } else { |
|
157 res->abspath = g_strndup (p, delim - p); |
|
158 p = delim; |
|
159 } |
|
160 } else { |
|
161 res->abspath = g_strdup ("/"); |
|
162 } |
|
163 |
|
164 if (p && *p == '?') |
|
165 res->query = g_strdup (p + 1); |
|
166 |
|
167 *url = res; |
|
168 |
|
169 return GST_RTSP_OK; |
|
170 |
|
171 /* ERRORS */ |
|
172 invalid: |
|
173 { |
|
174 gst_rtsp_url_free (res); |
|
175 return GST_RTSP_EINVAL; |
|
176 } |
|
177 } |
|
178 |
|
179 /** |
|
180 * gst_rtsp_url_free: |
|
181 * @url: a #GstRTSPUrl |
|
182 * |
|
183 * Free the memory used by @url. |
|
184 */ |
|
185 void |
|
186 gst_rtsp_url_free (GstRTSPUrl * url) |
|
187 { |
|
188 if (url == NULL) |
|
189 return; |
|
190 |
|
191 g_free (url->user); |
|
192 g_free (url->passwd); |
|
193 g_free (url->host); |
|
194 g_free (url->abspath); |
|
195 g_free (url->query); |
|
196 g_free (url); |
|
197 } |
|
198 |
|
199 /** |
|
200 * gst_rtsp_url_set_port: |
|
201 * @url: a #GstRTSPUrl |
|
202 * @port: the port |
|
203 * |
|
204 * Set the port number in @url to @port. |
|
205 * |
|
206 * Returns: #GST_RTSP_OK. |
|
207 */ |
|
208 GstRTSPResult |
|
209 gst_rtsp_url_set_port (GstRTSPUrl * url, guint16 port) |
|
210 { |
|
211 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL); |
|
212 |
|
213 url->port = port; |
|
214 |
|
215 return GST_RTSP_OK; |
|
216 } |
|
217 |
|
218 /** |
|
219 * gst_rtsp_url_get_port: |
|
220 * @url: a #GstRTSPUrl |
|
221 * @port: location to hold the port |
|
222 * |
|
223 * Get the port number of @url. |
|
224 * |
|
225 * Returns: #GST_RTSP_OK. |
|
226 */ |
|
227 GstRTSPResult |
|
228 gst_rtsp_url_get_port (GstRTSPUrl * url, guint16 * port) |
|
229 { |
|
230 g_return_val_if_fail (url != NULL, GST_RTSP_EINVAL); |
|
231 g_return_val_if_fail (port != NULL, GST_RTSP_EINVAL); |
|
232 |
|
233 /* if a port was specified, use that else use the default port. */ |
|
234 if (url->port != 0) |
|
235 *port = url->port; |
|
236 else |
|
237 *port = GST_RTSP_DEFAULT_PORT; |
|
238 |
|
239 return GST_RTSP_OK; |
|
240 } |
|
241 |
|
242 /** |
|
243 * gst_rtsp_url_get_request_uri: |
|
244 * @url: a #GstRTSPUrl |
|
245 * |
|
246 * Get a newly allocated string describing the request URI for @url. |
|
247 * |
|
248 * Returns: a string with the request URI. g_free() after usage. |
|
249 */ |
|
250 gchar * |
|
251 gst_rtsp_url_get_request_uri (GstRTSPUrl * url) |
|
252 { |
|
253 gchar *uri; |
|
254 |
|
255 g_return_val_if_fail (url != NULL, NULL); |
|
256 |
|
257 if (url->port != 0) { |
|
258 uri = g_strdup_printf ("rtsp://%s:%u%s%s%s", url->host, url->port, |
|
259 url->abspath, url->query ? "?" : "", url->query ? url->query : ""); |
|
260 } else { |
|
261 uri = g_strdup_printf ("rtsp://%s%s%s%s", url->host, url->abspath, |
|
262 url->query ? "?" : "", url->query ? url->query : ""); |
|
263 } |
|
264 |
|
265 return uri; |
|
266 } |