From dfff938dff089667038041fcb66262c87c3186c2 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Sun, 17 Dec 2017 16:12:10 -0500 Subject: Get rid of useless playlist probing and fix up some data types. Playback checked the files' presence before attempting to buffer the track. Just get rid of that and save an extra open/close call. It will find out if the path is bad when the metadata fails. Fix some size_t/off_t conflation. No need to update plugin version because no plugin actually uses bufopen(). Change-Id: I3db112449dc0b2eeb91c546f308880ac82494fc7 --- apps/buffering.c | 42 +++++++++++++++++++------------- apps/buffering.h | 16 +++++++------ apps/playback.c | 73 +++++++++++++++++++------------------------------------- apps/plugin.h | 2 +- 4 files changed, 59 insertions(+), 74 deletions(-) diff --git a/apps/buffering.c b/apps/buffering.c index 72b3890ef0..027d33a486 100644 --- a/apps/buffering.c +++ b/apps/buffering.c @@ -93,7 +93,7 @@ struct memory_handle { size_t data; /* Start index of the handle's data buffer */ size_t ridx; /* Read pointer, relative to the main buffer */ size_t widx; /* Write pointer, relative to the main buffer */ - ssize_t filesize; /* File total length */ + off_t filesize; /* File total length (possibly trimmed at tail) */ off_t start; /* Offset at which we started reading the file */ off_t pos; /* Read position in file */ off_t volatile end; /* Offset at which we stopped reading the file */ @@ -917,7 +917,7 @@ management functions for all the actual handle management work. return value: <0 if the file cannot be opened, or one file already queued to be opened, otherwise the handle for the file in the buffer */ -int bufopen(const char *file, size_t offset, enum data_type type, +int bufopen(const char *file, off_t offset, enum data_type type, void *user_data) { int handle_id = ERR_BUFFER_FULL; @@ -1508,31 +1508,36 @@ These functions are exported, to allow interaction with the buffer. They take care of the content of the structs, and rely on the linked list management functions for all the actual handle management work. */ +bool buf_is_handle(int handle_id) +{ + return find_handle(handle_id) != NULL; +} -ssize_t buf_handle_offset(int handle_id) +int buf_handle_data_type(int handle_id) { const struct memory_handle *h = find_handle(handle_id); if (!h) return ERR_HANDLE_NOT_FOUND; - return h->start; + return h->type; } -void buf_set_base_handle(int handle_id) +off_t buf_filesize(int handle_id) { - mutex_lock(&llist_mutex); - base_handle_id = handle_id; - mutex_unlock(&llist_mutex); + const struct memory_handle *h = find_handle(handle_id); + if (!h) + return ERR_HANDLE_NOT_FOUND; + return h->filesize; } -enum data_type buf_handle_data_type(int handle_id) +off_t buf_handle_offset(int handle_id) { const struct memory_handle *h = find_handle(handle_id); if (!h) - return TYPE_UNKNOWN; - return h->type; + return ERR_HANDLE_NOT_FOUND; + return h->start; } -ssize_t buf_handle_remaining(int handle_id) +off_t buf_handle_remaining(int handle_id) { const struct memory_handle *h = find_handle(handle_id); if (!h) @@ -1540,11 +1545,6 @@ ssize_t buf_handle_remaining(int handle_id) return h->filesize - h->end; } -bool buf_is_handle(int handle_id) -{ - return find_handle(handle_id) != NULL; -} - bool buf_pin_handle(int handle_id, bool pin) { struct memory_handle *h = find_handle(handle_id); @@ -1576,6 +1576,14 @@ size_t buf_length(void) return buffer_len; } +/* Set the handle from which useful data is counted */ +void buf_set_base_handle(int handle_id) +{ + mutex_lock(&llist_mutex); + base_handle_id = handle_id; + mutex_unlock(&llist_mutex); +} + /* Return the amount of buffer space used */ size_t buf_used(void) { diff --git a/apps/buffering.h b/apps/buffering.h index 4d4cb39df3..fcffcf086c 100644 --- a/apps/buffering.h +++ b/apps/buffering.h @@ -75,7 +75,7 @@ bool buffering_reset(char *buf, size_t buflen); * NOTE: Tail operations are only legal when the end of the file is buffered. ****************************************************************************/ -int bufopen(const char *file, size_t offset, enum data_type type, +int bufopen(const char *file, off_t offset, enum data_type type, void *user_data); int bufalloc(const void *src, size_t size, enum data_type type); bool bufclose(int handle_id); @@ -101,17 +101,19 @@ ssize_t bufcuttail(int handle_id, size_t size); * buf_back_off_storage: tell buffering thread to take it easy ****************************************************************************/ -enum data_type buf_handle_data_type(int handle_id); -ssize_t buf_handle_remaining(int handle_id); bool buf_is_handle(int handle_id); -ssize_t buf_handle_offset(int handle_id); -void buf_set_base_handle(int handle_id); -size_t buf_length(void); -size_t buf_used(void); +int buf_handle_data_type(int handle_id); +off_t buf_filesize(int handle_id); +off_t buf_handle_offset(int handle_id); +off_t buf_handle_remaining(int handle_id); bool buf_pin_handle(int handle_id, bool pin); bool buf_signal_handle(int handle_id, bool signal); +size_t buf_length(void); +size_t buf_used(void); + /* Settings */ +void buf_set_base_handle(int handle_id); void buf_set_watermark(size_t bytes); size_t buf_get_watermark(void); diff --git a/apps/playback.c b/apps/playback.c index 8b607f30f0..e8aaf3bacc 100644 --- a/apps/playback.c +++ b/apps/playback.c @@ -227,11 +227,6 @@ struct track_info #endif int audio_hid; /* Main audio data handle ID */ }; }; - off_t filesize; /* File total length on disk - TODO: This should be stored - in the handle or the - id3 and would use less - ram */ }; /* On-buffer info format; includes links */ @@ -459,8 +454,6 @@ static void track_info_wipe(struct track_info *infop) FOR_EACH_TRACK_INFO_HANDLE(i) infop->handle[i] = ERR_HANDLE_NOT_FOUND; - - infop->filesize = 0; } /** --- Track list --- **/ @@ -1065,7 +1058,7 @@ static void audio_update_filebuf_watermark(int seconds) track that fits, in which case we should avoid constant buffer low events */ if (track_list_count() > 1) - bytes = info.filesize + 1; + bytes = buf_filesize(info.audio_hid) + 1; } } else @@ -1613,7 +1606,7 @@ static bool audio_start_codec(bool auto_skip) id3_write(CODEC_ID3, cur_id3); ci.audio_hid = info.audio_hid; - ci.filesize = info.filesize; + ci.filesize = buf_filesize(info.audio_hid); buf_set_base_handle(info.audio_hid); /* All required data is now available for the codec */ @@ -1870,33 +1863,12 @@ static int audio_load_track(void) playlist_peek_offset); /* Get track name from current playlist read position */ - int fd = -1; - char name_buf[MAX_PATH + 1]; - const char *trackname; - - while (1) - { - trackname = playlist_peek(playlist_peek_offset, name_buf, - sizeof (name_buf)); - - if (!trackname) - break; - - /* Test for broken playlists by probing for the files */ - fd = open(trackname, O_RDONLY); - if (fd >= 0) - break; - - logf("Open failed"); - /* Skip invalid entry from playlist */ - playlist_skip_entry(NULL, playlist_peek_offset); - - /* Sync the playlist if it isn't finished */ - if (playlist_peek(playlist_peek_offset, NULL, 0)) - playlist_next(0); - } + char path_buf[MAX_PATH + 1]; + const char *path = playlist_peek(playlist_peek_offset, + path_buf, + sizeof (path_buf)); - if (!trackname) + if (!path) { /* No track - exhausted the playlist entries */ logf("End-of-playlist"); @@ -1920,7 +1892,7 @@ static int audio_load_track(void) /* Successfully opened the file - get track metadata */ if (filling == STATE_FULL || - (info.id3_hid = bufopen(trackname, 0, TYPE_ID3, NULL)) < 0) + (info.id3_hid = bufopen(path, 0, TYPE_ID3, NULL)) < 0) { /* Buffer or track list is full */ struct mp3entry *ub_id3; @@ -1929,9 +1901,15 @@ static int audio_load_track(void) /* Load the metadata for the first unbuffered track */ ub_id3 = id3_get(UNBUFFERED_ID3); - id3_mutex_lock(); - get_metadata(ub_id3, fd, trackname); - id3_mutex_unlock(); + + int fd = open(path, O_RDONLY); + if (fd >= 0) + { + id3_mutex_lock(); + get_metadata(ub_id3, fd, path); + id3_mutex_unlock(); + close(fd); + } if (filling != STATE_FULL) { @@ -1944,8 +1922,6 @@ static int audio_load_track(void) } else { - info.filesize = filesize(fd); - if (!track_list_commit_info(&info)) { track_list_free_info(&info); @@ -1957,7 +1933,6 @@ static int audio_load_track(void) track_list.in_progress_hid = info.self_hid; } - close(fd); return LOAD_TRACK_OK; } @@ -2050,23 +2025,23 @@ static int audio_finish_load_track(struct track_info *infop) #endif /* HAVE_CODEC_BUFFERING */ /** Finally, load the audio **/ - size_t file_offset = 0; + off_t file_offset = 0; if (track_id3->elapsed > track_id3->length) track_id3->elapsed = 0; - if ((off_t)track_id3->offset >= infop->filesize) + if ((off_t)track_id3->offset >= buf_filesize(infop->audio_hid)) track_id3->offset = 0; logf("%s: set offset for %s to %lu\n", __func__, - track_id3->title, (unsigned long)track_id3->offset); + track_id3->title, track_id3->offset); /* Adjust for resume rewind so we know what to buffer - starting the codec calls it again, so we don't save it (and they shouldn't accumulate) */ unsigned long elapsed, offset; resume_rewind_adjust_progress(track_id3, &elapsed, &offset); - logf("%s: Set resume for %s to %lu %lX", __func__, + logf("%s: Set resume for %s to %lu %lu", __func__, track_id3->title, elapsed, offset); enum data_type audiotype = rbcodec_format_is_atomic(track_id3->codectype) ? @@ -3049,7 +3024,7 @@ static void audio_on_ff_rewind(long time) /* Set the codec API to the correct metadata and track info */ ci.audio_hid = cur_info.audio_hid; - ci.filesize = cur_info.filesize; + ci.filesize = buf_filesize(cur_info.audio_hid); buf_set_base_handle(cur_info.audio_hid); } @@ -3343,9 +3318,9 @@ static void buffer_event_finished_callback(unsigned short id, void *ev_data) { (void)id; int hid = *(const int *)ev_data; - const enum data_type htype = buf_handle_data_type(hid); + int htype = buf_handle_data_type(hid); - logf("handle %d finished buffering (type:%u)", hid, (unsigned)htype); + logf("handle %d finished buffering (type:%d)", hid, htype); /* Limit queue traffic */ switch (htype) diff --git a/apps/plugin.h b/apps/plugin.h index c96c4ac899..a8e7c2af64 100644 --- a/apps/plugin.h +++ b/apps/plugin.h @@ -925,7 +925,7 @@ struct plugin_api { #if (CONFIG_CODEC == SWCODEC) /* buffering API */ - int (*bufopen)(const char *file, size_t offset, enum data_type type, + int (*bufopen)(const char *file, off_t offset, enum data_type type, void *user_data); int (*bufalloc)(const void *src, size_t size, enum data_type type); bool (*bufclose)(int handle_id); -- cgit v1.2.3