From ddc09b161aa32915bb60621c9164721ccf21a939 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 20 Jan 2020 17:36:00 -0800 Subject: [PATCH 1/3] GUACAMOLE-934: Add support for the Play Sound PDU. --- src/protocols/rdp/Makefile.am | 2 + src/protocols/rdp/beep.c | 137 ++++++++++++++++++++++++++++++++++ src/protocols/rdp/beep.h | 65 ++++++++++++++++ src/protocols/rdp/rdp.c | 4 + 4 files changed, 208 insertions(+) create mode 100644 src/protocols/rdp/beep.c create mode 100644 src/protocols/rdp/beep.h diff --git a/src/protocols/rdp/Makefile.am b/src/protocols/rdp/Makefile.am index ff2ff8dd..7f47a459 100644 --- a/src/protocols/rdp/Makefile.am +++ b/src/protocols/rdp/Makefile.am @@ -38,6 +38,7 @@ nodist_libguac_client_rdp_la_SOURCES = \ _generated_keymaps.c libguac_client_rdp_la_SOURCES = \ + beep.c \ bitmap.c \ channels/audio-input/audio-buffer.c \ channels/audio-input/audio-input.c \ @@ -81,6 +82,7 @@ libguac_client_rdp_la_SOURCES = \ user.c noinst_HEADERS = \ + beep.h \ bitmap.h \ channels/audio-input/audio-buffer.h \ channels/audio-input/audio-input.h \ diff --git a/src/protocols/rdp/beep.c b/src/protocols/rdp/beep.c new file mode 100644 index 00000000..e91a7afe --- /dev/null +++ b/src/protocols/rdp/beep.c @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "beep.h" +#include "rdp.h" +#include "settings.h" + +#include +#include +#include +#include + +#include +#include +#include + +/** + * Fills the given buffer with signed 8-bit, single-channel PCM at the given + * sample rate which will produce a beep of the given frequency. + * + * @param buffer + * The buffer to fill with PCM data. + * + * @param frequency + * The frequency of the beep to generate, in hertz. + * + * @param rate + * The sample rate of the PCM to generate, in samples per second. + * + * @param buffer_size + * The number of bytes of PCM data to write to the given buffer. + */ +static void guac_rdp_beep_fill_square_wave(unsigned char* buffer, + int frequency, int rate, int buffer_size) { + + int remaining = buffer_size; + int current_value = GUAC_RDP_BEEP_AMPLITUDE; + int pulse_width = rate / frequency / 2; + + /* Repeatedly write pulses (whose widths are determined by the desired + * frequency) until buffer space is exhausted */ + while (remaining > 0) { + + /* Truncate pulse if insufficient space remains */ + int block_size = pulse_width; + if (block_size > remaining) + block_size = remaining; + + /* Write blocks, alternating the sign of the amplitude of each + * successive block */ + memset(buffer, current_value, block_size); + current_value = -current_value; + + buffer += block_size; + remaining -= block_size; + + } + +} + +/** + * Writes PCM data to the given guac_audio_stream which produces a beep of the + * given frequency and duration. The provided guac_audio_stream may be + * configured for any sample rate but MUST be configured for single-channel, + * 8-bit PCM. + * + * @param audio + * The guac_audio_stream which should receive the PCM data. + * + * @param frequency + * The frequency of the beep, in hertz. + * + * @param duration + * The duration of the beep, in milliseconds. + */ +static void guac_rdp_beep_write_pcm(guac_audio_stream* audio, + int frequency, int duration) { + + int buffer_size = audio->rate * duration / 1000; + unsigned char* buffer = malloc(buffer_size); + + /* Beep for given frequency/duration using a simple square wave */ + guac_rdp_beep_fill_square_wave(buffer, frequency, audio->rate, buffer_size); + guac_audio_stream_write_pcm(audio, buffer, buffer_size); + + free(buffer); + +} + +BOOL guac_rdp_beep_play_sound(rdpContext* context, + const PLAY_SOUND_UPDATE* play_sound) { + + guac_client* client = ((rdp_freerdp_context*) context)->client; + guac_rdp_client* rdp_client = (guac_rdp_client*) client->data; + guac_rdp_settings* settings = rdp_client->settings; + + /* Ignore if audio is not enabled */ + if (!settings->audio_enabled) { + guac_client_log(client, GUAC_LOG_DEBUG, "Ignoring request to beep " + "for %" PRIu32 " millseconds at %" PRIu32 " Hz as audio is " + "disabled.", play_sound->duration, play_sound->frequency); + return TRUE; + } + + /* Allocate audio stream which sends audio in a format supported by the + * connected client(s) */ + guac_audio_stream* beep = guac_audio_stream_alloc(client, NULL, + GUAC_RDP_BEEP_SAMPLE_RATE, 1, 8); + + /* Limit maximum duration of each beep */ + int duration = play_sound->duration; + if (duration > GUAC_RDP_BEEP_MAX_DURATION) + duration = GUAC_RDP_BEEP_MAX_DURATION; + + guac_rdp_beep_write_pcm(beep, play_sound->frequency, duration); + guac_audio_stream_free(beep); + + return TRUE; + +} + diff --git a/src/protocols/rdp/beep.h b/src/protocols/rdp/beep.h new file mode 100644 index 00000000..8abc0648 --- /dev/null +++ b/src/protocols/rdp/beep.h @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef GUAC_RDP_BEEP_H +#define GUAC_RDP_BEEP_H + +#include +#include + +/** + * The sample rate of the each generated beep, in samples per second. + */ +#define GUAC_RDP_BEEP_SAMPLE_RATE 8000 + +/** + * The amplitude (volume) of each beep. As the beep is generated as 8-bit + * signed PCM, this should be kept between 0 and 127 inclusive. + */ +#define GUAC_RDP_BEEP_AMPLITUDE 64 + +/** + * The maximum duration of each beep, in milliseconds. This value should be + * kept relatively small to ensure the amount of data sent for each beep is + * minimal. + */ +#define GUAC_RDP_BEEP_MAX_DURATION 500 + +/** + * Processes a Play Sound PDU received from the RDP server, beeping for the + * requested duration and at the requested frequency. If audio has been + * disabled for the connection, the Play Sound PDU will be silently ignored, + * and this function has no effect. Beeps in excess of the maximum specified + * by GUAC_RDP_BEEP_MAX_DURATION will be truncated. + * + * @param context + * The rdpContext associated with the current RDP session. + * + * @param play_sound + * The PLAY_SOUND_UPDATE structure representing the received Play Sound + * PDU. + * + * @return + * TRUE if successful, FALSE otherwise. + */ +BOOL guac_rdp_beep_play_sound(rdpContext* context, + const PLAY_SOUND_UPDATE* play_sound); + +#endif + diff --git a/src/protocols/rdp/rdp.c b/src/protocols/rdp/rdp.c index 7e6b3e82..e7f3d3fb 100644 --- a/src/protocols/rdp/rdp.c +++ b/src/protocols/rdp/rdp.c @@ -17,6 +17,7 @@ * under the License. */ +#include "beep.h" #include "bitmap.h" #include "channels/audio-input/audio-buffer.h" #include "channels/audio-input/audio-input.h" @@ -167,6 +168,9 @@ BOOL rdp_freerdp_pre_connect(freerdp* instance) { pointer.SetDefault = guac_rdp_pointer_set_default; graphics_register_pointer(graphics, &pointer); + /* Beep on receipt of Play Sound PDU */ + instance->update->PlaySound = guac_rdp_beep_play_sound; + /* Set up GDI */ instance->update->DesktopResize = guac_rdp_gdi_desktop_resize; instance->update->EndPaint = guac_rdp_gdi_end_paint; From b8148b0dafbd6e5484d6142a0993c7dc30283c05 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 20 Jan 2020 21:16:36 -0800 Subject: [PATCH 2/3] GUACAMOLE-934: Handle potential lack of available audio streams. --- src/libguac/audio.c | 9 +++++++++ src/libguac/guacamole/audio.h | 3 ++- src/libguac/guacamole/client.h | 3 ++- src/libguac/guacamole/user.h | 8 ++++++-- src/protocols/rdp/beep.c | 9 +++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/libguac/audio.c b/src/libguac/audio.c index cc577c71..0f6a6aad 100644 --- a/src/libguac/audio.c +++ b/src/libguac/audio.c @@ -117,6 +117,12 @@ guac_audio_stream* guac_audio_stream_alloc(guac_client* client, audio->client = client; audio->stream = guac_client_alloc_stream(client); + /* Abort allocation if underlying stream cannot be allocated */ + if (audio->stream == NULL) { + free(audio); + return NULL; + } + /* Load PCM properties */ audio->rate = rate; audio->channels = channels; @@ -188,6 +194,9 @@ void guac_audio_stream_free(guac_audio_stream* audio) { if (audio->encoder != NULL && audio->encoder->end_handler) audio->encoder->end_handler(audio); + /* Release stream back to client pool */ + guac_client_free_stream(audio->client, audio->stream); + /* Free associated data */ free(audio); diff --git a/src/libguac/guacamole/audio.h b/src/libguac/guacamole/audio.h index df41772f..2f030ec4 100644 --- a/src/libguac/guacamole/audio.h +++ b/src/libguac/guacamole/audio.h @@ -148,7 +148,8 @@ struct guac_audio_stream { * @return * The newly allocated guac_audio_stream, or NULL if no audio stream could * be allocated due to lack of support on the part of the connecting - * Guacamole client. + * Guacamole client or due to reaching the maximum number of active + * streams. */ guac_audio_stream* guac_audio_stream_alloc(guac_client* client, guac_audio_encoder* encoder, int rate, int channels, int bps); diff --git a/src/libguac/guacamole/client.h b/src/libguac/guacamole/client.h index 88d1f416..71d48f22 100644 --- a/src/libguac/guacamole/client.h +++ b/src/libguac/guacamole/client.h @@ -383,7 +383,8 @@ void guac_client_free_layer(guac_client* client, guac_layer* layer); * The client to allocate the stream for. * * @return - * The next available stream, or a newly allocated stream. + * The next available stream, or a newly allocated stream, or NULL if the + * maximum number of active streams has been reached. */ guac_stream* guac_client_alloc_stream(guac_client* client); diff --git a/src/libguac/guacamole/user.h b/src/libguac/guacamole/user.h index 702160fa..fac42b98 100644 --- a/src/libguac/guacamole/user.h +++ b/src/libguac/guacamole/user.h @@ -574,8 +574,12 @@ int guac_user_handle_instruction(guac_user* user, const char* opcode, * Allocates a new stream. An arbitrary index is automatically assigned * if no previously-allocated stream is available for use. * - * @param user The user to allocate the stream for. - * @return The next available stream, or a newly allocated stream. + * @param user + * The user to allocate the stream for. + * + * @return + * The next available stream, or a newly allocated stream, or NULL if the + * maximum number of active streams has been reached. */ guac_stream* guac_user_alloc_stream(guac_user* user); diff --git a/src/protocols/rdp/beep.c b/src/protocols/rdp/beep.c index e91a7afe..46e80fd5 100644 --- a/src/protocols/rdp/beep.c +++ b/src/protocols/rdp/beep.c @@ -123,6 +123,15 @@ BOOL guac_rdp_beep_play_sound(rdpContext* context, guac_audio_stream* beep = guac_audio_stream_alloc(client, NULL, GUAC_RDP_BEEP_SAMPLE_RATE, 1, 8); + /* Stream availability is not guaranteed */ + if (beep == NULL) { + guac_client_log(client, GUAC_LOG_DEBUG, "Ignoring request to beep " + "for %" PRIu32 " millseconds at %" PRIu32 " Hz as no audio " + "stream could be allocated.", play_sound->duration, + play_sound->frequency); + return TRUE; + } + /* Limit maximum duration of each beep */ int duration = play_sound->duration; if (duration > GUAC_RDP_BEEP_MAX_DURATION) From b21d19d37da587c095bf1e57c4a74cd59dab751f Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 21 Jan 2020 00:03:42 -0800 Subject: [PATCH 3/3] GUACAMOLE-934: Correct waveform calculation. Switch to triangle wave to reduce aliasing distortion. --- src/protocols/rdp/beep.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/protocols/rdp/beep.c b/src/protocols/rdp/beep.c index 46e80fd5..8fb5001f 100644 --- a/src/protocols/rdp/beep.c +++ b/src/protocols/rdp/beep.c @@ -46,29 +46,30 @@ * @param buffer_size * The number of bytes of PCM data to write to the given buffer. */ -static void guac_rdp_beep_fill_square_wave(unsigned char* buffer, +static void guac_rdp_beep_fill_triangle_wave(unsigned char* buffer, int frequency, int rate, int buffer_size) { - int remaining = buffer_size; - int current_value = GUAC_RDP_BEEP_AMPLITUDE; - int pulse_width = rate / frequency / 2; + /* With the distance between each positive/negative peak and zero being the + * amplitude, and with the "bounce" between those peaks occurring once + * every two periods, the number of distinct states that the triangle wave + * function goes through is twice the peak-to-peak amplitude, or four times + * the overall amplitude */ + const int wave_period = GUAC_RDP_BEEP_AMPLITUDE * 4; - /* Repeatedly write pulses (whose widths are determined by the desired - * frequency) until buffer space is exhausted */ - while (remaining > 0) { + /* With the number of distinct states being the wave_period defined above, + * the "bounce" point within that period is half the period */ + const int wave_bounce_offset = wave_period / 2; - /* Truncate pulse if insufficient space remains */ - int block_size = pulse_width; - if (block_size > remaining) - block_size = remaining; + for (int position = 0; position < buffer_size; position++) { - /* Write blocks, alternating the sign of the amplitude of each - * successive block */ - memset(buffer, current_value, block_size); - current_value = -current_value; + /* Calculate relative position within the repeating portion of the wave + * (the portion with wave_period unique states) */ + int wave_position = (position * frequency * wave_period / rate) % wave_period; - buffer += block_size; - remaining -= block_size; + /* Calculate state of the triangle wave function at the calculated + * offset, knowing in advance the relative location that the function + * should "bounce" */ + *(buffer++) = abs(wave_position - wave_bounce_offset) - GUAC_RDP_BEEP_AMPLITUDE; } @@ -95,8 +96,8 @@ static void guac_rdp_beep_write_pcm(guac_audio_stream* audio, int buffer_size = audio->rate * duration / 1000; unsigned char* buffer = malloc(buffer_size); - /* Beep for given frequency/duration using a simple square wave */ - guac_rdp_beep_fill_square_wave(buffer, frequency, audio->rate, buffer_size); + /* Beep for given frequency/duration using a simple triangle wave */ + guac_rdp_beep_fill_triangle_wave(buffer, frequency, audio->rate, buffer_size); guac_audio_stream_write_pcm(audio, buffer, buffer_size); free(buffer);