From d1322b71595336740eb5e18e5deed056ddb71c7a Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Sun, 14 Aug 2011 15:13:00 +0000 Subject: GSoC/Buflib: Replace all direct accesses to audiobuf with buffer API functions. Namely, introduce buffer_get_buffer() and buffer_release_buffer(). buffer_get_buffer() aquires all available and grabs a lock, attempting to call buffer_alloc() or buffer_get_buffer() while this lock is locked will cause a panicf() (doesn't actually happen, but is for debugging purpose). buffer_release_buffer() unlocks that lock and can additionally increment the audiobuf buffer to make an allocation. Pass 0 to only unlock if buffer was used temporarily only. buffer_available() is a replacement function to query audiobuflen, i.e. what's left in the buffer. Buffer init is moved up in the init chain and handles ipodvideo64mb internally. Further changes happened to mp3data.c and talk.c as to not call the above API functions, but get the buffer from callers. The caller is the audio system which has the buffer lock while mp3data.c and talk mess with the buffer. mpeg.c now implements some buffer related functions of playback.h, especially audio_get_buffer(), allowing to reduce #ifdef hell a tiny bit. audiobuf and audiobufend are local to buffer.c now. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30308 a1c6a512-1295-4272-9138-f99709370657 --- apps/playback.c | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'apps/playback.c') diff --git a/apps/playback.c b/apps/playback.c index fe9bd579d4..3f6ee71ad7 100644 --- a/apps/playback.c +++ b/apps/playback.c @@ -744,16 +744,25 @@ static void audio_reset_buffer(void) /* see audio_get_recording_buffer if this is modified */ logf("%s()", __func__); + /* release the buffer on behalf of any caller of audio_get_buffer() */ + buffer_release_buffer(0); + /* If the setup of anything allocated before the file buffer is changed, do check the adjustments after the buffer_alloc call as it will likely be affected and need sliding over */ /* Initially set up file buffer as all space available */ - unsigned char *filebuf = audiobuf + talk_get_bufsize(); - size_t filebuflen = audiobufend - filebuf; - size_t allocsize; + size_t filebuflen, allocsize; + unsigned char *filebuf = buffer_get_buffer(&filebuflen); - ALIGN_BUFFER(filebuf, filebuflen, sizeof (intptr_t)); + /* Subtract whatever voice needs */ + allocsize = talkbuf_init(filebuf); + allocsize = ALIGN_UP(allocsize, sizeof (intptr_t)); + if (allocsize > filebuflen) + goto bufpanic; + + filebuf += allocsize; + filebuflen -= allocsize; if (talk_voice_required()) { @@ -3335,6 +3344,7 @@ void audio_hard_stop(void) #ifdef PLAYBACK_VOICE voice_stop(); #endif + buffer_release_buffer(0); } /* Resume playback if paused */ @@ -3441,7 +3451,7 @@ void audio_flush_and_reload_tracks(void) voicing */ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) { - unsigned char *buf, *end; + unsigned char *buf; if (audio_is_initialized) { @@ -3461,7 +3471,7 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) || !talk_voice_required()) { logf("get buffer: talk, audio"); - /* Ok to use everything from audiobuf to audiobufend - voice is loaded, + /* Ok to use everything from audiobuf - voice is loaded, the talk buffer is not needed because voice isn't being used, or could be AUDIOBUF_STATE_TRASHED already. If state is AUDIOBUF_STATE_VOICED_ONLY, no problem as long as memory isn't @@ -3474,9 +3484,7 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) talk_buffer_steal(); buffer_state = AUDIOBUF_STATE_TRASHED; } - - buf = audiobuf; - end = audiobufend; + buf = buffer_get_buffer(buffer_size); } else { @@ -3485,14 +3493,18 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) /* Skip talk buffer and move pcm buffer to end to maximize available contiguous memory - no audio running means voice will not need the swap space */ + size_t siz, talkbuf_size; logf("get buffer: audio"); - buf = audiobuf + talk_get_bufsize(); - end = audiobufend - voicebuf_init(audiobufend); + /* call buffer_get_buffer() to make use of the locking mechanism */ + buf = buffer_get_buffer(&siz); + buf += talkbuf_size = talkbuf_init(buf); + siz -= talkbuf_size; + siz -= voicebuf_init(buf + siz); + *buffer_size = siz; + buffer_state = AUDIOBUF_STATE_VOICED_ONLY; } - *buffer_size = end - buf; - return buf; } @@ -3500,14 +3512,11 @@ unsigned char * audio_get_buffer(bool talk_buf, size_t *buffer_size) /* Stop audio, voice and obtain all available buffer space */ unsigned char * audio_get_recording_buffer(size_t *buffer_size) { - audio_hard_stop(); talk_buffer_steal(); + audio_hard_stop(); - unsigned char *end = audiobufend; buffer_state = AUDIOBUF_STATE_TRASHED; - *buffer_size = end - audiobuf; - - return (unsigned char *)audiobuf; + return buffer_get_buffer(buffer_size); } #endif /* HAVE_RECORDING */ -- cgit v1.2.3