From b61291f4bd51e3d9e30504c16de743decd54f2d9 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Wed, 22 Feb 2012 15:46:14 +0100 Subject: Minor player cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move player buffer to heap, a few const’s here and there, increase header buffer size, fix comments. --- src/player.c | 55 ++++++++++++++++++++++++++++++------------------------- src/player.h | 8 +++++--- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/player.c b/src/player.c index 6cb8206..9a2306a 100644 --- a/src/player.c +++ b/src/player.c @@ -1,5 +1,5 @@ /* -Copyright (c) 2008-2010 +Copyright (c) 2008-2011 Lars-Dominik Braun Permission is hereby granted, free of charge, to any person obtaining a copy @@ -57,7 +57,7 @@ THE SOFTWARE. * @param apply this gain * @return this * yourvalue = newgain value */ -unsigned int BarPlayerCalcScale (float applyGain) { +unsigned int BarPlayerCalcScale (const float applyGain) { return pow (10.0, applyGain / 20.0) * RG_SCALE_FACTOR; } @@ -66,8 +66,8 @@ unsigned int BarPlayerCalcScale (float applyGain) { * @param replaygain scale (calculated by computeReplayGainScale) * @return scaled value */ -static inline signed short int applyReplayGain (signed short int value, - unsigned int scale) { +static inline signed short int applyReplayGain (const signed short int value, + const unsigned int scale) { int tmpReplayBuf = value * scale; /* avoid clipping */ if (tmpReplayBuf > SHRT_MAX*RG_SCALE_FACTOR) { @@ -85,10 +85,10 @@ static inline signed short int applyReplayGain (signed short int value, * @param data size * @return 1 on success, 0 when buffer overflow occured */ -static inline int BarPlayerBufferFill (struct audioPlayer *player, char *data, - size_t dataSize) { +static inline int BarPlayerBufferFill (struct audioPlayer *player, + const char *data, const size_t dataSize) { /* fill buffer */ - if (player->bufferFilled + dataSize > sizeof (player->buffer)) { + if (player->bufferFilled + dataSize > BAR_PLAYER_BUFSIZE) { BarUiMsg (player->settings, MSG_ERR, "Buffer overflow!\n"); return 0; } @@ -119,8 +119,9 @@ static inline void BarPlayerBufferMove (struct audioPlayer *player) { * @param extra data (player data) * @return received bytes or less on error */ -static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream) { - char *data = ptr; +static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, + void *stream) { + const char *data = ptr; struct audioPlayer *player = stream; QUIT_PAUSE_CHECK; @@ -235,10 +236,10 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream) if (player->sampleSizeN == 0) { /* mp4 uses big endian, convert */ player->sampleSizeN = - bigToHostEndian32 (*((int *) (player->buffer + + bigToHostEndian32 (*((uint32_t *) (player->buffer + player->bufferRead))); - player->sampleSize = calloc (player->sampleSizeN, - sizeof (player->sampleSizeN)); + player->sampleSize = malloc (player->sampleSizeN * + sizeof (*player->sampleSize)); player->bufferRead += 4; player->sampleSizeCurr = 0; /* set up song duration (assuming one frame always contains @@ -253,7 +254,7 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream) break; } else { player->sampleSize[player->sampleSizeCurr] = - bigToHostEndian32 (*((int *) (player->buffer + + bigToHostEndian32 (*((uint32_t *) (player->buffer + player->bufferRead))); player->sampleSizeCurr++; player->bufferRead += 4; @@ -293,7 +294,7 @@ static WaitressCbReturn_t BarPlayerAACCb (void *ptr, size_t size, void *stream) * @param mad fixed * @return short int */ -static inline signed short int BarPlayerMadToShort (mad_fixed_t fixed) { +static inline signed short int BarPlayerMadToShort (const mad_fixed_t fixed) { /* Clipping */ if (fixed >= MAD_F_ONE) { return SHRT_MAX; @@ -305,8 +306,11 @@ static inline signed short int BarPlayerMadToShort (mad_fixed_t fixed) { return (signed short int) (fixed >> (MAD_F_FRACBITS - 15)); } -static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, void *stream) { - char *data = ptr; +/* mp3 playback callback + */ +static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, + void *stream) { + const char *data = ptr; struct audioPlayer *player = stream; size_t i; @@ -318,7 +322,7 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, void *stream) /* some "prebuffering" */ if (player->mode < PLAYER_RECV_DATA && - player->bufferFilled < sizeof (player->buffer) / 2) { + player->bufferFilled < BAR_PLAYER_BUFSIZE / 2) { return WAITRESS_CB_RET_OK; } @@ -404,12 +408,12 @@ static WaitressCbReturn_t BarPlayerMp3Cb (void *ptr, size_t size, void *stream) #endif /* ENABLE_MAD */ /* player thread; for every song a new thread is started - * @param aacPlayer structure - * @return NULL NULL NULL ... + * @param audioPlayer structure + * @return PLAYER_RET_* */ void *BarPlayerThread (void *data) { struct audioPlayer *player = data; - char extraHeaders[25]; + char extraHeaders[32]; void *ret = PLAYER_RET_OK; #ifdef ENABLE_FAAD NeAACDecConfigurationPtr conf; @@ -421,6 +425,7 @@ void *BarPlayerThread (void *data) { player->waith.data = (void *) player; /* extraHeaders will be initialized later */ player->waith.extraHeaders = extraHeaders; + player->buffer = malloc (BAR_PLAYER_BUFSIZE); switch (player->audioFormat) { #ifdef ENABLE_FAAD @@ -448,6 +453,7 @@ void *BarPlayerThread (void *data) { #endif /* ENABLE_MAD */ default: + /* FIXME: leaks memory */ BarUiMsg (player->settings, MSG_ERR, "Unsupported audio format!\n"); return PLAYER_RET_OK; break; @@ -468,6 +474,7 @@ void *BarPlayerThread (void *data) { #ifdef ENABLE_FAAD case PIANO_AF_AACPLUS: NeAACDecClose(player->aacHandle); + free (player->sampleSize); break; #endif /* ENABLE_FAAD */ @@ -484,17 +491,15 @@ void *BarPlayerThread (void *data) { /* this should never happen: thread is aborted above */ break; } + if (player->aoError) { ret = (void *) PLAYER_RET_ERR; } + ao_close(player->audioOutDevice); WaitressFree (&player->waith); - #ifdef ENABLE_FAAD - if (player->sampleSize != NULL) { - free (player->sampleSize); - } - #endif /* ENABLE_FAAD */ pthread_mutex_destroy (&player->pauseMutex); + free (player->buffer); player->mode = PLAYER_FINISHED_PLAYBACK; diff --git a/src/player.h b/src/player.h index 070ac0f..ac6853c 100644 --- a/src/player.h +++ b/src/player.h @@ -38,6 +38,7 @@ THE SOFTWARE. /* required for freebsd */ #include #include +#include #include #include @@ -45,6 +46,7 @@ THE SOFTWARE. #include "settings.h" #define BAR_PLAYER_MS_TO_S_FACTOR 1000 +#define BAR_PLAYER_BUFSIZE (WAITRESS_BUFFER_SIZE*2) struct audioPlayer { char doQuit; @@ -82,7 +84,7 @@ struct audioPlayer { /* stsz atom: sample sizes */ size_t sampleSizeN; size_t sampleSizeCurr; - unsigned int *sampleSize; + uint32_t *sampleSize; NeAACDecHandle aacHandle; #endif @@ -97,10 +99,10 @@ struct audioPlayer { ao_device *audioOutDevice; const BarSettings_t *settings; + unsigned char *buffer; + pthread_mutex_t pauseMutex; WaitressHandle_t waith; - /* buffer; should be large enough */ - unsigned char buffer[WAITRESS_BUFFER_SIZE*2]; }; enum {PLAYER_RET_OK = 0, PLAYER_RET_ERR = 1}; -- cgit v1.2.3