diff options
Diffstat (limited to '')
-rw-r--r-- | src/player_thread.c | 345 |
1 files changed, 181 insertions, 164 deletions
diff --git a/src/player_thread.c b/src/player_thread.c index 52788e518..c0243fa00 100644 --- a/src/player_thread.c +++ b/src/player_thread.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2010 The Music Player Daemon Project + * Copyright (C) 2003-2011 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -48,6 +48,8 @@ enum xfade_state { }; struct player { + struct player_control *pc; + struct decoder_control *dc; struct music_pipe *pipe; @@ -118,26 +120,28 @@ struct player { * 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. + * precisely. */ float elapsed_time; }; static struct music_buffer *player_buffer; -static void player_command_finished_locked(void) +static void +player_command_finished_locked(struct player_control *pc) { - assert(pc.command != PLAYER_COMMAND_NONE); + assert(pc->command != PLAYER_COMMAND_NONE); - pc.command = PLAYER_COMMAND_NONE; + pc->command = PLAYER_COMMAND_NONE; g_cond_signal(main_cond); } -static void player_command_finished(void) +static void +player_command_finished(struct player_control *pc) { - player_lock(); - player_command_finished_locked(); - player_unlock(); + player_lock(pc); + player_command_finished_locked(pc); + player_unlock(pc); } /** @@ -148,17 +152,18 @@ static void player_command_finished(void) static void player_dc_start(struct player *player, struct music_pipe *pipe) { + struct player_control *pc = player->pc; struct decoder_control *dc = player->dc; - assert(player->queued || pc.command == PLAYER_COMMAND_SEEK); - assert(pc.next_song != NULL); + assert(player->queued || pc->command == PLAYER_COMMAND_SEEK); + assert(pc->next_song != NULL); - unsigned start_ms = pc.next_song->start_ms; - if (pc.command == PLAYER_COMMAND_SEEK) - start_ms += (unsigned)(pc.seek_where * 1000); + unsigned start_ms = pc->next_song->start_ms; + if (pc->command == PLAYER_COMMAND_SEEK) + start_ms += (unsigned)(pc->seek_where * 1000); - dc_start(dc, pc.next_song, - start_ms, pc.next_song->end_ms, + dc_start(dc, pc->next_song, + start_ms, pc->next_song->end_ms, player_buffer, pipe); } @@ -222,41 +227,42 @@ player_dc_stop(struct player *player) static bool player_wait_for_decoder(struct player *player) { + struct player_control *pc = player->pc; struct decoder_control *dc = player->dc; - assert(player->queued || pc.command == PLAYER_COMMAND_SEEK); - assert(pc.next_song != NULL); + 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_unlock(); + player_lock(pc); + pc->errored_song = dc->song; + pc->error = PLAYER_ERROR_FILE; + pc->next_song = NULL; + player_unlock(pc); return false; } - player->song = pc.next_song; + player->song = pc->next_song; player->elapsed_time = 0.0; /* set the "starting" flag, which will be cleared by player_check_decoder_startup() */ player->decoder_starting = true; - player_lock(); + player_lock(pc); /* 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); + 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; + pc->next_song = NULL; - player_unlock(); + player_unlock(pc); /* call syncPlaylistWithQueue() in the main thread */ event_pipe_emit(PIPE_EVENT_PLAYLIST); @@ -293,17 +299,19 @@ real_song_duration(const struct song *song, double decoder_duration) static bool player_open_output(struct player *player) { + struct player_control *pc = player->pc; + assert(audio_format_defined(&player->play_audio_format)); - assert(pc.state == PLAYER_STATE_PLAY || - pc.state == PLAYER_STATE_PAUSE); + assert(pc->state == PLAYER_STATE_PLAY || + pc->state == PLAYER_STATE_PAUSE); if (audio_output_all_open(&player->play_audio_format, player_buffer)) { player->output_open = true; player->paused = false; - player_lock(); - pc.state = PLAYER_STATE_PLAY; - player_unlock(); + player_lock(pc); + pc->state = PLAYER_STATE_PLAY; + player_unlock(pc); return true; } else { @@ -313,10 +321,10 @@ player_open_output(struct player *player) audio output becomes available */ player->paused = true; - player_lock(); - pc.error = PLAYER_ERROR_AUDIO; - pc.state = PLAYER_STATE_PAUSE; - player_unlock(); + player_lock(pc); + pc->error = PLAYER_ERROR_AUDIO; + pc->state = PLAYER_STATE_PAUSE; + player_unlock(pc); return false; } @@ -332,6 +340,7 @@ player_open_output(struct player *player) static bool player_check_decoder_startup(struct player *player) { + struct player_control *pc = player->pc; struct decoder_control *dc = player->dc; assert(player->decoder_starting); @@ -342,10 +351,10 @@ player_check_decoder_startup(struct player *player) /* the decoder failed */ decoder_unlock(dc); - player_lock(); - pc.errored_song = dc->song; - pc.error = PLAYER_ERROR_FILE; - player_unlock(); + player_lock(pc); + pc->errored_song = dc->song; + pc->error = PLAYER_ERROR_FILE; + player_unlock(pc); return false; } else if (!decoder_is_starting(dc)) { @@ -354,15 +363,15 @@ player_check_decoder_startup(struct player *player) decoder_unlock(dc); if (player->output_open && - !audio_output_all_wait(1)) + !audio_output_all_wait(pc, 1)) /* the output devices havn't finished playing all chunks yet - wait for that */ return true; - player_lock(); - pc.total_time = real_song_duration(dc->song, dc->total_time); - pc.audio_format = dc->in_audio_format; - player_unlock(); + player_lock(pc); + pc->total_time = real_song_duration(dc->song, dc->total_time); + pc->audio_format = dc->in_audio_format; + player_unlock(pc); player->play_audio_format = dc->out_audio_format; player->decoder_starting = false; @@ -380,7 +389,7 @@ player_check_decoder_startup(struct player *player) } else { /* the decoder is not yet ready; wait some more */ - player_wait_decoder(dc); + player_wait_decoder(pc, dc); decoder_unlock(dc); return true; @@ -435,10 +444,11 @@ player_send_silence(struct player *player) */ static bool player_seek_decoder(struct player *player) { - struct song *song = pc.next_song; + struct player_control *pc = player->pc; + struct song *song = pc->next_song; struct decoder_control *dc = player->dc; - assert(pc.next_song != NULL); + assert(pc->next_song != NULL); if (decoder_current_song(dc) != song) { /* the decoder is already decoding the "next" song - @@ -454,7 +464,7 @@ static bool player_seek_decoder(struct player *player) player_dc_start(player, player->pipe); if (!player_wait_for_decoder(player)) { /* decoder failure */ - player_command_finished(); + player_command_finished(pc); return false; } } else { @@ -466,7 +476,7 @@ static bool player_seek_decoder(struct player *player) player->pipe = dc->pipe; } - pc.next_song = NULL; + pc->next_song = NULL; player->queued = false; } @@ -475,28 +485,28 @@ static bool player_seek_decoder(struct player *player) while (player->decoder_starting) { if (!player_check_decoder_startup(player)) { /* decoder failure */ - player_command_finished(); + player_command_finished(pc); return false; } } /* send the SEEK command */ - double where = pc.seek_where; - if (where > pc.total_time) - where = pc.total_time - 0.1; + double where = pc->seek_where; + if (where > pc->total_time) + where = pc->total_time - 0.1; if (where < 0.0) where = 0.0; if (!dc_seek(dc, where + song->start_ms / 1000.0)) { /* decoder failure */ - player_command_finished(); + player_command_finished(pc); return false; } player->elapsed_time = where; - player_command_finished(); + player_command_finished(pc); player->xfade = XFADE_UNKNOWN; @@ -513,9 +523,10 @@ static bool player_seek_decoder(struct player *player) */ static void player_process_command(struct player *player) { + struct player_control *pc = player->pc; G_GNUC_UNUSED struct decoder_control *dc = player->dc; - switch (pc.command) { + switch (pc->command) { case PLAYER_COMMAND_NONE: case PLAYER_COMMAND_STOP: case PLAYER_COMMAND_EXIT: @@ -523,85 +534,85 @@ static void player_process_command(struct player *player) break; case PLAYER_COMMAND_UPDATE_AUDIO: - player_unlock(); + player_unlock(pc); audio_output_all_enable_disable(); - player_lock(); - player_command_finished_locked(); + player_lock(pc); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_QUEUE: - assert(pc.next_song != NULL); + assert(pc->next_song != NULL); assert(!player->queued); assert(!player_dc_at_next_song(player)); player->queued = true; - player_command_finished_locked(); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_PAUSE: - player_unlock(); + player_unlock(pc); player->paused = !player->paused; if (player->paused) { audio_output_all_pause(); - player_lock(); + player_lock(pc); - pc.state = PLAYER_STATE_PAUSE; + 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(); + player_lock(pc); - pc.state = PLAYER_STATE_PLAY; + pc->state = PLAYER_STATE_PLAY; } else { player_open_output(player); - player_lock(); + player_lock(pc); } - player_command_finished_locked(); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_SEEK: - player_unlock(); + player_unlock(pc); player_seek_decoder(player); - player_lock(); + player_lock(pc); break; case PLAYER_COMMAND_CANCEL: - if (pc.next_song == NULL) { + if (pc->next_song == NULL) { /* the cancel request arrived too late, we're already playing the queued song... stop everything now */ - pc.command = PLAYER_COMMAND_STOP; + pc->command = PLAYER_COMMAND_STOP; return; } if (player_dc_at_next_song(player)) { /* the decoder is already decoding the song - stop it and reset the position */ - player_unlock(); + player_unlock(pc); player_dc_stop(player); - player_lock(); + player_lock(pc); } - pc.next_song = NULL; + pc->next_song = NULL; player->queued = false; - player_command_finished_locked(); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_REFRESH: if (player->output_open && !player->paused) { - player_unlock(); + player_unlock(pc); audio_output_all_check(); - player_lock(); + player_lock(pc); } - pc.elapsed_time = audio_output_all_get_elapsed_time(); - if (pc.elapsed_time < 0.0) - pc.elapsed_time = player->elapsed_time; + 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(); + player_command_finished_locked(pc); break; } } @@ -637,7 +648,8 @@ update_song_tag(struct song *song, const struct tag *new_tag) * Player lock is not held. */ static bool -play_chunk(struct song *song, struct music_chunk *chunk, +play_chunk(struct player_control *pc, + struct song *song, struct music_chunk *chunk, const struct audio_format *format) { assert(music_chunk_check_format(chunk, format)); @@ -650,16 +662,16 @@ play_chunk(struct song *song, struct music_chunk *chunk, return true; } - player_lock(); - pc.bit_rate = chunk->bit_rate; - player_unlock(); + player_lock(pc); + pc->bit_rate = chunk->bit_rate; + player_unlock(pc); /* send the chunk to the audio outputs */ if (!audio_output_all_play(chunk)) return false; - pc.total_play_time += (double)chunk->length / + pc->total_play_time += (double)chunk->length / audio_format_time_to_size(format); return true; } @@ -673,9 +685,10 @@ play_chunk(struct song *song, struct music_chunk *chunk, static bool play_next_chunk(struct player *player) { + struct player_control *pc = player->pc; struct decoder_control *dc = player->dc; - if (!audio_output_all_wait(64)) + if (!audio_output_all_wait(pc, 64)) /* the output pipe is still large enough, don't send another chunk */ return true; @@ -712,7 +725,7 @@ play_next_chunk(struct player *player) other_chunk->tag); other_chunk->tag = NULL; - if (isnan(pc.mixramp_delay_seconds)) { + if (isnan(pc->mixramp_delay_seconds)) { chunk->mix_ratio = ((float)cross_fade_position) / player->cross_fade_chunks; } else { @@ -747,7 +760,7 @@ play_next_chunk(struct player *player) } else { /* wait for the decoder */ decoder_signal(dc); - player_wait_decoder(dc); + player_wait_decoder(pc, dc); decoder_unlock(dc); return true; @@ -770,19 +783,20 @@ play_next_chunk(struct player *player) /* play the current chunk */ - if (!play_chunk(player->song, chunk, &player->play_audio_format)) { + if (!play_chunk(player->pc, player->song, chunk, + &player->play_audio_format)) { music_buffer_return(player_buffer, chunk); - player_lock(); + player_lock(pc); - pc.error = PLAYER_ERROR_AUDIO; + pc->error = PLAYER_ERROR_AUDIO; /* pause: the user may resume playback as soon as an audio output becomes available */ - pc.state = PLAYER_STATE_PAUSE; + pc->state = PLAYER_STATE_PAUSE; player->paused = true; - player_unlock(); + player_unlock(pc); return false; } @@ -792,7 +806,7 @@ play_next_chunk(struct player *player) larger block at a time */ decoder_lock(dc); if (!decoder_is_idle(dc) && - music_pipe_size(dc->pipe) <= (pc.buffered_before_play + + music_pipe_size(dc->pipe) <= (pc->buffered_before_play + music_buffer_size(player_buffer) * 3) / 4) decoder_signal(dc); decoder_unlock(dc); @@ -834,9 +848,10 @@ 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(struct decoder_control *dc) +static void do_play(struct player_control *pc, struct decoder_control *dc) { struct player player = { + .pc = pc, .dc = dc, .buffering = true, .decoder_starting = false, @@ -851,46 +866,46 @@ static void do_play(struct decoder_control *dc) .elapsed_time = 0.0, }; - player_unlock(); + player_unlock(pc); player.pipe = music_pipe_new(); player_dc_start(&player, player.pipe); if (!player_wait_for_decoder(&player)) { player_dc_stop(&player); - player_command_finished(); + player_command_finished(pc); music_pipe_free(player.pipe); event_pipe_emit(PIPE_EVENT_PLAYLIST); - player_lock(); + player_lock(pc); return; } - player_lock(); + player_lock(pc); + pc->state = PLAYER_STATE_PLAY; - if (pc.command == PLAYER_COMMAND_SEEK) - player.elapsed_time = pc.seek_where; + if (pc->command == PLAYER_COMMAND_SEEK) + player.elapsed_time = pc->seek_where; - pc.state = PLAYER_STATE_PLAY; - player_command_finished_locked(); + player_command_finished_locked(pc); 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(); + if (pc->command == PLAYER_COMMAND_STOP || + pc->command == PLAYER_COMMAND_EXIT || + pc->command == PLAYER_COMMAND_CLOSE_AUDIO) { + player_unlock(pc); audio_output_all_cancel(); break; } - player_unlock(); + player_unlock(pc); 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 && + if (music_pipe_size(player.pipe) < pc->buffered_before_play && !decoder_lock_is_idle(dc)) { /* not enough decoded buffer space yet */ @@ -902,9 +917,9 @@ static void do_play(struct decoder_control *dc) decoder_lock(dc); /* XXX race condition: check decoder again */ - player_wait_decoder(dc); + player_wait_decoder(pc, dc); decoder_unlock(dc); - player_lock(); + player_lock(pc); continue; } else { /* buffering is complete */ @@ -918,7 +933,7 @@ static void do_play(struct decoder_control *dc) if (!player_check_decoder_startup(&player)) break; - player_lock(); + player_lock(pc); continue; } @@ -947,9 +962,9 @@ static void do_play(struct decoder_control *dc) calculate how many chunks will be required for it */ player.cross_fade_chunks = - cross_fade_calc(pc.cross_fade_seconds, dc->total_time, - pc.mixramp_db, - pc.mixramp_delay_seconds, + 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, @@ -957,7 +972,7 @@ static void do_play(struct decoder_control *dc) &dc->out_audio_format, &player.play_audio_format, music_buffer_size(player_buffer) - - pc.buffered_before_play); + pc->buffered_before_play); if (player.cross_fade_chunks > 0) { player.xfade = XFADE_ENABLED; player.cross_fading = false; @@ -968,10 +983,10 @@ static void do_play(struct decoder_control *dc) } if (player.paused) { - player_lock(); + player_lock(pc); - if (pc.command == PLAYER_COMMAND_NONE) - player_wait(); + if (pc->command == PLAYER_COMMAND_NONE) + player_wait(pc); continue; } else if (!music_pipe_empty(player.pipe)) { /* at least one music chunk is ready - send it @@ -1008,7 +1023,7 @@ static void do_play(struct decoder_control *dc) break; } - player_lock(); + player_lock(pc); } player_dc_stop(&player); @@ -1019,113 +1034,115 @@ static void do_play(struct decoder_control *dc) if (player.cross_fade_tag != NULL) tag_free(player.cross_fade_tag); - player_lock(); + player_lock(pc); if (player.queued) { - assert(pc.next_song != NULL); - pc.next_song = NULL; + assert(pc->next_song != NULL); + pc->next_song = NULL; } - pc.state = PLAYER_STATE_STOP; + pc->state = PLAYER_STATE_STOP; - player_unlock(); + player_unlock(pc); event_pipe_emit(PIPE_EVENT_PLAYLIST); - player_lock(); + player_lock(pc); } -static gpointer player_task(G_GNUC_UNUSED gpointer arg) +static gpointer +player_task(gpointer arg) { - struct decoder_control dc; + struct player_control *pc = arg; - dc_init(&dc); - decoder_thread_start(&dc); + struct decoder_control *dc = dc_new(pc->cond); + decoder_thread_start(dc); - player_buffer = music_buffer_new(pc.buffer_chunks); + player_buffer = music_buffer_new(pc->buffer_chunks); - player_lock(); + player_lock(pc); while (1) { - switch (pc.command) { + switch (pc->command) { case PLAYER_COMMAND_SEEK: case PLAYER_COMMAND_QUEUE: - assert(pc.next_song != NULL); + assert(pc->next_song != NULL); - do_play(&dc); + do_play(pc, dc); break; case PLAYER_COMMAND_STOP: - player_unlock(); + player_unlock(pc); audio_output_all_cancel(); - player_lock(); + player_lock(pc); /* fall through */ case PLAYER_COMMAND_PAUSE: - pc.next_song = NULL; - player_command_finished_locked(); + pc->next_song = NULL; + player_command_finished_locked(pc); break; case PLAYER_COMMAND_CLOSE_AUDIO: - player_unlock(); + player_unlock(pc); audio_output_all_release(); - player_lock(); - player_command_finished_locked(); + player_lock(pc); + player_command_finished_locked(pc); #ifndef NDEBUG /* in the DEBUG build, check for leaked music_chunk objects by freeing the music_buffer */ music_buffer_free(player_buffer); - player_buffer = music_buffer_new(pc.buffer_chunks); + player_buffer = music_buffer_new(pc->buffer_chunks); #endif break; case PLAYER_COMMAND_UPDATE_AUDIO: - player_unlock(); + player_unlock(pc); audio_output_all_enable_disable(); - player_lock(); - player_command_finished_locked(); + player_lock(pc); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_EXIT: - player_unlock(); + player_unlock(pc); - dc_quit(&dc); - dc_deinit(&dc); + dc_quit(dc); + dc_free(dc); audio_output_all_close(); music_buffer_free(player_buffer); - player_command_finished(); + player_command_finished(pc); return NULL; case PLAYER_COMMAND_CANCEL: - pc.next_song = NULL; - player_command_finished_locked(); + pc->next_song = NULL; + player_command_finished_locked(pc); break; case PLAYER_COMMAND_REFRESH: /* no-op when not playing */ - player_command_finished_locked(); + player_command_finished_locked(pc); break; case PLAYER_COMMAND_NONE: - player_wait(); + player_wait(pc); break; } } } -void player_create(void) +void +player_create(struct player_control *pc) { - assert(pc.thread == NULL); + assert(pc->thread == NULL); GError *e = NULL; - pc.thread = g_thread_create(player_task, NULL, true, &e); - if (pc.thread == NULL) + pc->thread = g_thread_create(player_task, pc, true, &e); + if (pc->thread == NULL) MPD_ERROR("Failed to spawn player task: %s", e->message); } |