GUACAMOLE-934: Add support for the Play Sound PDU.

This commit is contained in:
Michael Jumper 2020-01-20 17:36:00 -08:00
parent 0cd9ba387b
commit ddc09b161a
4 changed files with 208 additions and 0 deletions

View File

@ -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 \

137
src/protocols/rdp/beep.c Normal file
View File

@ -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 <freerdp/freerdp.h>
#include <guacamole/audio.h>
#include <guacamole/client.h>
#include <winpr/wtypes.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
/**
* 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;
}

65
src/protocols/rdp/beep.h Normal file
View File

@ -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 <freerdp/freerdp.h>
#include <winpr/wtypes.h>
/**
* 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

View File

@ -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;