Commit b2789c59 authored by Max Kellermann's avatar Max Kellermann

PlayerThread: use {decoder,player}_control references

parent 92a93c12
...@@ -462,7 +462,7 @@ int mpd_main(int argc, char *argv[]) ...@@ -462,7 +462,7 @@ int mpd_main(int argc, char *argv[])
ZeroconfInit(*main_loop); ZeroconfInit(*main_loop);
player_create(&instance->partition->pc); player_create(instance->partition->pc);
if (create_db) { if (create_db) {
/* the database failed to load: recreate the /* the database failed to load: recreate the
......
...@@ -50,9 +50,9 @@ enum xfade_state { ...@@ -50,9 +50,9 @@ enum xfade_state {
}; };
struct player { struct player {
struct player_control *pc; player_control &pc;
struct decoder_control *dc; decoder_control &dc;
MusicBuffer &buffer; MusicBuffer &buffer;
...@@ -128,7 +128,7 @@ struct player { ...@@ -128,7 +128,7 @@ struct player {
*/ */
float elapsed_time; float elapsed_time;
player(player_control *_pc, decoder_control *_dc, player(player_control &_pc, decoder_control &_dc,
MusicBuffer &_buffer) MusicBuffer &_buffer)
:pc(_pc), dc(_dc), buffer(_buffer), :pc(_pc), dc(_dc), buffer(_buffer),
buffering(false), buffering(false),
...@@ -145,20 +145,20 @@ struct player { ...@@ -145,20 +145,20 @@ struct player {
}; };
static void static void
player_command_finished_locked(struct player_control *pc) player_command_finished_locked(player_control &pc)
{ {
assert(pc->command != PLAYER_COMMAND_NONE); assert(pc.command != PLAYER_COMMAND_NONE);
pc->command = PLAYER_COMMAND_NONE; pc.command = PLAYER_COMMAND_NONE;
pc->ClientSignal(); pc.ClientSignal();
} }
static void static void
player_command_finished(struct player_control *pc) player_command_finished(player_control &pc)
{ {
pc->Lock(); pc.Lock();
player_command_finished_locked(pc); player_command_finished_locked(pc);
pc->Unlock(); pc.Unlock();
} }
/** /**
...@@ -169,19 +169,19 @@ player_command_finished(struct player_control *pc) ...@@ -169,19 +169,19 @@ player_command_finished(struct player_control *pc)
static void static void
player_dc_start(struct player *player, MusicPipe &pipe) player_dc_start(struct player *player, MusicPipe &pipe)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
assert(player->queued || pc->command == PLAYER_COMMAND_SEEK); assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
unsigned start_ms = pc->next_song->start_ms; unsigned start_ms = pc.next_song->start_ms;
if (pc->command == PLAYER_COMMAND_SEEK) if (pc.command == PLAYER_COMMAND_SEEK)
start_ms += (unsigned)(pc->seek_where * 1000); start_ms += (unsigned)(pc.seek_where * 1000);
dc->Start(pc->next_song->DupDetached(), dc.Start(pc.next_song->DupDetached(),
start_ms, pc->next_song->end_ms, start_ms, pc.next_song->end_ms,
player->buffer, pipe); player->buffer, pipe);
} }
/** /**
...@@ -196,7 +196,7 @@ player_dc_at_current_song(const struct player *player) ...@@ -196,7 +196,7 @@ player_dc_at_current_song(const struct player *player)
assert(player != NULL); assert(player != NULL);
assert(player->pipe != NULL); assert(player->pipe != NULL);
return player->dc->pipe == player->pipe; return player->dc.pipe == player->pipe;
} }
/** /**
...@@ -207,7 +207,7 @@ player_dc_at_current_song(const struct player *player) ...@@ -207,7 +207,7 @@ player_dc_at_current_song(const struct player *player)
static bool static bool
player_dc_at_next_song(const struct player *player) player_dc_at_next_song(const struct player *player)
{ {
return player->dc->pipe != NULL && !player_dc_at_current_song(player); return player->dc.pipe != NULL && !player_dc_at_current_song(player);
} }
/** /**
...@@ -218,19 +218,19 @@ player_dc_at_next_song(const struct player *player) ...@@ -218,19 +218,19 @@ player_dc_at_next_song(const struct player *player)
static void static void
player_dc_stop(struct player *player) player_dc_stop(struct player *player)
{ {
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
dc->Stop(); dc.Stop();
if (dc->pipe != NULL) { if (dc.pipe != NULL) {
/* clear and free the decoder pipe */ /* clear and free the decoder pipe */
dc->pipe->Clear(player->buffer); dc.pipe->Clear(player->buffer);
if (dc->pipe != player->pipe) if (dc.pipe != player->pipe)
delete dc->pipe; delete dc.pipe;
dc->pipe = NULL; dc.pipe = NULL;
} }
} }
...@@ -244,23 +244,23 @@ player_dc_stop(struct player *player) ...@@ -244,23 +244,23 @@ player_dc_stop(struct player *player)
static bool static bool
player_wait_for_decoder(struct player *player) player_wait_for_decoder(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
assert(player->queued || pc->command == PLAYER_COMMAND_SEEK); assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
player->queued = false; player->queued = false;
Error error = dc->LockGetError(); Error error = dc.LockGetError();
if (error.IsDefined()) { if (error.IsDefined()) {
pc->Lock(); pc.Lock();
pc->SetError(PLAYER_ERROR_DECODER, std::move(error)); pc.SetError(PLAYER_ERROR_DECODER, std::move(error));
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
pc->Unlock(); pc.Unlock();
return false; return false;
} }
...@@ -268,24 +268,24 @@ player_wait_for_decoder(struct player *player) ...@@ -268,24 +268,24 @@ player_wait_for_decoder(struct player *player)
if (player->song != NULL) if (player->song != NULL)
player->song->Free(); player->song->Free();
player->song = pc->next_song; player->song = pc.next_song;
player->elapsed_time = 0.0; player->elapsed_time = 0.0;
/* set the "starting" flag, which will be cleared by /* set the "starting" flag, which will be cleared by
player_check_decoder_startup() */ player_check_decoder_startup() */
player->decoder_starting = true; player->decoder_starting = true;
pc->Lock(); pc.Lock();
/* update player_control's song information */ /* update player_control's song information */
pc->total_time = pc->next_song->GetDuration(); pc.total_time = pc.next_song->GetDuration();
pc->bit_rate = 0; pc.bit_rate = 0;
pc->audio_format.Clear(); pc.audio_format.Clear();
/* clear the queued song */ /* clear the queued song */
pc->next_song = NULL; pc.next_song = NULL;
pc->Unlock(); pc.Unlock();
/* call syncPlaylistWithQueue() in the main thread */ /* call syncPlaylistWithQueue() in the main thread */
GlobalEvents::Emit(GlobalEvents::PLAYLIST); GlobalEvents::Emit(GlobalEvents::PLAYLIST);
...@@ -322,11 +322,11 @@ real_song_duration(const Song *song, double decoder_duration) ...@@ -322,11 +322,11 @@ real_song_duration(const Song *song, double decoder_duration)
static bool static bool
player_open_output(struct player *player) player_open_output(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
assert(player->play_audio_format.IsDefined()); assert(player->play_audio_format.IsDefined());
assert(pc->state == PLAYER_STATE_PLAY || assert(pc.state == PLAYER_STATE_PLAY ||
pc->state == PLAYER_STATE_PAUSE); pc.state == PLAYER_STATE_PAUSE);
Error error; Error error;
if (audio_output_all_open(player->play_audio_format, player->buffer, if (audio_output_all_open(player->play_audio_format, player->buffer,
...@@ -334,9 +334,9 @@ player_open_output(struct player *player) ...@@ -334,9 +334,9 @@ player_open_output(struct player *player)
player->output_open = true; player->output_open = true;
player->paused = false; player->paused = false;
pc->Lock(); pc.Lock();
pc->state = PLAYER_STATE_PLAY; pc.state = PLAYER_STATE_PLAY;
pc->Unlock(); pc.Unlock();
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
...@@ -350,10 +350,10 @@ player_open_output(struct player *player) ...@@ -350,10 +350,10 @@ player_open_output(struct player *player)
audio output becomes available */ audio output becomes available */
player->paused = true; player->paused = true;
pc->Lock(); pc.Lock();
pc->SetError(PLAYER_ERROR_OUTPUT, std::move(error)); pc.SetError(PLAYER_ERROR_OUTPUT, std::move(error));
pc->state = PLAYER_STATE_PAUSE; pc.state = PLAYER_STATE_PAUSE;
pc->Unlock(); pc.Unlock();
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
...@@ -371,46 +371,46 @@ player_open_output(struct player *player) ...@@ -371,46 +371,46 @@ player_open_output(struct player *player)
static bool static bool
player_check_decoder_startup(struct player *player) player_check_decoder_startup(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
assert(player->decoder_starting); assert(player->decoder_starting);
dc->Lock(); dc.Lock();
Error error = dc->GetError(); Error error = dc.GetError();
if (error.IsDefined()) { if (error.IsDefined()) {
/* the decoder failed */ /* the decoder failed */
dc->Unlock(); dc.Unlock();
pc->Lock(); pc.Lock();
pc->SetError(PLAYER_ERROR_DECODER, std::move(error)); pc.SetError(PLAYER_ERROR_DECODER, std::move(error));
pc->Unlock(); pc.Unlock();
return false; return false;
} else if (!dc->IsStarting()) { } else if (!dc.IsStarting()) {
/* the decoder is ready and ok */ /* the decoder is ready and ok */
dc->Unlock(); dc.Unlock();
if (player->output_open && if (player->output_open &&
!audio_output_all_wait(pc, 1)) !audio_output_all_wait(&pc, 1))
/* the output devices havn't finished playing /* the output devices havn't finished playing
all chunks yet - wait for that */ all chunks yet - wait for that */
return true; return true;
pc->Lock(); pc.Lock();
pc->total_time = real_song_duration(dc->song, dc->total_time); pc.total_time = real_song_duration(dc.song, dc.total_time);
pc->audio_format = dc->in_audio_format; pc.audio_format = dc.in_audio_format;
pc->Unlock(); pc.Unlock();
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
player->play_audio_format = dc->out_audio_format; player->play_audio_format = dc.out_audio_format;
player->decoder_starting = false; player->decoder_starting = false;
if (!player->paused && !player_open_output(player)) { if (!player->paused && !player_open_output(player)) {
char *uri = dc->song->GetURI(); char *uri = dc.song->GetURI();
g_warning("problems opening audio device " g_warning("problems opening audio device "
"while playing \"%s\"", uri); "while playing \"%s\"", uri);
g_free(uri); g_free(uri);
...@@ -422,8 +422,8 @@ player_check_decoder_startup(struct player *player) ...@@ -422,8 +422,8 @@ player_check_decoder_startup(struct player *player)
} else { } else {
/* the decoder is not yet ready; wait /* the decoder is not yet ready; wait
some more */ some more */
dc->WaitForDecoder(); dc.WaitForDecoder();
dc->Unlock(); dc.Unlock();
return true; return true;
} }
...@@ -478,15 +478,15 @@ player_send_silence(struct player *player) ...@@ -478,15 +478,15 @@ player_send_silence(struct player *player)
*/ */
static bool player_seek_decoder(struct player *player) static bool player_seek_decoder(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
Song *song = pc->next_song; Song *song = pc.next_song;
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
const unsigned start_ms = song->start_ms; const unsigned start_ms = song->start_ms;
if (!dc->LockIsCurrentSong(song)) { if (!dc.LockIsCurrentSong(song)) {
/* the decoder is already decoding the "next" song - /* the decoder is already decoding the "next" song -
stop it and start the previous song again */ stop it and start the previous song again */
...@@ -509,11 +509,11 @@ static bool player_seek_decoder(struct player *player) ...@@ -509,11 +509,11 @@ static bool player_seek_decoder(struct player *player)
but it is the same song file; exchange the pipe */ but it is the same song file; exchange the pipe */
player->pipe->Clear(player->buffer); player->pipe->Clear(player->buffer);
delete player->pipe; delete player->pipe;
player->pipe = dc->pipe; player->pipe = dc.pipe;
} }
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
player->queued = false; player->queued = false;
} }
...@@ -529,13 +529,13 @@ static bool player_seek_decoder(struct player *player) ...@@ -529,13 +529,13 @@ static bool player_seek_decoder(struct player *player)
/* send the SEEK command */ /* send the SEEK command */
double where = pc->seek_where; double where = pc.seek_where;
if (where > pc->total_time) if (where > pc.total_time)
where = pc->total_time - 0.1; where = pc.total_time - 0.1;
if (where < 0.0) if (where < 0.0)
where = 0.0; where = 0.0;
if (!dc->Seek(where + start_ms / 1000.0)) { if (!dc.Seek(where + start_ms / 1000.0)) {
/* decoder failure */ /* decoder failure */
player_command_finished(pc); player_command_finished(pc);
return false; return false;
...@@ -560,10 +560,10 @@ static bool player_seek_decoder(struct player *player) ...@@ -560,10 +560,10 @@ static bool player_seek_decoder(struct player *player)
*/ */
static void player_process_command(struct player *player) static void player_process_command(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
gcc_unused struct decoder_control *dc = player->dc; gcc_unused decoder_control &dc = player->dc;
switch (pc->command) { switch (pc.command) {
case PLAYER_COMMAND_NONE: case PLAYER_COMMAND_NONE:
case PLAYER_COMMAND_STOP: case PLAYER_COMMAND_STOP:
case PLAYER_COMMAND_EXIT: case PLAYER_COMMAND_EXIT:
...@@ -571,14 +571,14 @@ static void player_process_command(struct player *player) ...@@ -571,14 +571,14 @@ static void player_process_command(struct player *player)
break; break;
case PLAYER_COMMAND_UPDATE_AUDIO: case PLAYER_COMMAND_UPDATE_AUDIO:
pc->Unlock(); pc.Unlock();
audio_output_all_enable_disable(); audio_output_all_enable_disable();
pc->Lock(); pc.Lock();
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
case PLAYER_COMMAND_QUEUE: case PLAYER_COMMAND_QUEUE:
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
assert(!player->queued); assert(!player->queued);
assert(!player_dc_at_next_song(player)); assert(!player_dc_at_next_song(player));
...@@ -587,68 +587,68 @@ static void player_process_command(struct player *player) ...@@ -587,68 +587,68 @@ static void player_process_command(struct player *player)
break; break;
case PLAYER_COMMAND_PAUSE: case PLAYER_COMMAND_PAUSE:
pc->Unlock(); pc.Unlock();
player->paused = !player->paused; player->paused = !player->paused;
if (player->paused) { if (player->paused) {
audio_output_all_pause(); audio_output_all_pause();
pc->Lock(); pc.Lock();
pc->state = PLAYER_STATE_PAUSE; pc.state = PLAYER_STATE_PAUSE;
} else if (!player->play_audio_format.IsDefined()) { } else if (!player->play_audio_format.IsDefined()) {
/* the decoder hasn't provided an audio format /* the decoder hasn't provided an audio format
yet - don't open the audio device yet */ yet - don't open the audio device yet */
pc->Lock(); pc.Lock();
pc->state = PLAYER_STATE_PLAY; pc.state = PLAYER_STATE_PLAY;
} else { } else {
player_open_output(player); player_open_output(player);
pc->Lock(); pc.Lock();
} }
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
case PLAYER_COMMAND_SEEK: case PLAYER_COMMAND_SEEK:
pc->Unlock(); pc.Unlock();
player_seek_decoder(player); player_seek_decoder(player);
pc->Lock(); pc.Lock();
break; break;
case PLAYER_COMMAND_CANCEL: case PLAYER_COMMAND_CANCEL:
if (pc->next_song == NULL) { if (pc.next_song == NULL) {
/* the cancel request arrived too late, we're /* the cancel request arrived too late, we're
already playing the queued song... stop already playing the queued song... stop
everything now */ everything now */
pc->command = PLAYER_COMMAND_STOP; pc.command = PLAYER_COMMAND_STOP;
return; return;
} }
if (player_dc_at_next_song(player)) { if (player_dc_at_next_song(player)) {
/* the decoder is already decoding the song - /* the decoder is already decoding the song -
stop it and reset the position */ stop it and reset the position */
pc->Unlock(); pc.Unlock();
player_dc_stop(player); player_dc_stop(player);
pc->Lock(); pc.Lock();
} }
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
player->queued = false; player->queued = false;
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
case PLAYER_COMMAND_REFRESH: case PLAYER_COMMAND_REFRESH:
if (player->output_open && !player->paused) { if (player->output_open && !player->paused) {
pc->Unlock(); pc.Unlock();
audio_output_all_check(); audio_output_all_check();
pc->Lock(); pc.Lock();
} }
pc->elapsed_time = audio_output_all_get_elapsed_time(); pc.elapsed_time = audio_output_all_get_elapsed_time();
if (pc->elapsed_time < 0.0) if (pc.elapsed_time < 0.0)
pc->elapsed_time = player->elapsed_time; pc.elapsed_time = player->elapsed_time;
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
...@@ -685,7 +685,7 @@ update_song_tag(Song *song, const Tag &new_tag) ...@@ -685,7 +685,7 @@ update_song_tag(Song *song, const Tag &new_tag)
* Player lock is not held. * Player lock is not held.
*/ */
static bool static bool
play_chunk(struct player_control *pc, play_chunk(player_control &pc,
Song *song, struct music_chunk *chunk, Song *song, struct music_chunk *chunk,
MusicBuffer &buffer, MusicBuffer &buffer,
const AudioFormat format, const AudioFormat format,
...@@ -701,16 +701,16 @@ play_chunk(struct player_control *pc, ...@@ -701,16 +701,16 @@ play_chunk(struct player_control *pc,
return true; return true;
} }
pc->Lock(); pc.Lock();
pc->bit_rate = chunk->bit_rate; pc.bit_rate = chunk->bit_rate;
pc->Unlock(); pc.Unlock();
/* send the chunk to the audio outputs */ /* send the chunk to the audio outputs */
if (!audio_output_all_play(chunk, error)) if (!audio_output_all_play(chunk, error))
return false; return false;
pc->total_play_time += (double)chunk->length / pc.total_play_time += (double)chunk->length /
format.GetTimeToSize(); format.GetTimeToSize();
return true; return true;
} }
...@@ -724,10 +724,10 @@ play_chunk(struct player_control *pc, ...@@ -724,10 +724,10 @@ play_chunk(struct player_control *pc,
static bool static bool
play_next_chunk(struct player *player) play_next_chunk(struct player *player)
{ {
struct player_control *pc = player->pc; player_control &pc = player->pc;
struct decoder_control *dc = player->dc; decoder_control &dc = player->dc;
if (!audio_output_all_wait(pc, 64)) if (!audio_output_all_wait(&pc, 64))
/* the output pipe is still large enough, don't send /* the output pipe is still large enough, don't send
another chunk */ another chunk */
return true; return true;
...@@ -739,7 +739,7 @@ play_next_chunk(struct player *player) ...@@ -739,7 +739,7 @@ play_next_chunk(struct player *player)
(cross_fade_position = player->pipe->GetSize()) (cross_fade_position = player->pipe->GetSize())
<= player->cross_fade_chunks) { <= player->cross_fade_chunks) {
/* perform cross fade */ /* perform cross fade */
music_chunk *other_chunk = dc->pipe->Shift(); music_chunk *other_chunk = dc.pipe->Shift();
if (!player->cross_fading) { if (!player->cross_fading) {
/* beginning of the cross fade - adjust /* beginning of the cross fade - adjust
...@@ -763,7 +763,7 @@ play_next_chunk(struct player *player) ...@@ -763,7 +763,7 @@ play_next_chunk(struct player *player)
other_chunk->tag); other_chunk->tag);
other_chunk->tag = NULL; other_chunk->tag = NULL;
if (std::isnan(pc->mixramp_delay_seconds)) { if (std::isnan(pc.mixramp_delay_seconds)) {
chunk->mix_ratio = ((float)cross_fade_position) chunk->mix_ratio = ((float)cross_fade_position)
/ player->cross_fade_chunks; / player->cross_fade_chunks;
} else { } else {
...@@ -786,19 +786,19 @@ play_next_chunk(struct player *player) ...@@ -786,19 +786,19 @@ play_next_chunk(struct player *player)
} else { } else {
/* there are not enough decoded chunks yet */ /* there are not enough decoded chunks yet */
dc->Lock(); dc.Lock();
if (dc->IsIdle()) { if (dc.IsIdle()) {
/* the decoder isn't running, abort /* the decoder isn't running, abort
cross fading */ cross fading */
dc->Unlock(); dc.Unlock();
player->xfade = XFADE_DISABLED; player->xfade = XFADE_DISABLED;
} else { } else {
/* wait for the decoder */ /* wait for the decoder */
dc->Signal(); dc.Signal();
dc->WaitForDecoder(); dc.WaitForDecoder();
dc->Unlock(); dc.Unlock();
return true; return true;
} }
...@@ -827,16 +827,16 @@ play_next_chunk(struct player *player) ...@@ -827,16 +827,16 @@ play_next_chunk(struct player *player)
player->buffer.Return(chunk); player->buffer.Return(chunk);
pc->Lock(); pc.Lock();
pc->SetError(PLAYER_ERROR_OUTPUT, std::move(error)); pc.SetError(PLAYER_ERROR_OUTPUT, std::move(error));
/* pause: the user may resume playback as soon as an /* pause: the user may resume playback as soon as an
audio output becomes available */ audio output becomes available */
pc->state = PLAYER_STATE_PAUSE; pc.state = PLAYER_STATE_PAUSE;
player->paused = true; player->paused = true;
pc->Unlock(); pc.Unlock();
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
...@@ -846,12 +846,12 @@ play_next_chunk(struct player *player) ...@@ -846,12 +846,12 @@ play_next_chunk(struct player *player)
/* this formula should prevent that the decoder gets woken up /* this formula should prevent that the decoder gets woken up
with each chunk; it is more efficient to make it decode a with each chunk; it is more efficient to make it decode a
larger block at a time */ larger block at a time */
dc->Lock(); dc.Lock();
if (!dc->IsIdle() && if (!dc.IsIdle() &&
dc->pipe->GetSize() <= (pc->buffered_before_play + dc.pipe->GetSize() <= (pc.buffered_before_play +
player->buffer.GetSize() * 3) / 4) player->buffer.GetSize() * 3) / 4)
dc->Signal(); dc.Signal();
dc->Unlock(); dc.Unlock();
return true; return true;
} }
...@@ -875,23 +875,23 @@ player_song_border(struct player *player) ...@@ -875,23 +875,23 @@ player_song_border(struct player *player)
g_free(uri); g_free(uri);
delete player->pipe; delete player->pipe;
player->pipe = player->dc->pipe; player->pipe = player->dc.pipe;
audio_output_all_song_border(); audio_output_all_song_border();
if (!player_wait_for_decoder(player)) if (!player_wait_for_decoder(player))
return false; return false;
struct player_control *const pc = player->pc; player_control &pc = player->pc;
pc->Lock(); pc.Lock();
const bool border_pause = pc->border_pause; const bool border_pause = pc.border_pause;
if (border_pause) { if (border_pause) {
player->paused = true; player->paused = true;
pc->state = PLAYER_STATE_PAUSE; pc.state = PLAYER_STATE_PAUSE;
} }
pc->Unlock(); pc.Unlock();
if (border_pause) if (border_pause)
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
...@@ -905,12 +905,12 @@ player_song_border(struct player *player) ...@@ -905,12 +905,12 @@ player_song_border(struct player *player)
* decoder thread and the output threads. * decoder thread and the output threads.
*/ */
static void static void
do_play(struct player_control *pc, struct decoder_control *dc, do_play(player_control &pc, decoder_control &dc,
MusicBuffer &buffer) MusicBuffer &buffer)
{ {
player player(pc, dc, buffer); player player(pc, dc, buffer);
pc->Unlock(); pc.Unlock();
player.pipe = new MusicPipe(); player.pipe = new MusicPipe();
...@@ -922,37 +922,37 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -922,37 +922,37 @@ do_play(struct player_control *pc, struct decoder_control *dc,
player_command_finished(pc); player_command_finished(pc);
delete player.pipe; delete player.pipe;
GlobalEvents::Emit(GlobalEvents::PLAYLIST); GlobalEvents::Emit(GlobalEvents::PLAYLIST);
pc->Lock(); pc.Lock();
return; return;
} }
pc->Lock(); pc.Lock();
pc->state = PLAYER_STATE_PLAY; pc.state = PLAYER_STATE_PLAY;
if (pc->command == PLAYER_COMMAND_SEEK) if (pc.command == PLAYER_COMMAND_SEEK)
player.elapsed_time = pc->seek_where; player.elapsed_time = pc.seek_where;
player_command_finished_locked(pc); player_command_finished_locked(pc);
while (true) { while (true) {
player_process_command(&player); player_process_command(&player);
if (pc->command == PLAYER_COMMAND_STOP || if (pc.command == PLAYER_COMMAND_STOP ||
pc->command == PLAYER_COMMAND_EXIT || pc.command == PLAYER_COMMAND_EXIT ||
pc->command == PLAYER_COMMAND_CLOSE_AUDIO) { pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
pc->Unlock(); pc.Unlock();
audio_output_all_cancel(); audio_output_all_cancel();
break; break;
} }
pc->Unlock(); pc.Unlock();
if (player.buffering) { if (player.buffering) {
/* buffering at the start of the song - wait /* buffering at the start of the song - wait
until the buffer is large enough, to until the buffer is large enough, to
prevent stuttering on slow machines */ prevent stuttering on slow machines */
if (player.pipe->GetSize() < pc->buffered_before_play && if (player.pipe->GetSize() < pc.buffered_before_play &&
!dc->LockIsIdle()) { !dc.LockIsIdle()) {
/* not enough decoded buffer space yet */ /* not enough decoded buffer space yet */
if (!player.paused && if (!player.paused &&
...@@ -961,11 +961,11 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -961,11 +961,11 @@ do_play(struct player_control *pc, struct decoder_control *dc,
!player_send_silence(&player)) !player_send_silence(&player))
break; break;
dc->Lock(); dc.Lock();
/* XXX race condition: check decoder again */ /* XXX race condition: check decoder again */
dc->WaitForDecoder(); dc.WaitForDecoder();
dc->Unlock(); dc.Unlock();
pc->Lock(); pc.Lock();
continue; continue;
} else { } else {
/* buffering is complete */ /* buffering is complete */
...@@ -979,7 +979,7 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -979,7 +979,7 @@ do_play(struct player_control *pc, struct decoder_control *dc,
if (!player_check_decoder_startup(&player)) if (!player_check_decoder_startup(&player))
break; break;
pc->Lock(); pc.Lock();
continue; continue;
} }
...@@ -987,41 +987,41 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -987,41 +987,41 @@ do_play(struct player_control *pc, struct decoder_control *dc,
/* /*
music_pipe_check_format(&play_audio_format, music_pipe_check_format(&play_audio_format,
player.next_song_chunk, player.next_song_chunk,
&dc->out_audio_format); &dc.out_audio_format);
*/ */
#endif #endif
if (dc->LockIsIdle() && player.queued && if (dc.LockIsIdle() && player.queued &&
dc->pipe == player.pipe) { dc.pipe == player.pipe) {
/* the decoder has finished the current song; /* the decoder has finished the current song;
make it decode the next song */ make it decode the next song */
assert(dc->pipe == NULL || dc->pipe == player.pipe); assert(dc.pipe == NULL || dc.pipe == player.pipe);
player_dc_start(&player, *new MusicPipe()); player_dc_start(&player, *new MusicPipe());
} }
if (/* no cross-fading if MPD is going to pause at the if (/* no cross-fading if MPD is going to pause at the
end of the current song */ end of the current song */
!pc->border_pause && !pc.border_pause &&
player_dc_at_next_song(&player) && player_dc_at_next_song(&player) &&
player.xfade == XFADE_UNKNOWN && player.xfade == XFADE_UNKNOWN &&
!dc->LockIsStarting()) { !dc.LockIsStarting()) {
/* enable cross fading in this song? if yes, /* enable cross fading in this song? if yes,
calculate how many chunks will be required calculate how many chunks will be required
for it */ for it */
player.cross_fade_chunks = player.cross_fade_chunks =
cross_fade_calc(pc->cross_fade_seconds, dc->total_time, cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
pc->mixramp_db, pc.mixramp_db,
pc->mixramp_delay_seconds, pc.mixramp_delay_seconds,
dc->replay_gain_db, dc.replay_gain_db,
dc->replay_gain_prev_db, dc.replay_gain_prev_db,
dc->mixramp_start, dc.mixramp_start,
dc->mixramp_prev_end, dc.mixramp_prev_end,
dc->out_audio_format, dc.out_audio_format,
player.play_audio_format, player.play_audio_format,
player.buffer.GetSize() - player.buffer.GetSize() -
pc->buffered_before_play); pc.buffered_before_play);
if (player.cross_fade_chunks > 0) { if (player.cross_fade_chunks > 0) {
player.xfade = XFADE_ENABLED; player.xfade = XFADE_ENABLED;
player.cross_fading = false; player.cross_fading = false;
...@@ -1032,10 +1032,10 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -1032,10 +1032,10 @@ do_play(struct player_control *pc, struct decoder_control *dc,
} }
if (player.paused) { if (player.paused) {
pc->Lock(); pc.Lock();
if (pc->command == PLAYER_COMMAND_NONE) if (pc.command == PLAYER_COMMAND_NONE)
pc->Wait(); pc.Wait();
continue; continue;
} else if (!player.pipe->IsEmpty()) { } else if (!player.pipe->IsEmpty()) {
/* at least one music chunk is ready - send it /* at least one music chunk is ready - send it
...@@ -1054,7 +1054,7 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -1054,7 +1054,7 @@ do_play(struct player_control *pc, struct decoder_control *dc,
if (!player_song_border(&player)) if (!player_song_border(&player))
break; break;
} else if (dc->LockIsIdle()) { } else if (dc.LockIsIdle()) {
/* check the size of the pipe again, because /* check the size of the pipe again, because
the decoder thread may have added something the decoder thread may have added something
since we last checked */ since we last checked */
...@@ -1072,7 +1072,7 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -1072,7 +1072,7 @@ do_play(struct player_control *pc, struct decoder_control *dc,
break; break;
} }
pc->Lock(); pc.Lock();
} }
player_dc_stop(&player); player_dc_stop(&player);
...@@ -1085,66 +1085,66 @@ do_play(struct player_control *pc, struct decoder_control *dc, ...@@ -1085,66 +1085,66 @@ do_play(struct player_control *pc, struct decoder_control *dc,
if (player.song != NULL) if (player.song != NULL)
player.song->Free(); player.song->Free();
pc->Lock(); pc.Lock();
if (player.queued) { if (player.queued) {
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
} }
pc->state = PLAYER_STATE_STOP; pc.state = PLAYER_STATE_STOP;
pc->Unlock(); pc.Unlock();
GlobalEvents::Emit(GlobalEvents::PLAYLIST); GlobalEvents::Emit(GlobalEvents::PLAYLIST);
pc->Lock(); pc.Lock();
} }
static gpointer static gpointer
player_task(gpointer arg) player_task(gpointer arg)
{ {
struct player_control *pc = (struct player_control *)arg; player_control &pc = *(player_control *)arg;
decoder_control dc; decoder_control dc;
decoder_thread_start(&dc); decoder_thread_start(&dc);
MusicBuffer buffer(pc->buffer_chunks); MusicBuffer buffer(pc.buffer_chunks);
pc->Lock(); pc.Lock();
while (1) { while (1) {
switch (pc->command) { switch (pc.command) {
case PLAYER_COMMAND_SEEK: case PLAYER_COMMAND_SEEK:
case PLAYER_COMMAND_QUEUE: case PLAYER_COMMAND_QUEUE:
assert(pc->next_song != NULL); assert(pc.next_song != NULL);
do_play(pc, &dc, buffer); do_play(pc, dc, buffer);
break; break;
case PLAYER_COMMAND_STOP: case PLAYER_COMMAND_STOP:
pc->Unlock(); pc.Unlock();
audio_output_all_cancel(); audio_output_all_cancel();
pc->Lock(); pc.Lock();
/* fall through */ /* fall through */
case PLAYER_COMMAND_PAUSE: case PLAYER_COMMAND_PAUSE:
if (pc->next_song != NULL) { if (pc.next_song != NULL) {
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
} }
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
case PLAYER_COMMAND_CLOSE_AUDIO: case PLAYER_COMMAND_CLOSE_AUDIO:
pc->Unlock(); pc.Unlock();
audio_output_all_release(); audio_output_all_release();
pc->Lock(); pc.Lock();
player_command_finished_locked(pc); player_command_finished_locked(pc);
assert(buffer.IsEmptyUnsafe()); assert(buffer.IsEmptyUnsafe());
...@@ -1152,14 +1152,14 @@ player_task(gpointer arg) ...@@ -1152,14 +1152,14 @@ player_task(gpointer arg)
break; break;
case PLAYER_COMMAND_UPDATE_AUDIO: case PLAYER_COMMAND_UPDATE_AUDIO:
pc->Unlock(); pc.Unlock();
audio_output_all_enable_disable(); audio_output_all_enable_disable();
pc->Lock(); pc.Lock();
player_command_finished_locked(pc); player_command_finished_locked(pc);
break; break;
case PLAYER_COMMAND_EXIT: case PLAYER_COMMAND_EXIT:
pc->Unlock(); pc.Unlock();
dc.Quit(); dc.Quit();
...@@ -1169,9 +1169,9 @@ player_task(gpointer arg) ...@@ -1169,9 +1169,9 @@ player_task(gpointer arg)
return NULL; return NULL;
case PLAYER_COMMAND_CANCEL: case PLAYER_COMMAND_CANCEL:
if (pc->next_song != NULL) { if (pc.next_song != NULL) {
pc->next_song->Free(); pc.next_song->Free();
pc->next_song = NULL; pc.next_song = NULL;
} }
player_command_finished_locked(pc); player_command_finished_locked(pc);
...@@ -1183,23 +1183,23 @@ player_task(gpointer arg) ...@@ -1183,23 +1183,23 @@ player_task(gpointer arg)
break; break;
case PLAYER_COMMAND_NONE: case PLAYER_COMMAND_NONE:
pc->Wait(); pc.Wait();
break; break;
} }
} }
} }
void void
player_create(struct player_control *pc) player_create(player_control &pc)
{ {
assert(pc->thread == NULL); assert(pc.thread == NULL);
#if GLIB_CHECK_VERSION(2,32,0) #if GLIB_CHECK_VERSION(2,32,0)
pc->thread = g_thread_new("player", player_task, pc); pc.thread = g_thread_new("player", player_task, &pc);
#else #else
GError *e = NULL; GError *e = NULL;
pc->thread = g_thread_create(player_task, pc, true, &e); pc.thread = g_thread_create(player_task, &pc, true, &e);
if (pc->thread == NULL) if (pc.thread == NULL)
FatalError("Failed to spawn player task", e); FatalError("Failed to spawn player task", e);
#endif #endif
} }
...@@ -40,6 +40,6 @@ ...@@ -40,6 +40,6 @@
struct player_control; struct player_control;
void void
player_create(struct player_control *pc); player_create(player_control &pc);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment