/** * 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 player_control *pc = player->pc; struct song *song = pc->next_song; struct decoder_control *dc = player->dc; assert(pc->next_song != NULL); if (decoder_current_song(dc) != song) { /* the decoder is already decoding the "next" song - stop it and start the previous song again */ player_dc_stop(player); /* clear music chunks which might still reside in the pipe */ music_pipe_clear(player->pipe, player_buffer); /* re-start the decoder */ player_dc_start(player, player->pipe); if (!player_wait_for_decoder(player)) { /* decoder failure */ player_command_finished(pc); return false; } } else { if (!player_dc_at_current_song(player)) { /* the decoder is already decoding the "next" song, but it is the same song file; exchange the pipe */ music_pipe_clear(player->pipe, player_buffer); music_pipe_free(player->pipe); player->pipe = dc->pipe; } pc->next_song = NULL; player->queued = false; } /* wait for the decoder to complete initialization */ while (player->decoder_starting) { if (!player_check_decoder_startup(player)) { /* decoder failure */ 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; if (where < 0.0) where = 0.0; if (!dc_seek(dc, where + song->start_ms / 1000.0)) { /* decoder failure */ player_command_finished(pc); return false; } player->elapsed_time = where; player_command_finished(pc); player->xfade = XFADE_UNKNOWN; /* re-fill the buffer after seeking */ player->buffering = true; audio_output_all_cancel(); return true; }
/* * The main loop of the player thread, during playback. This is * basically a state machine, which multiplexes data between the * decoder thread and the output threads. */ static void do_play(struct decoder_control *dc) { struct player player = { .dc = dc, .buffering = true, .decoder_starting = false, .paused = false, .queued = true, .song = NULL, .xfade = XFADE_UNKNOWN, .cross_fading = false, .cross_fade_chunks = 0, .cross_fade_tag = NULL, .elapsed_time = 0.0, }; player_unlock(); player.pipe = music_pipe_new(); 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; } player_lock(); pc.state = PLAYER_STATE_PLAY; 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_lock_is_idle(dc)) { /* not enough decoded buffer space yet */ if (!player.paused && audio_format_defined(&player.play_audio_format) && audio_output_all_check() < 4 && !player_send_silence(&player)) break; decoder_lock(dc); /* XXX race condition: check decoder again */ player_wait_decoder(dc); decoder_unlock(dc); player_lock(); continue; } else { /* buffering is complete */ player.buffering = false; } } 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; } #ifndef NDEBUG /* music_pipe_check_format(&play_audio_format, player.next_song_chunk, &dc->out_audio_format); */ #endif 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(dc->pipe == NULL || dc->pipe == player.pipe); player_dc_start(&player, music_pipe_new()); } if (decoding_next_song(&player) && player.xfade == XFADE_UNKNOWN && !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, 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); if (player.cross_fade_chunks > 0) { player.xfade = XFADE_ENABLED; player.cross_fading = false; } else /* cross fading is disabled or the next song is too short */ player.xfade = XFADE_DISABLED; } 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 */ play_next_chunk(&player); } else if (audio_output_all_check() > 0) { /* not enough data from decoder, but the output thread is still busy, so it's okay */ /* XXX synchronize in a better way */ g_usleep(10000); } else if (decoding_next_song(&player)) { /* at the beginning of a new song */ if (!player_song_border(&player)) break; } 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) { /* 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 output pipe is empty) */ if (!player_send_silence(&player)) break; } player_lock(); } player_dc_stop(&player); music_pipe_clear(player.pipe, player_buffer); music_pipe_free(player.pipe); 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) { 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_QUEUE: assert(pc.next_song != NULL); 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_locked(); break; case PLAYER_COMMAND_CLOSE_AUDIO: player_unlock(); audio_output_all_release(); player_lock(); player_command_finished_locked(); #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); #endif break; case PLAYER_COMMAND_UPDATE_AUDIO: player_unlock(); audio_output_all_enable_disable(); player_lock(); player_command_finished_locked(); break; case PLAYER_COMMAND_EXIT: player_unlock(); dc_quit(&dc); dc_deinit(&dc); audio_output_all_close(); music_buffer_free(player_buffer); player_command_finished(); return NULL; case PLAYER_COMMAND_CANCEL: pc.next_song = NULL; player_command_finished_locked(); break; case PLAYER_COMMAND_REFRESH: /* no-op when not playing */ player_command_finished_locked(); break; case PLAYER_COMMAND_NONE: player_wait(); break; } } } void player_create(void) { GError *e = NULL; assert(pc.thread == NULL); pc.thread = g_thread_create(player_task, NULL, true, &e); if (pc.thread == NULL) g_error("Failed to spawn player task: %s", e->message); }