player_thread: make the music_buffer instance global

Preparation for the next patch: since the output devices stay open
even when the player thread stops playing, we will need a persistent
music buffer.
This commit is contained in:
Max Kellermann 2009-03-09 19:12:06 +01:00
parent 8de179ef7b
commit d213f9a3e5

View File

@ -45,7 +45,6 @@ enum xfade_state {
}; };
struct player { struct player {
struct music_buffer *buffer;
struct music_pipe *pipe; struct music_pipe *pipe;
/** /**
@ -91,6 +90,8 @@ struct player {
double size_to_time; double size_to_time;
}; };
static struct music_buffer *player_buffer;
static void player_command_finished(void) static void player_command_finished(void)
{ {
assert(pc.command != PLAYER_COMMAND_NONE); assert(pc.command != PLAYER_COMMAND_NONE);
@ -105,7 +106,7 @@ player_dc_stop(struct player *player)
dc_stop(&pc.notify); dc_stop(&pc.notify);
if (dc.pipe != NULL) { if (dc.pipe != NULL) {
music_pipe_clear(dc.pipe, player->buffer); music_pipe_clear(dc.pipe, player_buffer);
if (dc.pipe != player->pipe) if (dc.pipe != player->pipe)
music_pipe_free(dc.pipe); music_pipe_free(dc.pipe);
@ -207,7 +208,7 @@ static bool player_seek_decoder(struct player *player)
if (decoder_current_song() != pc.next_song) { if (decoder_current_song() != pc.next_song) {
player_dc_stop(player); player_dc_stop(player);
music_pipe_clear(player->pipe, player->buffer); music_pipe_clear(player->pipe, player_buffer);
dc.pipe = player->pipe; dc.pipe = player->pipe;
dc_start_async(pc.next_song); dc_start_async(pc.next_song);
@ -379,17 +380,16 @@ static void do_play(void)
bool cross_fading = false; bool cross_fading = false;
static const char silence[CHUNK_SIZE]; static const char silence[CHUNK_SIZE];
player.buffer = music_buffer_new(pc.buffer_chunks);
player.pipe = music_pipe_new(); player.pipe = music_pipe_new();
dc.buffer = player.buffer; dc.buffer = player_buffer;
dc.pipe = player.pipe; dc.pipe = player.pipe;
dc_start(&pc.notify, pc.next_song); dc_start(&pc.notify, pc.next_song);
if (!player_wait_for_decoder(&player)) { if (!player_wait_for_decoder(&player)) {
player_stop_decoder(); player_stop_decoder();
player_command_finished(); player_command_finished();
music_pipe_free(player.pipe); music_pipe_free(player.pipe);
music_buffer_free(player.buffer); music_buffer_free(player_buffer);
return; return;
} }
@ -456,7 +456,7 @@ static void do_play(void)
cross_fade_calc(pc.cross_fade_seconds, dc.total_time, cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
&dc.out_audio_format, &dc.out_audio_format,
&player.play_audio_format, &player.play_audio_format,
music_buffer_size(player.buffer) - music_buffer_size(player_buffer) -
pc.buffered_before_play); pc.buffered_before_play);
if (crossFadeChunks > 0) { if (crossFadeChunks > 0) {
player.xfade = XFADE_ENABLED; player.xfade = XFADE_ENABLED;
@ -498,7 +498,7 @@ static void do_play(void)
&dc.out_audio_format, &dc.out_audio_format,
fadePosition, fadePosition,
crossFadeChunks); crossFadeChunks);
music_buffer_return(player.buffer, other_chunk); music_buffer_return(player_buffer, other_chunk);
} else { } else {
/* there are not enough /* there are not enough
decoded chunks yet */ decoded chunks yet */
@ -526,7 +526,7 @@ static void do_play(void)
success = play_chunk(player.song, chunk, success = play_chunk(player.song, chunk,
&player.play_audio_format, &player.play_audio_format,
player.size_to_time); player.size_to_time);
music_buffer_return(player.buffer, chunk); music_buffer_return(player_buffer, chunk);
if (!success) if (!success)
break; break;
@ -537,7 +537,7 @@ static void do_play(void)
larger block at a time */ larger block at a time */
if (!decoder_is_idle() && if (!decoder_is_idle() &&
music_pipe_size(dc.pipe) <= (pc.buffered_before_play + music_pipe_size(dc.pipe) <= (pc.buffered_before_play +
music_buffer_size(player.buffer) * 3) / 4) music_buffer_size(player_buffer) * 3) / 4)
notify_signal(&dc.notify); notify_signal(&dc.notify);
} else if (dc.pipe != NULL && dc.pipe != player.pipe) { } else if (dc.pipe != NULL && dc.pipe != player.pipe) {
/* at the beginning of a new song */ /* at the beginning of a new song */
@ -572,20 +572,20 @@ static void do_play(void)
player_stop_decoder(); player_stop_decoder();
if (dc.pipe != NULL && dc.pipe != player.pipe) { if (dc.pipe != NULL && dc.pipe != player.pipe) {
music_pipe_clear(dc.pipe, player.buffer); music_pipe_clear(dc.pipe, player_buffer);
music_pipe_free(dc.pipe); music_pipe_free(dc.pipe);
} }
music_pipe_clear(player.pipe, player.buffer); music_pipe_clear(player.pipe, player_buffer);
music_pipe_free(player.pipe); music_pipe_free(player.pipe);
music_buffer_free(player.buffer);
} }
static gpointer player_task(G_GNUC_UNUSED gpointer arg) static gpointer player_task(G_GNUC_UNUSED gpointer arg)
{ {
decoder_thread_start(); decoder_thread_start();
player_buffer = music_buffer_new(pc.buffer_chunks);
while (1) { while (1) {
switch (pc.command) { switch (pc.command) {
case PLAYER_COMMAND_PLAY: case PLAYER_COMMAND_PLAY:
@ -610,6 +610,7 @@ static gpointer player_task(G_GNUC_UNUSED gpointer arg)
case PLAYER_COMMAND_EXIT: case PLAYER_COMMAND_EXIT:
dc_quit(); dc_quit();
audio_output_all_close(); audio_output_all_close();
music_buffer_free(player_buffer);
player_command_finished(); player_command_finished();
g_thread_exit(NULL); g_thread_exit(NULL);
break; break;