static bool decoder_file_decode(const struct decoder_plugin *plugin, struct decoder *decoder, const char *path) { assert(plugin != NULL); assert(plugin->file_decode != NULL); assert(decoder != NULL); assert(decoder->stream_tag == NULL); assert(decoder->decoder_tag == NULL); assert(path != NULL); assert(g_path_is_absolute(path)); assert(decoder->dc->state == DECODE_STATE_START); if (decoder->dc->command == DECODE_COMMAND_STOP) return true; decoder_unlock(decoder->dc); decoder_plugin_file_decode(plugin, decoder, path); decoder_lock(decoder->dc); assert(decoder->dc->state == DECODE_STATE_START || decoder->dc->state == DECODE_STATE_DECODE); return decoder->dc->state != DECODE_STATE_START; }
static bool decoder_stream_decode(const struct decoder_plugin *plugin, struct decoder *decoder, struct input_stream *input_stream) { assert(plugin != NULL); assert(plugin->stream_decode != NULL); assert(decoder != NULL); assert(decoder->stream_tag == NULL); assert(decoder->decoder_tag == NULL); assert(input_stream != NULL); assert(input_stream->ready); assert(decoder->dc->state == DECODE_STATE_START); if (decoder->dc->command == DECODE_COMMAND_STOP) return true; decoder_unlock(decoder->dc); /* rewind the stream, so each plugin gets a fresh start */ input_stream_seek(input_stream, 0, SEEK_SET, NULL); decoder_plugin_stream_decode(plugin, decoder, input_stream); decoder_lock(decoder->dc); assert(decoder->dc->state == DECODE_STATE_START || decoder->dc->state == DECODE_STATE_DECODE); return decoder->dc->state != DECODE_STATE_START; }
void decoder_command_finished(struct decoder *decoder) { struct decoder_control *dc = decoder->dc; decoder_lock(dc); assert(dc->command != DECODE_COMMAND_NONE); assert(dc->command != DECODE_COMMAND_SEEK || dc->seek_error || decoder->seeking); assert(dc->pipe != NULL); if (decoder->seeking) { decoder->seeking = false; /* delete frames from the old song position */ if (decoder->chunk != NULL) { music_buffer_return(dc->buffer, decoder->chunk); decoder->chunk = NULL; } music_pipe_clear(dc->pipe, dc->buffer); decoder->timestamp = dc->seek_where; } dc->command = DECODE_COMMAND_NONE; decoder_unlock(dc); player_lock_signal(); }
struct music_chunk * decoder_get_chunk(struct decoder *decoder, struct input_stream *is) { struct decoder_control *dc = decoder->dc; enum decoder_command cmd; assert(decoder != NULL); if (decoder->chunk != NULL) return decoder->chunk; do { decoder->chunk = music_buffer_allocate(dc->buffer); if (decoder->chunk != NULL) { decoder->chunk->replay_gain_serial = decoder->replay_gain_serial; if (decoder->replay_gain_serial != 0) decoder->chunk->replay_gain_info = decoder->replay_gain_info; return decoder->chunk; } decoder_lock(dc); cmd = need_chunks(dc, is, true); decoder_unlock(dc); } while (cmd == DECODE_COMMAND_NONE); return NULL; }
/** * Opens the input stream with input_stream_open(), and waits until * the stream gets ready. If a decoder STOP command is received * during that, it cancels the operation (but does not close the * stream). * * Unlock the decoder before calling this function. * * @return an input_stream on success or if #DECODE_COMMAND_STOP is * received, NULL on error */ static struct input_stream * decoder_input_stream_open(struct decoder_control *dc, const char *uri) { GError *error = NULL; struct input_stream *is; is = input_stream_open(uri, dc->mutex, dc->cond, &error); if (is == NULL) { if (error != NULL) { g_warning("%s", error->message); g_error_free(error); } return NULL; } /* wait for the input stream to become ready; its metadata will be available then */ decoder_lock(dc); input_stream_update(is); while (!is->ready && dc->command != DECODE_COMMAND_STOP) { decoder_wait(dc); input_stream_update(is); } if (!input_stream_check(is, &error)) { decoder_unlock(dc); g_warning("%s", error->message); g_error_free(error); return NULL; } decoder_unlock(dc); return is; }
static enum decoder_command decoder_lock_get_command(struct decoder_control *dc) { enum decoder_command command; decoder_lock(dc); command = dc->command; decoder_unlock(dc); return command; }
/** * Try decoding a stream. */ static bool decoder_run_stream(struct decoder *decoder, const char *uri) { struct decoder_control *dc = decoder->dc; struct input_stream *input_stream; bool success; decoder_unlock(dc); input_stream = decoder_input_stream_open(dc, uri); if (input_stream == NULL) { decoder_lock(dc); return false; } decoder_lock(dc); GSList *tried = NULL; success = dc->command == DECODE_COMMAND_STOP || /* first we try mime types: */ decoder_run_stream_mime_type(decoder, input_stream, &tried) || /* if that fails, try suffix matching the URL: */ decoder_run_stream_suffix(decoder, input_stream, uri, &tried) || /* fallback to mp3: this is needed for bastard streams that don't have a suffix or set the mimeType */ (tried == NULL && decoder_run_stream_fallback(decoder, input_stream)); g_slist_free(tried); decoder_unlock(dc); input_stream_close(input_stream); decoder_lock(dc); return success; }
static void decoder_run_song(struct decoder_control *dc, const struct song *song, const char *uri) { struct decoder decoder = { .dc = dc, }; int ret; decoder.timestamp = 0.0; decoder.seeking = false; decoder.song_tag = song->tag != NULL && song_is_file(song) ? tag_dup(song->tag) : NULL; decoder.stream_tag = NULL; decoder.decoder_tag = NULL; decoder.chunk = NULL; dc->state = DECODE_STATE_START; decoder_command_finished_locked(dc); pcm_convert_init(&decoder.conv_state); ret = song_is_file(song) ? decoder_run_file(&decoder, uri) : decoder_run_stream(&decoder, uri); decoder_unlock(dc); pcm_convert_deinit(&decoder.conv_state); /* flush the last chunk */ if (decoder.chunk != NULL) decoder_flush_chunk(&decoder); if (decoder.song_tag != NULL) tag_free(decoder.song_tag); if (decoder.stream_tag != NULL) tag_free(decoder.stream_tag); if (decoder.decoder_tag != NULL) tag_free(decoder.decoder_tag); decoder_lock(dc); dc->state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR; }
/** * This is a wrapper for input_stream_buffer(). It assumes that the * decoder is currently locked, and temporarily unlocks it while * calling input_stream_buffer(). We shouldn't hold the lock during a * potentially blocking operation. */ static bool decoder_input_buffer(struct decoder_control *dc, struct input_stream *is) { GError *error = NULL; int ret; decoder_unlock(dc); ret = input_stream_buffer(is, &error); if (ret < 0) { g_warning("%s", error->message); g_error_free(error); } decoder_lock(dc); return ret > 0; }
static gpointer decoder_task(gpointer arg) { struct decoder_control *dc = arg; decoder_lock(dc); do { assert(dc->state == DECODE_STATE_STOP || dc->state == DECODE_STATE_ERROR); switch (dc->command) { case DECODE_COMMAND_START: g_debug("clearing mixramp tags"); dc_mixramp_start(dc, NULL); dc_mixramp_prev_end(dc, dc->mixramp_end); dc->mixramp_end = NULL; /* Don't free, it's copied above. */ dc->replay_gain_prev_db = dc->replay_gain_db; dc->replay_gain_db = 0; /* fall through */ case DECODE_COMMAND_SEEK: decoder_run(dc); break; case DECODE_COMMAND_STOP: decoder_command_finished_locked(dc); break; case DECODE_COMMAND_NONE: decoder_wait(dc); break; } } while (dc->command != DECODE_COMMAND_NONE || !dc->quit); decoder_unlock(dc); return NULL; }
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)); }
static void decoder_run_song(struct decoder_control *dc, const struct song *song, const char *uri) { struct decoder decoder = { .dc = dc, .initial_seek_pending = dc->start_ms > 0, .initial_seek_running = false, }; int ret; decoder.timestamp = 0.0; decoder.seeking = false; decoder.song_tag = song->tag != NULL && song_is_file(song) ? tag_dup(song->tag) : NULL; decoder.stream_tag = NULL; decoder.decoder_tag = NULL; decoder.chunk = NULL; dc->state = DECODE_STATE_START; decoder_command_finished_locked(dc); pcm_convert_init(&decoder.conv_state); ret = song_is_file(song) ? decoder_run_file(&decoder, uri) : decoder_run_stream(&decoder, uri); decoder_unlock(dc); pcm_convert_deinit(&decoder.conv_state); /* flush the last chunk */ if (decoder.chunk != NULL) decoder_flush_chunk(&decoder); if (decoder.song_tag != NULL) tag_free(decoder.song_tag); if (decoder.stream_tag != NULL) tag_free(decoder.stream_tag); if (decoder.decoder_tag != NULL) tag_free(decoder.decoder_tag); decoder_lock(dc); dc->state = ret ? DECODE_STATE_STOP : DECODE_STATE_ERROR; } static void decoder_run(struct decoder_control *dc) { const struct song *song = dc->song; char *uri; assert(song != NULL); if (song_is_file(song)) uri = map_song_fs(song); else uri = song_get_uri(song); if (uri == NULL) { dc->state = DECODE_STATE_ERROR; decoder_command_finished_locked(dc); return; } decoder_run_song(dc, song, uri); g_free(uri); } static gpointer decoder_task(gpointer arg) { struct decoder_control *dc = arg; decoder_lock(dc); do { assert(dc->state == DECODE_STATE_STOP || dc->state == DECODE_STATE_ERROR); switch (dc->command) { case DECODE_COMMAND_START: dc_mixramp_start(dc, NULL); dc_mixramp_prev_end(dc, dc->mixramp_end); dc->mixramp_end = NULL; /* Don't free, it's copied above. */ dc->replay_gain_prev_db = dc->replay_gain_db; dc->replay_gain_db = 0; /* fall through */ case DECODE_COMMAND_SEEK: decoder_run(dc); break; case DECODE_COMMAND_STOP: decoder_command_finished_locked(dc); break; case DECODE_COMMAND_NONE: decoder_wait(dc); break; } } while (dc->command != DECODE_COMMAND_NONE || !dc->quit); decoder_unlock(dc); return NULL; } void decoder_thread_start(struct decoder_control *dc) { GError *e = NULL; assert(dc->thread == NULL); dc->quit = false; dc->thread = g_thread_create(decoder_task, dc, true, &e); if (dc->thread == NULL) MPD_ERROR("Failed to spawn decoder task: %s", e->message); }
static void decoder_run_song(struct decoder_control *dc, const struct song *song, const char *uri) { struct decoder decoder = { .dc = dc, .initial_seek_pending = dc->start_ms > 0, .initial_seek_running = false, }; int ret; decoder.timestamp = 0.0; decoder.seeking = false; decoder.song_tag = song->tag != NULL && song_is_file(song) ? tag_dup(song->tag) : NULL; decoder.stream_tag = NULL; decoder.decoder_tag = NULL; decoder.chunk = NULL; dc->state = DECODE_STATE_START; decoder_command_finished_locked(dc); pcm_convert_init(&decoder.conv_state); ret = song_is_file(song) ? decoder_run_file(&decoder, uri) : decoder_run_stream(&decoder, uri); decoder_unlock(dc); pcm_convert_deinit(&decoder.conv_state); /* flush the last chunk */ if (decoder.chunk != NULL) decoder_flush_chunk(&decoder); if (decoder.song_tag != NULL) tag_free(decoder.song_tag); if (decoder.stream_tag != NULL) tag_free(decoder.stream_tag); if (decoder.decoder_tag != NULL) tag_free(decoder.decoder_tag); decoder_lock(dc); if (ret) dc->state = DECODE_STATE_STOP; else { dc->state = DECODE_STATE_ERROR; const char *error_uri = song->uri; char *allocated = uri_remove_auth(error_uri); if (allocated != NULL) error_uri = allocated; dc->error = g_error_new(decoder_quark(), 0, "Failed to decode %s", error_uri); g_free(allocated); } } static void decoder_run(struct decoder_control *dc) { dc_clear_error(dc); const struct song *song = dc->song; char *uri; assert(song != NULL); if (song_is_file(song)) uri = map_song_fs(song); else uri = song_get_uri(song); if (uri == NULL) { dc->state = DECODE_STATE_ERROR; dc->error = g_error_new(decoder_quark(), 0, "Failed to map song"); decoder_command_finished_locked(dc); return; } decoder_run_song(dc, song, uri); g_free(uri); }
enum decoder_command decoder_data(struct decoder *decoder, struct input_stream *is, const void *_data, size_t length, uint16_t kbit_rate) { struct decoder_control *dc = decoder->dc; const char *data = _data; GError *error = NULL; enum decoder_command cmd; assert(dc->state == DECODE_STATE_DECODE); assert(dc->pipe != NULL); assert(length % audio_format_frame_size(&dc->in_audio_format) == 0); decoder_lock(dc); cmd = decoder_get_virtual_command(decoder); decoder_unlock(dc); if (cmd == DECODE_COMMAND_STOP || cmd == DECODE_COMMAND_SEEK || length == 0) return cmd; /* send stream tags */ if (update_stream_tag(decoder, is)) { if (decoder->decoder_tag != NULL) { /* merge with tag from decoder plugin */ struct tag *tag; tag = tag_merge(decoder->decoder_tag, decoder->stream_tag); cmd = do_send_tag(decoder, is, tag); tag_free(tag); } else /* send only the stream tag */ cmd = do_send_tag(decoder, is, decoder->stream_tag); if (cmd != DECODE_COMMAND_NONE) return cmd; } if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) { data = pcm_convert(&decoder->conv_state, &dc->in_audio_format, data, length, &dc->out_audio_format, &length, &error); if (data == NULL) { /* the PCM conversion has failed - stop playback, since we have no better way to bail out */ g_warning("%s", error->message); return DECODE_COMMAND_STOP; } } while (length > 0) { struct music_chunk *chunk; char *dest; size_t nbytes; bool full; chunk = decoder_get_chunk(decoder, is); if (chunk == NULL) { assert(dc->command != DECODE_COMMAND_NONE); return dc->command; } dest = music_chunk_write(chunk, &dc->out_audio_format, decoder->timestamp - dc->song->start_ms / 1000.0, kbit_rate, &nbytes); if (dest == NULL) { /* the chunk is full, flush it */ decoder_flush_chunk(decoder); player_lock_signal(); continue; } assert(nbytes > 0); if (nbytes > length) nbytes = length; /* copy the buffer */ memcpy(dest, data, nbytes); /* expand the music pipe chunk */ full = music_chunk_expand(chunk, &dc->out_audio_format, nbytes); if (full) { /* the chunk is full, flush it */ decoder_flush_chunk(decoder); player_lock_signal(); } data += nbytes; length -= nbytes; decoder->timestamp += (double)nbytes / audio_format_time_to_size(&dc->out_audio_format); if (dc->end_ms > 0 && decoder->timestamp >= dc->end_ms / 1000.0) /* the end of this range has been reached: stop decoding */ return DECODE_COMMAND_STOP; } return DECODE_COMMAND_NONE; }
/* * 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); }
/** * Obtains the next chunk from the music pipe, optionally applies * cross-fading, and sends it to all audio outputs. * * @return true on success, false on error (playback will be stopped) */ 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(pc, 64)) /* the output pipe is still large enough, don't send another chunk */ return true; unsigned cross_fade_position; struct music_chunk *chunk = NULL; if (player->xfade == XFADE_ENABLED && player_dc_at_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); if (!player->cross_fading) { /* beginning of the cross fade - adjust crossFadeChunks which might be bigger than the remaining number of chunks in the old song */ player->cross_fade_chunks = cross_fade_position; player->cross_fading = true; } 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 the current song is faded out */ player->cross_fade_tag = tag_merge_replace(player->cross_fade_tag, other_chunk->tag); other_chunk->tag = NULL; if (isnan(pc->mixramp_delay_seconds)) { chunk->mix_ratio = ((float)cross_fade_position) / player->cross_fade_chunks; } else { chunk->mix_ratio = nan(""); } if (music_chunk_is_empty(other_chunk)) { /* the "other" chunk was a music_chunk which had only a tag, but no music data - we cannot cross-fade that; but since this happens only at the beginning of the new song, we can easily recover by throwing it away now */ music_buffer_return(player_buffer, other_chunk); other_chunk = NULL; } chunk->other = other_chunk; } else { /* there are not enough decoded chunks yet */ 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 */ decoder_signal(dc); player_wait_decoder(pc, dc); decoder_unlock(dc); return true; } } } if (chunk == NULL) chunk = music_pipe_shift(player->pipe); assert(chunk != NULL); /* insert the postponed tag if cross-fading is finished */ if (player->xfade != XFADE_ENABLED && player->cross_fade_tag != NULL) { chunk->tag = tag_merge_replace(chunk->tag, player->cross_fade_tag); player->cross_fade_tag = NULL; } /* play the current chunk */ if (!play_chunk(player->pc, player->song, chunk, &player->play_audio_format)) { music_buffer_return(player_buffer, chunk); 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->paused = true; player_unlock(pc); 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 */ decoder_lock(dc); if (!decoder_is_idle(dc) && music_pipe_size(dc->pipe) <= (pc->buffered_before_play + music_buffer_size(player_buffer) * 3) / 4) decoder_signal(dc); decoder_unlock(dc); return true; }
/** * 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; } }