From 847ca4193e3fbc5b27285979888ab364bbf1792e Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Wed, 9 Jan 2013 11:52:01 -0800 Subject: Add play and pause commands Add commands that always play and always pause, in addition to the current toggle pause command. Closes #342. --- src/player.c | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'src/player.c') diff --git a/src/player.c b/src/player.c index 1cb44de..8a46f31 100644 --- a/src/player.c +++ b/src/player.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2012 +Copyright (c) 2008-2013 Lars-Dominik Braun Permission is hereby granted, free of charge, to any person obtaining a copy @@ -38,20 +38,34 @@ THE SOFTWARE. #define bigToHostEndian32(x) ntohl(x) -/* wait while locked, but don't slow down main thread by keeping - * locks too long */ -#define QUIT_PAUSE_CHECK \ - pthread_mutex_lock (&player->pauseMutex); \ - pthread_mutex_unlock (&player->pauseMutex); \ - if (player->doQuit) { \ - /* err => abort playback */ \ - return WAITRESS_CB_RET_ERR; \ - } - /* pandora uses float values with 2 digits precision. Scale them by 100 to get * a "nice" integer */ #define RG_SCALE_FACTOR 100 +/* wait until the pause flag is cleared + * @param player structure + * @return true if the player should quit + */ +static bool BarPlayerCheckPauseQuit (struct audioPlayer *player) { + bool quit = false; + + pthread_mutex_lock (&player->pauseMutex); + while (true) { + if (player->doQuit) { + quit = true; + break; + } + if (!player->doPause) { + break; + } + pthread_cond_wait(&player->pauseCond, + &player->pauseMutex); + } + pthread_mutex_unlock (&player->pauseMutex); + + return quit; +} + /* compute replaygain scale factor * algo taken from here: http://www.dsprelated.com/showmessage/29246/1.php * mpd does the same @@ -125,9 +139,8 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, const char *data = ptr; struct audioPlayer *player = stream; - QUIT_PAUSE_CHECK; - - if (!BarPlayerBufferFill (player, data, size)) { + if (BarPlayerCheckPauseQuit (player) || + !BarPlayerBufferFill (player, data, size)) { return WAITRESS_CB_RET_ERR; } @@ -141,7 +154,9 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, player->sampleSize[player->sampleSizeCurr]) { /* going through this loop can take up to a few seconds => * allow earlier thread abort */ - QUIT_PAUSE_CHECK; + if (BarPlayerCheckPauseQuit (player)) { + return WAITRESS_CB_RET_ERR; + } /* decode frame */ aacDecoded = NeAACDecDecode(player->aacHandle, &frameInfo, @@ -335,9 +350,8 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, struct audioPlayer *player = stream; size_t i; - QUIT_PAUSE_CHECK; - - if (!BarPlayerBufferFill (player, data, size)) { + if (BarPlayerCheckPauseQuit (player) || + !BarPlayerBufferFill (player, data, size)) { return WAITRESS_CB_RET_ERR; } @@ -417,7 +431,9 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, (unsigned long long int) player->samplerate; } - QUIT_PAUSE_CHECK; + if (BarPlayerCheckPauseQuit (player)) { + return WAITRESS_CB_RET_ERR; + } } while (player->mp3Stream.error != MAD_ERROR_BUFLEN); player->bufferRead += player->mp3Stream.next_frame - player->buffer; -- cgit v1.2.3