Initial cut of fork() => pthreads() for decoder and player

I initially started to do a heavy rewrite that changed the way processes
communicated, but that was too much to do at once.  So this change only
focuses on replacing the player and decode processes with threads and
using condition variables instead of polling in loops; so the changeset
itself is quiet small.

* The shared output buffer variables will still need locking
to guard against race conditions.  So in this effect, we're probably
just as buggy as before.  The reduced context-switching overhead of
using threads instead of processes may even make bugs show up more or
less often...

* Basic functionality appears to be working for playing local (and NFS)
audio, including:
play, pause, stop, seek, previous, next, and main playlist editing

* I haven't tested HTTP streams yet, they should work.

* I've only tested ALSA and Icecast.  ALSA works fine, Icecast
metadata seems to get screwy at times and breaks song
advancement in the playlist at times.

* state file loading works, too (after some last-minute hacks with
non-blocking wakeup functions)

* The non-blocking (*_nb) variants of the task management functions are
probably overused.  They're more lenient and easier to use because
much of our code is still based on our previous polling-based system.

* It currently segfaults on exit.  I haven't paid much attention
to the exit/signal-handling routines other than ensuring it
compiles.  At least the state file seems to work.  We don't
do any cleanups of the threads on exit, yet.

* Update is still done in a child process and not in a thread.
To do this in a thread, we'll need to ensure it does proper
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.

* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:
locking and communication with the main thread; but should
require less memory in the end because we'll be updating
the database "in-place" rather than updating a copy and
then bulk-loading when done.

* We're more sensitive to bugs in 3rd party libraries now.
My plan is to eventually use a master process which forks()
and restarts the child when it dies:

master - just does waitpid() + fork() in a loop
\- main thread
\- decoder thread
\- player thread

At the beginning of every song, the main thread will set
a dirty flag and update the state file.  This way, if we
encounter a song that triggers a segfault killing the
main thread, the master will start the replacement main
on the next song.

* The main thread still wakes up every second on select()
to check for signals; which affects power management.

[merged r7138 from branches/ew]

git-svn-id: https://svn.musicpd.org/mpd/trunk@7240 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Eric Wong 2008-04-12 04:08:00 +00:00
parent d742fa6596
commit 9cf66d0e8a
21 changed files with 215 additions and 380 deletions

View File

@ -30,63 +30,50 @@
#include "utf8.h" #include "utf8.h"
#include "os_compat.h" #include "os_compat.h"
static int decode_pid; static pthread_cond_t decoder_wakeup_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t decoder_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER;
void decodeSigHandler(int sig, siginfo_t * si, void *v) /* called inside decoder_task (inputPlugins) */
void decoder_wakeup_player(void)
{ {
if (sig == SIGCHLD) { wakeup_player_nb();
int status; }
if (decode_pid == wait3(&status, WNOHANG, NULL)) {
/* void decoder_sleep(void)
if (WIFSIGNALED(status)) { {
if (WTERMSIG(status) != SIGTERM) { pthread_cond_wait(&decoder_wakeup_cond, &decoder_wakeup_mutex);
ERROR("decode process died from " wakeup_player_nb();
"signal: %i\n", WTERMSIG(status)); }
}
} static void player_wakeup_decoder_nb(void)
*/ {
decode_pid = 0; pthread_cond_signal(&decoder_wakeup_cond);
getPlayerData()->playerControl.decode_pid = 0; }
}
} else if (sig == SIGTERM) { /* called from player_task */
int pid = decode_pid; static void player_wakeup_decoder(void)
if (pid > 0) { {
/* DEBUG("player (or child) got SIGTERM\n"); */ pthread_cond_signal(&decoder_wakeup_cond);
kill(pid, SIGCONT); player_sleep();
kill(pid, SIGTERM);
} /* else
DEBUG("decoder (or child) got SIGTERM\n"); */
exit(EXIT_SUCCESS);
}
} }
static void stopDecode(DecoderControl * dc) static void stopDecode(DecoderControl * dc)
{ {
if (decode_pid > 0 && (dc->start || dc->state != DECODE_STATE_STOP)) { if (dc->start || dc->state != DECODE_STATE_STOP) {
dc->stop = 1; dc->stop = 1;
kill(decode_pid, SIGCONT); do { player_wakeup_decoder_nb(); } while (dc->stop);
signalNotify(&(getPlayerData()->buffer.notify));
while (decode_pid > 0 && dc->stop)
my_usleep(10000);
} }
} }
static void quitDecode(PlayerControl * pc, DecoderControl * dc) static void quitDecode(PlayerControl * pc, DecoderControl * dc)
{ {
int pid;
stopDecode(dc); stopDecode(dc);
pc->state = PLAYER_STATE_STOP; pc->state = PLAYER_STATE_STOP;
dc->seek = 0; dc->seek = 0;
pc->play = 0; pc->play = 0;
pc->stop = 0; pc->stop = 0;
pc->pause = 0; pc->pause = 0;
wakeup_main_task();
pid = decode_pid;
if (pid > 0)
kill(pid, SIGSTOP);
kill(getppid(), SIGUSR1);
} }
static int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af) static int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af)
@ -111,7 +98,7 @@ static int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af)
#define handleDecodeStart() \ #define handleDecodeStart() \
if(decodeWaitedOn) { \ if(decodeWaitedOn) { \
if(dc->state!=DECODE_STATE_START && decode_pid > 0 && \ if(dc->state!=DECODE_STATE_START && \
dc->error==DECODE_ERROR_NOERROR) \ dc->error==DECODE_ERROR_NOERROR) \
{ \ { \
decodeWaitedOn = 0; \ decodeWaitedOn = 0; \
@ -121,8 +108,9 @@ static int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af)
ERROR("problems opening audio device while playing \"%s\"\n", pc->utf8url); \ ERROR("problems opening audio device while playing \"%s\"\n", pc->utf8url); \
quitDecode(pc,dc); \ quitDecode(pc,dc); \
return; \ return; \
} else if (decode_pid > 0) { \ } else { \
kill(decode_pid, SIGCONT); }\ player_wakeup_decoder(); \
} \
if (pause) { \ if (pause) { \
dropBufferedAudio(); \ dropBufferedAudio(); \
closeAudioDevice(); \ closeAudioDevice(); \
@ -135,14 +123,14 @@ static int calculateCrossFadeChunks(PlayerControl * pc, AudioFormat * af)
cb->audioFormat.channels/ \ cb->audioFormat.channels/ \
cb->audioFormat.sampleRate; \ cb->audioFormat.sampleRate; \
} \ } \
else if(dc->state!=DECODE_STATE_START || decode_pid <= 0) { \ else if(dc->state!=DECODE_STATE_START) { \
pathcpy_trunc(pc->erroredUrl, pc->utf8url); \ pathcpy_trunc(pc->erroredUrl, pc->utf8url); \
pc->error = PLAYER_ERROR_FILE; \ pc->error = PLAYER_ERROR_FILE; \
quitDecode(pc,dc); \ quitDecode(pc,dc); \
return; \ return; \
} \ } \
else { \ else { \
my_usleep(10000); \ player_sleep(); \
continue; \ continue; \
} \ } \
} }
@ -153,8 +141,8 @@ static int waitOnDecode(PlayerControl * pc, DecoderControl * dc,
MpdTag *tag = NULL; MpdTag *tag = NULL;
pathcpy_trunc(pc->currentUrl, pc->utf8url); pathcpy_trunc(pc->currentUrl, pc->utf8url);
while (decode_pid > 0 && dc->start) while (dc->start)
my_usleep(10000); player_wakeup_decoder();
if (dc->start || dc->error != DECODE_ERROR_NOERROR) { if (dc->start || dc->error != DECODE_ERROR_NOERROR) {
pathcpy_trunc(pc->erroredUrl, pc->utf8url); pathcpy_trunc(pc->erroredUrl, pc->utf8url);
@ -183,52 +171,45 @@ static int decodeSeek(PlayerControl * pc, DecoderControl * dc,
{ {
int ret = -1; int ret = -1;
if (decode_pid > 0) { if (dc->state == DECODE_STATE_STOP || dc->error ||
if (dc->state == DECODE_STATE_STOP || dc->error || strcmp(dc->utf8url, pc->utf8url) != 0) {
strcmp(dc->utf8url, pc->utf8url) != 0) { stopDecode(dc);
stopDecode(dc); *next = -1;
*next = -1; cb->begin = 0;
cb->begin = 0; cb->end = 0;
cb->end = 0; dc->error = 0;
dc->error = 0; dc->start = 1;
dc->start = 1; waitOnDecode(pc, dc, cb, decodeWaitedOn);
waitOnDecode(pc, dc, cb, decodeWaitedOn); }
} if (dc->state != DECODE_STATE_STOP && dc->seekable) {
if (decode_pid > 0 && dc->state != DECODE_STATE_STOP && *next = -1;
dc->seekable) { dc->seekWhere = pc->seekWhere > pc->totalTime - 0.1 ?
*next = -1; pc->totalTime - 0.1 : pc->seekWhere;
dc->seekWhere = pc->seekWhere > pc->totalTime - 0.1 ? dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere;
pc->totalTime - 0.1 : pc->seekWhere; dc->seekError = 0;
dc->seekWhere = 0 > dc->seekWhere ? 0 : dc->seekWhere; dc->seek = 1;
dc->seekError = 0; do { player_wakeup_decoder(); } while (dc->seek);
dc->seek = 1; if (!dc->seekError) {
kill(decode_pid, SIGCONT); pc->elapsedTime = dc->seekWhere;
signalNotify(&(getPlayerData()->buffer.notify)); ret = 0;
while (decode_pid > 0 && dc->seek)
my_usleep(10000);
if (!dc->seekError) {
pc->elapsedTime = dc->seekWhere;
ret = 0;
}
} }
} }
pc->seek = 0; pc->seek = 0;
wakeup_main_task();
return ret; return ret;
} }
#define processDecodeInput() \ #define processDecodeInput() \
if(pc->cycleLogFiles) { \
cycle_log_files(); \
pc->cycleLogFiles = 0; \
} \
if(pc->lockQueue) { \ if(pc->lockQueue) { \
pc->queueLockState = PLAYER_QUEUE_LOCKED; \ pc->queueLockState = PLAYER_QUEUE_LOCKED; \
pc->lockQueue = 0; \ pc->lockQueue = 0; \
wakeup_main_task(); \
} \ } \
if(pc->unlockQueue) { \ if(pc->unlockQueue) { \
pc->queueLockState = PLAYER_QUEUE_UNLOCKED; \ pc->queueLockState = PLAYER_QUEUE_UNLOCKED; \
pc->unlockQueue = 0; \ pc->unlockQueue = 0; \
wakeup_main_task(); \
} \ } \
if(pc->pause) { \ if(pc->pause) { \
pause = !pause; \ pause = !pause; \
@ -236,8 +217,6 @@ static int decodeSeek(PlayerControl * pc, DecoderControl * dc,
pc->state = PLAYER_STATE_PAUSE; \ pc->state = PLAYER_STATE_PAUSE; \
} else { \ } else { \
if (openAudioDevice(NULL) >= 0) { \ if (openAudioDevice(NULL) >= 0) { \
if (decode_pid > 0) \
kill(decode_pid, SIGCONT); \
pc->state = PLAYER_STATE_PLAY; \ pc->state = PLAYER_STATE_PLAY; \
} else { \ } else { \
pathcpy_trunc(pc->erroredUrl, pc->utf8url); \ pathcpy_trunc(pc->erroredUrl, pc->utf8url); \
@ -247,7 +226,7 @@ static int decodeSeek(PlayerControl * pc, DecoderControl * dc,
} \ } \
} \ } \
pc->pause = 0; \ pc->pause = 0; \
kill(getppid(), SIGUSR1); \ wakeup_main_task(); \
if (pause == -1) { \ if (pause == -1) { \
pause = 1; \ pause = 1; \
} else if (pause) { \ } else if (pause) { \
@ -303,7 +282,7 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb,
while (!inputStreamAtEOF(&inStream) && bufferInputStream(&inStream) < 0 while (!inputStreamAtEOF(&inStream) && bufferInputStream(&inStream) < 0
&& !dc->stop) { && !dc->stop) {
/* sleep so we don't consume 100% of the cpu */ /* sleep so we don't consume 100% of the cpu */
my_usleep(1000); my_usleep(10000);
} }
/* for http streams, seekable is determined in bufferInputStream */ /* for http streams, seekable is determined in bufferInputStream */
@ -353,8 +332,10 @@ static void decodeStart(PlayerControl * pc, OutputBuffer * cb,
if (plugin == NULL) { if (plugin == NULL) {
/* we already know our mp3Plugin supports streams, no /* we already know our mp3Plugin supports streams, no
* need to check for stream{Types,DecodeFunc} */ * need to check for stream{Types,DecodeFunc} */
if ((plugin = getInputPluginFromName("mp3"))) if ((plugin = getInputPluginFromName("mp3"))) {
ret = plugin->streamDecodeFunc(cb, dc, &inStream); ret = plugin->streamDecodeFunc(cb, dc,
&inStream);
}
} }
} else { } else {
unsigned int next = 0; unsigned int next = 0;
@ -397,51 +378,34 @@ stop_no_close:
dc->stop = 0; dc->stop = 0;
} }
static int decoderInit(PlayerControl * pc, OutputBuffer * cb, static void * decoder_task(mpd_unused void *unused)
DecoderControl * dc)
{ {
int pid; OutputBuffer *cb = &(getPlayerData()->buffer);
PlayerControl *pc = &(getPlayerData()->playerControl);
DecoderControl *dc = &(getPlayerData()->decoderControl);
pid = decode_pid; while (1) {
if (pid > 0) { if (dc->start || dc->seek) {
kill(pid, SIGCONT); decodeStart(pc, cb, dc);
return 0; } else if (dc->stop) {
} dc->state = DECODE_STATE_STOP;
dc->stop = 0;
blockSignals(); decoder_wakeup_player();
getPlayerData()->playerControl.decode_pid = 0; } else {
decode_pid = fork(); decoder_sleep();
if (decode_pid == 0) {
/* CHILD */
unblockSignals();
while (1) {
if (dc->cycleLogFiles) {
cycle_log_files();
dc->cycleLogFiles = 0;
} else if (dc->start || dc->seek)
decodeStart(pc, cb, dc);
else if (dc->stop) {
dc->state = DECODE_STATE_STOP;
dc->stop = 0;
} else
my_usleep(10000);
} }
exit(EXIT_SUCCESS);
/* END OF CHILD */
} else if (decode_pid < 0) {
unblockSignals();
pathcpy_trunc(pc->erroredUrl, pc->utf8url);
pc->error = PLAYER_ERROR_SYSTEM;
return -1;
} }
DEBUG("decoder PID: %d\n", decode_pid); }
getPlayerData()->playerControl.decode_pid = decode_pid;
unblockSignals();
return 0; void decoderInit(void)
{
pthread_attr_t attr;
pthread_t decoder_thread;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (pthread_create(&decoder_thread, &attr, decoder_task, NULL))
FATAL("Failed to spawn decoder task: %s\n", strerror(errno));
} }
static void handleMetadata(OutputBuffer * cb, PlayerControl * pc, int *previous, static void handleMetadata(OutputBuffer * cb, PlayerControl * pc, int *previous,
@ -519,14 +483,13 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
pc->elapsedTime = 0; pc->elapsedTime = 0;
pc->state = PLAYER_STATE_PLAY; pc->state = PLAYER_STATE_PLAY;
pc->play = 0; pc->play = 0;
kill(getppid(), SIGUSR1); wakeup_main_task();
while (decode_pid > 0 && while ((unsigned)cb->end - cb->begin < bbp &&
(unsigned)(cb->end - cb->begin) < bbp &&
cb->end != buffered_chunks - 1 && cb->end != buffered_chunks - 1 &&
dc->state != DECODE_STATE_STOP) { dc->state != DECODE_STATE_STOP) {
processDecodeInput(); processDecodeInput();
my_usleep(1000); player_sleep();
} }
while (!quit) { while (!quit) {
@ -540,7 +503,8 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
next = cb->end; next = cb->end;
dc->start = 1; dc->start = 1;
pc->queueState = PLAYER_QUEUE_DECODE; pc->queueState = PLAYER_QUEUE_DECODE;
kill(getppid(), SIGUSR1); wakeup_main_task();
player_wakeup_decoder_nb();
} }
if (next >= 0 && doCrossFade == 0 && !dc->start && if (next >= 0 && doCrossFade == 0 && !dc->start &&
dc->state != DECODE_STATE_START) { dc->state != DECODE_STATE_START) {
@ -563,11 +527,9 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
race conditions and weirdness */ race conditions and weirdness */
end = cb->end; end = cb->end;
if (pause) { if (pause)
if (decode_pid) player_sleep();
kill(decode_pid, SIGSTOP); else if (cb->begin != end && cb->begin != next) {
kill(getpid(), SIGSTOP);
} else if (cb->begin != end && cb->begin != next) {
if (doCrossFade == 1 && next >= 0 && if (doCrossFade == 1 && next >= 0 &&
((next > cb->begin && ((next > cb->begin &&
(fadePosition = next - cb->begin) (fadePosition = next - cb->begin)
@ -625,7 +587,7 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
cb->begin = 0; cb->begin = 0;
} else } else
cb->begin++; cb->begin++;
signalNotify(&cb->notify); player_wakeup_decoder_nb();
} else if (cb->begin != end && cb->begin == next) { } else if (cb->begin != end && cb->begin == next) {
if (doCrossFade == 1 && nextChunk >= 0) { if (doCrossFade == 1 && nextChunk >= 0) {
nextChunk = cb->begin + crossFadeChunks; nextChunk = cb->begin + crossFadeChunks;
@ -646,7 +608,7 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
while (pc->queueState == PLAYER_QUEUE_DECODE || while (pc->queueState == PLAYER_QUEUE_DECODE ||
pc->queueLockState == PLAYER_QUEUE_LOCKED) { pc->queueLockState == PLAYER_QUEUE_LOCKED) {
processDecodeInput(); processDecodeInput();
my_usleep(10000); player_sleep();
} }
if (pc->queueState != PLAYER_QUEUE_PLAY) { if (pc->queueState != PLAYER_QUEUE_PLAY) {
quit = 1; quit = 1;
@ -661,10 +623,9 @@ static void decodeParent(PlayerControl * pc, DecoderControl * dc, OutputBuffer *
doCrossFade = 0; doCrossFade = 0;
crossFadeChunks = 0; crossFadeChunks = 0;
pc->queueState = PLAYER_QUEUE_EMPTY; pc->queueState = PLAYER_QUEUE_EMPTY;
kill(getppid(), SIGUSR1); wakeup_main_task();
} }
} else if (decode_pid <= 0 || } else if (dc->state == DECODE_STATE_STOP && !dc->start) {
(dc->state == DECODE_STATE_STOP && !dc->start)) {
quit = 1; quit = 1;
break; break;
} else { } else {
@ -699,9 +660,7 @@ void decode(void)
dc->seek = 0; dc->seek = 0;
dc->stop = 0; dc->stop = 0;
dc->start = 1; dc->start = 1;
do { player_wakeup_decoder(); } while (dc->start);
if (decoderInit(pc, cb, dc) < 0)
return;
decodeParent(pc, dc, cb); decodeParent(pc, dc, cb);
} }

View File

@ -47,15 +47,18 @@ typedef struct _DecoderControl {
volatile mpd_sint8 seek; volatile mpd_sint8 seek;
volatile mpd_sint8 seekError; volatile mpd_sint8 seekError;
volatile mpd_sint8 seekable; volatile mpd_sint8 seekable;
volatile mpd_sint8 cycleLogFiles;
volatile double seekWhere; volatile double seekWhere;
AudioFormat audioFormat; AudioFormat audioFormat;
char utf8url[MPD_PATH_MAX]; char utf8url[MPD_PATH_MAX];
volatile float totalTime; volatile float totalTime;
} DecoderControl; } DecoderControl;
void decodeSigHandler(int sig, siginfo_t * siginfo, void *v);
void decode(void); void decode(void);
void decoder_wakeup_player(void);
void decoder_sleep(void);
void decoderInit(void);
#endif #endif

View File

@ -175,7 +175,6 @@ int updateInit(int fd, List * pathList)
if (directory_updatePid == 0) { if (directory_updatePid == 0) {
/* child */ /* child */
int dbUpdated = 0; int dbUpdated = 0;
clearPlayerPid();
unblockSignals(); unblockSignals();

View File

@ -400,6 +400,7 @@ static int aac_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
if (dc->seek) { if (dc->seek) {
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} else if (dc->stop) { } else if (dc->stop) {
eof = 1; eof = 1;
break; break;
@ -418,6 +419,7 @@ static int aac_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
if (dc->seek) { if (dc->seek) {
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
return 0; return 0;

View File

@ -102,6 +102,7 @@ static int audiofile_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
dc->audioFormat.sampleRate; dc->audioFormat.sampleRate;
afSeekFrame(af_fp, AF_DEFAULT_TRACK, current); afSeekFrame(af_fp, AF_DEFAULT_TRACK, current);
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
ret = ret =

View File

@ -394,6 +394,7 @@ static int flac_decode_internal(OutputBuffer * cb, DecoderControl * dc,
} else } else
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
} }
if (!dc->stop) { if (!dc->stop) {

View File

@ -194,6 +194,7 @@ static int mod_decode(OutputBuffer * cb, DecoderControl * dc, char *path)
if (dc->seek) { if (dc->seek) {
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
if (dc->stop) if (dc->stop)

View File

@ -857,6 +857,7 @@ static int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc,
clearOutputBuffer(cb); clearOutputBuffer(cb);
data->muteFrame = 0; data->muteFrame = 0;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
break; break;
default: default:
@ -972,10 +973,12 @@ static int mp3Read(mp3DecodeData * data, OutputBuffer * cb, DecoderControl * dc,
dc->seekError = 1; dc->seekError = 1;
data->muteFrame = 0; data->muteFrame = 0;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
} else if (dc->seek && !data->inStream->seekable) { } else if (dc->seek && !data->inStream->seekable) {
dc->seek = 0; dc->seek = 0;
dc->seekError = 1; dc->seekError = 1;
decoder_wakeup_player();
} }
} }
@ -1082,6 +1085,7 @@ static int mp3_decode(OutputBuffer * cb, DecoderControl * dc,
if (dc->seek && data.muteFrame == MUTEFRAME_SEEK) { if (dc->seek && data.muteFrame == MUTEFRAME_SEEK) {
clearOutputBuffer(cb); clearOutputBuffer(cb);
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
flushOutputBuffer(cb); flushOutputBuffer(cb);

View File

@ -221,6 +221,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc,
clearOutputBuffer(cb); clearOutputBuffer(cb);
seeking = 0; seeking = 0;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
if (seeking) if (seeking)
@ -296,6 +297,7 @@ static int mp4_decode(OutputBuffer * cb, DecoderControl * dc,
if (dc->seek && seeking) { if (dc->seek && seeking) {
clearOutputBuffer(cb); clearOutputBuffer(cb);
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
flushOutputBuffer(cb); flushOutputBuffer(cb);

View File

@ -194,6 +194,7 @@ static int mpc_decode(OutputBuffer * cb, DecoderControl * dc,
} else } else
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
vbrUpdateAcc = 0; vbrUpdateAcc = 0;

View File

@ -370,6 +370,7 @@ static int oggflac_decode(OutputBuffer * cb, DecoderControl * dc,
} else } else
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
} }

View File

@ -285,6 +285,7 @@ static int oggvorbis_decode(OutputBuffer * cb, DecoderControl * dc,
} else } else
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
ret = ov_read(&vf, chunk + chunkpos, ret = ov_read(&vf, chunk + chunkpos,
OGG_CHUNK_SIZE - chunkpos, OGG_CHUNK_SIZE - chunkpos,

View File

@ -192,6 +192,7 @@ static void wavpack_decode(OutputBuffer *cb, DecoderControl *dc,
} }
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
if (dc->stop) if (dc->stop)

View File

@ -429,6 +429,8 @@ int main(int argc, char *argv[])
initZeroconf(); initZeroconf();
openVolumeDevice(); openVolumeDevice();
decoderInit();
playerInit();
read_state_file(); read_state_file();
while (COMMAND_RETURN_KILL != doIOForInterfaces() && while (COMMAND_RETURN_KILL != doIOForInterfaces() &&

View File

@ -45,8 +45,6 @@ void initOutputBuffer(OutputBuffer * cb, char *chunks)
(float *)(((char *)cb->metaChunk) + (float *)(((char *)cb->metaChunk) +
buffered_chunks * sizeof(mpd_sint8)); buffered_chunks * sizeof(mpd_sint8));
cb->acceptMetadata = 0; cb->acceptMetadata = 0;
initNotify(&cb->notify);
} }
void clearAllMetaChunkSets(OutputBuffer * cb) void clearAllMetaChunkSets(OutputBuffer * cb)
@ -129,11 +127,12 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
} else { } else {
dc->seekError = 1; dc->seekError = 1;
dc->seek = 0; dc->seek = 0;
decoder_wakeup_player();
} }
} }
if (!inStream || if (!inStream ||
bufferInputStream(inStream) <= 0) { bufferInputStream(inStream) <= 0) {
waitNotify(&cb->notify); decoder_sleep();
} }
} }
if (dc->stop) if (dc->stop)
@ -163,6 +162,7 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
flushOutputBuffer(cb); flushOutputBuffer(cb);
} }
} }
decoder_wakeup_player();
return 0; return 0;
} }

View File

@ -26,7 +26,6 @@
#include "inputStream.h" #include "inputStream.h"
#include "metadataChunk.h" #include "metadataChunk.h"
#include "replayGain.h" #include "replayGain.h"
#include "notify.h"
#define OUTPUT_BUFFER_DC_STOP -1 #define OUTPUT_BUFFER_DC_STOP -1
#define OUTPUT_BUFFER_DC_SEEK -2 #define OUTPUT_BUFFER_DC_SEEK -2
@ -46,7 +45,6 @@ typedef struct _OutputBuffer {
mpd_sint8 metaChunkSet[BUFFERED_METACHUNKS]; mpd_sint8 metaChunkSet[BUFFERED_METACHUNKS];
mpd_sint8 *volatile metaChunk; mpd_sint8 *volatile metaChunk;
volatile mpd_sint8 acceptMetadata; volatile mpd_sint8 acceptMetadata;
Notify notify;
} OutputBuffer; } OutputBuffer;
void initOutputBuffer(OutputBuffer * cb, char *chunks); void initOutputBuffer(OutputBuffer * cb, char *chunks);

View File

@ -33,13 +33,66 @@
#include "sig_handlers.h" #include "sig_handlers.h"
#include "os_compat.h" #include "os_compat.h"
static pthread_cond_t player_wakeup = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t player_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t main_wakeup = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t main_wakeup_mutex = PTHREAD_MUTEX_INITIALIZER;
static void playerCloseAudio(void); static void playerCloseAudio(void);
volatile int player_pid = 0; void wakeup_player_nb(void)
void clearPlayerPid(void)
{ {
player_pid = 0; pthread_cond_signal(&player_wakeup);
}
static void wakeup_player(void)
{
pthread_cond_signal(&player_wakeup);
pthread_cond_wait(&main_wakeup, &main_wakeup_mutex);
}
void wakeup_main_task(void)
{
pthread_cond_signal(&main_wakeup);
}
void player_sleep(void)
{
pthread_cond_wait(&player_wakeup, &player_wakeup_mutex);
}
static void * player_task(mpd_unused void *unused)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
while (1) {
if (pc->play) {
decode();
continue; /* decode() calls wakeup_main_task */
} else if (pc->stop) {
pc->stop = 0;
} else if (pc->seek) {
pc->seek = 0;
} else if (pc->pause) {
pc->pause = 0;
} else if (pc->closeAudio) {
closeAudioDevice();
pc->closeAudio = 0;
} else if (pc->lockQueue) {
pc->queueLockState = PLAYER_QUEUE_LOCKED;
pc->lockQueue = 0;
} else if (pc->unlockQueue) {
pc->queueLockState = PLAYER_QUEUE_UNLOCKED;
pc->unlockQueue = 0;
} else {
player_sleep();
continue;
}
/* we did something, tell the main task about it */
wakeup_main_task();
}
return NULL;
} }
static void resetPlayerMetadata(void) static void resetPlayerMetadata(void)
@ -51,149 +104,24 @@ static void resetPlayerMetadata(void)
} }
} }
static void resetPlayer(void) void playerInit(void)
{ {
int pid; pthread_attr_t attr;
pthread_t player_thread;
clearPlayerPid(); pthread_attr_init(&attr);
getPlayerData()->playerControl.stop = 0; pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
getPlayerData()->playerControl.play = 0; if (pthread_create(&player_thread, &attr, player_task, NULL))
getPlayerData()->playerControl.pause = 0; FATAL("Failed to spawn player task: %s\n", strerror(errno));
getPlayerData()->playerControl.lockQueue = 0;
getPlayerData()->playerControl.unlockQueue = 0;
getPlayerData()->playerControl.state = PLAYER_STATE_STOP;
getPlayerData()->playerControl.queueState = PLAYER_QUEUE_UNLOCKED;
getPlayerData()->playerControl.seek = 0;
getPlayerData()->playerControl.metadataState =
PLAYER_METADATA_STATE_WRITE;
pid = getPlayerData()->playerControl.decode_pid;
if (pid > 0)
kill(pid, SIGTERM);
getPlayerData()->playerControl.decode_pid = 0;
}
void player_sigChldHandler(int pid, int status)
{
if (player_pid == pid)
{
/*
DEBUG("SIGCHLD caused by player process\n");
if (WIFSIGNALED(status) &&
WTERMSIG(status) != SIGTERM &&
WTERMSIG(status) != SIGINT)
{
ERROR("player process died from signal: %i\n",
WTERMSIG(status));
}
*/
resetPlayer();
}
else if (pid == getPlayerData()->playerControl.decode_pid &&
player_pid <= 0)
{
/*
if (WIFSIGNALED(status) && WTERMSIG(status) != SIGTERM)
{
ERROR("(caught by master parent) "
"decode process died from a "
"non-TERM signal: %i\n", WTERMSIG(status));
}
*/
getPlayerData()->playerControl.decode_pid = 0;
}
}
static int playerInit(void)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
int pid;
pid = player_pid;
if (pid > 0) {
kill(pid, SIGCONT);
pc->wait = 0;
return 0;
}
blockSignals();
player_pid = fork();
if (player_pid==0)
{
clock_t start = clock();
unblockSignals();
setSigHandlersForDecoder();
closeAllListenSockets();
freeAllInterfaces();
finishPlaylist();
closeMp3Directory();
finishPermissions();
finishCommands();
finishVolume();
DEBUG("took %f to init player\n",
(float)(clock()-start)/CLOCKS_PER_SEC);
while (1) {
if (pc->play)
decode();
else if (pc->stop)
pc->stop = 0;
else if (pc->seek)
pc->seek = 0;
else if (pc->pause)
pc->pause = 0;
else if (pc->closeAudio) {
closeAudioDevice();
pc->closeAudio = 0;
kill(getppid(), SIGUSR1);
} else if (pc->lockQueue) {
pc->queueLockState = PLAYER_QUEUE_LOCKED;
pc->lockQueue = 0;
} else if (pc->unlockQueue) {
pc->queueLockState = PLAYER_QUEUE_UNLOCKED;
pc->unlockQueue = 0;
} else if (pc->cycleLogFiles) {
cycle_log_files();
pc->cycleLogFiles = 0;
} else
my_usleep(10000);
}
}
else if (player_pid < 0)
{
unblockSignals();
ERROR("player Problems fork()'ing\n");
player_pid = 0;
return -1;
}
unblockSignals();
return 0;
} }
int playerWait(int fd) int playerWait(int fd)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl);
int pid;
if (pc->wait)
return 0;
if (playerStop(fd) < 0) if (playerStop(fd) < 0)
return -1; return -1;
playerCloseAudio(); playerCloseAudio();
pid = player_pid;
if (pid > 0) {
pc->wait = 1;
kill(pid, SIGSTOP);
}
return 0; return 0;
} }
@ -215,17 +143,10 @@ int playerPlay(int fd, Song * song)
set_current_song(song); set_current_song(song);
pc->play = 1;
if (playerInit() < 0) {
pc->play = 0;
return -1;
}
resetPlayerMetadata(); resetPlayerMetadata();
if (player_pid > 0 && pc->state == PLAYER_STATE_PAUSE) pc->play = 1;
kill(player_pid, SIGCONT); /* FIXME: _nb() variant is probably wrong here, and everywhere... */
while (player_pid > 0 && pc->play) do { wakeup_player_nb(); } while (pc->play);
my_usleep(1000);
return 0; return 0;
} }
@ -234,12 +155,9 @@ int playerStop(int fd)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid > 0 && pc->state != PLAYER_STATE_STOP) { if (pc->state != PLAYER_STATE_STOP) {
pc->stop = 1; pc->stop = 1;
if (pc->state == PLAYER_STATE_PAUSE) do { wakeup_player(); } while (pc->stop);
kill(player_pid, SIGCONT);
while (player_pid > 0 && pc->stop)
my_usleep(1000);
} }
pc->queueState = PLAYER_QUEUE_BLANK; pc->queueState = PLAYER_QUEUE_BLANK;
@ -248,27 +166,18 @@ int playerStop(int fd)
return 0; return 0;
} }
void playerKill(void) void playerKill(void) /* deprecated */
{ {
int pid; playerPause(STDERR_FILENO);
pid = player_pid;
if (pid > 0) {
kill(pid, SIGCONT);
kill(pid, SIGTERM);
}
} }
int playerPause(int fd) int playerPause(int fd)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid > 0 && pc->state != PLAYER_STATE_STOP) { if (pc->state != PLAYER_STATE_STOP) {
pc->pause = 1; pc->pause = 1;
if (player_pid > 0 && pc->state == PLAYER_STATE_PAUSE) do { wakeup_player(); } while (pc->pause);
kill(player_pid, SIGCONT);
while (player_pid > 0 && pc->pause)
my_usleep(1000);
} }
return 0; return 0;
@ -278,9 +187,6 @@ int playerSetPause(int fd, int pause_flag)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid <= 0)
return 0;
switch (pc->state) { switch (pc->state) {
case PLAYER_STATE_PLAY: case PLAYER_STATE_PLAY:
if (pause_flag) if (pause_flag)
@ -370,15 +276,10 @@ static void playerCloseAudio(void)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid > 0) { if (playerStop(STDERR_FILENO) < 0)
if (playerStop(STDERR_FILENO) < 0) return;
return; pc->closeAudio = 1;
pc->closeAudio = 1; do { wakeup_player(); } while (pc->closeAudio);
if (pc->state == PLAYER_STATE_PAUSE)
kill(player_pid, SIGCONT);
while (player_pid > 0 && pc->closeAudio)
my_usleep(1000);
}
} }
int queueSong(Song * song) int queueSong(Song * song)
@ -412,12 +313,9 @@ void playerQueueLock(void)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid > 0 && pc->queueLockState == PLAYER_QUEUE_UNLOCKED) { if (pc->queueLockState == PLAYER_QUEUE_UNLOCKED) {
if (pc->state == PLAYER_STATE_PAUSE)
kill(player_pid, SIGCONT);
pc->lockQueue = 1; pc->lockQueue = 1;
while (player_pid > 0 && pc->lockQueue) do { wakeup_player(); } while (pc->lockQueue);
my_usleep(1000);
} }
} }
@ -425,12 +323,9 @@ void playerQueueUnlock(void)
{ {
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (player_pid > 0 && pc->queueLockState == PLAYER_QUEUE_LOCKED) { if (pc->queueLockState == PLAYER_QUEUE_LOCKED) {
if (pc->state == PLAYER_STATE_PAUSE)
kill(player_pid, SIGCONT);
pc->unlockQueue = 1; pc->unlockQueue = 1;
while (player_pid > 0 && pc->unlockQueue) do { wakeup_player(); } while (pc->unlockQueue);
my_usleep(1000);
} }
} }
@ -454,10 +349,8 @@ int playerSeek(int fd, Song * song, float seek_time)
resetPlayerMetadata(); resetPlayerMetadata();
pc->seekWhere = seek_time; pc->seekWhere = seek_time;
pc->seek = 1; pc->seek = 1;
if (player_pid > 0 && pc->state == PLAYER_STATE_PAUSE) /* FIXME: _nb() is probably wrong here, too */
kill(player_pid, SIGCONT); do { wakeup_player_nb(); } while (pc->seek);
while (player_pid > 0 && pc->seek)
my_usleep(1000);
} }
return 0; return 0;
@ -519,15 +412,6 @@ int getPlayerChannels(void)
return pc->channels; return pc->channels;
} }
void playerCycleLogFiles(void)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
DecoderControl *dc = &(getPlayerData()->decoderControl);
pc->cycleLogFiles = 1;
dc->cycleLogFiles = 1;
}
/* this actually creates a dupe of the current metadata */ /* this actually creates a dupe of the current metadata */
Song *playerCurrentDecodeSong(void) Song *playerCurrentDecodeSong(void)
{ {
@ -537,7 +421,6 @@ Song *playerCurrentDecodeSong(void)
PlayerControl *pc = &(getPlayerData()->playerControl); PlayerControl *pc = &(getPlayerData()->playerControl);
if (pc->metadataState == PLAYER_METADATA_STATE_READ) { if (pc->metadataState == PLAYER_METADATA_STATE_READ) {
DEBUG("playerCurrentDecodeSong: caught new metadata!\n");
if (prev) if (prev)
free(prev); free(prev);
prev = xmalloc(sizeof(MetadataChunk)); prev = xmalloc(sizeof(MetadataChunk));

View File

@ -55,7 +55,6 @@
#define PLAYER_METADATA_STATE_WRITE 2 #define PLAYER_METADATA_STATE_WRITE 2
typedef struct _PlayerControl { typedef struct _PlayerControl {
volatile mpd_sint8 wait;
volatile mpd_sint8 stop; volatile mpd_sint8 stop;
volatile mpd_sint8 play; volatile mpd_sint8 play;
volatile mpd_sint8 pause; volatile mpd_sint8 pause;
@ -81,16 +80,16 @@ typedef struct _PlayerControl {
volatile float crossFade; volatile float crossFade;
volatile mpd_uint16 softwareVolume; volatile mpd_uint16 softwareVolume;
volatile double totalPlayTime; volatile double totalPlayTime;
volatile int decode_pid;
volatile mpd_sint8 cycleLogFiles;
volatile mpd_sint8 metadataState; volatile mpd_sint8 metadataState;
MetadataChunk metadataChunk; MetadataChunk metadataChunk;
MetadataChunk fileMetadataChunk; MetadataChunk fileMetadataChunk;
} PlayerControl; } PlayerControl;
void clearPlayerPid(void); void wakeup_main_task(void);
void player_sigChldHandler(int pid, int status); void wakeup_player_nb(void);
void player_sleep(void);
int playerPlay(int fd, Song * song); int playerPlay(int fd, Song * song);
@ -144,8 +143,8 @@ int getPlayerBits(void);
int getPlayerChannels(void); int getPlayerChannels(void);
void playerCycleLogFiles(void);
Song *playerCurrentDecodeSong(void); Song *playerCurrentDecodeSong(void);
void playerInit(void);
#endif #endif

View File

@ -97,7 +97,6 @@ void initPlayerData(void)
initOutputBuffer(&(playerData_pd->buffer), initOutputBuffer(&(playerData_pd->buffer),
((char *)playerData_pd) + sizeof(PlayerData)); ((char *)playerData_pd) + sizeof(PlayerData));
playerData_pd->playerControl.wait = 0;
playerData_pd->playerControl.stop = 0; playerData_pd->playerControl.stop = 0;
playerData_pd->playerControl.pause = 0; playerData_pd->playerControl.pause = 0;
playerData_pd->playerControl.play = 0; playerData_pd->playerControl.play = 0;
@ -115,7 +114,6 @@ void initPlayerData(void)
playerData_pd->playerControl.crossFade = crossfade; playerData_pd->playerControl.crossFade = crossfade;
playerData_pd->playerControl.softwareVolume = 1000; playerData_pd->playerControl.softwareVolume = 1000;
playerData_pd->playerControl.totalPlayTime = 0; playerData_pd->playerControl.totalPlayTime = 0;
playerData_pd->playerControl.decode_pid = 0;
playerData_pd->playerControl.metadataState = playerData_pd->playerControl.metadataState =
PLAYER_METADATA_STATE_WRITE; PLAYER_METADATA_STATE_WRITE;

View File

@ -45,7 +45,6 @@ int handlePendingSignals(void)
} }
if (cycle_log_files() < 0) if (cycle_log_files() < 0)
return COMMAND_RETURN_KILL; return COMMAND_RETURN_KILL;
playerCycleLogFiles();
} }
return 0; return 0;
@ -63,7 +62,6 @@ static void chldSigHandler(int sig)
else else
break; break;
} }
player_sigChldHandler(pid, status);
directory_sigChldHandler(pid, status); directory_sigChldHandler(pid, status);
} }
} }
@ -92,23 +90,6 @@ void finishSigHandlers(void)
signal_unhandle(SIGHUP); signal_unhandle(SIGHUP);
} }
void setSigHandlersForDecoder(void)
{
struct sigaction sa;
finishSigHandlers();
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_IGN;
while (sigaction(SIGHUP, &sa, NULL) < 0 && errno == EINTR) ;
while (sigaction(SIGINT, &sa, NULL) < 0 && errno == EINTR) ;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = decodeSigHandler;
while (sigaction(SIGCHLD, &sa, NULL) < 0 && errno == EINTR) ;
while (sigaction(SIGTERM, &sa, NULL) < 0 && errno == EINTR) ;
}
void ignoreSignals(void) void ignoreSignals(void)
{ {
struct sigaction sa; struct sigaction sa;

View File

@ -27,8 +27,6 @@ void initSigHandlers(void);
void finishSigHandlers(void); void finishSigHandlers(void);
void setSigHandlersForDecoder(void);
void ignoreSignals(void); void ignoreSignals(void);
void blockSignals(void); void blockSignals(void);