guacamole-spice-protocol/src/protocols/rdp/guac_rdpsnd/rdpsnd_messages.c

304 lines
9.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2013 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2012-10-28 01:36:21 +00:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2012-10-28 01:36:21 +00:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2014-01-01 22:44:28 +00:00
#include "config.h"
#include "client.h"
#include "rdpsnd_messages.h"
#include "rdpsnd_service.h"
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
#include <pthread.h>
2012-10-28 01:36:21 +00:00
#include <stdlib.h>
2014-01-01 22:44:28 +00:00
2012-10-28 01:36:21 +00:00
#include <freerdp/utils/svc_plugin.h>
2014-01-01 22:44:28 +00:00
#include <guacamole/audio.h>
#include <guacamole/client.h>
2012-10-28 01:36:21 +00:00
2013-07-17 02:40:35 +00:00
#ifdef ENABLE_WINPR
#include <winpr/stream.h>
2013-07-17 17:22:03 +00:00
#include <winpr/wtypes.h>
2013-07-17 02:40:35 +00:00
#else
2013-07-17 17:22:03 +00:00
#include "compat/winpr-stream.h"
#include "compat/winpr-wtypes.h"
2013-07-17 02:40:35 +00:00
#endif
2012-10-28 02:46:07 +00:00
/* MESSAGE HANDLERS */
2012-10-28 01:36:21 +00:00
void guac_rdpsnd_formats_handler(guac_rdpsndPlugin* rdpsnd,
guac_audio_stream* audio, wStream* input_stream,
guac_rdpsnd_pdu_header* header) {
int server_format_count;
int server_version;
int i;
2013-07-17 06:33:03 +00:00
wStream* output_stream;
int output_body_size;
unsigned char* output_stream_end;
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
rdp_guac_client_data* guac_client_data =
(rdp_guac_client_data*) audio->client->data;
/* Format header */
2013-07-17 17:04:23 +00:00
Stream_Seek(input_stream, 14);
Stream_Read_UINT16(input_stream, server_format_count);
Stream_Seek_UINT8(input_stream);
Stream_Read_UINT16(input_stream, server_version);
Stream_Seek_UINT8(input_stream);
/* Initialize Client Audio Formats and Version PDU */
2013-07-17 17:04:23 +00:00
output_stream = Stream_New(NULL, 24);
Stream_Write_UINT8(output_stream, SNDC_FORMATS);
Stream_Write_UINT8(output_stream, 0);
/* Fill in body size later */
2013-07-17 17:04:23 +00:00
Stream_Seek_UINT16(output_stream); /* offset = 0x02 */
/* Flags, volume, and pitch */
2013-07-17 17:04:23 +00:00
Stream_Write_UINT32(output_stream, TSSNDCAPS_ALIVE);
Stream_Write_UINT32(output_stream, 0);
Stream_Write_UINT32(output_stream, 0);
/* Datagram port (UDP) */
2013-07-17 17:04:23 +00:00
Stream_Write_UINT16(output_stream, 0);
/* Fill in format count later */
2013-07-17 17:04:23 +00:00
Stream_Seek_UINT16(output_stream); /* offset = 0x12 */
/* Version and padding */
2013-07-17 17:04:23 +00:00
Stream_Write_UINT8(output_stream, 0);
Stream_Write_UINT16(output_stream, 6);
Stream_Write_UINT8(output_stream, 0);
/* Check each server format, respond if supported */
for (i=0; i < server_format_count; i++) {
unsigned char* format_start;
int format_tag;
int channels;
int rate;
int bps;
int body_size;
/* Remember position in stream */
2013-07-17 17:04:23 +00:00
Stream_GetPointer(input_stream, format_start);
/* Read format */
2013-07-17 17:04:23 +00:00
Stream_Read_UINT16(input_stream, format_tag);
Stream_Read_UINT16(input_stream, channels);
Stream_Read_UINT32(input_stream, rate);
Stream_Seek_UINT32(input_stream);
Stream_Seek_UINT16(input_stream);
Stream_Read_UINT16(input_stream, bps);
/* Skip past extra data */
2013-07-17 17:04:23 +00:00
Stream_Read_UINT16(input_stream, body_size);
Stream_Seek(input_stream, body_size);
/* If PCM, accept */
if (format_tag == WAVE_FORMAT_PCM) {
2012-10-28 01:36:21 +00:00
2012-11-01 00:44:23 +00:00
/* If can fit another format, accept it */
if (rdpsnd->format_count < GUAC_RDP_MAX_FORMATS) {
/* Add channel */
int current = rdpsnd->format_count++;
rdpsnd->formats[current].rate = rate;
rdpsnd->formats[current].channels = channels;
rdpsnd->formats[current].bps = bps;
2012-11-01 00:44:23 +00:00
/* Log format */
guac_client_log(audio->client, GUAC_LOG_INFO,
2012-11-01 00:44:23 +00:00
"Accepted format: %i-bit PCM with %i channels at "
"%i Hz",
bps, channels, rate);
/* Queue format for sending as accepted */
2013-07-17 17:04:23 +00:00
Stream_EnsureRemainingCapacity(output_stream, 18 + body_size);
Stream_Write(output_stream, format_start, 18 + body_size);
/*
2013-07-17 17:04:23 +00:00
* BEWARE that using Stream_EnsureRemainingCapacity means that any
* pointers returned via Stream_GetPointer on output_stream are invalid.
*/
2012-11-01 00:44:23 +00:00
}
/* Otherwise, log that we dropped one */
else
guac_client_log(audio->client, GUAC_LOG_INFO,
2012-11-01 00:44:23 +00:00
"Dropped valid format: %i-bit PCM with %i channels at "
"%i Hz",
bps, channels, rate);
}
}
2012-11-01 00:44:23 +00:00
/* Calculate size of PDU */
2013-07-17 17:04:23 +00:00
output_body_size = Stream_GetPosition(output_stream) - 4;
Stream_GetPointer(output_stream, output_stream_end);
2012-10-28 01:36:21 +00:00
/* Set body size */
2013-07-17 17:04:23 +00:00
Stream_SetPosition(output_stream, 0x02);
Stream_Write_UINT16(output_stream, output_body_size);
2012-10-28 01:36:21 +00:00
/* Set format count */
2013-07-17 17:04:23 +00:00
Stream_SetPosition(output_stream, 0x12);
Stream_Write_UINT16(output_stream, rdpsnd->format_count);
2012-10-28 01:36:21 +00:00
/* Reposition cursor at end (necessary for message send) */
2013-07-17 17:04:23 +00:00
Stream_SetPointer(output_stream, output_stream_end);
2012-10-28 01:36:21 +00:00
/* Send accepted formats */
2012-11-01 04:52:16 +00:00
pthread_mutex_lock(&(guac_client_data->rdp_lock));
svc_plugin_send((rdpSvcPlugin*)rdpsnd, output_stream);
2012-10-28 01:36:21 +00:00
/* If version greater than 6, must send Quality Mode PDU */
if (server_version >= 6) {
2012-10-28 01:36:21 +00:00
/* Always send High Quality for now */
2013-07-17 17:04:23 +00:00
output_stream = Stream_New(NULL, 8);
Stream_Write_UINT8(output_stream, SNDC_QUALITYMODE);
Stream_Write_UINT8(output_stream, 0);
Stream_Write_UINT16(output_stream, 4);
Stream_Write_UINT16(output_stream, HIGH_QUALITY);
Stream_Write_UINT16(output_stream, 0);
2012-10-28 02:46:07 +00:00
svc_plugin_send((rdpSvcPlugin*)rdpsnd, output_stream);
}
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
pthread_mutex_unlock(&(guac_client_data->rdp_lock));
2012-10-28 01:36:21 +00:00
}
/* server is getting a feel of the round trip time */
void guac_rdpsnd_training_handler(guac_rdpsndPlugin* rdpsnd,
guac_audio_stream* audio, wStream* input_stream,
guac_rdpsnd_pdu_header* header) {
2012-10-28 01:36:21 +00:00
int data_size;
2013-07-17 06:33:03 +00:00
wStream* output_stream;
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
rdp_guac_client_data* guac_client_data =
(rdp_guac_client_data*) audio->client->data;
/* Read timestamp and data size */
2013-07-17 17:04:23 +00:00
Stream_Read_UINT16(input_stream, rdpsnd->server_timestamp);
Stream_Read_UINT16(input_stream, data_size);
2012-10-28 01:36:21 +00:00
/* Send training response */
2013-07-17 17:04:23 +00:00
output_stream = Stream_New(NULL, 8);
Stream_Write_UINT8(output_stream, SNDC_TRAINING);
Stream_Write_UINT8(output_stream, 0);
Stream_Write_UINT16(output_stream, 4);
Stream_Write_UINT16(output_stream, rdpsnd->server_timestamp);
Stream_Write_UINT16(output_stream, data_size);
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
pthread_mutex_lock(&(guac_client_data->rdp_lock));
svc_plugin_send((rdpSvcPlugin*) rdpsnd, output_stream);
2012-11-01 04:52:16 +00:00
pthread_mutex_unlock(&(guac_client_data->rdp_lock));
2012-10-28 01:36:21 +00:00
}
void guac_rdpsnd_wave_info_handler(guac_rdpsndPlugin* rdpsnd,
guac_audio_stream* audio, wStream* input_stream,
guac_rdpsnd_pdu_header* header) {
2012-10-28 01:36:21 +00:00
unsigned char buffer[4];
int format;
2012-10-28 01:36:21 +00:00
/* Read wave information */
2013-07-17 17:04:23 +00:00
Stream_Read_UINT16(input_stream, rdpsnd->server_timestamp);
Stream_Read_UINT16(input_stream, format);
Stream_Read_UINT8(input_stream, rdpsnd->waveinfo_block_number);
Stream_Seek(input_stream, 3);
Stream_Read(input_stream, buffer, 4);
/*
* Size of incoming wave data is equal to the body size field of this
* header, less the size of a WaveInfo PDU (not including the header),
* thus body_size - 12.
*/
rdpsnd->incoming_wave_size = header->body_size - 12;
2012-10-28 01:36:21 +00:00
/* Read wave in next iteration */
2013-07-17 17:04:23 +00:00
rdpsnd->next_pdu_is_wave = TRUE;
2012-10-28 01:36:21 +00:00
2012-11-01 00:44:23 +00:00
/* Init stream with requested format */
guac_audio_stream_begin(audio,
rdpsnd->formats[format].rate,
rdpsnd->formats[format].channels,
rdpsnd->formats[format].bps);
2012-10-29 17:51:56 +00:00
/* Write initial 4 bytes of data */
guac_audio_stream_write_pcm(audio, buffer, 4);
2012-10-28 01:36:21 +00:00
}
2012-10-28 01:36:21 +00:00
void guac_rdpsnd_wave_handler(guac_rdpsndPlugin* rdpsnd,
guac_audio_stream* audio, wStream* input_stream,
guac_rdpsnd_pdu_header* header) {
2012-10-28 01:36:21 +00:00
rdpSvcPlugin* plugin = (rdpSvcPlugin*)rdpsnd;
2012-10-28 01:36:21 +00:00
2012-11-01 04:52:16 +00:00
rdp_guac_client_data* guac_client_data =
(rdp_guac_client_data*) audio->client->data;
/* Wave Confirmation PDU */
2013-07-17 17:04:23 +00:00
wStream* output_stream = Stream_New(NULL, 8);
/* Get wave data */
2013-07-17 17:04:23 +00:00
unsigned char* buffer = Stream_Buffer(input_stream) + 4;
2012-10-28 01:36:21 +00:00
2012-10-29 17:51:56 +00:00
/* Write rest of audio packet */
guac_audio_stream_write_pcm(audio, buffer, rdpsnd->incoming_wave_size);
guac_audio_stream_end(audio);
/* Write Wave Confirmation PDU */
2013-07-17 17:04:23 +00:00
Stream_Write_UINT8(output_stream, SNDC_WAVECONFIRM);
Stream_Write_UINT8(output_stream, 0);
Stream_Write_UINT16(output_stream, 4);
Stream_Write_UINT16(output_stream, rdpsnd->server_timestamp);
Stream_Write_UINT8(output_stream, rdpsnd->waveinfo_block_number);
Stream_Write_UINT8(output_stream, 0);
2012-10-28 01:36:21 +00:00
/* Send Wave Confirmation PDU */
2012-11-01 04:52:16 +00:00
pthread_mutex_lock(&(guac_client_data->rdp_lock));
2012-10-28 02:46:07 +00:00
svc_plugin_send(plugin, output_stream);
2012-11-01 04:52:16 +00:00
pthread_mutex_unlock(&(guac_client_data->rdp_lock));
/* We no longer expect to receive wave data */
2013-07-17 17:04:23 +00:00
rdpsnd->next_pdu_is_wave = FALSE;
2012-10-28 01:36:21 +00:00
}
2012-10-28 01:36:21 +00:00
void guac_rdpsnd_close_handler(guac_rdpsndPlugin* rdpsnd,
guac_audio_stream* audio, wStream* input_stream,
guac_rdpsnd_pdu_header* header) {
2012-10-28 01:36:21 +00:00
/* STUB: Do nothing for now */
2012-10-28 01:36:21 +00:00
}