Thread init/exit moved into own functions.

This commit is contained in:
Johns 2011-12-11 19:42:22 +01:00
parent 749b0d6721
commit 24132171f6

61
audio.c
View File

@ -351,7 +351,6 @@ static void *AudioPlayHandlerThread(void *dummy)
int err; int err;
Debug(3, "audio: play thread started\n"); Debug(3, "audio: play thread started\n");
for (;;) { for (;;) {
Debug(3, "audio: wait on start condition\n"); Debug(3, "audio: wait on start condition\n");
pthread_mutex_lock(&AudioMutex); pthread_mutex_lock(&AudioMutex);
@ -437,6 +436,37 @@ void AudioEnqueue(const void *samples, int count)
} }
} }
/**
** Initialize audio thread.
*/
static void AudioInitThread(void)
{
pthread_mutex_init(&AudioMutex, NULL);
pthread_cond_init(&AudioStartCond, NULL);
pthread_create(&AudioThread, NULL, AudioPlayHandlerThread, NULL);
//pthread_detach(AudioThread);
do {
pthread_yield();
} while (!AlsaPCMHandle);
}
/**
** Cleanup audio thread.
*/
static void AudioExitThread(void)
{
void *retval;
if (pthread_cancel(AudioThread)) {
Error(_("audio: can't queue cancel alsa play thread\n"));
}
if (pthread_join(AudioThread, &retval) || retval != PTHREAD_CANCELED) {
Error(_("audio: can't cancel alsa play thread\n"));
}
pthread_cond_destroy(&AudioStartCond);
pthread_mutex_destroy(&AudioMutex);
}
#endif #endif
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -531,10 +561,6 @@ static void AlsaInitPCM(void)
snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size); snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
Info(_("audio/alsa: max buffer size %lu\n"), buffer_size); Info(_("audio/alsa: max buffer size %lu\n"), buffer_size);
pthread_mutex_init(&AudioMutex, NULL);
pthread_cond_init(&AudioStartCond, NULL);
pthread_create(&AudioThread, NULL, AudioPlayHandlerThread, NULL);
//pthread_detach(AudioThread);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -647,6 +673,9 @@ uint64_t AudioGetDelay(void)
snd_pcm_sframes_t delay; snd_pcm_sframes_t delay;
uint64_t pts; uint64_t pts;
if (!AlsaPCMHandle) {
return 0;
}
// delay in frames in alsa + kernel buffers // delay in frames in alsa + kernel buffers
if ((err = snd_pcm_delay(AlsaPCMHandle, &delay)) < 0) { if ((err = snd_pcm_delay(AlsaPCMHandle, &delay)) < 0) {
//Debug(3, "audio/alsa: no hw delay\n"); //Debug(3, "audio/alsa: no hw delay\n");
@ -887,6 +916,9 @@ void AudioInit(void)
if (AudioSetup(&freq, &chan)) { // set default parameters if (AudioSetup(&freq, &chan)) { // set default parameters
Error(_("audio: can't do initial setup\n")); Error(_("audio: can't do initial setup\n"));
} }
#ifdef USE_AUDIO_THREAD
AudioInitThread();
#endif
AudioPaused = 1; AudioPaused = 1;
} }
@ -896,17 +928,9 @@ void AudioInit(void)
*/ */
void AudioExit(void) void AudioExit(void)
{ {
void *retval; #ifdef USE_AUDIO_THREAD
AudioExitThread();
if (pthread_cancel(AudioThread)) { #endif
Error(_("audio: can't queue cancel alsa play thread\n"));
}
if (pthread_join(AudioThread, &retval) || retval != PTHREAD_CANCELED) {
Error(_("audio: can't cancel alsa play thread\n"));
}
pthread_cond_destroy(&AudioStartCond);
pthread_mutex_destroy(&AudioMutex);
if (AlsaPCMHandle) { if (AlsaPCMHandle) {
snd_pcm_close(AlsaPCMHandle); snd_pcm_close(AlsaPCMHandle);
AlsaPCMHandle = NULL; AlsaPCMHandle = NULL;
@ -931,17 +955,20 @@ void AudioTest(void)
for (;;) { for (;;) {
unsigned u; unsigned u;
uint8_t buffer[16 * 1024]; // some random data uint8_t buffer[16 * 1024]; // some random data
int i;
for (u = 0; u < sizeof(buffer); u++) { for (u = 0; u < sizeof(buffer); u++) {
buffer[u] = random() & 0xffff; buffer[u] = random() & 0xffff;
} }
Debug(3, "audio/test: loop\n"); Debug(3, "audio/test: loop\n");
for (;;) { for (i = 0; i < 100; ++i) {
while (RingBufferFreeBytes(AlsaRingBuffer) > sizeof(buffer)) { while (RingBufferFreeBytes(AlsaRingBuffer) > sizeof(buffer)) {
AudioEnqueue(buffer, sizeof(buffer)); AudioEnqueue(buffer, sizeof(buffer));
} }
usleep(20 * 1000);
} }
break;
} }
} }