Skip to content

Commit

Permalink
mpd: robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
vroland committed May 5, 2020
1 parent aadadcc commit f49a65f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
28 changes: 18 additions & 10 deletions examples/mpd_status/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ void show_status(struct mpd_status *status, struct mpd_song *song) {

int cursor_x = 700;
int cursor_y = 200;
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_TITLE, 0), &cursor_x,
&cursor_y, img_buf);
cursor_x = 700;
cursor_y = 200 + FiraSans.advance_y * 2;
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), &cursor_x,
&cursor_y, img_buf);
cursor_y = 200 + FiraSans.advance_y * 3;
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), &cursor_x,
&cursor_y, img_buf);
if (mpd_song_get_tag(song, MPD_TAG_TITLE, 0)) {
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_TITLE, 0), &cursor_x, &cursor_y, img_buf);
}

if (mpd_song_get_tag(song, MPD_TAG_ALBUM, 0)) {
cursor_x = 700;
cursor_y = 200 + FiraSans.advance_y * 2;
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_ALBUM, 0), &cursor_x,
&cursor_y, img_buf);
}

if (mpd_song_get_tag(song, MPD_TAG_ARTIST, 0)) {
cursor_x = 700;
cursor_y = 200 + FiraSans.advance_y * 3;
write_string((GFXfont*)&FiraSans, (char*)mpd_song_get_tag(song, MPD_TAG_ARTIST, 0), &cursor_x,
&cursor_y, img_buf);
}

epd_poweron();
epd_clear();
Expand Down Expand Up @@ -170,7 +178,7 @@ void epd_task() {
show_status(playback_info->status, playback_info->current_song);

char *album = (char*)mpd_song_get_tag(playback_info->current_song, MPD_TAG_ALBUM, 0);
if (album_cover == NULL || strncmp(album_cover->identifier, album, 128) != 0) {
if (album_cover == NULL || album_cover->identifier == NULL || album == NULL || strncmp(album_cover->identifier, album, 128) != 0) {
if (album_cover) {
free_album_cover(album_cover);
album_cover = NULL;
Expand Down
63 changes: 51 additions & 12 deletions examples/mpd_status/main/mpd_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ typedef struct {
uint32_t cover_offset;
uint32_t scale;
struct mpd_connection *conn;
// use albumart exclusively
bool use_albumart;
} image_fetch_context_t;

static void handle_error(struct mpd_connection *c) {
Expand All @@ -47,11 +49,20 @@ feed_jpg_chunk(JDEC *jd, /* Decompressor object */
char offset_s[32];
snprintf(offset_s, sizeof(offset_s), "%u", context->cover_offset);

printf("use albumart: %d\n", context->use_albumart);
struct mpd_connection *c = context->conn;
if (!mpd_send_command(c, "readpicture", context->image_uri, offset_s,
NULL)) {
handle_error(c);
return -1;
if (context->use_albumart) {
if (!mpd_send_command(c, "albumart", context->image_uri, offset_s,
NULL)) {
handle_error(c);
return -1;
}
} else {
if (!mpd_send_command(c, "readpicture", context->image_uri, offset_s,
NULL)) {
handle_error(c);
return -1;
}
}

struct mpd_pair *pair = mpd_recv_pair_named(c, "size");
Expand All @@ -60,7 +71,24 @@ feed_jpg_chunk(JDEC *jd, /* Decompressor object */
handle_error(c);
return -1;
}
if (!context->use_albumart) {
context->use_albumart = true;
ESP_LOGI("mpd_image", "no image with readpicture, trying albumart...\n");
if (!mpd_response_finish(c)) {
handle_error(c);
return -1;
}
int returned = feed_jpg_chunk(jd, buff, nd);
printf("returned: %d\n", returned);
return returned;
}

if (!mpd_response_finish(c)) {
handle_error(c);
return -1;
}
fprintf(stderr, "No 'size'\n");

return -1;
}

Expand All @@ -77,10 +105,16 @@ feed_jpg_chunk(JDEC *jd, /* Decompressor object */
}

uint32_t chunk_size = strtoull(pair->value, NULL, 10);
printf("chunk size: %d\n", chunk_size);
mpd_return_pair(c, pair);

if (chunk_size == 0)
if (chunk_size == 0) {
if (!mpd_response_finish(c)) {
handle_error(c);
return -1;
}
return 0;
}

context->chunk_data = malloc(chunk_size);
if (!mpd_recv_binary(c, context->chunk_data, chunk_size)) {
Expand Down Expand Up @@ -179,6 +213,7 @@ album_cover_t *readpicture(struct mpd_connection *c, char *uri,
context.chunk_data_size = 0;
context.chunk_data_offset = 0;
context.conn = c;
context.use_albumart = false;

/* Prepare to decompress the file */
rc =
Expand Down Expand Up @@ -232,13 +267,17 @@ album_cover_t *readpicture(struct mpd_connection *c, char *uri,
ESP_LOGW("mpd_image", "cannot allocate uri dup!");
return NULL;
}
cover->identifier = strndup(identifier, 128);
if (cover->identifier == NULL) {
free(cover->source_uri);
free(buf);
free(cover);
ESP_LOGW("mpd_image", "cannot allocate identifier dup!");
return NULL;
if (identifier != NULL) {
cover->identifier = strndup(identifier, 128);
if (cover->identifier == NULL) {
free(cover->source_uri);
free(buf);
free(cover);
ESP_LOGW("mpd_image", "cannot allocate identifier dup!");
return NULL;
}
} else {
cover->identifier = NULL;
}
return cover;
}

0 comments on commit f49a65f

Please sign in to comment.