output_thread: return bool from ao_play()

Return false when there was no chunk in the pipe.  If the function
returns true, then audio_output_task() will not wait for a notify from
the player thread.  This fixes a race condition.
This commit is contained in:
Max Kellermann 2009-10-29 23:35:27 +01:00
parent 4748decd8d
commit cec019efff
1 changed files with 22 additions and 4 deletions

View File

@ -312,7 +312,16 @@ ao_play_chunk(struct audio_output *ao, const struct music_chunk *chunk)
return true; return true;
} }
static void ao_play(struct audio_output *ao) /**
* Plays all remaining chunks, until the tail of the pipe has been
* reached (and no more chunks are queued), or until a command is
* received.
*
* @return true if at least one chunk has been available, false if the
* tail of the pipe was already reached
*/
static bool
ao_play(struct audio_output *ao)
{ {
bool success; bool success;
const struct music_chunk *chunk; const struct music_chunk *chunk;
@ -323,8 +332,13 @@ static void ao_play(struct audio_output *ao)
if (chunk != NULL) if (chunk != NULL)
/* continue the previous play() call */ /* continue the previous play() call */
chunk = chunk->next; chunk = chunk->next;
else else {
chunk = music_pipe_peek(ao->pipe); chunk = music_pipe_peek(ao->pipe);
if (chunk == NULL)
/* no chunk available */
return false;
}
ao->chunk_finished = false; ao->chunk_finished = false;
while (chunk != NULL && ao->command == AO_COMMAND_NONE) { while (chunk != NULL && ao->command == AO_COMMAND_NONE) {
@ -347,6 +361,8 @@ static void ao_play(struct audio_output *ao)
g_mutex_unlock(ao->mutex); g_mutex_unlock(ao->mutex);
notify_signal(&pc.notify); notify_signal(&pc.notify);
g_mutex_lock(ao->mutex); g_mutex_lock(ao->mutex);
return true;
} }
static void ao_pause(struct audio_output *ao) static void ao_pause(struct audio_output *ao)
@ -440,8 +456,10 @@ static gpointer audio_output_task(gpointer arg)
return NULL; return NULL;
} }
if (ao->open) if (ao->open && ao_play(ao))
ao_play(ao); /* don't wait for an event if there are more
chunks in the pipe */
continue;
if (ao->command == AO_COMMAND_NONE) if (ao->command == AO_COMMAND_NONE)
g_cond_wait(ao->cond, ao->mutex); g_cond_wait(ao->cond, ao->mutex);