From 3b6b49c735331e58767f2635859d557058cbde18 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sat, 28 Jun 2008 12:39:00 +0200 Subject: client: Use mode instead of finishedPlayback in player Smarter solution than commit 8421cfd9886ec5ee19cde18b48853c2f9ea60c29. --- src/main.c | 57 +++++++++++++++++++++++++++------------------------------ src/player.c | 30 ++++++++++++++++++------------ src/player.h | 7 ++++--- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/src/main.c b/src/main.c index bd3a92c..51b4f3e 100644 --- a/src/main.c +++ b/src/main.c @@ -287,39 +287,36 @@ int main (int argc, char **argv) { /* little hack, needed to signal: hey! we need a playlist, but don't * free anything (there is nothing to be freed yet) */ memset (&player, 0, sizeof (player)); - player.finishedPlayback = 1; while (!doQuit) { - /* check whether player finished playing and start playing new - * song */ - if (player.finishedPlayback == 1) { - /* already played a song, clean up things */ - if (player.url != NULL) { - scrobbleSong.length = BarSamplesToSeconds (player.samplerate, - player.channels, player.sampleSizeN); - /* scrobble when >= nn% are played */ - if (BarSamplesToSeconds (player.samplerate, - player.channels, player.sampleSizeCurr) * 100 / - scrobbleSong.length >= - bsettings.lastfmScrobblePercent && - bsettings.enableScrobbling) { - BarUiMsg ("Scrobbling song... "); - if (WardrobeSubmit (&wh, &scrobbleSong) == - WARDROBE_RET_OK) { - BarUiMsg ("Ok.\n"); - } else { - printf ("Error.\n"); - } + /* already played a song, clean up things/scrobble song */ + if (player.mode == PLAYER_FINISHED_PLAYBACK) { + scrobbleSong.length = BarSamplesToSeconds (player.samplerate, + player.channels, player.sampleSizeN); + /* scrobble when >= nn% are played */ + if (BarSamplesToSeconds (player.samplerate, + player.channels, player.sampleSizeCurr) * 100 / + scrobbleSong.length >= + bsettings.lastfmScrobblePercent && + bsettings.enableScrobbling) { + BarUiMsg ("Scrobbling song... "); + if (WardrobeSubmit (&wh, &scrobbleSong) == + WARDROBE_RET_OK) { + BarUiMsg ("Ok.\n"); + } else { + BarUiMsg ("Error.\n"); } - WardrobeSongDestroy (&scrobbleSong); - free (player.url); - /* we must _not_ NULL the whole player structure (because - * of finishedPlayback which may be = 1), but url to - * indicate we already freed things; very ugly... */ - player.url = NULL; - pthread_join (playerThread, NULL); } + WardrobeSongDestroy (&scrobbleSong); + free (player.url); + pthread_join (playerThread, NULL); + memset (&player, 0, sizeof (player)); + } + /* check whether player finished playing and start playing new + * song */ + if (player.mode >= PLAYER_FINISHED_PLAYBACK || + player.mode == PLAYER_FREED) { if (curStation != NULL) { /* what's next? */ if (curSong != NULL) { @@ -527,8 +524,8 @@ int main (int argc, char **argv) { } /* end poll */ /* show time */ - if (player.finishedPlayback == 0 && - player.mode >= SAMPLESIZE_INITIALIZED) { + if (player.mode >= PLAYER_SAMPLESIZE_INITIALIZED && + player.mode < PLAYER_FINISHED_PLAYBACK) { float songLength = BarSamplesToSeconds (player.samplerate, player.channels, player.sampleSizeN); float songRemaining = songLength - diff --git a/src/player.c b/src/player.c index d70b63f..d72979d 100644 --- a/src/player.c +++ b/src/player.c @@ -74,7 +74,7 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { player->bufferFilled += size*nmemb; player->bufferRead = 0; - if (player->mode == RECV_DATA) { + if (player->mode == PLAYER_RECV_DATA) { char *aacDecoded; NeAACDecFrameInfo frameInfo; @@ -101,18 +101,18 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { } } } else { - if (player->mode == FIND_ESDS) { + if (player->mode == PLAYER_INITIALIZED) { while (player->bufferRead+4 < player->bufferFilled) { if (memcmp (player->buffer + player->bufferRead, "esds", 4) == 0) { - player->mode = FOUND_ESDS; + player->mode = PLAYER_FOUND_ESDS; player->bufferRead += 4; break; } player->bufferRead++; } } - if (player->mode == FOUND_ESDS) { + if (player->mode == PLAYER_FOUND_ESDS) { /* FIXME: is this the correct way? */ /* we're gonna read 10 bytes */ while (player->bufferRead+1+4+5 < player->bufferFilled) { @@ -140,17 +140,17 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { format.byte_format = AO_FMT_LITTLE; player->audioOutDevice = ao_open_live (audioOutDriver, &format, NULL); - player->mode = AUDIO_INITIALIZED; + player->mode = PLAYER_AUDIO_INITIALIZED; break; } player->bufferRead++; } } - if (player->mode == AUDIO_INITIALIZED) { + if (player->mode == PLAYER_AUDIO_INITIALIZED) { while (player->bufferRead+4+8 < player->bufferFilled) { if (memcmp (player->buffer + player->bufferRead, "stsz", 4) == 0) { - player->mode = FOUND_STSZ; + player->mode = PLAYER_FOUND_STSZ; player->bufferRead += 4; /* skip version and unknown */ player->bufferRead += 8; @@ -160,7 +160,7 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { } } /* get frame sizes */ - if (player->mode == FOUND_STSZ) { + if (player->mode == PLAYER_FOUND_STSZ) { while (player->bufferRead+4 < player->bufferFilled) { /* how many frames do we have? */ if (player->sampleSizeN == 0) { @@ -182,17 +182,17 @@ size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) { } /* all sizes read, nearly ready for data mode */ if (player->sampleSizeCurr >= player->sampleSizeN) { - player->mode = SAMPLESIZE_INITIALIZED; + player->mode = PLAYER_SAMPLESIZE_INITIALIZED; break; } } } /* search for data atom and let the show begin... */ - if (player->mode == SAMPLESIZE_INITIALIZED) { + if (player->mode == PLAYER_SAMPLESIZE_INITIALIZED) { while (player->bufferRead+4 < player->bufferFilled) { if (memcmp (player->buffer + player->bufferRead, "mdat", 4) == 0) { - player->mode = RECV_DATA; + player->mode = PLAYER_RECV_DATA; player->sampleSizeCurr = 0; player->bufferRead += 4; break; @@ -219,19 +219,25 @@ void *BarPlayerThread (void *data) { struct aacPlayer *player = data; NeAACDecConfigurationPtr conf; + /* init handles */ player->audioFd = curl_easy_init (); player->aacHandle = NeAACDecOpen(); + /* set aac conf */ conf = NeAACDecGetCurrentConfiguration(player->aacHandle); conf->outputFormat = FAAD_FMT_16BIT; conf->downMatrix = 1; NeAACDecSetConfiguration(player->aacHandle, conf); + /* init curl */ curl_easy_setopt (player->audioFd, CURLOPT_URL, player->url); curl_easy_setopt (player->audioFd, CURLOPT_WRITEFUNCTION, BarPlayerCurlCb); curl_easy_setopt (player->audioFd, CURLOPT_WRITEDATA, player); curl_easy_setopt (player->audioFd, CURLOPT_USERAGENT, PACKAGE_STRING); curl_easy_setopt (player->audioFd, CURLOPT_CONNECTTIMEOUT, 60); + + player->mode = PLAYER_INITIALIZED; + curl_easy_perform (player->audioFd); NeAACDecClose(player->aacHandle); @@ -241,7 +247,7 @@ void *BarPlayerThread (void *data) { free (player->sampleSize); } - player->finishedPlayback = 1; + player->mode = PLAYER_FINISHED_PLAYBACK; return NULL; } diff --git a/src/player.h b/src/player.h index 89f99f8..9e44bef 100644 --- a/src/player.h +++ b/src/player.h @@ -29,8 +29,10 @@ struct aacPlayer { char buffer[CURL_MAX_WRITE_SIZE*2]; size_t bufferFilled; size_t bufferRead; - enum {FIND_ESDS, FOUND_ESDS, AUDIO_INITIALIZED, FOUND_STSZ, - SAMPLESIZE_INITIALIZED, RECV_DATA} mode; + enum {PLAYER_FREED = 0, PLAYER_INITIALIZED, PLAYER_FOUND_ESDS, + PLAYER_AUDIO_INITIALIZED, PLAYER_FOUND_STSZ, + PLAYER_SAMPLESIZE_INITIALIZED, PLAYER_RECV_DATA, + PLAYER_FINISHED_PLAYBACK} mode; /* stsz atom: sample sizes */ unsigned int *sampleSize; size_t sampleSizeN; @@ -42,7 +44,6 @@ struct aacPlayer { /* audio out */ ao_device *audioOutDevice; char *url; - char finishedPlayback; char doQuit; char doPause; CURL *audioFd; -- cgit v1.2.3