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