aac: Collect all interesting ID3 frames
[cmus.git] / input.c
blob27353bff57733aab56b7d472e577c6dd92c4f6e2
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
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
17 * 02111-1307, USA.
20 #include "input.h"
21 #include "ip.h"
22 #include "pcm.h"
23 #include "http.h"
24 #include "xmalloc.h"
25 #include "file.h"
26 #include "utils.h"
27 #include "cmus.h"
28 #include "list.h"
29 #include "misc.h"
30 #include "debug.h"
31 #include "ui_curses.h"
32 #include "config/libdir.h"
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <dirent.h>
42 #include <dlfcn.h>
44 struct input_plugin {
45 const struct input_plugin_ops *ops;
46 struct input_plugin_data data;
47 unsigned int open : 1;
48 unsigned int eof : 1;
49 int http_code;
50 char *http_reason;
52 /* cached duration, -1 = unset */
53 int duration;
56 * pcm is converted to 16-bit signed little-endian stereo
57 * NOTE: no conversion is done if channels > 2 or bits > 16
59 void (*pcm_convert)(char *, const char *, int);
60 void (*pcm_convert_in_place)(char *, int);
62 * 4 if 8-bit mono
63 * 2 if 8-bit stereo or 16-bit mono
64 * 1 otherwise
66 int pcm_convert_scale;
69 struct ip {
70 struct list_head node;
71 char *name;
72 void *handle;
74 const char * const *extensions;
75 const char * const *mime_types;
76 const struct input_plugin_ops *ops;
79 static const char * const plugin_dir = LIBDIR "/cmus/ip";
80 static LIST_HEAD(ip_head);
82 /* timeouts (ms) */
83 static int http_connection_timeout = 5e3;
84 static int http_read_timeout = 5e3;
86 static const char *pl_mime_types[] = {
87 "audio/m3u",
88 "audio/x-scpls",
89 "audio/x-mpegurl"
92 static const char *get_extension(const char *filename)
94 const char *ext;
96 ext = filename + strlen(filename) - 1;
97 while (ext >= filename && *ext != '/') {
98 if (*ext == '.') {
99 ext++;
100 return ext;
102 ext--;
104 return NULL;
107 static const struct input_plugin_ops *get_ops_by_filename(const char *filename)
109 struct ip *ip;
110 const char *ext;
112 ext = get_extension(filename);
113 if (ext == NULL)
114 return NULL;
115 list_for_each_entry(ip, &ip_head, node) {
116 const char * const *exts = ip->extensions;
117 int i;
119 for (i = 0; exts[i]; i++) {
120 if (strcasecmp(ext, exts[i]) == 0)
121 return ip->ops;
124 return NULL;
127 static const struct input_plugin_ops *get_ops_by_mime_type(const char *mime_type)
129 struct ip *ip;
131 list_for_each_entry(ip, &ip_head, node) {
132 const char * const *types = ip->mime_types;
133 int i;
135 for (i = 0; types[i]; i++) {
136 if (strcasecmp(mime_type, types[i]) == 0)
137 return ip->ops;
140 return NULL;
143 static int do_http_get(const char *uri, struct http_header **headersp, int *codep, char **reasonp)
145 struct http_uri u;
146 struct http_header *h;
147 int sock, i, rc, code;
148 char *reason;
150 *headersp = NULL;
151 *codep = -1;
152 *reasonp = NULL;
154 if (http_parse_uri(uri, &u))
155 return -IP_ERROR_INVALID_URI;
157 /* d_print("%s -> '%s':'%s'@'%s':%d'%s'\n", uri, user, pass, host, port, path); */
159 sock = http_open(u.host, u.port, http_connection_timeout);
160 if (sock == -1) {
161 http_free_uri(&u);
162 return -IP_ERROR_ERRNO;
165 h = xnew(struct http_header, 5);
166 i = 0;
167 h[i].key = xstrdup("Host");
168 h[i].val = xstrdup(u.host);
169 i++;
170 h[i].key = xstrdup("User-Agent");
171 h[i].val = xstrdup("cmus/" VERSION);
172 i++;
173 h[i].key = xstrdup("Icy-MetaData");
174 h[i].val = xstrdup("1");
175 i++;
176 if (u.user && u.pass) {
177 char buf[256];
178 char *encoded;
180 snprintf(buf, sizeof(buf), "%s:%s", u.user, u.pass);
181 encoded = base64_encode(buf);
182 if (encoded == NULL) {
183 d_print("couldn't base64 encode '%s'\n", buf);
184 } else {
185 snprintf(buf, sizeof(buf), "Basic %s", encoded);
186 free(encoded);
187 h[i].key = xstrdup("Authorization");
188 h[i].val = xstrdup(buf);
189 i++;
192 h[i].key = NULL;
193 h[i].val = NULL;
194 i++;
196 rc = http_get(sock, u.path, h, &code, &reason, headersp, http_read_timeout);
197 http_headers_free(h);
198 http_free_uri(&u);
199 switch (rc) {
200 case -1:
201 d_print("error: %s\n", strerror(errno));
202 close(sock);
203 return -IP_ERROR_ERRNO;
204 case -2:
205 d_print("error parsing HTTP response\n");
206 close(sock);
207 return -IP_ERROR_HTTP_RESPONSE;
209 d_print("HTTP response: %d %s\n", code, reason);
210 if (code != 200) {
211 *codep = code;
212 *reasonp = reason;
213 close(sock);
214 return -IP_ERROR_HTTP_STATUS;
216 free(reason);
217 return sock;
220 static int setup_remote(struct input_plugin *ip, const struct http_header *headers, int sock)
222 const char *val;
224 val = http_headers_get_value(headers, "Content-Type");
225 if (val) {
226 d_print("Content-Type: %s\n", val);
227 ip->ops = get_ops_by_mime_type(val);
228 if (ip->ops == NULL) {
229 d_print("unsupported content type: %s\n", val);
230 close(sock);
231 return -IP_ERROR_FILE_FORMAT;
233 } else {
234 const char *type = "audio/mpeg";
236 d_print("assuming %s content type\n", type);
237 ip->ops = get_ops_by_mime_type(type);
238 if (ip->ops == NULL) {
239 d_print("unsupported content type: %s\n", type);
240 close(sock);
241 return -IP_ERROR_FILE_FORMAT;
245 ip->data.fd = sock;
246 ip->data.metadata = (char *)xmalloc(16 * 255 + 1);
248 val = http_headers_get_value(headers, "icy-metaint");
249 if (val) {
250 long int lint;
252 if (str_to_int(val, &lint) == 0 && lint >= 0) {
253 ip->data.metaint = lint;
254 d_print("metaint: %d\n", ip->data.metaint);
257 return 0;
260 static int handle_line(void *data, const char *line)
262 char **firstp = data;
264 *firstp = xstrdup(line);
265 /* ignore other lines */
266 return 1;
269 static int read_playlist(struct input_plugin *ip, int sock)
271 struct http_header *headers;
272 char *body, *reason, *first;
273 int rc, code;
275 rc = http_read_body(sock, &body, http_read_timeout);
276 close(sock);
277 if (rc)
278 return -IP_ERROR_ERRNO;
280 /* get only first URL from the playlist */
281 first = NULL;
282 cmus_playlist_for_each(body, strlen(body), 0, handle_line, &first);
283 free(body);
285 if (first == NULL) {
286 d_print("empty playlist\n");
287 return -IP_ERROR_HTTP_RESPONSE;
290 sock = do_http_get(first, &headers, &code, &reason);
291 free(first);
292 if (sock < 0) {
293 ip->http_code = code;
294 ip->http_reason = reason;
295 /* URI in the _playlist_ is invalid, not our fault */
296 if (sock == -IP_ERROR_INVALID_URI)
297 sock = -IP_ERROR_HTTP_RESPONSE;
298 return sock;
301 rc = setup_remote(ip, headers, sock);
302 http_headers_free(headers);
303 return rc;
306 static int open_remote(struct input_plugin *ip)
308 struct input_plugin_data *d = &ip->data;
309 char *reason;
310 int sock, rc, code;
311 struct http_header *headers;
312 const char *val;
314 sock = do_http_get(d->filename, &headers, &code, &reason);
315 if (sock < 0) {
316 ip->http_code = code;
317 ip->http_reason = reason;
318 return sock;
321 val = http_headers_get_value(headers, "Content-Type");
322 if (val) {
323 int i;
325 for (i = 0; i < sizeof(pl_mime_types) / sizeof(pl_mime_types[0]); i++) {
326 if (!strcasecmp(val, pl_mime_types[i])) {
327 d_print("Content-Type: %s\n", val);
328 http_headers_free(headers);
329 return read_playlist(ip, sock);
334 rc = setup_remote(ip, headers, sock);
335 http_headers_free(headers);
336 return rc;
339 static int open_file(struct input_plugin *ip)
341 ip->ops = get_ops_by_filename(ip->data.filename);
342 if (ip->ops == NULL)
343 return -IP_ERROR_UNRECOGNIZED_FILE_TYPE;
344 ip->data.fd = open(ip->data.filename, O_RDONLY);
345 if (ip->data.fd == -1) {
346 ip->ops = NULL;
347 return -IP_ERROR_ERRNO;
349 return 0;
352 void ip_load_plugins(void)
354 DIR *dir;
355 struct dirent *d;
357 dir = opendir(plugin_dir);
358 if (dir == NULL) {
359 error_msg("couldn't open directory `%s': %s", plugin_dir, strerror(errno));
360 return;
362 while ((d = readdir(dir)) != NULL) {
363 char filename[256];
364 struct ip *ip;
365 void *so;
366 char *ext;
367 const char *sym;
369 if (d->d_name[0] == '.')
370 continue;
371 ext = strrchr(d->d_name, '.');
372 if (ext == NULL)
373 continue;
374 if (strcmp(ext, ".so"))
375 continue;
377 snprintf(filename, sizeof(filename), "%s/%s", plugin_dir, d->d_name);
379 so = dlopen(filename, RTLD_NOW);
380 if (so == NULL) {
381 error_msg("%s", dlerror());
382 continue;
385 ip = xnew(struct ip, 1);
387 sym = "ip_extensions";
388 if (!(ip->extensions = dlsym(so, sym)))
389 goto sym_err;
391 sym = "ip_mime_types";
392 if (!(ip->mime_types = dlsym(so, sym)))
393 goto sym_err;
395 sym = "ip_ops";
396 if (!(ip->ops = dlsym(so, sym)))
397 goto sym_err;
399 ip->name = xstrndup(d->d_name, ext - d->d_name);
400 ip->handle = so;
402 list_add_tail(&ip->node, &ip_head);
403 continue;
404 sym_err:
405 error_msg("%s: symbol %s not found", filename, sym);
406 free(ip);
407 dlclose(so);
409 closedir(dir);
412 static void ip_init(struct input_plugin *ip, char *filename)
414 memset(ip, 0, sizeof(*ip));
415 ip->http_code = -1;
416 ip->pcm_convert_scale = -1;
417 ip->duration = -1;
418 ip->data.fd = -1;
419 ip->data.filename = filename;
420 ip->data.remote = is_url(filename);
423 struct input_plugin *ip_new(const char *filename)
425 struct input_plugin *ip = xnew(struct input_plugin, 1);
427 ip_init(ip, xstrdup(filename));
428 return ip;
431 void ip_delete(struct input_plugin *ip)
433 if (ip->open)
434 ip_close(ip);
435 free(ip->data.filename);
436 free(ip);
439 int ip_open(struct input_plugin *ip)
441 int rc;
443 BUG_ON(ip->open);
445 /* set fd and ops */
446 if (ip->data.remote) {
447 rc = open_remote(ip);
448 } else {
449 rc = open_file(ip);
452 if (rc) {
453 d_print("opening `%s' failed: %d %s\n", ip->data.filename, rc,
454 rc == -1 ? strerror(errno) : "");
455 return rc;
458 rc = ip->ops->open(&ip->data);
459 if (rc) {
460 d_print("opening file `%s' failed: %d %s\n", ip->data.filename, rc,
461 rc == -1 ? strerror(errno) : "");
462 if (ip->data.fd != -1)
463 close(ip->data.fd);
464 free(ip->data.metadata);
465 ip_init(ip, ip->data.filename);
466 return rc;
468 ip->open = 1;
469 return 0;
472 void ip_setup(struct input_plugin *ip)
474 unsigned int bits, is_signed, channels;
475 sample_format_t sf = ip->data.sf;
477 bits = sf_get_bits(sf);
478 is_signed = sf_get_signed(sf);
479 channels = sf_get_channels(sf);
481 ip->pcm_convert_scale = 1;
482 ip->pcm_convert = NULL;
483 ip->pcm_convert_in_place = NULL;
485 if (bits <= 16 && channels <= 2) {
486 unsigned int mask = ((bits >> 2) & 4) | (is_signed << 1);
488 ip->pcm_convert = pcm_conv[mask | (channels - 1)];
489 ip->pcm_convert_in_place = pcm_conv_in_place[mask | sf_get_bigendian(sf)];
491 ip->pcm_convert_scale = (3 - channels) * (3 - bits / 8);
494 d_print("pcm convert: scale=%d convert=%d convert_in_place=%d\n",
495 ip->pcm_convert_scale,
496 ip->pcm_convert != NULL,
497 ip->pcm_convert_in_place != NULL);
500 int ip_close(struct input_plugin *ip)
502 int rc;
504 rc = ip->ops->close(&ip->data);
505 BUG_ON(ip->data.private);
506 if (ip->data.fd != -1)
507 close(ip->data.fd);
508 free(ip->data.metadata);
509 free(ip->http_reason);
511 ip_init(ip, ip->data.filename);
512 return rc;
515 int ip_read(struct input_plugin *ip, char *buffer, int count)
517 struct timeval tv;
518 fd_set readfds;
519 /* 4608 seems to be optimal for mp3s, 4096 for oggs */
520 char tmp[8 * 1024];
521 char *buf;
522 int sample_size;
523 int rc;
525 BUG_ON(count <= 0);
527 FD_ZERO(&readfds);
528 FD_SET(ip->data.fd, &readfds);
529 /* zero timeout -> return immediately */
530 tv.tv_sec = 0;
531 tv.tv_usec = 50e3;
532 rc = select(ip->data.fd + 1, &readfds, NULL, NULL, &tv);
533 if (rc == -1) {
534 if (errno == EINTR)
535 errno = EAGAIN;
536 return -1;
538 if (rc == 0) {
539 errno = EAGAIN;
540 return -1;
543 buf = buffer;
544 if (ip->pcm_convert_scale > 1) {
545 /* use tmp buffer for 16-bit mono and 8-bit */
546 buf = tmp;
547 count /= ip->pcm_convert_scale;
548 if (count > sizeof(tmp))
549 count = sizeof(tmp);
552 rc = ip->ops->read(&ip->data, buf, count);
553 if (rc == -1 && (errno == EAGAIN || errno == EINTR)) {
554 errno = EAGAIN;
555 return -1;
557 if (rc <= 0) {
558 ip->eof = 1;
559 return rc;
562 BUG_ON((rc & ~((unsigned int)sf_get_frame_size(ip->data.sf) - 1U)) != rc);
564 sample_size = sf_get_sample_size(ip->data.sf);
565 if (ip->pcm_convert_in_place != NULL)
566 ip->pcm_convert_in_place(buf, rc / sample_size);
567 if (ip->pcm_convert != NULL)
568 ip->pcm_convert(buffer, tmp, rc / sample_size);
569 return rc * ip->pcm_convert_scale;
572 int ip_seek(struct input_plugin *ip, double offset)
574 int rc;
576 if (ip->data.remote)
577 return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
578 rc = ip->ops->seek(&ip->data, offset);
579 if (rc == 0)
580 ip->eof = 0;
581 return rc;
584 int ip_read_comments(struct input_plugin *ip, struct keyval **comments)
586 return ip->ops->read_comments(&ip->data, comments);
589 int ip_duration(struct input_plugin *ip)
591 if (ip->data.remote)
592 return -1;
593 if (ip->duration == -1)
594 ip->duration = ip->ops->duration(&ip->data);
595 if (ip->duration < 0)
596 return -1;
597 return ip->duration;
600 sample_format_t ip_get_sf(struct input_plugin *ip)
602 BUG_ON(!ip->open);
603 return ip->data.sf;
606 const char *ip_get_filename(struct input_plugin *ip)
608 return ip->data.filename;
611 const char *ip_get_metadata(struct input_plugin *ip)
613 BUG_ON(!ip->open);
614 return ip->data.metadata;
617 int ip_is_remote(struct input_plugin *ip)
619 return ip->data.remote;
622 int ip_metadata_changed(struct input_plugin *ip)
624 int ret = ip->data.metadata_changed;
626 BUG_ON(!ip->open);
627 ip->data.metadata_changed = 0;
628 return ret;
631 int ip_eof(struct input_plugin *ip)
633 BUG_ON(!ip->open);
634 return ip->eof;
637 char *ip_get_error_msg(struct input_plugin *ip, int rc, const char *arg)
639 char buffer[1024];
641 switch (-rc) {
642 case IP_ERROR_ERRNO:
643 snprintf(buffer, sizeof(buffer), "%s: %s", arg, strerror(errno));
644 break;
645 case IP_ERROR_UNRECOGNIZED_FILE_TYPE:
646 snprintf(buffer, sizeof(buffer),
647 "%s: unrecognized filename extension", arg);
648 break;
649 case IP_ERROR_FUNCTION_NOT_SUPPORTED:
650 snprintf(buffer, sizeof(buffer),
651 "%s: function not supported", arg);
652 break;
653 case IP_ERROR_FILE_FORMAT:
654 snprintf(buffer, sizeof(buffer),
655 "%s: file format not supported or corrupted file",
656 arg);
657 break;
658 case IP_ERROR_INVALID_URI:
659 snprintf(buffer, sizeof(buffer), "%s: invalid URI", arg);
660 break;
661 case IP_ERROR_SAMPLE_FORMAT:
662 snprintf(buffer, sizeof(buffer),
663 "%s: input plugin doesn't support the sample format",
664 arg);
665 break;
666 case IP_ERROR_HTTP_RESPONSE:
667 snprintf(buffer, sizeof(buffer), "%s: invalid HTTP response", arg);
668 break;
669 case IP_ERROR_HTTP_STATUS:
670 snprintf(buffer, sizeof(buffer), "%s: %d %s", arg, ip->http_code, ip->http_reason);
671 free(ip->http_reason);
672 ip->http_reason = NULL;
673 ip->http_code = -1;
674 break;
675 case IP_ERROR_INTERNAL:
676 snprintf(buffer, sizeof(buffer), "%s: internal error", arg);
677 break;
678 case IP_ERROR_SUCCESS:
679 default:
680 snprintf(buffer, sizeof(buffer),
681 "%s: this is not an error (%d), this is a bug",
682 arg, rc);
683 break;
685 return xstrdup(buffer);
688 char **ip_get_supported_extensions(void)
690 struct ip *ip;
691 char **exts;
692 int i, size;
693 int count = 0;
695 size = 8;
696 exts = xnew(char *, size);
697 list_for_each_entry(ip, &ip_head, node) {
698 const char * const *e = ip->extensions;
700 for (i = 0; e[i]; i++) {
701 if (count == size - 1) {
702 size *= 2;
703 exts = xrenew(char *, exts, size);
705 exts[count++] = xstrdup(e[i]);
708 exts[count] = NULL;
709 qsort(exts, count, sizeof(char *), strptrcmp);
710 return exts;
713 void ip_dump_plugins(void)
715 struct ip *ip;
716 int i;
718 printf("Input Plugins: %s\n", plugin_dir);
719 list_for_each_entry(ip, &ip_head, node) {
720 printf(" %s:\n File Types:", ip->name);
721 for (i = 0; ip->extensions[i]; i++)
722 printf(" %s", ip->extensions[i]);
723 printf("\n MIME Types:");
724 for (i = 0; ip->mime_types[i]; i++)
725 printf(" %s", ip->mime_types[i]);
726 printf("\n");