diff --git a/src/protocols/vnc/client.c b/src/protocols/vnc/client.c index 99a2bbea..ba2717b8 100644 --- a/src/protocols/vnc/client.c +++ b/src/protocols/vnc/client.c @@ -37,7 +37,6 @@ #include #include -#include #include @@ -107,10 +106,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) { vnc_guac_client_data* guac_client_data; -#ifdef ENABLE_PULSE - pthread_t pa_read_thread; -#endif - int read_only; /* Set up libvncclient logging */ @@ -180,16 +175,11 @@ int guac_client_init(guac_client* client, int argc, char** argv) { "Audio will be encoded as %s", guac_client_data->audio->encoder->mimetype); - /* Create a thread to read audio data */ - if (pthread_create(&pa_read_thread, NULL, guac_pa_read_audio, - (void*) client)) { - guac_protocol_send_error(client->socket, - "Error initializing PulseAudio read thread"); - guac_socket_flush(client->socket); - return 1; - } - - guac_client_data->audio_read_thread = &pa_read_thread; + /* Require threadsafe sockets if audio enabled */ + guac_socket_require_threadsafe(client->socket); + + /* Start audio stream */ + guac_pa_start_stream(client); } @@ -198,9 +188,6 @@ int guac_client_init(guac_client* client, int argc, char** argv) { guac_client_log_info(client, "No available audio encoding. Sound disabled."); - /* Require threadsafe sockets if audio enabled */ - guac_socket_require_threadsafe(client->socket); - } /* end if audio enabled */ #endif diff --git a/src/protocols/vnc/client.h b/src/protocols/vnc/client.h index 194eb0ec..176f00eb 100644 --- a/src/protocols/vnc/client.h +++ b/src/protocols/vnc/client.h @@ -42,6 +42,8 @@ #include #include +#include + extern char* __GUAC_CLIENT; typedef struct vnc_guac_client_data { @@ -67,9 +69,9 @@ typedef struct vnc_guac_client_data { guac_audio_stream* audio; /** - * PulseAudio read thread. + * PulseAudio event loop. */ - pthread_t* audio_read_thread; + pa_threaded_mainloop* pa_mainloop; } vnc_guac_client_data; diff --git a/src/protocols/vnc/guac_handlers.c b/src/protocols/vnc/guac_handlers.c index a639cce2..662e5e0f 100644 --- a/src/protocols/vnc/guac_handlers.c +++ b/src/protocols/vnc/guac_handlers.c @@ -39,7 +39,6 @@ #include #include #include -#include #include @@ -130,13 +129,9 @@ int vnc_guac_client_free_handler(guac_client* client) { rfbClient* rfb_client = guac_client_data->rfb_client; #ifdef ENABLE_PULSE - if (guac_client_data->audio_enabled) { - - /* Wait for audio read thread to join */ - if (guac_client_data->audio_read_thread) - pthread_join(*(guac_client_data->audio_read_thread), NULL); - - } + /* If audio enabled, stop streaming */ + if (guac_client_data->audio_enabled) + guac_pa_stop_stream(client); #endif /* Free encodings string, if used */ diff --git a/src/protocols/vnc/pulse.c b/src/protocols/vnc/pulse.c index 3c502685..66bb0a2f 100644 --- a/src/protocols/vnc/pulse.c +++ b/src/protocols/vnc/pulse.c @@ -37,64 +37,19 @@ #include -#include -#include +#include -void* guac_pa_read_audio(void* data) { +void guac_pa_start_stream(guac_client* client) { - pa_sample_spec spec; - pa_simple* stream; - int error; - - /* Get client */ - guac_client* client = (guac_client*) data; - - /* Init sample spec */ - spec.format = PA_SAMPLE_S16LE; - spec.rate = 44100; - spec.channels = 2; - - /* Create new stream */ - stream = pa_simple_new(NULL, /* Name of server */ - "Guacamole", /* Name of client */ - PA_STREAM_RECORD, NULL, /* Direction and device */ - "Audio", /* Name of stream */ - &spec, NULL, NULL, /* Stream options */ - &error); - - /* Fail on error */ - if (stream == NULL) { - guac_client_log_error(client, "Unable to connect to PulseAudio: %s", - pa_strerror(error)); - return NULL; - } - - /* Start streaming */ - guac_client_log_info(client, "Streaming audio from PulseAudio"); - - while (client->state == GUAC_CLIENT_RUNNING) { - - uint8_t buffer[1024]; - - guac_client_log_info(client, "Reading..."); - - /* Read packet of audio data */ - if (pa_simple_read(stream, buffer, sizeof(buffer), &error) < 0) { - guac_client_log_error(client, "Unable to read from PulseAudio: %s", - pa_strerror(error)); - return NULL; - } - - /* STUB: Write data */ - guac_client_log_info(client, "Would write %i", sizeof(buffer)); - - } - - /* Close stream */ - guac_client_log_info(client, "Streaming from PulseAudio finished"); - pa_simple_free(stream); - - return NULL; + /* STUB */ + guac_client_log_info(client, "Starting audio stream"); + +} + +void guac_pa_stop_stream(guac_client* client) { + + /* STUB */ + guac_client_log_info(client, "Audio stream finished"); } diff --git a/src/protocols/vnc/pulse.h b/src/protocols/vnc/pulse.h index 5ccd92b4..7f7e587e 100644 --- a/src/protocols/vnc/pulse.h +++ b/src/protocols/vnc/pulse.h @@ -39,12 +39,18 @@ #define __GUAC_VNC_PULSE_H /** - * Read thread which transfers PCM data read from PulseAudio to the audio - * encoder in use. + * Starts streaming audio from PulseAudio to the given Guacamole client. * - * @param data Pointer to the active guac_client. + * @param client The client to stream data to. */ -void* guac_pa_read_audio(void* data); +void guac_pa_start_stream(guac_client* client); + +/** + * Stops streaming audio from PulseAudio to the given Guacamole client. + * + * @param client The client to stream data to. + */ +void guac_pa_stop_stream(guac_client* client); #endif