Mercurial > audlegacy
annotate Plugins/Input/mpg123/http.c @ 1594:44f67f556b60 trunk
[svn] - precision in title format is regarded as character count, not byte count.
author | yaz |
---|---|
date | Thu, 24 Aug 2006 23:08:22 -0700 |
parents | 705d4c089fce |
children |
rev | line source |
---|---|
61 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
1459 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
61 | 20 */ |
21 | |
22 #include <glib.h> | |
23 #include <glib/gi18n.h> | |
24 #include <gtk/gtk.h> | |
25 #include <stdlib.h> | |
26 #include <string.h> | |
27 #include <stdio.h> | |
28 | |
29 #include <fcntl.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 #include <sys/types.h> | |
33 #include <sys/socket.h> | |
34 #include <sys/time.h> | |
35 #include <netinet/in.h> | |
36 #include <arpa/inet.h> | |
37 #include <netdb.h> | |
38 | |
39 #include <libaudacious/util.h> | |
40 | |
41 #include "mpg123.h" | |
42 | |
43 | |
44 #define min(x,y) ((x)<(y)?(x):(y)) | |
45 #define min3(x,y,z) (min(x,y)<(z)?min(x,y):(z)) | |
46 #define min4(x,y,z,w) (min3(x,y,z)<(w)?min3(x,y,z):(w)) | |
47 | |
48 static gchar *icy_name = NULL; | |
49 static gint icy_metaint = 0; | |
50 | |
51 #undef DEBUG_UDP | |
52 | |
53 /* Static udp channel functions */ | |
54 static gint udp_establish_listener(gint * sock); | |
55 static gint udp_check_for_data(gint sock); | |
56 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
57 extern gint mpgdec_bitrate, mpgdec_frequency, mpgdec_stereo; |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
58 extern gboolean mpgdec_stereo; |
61 | 59 |
60 static gboolean prebuffering, going, eof = FALSE; | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
61 static gint sock; |
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
62 static gsize rd_index, wr_index, buffer_length, prebuffer_length; |
61 | 63 static guint64 buffer_read = 0; |
64 static gchar *buffer; | |
65 static GThread *thread; | |
66 static GtkWidget *error_dialog = NULL; | |
67 | |
68 static VFSFile *output_file = NULL; | |
69 | |
70 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) | |
71 | |
72 /* Encode the string S of length LENGTH to base64 format and place it | |
73 to STORE. STORE will be 0-terminated, and must point to a writable | |
74 buffer of at least 1+BASE64_LENGTH(length) bytes. */ | |
75 static void | |
76 base64_encode(const gchar * s, gchar * store, gint length) | |
77 { | |
78 /* Conversion table. */ | |
79 static gchar tbl[64] = { | |
80 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | |
81 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
82 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | |
83 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | |
84 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
85 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | |
86 'w', 'x', 'y', 'z', '0', '1', '2', '3', | |
87 '4', '5', '6', '7', '8', '9', '+', '/' | |
88 }; | |
89 gint i; | |
90 guchar *p = (guchar *) store; | |
91 | |
92 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | |
93 for (i = 0; i < length; i += 3) { | |
94 *p++ = tbl[s[0] >> 2]; | |
95 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; | |
96 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; | |
97 *p++ = tbl[s[2] & 0x3f]; | |
98 s += 3; | |
99 } | |
100 /* Pad the result if necessary... */ | |
101 if (i == length + 1) | |
102 *(p - 1) = '='; | |
103 else if (i == length + 2) | |
104 *(p - 1) = *(p - 2) = '='; | |
105 /* ...and zero-terminate it. */ | |
106 *p = '\0'; | |
107 } | |
108 | |
109 /* Create the authentication header contents for the `Basic' scheme. | |
110 This is done by encoding the string `USER:PASS' in base64 and | |
111 prepending `HEADER: Basic ' to it. */ | |
112 static gchar * | |
113 basic_authentication_encode(const gchar * user, | |
114 const gchar * passwd, const gchar * header) | |
115 { | |
116 gchar *t1, *t2, *res; | |
117 gint len1 = strlen(user) + 1 + strlen(passwd); | |
118 gint len2 = BASE64_LENGTH(len1); | |
119 | |
120 t1 = g_strdup_printf("%s:%s", user, passwd); | |
121 t2 = g_malloc0(len2 + 1); | |
122 base64_encode(t1, t2, len1); | |
123 res = g_strdup_printf("%s: Basic %s\r\n", header, t2); | |
124 g_free(t2); | |
125 g_free(t1); | |
126 | |
127 return res; | |
128 } | |
129 | |
354
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
198
diff
changeset
|
130 void |
61 | 131 parse_url(const gchar * url, gchar ** user, gchar ** pass, |
132 gchar ** host, gint * port, gchar ** filename) | |
133 { | |
134 gchar *h, *p, *pt, *f, *temp, *ptr; | |
135 | |
136 temp = g_strdup(url); | |
137 ptr = temp; | |
138 | |
139 if (!strncasecmp("http://", ptr, 7)) | |
140 ptr += 7; | |
141 h = strchr(ptr, '@'); | |
142 f = strchr(ptr, '/'); | |
143 if (h != NULL && (!f || h < f)) { | |
144 *h = '\0'; | |
145 p = strchr(ptr, ':'); | |
146 if (p != NULL && p < h) { | |
147 *p = '\0'; | |
148 p++; | |
149 *pass = g_strdup(p); | |
150 } | |
151 else | |
152 *pass = NULL; | |
153 *user = g_strdup(ptr); | |
154 h++; | |
155 ptr = h; | |
156 } | |
157 else { | |
158 *user = NULL; | |
159 *pass = NULL; | |
160 h = ptr; | |
161 } | |
162 pt = strchr(ptr, ':'); | |
163 if (pt != NULL && (f == NULL || pt < f)) { | |
164 *pt = '\0'; | |
165 *port = atoi(pt + 1); | |
166 } | |
167 else { | |
168 if (f) | |
169 *f = '\0'; | |
170 *port = 80; | |
171 } | |
172 *host = g_strdup(h); | |
173 | |
174 if (f) | |
175 *filename = g_strdup(f + 1); | |
176 else | |
177 *filename = NULL; | |
178 g_free(temp); | |
179 } | |
180 | |
181 void | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
182 mpgdec_http_close(void) |
61 | 183 { |
184 going = FALSE; | |
185 | |
186 g_thread_join(thread); | |
187 g_free(icy_name); | |
188 icy_name = NULL; | |
189 } | |
190 | |
191 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
192 static gsize |
61 | 193 http_used(void) |
194 { | |
195 if (wr_index >= rd_index) | |
196 return wr_index - rd_index; | |
197 return buffer_length - (rd_index - wr_index); | |
198 } | |
199 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
200 static gsize |
61 | 201 http_free(void) |
202 { | |
203 if (rd_index > wr_index) | |
204 return (rd_index - wr_index) - 1; | |
205 return (buffer_length - (wr_index - rd_index)) - 1; | |
206 } | |
207 | |
208 static void | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
209 http_wait_for_data(gsize bytes) |
61 | 210 { |
211 while ((prebuffering || http_used() < bytes) && !eof && going | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
212 && mpgdec_info->going) |
1097 | 213 g_usleep(10000); |
61 | 214 } |
215 | |
216 static void | |
217 show_error_message(gchar * error) | |
218 { | |
219 if (!error_dialog) { | |
220 GDK_THREADS_ENTER(); | |
221 error_dialog = xmms_show_message(_("Error"), error, _("Ok"), FALSE, | |
222 NULL, NULL); | |
223 g_signal_connect(G_OBJECT(error_dialog), | |
224 "destroy", | |
225 G_CALLBACK(gtk_widget_destroyed), &error_dialog); | |
226 GDK_THREADS_LEAVE(); | |
227 } | |
228 } | |
229 | |
230 int | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
231 mpgdec_http_read(gpointer data, gsize length) |
61 | 232 { |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
233 gsize len, cnt, off = 0, meta_len, meta_off = 0, i; |
61 | 234 gchar *meta_data, **tags; |
235 | |
236 http_wait_for_data(length); | |
237 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
238 if (!going && !mpgdec_info->going) |
61 | 239 return 0; |
240 len = min(http_used(), length); | |
241 | |
242 while (len && http_used()) { | |
243 if ((icy_metaint > 0) && (buffer_read % icy_metaint) == 0 && | |
244 (buffer_read > 0)) { | |
245 meta_len = *((guchar *) buffer + rd_index) * 16; | |
246 rd_index = (rd_index + 1) % buffer_length; | |
247 if (meta_len > 0) { | |
248 http_wait_for_data(meta_len); | |
249 meta_data = g_malloc0(meta_len); | |
250 if (http_used() >= meta_len) { | |
251 while (meta_len) { | |
252 cnt = min(meta_len, buffer_length - rd_index); | |
253 memcpy(meta_data + meta_off, buffer + rd_index, cnt); | |
254 rd_index = (rd_index + cnt) % buffer_length; | |
255 meta_len -= cnt; | |
256 meta_off += cnt; | |
257 } | |
258 tags = g_strsplit(meta_data, "';", 0); | |
259 | |
260 for (i = 0; tags[i]; i++) { | |
261 if (!strncasecmp(tags[i], "StreamTitle=", 12)) { | |
262 gchar *temp = tags[i] + 13; | |
263 gchar *title = | |
264 g_strdup_printf("%s (%s)", temp, icy_name); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
265 mpgdec_ip.set_info(title, -1, |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
266 mpgdec_bitrate * 1000, |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
267 mpgdec_frequency, |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
268 mpgdec_stereo); |
61 | 269 g_free(title); |
270 } | |
271 | |
272 } | |
273 g_strfreev(tags); | |
274 | |
275 } | |
276 g_free(meta_data); | |
277 } | |
278 if (!http_used()) | |
279 http_wait_for_data(length - off); | |
280 cnt = min3(len, buffer_length - rd_index, http_used()); | |
281 } | |
282 else if (icy_metaint > 0) | |
283 cnt = | |
284 min4(len, buffer_length - rd_index, http_used(), | |
285 icy_metaint - (buffer_read % icy_metaint)); | |
286 else | |
287 cnt = min3(len, buffer_length - rd_index, http_used()); | |
288 if (output_file) | |
289 vfs_fwrite(buffer + rd_index, 1, cnt, output_file); | |
290 | |
291 memcpy((gchar *) data + off, buffer + rd_index, cnt); | |
292 rd_index = (rd_index + cnt) % buffer_length; | |
293 buffer_read += cnt; | |
294 len -= cnt; | |
295 off += cnt; | |
296 } | |
297 return off; | |
298 } | |
299 | |
300 static gboolean | |
301 http_check_for_data(void) | |
302 { | |
303 fd_set set; | |
304 struct timeval tv; | |
305 gint ret; | |
306 | |
307 tv.tv_sec = 0; | |
308 tv.tv_usec = 20000; | |
309 FD_ZERO(&set); | |
310 FD_SET(sock, &set); | |
311 ret = select(sock + 1, &set, NULL, NULL, &tv); | |
312 if (ret > 0) | |
313 return TRUE; | |
314 return FALSE; | |
315 } | |
316 | |
317 gint | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
318 mpgdec_http_read_line(gchar * buf, gint size) |
61 | 319 { |
320 gint i = 0; | |
321 | |
322 while (going && i < size - 1) { | |
323 if (http_check_for_data()) { | |
324 if (read(sock, buf + i, 1) <= 0) | |
325 return -1; | |
326 if (buf[i] == '\n') | |
327 break; | |
328 if (buf[i] != '\r') | |
329 i++; | |
330 } | |
331 } | |
332 if (!going) | |
333 return -1; | |
334 buf[i] = '\0'; | |
335 return i; | |
336 } | |
337 | |
338 static gpointer | |
339 http_buffer_loop(gpointer arg) | |
340 { | |
341 gchar line[1024], *user, *pass, *host, *filename, | |
342 *status, *url, *temp, *file; | |
343 gchar *chost; | |
377 | 344 gint cnt, written, error, port, cport; |
345 guint err_len; | |
61 | 346 gboolean redirect; |
347 gint udp_sock = 0; | |
348 fd_set set; | |
978 | 349 #ifdef USE_IPV6 |
350 struct addrinfo hints, *res, *res0; | |
351 char service[6]; | |
352 #else | |
61 | 353 struct hostent *hp; |
354 struct sockaddr_in address; | |
978 | 355 #endif |
61 | 356 struct timeval tv; |
357 | |
358 url = (gchar *) arg; | |
359 do { | |
360 redirect = FALSE; | |
361 | |
362 g_strstrip(url); | |
363 | |
364 parse_url(url, &user, &pass, &host, &port, &filename); | |
365 | |
366 if ((!filename || !*filename) && url[strlen(url) - 1] != '/') | |
367 temp = g_strconcat(url, "/", NULL); | |
368 else | |
369 temp = g_strdup(url); | |
370 g_free(url); | |
371 url = temp; | |
372 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
373 chost = mpgdec_cfg.use_proxy ? mpgdec_cfg.proxy_host : host; |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
374 cport = mpgdec_cfg.use_proxy ? mpgdec_cfg.proxy_port : port; |
61 | 375 |
978 | 376 #ifdef USE_IPV6 |
377 snprintf(service, 6, "%d", cport); | |
378 memset(&hints, 0, sizeof(hints)); | |
379 hints.ai_socktype = SOCK_STREAM; | |
380 if (! getaddrinfo(chost, service, &hints, &res0)) { | |
381 eof = TRUE; | |
382 for (res = res0; res; res = res->ai_next) { | |
383 if ((sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) | |
384 continue; | |
385 fcntl(sock, F_SETFL, O_NONBLOCK); | |
386 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
387 mpgdec_ip.set_info_text(status); |
978 | 388 g_free(status); |
389 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = htons(cport); | |
390 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { | |
391 if (errno != EINPROGRESS) { | |
392 close(sock); | |
393 continue; | |
394 } | |
395 } | |
396 eof = FALSE; | |
397 break; | |
398 } | |
399 freeaddrinfo(res0); | |
400 if (eof) { | |
401 status = g_strdup_printf(_("Couldn't connect to host %s:%d"), chost, cport); | |
402 show_error_message(status); | |
403 g_free(status); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
404 mpgdec_ip.set_info_text(NULL); |
978 | 405 } |
406 } else { | |
407 status = g_strdup_printf(_("Couldn't look up host %s"), chost); | |
408 show_error_message(status); | |
409 g_free(status); | |
410 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
411 mpgdec_ip.set_info_text(NULL); |
978 | 412 eof = TRUE; |
413 } | |
414 #else | |
61 | 415 sock = socket(AF_INET, SOCK_STREAM, 0); |
416 fcntl(sock, F_SETFL, O_NONBLOCK); | |
417 address.sin_family = AF_INET; | |
418 | |
419 status = g_strdup_printf(_("LOOKING UP %s"), chost); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
420 mpgdec_ip.set_info_text(status); |
61 | 421 g_free(status); |
422 | |
423 if (!(hp = gethostbyname(chost))) { | |
424 status = g_strdup_printf(_("Couldn't look up host %s"), chost); | |
425 show_error_message(status); | |
426 g_free(status); | |
427 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
428 mpgdec_ip.set_info_text(NULL); |
61 | 429 eof = TRUE; |
430 } | |
978 | 431 #endif |
61 | 432 |
433 if (!eof) { | |
978 | 434 #ifndef USE_IPV6 |
61 | 435 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list), |
436 sizeof(address.sin_addr.s_addr)); | |
437 address.sin_port = g_htons(cport); | |
438 | |
439 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
440 mpgdec_ip.set_info_text(status); |
61 | 441 g_free(status); |
442 if (connect | |
443 (sock, (struct sockaddr *) &address, | |
444 sizeof(struct sockaddr_in)) == -1) { | |
445 if (errno != EINPROGRESS) { | |
446 status = | |
447 g_strdup_printf(_("Couldn't connect to host %s"), | |
448 chost); | |
449 show_error_message(status); | |
450 g_free(status); | |
451 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
452 mpgdec_ip.set_info_text(NULL); |
61 | 453 eof = TRUE; |
454 } | |
455 } | |
978 | 456 #endif |
61 | 457 while (going) { |
458 tv.tv_sec = 0; | |
459 tv.tv_usec = 10000; | |
460 FD_ZERO(&set); | |
461 FD_SET(sock, &set); | |
462 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) { | |
463 err_len = sizeof(error); | |
464 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len); | |
465 if (error) { | |
466 status = | |
467 g_strdup_printf(_ | |
468 ("Couldn't connect to host %s"), | |
469 chost); | |
470 show_error_message(status); | |
471 g_free(status); | |
472 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
473 mpgdec_ip.set_info_text(NULL); |
61 | 474 eof = TRUE; |
475 | |
476 } | |
477 break; | |
478 } | |
479 } | |
480 if (!eof) { | |
481 gchar *auth = NULL, *proxy_auth = NULL; | |
482 gchar udpspace[30]; | |
483 gint udp_port; | |
484 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
485 if (mpgdec_cfg.use_udp_channel) { |
61 | 486 udp_port = udp_establish_listener(&udp_sock); |
487 if (udp_port > 0) | |
488 sprintf(udpspace, "x-audiocast-udpport: %d\r\n", | |
489 udp_port); | |
490 else | |
491 udp_sock = 0; | |
492 } | |
493 | |
494 if (user && pass) | |
495 auth = | |
496 basic_authentication_encode(user, pass, | |
497 "Authorization"); | |
498 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
499 if (mpgdec_cfg.use_proxy) { |
61 | 500 file = g_strdup(url); |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
501 if (mpgdec_cfg.proxy_use_auth && mpgdec_cfg.proxy_user |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
502 && mpgdec_cfg.proxy_pass) { |
61 | 503 proxy_auth = |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
504 basic_authentication_encode(mpgdec_cfg. |
61 | 505 proxy_user, |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
506 mpgdec_cfg. |
61 | 507 proxy_pass, |
508 "Proxy-Authorization"); | |
509 } | |
510 } | |
511 else | |
512 file = g_strconcat("/", filename, NULL); | |
513 temp = g_strdup_printf("GET %s HTTP/1.0\r\n" | |
514 "Host: %s\r\n" | |
515 "User-Agent: %s/%s\r\n" | |
516 "%s%s%s%s\r\n", | |
748
71189eb31ea9
[svn] use PACKAGE_NAME instead of PACKAGE for autoconf compatibility
nenolod
parents:
701
diff
changeset
|
517 file, host, PACKAGE_NAME, PACKAGE_VERSION, |
61 | 518 proxy_auth ? proxy_auth : "", |
519 auth ? auth : "", | |
520 "Icy-MetaData:1\r\n", | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
521 mpgdec_cfg. |
61 | 522 use_udp_channel ? udpspace : ""); |
523 | |
524 g_free(file); | |
525 if (proxy_auth) | |
526 g_free(proxy_auth); | |
527 if (auth) | |
528 g_free(auth); | |
529 write(sock, temp, strlen(temp)); | |
530 g_free(temp); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
531 mpgdec_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY")); |
61 | 532 while (going && !eof) { |
533 if (http_check_for_data()) { | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
534 if (mpgdec_http_read_line(line, 1024)) { |
61 | 535 status = strchr(line, ' '); |
536 if (status) { | |
537 if (status[1] == '2') | |
538 break; | |
539 else if (status[1] == '3' | |
540 && status[2] == '0' | |
541 && status[3] == '2') { | |
542 while (going) { | |
543 if (http_check_for_data()) { | |
544 if ((cnt = | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
545 mpgdec_http_read_line |
61 | 546 (line, 1024)) != -1) { |
547 if (!cnt) | |
548 break; | |
549 if (!strncmp | |
550 (line, "Location:", 9)) { | |
551 g_free(url); | |
552 url = g_strdup(line + 10); | |
553 } | |
554 } | |
555 else { | |
556 eof = TRUE; | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
557 mpgdec_ip.set_info_text(NULL); |
61 | 558 break; |
559 } | |
560 } | |
561 } | |
562 redirect = TRUE; | |
563 break; | |
564 } | |
565 else { | |
566 status = | |
567 g_strdup_printf(_ | |
568 ("Couldn't connect to host %s\nServer reported: %s"), | |
569 chost, status); | |
570 show_error_message(status); | |
571 g_free(status); | |
572 break; | |
573 } | |
574 } | |
575 } | |
576 else { | |
577 eof = TRUE; | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
578 mpgdec_ip.set_info_text(NULL); |
61 | 579 } |
580 } | |
581 } | |
582 | |
583 while (going && !redirect) { | |
584 if (http_check_for_data()) { | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
585 if ((cnt = mpgdec_http_read_line(line, 1024)) != -1) { |
61 | 586 if (!cnt) |
587 break; | |
588 if (!strncmp(line, "icy-name:", 9)) | |
589 icy_name = g_strdup(line + 9); | |
590 else if (!strncmp(line, "x-audiocast-name:", 17)) | |
591 icy_name = g_strdup(line + 17); | |
592 if (!strncmp(line, "icy-metaint:", 12)) | |
593 icy_metaint = atoi(line + 12); | |
594 if (!strncmp(line, "x-audiocast-udpport:", 20)) { | |
595 #ifdef DEBUG_UDP | |
596 fprintf(stderr, | |
597 "Server wants udp messages on port %d\n", | |
598 atoi(line + 20)); | |
599 #endif | |
600 /* udp_serverport = atoi (line + 20); */ | |
601 } | |
602 | |
603 } | |
604 else { | |
605 eof = TRUE; | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
606 mpgdec_ip.set_info_text(NULL); |
61 | 607 break; |
608 } | |
609 } | |
610 } | |
611 } | |
612 } | |
613 | |
614 if (redirect) { | |
615 if (output_file) { | |
616 vfs_fclose(output_file); | |
617 output_file = NULL; | |
618 } | |
619 close(sock); | |
620 g_free(user); | |
621 g_free(pass); | |
622 g_free(host); | |
623 g_free(filename); | |
624 } | |
625 } while (redirect); | |
626 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
627 if (mpgdec_cfg.save_http_stream) { |
61 | 628 gchar *output_name; |
629 gint i = 1; | |
630 | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
631 file = mpgdec_http_get_title(url); |
61 | 632 output_name = file; |
633 if (!strncasecmp(output_name, "http://", 7)) | |
634 output_name += 7; | |
635 temp = strrchr(output_name, '.'); | |
636 if (temp && !strcasecmp(temp, ".mp3")) | |
637 *temp = '\0'; | |
638 | |
639 while ((temp = strchr(output_name, '/'))) | |
640 *temp = '_'; | |
641 output_name = g_strdup_printf("%s/%s.mp3", | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
642 mpgdec_cfg.save_http_path, output_name); |
61 | 643 while (!access(output_name, F_OK) && i < 100000) { |
644 g_free(output_name); | |
645 output_name = g_strdup_printf("%s/%s-%d.mp3", | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
646 mpgdec_cfg.save_http_path, |
61 | 647 output_name, i++); |
648 } | |
649 | |
650 g_free(file); | |
651 | |
652 output_file = vfs_fopen(output_name, "wb"); | |
653 g_free(output_name); | |
654 } | |
655 | |
656 while (going) { | |
657 | |
1273
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
658 if (!http_used() && !mpgdec_ip.output->buffer_playing()) { |
61 | 659 prebuffering = TRUE; |
1273
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
660 mpgdec_ip.set_status_buffering(TRUE); |
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
661 } |
61 | 662 if (http_free() > 0 && !eof) { |
663 if (http_check_for_data()) { | |
664 cnt = min(http_free(), buffer_length - wr_index); | |
665 if (cnt > 1024) | |
666 cnt = 1024; | |
667 written = read(sock, buffer + wr_index, cnt); | |
668 if (written <= 0) { | |
669 eof = TRUE; | |
670 if (prebuffering) { | |
671 prebuffering = FALSE; | |
1273
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
672 mpgdec_ip.set_status_buffering(FALSE); |
61 | 673 |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
674 mpgdec_ip.set_info_text(NULL); |
61 | 675 } |
676 | |
677 } | |
678 else | |
679 wr_index = (wr_index + written) % buffer_length; | |
680 } | |
681 | |
682 if (prebuffering) { | |
683 if (http_used() > prebuffer_length) { | |
684 prebuffering = FALSE; | |
1273
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
685 mpgdec_ip.set_status_buffering(FALSE); |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
686 mpgdec_ip.set_info_text(NULL); |
61 | 687 } |
688 else { | |
689 status = | |
990
05de825c2765
[svn] Use correct format string; gsize != long unsigned int.
chainsaw
parents:
978
diff
changeset
|
690 g_strdup_printf(_("PRE-BUFFERING: %zuKB/%zuKB"), |
61 | 691 http_used() / 1024, |
692 prebuffer_length / 1024); | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
693 mpgdec_ip.set_info_text(status); |
61 | 694 g_free(status); |
695 } | |
696 | |
697 } | |
698 } | |
699 else | |
1097 | 700 g_usleep(10000); |
61 | 701 |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
702 if (mpgdec_cfg.use_udp_channel && udp_sock != 0) |
61 | 703 if (udp_check_for_data(udp_sock) < 0) { |
704 close(udp_sock); | |
705 udp_sock = 0; | |
706 } | |
707 } | |
708 if (output_file) { | |
709 vfs_fclose(output_file); | |
710 output_file = NULL; | |
711 } | |
712 close(sock); | |
713 if (udp_sock != 0) | |
714 close(udp_sock); | |
715 | |
716 g_free(user); | |
717 g_free(pass); | |
718 g_free(host); | |
719 g_free(filename); | |
720 g_free(buffer); | |
721 g_free(url); | |
722 | |
723 return NULL; | |
724 } | |
725 | |
726 int | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
727 mpgdec_http_open(gchar * _url) |
61 | 728 { |
729 gchar *url; | |
730 | |
731 url = g_strdup(_url); | |
732 | |
733 rd_index = 0; | |
734 wr_index = 0; | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
735 buffer_length = mpgdec_cfg.http_buffer_size * 1024; |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
736 prebuffer_length = (buffer_length * mpgdec_cfg.http_prebuffer) / 100; |
61 | 737 buffer_read = 0; |
738 icy_metaint = 0; | |
739 prebuffering = TRUE; | |
1273
3b990c26fc46
[svn] - Support for the buffer indicator in playpaus.png that was apparently
nhjm449
parents:
1098
diff
changeset
|
740 mpgdec_ip.set_status_buffering(TRUE); |
61 | 741 going = TRUE; |
742 eof = FALSE; | |
743 buffer = g_malloc(buffer_length); | |
744 | |
745 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL); | |
746 | |
747 return 0; | |
748 } | |
749 | |
750 char * | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
751 mpgdec_http_get_title(gchar * url) |
61 | 752 { |
753 if (icy_name) | |
754 return g_strdup(icy_name); | |
755 if (g_basename(url) && strlen(g_basename(url)) > 0) | |
756 return g_strdup(g_basename(url)); | |
757 return g_strdup(url); | |
758 } | |
759 | |
760 /* Start UDP Channel specific stuff */ | |
761 | |
762 /* Find a good local udp port and bind udp_sock to it, return the port */ | |
763 static gint | |
764 udp_establish_listener(gint * sock) | |
765 { | |
978 | 766 #ifdef USE_IPV6 |
767 struct sockaddr_in6 sin; | |
768 socklen_t sinlen = sizeof(struct sockaddr_in6); | |
769 #else | |
61 | 770 struct sockaddr_in sin; |
771 socklen_t sinlen = sizeof(struct sockaddr_in); | |
978 | 772 #endif |
61 | 773 |
774 #ifdef DEBUG_UDP | |
775 fprintf(stderr, "Establishing udp listener\n"); | |
776 #endif | |
777 | |
978 | 778 #ifdef USE_IPV6 |
779 if ((*sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { | |
780 #else | |
61 | 781 if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
978 | 782 #endif |
61 | 783 g_log(NULL, G_LOG_LEVEL_CRITICAL, |
784 "udp_establish_listener(): unable to create socket"); | |
785 return -1; | |
786 } | |
787 | |
788 memset(&sin, 0, sinlen); | |
978 | 789 #ifdef USE_IPV6 |
790 sin.sin6_family = AF_INET6; | |
791 #else | |
61 | 792 sin.sin_family = AF_INET; |
793 sin.sin_addr.s_addr = g_htonl(INADDR_ANY); | |
978 | 794 #endif |
61 | 795 |
796 if (bind(*sock, (struct sockaddr *) &sin, sinlen) < 0) { | |
797 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
798 "udp_establish_listener(): Failed to bind socket to localhost: %s", | |
799 strerror(errno)); | |
800 close(*sock); | |
801 return -1; | |
802 } | |
803 if (fcntl(*sock, F_SETFL, O_NONBLOCK) < 0) { | |
804 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
805 "udp_establish_listener(): Failed to set flags: %s", | |
806 strerror(errno)); | |
807 close(*sock); | |
808 return -1; | |
809 } | |
810 | |
811 memset(&sin, 0, sinlen); | |
812 if (getsockname(*sock, (struct sockaddr *) &sin, &sinlen) < 0) { | |
813 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
814 "udp_establish_listener(): Failed to retrieve socket info: %s", | |
815 strerror(errno)); | |
816 close(*sock); | |
817 return -1; | |
818 } | |
819 #ifdef DEBUG_UDP | |
820 fprintf(stderr, "Listening on local %s:%d\n", inet_ntoa(sin.sin_addr), | |
821 g_ntohs(sin.sin_port)); | |
822 #endif | |
823 | |
978 | 824 #ifdef USE_IPV6 |
825 return g_ntohs(sin.sin6_port); | |
826 #else | |
61 | 827 return g_ntohs(sin.sin_port); |
978 | 828 #endif |
61 | 829 } |
830 | |
831 static int | |
832 udp_check_for_data(int sock) | |
833 { | |
834 char buf[1025], **lines; | |
835 char *valptr; | |
836 gchar *title; | |
837 gint len, i; | |
978 | 838 #ifdef USE_IPV6 |
839 struct sockaddr_in6 from; | |
840 #else | |
61 | 841 struct sockaddr_in from; |
978 | 842 #endif |
61 | 843 socklen_t fromlen; |
844 | |
978 | 845 fromlen = sizeof from; |
61 | 846 |
847 if ((len = | |
848 recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &from, | |
849 &fromlen)) < 0) { | |
850 if (errno != EAGAIN) { | |
851 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
852 "udp_read_data(): Error reading from socket: %s", | |
853 strerror(errno)); | |
854 return -1; | |
855 } | |
856 return 0; | |
857 } | |
858 buf[len] = '\0'; | |
859 #ifdef DEBUG_UDP | |
860 fprintf(stderr, "Received: [%s]\n", buf); | |
861 #endif | |
862 lines = g_strsplit(buf, "\n", 0); | |
863 if (!lines) | |
864 return 0; | |
865 | |
866 for (i = 0; lines[i]; i++) { | |
867 while ((lines[i][strlen(lines[i]) - 1] == '\n') || | |
868 (lines[i][strlen(lines[i]) - 1] == '\r')) | |
869 lines[i][strlen(lines[i]) - 1] = '\0'; | |
870 | |
871 valptr = strchr(lines[i], ':'); | |
872 | |
873 if (!valptr) | |
874 continue; | |
875 else | |
876 valptr++; | |
877 | |
878 g_strstrip(valptr); | |
879 if (!strlen(valptr)) | |
880 continue; | |
881 | |
882 if (strstr(lines[i], "x-audiocast-streamtitle") != NULL) { | |
883 title = g_strdup_printf("%s (%s)", valptr, icy_name); | |
884 if (going) | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
885 mpgdec_ip.set_info(title, -1, mpgdec_bitrate * 1000, |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
886 mpgdec_frequency, mpgdec_stereo); |
61 | 887 g_free(title); |
888 } | |
889 #if 0 | |
890 else if (strstr(lines[i], "x-audiocast-streamlength") != NULL) { | |
891 if (atoi(valptr) != -1) | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
892 mpgdec_ip.set_info(NULL, atoi(valptr), |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
893 mpgdec_bitrate * 1000, mpgdec_frequency, |
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
894 mpgdec_stereo); |
61 | 895 } |
896 #endif | |
897 | |
898 else if (strstr(lines[i], "x-audiocast-streammsg") != NULL) { | |
1098
b5ae09a6c2f1
[svn] - prepare to split audacious code away from the actual decoder and use a highlevel API
nenolod
parents:
1097
diff
changeset
|
899 /* mpgdec_ip.set_info(title, -1, mpgdec_bitrate * 1000, mpgdec_frequency, mpgdec_stereo); */ |
61 | 900 /* xmms_show_message(_("Message"), valptr, _("Ok"), */ |
901 /* FALSE, NULL, NULL); */ | |
902 g_message("Stream_message: %s", valptr); | |
903 } | |
904 #if 0 | |
905 /* Use this to direct your webbrowser.. yeah right.. */ | |
906 else if (strstr(lines[i], "x-audiocast-streamurl") != NULL) { | |
907 if (lasturl && g_strcmp(valptr, lasturl)) { | |
908 c_message(stderr, "Song URL: %s\n", valptr); | |
909 g_free(lasturl); | |
910 lasturl = g_strdup(valptr); | |
911 } | |
912 } | |
913 #endif | |
914 else if (strstr(lines[i], "x-audiocast-udpseqnr:") != NULL) { | |
915 gchar obuf[60]; | |
916 sprintf(obuf, "x-audiocast-ack: %ld \r\n", atol(valptr)); | |
917 if (sendto | |
918 (sock, obuf, strlen(obuf), 0, (struct sockaddr *) &from, | |
919 fromlen) < 0) { | |
920 g_log(NULL, G_LOG_LEVEL_WARNING, | |
921 "udp_check_for_data(): Unable to send ack to server: %s", | |
922 strerror(errno)); | |
923 } | |
924 #ifdef DEBUG_UDP | |
925 else | |
926 fprintf(stderr, "Sent ack: %s", obuf); | |
978 | 927 #ifdef USE_IPV6 |
928 { | |
929 char adr[INET6_ADDRSTRLEN]; | |
930 inet_ntop(AF_INET6, &from.sin6_addr, adr, INET6_ADDRSTRLEN); | |
931 fprintf(stderr, "Remote: [%s]:%d\n", adr, | |
932 g_ntohs(from.sin6_port)); | |
933 } | |
934 #else | |
61 | 935 fprintf(stderr, "Remote: %s:%d\n", inet_ntoa(from.sin_addr), |
936 g_ntohs(from.sin_port)); | |
937 #endif | |
978 | 938 #endif |
61 | 939 } |
940 } | |
941 g_strfreev(lines); | |
942 return 0; | |
943 } |