diff --git a/src/httpd_dacp.c b/src/httpd_dacp.c index 0e76c6a9..fcee9ddb 100644 --- a/src/httpd_dacp.c +++ b/src/httpd_dacp.c @@ -1140,12 +1140,17 @@ playqueuecontents_add_source(struct evbuffer *songlist, uint32_t source_id, int song = evbuffer_new(); if (!song) - { - DPRINTF(E_LOG, L_DACP, "Could not allocate song evbuffer for playqueue-contents\n"); - return -1; - } + { + DPRINTF(E_LOG, L_DACP, "Could not allocate song evbuffer for playqueue-contents\n"); + return -1; + } mfi = db_file_fetch_byid(source_id); + if (!mfi) + { + DPRINTF(E_LOG, L_DACP, "Could not fetch file id %d\n", source_id); + return -1; + } dmap_add_container(song, "ceQs", 16); dmap_add_raw_uint32(song, 1); /* Database */ dmap_add_raw_uint32(song, plid); @@ -1167,14 +1172,13 @@ playqueuecontents_add_source(struct evbuffer *songlist, uint32_t source_id, int ret = evbuffer_add_buffer(songlist, song); evbuffer_free(song); - if (mfi) - free_mfi(mfi, 0); + free_mfi(mfi, 0); if (ret < 0) - { - DPRINTF(E_LOG, L_DACP, "Could not add song to songlist for playqueue-contents\n"); - return ret; - } + { + DPRINTF(E_LOG, L_DACP, "Could not add song to songlist for playqueue-contents\n"); + return ret; + } return 0; } @@ -1246,10 +1250,9 @@ static void dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbu { start_index = (history->start_index + history->count - abs(span)) % MAX_HISTORY_COUNT; } - for (i = 0; i < history->count && i < abs(span); i++) + for (n = 0; n < history->count && n < abs(span); n++) { - n++; - ret = playqueuecontents_add_source(songlist, history->buffer[(start_index + i) % MAX_HISTORY_COUNT], (n + i + 1), status.plid); + ret = playqueuecontents_add_source(songlist, history->id[(start_index + n) % MAX_HISTORY_COUNT], (n + 1), status.plid); if (ret < 0) { DPRINTF(E_LOG, L_DACP, "Could not add song to songlist for playqueue-contents\n"); @@ -1286,6 +1289,7 @@ static void dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbu } } + /* Playlists are hist, curr and main. */ playlists = evbuffer_new(); if (!playlists) { diff --git a/src/player.c b/src/player.c index 21353ac2..2b29f5c2 100644 --- a/src/player.c +++ b/src/player.c @@ -1448,11 +1448,23 @@ player_history_get(void) static void history_add(uint32_t id) { - unsigned int next_index = (history->start_index + history->count) % MAX_HISTORY_COUNT; + unsigned int cur_index; + unsigned int next_index; + + /* Check if the current song is already the last in the history to avoid duplicates */ + cur_index = (history->start_index + history->count - 1) % MAX_HISTORY_COUNT; + if (id == history->id[cur_index]) + { + DPRINTF(E_LOG, L_PLAYER, "Current playing/streaming song already in history\n"); + return; + } + + /* Calculate the next index and update the start-index and count for the id-buffer */ + next_index = (history->start_index + history->count) % MAX_HISTORY_COUNT; if (next_index == history->start_index && history->count > 0) history->start_index = (history->start_index + 1) % MAX_HISTORY_COUNT; - history->buffer[next_index] = id; + history->id[next_index] = id; if (history->count < MAX_HISTORY_COUNT) history->count++; @@ -1471,20 +1483,20 @@ source_read(uint8_t *buf, int len, uint64_t rtptime) nbytes = 0; new = 0; while (nbytes < len) - { - if (new) { - DPRINTF(E_DBG, L_PLAYER, "New file\n"); + if (new) + { + DPRINTF(E_DBG, L_PLAYER, "New file\n"); - new = 0; + new = 0; - // add song to the played history - history_add(cur_streaming->id); + // add song to the played history + history_add(cur_streaming->id); - ret = source_next(0); - if (ret < 0) - return -1; - } + ret = source_next(0); + if (ret < 0) + return -1; + } if (EVBUFFER_LENGTH(audio_buf) == 0) { @@ -2295,15 +2307,15 @@ playback_stop(struct player_command *cmd) pb_timer_fd = -1; if (cur_playing) - { - history_add(cur_playing->id); - source_stop(cur_playing); - } + { + history_add(cur_playing->id); + source_stop(cur_playing); + } else if (cur_streaming) - { - history_add(cur_streaming->id); - source_stop(cur_streaming); - } + { + history_add(cur_streaming->id); + source_stop(cur_streaming); + } cur_playing = NULL; cur_streaming = NULL; @@ -2622,15 +2634,17 @@ playback_prev_bh(struct player_command *cmd) int ret; if (cur_playing) - { - history_add(cur_playing->id); - source_stop(cur_playing); - } + { + if (cur_playing->end > cur_playing->stream_start) + history_add(cur_playing->id); + source_stop(cur_playing); + } else if (cur_streaming) - { - history_add(cur_streaming->id); - source_stop(cur_streaming); - } + { + if (cur_streaming->end > cur_streaming->stream_start) + history_add(cur_streaming->id); + source_stop(cur_streaming); + } ret = source_prev(); if (ret < 0) @@ -2660,15 +2674,17 @@ playback_next_bh(struct player_command *cmd) int ret; if (cur_playing) - { - history_add(cur_playing->id); - source_stop(cur_playing); - } + { + if (cur_playing->end > cur_playing->stream_start) + history_add(cur_playing->id); + source_stop(cur_playing); + } else if (cur_streaming) - { - history_add(cur_streaming->id); - source_stop(cur_streaming); - } + { + if (cur_streaming->end > cur_streaming->stream_start) + history_add(cur_streaming->id); + source_stop(cur_streaming); + } ret = source_next(1); if (ret < 0) diff --git a/src/player.h b/src/player.h index 8acb8d15..21f5bded 100644 --- a/src/player.h +++ b/src/player.h @@ -96,7 +96,7 @@ struct player_history unsigned int count; /* Circular buffer of song ids previously played by forked-daapd */ - uint32_t buffer[MAX_HISTORY_COUNT]; + uint32_t id[MAX_HISTORY_COUNT]; };