aboutsummaryrefslogtreecommitdiffstats
path: root/mpd-notification.c
diff options
context:
space:
mode:
Diffstat (limited to 'mpd-notification.c')
-rw-r--r--mpd-notification.c44
1 files changed, 33 insertions, 11 deletions
diff --git a/mpd-notification.c b/mpd-notification.c
index 543f60a..194655b 100644
--- a/mpd-notification.c
+++ b/mpd-notification.c
@@ -71,14 +71,34 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) {
#ifdef HAVE_LIBAV
int i;
- AVPacket pkt;
- AVFormatContext * pFormatCtx;
+ magic_t magic = NULL;
+ const char *magic_mime;
+ AVFormatContext * pFormatCtx = NULL;
GdkPixbufLoader * loader;
/* try album artwork first */
uri_path = malloc(strlen(music_dir) + strlen(uri) + 2);
sprintf(uri_path, "%s/%s", music_dir, uri);
+ if ((magic = magic_open(MAGIC_MIME_TYPE)) == NULL) {
+ fprintf(stderr, "unable to initialize magic library\n");
+ goto image;
+ }
+
+ if (magic_load(magic, NULL) != 0) {
+ fprintf(stderr, "cannot load magic database: %s\n", magic_error(magic));
+ magic_close(magic);
+ goto image;
+ }
+
+ if ((magic_mime = magic_file(magic, uri_path)) == NULL) {
+ fprintf(stderr, "No MIME...\n");
+ goto image;
+ }
+
+ if (strcmp(magic_mime, "audio/mpeg") != 0)
+ goto image;
+
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, uri_path, NULL, NULL) != 0) {
@@ -86,10 +106,6 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) {
goto image;
}
- /* only mp3 file contain artwork, so ignore others */
- if (strcmp(pFormatCtx->iformat->name, "mp3") != 0)
- goto image;
-
if (pFormatCtx->iformat->read_header(pFormatCtx) < 0) {
printf("could not read the format header\n");
goto image;
@@ -98,7 +114,7 @@ GdkPixbuf * retrieve_artwork(const char * music_dir, const char * uri) {
/* find the first attached picture, if available */
for (i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
- pkt = pFormatCtx->streams[i]->attached_pic;
+ AVPacket pkt = pFormatCtx->streams[i]->attached_pic;
loader = gdk_pixbuf_loader_new();
gdk_pixbuf_loader_write(loader, pkt.data, pkt.size, NULL);
@@ -143,11 +159,17 @@ image:
done:
fail:
- avformat_close_input(&pFormatCtx);
- avformat_free_context(pFormatCtx);
+ if (pFormatCtx != NULL) {
+ avformat_close_input(&pFormatCtx);
+ avformat_free_context(pFormatCtx);
+ }
+
+#ifdef HAVE_LIBAV
+ if (magic != NULL)
+ magic_close(magic);
+#endif
- if (uri_path)
- free(uri_path);
+ free(uri_path);
return pixbuf;
}