2 * Copyright 2004-2005 Timo Hirvonen
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
32 #include "config/libdir.h"
33 #include "config/version.h"
39 #include <sys/types.h>
46 const struct input_plugin_ops
*ops
;
47 struct input_plugin_data data
;
48 unsigned int open
: 1;
54 * pcm is converted to 16-bit signed little-endian stereo
55 * NOTE: no conversion is done if channels > 2 or bits > 16
57 void (*pcm_convert
)(char *, const char *, int);
58 void (*pcm_convert_in_place
)(char *, int);
61 * 2 if 8-bit stereo or 16-bit mono
64 int pcm_convert_scale
;
68 struct list_head node
;
72 const char * const *extensions
;
73 const char * const *mime_types
;
74 const struct input_plugin_ops
*ops
;
77 static const char * const plugin_dir
= LIBDIR
"/cmus/ip";
78 static LIST_HEAD(ip_head
);
81 static int http_connection_timeout
= 5e3
;
82 static int http_read_timeout
= 5e3
;
84 static const char *get_extension(const char *filename
)
88 ext
= filename
+ strlen(filename
) - 1;
89 while (ext
>= filename
&& *ext
!= '/') {
99 static const struct input_plugin_ops
*get_ops_by_filename(const char *filename
)
104 ext
= get_extension(filename
);
107 list_for_each_entry(ip
, &ip_head
, node
) {
108 const char * const *exts
= ip
->extensions
;
111 for (i
= 0; exts
[i
]; i
++) {
112 if (strcasecmp(ext
, exts
[i
]) == 0)
119 static const struct input_plugin_ops
*get_ops_by_mime_type(const char *mime_type
)
123 list_for_each_entry(ip
, &ip_head
, node
) {
124 const char * const *types
= ip
->mime_types
;
127 for (i
= 0; types
[i
]; i
++) {
128 if (strcasecmp(mime_type
, types
[i
]) == 0)
135 static int do_http_get(const char *uri
, struct http_header
**headersp
, int *codep
, char **reasonp
)
138 struct http_header
*h
;
139 int sock
, i
, rc
, code
;
146 if (http_parse_uri(uri
, &u
))
147 return -IP_ERROR_INVALID_URI
;
149 /* d_print("%s -> '%s':'%s'@'%s':%d'%s'\n", uri, user, pass, host, port, path); */
151 sock
= http_open(u
.host
, u
.port
, http_connection_timeout
);
154 return -IP_ERROR_ERRNO
;
157 h
= xnew(struct http_header
, 5);
159 h
[i
].key
= xstrdup("Host");
160 h
[i
].val
= xstrdup(u
.host
);
162 h
[i
].key
= xstrdup("User-Agent");
163 h
[i
].val
= xstrdup("cmus/" VERSION
);
165 h
[i
].key
= xstrdup("Icy-MetaData");
166 h
[i
].val
= xstrdup("1");
168 if (u
.user
&& u
.pass
) {
172 snprintf(buf
, sizeof(buf
), "%s:%s", u
.user
, u
.pass
);
173 encoded
= base64_encode(buf
);
174 if (encoded
== NULL
) {
175 d_print("couldn't base64 encode '%s'\n", buf
);
177 snprintf(buf
, sizeof(buf
), "Basic %s", encoded
);
179 h
[i
].key
= xstrdup("Authorization");
180 h
[i
].val
= xstrdup(buf
);
188 rc
= http_get(sock
, u
.path
, h
, &code
, &reason
, headersp
, http_read_timeout
);
189 http_headers_free(h
);
193 d_print("error: %s\n", strerror(errno
));
195 return -IP_ERROR_ERRNO
;
197 d_print("error parsing HTTP response\n");
199 return -IP_ERROR_HTTP_RESPONSE
;
201 d_print("HTTP response: %d %s\n", code
, reason
);
206 return -IP_ERROR_HTTP_STATUS
;
212 static int setup_remote(struct input_plugin
*ip
, const struct http_header
*headers
, int sock
)
216 val
= http_headers_get_value(headers
, "Content-Type");
218 ip
->ops
= get_ops_by_mime_type(val
);
219 if (ip
->ops
== NULL
) {
220 d_print("unsupported content type: %s\n", val
);
222 return -IP_ERROR_FILE_FORMAT
;
225 const char *type
= "audio/mpeg";
227 d_print("assuming %s content type\n", type
);
228 ip
->ops
= get_ops_by_mime_type(type
);
229 if (ip
->ops
== NULL
) {
230 d_print("unsupported content type: %s\n", type
);
232 return -IP_ERROR_FILE_FORMAT
;
237 ip
->data
.metadata
= (char *)xmalloc(16 * 255 + 1);
239 val
= http_headers_get_value(headers
, "icy-metaint");
243 if (str_to_int(val
, &lint
) == 0 && lint
>= 0) {
244 ip
->data
.metaint
= lint
;
245 d_print("metaint: %d\n", ip
->data
.metaint
);
251 static int handle_line(void *data
, const char *line
)
253 char **firstp
= data
;
255 *firstp
= xstrdup(line
);
256 /* ignore other lines */
260 static int read_playlist(struct input_plugin
*ip
, int sock
)
262 struct http_header
*headers
;
263 char *body
, *reason
, *first
;
266 rc
= http_read_body(sock
, &body
, http_read_timeout
);
269 return -IP_ERROR_ERRNO
;
271 /* get only first URL from the playlist */
273 cmus_playlist_for_each(body
, strlen(body
), 0, handle_line
, &first
);
277 d_print("empty playlist\n");
278 return -IP_ERROR_HTTP_RESPONSE
;
281 sock
= do_http_get(first
, &headers
, &code
, &reason
);
284 ip
->http_code
= code
;
285 ip
->http_reason
= reason
;
286 /* URI in the _playlist_ is invalid, not our fault */
287 if (sock
== -IP_ERROR_INVALID_URI
)
288 sock
= -IP_ERROR_HTTP_RESPONSE
;
292 rc
= setup_remote(ip
, headers
, sock
);
293 http_headers_free(headers
);
297 static int open_remote(struct input_plugin
*ip
)
299 struct input_plugin_data
*d
= &ip
->data
;
302 struct http_header
*headers
;
305 sock
= do_http_get(d
->filename
, &headers
, &code
, &reason
);
307 ip
->http_code
= code
;
308 ip
->http_reason
= reason
;
312 val
= http_headers_get_value(headers
, "Content-Type");
314 d_print("Content-Type: %s\n", val
);
315 if (!strcasecmp(val
, "audio/x-scpls") || !strcasecmp(val
, "audio/m3u")) {
316 http_headers_free(headers
);
317 return read_playlist(ip
, sock
);
321 rc
= setup_remote(ip
, headers
, sock
);
322 http_headers_free(headers
);
326 static int open_file(struct input_plugin
*ip
)
328 ip
->ops
= get_ops_by_filename(ip
->data
.filename
);
330 return -IP_ERROR_UNRECOGNIZED_FILE_TYPE
;
331 ip
->data
.fd
= open(ip
->data
.filename
, O_RDONLY
);
332 if (ip
->data
.fd
== -1) {
334 return -IP_ERROR_ERRNO
;
339 void ip_load_plugins(void)
344 dir
= opendir(plugin_dir
);
346 warn_errno("couldn't open directory `%s'", plugin_dir
);
349 while ((d
= readdir(dir
)) != NULL
) {
356 if (d
->d_name
[0] == '.')
358 ext
= strrchr(d
->d_name
, '.');
361 if (strcmp(ext
, ".so"))
364 snprintf(filename
, sizeof(filename
), "%s/%s", plugin_dir
, d
->d_name
);
366 so
= dlopen(filename
, RTLD_NOW
);
368 warn("%s\n", dlerror());
372 ip
= xnew(struct ip
, 1);
374 sym
= "ip_extensions";
375 if (!(ip
->extensions
= dlsym(so
, sym
)))
378 sym
= "ip_mime_types";
379 if (!(ip
->mime_types
= dlsym(so
, sym
)))
383 if (!(ip
->ops
= dlsym(so
, sym
)))
386 ip
->name
= xstrndup(d
->d_name
, ext
- d
->d_name
);
389 list_add_tail(&ip
->node
, &ip_head
);
392 warn("%s: symbol %s not found\n", filename
, sym
);
399 static void ip_init(struct input_plugin
*ip
, char *filename
)
401 memset(ip
, 0, sizeof(*ip
));
403 ip
->pcm_convert_scale
= -1;
405 ip
->data
.filename
= filename
;
406 ip
->data
.remote
= is_url(filename
);
409 struct input_plugin
*ip_new(const char *filename
)
411 struct input_plugin
*ip
= xnew(struct input_plugin
, 1);
413 ip_init(ip
, xstrdup(filename
));
417 void ip_delete(struct input_plugin
*ip
)
421 free(ip
->data
.filename
);
425 int ip_open(struct input_plugin
*ip
)
427 unsigned int bits
, is_signed
, channels
;
434 if (ip
->data
.remote
) {
435 rc
= open_remote(ip
);
441 d_print("opening `%s' failed: %d %s\n", ip
->data
.filename
, rc
,
442 rc
== -1 ? strerror(errno
) : "");
446 rc
= ip
->ops
->open(&ip
->data
);
448 d_print("opening file `%s' failed: %d %s\n", ip
->data
.filename
, rc
,
449 rc
== -1 ? strerror(errno
) : "");
450 if (ip
->data
.fd
!= -1)
454 free(ip
->data
.metadata
);
455 ip
->data
.metadata
= NULL
;
461 bits
= sf_get_bits(sf
);
462 is_signed
= sf_get_signed(sf
);
463 channels
= sf_get_channels(sf
);
465 ip
->pcm_convert_scale
= 1;
466 ip
->pcm_convert
= NULL
;
467 ip
->pcm_convert_in_place
= NULL
;
469 if (bits
<= 16 && channels
<= 2) {
470 unsigned int mask
= ((bits
>> 2) & 4) | (is_signed
<< 1);
472 ip
->pcm_convert
= pcm_conv
[mask
| (channels
- 1)];
473 ip
->pcm_convert_in_place
= pcm_conv_in_place
[mask
| sf_get_bigendian(sf
)];
475 ip
->pcm_convert_scale
= (3 - channels
) * (3 - bits
/ 8);
478 d_print("pcm convert: scale=%d convert=%d convert_in_place=%d\n",
479 ip
->pcm_convert_scale
,
480 ip
->pcm_convert
!= NULL
,
481 ip
->pcm_convert_in_place
!= NULL
);
487 int ip_close(struct input_plugin
*ip
)
491 rc
= ip
->ops
->close(&ip
->data
);
492 BUG_ON(ip
->data
.private);
493 if (ip
->data
.fd
!= -1)
495 free(ip
->data
.metadata
);
496 free(ip
->http_reason
);
498 ip_init(ip
, ip
->data
.filename
);
502 int ip_read(struct input_plugin
*ip
, char *buffer
, int count
)
506 /* 4608 seems to be optimal for mp3s, 4096 for oggs */
514 FD_SET(ip
->data
.fd
, &readfds
);
515 /* zero timeout -> return immediately */
518 rc
= select(ip
->data
.fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
520 d_print("select: error: %s\n", strerror(errno
));
531 if (ip
->pcm_convert_scale
> 1) {
532 /* use tmp buffer for 16-bit mono and 8-bit */
534 count
/= ip
->pcm_convert_scale
;
535 if (count
> sizeof(tmp
))
539 rc
= ip
->ops
->read(&ip
->data
, buf
, count
);
543 d_print("error: %s\n", strerror(errno
));
546 int sample_size
= sf_get_sample_size(ip
->data
.sf
);
548 if (ip
->pcm_convert_in_place
!= NULL
)
549 ip
->pcm_convert_in_place(buf
, rc
/ sample_size
);
550 if (ip
->pcm_convert
!= NULL
)
551 ip
->pcm_convert(buffer
, tmp
, rc
/ sample_size
);
552 rc
*= ip
->pcm_convert_scale
;
557 int ip_seek(struct input_plugin
*ip
, double offset
)
562 return -IP_ERROR_FUNCTION_NOT_SUPPORTED
;
563 rc
= ip
->ops
->seek(&ip
->data
, offset
);
569 int ip_read_comments(struct input_plugin
*ip
, struct keyval
**comments
)
571 return ip
->ops
->read_comments(&ip
->data
, comments
);
574 int ip_duration(struct input_plugin
*ip
)
576 return ip
->ops
->duration(&ip
->data
);
579 sample_format_t
ip_get_sf(struct input_plugin
*ip
)
585 const char *ip_get_filename(struct input_plugin
*ip
)
587 return ip
->data
.filename
;
590 const char *ip_get_metadata(struct input_plugin
*ip
)
593 return ip
->data
.metadata
;
596 int ip_is_remote(struct input_plugin
*ip
)
598 return ip
->data
.remote
;
601 int ip_metadata_changed(struct input_plugin
*ip
)
603 int ret
= ip
->data
.metadata_changed
;
606 ip
->data
.metadata_changed
= 0;
610 int ip_eof(struct input_plugin
*ip
)
616 char *ip_get_error_msg(struct input_plugin
*ip
, int rc
, const char *arg
)
622 snprintf(buffer
, sizeof(buffer
), "%s: %s", arg
, strerror(errno
));
624 case IP_ERROR_UNRECOGNIZED_FILE_TYPE
:
625 snprintf(buffer
, sizeof(buffer
),
626 "%s: unrecognized filename extension", arg
);
628 case IP_ERROR_FUNCTION_NOT_SUPPORTED
:
629 snprintf(buffer
, sizeof(buffer
),
630 "%s: function not supported", arg
);
632 case IP_ERROR_FILE_FORMAT
:
633 snprintf(buffer
, sizeof(buffer
),
634 "%s: file format not supported or corrupted file",
637 case IP_ERROR_INVALID_URI
:
638 snprintf(buffer
, sizeof(buffer
), "%s: invalid URI", arg
);
640 case IP_ERROR_SAMPLE_FORMAT
:
641 snprintf(buffer
, sizeof(buffer
),
642 "%s: input plugin doesn't support the sample format",
645 case IP_ERROR_HTTP_RESPONSE
:
646 snprintf(buffer
, sizeof(buffer
), "%s: invalid HTTP response", arg
);
648 case IP_ERROR_HTTP_STATUS
:
649 snprintf(buffer
, sizeof(buffer
), "%s: %d %s", arg
, ip
->http_code
, ip
->http_reason
);
650 free(ip
->http_reason
);
651 ip
->http_reason
= NULL
;
654 case IP_ERROR_INTERNAL
:
655 snprintf(buffer
, sizeof(buffer
), "%s: internal error", arg
);
657 case IP_ERROR_SUCCESS
:
659 snprintf(buffer
, sizeof(buffer
),
660 "%s: this is not an error (%d), this is a bug",
664 return xstrdup(buffer
);
667 char **ip_get_supported_extensions(void)
675 exts
= xnew(char *, size
);
676 list_for_each_entry(ip
, &ip_head
, node
) {
677 const char * const *e
= ip
->extensions
;
679 for (i
= 0; e
[i
]; i
++) {
680 if (count
== size
- 1) {
682 exts
= xrenew(char *, exts
, size
);
684 exts
[count
++] = xstrdup(e
[i
]);
688 qsort(exts
, count
, sizeof(char *), strptrcmp
);
692 void ip_dump_plugins(void)
697 printf("Input Plugins: %s\n", plugin_dir
);
698 list_for_each_entry(ip
, &ip_head
, node
) {
699 printf(" %s:\n File Types:", ip
->name
);
700 for (i
= 0; ip
->extensions
[i
]; i
++)
701 printf(" %s", ip
->extensions
[i
]);
702 printf("\n MIME Types:");
703 for (i
= 0; ip
->mime_types
[i
]; i
++)
704 printf(" %s", ip
->mime_types
[i
]);