comparison Plugins/Input/mpg123/http.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 5a338b31e393
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
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;
61 static gint sock, rd_index, wr_index, buffer_length, prebuffer_length;
62 static guint64 buffer_read = 0;
63 static gchar *buffer;
64 static GThread *thread;
65 static GtkWidget *error_dialog = NULL;
66
67 static VFSFile *output_file = NULL;
68
69 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3))
70
71 /* Encode the string S of length LENGTH to base64 format and place it
72 to STORE. STORE will be 0-terminated, and must point to a writable
73 buffer of at least 1+BASE64_LENGTH(length) bytes. */
74 static void
75 base64_encode(const gchar * s, gchar * store, gint length)
76 {
77 /* Conversion table. */
78 static gchar tbl[64] = {
79 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
80 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
81 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
82 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
83 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
84 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
85 'w', 'x', 'y', 'z', '0', '1', '2', '3',
86 '4', '5', '6', '7', '8', '9', '+', '/'
87 };
88 gint i;
89 guchar *p = (guchar *) store;
90
91 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
92 for (i = 0; i < length; i += 3) {
93 *p++ = tbl[s[0] >> 2];
94 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
95 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
96 *p++ = tbl[s[2] & 0x3f];
97 s += 3;
98 }
99 /* Pad the result if necessary... */
100 if (i == length + 1)
101 *(p - 1) = '=';
102 else if (i == length + 2)
103 *(p - 1) = *(p - 2) = '=';
104 /* ...and zero-terminate it. */
105 *p = '\0';
106 }
107
108 /* Create the authentication header contents for the `Basic' scheme.
109 This is done by encoding the string `USER:PASS' in base64 and
110 prepending `HEADER: Basic ' to it. */
111 static gchar *
112 basic_authentication_encode(const gchar * user,
113 const gchar * passwd, const gchar * header)
114 {
115 gchar *t1, *t2, *res;
116 gint len1 = strlen(user) + 1 + strlen(passwd);
117 gint len2 = BASE64_LENGTH(len1);
118
119 t1 = g_strdup_printf("%s:%s", user, passwd);
120 t2 = g_malloc0(len2 + 1);
121 base64_encode(t1, t2, len1);
122 res = g_strdup_printf("%s: Basic %s\r\n", header, t2);
123 g_free(t2);
124 g_free(t1);
125
126 return res;
127 }
128
129 static void
130 parse_url(const gchar * url, gchar ** user, gchar ** pass,
131 gchar ** host, gint * port, gchar ** filename)
132 {
133 gchar *h, *p, *pt, *f, *temp, *ptr;
134
135 temp = g_strdup(url);
136 ptr = temp;
137
138 if (!strncasecmp("http://", ptr, 7))
139 ptr += 7;
140 h = strchr(ptr, '@');
141 f = strchr(ptr, '/');
142 if (h != NULL && (!f || h < f)) {
143 *h = '\0';
144 p = strchr(ptr, ':');
145 if (p != NULL && p < h) {
146 *p = '\0';
147 p++;
148 *pass = g_strdup(p);
149 }
150 else
151 *pass = NULL;
152 *user = g_strdup(ptr);
153 h++;
154 ptr = h;
155 }
156 else {
157 *user = NULL;
158 *pass = NULL;
159 h = ptr;
160 }
161 pt = strchr(ptr, ':');
162 if (pt != NULL && (f == NULL || pt < f)) {
163 *pt = '\0';
164 *port = atoi(pt + 1);
165 }
166 else {
167 if (f)
168 *f = '\0';
169 *port = 80;
170 }
171 *host = g_strdup(h);
172
173 if (f)
174 *filename = g_strdup(f + 1);
175 else
176 *filename = NULL;
177 g_free(temp);
178 }
179
180 void
181 mpg123_http_close(void)
182 {
183 going = FALSE;
184
185 g_thread_join(thread);
186 g_free(icy_name);
187 icy_name = NULL;
188 }
189
190
191 static gint
192 http_used(void)
193 {
194 if (wr_index >= rd_index)
195 return wr_index - rd_index;
196 return buffer_length - (rd_index - wr_index);
197 }
198
199 static gint
200 http_free(void)
201 {
202 if (rd_index > wr_index)
203 return (rd_index - wr_index) - 1;
204 return (buffer_length - (wr_index - rd_index)) - 1;
205 }
206
207 static void
208 http_wait_for_data(gint bytes)
209 {
210 while ((prebuffering || http_used() < bytes) && !eof && going
211 && mpg123_info->going)
212 xmms_usleep(10000);
213 }
214
215 static void
216 show_error_message(gchar * error)
217 {
218 if (!error_dialog) {
219 GDK_THREADS_ENTER();
220 error_dialog = xmms_show_message(_("Error"), error, _("Ok"), FALSE,
221 NULL, NULL);
222 g_signal_connect(G_OBJECT(error_dialog),
223 "destroy",
224 G_CALLBACK(gtk_widget_destroyed), &error_dialog);
225 GDK_THREADS_LEAVE();
226 }
227 }
228
229 int
230 mpg123_http_read(gpointer data, gint length)
231 {
232 gint len, cnt, off = 0, meta_len, meta_off = 0, i;
233 gchar *meta_data, **tags;
234
235 http_wait_for_data(length);
236
237 if (!going && !mpg123_info->going)
238 return 0;
239 len = min(http_used(), length);
240
241 while (len && http_used()) {
242 if ((icy_metaint > 0) && (buffer_read % icy_metaint) == 0 &&
243 (buffer_read > 0)) {
244 meta_len = *((guchar *) buffer + rd_index) * 16;
245 rd_index = (rd_index + 1) % buffer_length;
246 if (meta_len > 0) {
247 http_wait_for_data(meta_len);
248 meta_data = g_malloc0(meta_len);
249 if (http_used() >= meta_len) {
250 while (meta_len) {
251 cnt = min(meta_len, buffer_length - rd_index);
252 memcpy(meta_data + meta_off, buffer + rd_index, cnt);
253 rd_index = (rd_index + cnt) % buffer_length;
254 meta_len -= cnt;
255 meta_off += cnt;
256 }
257 tags = g_strsplit(meta_data, "';", 0);
258
259 for (i = 0; tags[i]; i++) {
260 if (!strncasecmp(tags[i], "StreamTitle=", 12)) {
261 gchar *temp = tags[i] + 13;
262 gchar *title =
263 g_strdup_printf("%s (%s)", temp, icy_name);
264 mpg123_ip.set_info(title, -1,
265 mpg123_bitrate * 1000,
266 mpg123_frequency,
267 mpg123_stereo);
268 g_free(title);
269 }
270
271 }
272 g_strfreev(tags);
273
274 }
275 g_free(meta_data);
276 }
277 if (!http_used())
278 http_wait_for_data(length - off);
279 cnt = min3(len, buffer_length - rd_index, http_used());
280 }
281 else if (icy_metaint > 0)
282 cnt =
283 min4(len, buffer_length - rd_index, http_used(),
284 icy_metaint - (buffer_read % icy_metaint));
285 else
286 cnt = min3(len, buffer_length - rd_index, http_used());
287 if (output_file)
288 vfs_fwrite(buffer + rd_index, 1, cnt, output_file);
289
290 memcpy((gchar *) data + off, buffer + rd_index, cnt);
291 rd_index = (rd_index + cnt) % buffer_length;
292 buffer_read += cnt;
293 len -= cnt;
294 off += cnt;
295 }
296 return off;
297 }
298
299 static gboolean
300 http_check_for_data(void)
301 {
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;
344 gint cnt, written, error, err_len, port, cport;
345 gboolean redirect;
346 gint udp_sock = 0;
347 fd_set set;
348 struct hostent *hp;
349 struct sockaddr_in address;
350 struct timeval tv;
351
352 url = (gchar *) arg;
353 do {
354 redirect = FALSE;
355
356 g_strstrip(url);
357
358 parse_url(url, &user, &pass, &host, &port, &filename);
359
360 if ((!filename || !*filename) && url[strlen(url) - 1] != '/')
361 temp = g_strconcat(url, "/", NULL);
362 else
363 temp = g_strdup(url);
364 g_free(url);
365 url = temp;
366
367 chost = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_host : host;
368 cport = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_port : port;
369
370 sock = socket(AF_INET, SOCK_STREAM, 0);
371 fcntl(sock, F_SETFL, O_NONBLOCK);
372 address.sin_family = AF_INET;
373
374 status = g_strdup_printf(_("LOOKING UP %s"), chost);
375 mpg123_ip.set_info_text(status);
376 g_free(status);
377
378 if (!(hp = gethostbyname(chost))) {
379 status = g_strdup_printf(_("Couldn't look up host %s"), chost);
380 show_error_message(status);
381 g_free(status);
382
383 mpg123_ip.set_info_text(NULL);
384 eof = TRUE;
385 }
386
387 if (!eof) {
388 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list),
389 sizeof(address.sin_addr.s_addr));
390 address.sin_port = g_htons(cport);
391
392 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport);
393 mpg123_ip.set_info_text(status);
394 g_free(status);
395 if (connect
396 (sock, (struct sockaddr *) &address,
397 sizeof(struct sockaddr_in)) == -1) {
398 if (errno != EINPROGRESS) {
399 status =
400 g_strdup_printf(_("Couldn't connect to host %s"),
401 chost);
402 show_error_message(status);
403 g_free(status);
404
405 mpg123_ip.set_info_text(NULL);
406 eof = TRUE;
407 }
408 }
409 while (going) {
410 tv.tv_sec = 0;
411 tv.tv_usec = 10000;
412 FD_ZERO(&set);
413 FD_SET(sock, &set);
414 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) {
415 err_len = sizeof(error);
416 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len);
417 if (error) {
418 status =
419 g_strdup_printf(_
420 ("Couldn't connect to host %s"),
421 chost);
422 show_error_message(status);
423 g_free(status);
424
425 mpg123_ip.set_info_text(NULL);
426 eof = TRUE;
427
428 }
429 break;
430 }
431 }
432 if (!eof) {
433 gchar *auth = NULL, *proxy_auth = NULL;
434 gchar udpspace[30];
435 gint udp_port;
436
437 if (mpg123_cfg.use_udp_channel) {
438 udp_port = udp_establish_listener(&udp_sock);
439 if (udp_port > 0)
440 sprintf(udpspace, "x-audiocast-udpport: %d\r\n",
441 udp_port);
442 else
443 udp_sock = 0;
444 }
445
446 if (user && pass)
447 auth =
448 basic_authentication_encode(user, pass,
449 "Authorization");
450
451 if (mpg123_cfg.use_proxy) {
452 file = g_strdup(url);
453 if (mpg123_cfg.proxy_use_auth && mpg123_cfg.proxy_user
454 && mpg123_cfg.proxy_pass) {
455 proxy_auth =
456 basic_authentication_encode(mpg123_cfg.
457 proxy_user,
458 mpg123_cfg.
459 proxy_pass,
460 "Proxy-Authorization");
461 }
462 }
463 else
464 file = g_strconcat("/", filename, NULL);
465 temp = g_strdup_printf("GET %s HTTP/1.0\r\n"
466 "Host: %s\r\n"
467 "User-Agent: %s/%s\r\n"
468 "%s%s%s%s\r\n",
469 file, host, PACKAGE, VERSION,
470 proxy_auth ? proxy_auth : "",
471 auth ? auth : "",
472 "Icy-MetaData:1\r\n",
473 mpg123_cfg.
474 use_udp_channel ? udpspace : "");
475
476 g_free(file);
477 if (proxy_auth)
478 g_free(proxy_auth);
479 if (auth)
480 g_free(auth);
481 write(sock, temp, strlen(temp));
482 g_free(temp);
483 mpg123_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY"));
484 while (going && !eof) {
485 if (http_check_for_data()) {
486 if (mpg123_http_read_line(line, 1024)) {
487 status = strchr(line, ' ');
488 if (status) {
489 if (status[1] == '2')
490 break;
491 else if (status[1] == '3'
492 && status[2] == '0'
493 && status[3] == '2') {
494 while (going) {
495 if (http_check_for_data()) {
496 if ((cnt =
497 mpg123_http_read_line
498 (line, 1024)) != -1) {
499 if (!cnt)
500 break;
501 if (!strncmp
502 (line, "Location:", 9)) {
503 g_free(url);
504 url = g_strdup(line + 10);
505 }
506 }
507 else {
508 eof = TRUE;
509 mpg123_ip.set_info_text(NULL);
510 break;
511 }
512 }
513 }
514 redirect = TRUE;
515 break;
516 }
517 else {
518 status =
519 g_strdup_printf(_
520 ("Couldn't connect to host %s\nServer reported: %s"),
521 chost, status);
522 show_error_message(status);
523 g_free(status);
524 break;
525 }
526 }
527 }
528 else {
529 eof = TRUE;
530 mpg123_ip.set_info_text(NULL);
531 }
532 }
533 }
534
535 while (going && !redirect) {
536 if (http_check_for_data()) {
537 if ((cnt = mpg123_http_read_line(line, 1024)) != -1) {
538 if (!cnt)
539 break;
540 if (!strncmp(line, "icy-name:", 9))
541 icy_name = g_strdup(line + 9);
542 else if (!strncmp(line, "x-audiocast-name:", 17))
543 icy_name = g_strdup(line + 17);
544 if (!strncmp(line, "icy-metaint:", 12))
545 icy_metaint = atoi(line + 12);
546 if (!strncmp(line, "x-audiocast-udpport:", 20)) {
547 #ifdef DEBUG_UDP
548 fprintf(stderr,
549 "Server wants udp messages on port %d\n",
550 atoi(line + 20));
551 #endif
552 /* udp_serverport = atoi (line + 20); */
553 }
554
555 }
556 else {
557 eof = TRUE;
558 mpg123_ip.set_info_text(NULL);
559 break;
560 }
561 }
562 }
563 }
564 }
565
566 if (redirect) {
567 if (output_file) {
568 vfs_fclose(output_file);
569 output_file = NULL;
570 }
571 close(sock);
572 g_free(user);
573 g_free(pass);
574 g_free(host);
575 g_free(filename);
576 }
577 } while (redirect);
578
579 if (mpg123_cfg.save_http_stream) {
580 gchar *output_name;
581 gint i = 1;
582
583 file = mpg123_http_get_title(url);
584 output_name = file;
585 if (!strncasecmp(output_name, "http://", 7))
586 output_name += 7;
587 temp = strrchr(output_name, '.');
588 if (temp && !strcasecmp(temp, ".mp3"))
589 *temp = '\0';
590
591 while ((temp = strchr(output_name, '/')))
592 *temp = '_';
593 output_name = g_strdup_printf("%s/%s.mp3",
594 mpg123_cfg.save_http_path, output_name);
595 while (!access(output_name, F_OK) && i < 100000) {
596 g_free(output_name);
597 output_name = g_strdup_printf("%s/%s-%d.mp3",
598 mpg123_cfg.save_http_path,
599 output_name, i++);
600 }
601
602 g_free(file);
603
604 output_file = vfs_fopen(output_name, "wb");
605 g_free(output_name);
606 }
607
608 while (going) {
609
610 if (!http_used() && !mpg123_ip.output->buffer_playing())
611 prebuffering = TRUE;
612 if (http_free() > 0 && !eof) {
613 if (http_check_for_data()) {
614 cnt = min(http_free(), buffer_length - wr_index);
615 if (cnt > 1024)
616 cnt = 1024;
617 written = read(sock, buffer + wr_index, cnt);
618 if (written <= 0) {
619 eof = TRUE;
620 if (prebuffering) {
621 prebuffering = FALSE;
622
623 mpg123_ip.set_info_text(NULL);
624 }
625
626 }
627 else
628 wr_index = (wr_index + written) % buffer_length;
629 }
630
631 if (prebuffering) {
632 if (http_used() > prebuffer_length) {
633 prebuffering = FALSE;
634 mpg123_ip.set_info_text(NULL);
635 }
636 else {
637 status =
638 g_strdup_printf(_("PRE-BUFFERING: %dKB/%dKB"),
639 http_used() / 1024,
640 prebuffer_length / 1024);
641 mpg123_ip.set_info_text(status);
642 g_free(status);
643 }
644
645 }
646 }
647 else
648 xmms_usleep(10000);
649
650 if (mpg123_cfg.use_udp_channel && udp_sock != 0)
651 if (udp_check_for_data(udp_sock) < 0) {
652 close(udp_sock);
653 udp_sock = 0;
654 }
655 }
656 if (output_file) {
657 vfs_fclose(output_file);
658 output_file = NULL;
659 }
660 close(sock);
661 if (udp_sock != 0)
662 close(udp_sock);
663
664 g_free(user);
665 g_free(pass);
666 g_free(host);
667 g_free(filename);
668 g_free(buffer);
669 g_free(url);
670
671 return NULL;
672 }
673
674 int
675 mpg123_http_open(gchar * _url)
676 {
677 gchar *url;
678
679 url = g_strdup(_url);
680
681 rd_index = 0;
682 wr_index = 0;
683 buffer_length = mpg123_cfg.http_buffer_size * 1024;
684 prebuffer_length = (buffer_length * mpg123_cfg.http_prebuffer) / 100;
685 buffer_read = 0;
686 icy_metaint = 0;
687 prebuffering = TRUE;
688 going = TRUE;
689 eof = FALSE;
690 buffer = g_malloc(buffer_length);
691
692 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL);
693
694 return 0;
695 }
696
697 char *
698 mpg123_http_get_title(gchar * url)
699 {
700 if (icy_name)
701 return g_strdup(icy_name);
702 if (g_basename(url) && strlen(g_basename(url)) > 0)
703 return g_strdup(g_basename(url));
704 return g_strdup(url);
705 }
706
707 /* Start UDP Channel specific stuff */
708
709 /* Find a good local udp port and bind udp_sock to it, return the port */
710 static gint
711 udp_establish_listener(gint * sock)
712 {
713 struct sockaddr_in sin;
714 socklen_t sinlen = sizeof(struct sockaddr_in);
715
716 #ifdef DEBUG_UDP
717 fprintf(stderr, "Establishing udp listener\n");
718 #endif
719
720 if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
721 g_log(NULL, G_LOG_LEVEL_CRITICAL,
722 "udp_establish_listener(): unable to create socket");
723 return -1;
724 }
725
726 memset(&sin, 0, sinlen);
727 sin.sin_family = AF_INET;
728 sin.sin_addr.s_addr = g_htonl(INADDR_ANY);
729
730 if (bind(*sock, (struct sockaddr *) &sin, sinlen) < 0) {
731 g_log(NULL, G_LOG_LEVEL_CRITICAL,
732 "udp_establish_listener(): Failed to bind socket to localhost: %s",
733 strerror(errno));
734 close(*sock);
735 return -1;
736 }
737 if (fcntl(*sock, F_SETFL, O_NONBLOCK) < 0) {
738 g_log(NULL, G_LOG_LEVEL_CRITICAL,
739 "udp_establish_listener(): Failed to set flags: %s",
740 strerror(errno));
741 close(*sock);
742 return -1;
743 }
744
745 memset(&sin, 0, sinlen);
746 if (getsockname(*sock, (struct sockaddr *) &sin, &sinlen) < 0) {
747 g_log(NULL, G_LOG_LEVEL_CRITICAL,
748 "udp_establish_listener(): Failed to retrieve socket info: %s",
749 strerror(errno));
750 close(*sock);
751 return -1;
752 }
753 #ifdef DEBUG_UDP
754 fprintf(stderr, "Listening on local %s:%d\n", inet_ntoa(sin.sin_addr),
755 g_ntohs(sin.sin_port));
756 #endif
757
758 return g_ntohs(sin.sin_port);
759 }
760
761 static int
762 udp_check_for_data(int sock)
763 {
764 char buf[1025], **lines;
765 char *valptr;
766 gchar *title;
767 gint len, i;
768 struct sockaddr_in from;
769 socklen_t fromlen;
770
771 fromlen = sizeof(struct sockaddr_in);
772
773 if ((len =
774 recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &from,
775 &fromlen)) < 0) {
776 if (errno != EAGAIN) {
777 g_log(NULL, G_LOG_LEVEL_CRITICAL,
778 "udp_read_data(): Error reading from socket: %s",
779 strerror(errno));
780 return -1;
781 }
782 return 0;
783 }
784 buf[len] = '\0';
785 #ifdef DEBUG_UDP
786 fprintf(stderr, "Received: [%s]\n", buf);
787 #endif
788 lines = g_strsplit(buf, "\n", 0);
789 if (!lines)
790 return 0;
791
792 for (i = 0; lines[i]; i++) {
793 while ((lines[i][strlen(lines[i]) - 1] == '\n') ||
794 (lines[i][strlen(lines[i]) - 1] == '\r'))
795 lines[i][strlen(lines[i]) - 1] = '\0';
796
797 valptr = strchr(lines[i], ':');
798
799 if (!valptr)
800 continue;
801 else
802 valptr++;
803
804 g_strstrip(valptr);
805 if (!strlen(valptr))
806 continue;
807
808 if (strstr(lines[i], "x-audiocast-streamtitle") != NULL) {
809 title = g_strdup_printf("%s (%s)", valptr, icy_name);
810 if (going)
811 mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000,
812 mpg123_frequency, mpg123_stereo);
813 g_free(title);
814 }
815 #if 0
816 else if (strstr(lines[i], "x-audiocast-streamlength") != NULL) {
817 if (atoi(valptr) != -1)
818 mpg123_ip.set_info(NULL, atoi(valptr),
819 mpg123_bitrate * 1000, mpg123_frequency,
820 mpg123_stereo);
821 }
822 #endif
823
824 else if (strstr(lines[i], "x-audiocast-streammsg") != NULL) {
825 /* mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000, mpg123_frequency, mpg123_stereo); */
826 /* xmms_show_message(_("Message"), valptr, _("Ok"), */
827 /* FALSE, NULL, NULL); */
828 g_message("Stream_message: %s", valptr);
829 }
830 #if 0
831 /* Use this to direct your webbrowser.. yeah right.. */
832 else if (strstr(lines[i], "x-audiocast-streamurl") != NULL) {
833 if (lasturl && g_strcmp(valptr, lasturl)) {
834 c_message(stderr, "Song URL: %s\n", valptr);
835 g_free(lasturl);
836 lasturl = g_strdup(valptr);
837 }
838 }
839 #endif
840 else if (strstr(lines[i], "x-audiocast-udpseqnr:") != NULL) {
841 gchar obuf[60];
842 sprintf(obuf, "x-audiocast-ack: %ld \r\n", atol(valptr));
843 if (sendto
844 (sock, obuf, strlen(obuf), 0, (struct sockaddr *) &from,
845 fromlen) < 0) {
846 g_log(NULL, G_LOG_LEVEL_WARNING,
847 "udp_check_for_data(): Unable to send ack to server: %s",
848 strerror(errno));
849 }
850 #ifdef DEBUG_UDP
851 else
852 fprintf(stderr, "Sent ack: %s", obuf);
853 fprintf(stderr, "Remote: %s:%d\n", inet_ntoa(from.sin_addr),
854 g_ntohs(from.sin_port));
855 #endif
856 }
857 }
858 g_strfreev(lines);
859 return 0;
860 }