From 47b060d6ebdd7c5b2f176b5af5ddba0b482f56f6 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 21 Aug 2015 20:35:13 -0700 Subject: [PATCH 1/2] GUAC-427: Avoid sending silence received from PulseAudio. --- src/protocols/vnc/pulse.c | 55 ++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/protocols/vnc/pulse.c b/src/protocols/vnc/pulse.c index f0e332ab..39af79cd 100644 --- a/src/protocols/vnc/pulse.c +++ b/src/protocols/vnc/pulse.c @@ -29,6 +29,38 @@ #include #include +/** + * Returns whether the given buffer contains only silence (only null bytes). + * + * @param buffer + * The audio buffer to check. + * + * @param length + * The length of the buffer to check. + * + * @return + * Non-zero if the audio buffer contains silence, zero otherwise. + */ +static int guac_pa_is_silence(const void* buffer, size_t length) { + + int i; + + unsigned char* current = (unsigned char*) buffer; + + /* For each byte in buffer */ + for (i = 0; i < length; i++) { + + /* If current value non-zero, then not silence */ + if (*(current++)) + return 0; + + } + + /* Otherwise, the buffer contains 100% silence */ + return 1; + +} + static void __stream_read_callback(pa_stream* stream, size_t length, void* data) { @@ -41,16 +73,21 @@ static void __stream_read_callback(pa_stream* stream, size_t length, /* Read data */ pa_stream_peek(stream, &buffer, &length); - /* Write data */ - guac_audio_stream_write_pcm(audio, buffer, length); + /* Avoid sending silence unless data is waiting to be flushed */ + if (audio->pcm_bytes_written != 0 || !guac_pa_is_silence(buffer, length)) { + + /* Write data */ + guac_audio_stream_write_pcm(audio, buffer, length); + + /* Flush occasionally */ + if (audio->pcm_bytes_written > GUAC_VNC_PCM_WRITE_RATE) { + guac_audio_stream_end(audio); + guac_audio_stream_begin(client_data->audio, + GUAC_VNC_AUDIO_RATE, + GUAC_VNC_AUDIO_CHANNELS, + GUAC_VNC_AUDIO_BPS); + } - /* Flush occasionally */ - if (audio->pcm_bytes_written > GUAC_VNC_PCM_WRITE_RATE) { - guac_audio_stream_end(audio); - guac_audio_stream_begin(client_data->audio, - GUAC_VNC_AUDIO_RATE, - GUAC_VNC_AUDIO_CHANNELS, - GUAC_VNC_AUDIO_BPS); } /* Advance buffer */ From 927ceb99dd5caba075ce4439a8ac39bab707c3ef Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 4 Sep 2015 13:47:21 -0700 Subject: [PATCH 2/2] GUAC-427: Fix const-ness of pointer. --- src/protocols/vnc/pulse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocols/vnc/pulse.c b/src/protocols/vnc/pulse.c index 39af79cd..338df6d7 100644 --- a/src/protocols/vnc/pulse.c +++ b/src/protocols/vnc/pulse.c @@ -45,7 +45,7 @@ static int guac_pa_is_silence(const void* buffer, size_t length) { int i; - unsigned char* current = (unsigned char*) buffer; + const unsigned char* current = (const unsigned char*) buffer; /* For each byte in buffer */ for (i = 0; i < length; i++) {