aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2016-10-07 09:54:54 +0200
committerGravatar Christian Hesse <mail@eworm.de>2016-10-07 09:54:54 +0200
commit91fd7b03788b29ae8cd7d09277d2f7ca32aba26c (patch)
tree38a53a9f472402dcb0f2898367520b20985d62c7
parentb3a0f6f65c9828314e60929dee16c0aa1de06158 (diff)
downloadmpd-notification-91fd7b03788b29ae8cd7d09277d2f7ca32aba26c.tar.gz
mpd-notification-91fd7b03788b29ae8cd7d09277d2f7ca32aba26c.tar.zst
use libmagic to decide whether to search for media artwork
-rw-r--r--Makefile1
-rw-r--r--mpd-notification.c44
-rw-r--r--mpd-notification.h6
3 files changed, 38 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index f597bf0..98fbd93 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ CFLAGS += $(shell pkg-config --cflags --libs libnotify)
LIBAV_CFLAGS := $(shell pkg-config --cflags --libs libavformat libavutil 2>/dev/null)
ifneq ($(LIBAV_CFLAGS),)
CFLAGS += -DHAVE_LIBAV $(LIBAV_CFLAGS)
+CFLAGS += -lmagic
endif
LDFLAGS += -Wl,-z,now -Wl,-z,relro -pie
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;
}
diff --git a/mpd-notification.h b/mpd-notification.h
index 591b763..bb27007 100644
--- a/mpd-notification.h
+++ b/mpd-notification.h
@@ -19,11 +19,13 @@
#include <unistd.h>
#include <iniparser.h>
+#include <libnotify/notify.h>
+#include <mpd/client.h>
+
#ifdef HAVE_LIBAV
#include <libavformat/avformat.h>
+#include <magic.h>
#endif
-#include <libnotify/notify.h>
-#include <mpd/client.h>
#include "config.h"
#include "version.h"