/** * 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) { assert(audio_format_defined(&player->play_audio_format)); struct music_chunk *chunk = music_buffer_allocate(player_buffer); if (chunk == NULL) { g_warning("Failed to allocate silence buffer"); return false; } #ifndef NDEBUG chunk->audio_format = player->play_audio_format; #endif size_t frame_size = audio_format_frame_size(&player->play_audio_format); /* this formula ensures that we don't send partial frames */ unsigned num_frames = sizeof(chunk->data) / frame_size; chunk->times = -1.0; /* undefined time stamp */ chunk->length = num_frames * frame_size; memset(chunk->data, 0, chunk->length); if (!audio_output_all_play(chunk)) { music_buffer_return(player_buffer, chunk); return false; } return true; }
/** * Wrapper for audio_output_all_open(). Upon failure, it pauses the * player. * * @return true on success */ static bool player_open_output(struct player *player) { assert(audio_format_defined(&player->play_audio_format)); 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(); return true; } else { player->output_open = false; /* pause: the user may resume playback as soon as an audio output becomes available */ player->paused = true; player_lock(); pc.error = PLAYER_ERROR_AUDIO; pc.state = PLAYER_STATE_PAUSE; player_unlock(); return false; } }
void decoder_initialized(G_GNUC_UNUSED struct decoder * decoder, const struct audio_format *audio_format, bool seekable, float total_time) { assert(dc.state == DECODE_STATE_START); assert(decoder != NULL); assert(decoder->stream_tag == NULL); assert(decoder->decoder_tag == NULL); assert(!decoder->seeking); assert(audio_format != NULL); assert(audio_format_defined(audio_format)); assert(audio_format_valid(audio_format)); dc.in_audio_format = *audio_format; getOutputAudioFormat(audio_format, &dc.out_audio_format); dc.seekable = seekable; dc.total_time = total_time; dc.state = DECODE_STATE_DECODE; notify_signal(&pc.notify); g_debug("audio_format=%u:%u:%u, seekable=%s", dc.in_audio_format.sample_rate, dc.in_audio_format.bits, dc.in_audio_format.channels, seekable ? "true" : "false"); if (!audio_format_equals(&dc.in_audio_format, &dc.out_audio_format)) g_debug("converting to %u:%u:%u", dc.out_audio_format.sample_rate, dc.out_audio_format.bits, dc.out_audio_format.channels); }
void decoder_initialized(struct decoder *decoder, const struct audio_format *audio_format, bool seekable, float total_time) { struct decoder_control *dc = decoder->dc; struct audio_format_string af_string; assert(dc->state == DECODE_STATE_START); assert(dc->pipe != NULL); assert(decoder != NULL); assert(decoder->stream_tag == NULL); assert(decoder->decoder_tag == NULL); assert(!decoder->seeking); assert(audio_format != NULL); assert(audio_format_defined(audio_format)); assert(audio_format_valid(audio_format)); dc->in_audio_format = *audio_format; getOutputAudioFormat(audio_format, &dc->out_audio_format); dc->seekable = seekable; dc->total_time = total_time; decoder_lock(dc); dc->state = DECODE_STATE_DECODE; decoder_unlock(dc); player_lock_signal(); g_debug("audio_format=%s, seekable=%s", audio_format_to_string(&dc->in_audio_format, &af_string), seekable ? "true" : "false"); if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) g_debug("converting to %s", audio_format_to_string(&dc->out_audio_format, &af_string)); }
/** * Wrapper for audio_output_all_open(). Upon failure, it pauses the * player. * * @return true on success */ 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); GError *error = NULL; if (audio_output_all_open(&player->play_audio_format, player_buffer, &error)) { player->output_open = true; player->paused = false; player_lock(pc); pc->state = PLAYER_STATE_PLAY; player_unlock(pc); return true; } else { g_warning("%s", error->message); player->output_open = false; /* pause: the user may resume playback as soon as an audio output becomes available */ player->paused = true; player_lock(pc); pc_set_error(pc, PLAYER_ERROR_OUTPUT, error); pc->state = PLAYER_STATE_PAUSE; player_unlock(pc); return false; } }
/* * 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 player_control *pc, struct decoder_control *dc) { struct player player = { .pc = pc, .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(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(pc); music_pipe_free(player.pipe); event_pipe_emit(PIPE_EVENT_PLAYLIST); player_lock(pc); return; } player_lock(pc); pc->state = PLAYER_STATE_PLAY; 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(pc); audio_output_all_cancel(); break; } 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 && !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(pc, dc); decoder_unlock(dc); player_lock(pc); continue; } else { /* buffering is complete */ player.buffering = false; } } if (player.decoder_starting) { /* wait until the decoder is initialized completely */ if (!player_check_decoder_startup(&player)) break; player_lock(pc); 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 (player_dc_at_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(pc); 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 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 (player_dc_at_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_empty(player.pipe)) { /* 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(pc); } 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(pc); if (player.queued) { assert(pc->next_song != NULL); pc->next_song = NULL; } pc->state = PLAYER_STATE_STOP; player_unlock(pc); event_pipe_emit(PIPE_EVENT_PLAYLIST); player_lock(pc); } static gpointer player_task(gpointer arg) { struct player_control *pc = arg; struct decoder_control *dc = dc_new(pc->cond); decoder_thread_start(dc); player_buffer = music_buffer_new(pc->buffer_chunks); player_lock(pc); while (1) { switch (pc->command) { case PLAYER_COMMAND_QUEUE: assert(pc->next_song != NULL); do_play(pc, dc); break; case PLAYER_COMMAND_STOP: player_unlock(pc); audio_output_all_cancel(); player_lock(pc); /* fall through */ case PLAYER_COMMAND_SEEK: case PLAYER_COMMAND_PAUSE: pc->next_song = NULL; player_command_finished_locked(pc); break; case PLAYER_COMMAND_CLOSE_AUDIO: player_unlock(pc); audio_output_all_release(); 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); #endif break; case PLAYER_COMMAND_UPDATE_AUDIO: player_unlock(pc); audio_output_all_enable_disable(); player_lock(pc); player_command_finished_locked(pc); break; case PLAYER_COMMAND_EXIT: player_unlock(pc); dc_quit(dc); dc_free(dc); audio_output_all_close(); music_buffer_free(player_buffer); player_command_finished(pc); return NULL; case PLAYER_COMMAND_CANCEL: pc->next_song = NULL; player_command_finished_locked(pc); break; case PLAYER_COMMAND_REFRESH: /* no-op when not playing */ player_command_finished_locked(pc); break; case PLAYER_COMMAND_NONE: player_wait(pc); break; } } } void player_create(struct player_control *pc) { assert(pc->thread == NULL); GError *e = 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); }
/** * Player lock must be held before calling. */ 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) { case PLAYER_COMMAND_NONE: case PLAYER_COMMAND_STOP: case PLAYER_COMMAND_EXIT: case PLAYER_COMMAND_CLOSE_AUDIO: break; case PLAYER_COMMAND_UPDATE_AUDIO: player_unlock(pc); audio_output_all_enable_disable(); player_lock(pc); player_command_finished_locked(pc); break; case PLAYER_COMMAND_QUEUE: assert(pc->next_song != NULL); assert(!player->queued); assert(!player_dc_at_next_song(player)); player->queued = true; player_command_finished_locked(pc); break; case PLAYER_COMMAND_PAUSE: player_unlock(pc); player->paused = !player->paused; if (player->paused) { audio_output_all_pause(); player_lock(pc); 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); pc->state = PLAYER_STATE_PLAY; } else if (audio_output_all_open(&player->play_audio_format, player_buffer)) { /* unpaused, continue playing */ player_lock(pc); pc->state = PLAYER_STATE_PLAY; } else { /* the audio device has failed - rollback to pause mode */ pc->error = PLAYER_ERROR_AUDIO; player->paused = true; player_lock(pc); } player_command_finished_locked(pc); break; case PLAYER_COMMAND_SEEK: player_unlock(pc); player_seek_decoder(player); player_lock(pc); break; case PLAYER_COMMAND_CANCEL: 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; return; } if (player_dc_at_next_song(player)) { /* the decoder is already decoding the song - stop it and reset the position */ player_unlock(pc); player_dc_stop(player); player_lock(pc); } pc->next_song = NULL; player->queued = false; player_command_finished_locked(pc); break; case PLAYER_COMMAND_REFRESH: if (audio_format_defined(&player->play_audio_format) && !player->paused) { player_unlock(pc); audio_output_all_check(); player_lock(pc); } 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(pc); break; } }
/** * 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 player_control *pc = player->pc; struct decoder_control *dc = player->dc; assert(player->decoder_starting); decoder_lock(dc); if (decoder_has_failed(dc)) { /* the decoder failed */ decoder_unlock(dc); player_lock(pc); pc->errored_song = dc->song; pc->error = PLAYER_ERROR_FILE; player_unlock(pc); return false; } 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(pc, 1)) /* the output devices havn't finished playing all chunks yet - wait for that */ return true; 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; if (!player->paused && !audio_output_all_open(&dc->out_audio_format, player_buffer)) { char *uri = song_get_uri(dc->song); g_warning("problems opening audio device " "while playing \"%s\"", uri); g_free(uri); player_lock(pc); 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(pc); player->paused = true; return true; } return true; } else { /* the decoder is not yet ready; wait some more */ player_wait_decoder(pc, dc); decoder_unlock(dc); return true; } }