player: added struct player

The player struct holds the local variables which used to be passed to
all those helper functions in player_thread.c.
This commit is contained in:
Max Kellermann 2008-10-12 00:02:23 +02:00
parent 2fcbabf4d1
commit 9d51bd392b

View File

@ -33,6 +33,36 @@ enum xfade_state {
XFADE_ENABLED = 1 XFADE_ENABLED = 1
}; };
struct player {
/**
* number of chunks which have to be decoded before playing
* starts
*/
unsigned int buffered_before_play;
/**
* true if the decoder is starting and did not provide data
* yet
*/
bool decoder_starting;
/**
* is the player paused?
*/
bool paused;
/**
* is cross fading enabled?
*/
enum xfade_state xfade;
/**
* index of the first chunk of the next song, -1 if there is
* no next song
*/
int next_song_chunk;
};
static void player_command_finished(void) static void player_command_finished(void)
{ {
assert(pc.command != PLAYER_COMMAND_NONE); assert(pc.command != PLAYER_COMMAND_NONE);
@ -49,7 +79,7 @@ static void quitDecode(void)
wakeup_main_task(); wakeup_main_task();
} }
static int waitOnDecode(int *decodeWaitedOn) static int waitOnDecode(struct player *player)
{ {
dc_command_wait(&pc.notify); dc_command_wait(&pc.notify);
@ -64,22 +94,22 @@ static int waitOnDecode(int *decodeWaitedOn)
? pc.next_song->tag->time : 0; ? pc.next_song->tag->time : 0;
pc.bitRate = 0; pc.bitRate = 0;
audio_format_clear(&pc.audio_format); audio_format_clear(&pc.audio_format);
*decodeWaitedOn = 1; player->decoder_starting = true;
return 0; return 0;
} }
static int decodeSeek(int *decodeWaitedOn, int *next) static int decodeSeek(struct player *player)
{ {
int ret = -1; int ret = -1;
double where; double where;
if (decoder_current_song() != pc.next_song) { if (decoder_current_song() != pc.next_song) {
dc_stop(&pc.notify); dc_stop(&pc.notify);
*next = -1; player->next_song_chunk = -1;
ob_clear(); ob_clear();
dc_start_async(pc.next_song); dc_start_async(pc.next_song);
waitOnDecode(decodeWaitedOn); waitOnDecode(player);
} }
where = pc.seekWhere; where = pc.seekWhere;
@ -97,10 +127,7 @@ static int decodeSeek(int *decodeWaitedOn, int *next)
return ret; return ret;
} }
static void processDecodeInput(int *pause_r, unsigned int *bbp_r, static void processDecodeInput(struct player *player)
enum xfade_state *do_xfade_r,
int *decodeWaitedOn_r,
int *next_r)
{ {
switch (pc.command) { switch (pc.command) {
case PLAYER_COMMAND_NONE: case PLAYER_COMMAND_NONE:
@ -121,8 +148,10 @@ static void processDecodeInput(int *pause_r, unsigned int *bbp_r,
break; break;
case PLAYER_COMMAND_PAUSE: case PLAYER_COMMAND_PAUSE:
*pause_r = !*pause_r; player->paused = !player->paused;
if (*pause_r) { if (player->paused) {
dropBufferedAudio();
audio_output_pause_all();
pc.state = PLAYER_STATE_PAUSE; pc.state = PLAYER_STATE_PAUSE;
} else { } else {
if (openAudioDevice(NULL) >= 0) { if (openAudioDevice(NULL) >= 0) {
@ -135,23 +164,17 @@ static void processDecodeInput(int *pause_r, unsigned int *bbp_r,
ERROR("problems opening audio device " ERROR("problems opening audio device "
"while playing \"%s\"\n", "while playing \"%s\"\n",
song_get_url(dc.next_song, tmp)); song_get_url(dc.next_song, tmp));
*pause_r = -1; player->paused = true;
} }
} }
player_command_finished(); player_command_finished();
if (*pause_r == -1) {
*pause_r = 1;
} else if (*pause_r) {
dropBufferedAudio();
audio_output_pause_all();
}
break; break;
case PLAYER_COMMAND_SEEK: case PLAYER_COMMAND_SEEK:
dropBufferedAudio(); dropBufferedAudio();
if (decodeSeek(decodeWaitedOn_r, next_r) == 0) { if (decodeSeek(player) == 0) {
*do_xfade_r = XFADE_UNKNOWN; player->xfade = XFADE_UNKNOWN;
*bbp_r = 0; player->buffered_before_play = 0;
} }
break; break;
} }
@ -176,25 +199,26 @@ static int playChunk(ob_chunk * chunk,
static void do_play(void) static void do_play(void)
{ {
int do_pause = 0; struct player player = {
.buffered_before_play = pc.buffered_before_play,
.decoder_starting = false,
.paused = false,
.xfade = XFADE_UNKNOWN,
.next_song_chunk = -1,
};
int buffering = 1; int buffering = 1;
unsigned int bbp = pc.buffered_before_play;
enum xfade_state do_xfade = XFADE_UNKNOWN;
unsigned int crossFadeChunks = 0; unsigned int crossFadeChunks = 0;
/** the position of the next cross-faded chunk in the next /** the position of the next cross-faded chunk in the next
song */ song */
int nextChunk = 0; int nextChunk = 0;
int decodeWaitedOn = 0;
static const char silence[CHUNK_SIZE]; static const char silence[CHUNK_SIZE];
double sizeToTime = 0.0; double sizeToTime = 0.0;
/** the position of the first chunk in the next song */
int next = -1;
ob_clear(); ob_clear();
ob_set_lazy(false); ob_set_lazy(false);
dc_start(&pc.notify, pc.next_song); dc_start(&pc.notify, pc.next_song);
if (waitOnDecode(&decodeWaitedOn) < 0) { if (waitOnDecode(&player) < 0) {
quitDecode(); quitDecode();
return; return;
} }
@ -204,8 +228,7 @@ static void do_play(void)
player_command_finished(); player_command_finished();
while (1) { while (1) {
processDecodeInput(&do_pause, &bbp, &do_xfade, processDecodeInput(&player);
&decodeWaitedOn, &next);
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) {
@ -214,7 +237,7 @@ static void do_play(void)
} }
if (buffering) { if (buffering) {
if (ob_available() < bbp) { if (ob_available() < player.buffered_before_play) {
/* not enough decoded buffer space yet */ /* not enough decoded buffer space yet */
notify_wait(&pc.notify); notify_wait(&pc.notify);
continue; continue;
@ -225,7 +248,7 @@ static void do_play(void)
} }
} }
if (decodeWaitedOn) { if (player.decoder_starting) {
if (dc.error != DECODE_ERROR_NOERROR) { if (dc.error != DECODE_ERROR_NOERROR) {
/* the decoder failed */ /* the decoder failed */
assert(dc.next_song == NULL || dc.next_song->url != NULL); assert(dc.next_song == NULL || dc.next_song->url != NULL);
@ -235,7 +258,7 @@ static void do_play(void)
} }
else if (!decoder_is_starting()) { else if (!decoder_is_starting()) {
/* the decoder is ready and ok */ /* the decoder is ready and ok */
decodeWaitedOn = 0; player.decoder_starting = false;
if(openAudioDevice(&(ob.audioFormat))<0) { if(openAudioDevice(&(ob.audioFormat))<0) {
char tmp[MPD_PATH_MAX]; char tmp[MPD_PATH_MAX];
assert(dc.next_song == NULL || dc.next_song->url != NULL); assert(dc.next_song == NULL || dc.next_song->url != NULL);
@ -247,7 +270,7 @@ static void do_play(void)
break; break;
} }
if (do_pause) { if (player.paused) {
dropBufferedAudio(); dropBufferedAudio();
closeAudioDevice(); closeAudioDevice();
} }
@ -268,12 +291,13 @@ static void do_play(void)
pc.queueLockState == PLAYER_QUEUE_UNLOCKED) { pc.queueLockState == PLAYER_QUEUE_UNLOCKED) {
/* 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 */
next = ob.end; player.next_song_chunk = ob.end;
dc_start_async(pc.next_song); dc_start_async(pc.next_song);
pc.queueState = PLAYER_QUEUE_DECODE; pc.queueState = PLAYER_QUEUE_DECODE;
wakeup_main_task(); wakeup_main_task();
} }
if (next >= 0 && do_xfade == XFADE_UNKNOWN && if (player.next_song_chunk >= 0 &&
player.xfade == XFADE_UNKNOWN &&
!decoder_is_starting()) { !decoder_is_starting()) {
/* 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
@ -284,21 +308,23 @@ static void do_play(void)
ob.size - ob.size -
pc.buffered_before_play); pc.buffered_before_play);
if (crossFadeChunks > 0) { if (crossFadeChunks > 0) {
do_xfade = XFADE_ENABLED; player.xfade = XFADE_ENABLED;
nextChunk = -1; nextChunk = -1;
} else } else
/* cross fading is disabled or the /* cross fading is disabled or the
next song is too short */ next song is too short */
do_xfade = XFADE_DISABLED; player.xfade = XFADE_DISABLED;
} }
if (do_pause) if (player.paused)
notify_wait(&pc.notify); notify_wait(&pc.notify);
else if (!ob_is_empty() && (int)ob.begin != next) { else if (!ob_is_empty() &&
(int)ob.begin != player.next_song_chunk) {
ob_chunk *beginChunk = ob_get_chunk(ob.begin); ob_chunk *beginChunk = ob_get_chunk(ob.begin);
unsigned int fadePosition; unsigned int fadePosition;
if (do_xfade == XFADE_ENABLED && next >= 0 && if (player.xfade == XFADE_ENABLED &&
(fadePosition = ob_relative(next)) player.next_song_chunk >= 0 &&
(fadePosition = ob_relative(player.next_song_chunk))
<= crossFadeChunks) { <= crossFadeChunks) {
/* perform cross fade */ /* perform cross fade */
if (nextChunk < 0) { if (nextChunk < 0) {
@ -324,7 +350,7 @@ static void do_play(void)
/* the decoder isn't /* the decoder isn't
running, abort running, abort
cross fading */ cross fading */
do_xfade = XFADE_DISABLED; player.xfade = XFADE_DISABLED;
} else { } else {
/* wait for the /* wait for the
decoder */ decoder */
@ -347,17 +373,18 @@ static void do_play(void)
larger block at a time */ larger block at a time */
if (ob_available() <= (pc.buffered_before_play + ob.size * 3) / 4) if (ob_available() <= (pc.buffered_before_play + ob.size * 3) / 4)
notify_signal(&dc.notify); notify_signal(&dc.notify);
} else if (!ob_is_empty() && (int)ob.begin == next) { } else if (!ob_is_empty() &&
(int)ob.begin == player.next_song_chunk) {
/* at the beginning of a new song */ /* at the beginning of a new song */
if (do_xfade == XFADE_ENABLED && nextChunk >= 0) { if (player.xfade == XFADE_ENABLED && nextChunk >= 0) {
/* the cross-fade is finished; skip /* the cross-fade is finished; skip
the section which was cross-faded the section which was cross-faded
(and thus already played) */ (and thus already played) */
ob_skip(crossFadeChunks); ob_skip(crossFadeChunks);
} }
do_xfade = XFADE_UNKNOWN; player.xfade = XFADE_UNKNOWN;
/* wait for a signal from the playlist */ /* wait for a signal from the playlist */
if (pc.queueState == PLAYER_QUEUE_DECODE || if (pc.queueState == PLAYER_QUEUE_DECODE ||
@ -368,8 +395,8 @@ static void do_play(void)
if (pc.queueState != PLAYER_QUEUE_PLAY) if (pc.queueState != PLAYER_QUEUE_PLAY)
break; break;
next = -1; player.next_song_chunk = -1;
if (waitOnDecode(&decodeWaitedOn) < 0) if (waitOnDecode(&player) < 0)
return; return;
pc.queueState = PLAYER_QUEUE_EMPTY; pc.queueState = PLAYER_QUEUE_EMPTY;