Fix realloc, add convenience buffer to audio stream.

This commit is contained in:
Michael Jumper 2012-10-28 01:14:19 -07:00
parent 0114621de3
commit bcde14d270
5 changed files with 81 additions and 15 deletions

View File

@ -57,7 +57,10 @@ libguac_client_rdp_la_SOURCES = \
src/rdp_keymap_en_us.c \
src/default_pointer.c
guac_rdpsnd_la_SOURCES = guac_rdpsnd/messages.c guac_rdpsnd/service.c
guac_rdpsnd_la_SOURCES = \
src/audio.c \
guac_rdpsnd/messages.c \
guac_rdpsnd/service.c
noinst_HEADERS = \
include/audio.h \

View File

@ -205,6 +205,11 @@ void rdpsnd_process_message_wave(guac_rdpsndPlugin* rdpsnd,
guac_client_log_info(audio->client, "Got sound: %i bytes.", size);
/* For now, start AND end stream right here */
audio_stream_begin(audio);
audio_stream_write_pcm(audio, buffer, size);
audio_stream_end(audio);
output_stream = stream_new(8);
stream_write_uint8(output_stream, SNDC_WAVECONFIRM);
stream_write_uint8(output_stream, 0);

View File

@ -57,7 +57,7 @@ typedef void audio_encoder_end_handler(audio_stream* audio);
* Handler which is called when the audio stream is flushed.
*/
typedef void audio_encoder_write_handler(audio_stream* audio,
int* pcm_data, int length);
unsigned char* pcm_data, int length);
/**
* Arbitrary audio codec encoder.
@ -89,23 +89,38 @@ typedef struct audio_encoder {
struct audio_stream {
/**
* PCM data buffer, 16-bit samples, 2-channel.
* PCM data buffer, 16-bit samples, 2-channel, 44100 Hz.
*/
int* pcm_data;
unsigned char* pcm_data;
/**
* Number of samples in buffer.
* Number of bytes in buffer.
*/
int used;
/**
* Maximum number of samples in buffer.
* Maximum number of bytes in buffer.
*/
int length;
/**
* Arbitrary codec encoder. When the PCM buffer is flushed, PCM data will be sent
* to this encoder.
* Encoded audio data buffer, as written by the encoder.
*/
unsigned char* encoded_data;
/**
* Number of bytes in the encoded data buffer.
*/
int encoded_data_used;
/**
* Maximum number of bytes in the encoded data buffer.
*/
int encoded_data_length;
/**
* Arbitrary codec encoder. When the PCM buffer is flushed, PCM data will
* be sent to this encoder.
*/
audio_encoder* encoder;
@ -145,12 +160,26 @@ void audio_stream_end(audio_stream* stream);
/**
* Writes PCM data to the given audio stream.
*/
void audio_stream_write_pcm(audio_stream* stream, const int* data, int length);
void audio_stream_write_pcm(audio_stream* stream,
unsigned char* data, int length);
/**
* Flushes the given audio stream.
*/
void audio_stream_flush(audio_stream* stream);
/**
* Appends arbitrarily-encoded data to the encoded_data buffer
* within the given audio stream.
*/
void audio_stream_append_data(audio_stream* stream,
unsigned char* data, int length);
/**
* Clears all data from the encoded_data buffer in the given
* audio stream.
*/
void audio_stream_clear_data(audio_stream* stream);
#endif

View File

@ -53,8 +53,12 @@ audio_stream* audio_stream_alloc(guac_client* client, audio_encoder* encoder) {
audio->used = 0;
audio->length = 0x40000;
/* Allocate buffer */
audio->pcm_data = malloc(sizeof(int) * audio->length);
audio->encoded_data_used = 0;
audio->encoded_data_length = 0x40000;
/* Allocate bufferis */
audio->pcm_data = malloc(audio->length);
audio->encoded_data = malloc(audio->encoded_data_length);
/* Assign encoder */
audio->encoder = encoder;
@ -77,14 +81,15 @@ void audio_stream_free(audio_stream* audio) {
free(audio);
}
void audio_stream_write_pcm(audio_stream* audio, const int* data, int length) {
void audio_stream_write_pcm(audio_stream* audio,
unsigned char* data, int length) {
/* Resize audio buffer if necessary */
if (length > audio->length) {
/* Resize to double provided length */
audio->length = length*2;
audio->pcm_data = realloc(audio, sizeof(int) * audio->length);
audio->pcm_data = realloc(audio->pcm_data, audio->length);
}
@ -93,7 +98,7 @@ void audio_stream_write_pcm(audio_stream* audio, const int* data, int length) {
audio_stream_flush(audio);
/* Append to buffer */
memcpy(&(audio->pcm_data[audio->used]), data, sizeof(int) * length);
memcpy(&(audio->pcm_data[audio->used]), data, length);
audio->used += length;
}
@ -114,3 +119,26 @@ void audio_stream_flush(audio_stream* audio) {
}
void audio_stream_append_data(audio_stream* audio,
unsigned char* data, int length) {
/* Resize audio buffer if necessary */
if (audio->encoded_data_used + length > audio->encoded_data_length) {
/* Increase to double concatenated size to accomodate */
audio->encoded_data_length = (audio->encoded_data_length + length)*2;
audio->encoded_data = realloc(audio->encoded_data,
audio->encoded_data_length);
}
/* Append to buffer */
memcpy(&(audio->encoded_data[audio->encoded_data_used]), data, length);
audio->encoded_data_used += length;
}
void audio_stream_clear_data(audio_stream* audio) {
audio->encoded_data_used = 0;
}

View File

@ -54,7 +54,8 @@ void ogg_encoder_end_handler(audio_stream* audio) {
}
void ogg_encoder_write_handler(audio_stream* audio, int* pcm_data, int length) {
void ogg_encoder_write_handler(audio_stream* audio,
unsigned char* pcm_data, int length) {
guac_client_log_info(audio->client,
"OGG: Writing data to stream, length=%i",