From cfcca8681b88a171fb2cdbb83daa5f22bbedb6b8 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 11 Jul 2020 16:40:51 -0700 Subject: *: get libjack synchronization on the same page This isn't tested as I don't currently have a JACK setup, but it at least compiles and looks semi sane. --- recordmydesktop/src/rmd_cache_audio.c | 38 +++++++++++++-------------- recordmydesktop/src/rmd_encode_sound_buffer.c | 35 ++++++++++++------------ recordmydesktop/src/rmd_initialize_data.c | 3 +-- recordmydesktop/src/rmd_jack.c | 28 ++++++++------------ recordmydesktop/src/rmd_types.h | 3 +-- 5 files changed, 49 insertions(+), 58 deletions(-) (limited to 'recordmydesktop') diff --git a/recordmydesktop/src/rmd_cache_audio.c b/recordmydesktop/src/rmd_cache_audio.c index 5a71050..fabcb39 100644 --- a/recordmydesktop/src/rmd_cache_audio.c +++ b/recordmydesktop/src/rmd_cache_audio.c @@ -42,8 +42,12 @@ void *rmdCacheSoundBuffer(ProgData *pdata) { size_t write_size = pdata->periodsize * pdata->sound_framesize; #ifdef HAVE_LIBJACK - if (pdata->args.use_jack) + void *jackbuf = NULL; + + if (pdata->args.use_jack) { write_size = pdata->sound_framesize * pdata->jdata->buffersize; + jackbuf = malloc(pdata->sound_framesize * pdata->jdata->buffersize); + } #endif //We are simply going to throw sound on the disk. @@ -51,12 +55,6 @@ void *rmdCacheSoundBuffer(ProgData *pdata) { //compressing would reducethe overall size by only an //insignificant fraction. -#ifdef HAVE_LIBJACK - void *jackbuf = NULL; - if (pdata->args.use_jack) - jackbuf = malloc(pdata->sound_framesize * pdata->jdata->buffersize); -#endif - while (pdata->running) { SndBuffer *buff = NULL; @@ -83,19 +81,19 @@ void *rmdCacheSoundBuffer(ProgData *pdata) { free(buff); } else { #ifdef HAVE_LIBJACK - if ((*jack_ringbuffer_read_space)(pdata->jdata->sound_buffer) >= write_size) { - (*jack_ringbuffer_read)(pdata->jdata->sound_buffer, jackbuf, write_size); - fwrite(jackbuf, 1, write_size, pdata->cache_data->afp); - } else { - /* FIXME TODO this needs redoing... */ - //pdata->v_enc_thread_waiting=1; - pthread_mutex_lock(&pdata->snd_buff_ready_mutex); - pthread_cond_wait(&pdata->sound_data_read, &pdata->snd_buff_ready_mutex); - pthread_mutex_unlock(&pdata->snd_buff_ready_mutex); - //pdata->v_enc_thread_waiting=0; - - continue; - } + pthread_mutex_lock(&pdata->sound_buffer_mutex); + while ( pdata->running && + jack_ringbuffer_read_space(pdata->jdata->sound_buffer) < write_size) + pthread_cond_wait(&pdata->sound_data_read, &pdata->sound_buffer_mutex); + + if (pdata->running) + jack_ringbuffer_read(pdata->jdata->sound_buffer, jackbuf, write_size); + pthread_mutex_unlock(&pdata->sound_buffer_mutex); + + if (!pdata->running) + break; + + fwrite(jackbuf, 1, write_size, pdata->cache_data->afp); #endif } pdata->avd -= pdata->periodtime; diff --git a/recordmydesktop/src/rmd_encode_sound_buffer.c b/recordmydesktop/src/rmd_encode_sound_buffer.c index d9afee7..5fa1dc6 100644 --- a/recordmydesktop/src/rmd_encode_sound_buffer.c +++ b/recordmydesktop/src/rmd_encode_sound_buffer.c @@ -82,31 +82,32 @@ void *rmdEncodeSoundBuffer(ProgData *pdata) { free(buff); } else { #ifdef HAVE_LIBJACK - if ( (*jack_ringbuffer_read_space)(pdata->jdata->sound_buffer) >= - (pdata->sound_framesize * pdata->jdata->buffersize)) { - (*jack_ringbuffer_read)( pdata->jdata->sound_buffer, + pthread_mutex_lock(&pdata->sound_buffer_mutex); + while ( pdata->running && + jack_ringbuffer_read_space(pdata->jdata->sound_buffer) < + pdata->sound_framesize * pdata->jdata->buffersize) + pthread_cond_wait(&pdata->sound_data_read, &pdata->sound_buffer_mutex); + + if (pdata->running) + jack_ringbuffer_read( pdata->jdata->sound_buffer, jackbuf, (pdata->sound_framesize * pdata->jdata->buffersize) ); - vorbis_buffer = vorbis_analysis_buffer(&pdata->enc_data->m_vo_dsp, sampread); + pthread_mutex_unlock(&pdata->sound_buffer_mutex); + + if (!pdata->running) + break; + + vorbis_buffer = vorbis_analysis_buffer(&pdata->enc_data->m_vo_dsp, sampread); - for (int j = 0, count = 0; j < pdata->args.channels; j++) { - for (int i = 0; i < sampread; i++) { - vorbis_buffer[j][i] = ((float*)jackbuf)[count]; - count++; - } + for (int j = 0, count = 0; j < pdata->args.channels; j++) { + for (int i = 0; i < sampread; i++) { + vorbis_buffer[j][i] = ((float*)jackbuf)[count]; + count++; } - } else { - /* FIXME TODO this needs redoing, I don't have JACK ATM so I can't build/test it for now */ - // pdata->v_enc_thread_waiting = 1; - pthread_mutex_lock(&pdata->snd_buff_ready_mutex); - pthread_cond_wait(&pdata->sound_data_read, &pdata->snd_buff_ready_mutex); - pthread_mutex_unlock(&pdata->snd_buff_ready_mutex); - // pdata->v_enc_thread_waiting=0; - continue; } #endif } diff --git a/recordmydesktop/src/rmd_initialize_data.c b/recordmydesktop/src/rmd_initialize_data.c index 92e3e14..0f0a073 100644 --- a/recordmydesktop/src/rmd_initialize_data.c +++ b/recordmydesktop/src/rmd_initialize_data.c @@ -74,7 +74,6 @@ int rmdInitializeData(ProgData *pdata, EncData *enc_data, CacheData *cache_data) pdata->dummy_p_size = 0; pthread_mutex_init(&pdata->sound_buffer_mutex, NULL); - pthread_mutex_init(&pdata->snd_buff_ready_mutex, NULL); pthread_mutex_init(&pdata->img_buff_ready_mutex, NULL); pthread_mutex_init(&pdata->theora_lib_mutex, NULL); pthread_mutex_init(&pdata->vorbis_lib_mutex, NULL); @@ -142,7 +141,7 @@ int rmdInitializeData(ProgData *pdata, EncData *enc_data, CacheData *cache_data) pdata->jdata->port_names = pdata->args.jack_port_names; pdata->jdata->nports = pdata->args.jack_nports; pdata->jdata->ringbuffer_secs = pdata->args.jack_ringbuffer_secs; - pdata->jdata->snd_buff_ready_mutex = &pdata->snd_buff_ready_mutex; + pdata->jdata->sound_buffer_mutex = &pdata->sound_buffer_mutex; pdata->jdata->sound_data_read = &pdata->sound_data_read; pdata->jdata->capture_started = 0; diff --git a/recordmydesktop/src/rmd_jack.c b/recordmydesktop/src/rmd_jack.c index ba37c8b..841017f 100644 --- a/recordmydesktop/src/rmd_jack.c +++ b/recordmydesktop/src/rmd_jack.c @@ -55,25 +55,19 @@ static int rmdJackCapture(jack_nframes_t nframes, void *jdata_t) { for(int i = 0; i < jdata->nports; i++) jdata->portbuf[i] = jack_port_get_buffer(jdata->ports[i], nframes); + + pthread_mutex_lock(jdata->sound_buffer_mutex); //vorbis analysis buffer wants uninterleaved data //so we are simply placing the buffers for every channel //sequentially on the ringbuffer for(int i = 0; i < jdata->nports; i++) - (*jack_ringbuffer_write)( jdata->sound_buffer, - (void *)(jdata->portbuf[i]), - nframes * - sizeof(jack_default_audio_sample_t)); -/*FIXME */ -//This is not safe. -//cond_var signaling must move away from signal handlers -//alltogether (rmdJackCapture, SetExpired, SetPaused). -//Better would be a set of pipes for each of these. -//The callback should write on the pipe and the main thread -//should perform a select over the fd's, signaling afterwards the -//appropriate cond_var. - pthread_mutex_lock(jdata->snd_buff_ready_mutex); + jack_ringbuffer_write( jdata->sound_buffer, + (void *)(jdata->portbuf[i]), + nframes * + sizeof(jack_default_audio_sample_t)); + pthread_cond_signal(jdata->sound_data_read); - pthread_mutex_unlock(jdata->snd_buff_ready_mutex); + pthread_mutex_unlock(jdata->sound_buffer_mutex); return 0; } @@ -148,7 +142,7 @@ int rmdStartJackClient(JackData *jdata) { snprintf( pidbuf, 8, "%d", pid ); strcat(rmd_client_name, pidbuf); - if ((jdata->client = (*jack_client_new)(rmd_client_name)) == 0) { + if ((jdata->client = jack_client_new(rmd_client_name)) == 0) { fprintf(stderr, "Could not create new client!\n" "Make sure that Jack server is running!\n"); return 15; @@ -172,7 +166,7 @@ int rmdStartJackClient(JackData *jdata) { jdata->frequency* sizeof(jack_default_audio_sample_t)* jdata->nports); - jdata->sound_buffer = (*jack_ringbuffer_create)((int)(ring_buffer_size+0.5));//round up + jdata->sound_buffer = jack_ringbuffer_create((int)(ring_buffer_size+0.5));//round up jack_set_process_callback(jdata->client, rmdJackCapture, jdata); jack_on_shutdown(jdata->client, rmdJackShutdown, jdata); @@ -192,7 +186,7 @@ int rmdStartJackClient(JackData *jdata) { int rmdStopJackClient(JackData *jdata) { int ret = 0; - (*jack_ringbuffer_free)(jdata->sound_buffer); + jack_ringbuffer_free(jdata->sound_buffer); if (jack_client_close(jdata->client)) { fprintf(stderr, "Cannot close Jack client!\n"); ret = 1; diff --git a/recordmydesktop/src/rmd_types.h b/recordmydesktop/src/rmd_types.h index d3e1ce0..6018955 100644 --- a/recordmydesktop/src/rmd_types.h +++ b/recordmydesktop/src/rmd_types.h @@ -230,7 +230,7 @@ typedef struct _JackData{ jack_port_t **ports; //connections to thes ports. jack_default_audio_sample_t **portbuf; //retrieval of audio buffers. - pthread_mutex_t *snd_buff_ready_mutex; //mutex and cond_var + pthread_mutex_t *sound_buffer_mutex; //mutex and cond_var pthread_cond_t *sound_data_read; //in the pdata struct jack_ringbuffer_t *sound_buffer; //data exchange happens through this int capture_started; //used to hold recording in the beginning @@ -266,7 +266,6 @@ struct _ProgData { Display *dpy; //curtrent display /** Mutexes*/ pthread_mutex_t sound_buffer_mutex, - snd_buff_ready_mutex, img_buff_ready_mutex, theora_lib_mutex, //serializes access to th_encoding_clean w/theora_lib_clean vorbis_lib_mutex, //serializes acces to v_encoding_clean w/vorbis_lib_clean -- cgit v1.2.3