/** * 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; } }
/** * This is the "PLAYLIST" event handler. It is invoked by the player * thread whenever it requests a new queued song, or when it exits. */ void playlist_sync(struct playlist *playlist, struct player_control *pc) { if (!playlist->playing) /* this event has reached us out of sync: we aren't playing anymore; ignore the event */ return; player_lock(pc); enum player_state pc_state = pc_get_state(pc); const struct song *pc_next_song = pc->next_song; player_unlock(pc); if (pc_state == PLAYER_STATE_STOP) /* the player thread has stopped: check if playback should be restarted with the next song. That can happen if the playlist isn't filling the queue fast enough */ playlist_resume_playback(playlist, pc); else { /* check if the player thread has already started playing the queued song */ if (pc_next_song == NULL && playlist->queued != -1) playlist_song_started(playlist, pc); player_lock(pc); pc_next_song = pc->next_song; player_unlock(pc); /* make sure the queued song is always set (if possible) */ if (pc_next_song == NULL && playlist->queued < 0) playlist_update_queued_song(playlist, pc, NULL); } }
/** * 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); GError *error = dc_get_error(dc); if (error != NULL) { /* the decoder failed */ decoder_unlock(dc); player_lock(pc); pc_set_error(pc, PLAYER_ERROR_DECODER, error); player_unlock(pc); return false; } else if (!decoder_is_starting(dc)) { /* the decoder is ready and ok */ decoder_unlock(dc); if (player->output_open && !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 && !player_open_output(player)) { char *uri = song_get_uri(dc->song); g_warning("problems opening audio device " "while playing \"%s\"", uri); g_free(uri); return true; } return true; } else { /* the decoder is not yet ready; wait some more */ player_wait_decoder(pc, dc); decoder_unlock(dc); return true; } }
/** * 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) { 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); player->queued = false; GError *error = dc_lock_get_error(dc); if (error != NULL) { player_lock(pc); pc_set_error(pc, PLAYER_ERROR_DECODER, error); song_free(pc->next_song); pc->next_song = NULL; player_unlock(pc); return false; } if (player->song != NULL) song_free(player->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(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); /* clear the queued song */ pc->next_song = NULL; player_unlock(pc); /* call syncPlaylistWithQueue() in the main thread */ event_pipe_emit(PIPE_EVENT_PLAYLIST); return true; }
/** * This is called at the border between two songs: the audio output * 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) { player->xfade = XFADE_UNKNOWN; char *uri = song_get_uri(player->song); g_message("played \"%s\"", uri); g_free(uri); music_pipe_free(player->pipe); player->pipe = player->dc->pipe; audio_output_all_song_border(); if (!player_wait_for_decoder(player)) return false; struct player_control *const pc = player->pc; player_lock(pc); if (pc->border_pause) { player->paused = true; pc->state = PLAYER_STATE_PAUSE; } player_unlock(pc); return true; }
static void player_command(enum player_command cmd) { player_lock(); player_command_locked(cmd); player_unlock(); }
void player_pause(void) { player_lock(); if (consumer_status == CS_STOPPED) { __producer_play(); if (producer_status == PS_PLAYING) { __consumer_play(); if (consumer_status != CS_PLAYING) __producer_stop(); } __player_status_changed(); if (consumer_status == CS_PLAYING) __prebuffer(); player_unlock(); return; } if (ip && ip_is_remote(ip)) { /* pausing not allowed */ player_unlock(); return; } __producer_pause(); __consumer_pause(); __player_status_changed(); player_unlock(); }
void player_play_file(struct track_info *ti) { player_lock(); __producer_set_file(ti); if (producer_status == PS_UNLOADED) { __consumer_stop(); goto out; } /* PS_STOPPED */ __producer_play(); /* PS_UNLOADED,PS_PLAYING */ if (producer_status == PS_UNLOADED) { __consumer_stop(); goto out; } /* PS_PLAYING */ if (consumer_status == CS_STOPPED) { __consumer_play(); if (consumer_status == CS_STOPPED) __producer_stop(); } else { op_drop(); change_sf(1); } out: __player_status_changed(); if (producer_status == PS_PLAYING) __prebuffer(); player_unlock(); }
static void player_command(struct player_control *pc, enum player_command cmd) { player_lock(pc); player_command_locked(pc, cmd); player_unlock(pc); }
static void player_command_finished(struct player_control *pc) { player_lock(pc); player_command_finished_locked(pc); player_unlock(pc); }
/** * 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 player_control *pc, struct song *song, struct music_chunk *chunk, const struct audio_format *format) { assert(music_chunk_check_format(chunk, format)); if (chunk->tag != NULL) update_song_tag(song, chunk->tag); if (chunk->length == 0) { music_buffer_return(player_buffer, chunk); return true; } 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 / audio_format_time_to_size(format); return true; }
void pc_set_border_pause(struct player_control *pc, bool border_pause) { player_lock(pc); pc->border_pause = border_pause; player_unlock(pc); }
void player_stop(void) { player_lock(); __consumer_stop(); __producer_stop(); __player_status_changed(); player_unlock(); }
void pc_clear_error(void) { player_lock(); pc.error = PLAYER_ERROR_NOERROR; pc.errored_song = NULL; player_unlock(); }
void pc_enqueue_song(struct song *song) { assert(song != NULL); player_lock(); pc_enqueue_song_locked(song); player_unlock(); }
void pc_enqueue_song(struct player_control *pc, struct song *song) { assert(song != NULL); player_lock(pc); pc_enqueue_song_locked(pc, song); player_unlock(pc); }
void player_init(const struct player_callbacks *callbacks) { int rc; #ifdef REALTIME_SCHEDULING pthread_attr_t attr; #endif pthread_attr_t *attrp = NULL; /* This mutex is locked inside of the mpris implementation which is * called into from many different places. It is not trivial to see if * those places do already hold this lock and so the mpris functions * always acquires it. To avoid deadlocks in the places where the lock * is already held by the calling context, we use a recursive mutex. */ cmus_mutex_init_recursive(&player_info.mutex); /* 1 s is 176400 B (0.168 MB) * 10 s is 1.68 MB */ buffer_nr_chunks = 10 * 44100 * 16 / 8 * 2 / CHUNK_SIZE; buffer_init(); player_cbs = callbacks; #ifdef REALTIME_SCHEDULING rc = pthread_attr_init(&attr); BUG_ON(rc); rc = pthread_attr_setschedpolicy(&attr, SCHED_RR); if (rc) { d_print("could not set real-time scheduling priority: %s\n", strerror(rc)); } else { struct sched_param param; d_print("using real-time scheduling\n"); param.sched_priority = sched_get_priority_max(SCHED_RR); d_print("setting priority to %d\n", param.sched_priority); rc = pthread_attr_setschedparam(&attr, ¶m); BUG_ON(rc); attrp = &attr; } #endif rc = pthread_create(&producer_thread, NULL, producer_loop, NULL); BUG_ON(rc); rc = pthread_create(&consumer_thread, attrp, consumer_loop, NULL); if (rc && attrp) { d_print("could not create thread using real-time scheduling: %s\n", strerror(rc)); rc = pthread_create(&consumer_thread, NULL, consumer_loop, NULL); } BUG_ON(rc); /* update player_info.cont etc. */ player_lock(); _player_status_changed(); player_unlock(); }
/** * 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) { struct decoder_control *dc = player->dc; 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(); return false; } 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(); /* 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); return true; }
char * pc_get_error_message(struct player_control *pc) { player_lock(pc); char *message = pc->error_type != PLAYER_ERROR_NONE ? g_strdup(pc->error->message) : NULL; player_unlock(pc); return message; }
void player_set_rg_preamp(double db) { player_lock(); replaygain_preamp = db; player_info_lock(); update_rg_scale(); player_info_unlock(); player_unlock(); }
void player_set_rg_limit(int limit) { player_lock(); replaygain_limit = limit; player_info_lock(); update_rg_scale(); player_info_unlock(); player_unlock(); }
void pc_clear_error(struct player_control *pc) { player_lock(pc); if (pc->error_type != PLAYER_ERROR_NONE) { pc->error_type = PLAYER_ERROR_NONE; g_error_free(pc->error); } player_unlock(pc); }
void player_set_buffer_chunks(unsigned int nr_chunks) { player_lock(); __producer_stop(); __consumer_stop(); buffer_nr_chunks = nr_chunks; buffer_init(); __player_status_changed(); player_unlock(); }
void pc_pause(void) { player_lock(); if (pc.state != PLAYER_STATE_STOP) { player_command_locked(PLAYER_COMMAND_PAUSE); idle_add(IDLE_PLAYER); } player_unlock(); }
void pc_enqueue_song(struct song *song) { assert(song != NULL); player_lock(); assert(pc.next_song == NULL); pc.next_song = song; player_command_locked(PLAYER_COMMAND_QUEUE); player_unlock(); }
void pc_pause(struct player_control *pc) { player_lock(pc); if (pc->state != PLAYER_STATE_STOP) { player_command_locked(pc, PLAYER_COMMAND_PAUSE); idle_add(IDLE_PLAYER); } player_unlock(pc); }
void player_set_rg(enum replaygain rg) { player_lock(); /* don't mess with scale_pos if soft_vol or replaygain is already enabled */ if (!soft_vol && !replaygain) scale_pos = consumer_pos; replaygain = rg; player_info_lock(); update_rg_scale(); player_info_unlock(); player_unlock(); }
/** * 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; } }
/* * change output plugin without stopping playback */ void player_set_op(const char *name) { int rc; player_lock(); /* drop needed because close drains the buffer */ if (consumer_status == CS_PAUSED) op_drop(); if (consumer_status == CS_PLAYING || consumer_status == CS_PAUSED) op_close(); if (name) { d_print("setting op to '%s'\n", name); rc = op_select(name); } else { /* first initialized plugin */ d_print("selecting first initialized op\n"); rc = op_select_any(); } if (rc) { __consumer_status_update(CS_STOPPED); __producer_stop(); if (name) player_op_error(rc, "selecting output plugin '%s'", name); else player_op_error(rc, "selecting any output plugin"); player_unlock(); return; } if (consumer_status == CS_PLAYING || consumer_status == CS_PAUSED) { set_buffer_sf(); rc = op_open(buffer_sf, buffer_channel_map); if (rc) { __consumer_status_update(CS_STOPPED); __producer_stop(); player_op_error(rc, "opening audio device"); player_unlock(); return; } if (consumer_status == CS_PAUSED) op_pause(); } player_unlock(); }
void player_init(const struct player_callbacks *callbacks) { int rc; #ifdef REALTIME_SCHEDULING pthread_attr_t attr; #endif pthread_attr_t *attrp = NULL; /* 1 s is 176400 B (0.168 MB) * 10 s is 1.68 MB */ buffer_nr_chunks = 10 * 44100 * 16 / 8 * 2 / CHUNK_SIZE; buffer_init(); player_cbs = callbacks; #ifdef REALTIME_SCHEDULING rc = pthread_attr_init(&attr); BUG_ON(rc); rc = pthread_attr_setschedpolicy(&attr, SCHED_RR); if (rc) { d_print("could not set real-time scheduling priority: %s\n", strerror(rc)); } else { struct sched_param param; d_print("using real-time scheduling\n"); param.sched_priority = sched_get_priority_max(SCHED_RR); d_print("setting priority to %d\n", param.sched_priority); rc = pthread_attr_setschedparam(&attr, ¶m); BUG_ON(rc); attrp = &attr; } #endif rc = pthread_create(&producer_thread, NULL, producer_loop, NULL); BUG_ON(rc); rc = pthread_create(&consumer_thread, attrp, consumer_loop, NULL); if (rc && attrp) { d_print("could not create thread using real-time scheduling: %s\n", strerror(rc)); rc = pthread_create(&consumer_thread, NULL, consumer_loop, NULL); } BUG_ON(rc); /* update player_info.cont etc. */ player_lock(); __player_status_changed(); player_unlock(); }