From bbb3322ddc35875572486afaa0a3ade92b9490b5 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Mon, 30 Jan 2017 22:50:02 +0100 Subject: [PATCH] [spotify] Flush audio buffer on playback stop - and since we are flushing from another thread we use evbuffer_enable_locking() which in turn adds a requirement for libevent_pthreads to be present --- configure.ac | 7 +++++++ src/main.c | 7 ++++++- src/spotify.c | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index ecb6000e..6d369df7 100644 --- a/configure.ac +++ b/configure.ac @@ -222,6 +222,11 @@ dnl Build with libcurl FORK_ARG_WITH_CHECK([FORKED], [libcurl support], [libcurl], [LIBCURL], [libcurl], [curl_global_init], [curl/curl.h]) +dnl Build with libevent_pthreads +FORK_ARG_WITH_CHECK([FORKED], [libevent_pthreads support], [libevent_pthreads], + [LIBEVENT_PTHREADS], [libevent_pthreads], [evthread_use_pthreads], + [event2/thread.h]) + dnl Build with json-c FORK_ARG_WITH_CHECK([FORKED], [json-c support], [json], [JSON_C], [json-c >= 0.11], [json_tokener_parse], [json.h], [], @@ -254,6 +259,8 @@ dnl Spotify with dynamic linking to libspotify FORK_ARG_ENABLE([Spotify support], [spotify], [SPOTIFY], [AS_IF([[test "x$with_json" = "xno"]], [AC_MSG_ERROR([[Spotify support requires json-c]])]) + AS_IF([[test "x$with_libevent_pthreads" = "xno"]], + [AC_MSG_ERROR([[Spotify support requires libevent_pthreads]])]) AC_CHECK_HEADER([[libspotify/api.h]], [], [AC_MSG_ERROR([[libspotify/api.h not found]])]) AC_DEFINE([HAVE_SPOTIFY_H], 1, diff --git a/src/main.c b/src/main.c index fef3a504..269e7dfc 100644 --- a/src/main.c +++ b/src/main.c @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -683,7 +684,11 @@ main(int argc, char **argv) } /* Initialize event base (after forking) */ - evbase_main = event_base_new(); + CHECK_NULL(L_MAIN, evbase_main = event_base_new()); + +#ifdef SPOTIFY + CHECK_ERR(L_MAIN, evthread_use_pthreads()); +#endif DPRINTF(E_LOG, L_MAIN, "mDNS init\n"); ret = mdns_init(); diff --git a/src/spotify.c b/src/spotify.c index 0030a671..5639414c 100644 --- a/src/spotify.c +++ b/src/spotify.c @@ -1252,6 +1252,7 @@ playback_stop(void *arg, int *retval) g_state = SPOTIFY_STATE_STOPPED; + evbuffer_drain(spotify_audio_buffer, evbuffer_get_length(spotify_audio_buffer)); *retval = 0; return COMMAND_END; @@ -1297,7 +1298,6 @@ playback_eot(void *arg, int *retval) g_state = SPOTIFY_STATE_STOPPING; // TODO 1) This will block for a while, but perhaps ok? - // 2) spotify_audio_buffer not entirely thread safe here (but unlikely risk...) input_write(spotify_audio_buffer, INPUT_FLAG_EOF); *retval = 0; @@ -2487,6 +2487,8 @@ spotify_init(void) spotify_audio_buffer = evbuffer_new(); + CHECK_ERR(L_SPOTIFY, evbuffer_enable_locking(spotify_audio_buffer, NULL)); + CHECK_ERR(L_SPOTIFY, mutex_init(&login_lck)); CHECK_ERR(L_SPOTIFY, pthread_cond_init(&login_cond, NULL));