From 96f659446cf0d0cf5c6091449fd9a70c62e516d8 Mon Sep 17 00:00:00 2001 From: Torben Hohn Date: Tue, 31 Mar 2009 17:56:02 +0200 Subject: [PATCH] be a bit more careful to not trigger race conditions in flash --- flashsupport.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/flashsupport.c b/flashsupport.c index a88bf1c..fe0be51 100644 --- a/flashsupport.c +++ b/flashsupport.c @@ -193,6 +193,7 @@ extern "C" { #ifdef JACK #include #include +#include #include #include #endif @@ -1082,11 +1083,16 @@ static void *jack_flash_thread( void *arg ) size_t bufsize = 2*flash_frames * sizeof( int16_t ); int16_t *buffer = alloca( bufsize ); + sem_wait( &(p->semaphore) ); + usleep( 50 ); + while( !p->quit ) { sem_wait( &(p->semaphore) ); FPI_SoundOutput_FillBuffer(p, (char*) buffer, bufsize); - jack_ringbuffer_write( p->buffer, (char*)buffer, bufsize ); + if( jack_ringbuffer_write( p->buffer, (char*)buffer, bufsize ) != bufsize ) + printf( "something is wrong\n" ); } + return NULL; } @@ -1113,7 +1119,6 @@ static int jack_process_cb( jack_nframes_t nframes, void *arg ) return 0; } - //FPI_SoundOutput_FillBuffer(p, (char*) buffer, bufsize); if( jack_ringbuffer_read( p->buffer, (char *)buffer, bufsize ) != bufsize ) { printf( "Something is pretty wrong :( \n" ); return 0; @@ -1159,6 +1164,7 @@ static void *FPX_SoundOutput_Open(void) { int jack_rate; int flash_frames; size_t bufsize; + int i; /* Unfortunately we cannot report any useful error string back to * Flash. It would be highly preferable if Flash supported some @@ -1182,10 +1188,21 @@ static void *FPX_SoundOutput_Open(void) { flash_frames = jack_get_buffer_size( p->client ) * 44100 / jack_rate; bufsize = 2*flash_frames * sizeof( int16_t ); - p->buffer = jack_ringbuffer_create( bufsize * 2 ); - sem_init( &(p->semaphore), 0, 2 ); + p->buffer = jack_ringbuffer_create( bufsize * 6 ); + sem_init( &(p->semaphore), 0, 0 ); pthread_create( &(p->tid), NULL, jack_flash_thread, p ); +#if 0 +// This seems to trigger a race condition in flash. + + jack_client_create_thread( p->client, + &(p->tid), + (jack_client_real_time_priority( p->client ) == -1) ? 1 : (jack_client_real_time_priority( p->client ) - 1), + (jack_client_real_time_priority( p->client ) == -1) ? 0 : 1, + jack_flash_thread, + p ); +#endif + /* Second, initialize the connection context */ if (!(p->port_l = jack_port_register( p->client, "out1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) ) @@ -1201,6 +1218,13 @@ static void *FPX_SoundOutput_Open(void) { snprintf( namebuf, 99, "%s:out2", jack_get_client_name( p->client ) ); jack_connect( p->client, namebuf, "system:playback_2" ); + // Now start the fill thread. + // the semaphore represents the number of writeable buffers in + // the ringbuffer. we add 1 for the initial sem_wait. + + for(i=0; i<( 5 + 1 ); i++ ) + sem_post( &(p->semaphore) ); + return p; fail: -- 2.11.4.GIT