示例#1
0
文件: player.c 项目: CBke/cmus
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();
}
示例#2
0
static void
player_command(struct player_control *pc, enum player_command cmd)
{
	player_lock(pc);
	player_command_locked(pc, cmd);
	player_unlock(pc);
}
示例#3
0
void
pc_set_border_pause(struct player_control *pc, bool border_pause)
{
	player_lock(pc);
	pc->border_pause = border_pause;
	player_unlock(pc);
}
示例#4
0
/**
 * 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)
{
	if (!playlist->playing)
		/* this event has reached us out of sync: we aren't
		   playing anymore; ignore the event */
		return;

	player_lock();
	enum player_state pc_state = pc_get_state();
	const struct song *pc_next_song = pc.next_song;
	player_unlock();

	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);
	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);

		/* make sure the queued song is always set (if
		   possible) */
		if (pc.next_song == NULL && playlist->queued < 0)
			playlist_update_queued_song(playlist, NULL);
	}
}
示例#5
0
void
pc_clear_error(void)
{
	player_lock();
	pc.error = PLAYER_ERROR_NOERROR;
	pc.errored_song = NULL;
	player_unlock();
}
示例#6
0
文件: player.c 项目: ayoucai/cmus
void player_stop(void)
{
	player_lock();
	__consumer_stop();
	__producer_stop();
	__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 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;

	if (decoder_lock_has_failed(dc)) {
		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->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;
}
示例#8
0
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);
}
示例#9
0
void
pc_enqueue_song(struct song *song)
{
	assert(song != NULL);

	player_lock();
	pc_enqueue_song_locked(song);
	player_unlock();
}
示例#10
0
文件: player.c 项目: jmerdich/cmus
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, &param);
		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();
}
示例#11
0
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;
}
示例#12
0
文件: player.c 项目: ayoucai/cmus
void player_set_rg_preamp(double db)
{
	player_lock();
	replaygain_preamp = db;

	player_info_lock();
	update_rg_scale();
	player_info_unlock();

	player_unlock();
}
示例#13
0
文件: player.c 项目: ayoucai/cmus
void player_set_rg_limit(int limit)
{
	player_lock();
	replaygain_limit = limit;

	player_info_lock();
	update_rg_scale();
	player_info_unlock();

	player_unlock();
}
示例#14
0
文件: player.c 项目: ayoucai/cmus
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();
}
示例#15
0
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
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();
}
示例#17
0
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);
}
示例#18
0
void
pc_pause(void)
{
	player_lock();

	if (pc.state != PLAYER_STATE_STOP) {
		player_command_locked(PLAYER_COMMAND_PAUSE);
		idle_add(IDLE_PLAYER);
	}

	player_unlock();
}
示例#19
0
文件: player.c 项目: ayoucai/cmus
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();
}
示例#20
0
/**
 * 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;
	}
}
示例#21
0
文件: player.c 项目: ayoucai/cmus
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, &param);
		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();
}
示例#22
0
文件: player.c 项目: ayoucai/cmus
void player_play(void)
{
	int prebuffer;

	player_lock();
	if (producer_status == PS_PLAYING && ip_is_remote(ip)) {
		/* seeking not allowed */
		player_unlock();
		return;
	}
	prebuffer = consumer_status == CS_STOPPED;
	__producer_play();
	if (producer_status == PS_PLAYING) {
		__consumer_play();
		if (consumer_status != CS_PLAYING)
			__producer_stop();
	} else {
		__consumer_stop();
	}
	__player_status_changed();
	if (consumer_status == CS_PLAYING && prebuffer)
		__prebuffer();
	player_unlock();
}
示例#23
0
文件: player.c 项目: ayoucai/cmus
void player_exit(void)
{
	int rc;

	player_lock();
	consumer_running = 0;
	pthread_cond_broadcast(&consumer_playing);
	producer_running = 0;
	pthread_cond_broadcast(&producer_playing);
	player_unlock();
	
	rc = pthread_join(consumer_thread, NULL);
	BUG_ON(rc);
	rc = pthread_join(producer_thread, NULL);
	BUG_ON(rc);
	buffer_free();
}
void
pc_get_status(struct player_status *status)
{
	player_lock();
	player_command_locked(PLAYER_COMMAND_REFRESH);

	status->state = pc.state;

	if (pc.state != PLAYER_STATE_STOP) {
		status->bit_rate = pc.bit_rate;
		status->audio_format = pc.audio_format;
		status->total_time = pc.total_time;
		status->elapsed_time = pc.elapsed_time;
	}

	player_unlock();
}
示例#25
0
bool
pc_seek(struct song *song, float seek_time)
{
	assert(song != NULL);

	player_lock();
	pc.next_song = song;
	pc.seek_where = seek_time;
	player_command_locked(PLAYER_COMMAND_SEEK);
	player_unlock();

	assert(pc.next_song == NULL);

	idle_add(IDLE_PLAYER);

	return true;
}
示例#26
0
void
pc_play(struct song *song)
{
	assert(song != NULL);

	player_lock();

	if (pc.state != PLAYER_STATE_STOP)
		player_command_locked(PLAYER_COMMAND_STOP);

	assert(pc.next_song == NULL);

	pc_enqueue_song_locked(song);

	assert(pc.next_song == NULL);

	player_unlock();

	idle_add(IDLE_PLAYER);
}
示例#27
0
bool
pc_seek(struct player_control *pc, struct song *song, float seek_time)
{
	assert(song != NULL);

	player_lock(pc);

	if (pc->next_song != NULL)
		song_free(pc->next_song);

	pc->next_song = song;
	pc->seek_where = seek_time;
	player_command_locked(pc, PLAYER_COMMAND_SEEK);
	player_unlock(pc);

	assert(pc->next_song == NULL);

	idle_add(IDLE_PLAYER);

	return true;
}
示例#28
0
void
pc_set_pause(struct player_control *pc, bool pause_flag)
{
	player_lock(pc);

	switch (pc->state) {
	case PLAYER_STATE_STOP:
		break;

	case PLAYER_STATE_PLAY:
		if (pause_flag)
			pc_pause_locked(pc);
		break;

	case PLAYER_STATE_PAUSE:
		if (!pause_flag)
			pc_pause_locked(pc);
		break;
	}

	player_unlock(pc);
}
示例#29
0
void
pc_set_pause(bool pause_flag)
{
	player_lock();

	switch (pc.state) {
	case PLAYER_STATE_STOP:
		break;

	case PLAYER_STATE_PLAY:
		if (pause_flag)
			pc_pause_locked();
		break;

	case PLAYER_STATE_PAUSE:
		if (!pause_flag)
			pc_pause_locked();
		break;
	}

	player_unlock();
}
/*
 * 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);
}