PlayerControl: convert functions to methods

This commit is contained in:
Max Kellermann 2013-09-27 09:20:53 +02:00
parent ef663810a2
commit a10f3a8aec
2 changed files with 76 additions and 66 deletions

View File

@ -22,15 +22,10 @@
#include "Idle.hxx" #include "Idle.hxx"
#include "Song.hxx" #include "Song.hxx"
#include "DecoderControl.hxx" #include "DecoderControl.hxx"
#include "Main.hxx"
#include <cmath> #include <cmath>
#include <assert.h> #include <assert.h>
#include <stdio.h>
static void
pc_enqueue_song_locked(struct player_control *pc, Song *song);
player_control::player_control(unsigned _buffer_chunks, player_control::player_control(unsigned _buffer_chunks,
unsigned _buffered_before_play) unsigned _buffered_before_play)
@ -61,31 +56,6 @@ player_control::~player_control()
next_song->Free(); next_song->Free();
} }
static void
player_command_wait_locked(struct player_control *pc)
{
while (pc->command != PLAYER_COMMAND_NONE)
pc->ClientWait();
}
static void
player_command_locked(struct player_control *pc, enum player_command cmd)
{
assert(pc->command == PLAYER_COMMAND_NONE);
pc->command = cmd;
pc->Signal();
player_command_wait_locked(pc);
}
static void
player_command(struct player_control *pc, enum player_command cmd)
{
pc->Lock();
player_command_locked(pc, cmd);
pc->Unlock();
}
void void
player_control::Play(Song *song) player_control::Play(Song *song)
{ {
@ -94,11 +64,11 @@ player_control::Play(Song *song)
Lock(); Lock();
if (state != PLAYER_STATE_STOP) if (state != PLAYER_STATE_STOP)
player_command_locked(this, PLAYER_COMMAND_STOP); SynchronousCommand(PLAYER_COMMAND_STOP);
assert(next_song == nullptr); assert(next_song == nullptr);
pc_enqueue_song_locked(this, song); EnqueueSongLocked(song);
assert(next_song == nullptr); assert(next_song == nullptr);
@ -108,14 +78,14 @@ player_control::Play(Song *song)
void void
player_control::Cancel() player_control::Cancel()
{ {
player_command(this, PLAYER_COMMAND_CANCEL); LockSynchronousCommand(PLAYER_COMMAND_CANCEL);
assert(next_song == NULL); assert(next_song == NULL);
} }
void void
player_control::Stop() player_control::Stop()
{ {
player_command(this, PLAYER_COMMAND_CLOSE_AUDIO); LockSynchronousCommand(PLAYER_COMMAND_CLOSE_AUDIO);
assert(next_song == nullptr); assert(next_song == nullptr);
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
@ -124,7 +94,7 @@ player_control::Stop()
void void
player_control::UpdateAudio() player_control::UpdateAudio()
{ {
player_command(this, PLAYER_COMMAND_UPDATE_AUDIO); LockSynchronousCommand(PLAYER_COMMAND_UPDATE_AUDIO);
} }
void void
@ -132,35 +102,30 @@ player_control::Kill()
{ {
assert(thread != NULL); assert(thread != NULL);
player_command(this, PLAYER_COMMAND_EXIT); LockSynchronousCommand(PLAYER_COMMAND_EXIT);
g_thread_join(thread); g_thread_join(thread);
thread = NULL; thread = NULL;
idle_add(IDLE_PLAYER); idle_add(IDLE_PLAYER);
} }
void
player_control::PauseLocked()
{
if (state != PLAYER_STATE_STOP) {
SynchronousCommand(PLAYER_COMMAND_PAUSE);
idle_add(IDLE_PLAYER);
}
}
void void
player_control::Pause() player_control::Pause()
{ {
Lock(); Lock();
PauseLocked();
if (state != PLAYER_STATE_STOP) {
player_command_locked(this, PLAYER_COMMAND_PAUSE);
idle_add(IDLE_PLAYER);
}
Unlock(); Unlock();
} }
static void
pc_pause_locked(struct player_control *pc)
{
if (pc->state != PLAYER_STATE_STOP) {
player_command_locked(pc, PLAYER_COMMAND_PAUSE);
idle_add(IDLE_PLAYER);
}
}
void void
player_control::SetPause(bool pause_flag) player_control::SetPause(bool pause_flag)
{ {
@ -172,12 +137,12 @@ player_control::SetPause(bool pause_flag)
case PLAYER_STATE_PLAY: case PLAYER_STATE_PLAY:
if (pause_flag) if (pause_flag)
pc_pause_locked(this); PauseLocked();
break; break;
case PLAYER_STATE_PAUSE: case PLAYER_STATE_PAUSE:
if (!pause_flag) if (!pause_flag)
pc_pause_locked(this); PauseLocked();
break; break;
} }
@ -198,7 +163,7 @@ player_control::GetStatus()
player_status status; player_status status;
Lock(); Lock();
player_command_locked(this, PLAYER_COMMAND_REFRESH); SynchronousCommand(PLAYER_COMMAND_REFRESH);
status.state = state; status.state = state;
@ -248,23 +213,13 @@ player_control::GetErrorMessage() const
return message; return message;
} }
static void
pc_enqueue_song_locked(struct player_control *pc, Song *song)
{
assert(song != NULL);
assert(pc->next_song == NULL);
pc->next_song = song;
player_command_locked(pc, PLAYER_COMMAND_QUEUE);
}
void void
player_control::EnqueueSong(Song *song) player_control::EnqueueSong(Song *song)
{ {
assert(song != NULL); assert(song != NULL);
Lock(); Lock();
pc_enqueue_song_locked(this, song); EnqueueSongLocked(song);
Unlock(); Unlock();
} }
@ -280,7 +235,7 @@ player_control::Seek(Song *song, float seek_time)
next_song = song; next_song = song;
seek_where = seek_time; seek_where = seek_time;
player_command_locked(this, PLAYER_COMMAND_SEEK); SynchronousCommand(PLAYER_COMMAND_SEEK);
Unlock(); Unlock();
assert(next_song == nullptr); assert(next_song == nullptr);

View File

@ -242,6 +242,47 @@ struct player_control {
ClientSignal(); ClientSignal();
} }
private:
/**
* Wait for the command to be finished by the player thread.
*
* To be called from the main thread. Caller must lock the
* object.
*/
void WaitCommandLocked() {
while (command != PLAYER_COMMAND_NONE)
ClientWait();
}
/**
* Send a command to the player thread and synchronously wait
* for it to finish.
*
* To be called from the main thread. Caller must lock the
* object.
*/
void SynchronousCommand(player_command cmd) {
assert(command == PLAYER_COMMAND_NONE);
command = cmd;
Signal();
WaitCommandLocked();
}
/**
* Send a command to the player thread and synchronously wait
* for it to finish.
*
* To be called from the main thread. This method locks the
* object.
*/
void LockSynchronousCommand(player_command cmd) {
Lock();
SynchronousCommand(cmd);
Unlock();
}
public:
/** /**
* @param song the song to be queued; the given instance will * @param song the song to be queued; the given instance will
* be owned and freed by the player * be owned and freed by the player
@ -255,6 +296,10 @@ struct player_control {
void SetPause(bool pause_flag); void SetPause(bool pause_flag);
private:
void PauseLocked();
public:
void Pause(); void Pause();
/** /**
@ -298,6 +343,16 @@ struct player_control {
void UpdateAudio(); void UpdateAudio();
private:
void EnqueueSongLocked(Song *song) {
assert(song != nullptr);
assert(next_song == nullptr);
next_song = song;
SynchronousCommand(PLAYER_COMMAND_QUEUE);
}
public:
/** /**
* @param song the song to be queued; the given instance will be owned * @param song the song to be queued; the given instance will be owned
* and freed by the player * and freed by the player