Mercurial > audlegacy
annotate Plugins/Input/mpg123/http.c @ 801:ec9ba0ad38a8 trunk
[svn] - use OBJECTIVE_DATA instead of a kludge in the posthook here
author | nenolod |
---|---|
date | Sat, 04 Mar 2006 14:34:04 -0800 |
parents | 71189eb31ea9 |
children | a7b53e6a71e0 |
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 | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
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 | |
57 extern gint mpg123_bitrate, mpg123_frequency, mpg123_stereo; | |
58 extern gboolean mpg123_stereo; | |
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 | |
182 mpg123_http_close(void) | |
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 | |
212 && mpg123_info->going) | |
213 xmms_usleep(10000); | |
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 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
231 mpg123_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 | |
238 if (!going && !mpg123_info->going) | |
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); | |
265 mpg123_ip.set_info(title, -1, | |
266 mpg123_bitrate * 1000, | |
267 mpg123_frequency, | |
268 mpg123_stereo); | |
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 | |
318 mpg123_http_read_line(gchar * buf, gint size) | |
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; | |
349 struct hostent *hp; | |
350 struct sockaddr_in address; | |
351 struct timeval tv; | |
352 | |
353 url = (gchar *) arg; | |
354 do { | |
355 redirect = FALSE; | |
356 | |
357 g_strstrip(url); | |
358 | |
359 parse_url(url, &user, &pass, &host, &port, &filename); | |
360 | |
361 if ((!filename || !*filename) && url[strlen(url) - 1] != '/') | |
362 temp = g_strconcat(url, "/", NULL); | |
363 else | |
364 temp = g_strdup(url); | |
365 g_free(url); | |
366 url = temp; | |
367 | |
368 chost = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_host : host; | |
369 cport = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_port : port; | |
370 | |
371 sock = socket(AF_INET, SOCK_STREAM, 0); | |
372 fcntl(sock, F_SETFL, O_NONBLOCK); | |
373 address.sin_family = AF_INET; | |
374 | |
375 status = g_strdup_printf(_("LOOKING UP %s"), chost); | |
376 mpg123_ip.set_info_text(status); | |
377 g_free(status); | |
378 | |
379 if (!(hp = gethostbyname(chost))) { | |
380 status = g_strdup_printf(_("Couldn't look up host %s"), chost); | |
381 show_error_message(status); | |
382 g_free(status); | |
383 | |
384 mpg123_ip.set_info_text(NULL); | |
385 eof = TRUE; | |
386 } | |
387 | |
388 if (!eof) { | |
389 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list), | |
390 sizeof(address.sin_addr.s_addr)); | |
391 address.sin_port = g_htons(cport); | |
392 | |
393 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport); | |
394 mpg123_ip.set_info_text(status); | |
395 g_free(status); | |
396 if (connect | |
397 (sock, (struct sockaddr *) &address, | |
398 sizeof(struct sockaddr_in)) == -1) { | |
399 if (errno != EINPROGRESS) { | |
400 status = | |
401 g_strdup_printf(_("Couldn't connect to host %s"), | |
402 chost); | |
403 show_error_message(status); | |
404 g_free(status); | |
405 | |
406 mpg123_ip.set_info_text(NULL); | |
407 eof = TRUE; | |
408 } | |
409 } | |
410 while (going) { | |
411 tv.tv_sec = 0; | |
412 tv.tv_usec = 10000; | |
413 FD_ZERO(&set); | |
414 FD_SET(sock, &set); | |
415 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) { | |
416 err_len = sizeof(error); | |
417 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len); | |
418 if (error) { | |
419 status = | |
420 g_strdup_printf(_ | |
421 ("Couldn't connect to host %s"), | |
422 chost); | |
423 show_error_message(status); | |
424 g_free(status); | |
425 | |
426 mpg123_ip.set_info_text(NULL); | |
427 eof = TRUE; | |
428 | |
429 } | |
430 break; | |
431 } | |
432 } | |
433 if (!eof) { | |
434 gchar *auth = NULL, *proxy_auth = NULL; | |
435 gchar udpspace[30]; | |
436 gint udp_port; | |
437 | |
438 if (mpg123_cfg.use_udp_channel) { | |
439 udp_port = udp_establish_listener(&udp_sock); | |
440 if (udp_port > 0) | |
441 sprintf(udpspace, "x-audiocast-udpport: %d\r\n", | |
442 udp_port); | |
443 else | |
444 udp_sock = 0; | |
445 } | |
446 | |
447 if (user && pass) | |
448 auth = | |
449 basic_authentication_encode(user, pass, | |
450 "Authorization"); | |
451 | |
452 if (mpg123_cfg.use_proxy) { | |
453 file = g_strdup(url); | |
454 if (mpg123_cfg.proxy_use_auth && mpg123_cfg.proxy_user | |
455 && mpg123_cfg.proxy_pass) { | |
456 proxy_auth = | |
457 basic_authentication_encode(mpg123_cfg. | |
458 proxy_user, | |
459 mpg123_cfg. | |
460 proxy_pass, | |
461 "Proxy-Authorization"); | |
462 } | |
463 } | |
464 else | |
465 file = g_strconcat("/", filename, NULL); | |
466 temp = g_strdup_printf("GET %s HTTP/1.0\r\n" | |
467 "Host: %s\r\n" | |
468 "User-Agent: %s/%s\r\n" | |
469 "%s%s%s%s\r\n", | |
748
71189eb31ea9
[svn] use PACKAGE_NAME instead of PACKAGE for autoconf compatibility
nenolod
parents:
701
diff
changeset
|
470 file, host, PACKAGE_NAME, PACKAGE_VERSION, |
61 | 471 proxy_auth ? proxy_auth : "", |
472 auth ? auth : "", | |
473 "Icy-MetaData:1\r\n", | |
474 mpg123_cfg. | |
475 use_udp_channel ? udpspace : ""); | |
476 | |
477 g_free(file); | |
478 if (proxy_auth) | |
479 g_free(proxy_auth); | |
480 if (auth) | |
481 g_free(auth); | |
482 write(sock, temp, strlen(temp)); | |
483 g_free(temp); | |
484 mpg123_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY")); | |
485 while (going && !eof) { | |
486 if (http_check_for_data()) { | |
487 if (mpg123_http_read_line(line, 1024)) { | |
488 status = strchr(line, ' '); | |
489 if (status) { | |
490 if (status[1] == '2') | |
491 break; | |
492 else if (status[1] == '3' | |
493 && status[2] == '0' | |
494 && status[3] == '2') { | |
495 while (going) { | |
496 if (http_check_for_data()) { | |
497 if ((cnt = | |
498 mpg123_http_read_line | |
499 (line, 1024)) != -1) { | |
500 if (!cnt) | |
501 break; | |
502 if (!strncmp | |
503 (line, "Location:", 9)) { | |
504 g_free(url); | |
505 url = g_strdup(line + 10); | |
506 } | |
507 } | |
508 else { | |
509 eof = TRUE; | |
510 mpg123_ip.set_info_text(NULL); | |
511 break; | |
512 } | |
513 } | |
514 } | |
515 redirect = TRUE; | |
516 break; | |
517 } | |
518 else { | |
519 status = | |
520 g_strdup_printf(_ | |
521 ("Couldn't connect to host %s\nServer reported: %s"), | |
522 chost, status); | |
523 show_error_message(status); | |
524 g_free(status); | |
525 break; | |
526 } | |
527 } | |
528 } | |
529 else { | |
530 eof = TRUE; | |
531 mpg123_ip.set_info_text(NULL); | |
532 } | |
533 } | |
534 } | |
535 | |
536 while (going && !redirect) { | |
537 if (http_check_for_data()) { | |
538 if ((cnt = mpg123_http_read_line(line, 1024)) != -1) { | |
539 if (!cnt) | |
540 break; | |
541 if (!strncmp(line, "icy-name:", 9)) | |
542 icy_name = g_strdup(line + 9); | |
543 else if (!strncmp(line, "x-audiocast-name:", 17)) | |
544 icy_name = g_strdup(line + 17); | |
545 if (!strncmp(line, "icy-metaint:", 12)) | |
546 icy_metaint = atoi(line + 12); | |
547 if (!strncmp(line, "x-audiocast-udpport:", 20)) { | |
548 #ifdef DEBUG_UDP | |
549 fprintf(stderr, | |
550 "Server wants udp messages on port %d\n", | |
551 atoi(line + 20)); | |
552 #endif | |
553 /* udp_serverport = atoi (line + 20); */ | |
554 } | |
555 | |
556 } | |
557 else { | |
558 eof = TRUE; | |
559 mpg123_ip.set_info_text(NULL); | |
560 break; | |
561 } | |
562 } | |
563 } | |
564 } | |
565 } | |
566 | |
567 if (redirect) { | |
568 if (output_file) { | |
569 vfs_fclose(output_file); | |
570 output_file = NULL; | |
571 } | |
572 close(sock); | |
573 g_free(user); | |
574 g_free(pass); | |
575 g_free(host); | |
576 g_free(filename); | |
577 } | |
578 } while (redirect); | |
579 | |
580 if (mpg123_cfg.save_http_stream) { | |
581 gchar *output_name; | |
582 gint i = 1; | |
583 | |
584 file = mpg123_http_get_title(url); | |
585 output_name = file; | |
586 if (!strncasecmp(output_name, "http://", 7)) | |
587 output_name += 7; | |
588 temp = strrchr(output_name, '.'); | |
589 if (temp && !strcasecmp(temp, ".mp3")) | |
590 *temp = '\0'; | |
591 | |
592 while ((temp = strchr(output_name, '/'))) | |
593 *temp = '_'; | |
594 output_name = g_strdup_printf("%s/%s.mp3", | |
595 mpg123_cfg.save_http_path, output_name); | |
596 while (!access(output_name, F_OK) && i < 100000) { | |
597 g_free(output_name); | |
598 output_name = g_strdup_printf("%s/%s-%d.mp3", | |
599 mpg123_cfg.save_http_path, | |
600 output_name, i++); | |
601 } | |
602 | |
603 g_free(file); | |
604 | |
605 output_file = vfs_fopen(output_name, "wb"); | |
606 g_free(output_name); | |
607 } | |
608 | |
609 while (going) { | |
610 | |
611 if (!http_used() && !mpg123_ip.output->buffer_playing()) | |
612 prebuffering = TRUE; | |
613 if (http_free() > 0 && !eof) { | |
614 if (http_check_for_data()) { | |
615 cnt = min(http_free(), buffer_length - wr_index); | |
616 if (cnt > 1024) | |
617 cnt = 1024; | |
618 written = read(sock, buffer + wr_index, cnt); | |
619 if (written <= 0) { | |
620 eof = TRUE; | |
621 if (prebuffering) { | |
622 prebuffering = FALSE; | |
623 | |
624 mpg123_ip.set_info_text(NULL); | |
625 } | |
626 | |
627 } | |
628 else | |
629 wr_index = (wr_index + written) % buffer_length; | |
630 } | |
631 | |
632 if (prebuffering) { | |
633 if (http_used() > prebuffer_length) { | |
634 prebuffering = FALSE; | |
635 mpg123_ip.set_info_text(NULL); | |
636 } | |
637 else { | |
638 status = | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
639 g_strdup_printf(_("PRE-BUFFERING: %luKB/%luKB"), |
61 | 640 http_used() / 1024, |
641 prebuffer_length / 1024); | |
642 mpg123_ip.set_info_text(status); | |
643 g_free(status); | |
644 } | |
645 | |
646 } | |
647 } | |
648 else | |
649 xmms_usleep(10000); | |
650 | |
651 if (mpg123_cfg.use_udp_channel && udp_sock != 0) | |
652 if (udp_check_for_data(udp_sock) < 0) { | |
653 close(udp_sock); | |
654 udp_sock = 0; | |
655 } | |
656 } | |
657 if (output_file) { | |
658 vfs_fclose(output_file); | |
659 output_file = NULL; | |
660 } | |
661 close(sock); | |
662 if (udp_sock != 0) | |
663 close(udp_sock); | |
664 | |
665 g_free(user); | |
666 g_free(pass); | |
667 g_free(host); | |
668 g_free(filename); | |
669 g_free(buffer); | |
670 g_free(url); | |
671 | |
672 return NULL; | |
673 } | |
674 | |
675 int | |
676 mpg123_http_open(gchar * _url) | |
677 { | |
678 gchar *url; | |
679 | |
680 url = g_strdup(_url); | |
681 | |
682 rd_index = 0; | |
683 wr_index = 0; | |
684 buffer_length = mpg123_cfg.http_buffer_size * 1024; | |
685 prebuffer_length = (buffer_length * mpg123_cfg.http_prebuffer) / 100; | |
686 buffer_read = 0; | |
687 icy_metaint = 0; | |
688 prebuffering = TRUE; | |
689 going = TRUE; | |
690 eof = FALSE; | |
691 buffer = g_malloc(buffer_length); | |
692 | |
693 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL); | |
694 | |
695 return 0; | |
696 } | |
697 | |
698 char * | |
699 mpg123_http_get_title(gchar * url) | |
700 { | |
701 if (icy_name) | |
702 return g_strdup(icy_name); | |
703 if (g_basename(url) && strlen(g_basename(url)) > 0) | |
704 return g_strdup(g_basename(url)); | |
705 return g_strdup(url); | |
706 } | |
707 | |
708 /* Start UDP Channel specific stuff */ | |
709 | |
710 /* Find a good local udp port and bind udp_sock to it, return the port */ | |
711 static gint | |
712 udp_establish_listener(gint * sock) | |
713 { | |
714 struct sockaddr_in sin; | |
715 socklen_t sinlen = sizeof(struct sockaddr_in); | |
716 | |
717 #ifdef DEBUG_UDP | |
718 fprintf(stderr, "Establishing udp listener\n"); | |
719 #endif | |
720 | |
721 if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { | |
722 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
723 "udp_establish_listener(): unable to create socket"); | |
724 return -1; | |
725 } | |
726 | |
727 memset(&sin, 0, sinlen); | |
728 sin.sin_family = AF_INET; | |
729 sin.sin_addr.s_addr = g_htonl(INADDR_ANY); | |
730 | |
731 if (bind(*sock, (struct sockaddr *) &sin, sinlen) < 0) { | |
732 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
733 "udp_establish_listener(): Failed to bind socket to localhost: %s", | |
734 strerror(errno)); | |
735 close(*sock); | |
736 return -1; | |
737 } | |
738 if (fcntl(*sock, F_SETFL, O_NONBLOCK) < 0) { | |
739 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
740 "udp_establish_listener(): Failed to set flags: %s", | |
741 strerror(errno)); | |
742 close(*sock); | |
743 return -1; | |
744 } | |
745 | |
746 memset(&sin, 0, sinlen); | |
747 if (getsockname(*sock, (struct sockaddr *) &sin, &sinlen) < 0) { | |
748 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
749 "udp_establish_listener(): Failed to retrieve socket info: %s", | |
750 strerror(errno)); | |
751 close(*sock); | |
752 return -1; | |
753 } | |
754 #ifdef DEBUG_UDP | |
755 fprintf(stderr, "Listening on local %s:%d\n", inet_ntoa(sin.sin_addr), | |
756 g_ntohs(sin.sin_port)); | |
757 #endif | |
758 | |
759 return g_ntohs(sin.sin_port); | |
760 } | |
761 | |
762 static int | |
763 udp_check_for_data(int sock) | |
764 { | |
765 char buf[1025], **lines; | |
766 char *valptr; | |
767 gchar *title; | |
768 gint len, i; | |
769 struct sockaddr_in from; | |
770 socklen_t fromlen; | |
771 | |
772 fromlen = sizeof(struct sockaddr_in); | |
773 | |
774 if ((len = | |
775 recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &from, | |
776 &fromlen)) < 0) { | |
777 if (errno != EAGAIN) { | |
778 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
779 "udp_read_data(): Error reading from socket: %s", | |
780 strerror(errno)); | |
781 return -1; | |
782 } | |
783 return 0; | |
784 } | |
785 buf[len] = '\0'; | |
786 #ifdef DEBUG_UDP | |
787 fprintf(stderr, "Received: [%s]\n", buf); | |
788 #endif | |
789 lines = g_strsplit(buf, "\n", 0); | |
790 if (!lines) | |
791 return 0; | |
792 | |
793 for (i = 0; lines[i]; i++) { | |
794 while ((lines[i][strlen(lines[i]) - 1] == '\n') || | |
795 (lines[i][strlen(lines[i]) - 1] == '\r')) | |
796 lines[i][strlen(lines[i]) - 1] = '\0'; | |
797 | |
798 valptr = strchr(lines[i], ':'); | |
799 | |
800 if (!valptr) | |
801 continue; | |
802 else | |
803 valptr++; | |
804 | |
805 g_strstrip(valptr); | |
806 if (!strlen(valptr)) | |
807 continue; | |
808 | |
809 if (strstr(lines[i], "x-audiocast-streamtitle") != NULL) { | |
810 title = g_strdup_printf("%s (%s)", valptr, icy_name); | |
811 if (going) | |
812 mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000, | |
813 mpg123_frequency, mpg123_stereo); | |
814 g_free(title); | |
815 } | |
816 #if 0 | |
817 else if (strstr(lines[i], "x-audiocast-streamlength") != NULL) { | |
818 if (atoi(valptr) != -1) | |
819 mpg123_ip.set_info(NULL, atoi(valptr), | |
820 mpg123_bitrate * 1000, mpg123_frequency, | |
821 mpg123_stereo); | |
822 } | |
823 #endif | |
824 | |
825 else if (strstr(lines[i], "x-audiocast-streammsg") != NULL) { | |
826 /* mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000, mpg123_frequency, mpg123_stereo); */ | |
827 /* xmms_show_message(_("Message"), valptr, _("Ok"), */ | |
828 /* FALSE, NULL, NULL); */ | |
829 g_message("Stream_message: %s", valptr); | |
830 } | |
831 #if 0 | |
832 /* Use this to direct your webbrowser.. yeah right.. */ | |
833 else if (strstr(lines[i], "x-audiocast-streamurl") != NULL) { | |
834 if (lasturl && g_strcmp(valptr, lasturl)) { | |
835 c_message(stderr, "Song URL: %s\n", valptr); | |
836 g_free(lasturl); | |
837 lasturl = g_strdup(valptr); | |
838 } | |
839 } | |
840 #endif | |
841 else if (strstr(lines[i], "x-audiocast-udpseqnr:") != NULL) { | |
842 gchar obuf[60]; | |
843 sprintf(obuf, "x-audiocast-ack: %ld \r\n", atol(valptr)); | |
844 if (sendto | |
845 (sock, obuf, strlen(obuf), 0, (struct sockaddr *) &from, | |
846 fromlen) < 0) { | |
847 g_log(NULL, G_LOG_LEVEL_WARNING, | |
848 "udp_check_for_data(): Unable to send ack to server: %s", | |
849 strerror(errno)); | |
850 } | |
851 #ifdef DEBUG_UDP | |
852 else | |
853 fprintf(stderr, "Sent ack: %s", obuf); | |
854 fprintf(stderr, "Remote: %s:%d\n", inet_ntoa(from.sin_addr), | |
855 g_ntohs(from.sin_port)); | |
856 #endif | |
857 } | |
858 } | |
859 g_strfreev(lines); | |
860 return 0; | |
861 } |