aboutsummaryrefslogtreecommitdiffstats
path: root/src/player_thread.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/player_thread.c522
1 files changed, 371 insertions, 151 deletions
diff --git a/src/player_thread.c b/src/player_thread.c
index e2c9b6f93..cf4e61384 100644
--- a/src/player_thread.c
+++ b/src/player_thread.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2009 The Music Player Daemon Project
+ * Copyright (C) 2003-2010 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -17,6 +17,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "config.h"
#include "player_thread.h"
#include "player_control.h"
#include "decoder_control.h"
@@ -46,6 +47,8 @@ enum xfade_state {
};
struct player {
+ struct decoder_control *dc;
+
struct music_pipe *pipe;
/**
@@ -102,75 +105,129 @@ struct player {
struct audio_format play_audio_format;
/**
- * Coefficient for converting a PCM buffer size into a time
- * span.
+ * The time stamp of the chunk most recently sent to the
+ * output thread. This attribute is only used if
+ * audio_output_all_get_elapsed_time() didn't return a usable
+ * value; the output thread can estimate the elapsed time more
+ * precisly.
*/
- double size_to_time;
+ float elapsed_time;
};
static struct music_buffer *player_buffer;
-static void player_command_finished(void)
+static void player_command_finished_locked(void)
{
assert(pc.command != PLAYER_COMMAND_NONE);
pc.command = PLAYER_COMMAND_NONE;
- notify_signal(&main_notify);
+ g_cond_signal(main_cond);
+}
+
+static void player_command_finished(void)
+{
+ player_lock();
+ player_command_finished_locked();
+ player_unlock();
+}
+
+/**
+ * Start the decoder.
+ *
+ * Player lock is not held.
+ */
+static void
+player_dc_start(struct player *player, struct music_pipe *pipe)
+{
+ struct decoder_control *dc = player->dc;
+
+ assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
+ assert(pc.next_song != NULL);
+
+ dc_start(dc, pc.next_song, player_buffer, pipe);
}
/**
* Stop the decoder and clears (and frees) its music pipe.
+ *
+ * Player lock is not held.
*/
static void
player_dc_stop(struct player *player)
{
- dc_stop(&pc.notify);
+ struct decoder_control *dc = player->dc;
+
+ dc_stop(dc);
- if (dc.pipe != NULL) {
+ if (dc->pipe != NULL) {
/* clear and free the decoder pipe */
- music_pipe_clear(dc.pipe, player_buffer);
+ music_pipe_clear(dc->pipe, player_buffer);
- if (dc.pipe != player->pipe)
- music_pipe_free(dc.pipe);
+ if (dc->pipe != player->pipe)
+ music_pipe_free(dc->pipe);
- dc.pipe = NULL;
+ dc->pipe = NULL;
}
}
/**
+ * Returns true if the decoder is decoding the next song (or has begun
+ * decoding it, or has finished doing it), and the player hasn't
+ * switched to that song yet.
+ */
+static bool
+decoding_next_song(const struct player *player)
+{
+ return player->dc->pipe != NULL && player->dc->pipe != player->pipe;
+}
+
+/**
* After the decoder has been started asynchronously, wait for the
* "START" command to finish. The decoder may not be initialized yet,
* i.e. there is no audio_format information yet.
+ *
+ * The player lock is not held.
*/
static bool
player_wait_for_decoder(struct player *player)
{
- dc_command_wait(&pc.notify);
+ struct decoder_control *dc = player->dc;
- if (decoder_has_failed()) {
- assert(dc.next_song == NULL || dc.next_song->url != NULL);
- pc.errored_song = dc.next_song;
+ assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
+ assert(pc.next_song != NULL);
+
+ player->queued = false;
+
+ if (decoder_lock_has_failed(dc)) {
+ player_lock();
+ pc.errored_song = dc->song;
pc.error = PLAYER_ERROR_FILE;
pc.next_song = NULL;
- player->queued = false;
+ player_unlock();
+
return false;
}
- pc.total_time = pc.next_song->tag != NULL
- ? pc.next_song->tag->time : 0;
- pc.bit_rate = 0;
- audio_format_clear(&pc.audio_format);
-
player->song = pc.next_song;
- pc.next_song = NULL;
- pc.elapsed_time = 0;
- player->queued = false;
+ player->elapsed_time = 0.0;
/* set the "starting" flag, which will be cleared by
player_check_decoder_startup() */
player->decoder_starting = true;
+ player_lock();
+
+ /* update player_control's song information */
+ pc.total_time = song_get_duration(pc.next_song);
+ pc.bit_rate = 0;
+ audio_format_clear(&pc.audio_format);
+
+ /* clear the queued song */
+ pc.next_song = NULL;
+
+ player_unlock();
+
/* call syncPlaylistWithQueue() in the main thread */
event_pipe_emit(PIPE_EVENT_PLAYLIST);
@@ -178,54 +235,86 @@ player_wait_for_decoder(struct player *player)
}
/**
+ * Returns the real duration of the song, comprising the duration
+ * indicated by the decoder plugin.
+ */
+static double
+real_song_duration(const struct song *song, double decoder_duration)
+{
+ assert(song != NULL);
+
+ if (decoder_duration <= 0.0)
+ /* the decoder plugin didn't provide information; fall
+ back to song_get_duration() */
+ return song_get_duration(song);
+
+ if (song->end_ms > 0 && song->end_ms / 1000.0 < decoder_duration)
+ return (song->end_ms - song->start_ms) / 1000.0;
+
+ return decoder_duration - song->start_ms / 1000.0;
+}
+
+/**
* The decoder has acknowledged the "START" command (see
* player_wait_for_decoder()). This function checks if the decoder
* initialization has completed yet.
+ *
+ * The player lock is not held.
*/
static bool
player_check_decoder_startup(struct player *player)
{
+ struct decoder_control *dc = player->dc;
+
assert(player->decoder_starting);
- if (decoder_has_failed()) {
+ decoder_lock(dc);
+
+ if (decoder_has_failed(dc)) {
/* the decoder failed */
- assert(dc.next_song == NULL || dc.next_song->url != NULL);
+ decoder_unlock(dc);
- pc.errored_song = dc.next_song;
+ player_lock();
+ pc.errored_song = dc->song;
pc.error = PLAYER_ERROR_FILE;
+ player_unlock();
return false;
- } else if (!decoder_is_starting()) {
+ } else if (!decoder_is_starting(dc)) {
/* the decoder is ready and ok */
+ decoder_unlock(dc);
+
if (audio_format_defined(&player->play_audio_format) &&
!audio_output_all_wait(1))
/* the output devices havn't finished playing
all chunks yet - wait for that */
return true;
- pc.total_time = dc.total_time;
- pc.audio_format = dc.in_audio_format;
- player->play_audio_format = dc.out_audio_format;
- player->size_to_time =
- audioFormatSizeToTime(&dc.out_audio_format);
+ player_lock();
+ pc.total_time = real_song_duration(dc->song, dc->total_time);
+ pc.audio_format = dc->in_audio_format;
+ player_unlock();
+
+ player->play_audio_format = dc->out_audio_format;
player->decoder_starting = false;
if (!player->paused &&
- !audio_output_all_open(&dc.out_audio_format,
+ !audio_output_all_open(&dc->out_audio_format,
player_buffer)) {
- char *uri = song_get_uri(dc.next_song);
+ char *uri = song_get_uri(dc->song);
g_warning("problems opening audio device "
"while playing \"%s\"", uri);
g_free(uri);
- assert(dc.next_song == NULL || dc.next_song->url != NULL);
- pc.errored_song = dc.next_song;
+ player_lock();
pc.error = PLAYER_ERROR_AUDIO;
/* pause: the user may resume playback as soon
as an audio output becomes available */
pc.state = PLAYER_STATE_PAUSE;
+ player_unlock();
+
player->paused = true;
return true;
}
@@ -234,7 +323,8 @@ player_check_decoder_startup(struct player *player)
} else {
/* the decoder is not yet ready; wait
some more */
- notify_wait(&pc.notify);
+ player_wait_decoder(dc);
+ decoder_unlock(dc);
return true;
}
@@ -244,6 +334,8 @@ player_check_decoder_startup(struct player *player)
* Sends a chunk of silence to the audio outputs. This is called when
* there is not enough decoded data in the pipe yet, to prevent
* underruns in the hardware buffers.
+ *
+ * The player lock is not held.
*/
static bool
player_send_silence(struct player *player)
@@ -267,6 +359,7 @@ player_send_silence(struct player *player)
chunk->audio_format = player->play_audio_format;
#endif
+ chunk->times = -1.0; /* undefined time stamp */
chunk->length = num_frames * frame_size;
memset(chunk->data, 0, chunk->length);
@@ -280,15 +373,19 @@ player_send_silence(struct player *player)
/**
* This is the handler for the #PLAYER_COMMAND_SEEK command.
+ *
+ * The player lock is not held.
*/
static bool player_seek_decoder(struct player *player)
{
+ struct song *song = pc.next_song;
+ struct decoder_control *dc = player->dc;
double where;
bool ret;
assert(pc.next_song != NULL);
- if (decoder_current_song() != pc.next_song) {
+ if (decoder_current_song(dc) != song) {
/* the decoder is already decoding the "next" song -
stop it and start the previous song again */
@@ -297,10 +394,9 @@ static bool player_seek_decoder(struct player *player)
/* clear music chunks which might still reside in the
pipe */
music_pipe_clear(player->pipe, player_buffer);
- dc.pipe = player->pipe;
/* re-start the decoder */
- dc_start_async(pc.next_song);
+ player_dc_start(player, player->pipe);
ret = player_wait_for_decoder(player);
if (!ret) {
/* decoder failure */
@@ -331,14 +427,15 @@ static bool player_seek_decoder(struct player *player)
if (where < 0.0)
where = 0.0;
- ret = dc_seek(&pc.notify, where);
+ ret = dc_seek(dc, where + song->start_ms / 1000.0);
if (!ret) {
/* decoder failure */
player_command_finished();
return false;
}
- pc.elapsed_time = where;
+ player->elapsed_time = where;
+
player_command_finished();
player->xfade = XFADE_UNKNOWN;
@@ -351,53 +448,73 @@ static bool player_seek_decoder(struct player *player)
return true;
}
+/**
+ * Player lock must be held before calling.
+ */
static void player_process_command(struct player *player)
{
+ G_GNUC_UNUSED struct decoder_control *dc = player->dc;
+
switch (pc.command) {
case PLAYER_COMMAND_NONE:
- case PLAYER_COMMAND_PLAY:
case PLAYER_COMMAND_STOP:
case PLAYER_COMMAND_EXIT:
case PLAYER_COMMAND_CLOSE_AUDIO:
break;
+ case PLAYER_COMMAND_UPDATE_AUDIO:
+ player_unlock();
+ audio_output_all_enable_disable();
+ player_lock();
+ player_command_finished_locked();
+ break;
+
case PLAYER_COMMAND_QUEUE:
assert(pc.next_song != NULL);
assert(!player->queued);
- assert(dc.pipe == NULL || dc.pipe == player->pipe);
+ assert(dc->pipe == NULL || dc->pipe == player->pipe);
player->queued = true;
- player_command_finished();
+ player_command_finished_locked();
break;
case PLAYER_COMMAND_PAUSE:
+ player_unlock();
+
player->paused = !player->paused;
if (player->paused) {
audio_output_all_pause();
+ player_lock();
+
pc.state = PLAYER_STATE_PAUSE;
} else if (!audio_format_defined(&player->play_audio_format)) {
/* the decoder hasn't provided an audio format
yet - don't open the audio device yet */
+ player_lock();
pc.state = PLAYER_STATE_PLAY;
} else if (audio_output_all_open(&player->play_audio_format, player_buffer)) {
/* unpaused, continue playing */
+ player_lock();
+
pc.state = PLAYER_STATE_PLAY;
} else {
/* the audio device has failed - rollback to
pause mode */
- assert(dc.next_song == NULL || dc.next_song->url != NULL);
- pc.errored_song = dc.next_song;
pc.error = PLAYER_ERROR_AUDIO;
player->paused = true;
+
+ player_lock();
}
- player_command_finished();
+ player_command_finished_locked();
break;
case PLAYER_COMMAND_SEEK:
+ player_unlock();
player_seek_decoder(player);
+ player_lock();
break;
case PLAYER_COMMAND_CANCEL:
@@ -409,80 +526,91 @@ static void player_process_command(struct player *player)
return;
}
- if (dc.pipe != NULL && dc.pipe != player->pipe)
+ if (decoding_next_song(player)) {
/* the decoder is already decoding the song -
stop it and reset the position */
+ player_unlock();
player_dc_stop(player);
+ player_lock();
+ }
pc.next_song = NULL;
player->queued = false;
- player_command_finished();
+ player_command_finished_locked();
+ break;
+
+ case PLAYER_COMMAND_REFRESH:
+ if (audio_format_defined(&player->play_audio_format) &&
+ !player->paused) {
+ player_unlock();
+ audio_output_all_check();
+ player_lock();
+ }
+
+ pc.elapsed_time = audio_output_all_get_elapsed_time();
+ if (pc.elapsed_time < 0.0)
+ pc.elapsed_time = player->elapsed_time;
+
+ player_command_finished_locked();
break;
}
}
+static void
+update_song_tag(struct song *song, const struct tag *new_tag)
+{
+ struct tag *old_tag;
+
+ if (song_is_file(song))
+ /* don't update tags of local files, only remote
+ streams may change tags dynamically */
+ return;
+
+ old_tag = song->tag;
+ song->tag = tag_dup(new_tag);
+
+ if (old_tag != NULL)
+ tag_free(old_tag);
+
+ /* the main thread will update the playlist version when he
+ receives this event */
+ event_pipe_emit(PIPE_EVENT_TAG);
+
+ /* notify all clients that the tag of the current song has
+ changed */
+ idle_add(IDLE_PLAYER);
+}
+
/**
* Plays a #music_chunk object (after applying software volume). If
* it contains a (stream) tag, copy it to the current song, so MPD's
* playlist reflects the new stream tag.
+ *
+ * Player lock is not held.
*/
static bool
play_chunk(struct song *song, struct music_chunk *chunk,
- const struct audio_format *format, double sizeToTime)
+ const struct audio_format *format)
{
- bool success;
-
assert(music_chunk_check_format(chunk, format));
- if (chunk->tag != NULL) {
- if (!song_is_file(song)) {
- /* always update the tag of remote streams */
- struct tag *old_tag = song->tag;
-
- song->tag = tag_dup(chunk->tag);
-
- if (old_tag != NULL)
- tag_free(old_tag);
-
- /* the main thread will update the playlist
- version when he receives this event */
- event_pipe_emit(PIPE_EVENT_TAG);
-
- /* notify all clients that the tag of the
- current song has changed */
- idle_add(IDLE_PLAYER);
- }
- }
+ if (chunk->tag != NULL)
+ update_song_tag(song, chunk->tag);
if (chunk->length == 0) {
music_buffer_return(player_buffer, chunk);
return true;
}
- pc.elapsed_time = chunk->times;
pc.bit_rate = chunk->bit_rate;
- /* apply software volume */
-
- success = pcm_volume(chunk->data, chunk->length,
- format, pc.software_volume);
- if (!success) {
- g_warning("pcm_volume() failed on %u:%u:%u",
- format->sample_rate, format->bits, format->channels);
- pc.errored_song = dc.current_song;
- pc.error = PLAYER_ERROR_AUDIO;
- return false;
- }
-
/* send the chunk to the audio outputs */
- if (!audio_output_all_play(chunk)) {
- pc.errored_song = dc.current_song;
- pc.error = PLAYER_ERROR_AUDIO;
+ if (!audio_output_all_play(chunk))
return false;
- }
- pc.total_play_time += sizeToTime * chunk->length;
+ pc.total_play_time += (double)chunk->length /
+ audio_format_time_to_size(format);
return true;
}
@@ -495,6 +623,7 @@ play_chunk(struct song *song, struct music_chunk *chunk,
static bool
play_next_chunk(struct player *player)
{
+ struct decoder_control *dc = player->dc;
struct music_chunk *chunk = NULL;
unsigned cross_fade_position;
bool success;
@@ -505,12 +634,12 @@ play_next_chunk(struct player *player)
return true;
if (player->xfade == XFADE_ENABLED &&
- dc.pipe != NULL && dc.pipe != player->pipe &&
+ decoding_next_song(player) &&
(cross_fade_position = music_pipe_size(player->pipe))
<= player->cross_fade_chunks) {
/* perform cross fade */
struct music_chunk *other_chunk =
- music_pipe_shift(dc.pipe);
+ music_pipe_shift(dc->pipe);
if (!player->cross_fading) {
/* beginning of the cross fade - adjust
@@ -524,6 +653,7 @@ play_next_chunk(struct player *player)
if (other_chunk != NULL) {
chunk = music_pipe_shift(player->pipe);
assert(chunk != NULL);
+ assert(chunk->other == NULL);
/* don't send the tags of the new song (which
is being faded in) yet; postpone it until
@@ -533,21 +663,30 @@ play_next_chunk(struct player *player)
other_chunk->tag);
other_chunk->tag = NULL;
- cross_fade_apply(chunk, other_chunk,
- &dc.out_audio_format,
- cross_fade_position,
- player->cross_fade_chunks);
- music_buffer_return(player_buffer, other_chunk);
+ if (isnan(pc.mixramp_delay_seconds)) {
+ chunk->mix_ratio = ((float)cross_fade_position)
+ / player->cross_fade_chunks;
+ } else {
+ chunk->mix_ratio = nan("");
+ }
+
+ chunk->other = other_chunk;
} else {
/* there are not enough decoded chunks yet */
- if (decoder_is_idle()) {
+
+ decoder_lock(dc);
+
+ if (decoder_is_idle(dc)) {
/* the decoder isn't running, abort
cross fading */
+ decoder_unlock(dc);
+
player->xfade = XFADE_DISABLED;
} else {
/* wait for the decoder */
- notify_signal(&dc.notify);
- notify_wait(&pc.notify);
+ decoder_signal(dc);
+ player_wait_decoder(dc);
+ decoder_unlock(dc);
return true;
}
@@ -569,27 +708,34 @@ play_next_chunk(struct player *player)
/* play the current chunk */
- success = play_chunk(player->song, chunk, &player->play_audio_format,
- player->size_to_time);
+ success = play_chunk(player->song, chunk, &player->play_audio_format);
if (!success) {
music_buffer_return(player_buffer, chunk);
+ player_lock();
+
+ pc.error = PLAYER_ERROR_AUDIO;
+
/* pause: the user may resume playback as soon as an
audio output becomes available */
pc.state = PLAYER_STATE_PAUSE;
player->paused = true;
+ player_unlock();
+
return false;
}
/* this formula should prevent that the decoder gets woken up
with each chunk; it is more efficient to make it decode a
larger block at a time */
- if (!decoder_is_idle() &&
- music_pipe_size(dc.pipe) <= (pc.buffered_before_play +
+ decoder_lock(dc);
+ if (!decoder_is_idle(dc) &&
+ music_pipe_size(dc->pipe) <= (pc.buffered_before_play +
music_buffer_size(player_buffer) * 3) / 4)
- notify_signal(&dc.notify);
+ decoder_signal(dc);
+ decoder_unlock(dc);
return true;
}
@@ -599,15 +745,25 @@ play_next_chunk(struct player *player)
* has consumed all chunks of the current song, and we should start
* sending chunks from the next one.
*
+ * The player lock is not held.
+ *
* @return true on success, false on error (playback will be stopped)
*/
static bool
player_song_border(struct player *player)
{
+ char *uri;
+
player->xfade = XFADE_UNKNOWN;
+ uri = song_get_uri(player->song);
+ g_message("played \"%s\"", uri);
+ g_free(uri);
+
music_pipe_free(player->pipe);
- player->pipe = dc.pipe;
+ player->pipe = player->dc->pipe;
+
+ audio_output_all_song_border();
if (!player_wait_for_decoder(player))
return false;
@@ -620,54 +776,59 @@ player_song_border(struct player *player)
* basically a state machine, which multiplexes data between the
* decoder thread and the output threads.
*/
-static void do_play(void)
+static void do_play(struct decoder_control *dc)
{
struct player player = {
+ .dc = dc,
.buffering = true,
.decoder_starting = false,
.paused = false,
- .queued = false,
+ .queued = true,
.song = NULL,
.xfade = XFADE_UNKNOWN,
.cross_fading = false,
.cross_fade_chunks = 0,
.cross_fade_tag = NULL,
- .size_to_time = 0.0,
+ .elapsed_time = 0.0,
};
+ player_unlock();
+
player.pipe = music_pipe_new();
- dc.buffer = player_buffer;
- dc.pipe = player.pipe;
- dc_start(&pc.notify, pc.next_song);
+ player_dc_start(&player, player.pipe);
if (!player_wait_for_decoder(&player)) {
player_dc_stop(&player);
player_command_finished();
music_pipe_free(player.pipe);
event_pipe_emit(PIPE_EVENT_PLAYLIST);
+ player_lock();
return;
}
- pc.elapsed_time = 0;
+ player_lock();
pc.state = PLAYER_STATE_PLAY;
- player_command_finished();
+ player_command_finished_locked();
while (true) {
player_process_command(&player);
if (pc.command == PLAYER_COMMAND_STOP ||
pc.command == PLAYER_COMMAND_EXIT ||
pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
+ player_unlock();
audio_output_all_cancel();
break;
}
+ player_unlock();
+
if (player.buffering) {
/* buffering at the start of the song - wait
until the buffer is large enough, to
prevent stuttering on slow machines */
if (music_pipe_size(player.pipe) < pc.buffered_before_play &&
- !decoder_is_idle()) {
+ !decoder_lock_is_idle(dc)) {
/* not enough decoded buffer space yet */
if (!player.paused &&
@@ -676,7 +837,11 @@ static void do_play(void)
!player_send_silence(&player))
break;
- notify_wait(&pc.notify);
+ decoder_lock(dc);
+ /* XXX race condition: check decoder again */
+ player_wait_decoder(dc);
+ decoder_unlock(dc);
+ player_lock();
continue;
} else {
/* buffering is complete */
@@ -687,10 +852,19 @@ static void do_play(void)
if (player.decoder_starting) {
/* wait until the decoder is initialized completely */
bool success;
+ const struct song *song;
success = player_check_decoder_startup(&player);
if (!success)
break;
+
+ /* seek to the beginning of the range */
+ song = decoder_current_song(dc);
+ if (song != NULL && song->start_ms > 0 &&
+ !dc_seek(dc, song->start_ms / 1000.0))
+ player_dc_stop(&player);
+
+ player_lock();
continue;
}
@@ -698,30 +872,34 @@ static void do_play(void)
/*
music_pipe_check_format(&play_audio_format,
player.next_song_chunk,
- &dc.out_audio_format);
+ &dc->out_audio_format);
*/
#endif
- if (decoder_is_idle() && player.queued) {
+ if (decoder_lock_is_idle(dc) && player.queued &&
+ dc->pipe == player.pipe) {
/* the decoder has finished the current song;
make it decode the next song */
- assert(pc.next_song != NULL);
- assert(dc.pipe == NULL || dc.pipe == player.pipe);
+ assert(dc->pipe == NULL || dc->pipe == player.pipe);
- player.queued = false;
- dc.pipe = music_pipe_new();
- dc_start_async(pc.next_song);
+ player_dc_start(&player, music_pipe_new());
}
- if (dc.pipe != NULL && dc.pipe != player.pipe &&
+ if (decoding_next_song(&player) &&
player.xfade == XFADE_UNKNOWN &&
- !decoder_is_starting()) {
+ !decoder_lock_is_starting(dc)) {
/* enable cross fading in this song? if yes,
calculate how many chunks will be required
for it */
player.cross_fade_chunks =
- cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
- &dc.out_audio_format,
+ cross_fade_calc(pc.cross_fade_seconds, dc->total_time,
+ pc.mixramp_db,
+ pc.mixramp_delay_seconds,
+ dc->replay_gain_db,
+ dc->replay_gain_prev_db,
+ dc->mixramp_start,
+ dc->mixramp_prev_end,
+ &dc->out_audio_format,
&player.play_audio_format,
music_buffer_size(player_buffer) -
pc.buffered_before_play);
@@ -734,9 +912,13 @@ static void do_play(void)
player.xfade = XFADE_DISABLED;
}
- if (player.paused)
- notify_wait(&pc.notify);
- else if (music_pipe_size(player.pipe) > 0) {
+ if (player.paused) {
+ player_lock();
+
+ if (pc.command == PLAYER_COMMAND_NONE)
+ player_wait();
+ continue;
+ } else if (music_pipe_size(player.pipe) > 0) {
/* at least one music chunk is ready - send it
to the audio output */
@@ -748,17 +930,21 @@ static void do_play(void)
/* XXX synchronize in a better way */
g_usleep(10000);
- } else if (dc.pipe != NULL && dc.pipe != player.pipe) {
+ } else if (decoding_next_song(&player)) {
/* at the beginning of a new song */
if (!player_song_border(&player))
break;
- } else if (decoder_is_idle()) {
+ } else if (decoder_lock_is_idle(dc)) {
/* check the size of the pipe again, because
the decoder thread may have added something
since we last checked */
- if (music_pipe_size(player.pipe) == 0)
+ if (music_pipe_size(player.pipe) == 0) {
+ /* wait for the hardware to finish
+ playback */
+ audio_output_all_drain();
break;
+ }
} else {
/* the decoder is too busy and hasn't provided
new PCM data in time: send silence (if the
@@ -766,11 +952,8 @@ static void do_play(void)
if (!player_send_silence(&player))
break;
}
- }
- if (player.queued) {
- assert(pc.next_song != NULL);
- pc.next_song = NULL;
+ player_lock();
}
player_dc_stop(&player);
@@ -781,38 +964,61 @@ static void do_play(void)
if (player.cross_fade_tag != NULL)
tag_free(player.cross_fade_tag);
+ player_lock();
+
+ if (player.queued) {
+ assert(pc.next_song != NULL);
+ pc.next_song = NULL;
+ }
+
pc.state = PLAYER_STATE_STOP;
+
+ player_unlock();
+
event_pipe_emit(PIPE_EVENT_PLAYLIST);
+
+ player_lock();
}
static gpointer player_task(G_GNUC_UNUSED gpointer arg)
{
- decoder_thread_start();
+ struct decoder_control dc;
+
+ dc_init(&dc);
+ decoder_thread_start(&dc);
player_buffer = music_buffer_new(pc.buffer_chunks);
+ player_lock();
+
while (1) {
switch (pc.command) {
- case PLAYER_COMMAND_PLAY:
case PLAYER_COMMAND_QUEUE:
assert(pc.next_song != NULL);
- do_play();
+ do_play(&dc);
break;
case PLAYER_COMMAND_STOP:
+ player_unlock();
audio_output_all_cancel();
+ player_lock();
+
/* fall through */
case PLAYER_COMMAND_SEEK:
case PLAYER_COMMAND_PAUSE:
pc.next_song = NULL;
- player_command_finished();
+ player_command_finished_locked();
break;
case PLAYER_COMMAND_CLOSE_AUDIO:
- audio_output_all_close();
- player_command_finished();
+ player_unlock();
+
+ audio_output_all_release();
+
+ player_lock();
+ player_command_finished_locked();
#ifndef NDEBUG
/* in the DEBUG build, check for leaked
@@ -824,25 +1030,39 @@ static gpointer player_task(G_GNUC_UNUSED gpointer arg)
break;
+ case PLAYER_COMMAND_UPDATE_AUDIO:
+ player_unlock();
+ audio_output_all_enable_disable();
+ player_lock();
+ player_command_finished_locked();
+ break;
+
case PLAYER_COMMAND_EXIT:
- dc_quit();
+ player_unlock();
+
+ dc_quit(&dc);
+ dc_deinit(&dc);
audio_output_all_close();
music_buffer_free(player_buffer);
+
player_command_finished();
- g_thread_exit(NULL);
- break;
+ return NULL;
case PLAYER_COMMAND_CANCEL:
pc.next_song = NULL;
- player_command_finished();
+ player_command_finished_locked();
+ break;
+
+ case PLAYER_COMMAND_REFRESH:
+ /* no-op when not playing */
+ player_command_finished_locked();
break;
case PLAYER_COMMAND_NONE:
- notify_wait(&pc.notify);
+ player_wait();
break;
}
}
- return NULL;
}
void player_create(void)