From ca09f91f647f6677fea7804930d7d1b2c876f76e Mon Sep 17 00:00:00 2001 From: Michael Giacomelli Date: Wed, 9 Dec 2020 13:17:05 -0500 Subject: Fix deadlocks when trying to buffer large album art. Internally, buffering tries to load the entire album art file into the audio buffer, which will fail if the file is larger than the buffer. Playback.c interprets a file failing to buffer to mean that the buffer is full, so it waits for more space and tries again. This results in a deadlock since the file will never fit. Change bufopen to return a new error condition when an image file will not fit on the buffer because it is too large: ERR_BITMAP_TOO_LARGE. Note that we arbitrarily set "too large" to be within 64KB of the entire buffer size or larger, this could be adjusted if needed. Change audio_load_albumart to pass through error messages from bufopen. In playback.c, check to see why audio_load_albumart fails. If it fails because the file is too large to buffer, simply ignore the file. If it fails because the file would fit but the buffer is full, try again later. Change-Id: I66799ae26f124b495e1522fce7285332f4cf986f --- apps/buffering.c | 7 +++++++ apps/buffering.h | 1 + apps/playback.c | 15 ++++++++++++--- apps/radio/radioart.c | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) mode change 100644 => 100755 apps/buffering.c mode change 100644 => 100755 apps/buffering.h mode change 100644 => 100755 apps/playback.c mode change 100644 => 100755 apps/radio/radioart.c diff --git a/apps/buffering.c b/apps/buffering.c old mode 100644 new mode 100755 index 9ffd35714c..3adbc4a6b9 --- a/apps/buffering.c +++ b/apps/buffering.c @@ -999,7 +999,14 @@ int bufopen(const char *file, off_t offset, enum data_type type, DEBUGF("%s(): failed to add handle\n", __func__); mutex_unlock(&llist_mutex); close(fd); + + /*warn playback.c if it is trying to buffer too large of an image*/ + if(type == TYPE_BITMAP && padded_size >= buffer_len - 64*1024) + { + return ERR_BITMAP_TOO_LARGE; + } return ERR_BUFFER_FULL; + } handle_id = h->id; diff --git a/apps/buffering.h b/apps/buffering.h old mode 100644 new mode 100755 index fcffcf086c..1a75d865ae --- a/apps/buffering.h +++ b/apps/buffering.h @@ -47,6 +47,7 @@ enum data_type { #define ERR_HANDLE_NOT_DONE -5 #define ERR_UNSUPPORTED_TYPE -6 #define ERR_WRONG_THREAD -7 +#define ERR_BITMAP_TOO_LARGE -8 /* Initialise the buffering subsystem */ void buffering_init(void) INIT_ATTR; diff --git a/apps/playback.c b/apps/playback.c old mode 100644 new mode 100755 index 31aed2abe1..5419888c27 --- a/apps/playback.c +++ b/apps/playback.c @@ -1689,7 +1689,7 @@ static bool audio_load_cuesheet(struct track_info *infop, #ifdef HAVE_ALBUMART /* Load any album art for the file - returns false if the buffer is full */ -static bool audio_load_albumart(struct track_info *infop, +static int audio_load_albumart(struct track_info *infop, struct mp3entry *track_id3) { FOREACH_ALBUMART(i) @@ -1730,7 +1730,11 @@ static bool audio_load_albumart(struct track_info *infop, if (hid == ERR_BUFFER_FULL) { logf("buffer is full for now (%s)", __func__); - return false; + return ERR_BUFFER_FULL; + } + else if (hid == ERR_BITMAP_TOO_LARGE){ + logf("image is too large to fit in buffer (%s)", __func__); + return ERR_BITMAP_TOO_LARGE; } else { @@ -1981,7 +1985,12 @@ static int audio_finish_load_track(struct track_info *infop) #ifdef HAVE_ALBUMART /* Try to load album art for the track */ - if (!audio_load_albumart(infop, track_id3)) + int retval = audio_load_albumart(infop, track_id3); + if (retval == ERR_BITMAP_TOO_LARGE) + { + /* No space for album art on buffer because the file is larger than the buffer. + Ignore the file and keep buffering */ + } else if (retval == ERR_BUFFER_FULL) { /* No space for album art on buffer, not an error */ filling = STATE_FULL; diff --git a/apps/radio/radioart.c b/apps/radio/radioart.c old mode 100644 new mode 100755 index 5e1a0ad5cf..87d37cd52c --- a/apps/radio/radioart.c +++ b/apps/radio/radioart.c @@ -88,7 +88,7 @@ static int load_radioart_image(struct radioart *ra, const char* preset_name, user_data.embedded_albumart = NULL; user_data.dim = &ra->dim; ra->handle = bufopen(path, 0, TYPE_BITMAP, &user_data); - if (ra->handle == ERR_BUFFER_FULL) + if (ra->handle == ERR_BUFFER_FULL || ra->handle == ERR_BITMAP_TOO_LARGE) { int i = find_oldest_image_index(); if (i != -1) -- cgit v1.2.3