From cabc465464254282d3b3fbaf855e8e954e988d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Thu, 7 Mar 2013 15:33:27 -0500 Subject: [PATCH] channels/rdpsnd/server: get rid of old thread util --- channels/rdpsnd/server/rdpsnd.c | 69 ++++++++++++++++++--------------- channels/server/channels.c | 18 ++++++--- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/channels/rdpsnd/server/rdpsnd.c b/channels/rdpsnd/server/rdpsnd.c index fc83fe002..4ace065f2 100644 --- a/channels/rdpsnd/server/rdpsnd.c +++ b/channels/rdpsnd/server/rdpsnd.c @@ -26,10 +26,11 @@ #include #include +#include +#include #include #include -#include #include #include @@ -37,8 +38,9 @@ typedef struct _rdpsnd_server { rdpsnd_server_context context; + HANDLE thread; + HANDLE StopEvent; void* rdpsnd_channel; - freerdp_thread* rdpsnd_channel_thread; STREAM* rdpsnd_pdu; FREERDP_DSP_CONTEXT* dsp_context; @@ -144,7 +146,7 @@ static BOOL rdpsnd_server_recv_formats(rdpsnd_server* rdpsnd, STREAM* s) stream_read_UINT16(s, rdpsnd->context.client_formats[i].wFormatTag); stream_read_UINT16(s, rdpsnd->context.client_formats[i].nChannels); stream_read_UINT32(s, rdpsnd->context.client_formats[i].nSamplesPerSec); - stream_seek_UINT32(s); /* nAvgBytesPerSec */ + stream_read_UINT32(s, rdpsnd->context.client_formats[i].nAvgBytesPerSec); stream_read_UINT16(s, rdpsnd->context.client_formats[i].nBlockAlign); stream_read_UINT16(s, rdpsnd->context.client_formats[i].wBitsPerSample); stream_read_UINT16(s, rdpsnd->context.client_formats[i].cbSize); @@ -164,18 +166,21 @@ static void* rdpsnd_server_thread_func(void* arg) void* fd; STREAM* s; void* buffer; + DWORD status; BYTE msgType; UINT16 BodySize; + HANDLE events[2]; UINT32 bytes_returned = 0; rdpsnd_server* rdpsnd = (rdpsnd_server*) arg; - freerdp_thread* thread = rdpsnd->rdpsnd_channel_thread; + + events[0] = rdpsnd->StopEvent; if (WTSVirtualChannelQuery(rdpsnd->rdpsnd_channel, WTSVirtualFileHandle, &buffer, &bytes_returned) == TRUE) { fd = *((void**) buffer); WTSFreeMemory(buffer); - thread->signals[thread->num_signals++] = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd); + events[1] = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd); } s = stream_new(4096); @@ -184,11 +189,12 @@ static void* rdpsnd_server_thread_func(void* arg) while (1) { - if (freerdp_thread_wait(thread) < 0) - break; - - if (freerdp_thread_is_stopped(thread)) + status = WaitForMultipleObjects(2, events, FALSE, INFINITE); + + if (WaitForSingleObject(rdpsnd->StopEvent, 0) == WAIT_OBJECT_0) + { break; + } stream_set_pos(s, 0); @@ -226,9 +232,8 @@ static void* rdpsnd_server_thread_func(void* arg) } stream_free(s); - freerdp_thread_quit(thread); - return 0; + return NULL; } static BOOL rdpsnd_server_initialize(rdpsnd_server_context* context) @@ -240,8 +245,11 @@ static BOOL rdpsnd_server_initialize(rdpsnd_server_context* context) if (rdpsnd->rdpsnd_channel != NULL) { rdpsnd->rdpsnd_pdu = stream_new(4096); - rdpsnd->rdpsnd_channel_thread = freerdp_thread_new(); - freerdp_thread_start(rdpsnd->rdpsnd_channel_thread, rdpsnd_server_thread_func, rdpsnd); + + rdpsnd->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + + rdpsnd->thread = CreateThread(NULL, 0, + (LPTHREAD_START_ROUTINE) rdpsnd_server_thread_func, (void*) rdpsnd, 0, NULL); return TRUE; } @@ -264,7 +272,6 @@ static void rdpsnd_server_select_format(rdpsnd_server_context* context, int clie return; } - rdpsnd->src_bytes_per_sample = context->src_format.wBitsPerSample / 8; rdpsnd->src_bytes_per_frame = rdpsnd->src_bytes_per_sample * context->src_format.nChannels; @@ -277,12 +284,12 @@ static void rdpsnd_server_select_format(rdpsnd_server_context* context, int clie return; } - if (format->wFormatTag == 0x11) + if (format->wFormatTag == WAVE_FORMAT_DVI_ADPCM) { bs = (format->nBlockAlign - 4 * format->nChannels) * 4; rdpsnd->out_frames = (format->nBlockAlign * 4 * format->nChannels * 2 / bs + 1) * bs / (format->nChannels * 2); } - else if (format->wFormatTag == 0x02) + else if (format->wFormatTag == WAVE_FORMAT_ADPCM) { bs = (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2; rdpsnd->out_frames = bs * 4; @@ -312,10 +319,10 @@ static void rdpsnd_server_select_format(rdpsnd_server_context* context, int clie static BOOL rdpsnd_server_send_audio_pdu(rdpsnd_server* rdpsnd) { int size; - BOOL r; BYTE* src; int frames; int fill_size; + BOOL status; AUDIO_FORMAT* format; int tbytes_per_frame; STREAM* s = rdpsnd->rdpsnd_pdu; @@ -339,14 +346,14 @@ static BOOL rdpsnd_server_send_audio_pdu(rdpsnd_server* rdpsnd) } size = frames * tbytes_per_frame; - if (format->wFormatTag == 0x11) + if (format->wFormatTag == WAVE_FORMAT_DVI_ADPCM) { rdpsnd->dsp_context->encode_ima_adpcm(rdpsnd->dsp_context, src, size, format->nChannels, format->nBlockAlign); src = rdpsnd->dsp_context->adpcm_buffer; size = rdpsnd->dsp_context->adpcm_size; } - else if (format->wFormatTag == 0x02) + else if (format->wFormatTag == WAVE_FORMAT_ADPCM) { rdpsnd->dsp_context->encode_ms_adpcm(rdpsnd->dsp_context, src, size, format->nChannels, format->nBlockAlign); @@ -357,11 +364,14 @@ static BOOL rdpsnd_server_send_audio_pdu(rdpsnd_server* rdpsnd) rdpsnd->context.block_no = (rdpsnd->context.block_no + 1) % 256; /* Fill to nBlockAlign for the last audio packet */ - if ((format->wFormatTag == 0x11 || format->wFormatTag == 0x02) && - rdpsnd->out_pending_frames < rdpsnd->out_frames && (size % format->nBlockAlign) != 0) + + fill_size = 0; + + if ((format->wFormatTag == WAVE_FORMAT_DVI_ADPCM || format->wFormatTag == WAVE_FORMAT_ADPCM) && + (rdpsnd->out_pending_frames < rdpsnd->out_frames) && ((size % format->nBlockAlign) != 0)) + { fill_size = format->nBlockAlign - (size % format->nBlockAlign); - else - fill_size = 0; + } /* WaveInfo PDU */ stream_set_pos(s, 0); @@ -386,12 +396,12 @@ static BOOL rdpsnd_server_send_audio_pdu(rdpsnd_server* rdpsnd) if (fill_size > 0) stream_write_zero(s, fill_size); - r = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, stream_get_head(s), stream_get_length(s), NULL); + status = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, stream_get_head(s), stream_get_length(s), NULL); stream_set_pos(s, 0); rdpsnd->out_pending_frames = 0; - return r; + return status; } static BOOL rdpsnd_server_send_samples(rdpsnd_server_context* context, const void* buf, int nframes) @@ -408,7 +418,7 @@ static BOOL rdpsnd_server_send_samples(rdpsnd_server_context* context, const voi cframes = MIN(nframes, rdpsnd->out_frames - rdpsnd->out_pending_frames); cframesize = cframes * rdpsnd->src_bytes_per_frame; - memcpy(rdpsnd->out_buffer + (rdpsnd->out_pending_frames * rdpsnd->src_bytes_per_frame), buf, cframesize); + CopyMemory(rdpsnd->out_buffer + (rdpsnd->out_pending_frames * rdpsnd->src_bytes_per_frame), buf, cframesize); buf = (BYTE*) buf + cframesize; nframes -= cframes; rdpsnd->out_pending_frames += cframes; @@ -480,11 +490,8 @@ void rdpsnd_server_context_free(rdpsnd_server_context* context) { rdpsnd_server* rdpsnd = (rdpsnd_server*) context; - if (rdpsnd->rdpsnd_channel_thread) - { - freerdp_thread_stop(rdpsnd->rdpsnd_channel_thread); - freerdp_thread_free(rdpsnd->rdpsnd_channel_thread); - } + SetEvent(rdpsnd->StopEvent); + WaitForSingleObject(rdpsnd->thread, INFINITE); if (rdpsnd->rdpsnd_channel) WTSVirtualChannelClose(rdpsnd->rdpsnd_channel); diff --git a/channels/server/channels.c b/channels/server/channels.c index 5f9865c78..c83ed7cd1 100644 --- a/channels/server/channels.c +++ b/channels/server/channels.c @@ -104,7 +104,7 @@ static void wts_queue_receive_data(rdpPeerChannel* channel, const BYTE* buffer, item->length = length; item->buffer = malloc(length); - memcpy(item->buffer, buffer, length); + CopyMemory(item->buffer, buffer, length); WaitForSingleObject(channel->mutex, INFINITE); list_enqueue(channel->receive_queue, item); @@ -128,7 +128,7 @@ static void wts_queue_send_item(rdpPeerChannel* channel, wts_data_item* item) SetEvent(vcm->send_event); } -static int wts_read_variable_uint(STREAM* s, int cbLen, UINT32 *val) +static int wts_read_variable_uint(STREAM* s, int cbLen, UINT32* val) { switch (cbLen) { @@ -137,11 +137,13 @@ static int wts_read_variable_uint(STREAM* s, int cbLen, UINT32 *val) return 0; stream_read_BYTE(s, *val); return 1; + case 1: if (stream_get_left(s) < 2) return 0; stream_read_UINT16(s, *val); return 2; + default: if (stream_get_left(s) < 4) return 0; @@ -396,6 +398,7 @@ static int WTSReceiveChannelData(freerdp_peer* client, int channelId, BYTE* data if (client->settings->ChannelDefArray[i].ChannelId == channelId) break; } + if (i < client->settings->ChannelCount) { channel = (rdpPeerChannel*) client->settings->ChannelDefArray[i].handle; @@ -646,7 +649,7 @@ BOOL WTSVirtualChannelQuery( } *ppBuffer = malloc(sizeof(void*)); - memcpy(*ppBuffer, &fds[0], sizeof(void*)); + CopyMemory(*ppBuffer, &fds[0], sizeof(void*)); *pBytesReturned = sizeof(void*); result = TRUE; break; @@ -665,18 +668,21 @@ BOOL WTSVirtualChannelQuery( bval = FALSE; result = TRUE; break; + case DVC_OPEN_STATE_SUCCEEDED: bval = TRUE; result = TRUE; break; + default: bval = FALSE; result = FALSE; break; } } + *ppBuffer = malloc(sizeof(BOOL)); - memcpy(*ppBuffer, &bval, sizeof(BOOL)); + CopyMemory(*ppBuffer, &bval, sizeof(BOOL)); *pBytesReturned = sizeof(BOOL); break; @@ -725,7 +731,7 @@ BOOL WTSVirtualChannelRead( ReleaseMutex(channel->mutex); - memcpy(Buffer, item->buffer, item->length); + CopyMemory(Buffer, item->buffer, item->length); wts_data_item_free(item) ; return TRUE; @@ -755,7 +761,7 @@ BOOL WTSVirtualChannelWrite( item->buffer = malloc(Length); item->length = Length; - memcpy(item->buffer, Buffer, Length); + CopyMemory(item->buffer, Buffer, Length); wts_queue_send_item(channel, item); }