FreeRDP/server/Windows/wf_info.c

303 lines
7.4 KiB
C
Raw Normal View History

2012-08-18 22:34:24 +04:00
/**
* FreeRDP: A Remote Desktop Protocol Client
* FreeRDP Windows Server
*
* Copyright 2012 Corey Clayton <can.of.tuna@gmail.com>
*
* Licensed 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 <stdlib.h>
#include <winpr/tchar.h>
#include <winpr/windows.h>
#include "wf_info.h"
#include "wf_mirage.h"
wfInfo * wf_info_init(wfInfo * info)
{
if(!info)
{
info = (wfInfo*)malloc(sizeof(wfInfo)); //free this on shutdown
memset(info, 0, sizeof(wfInfo));
info->mutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (info->mutex == NULL)
{
_tprintf(_T("CreateMutex error: %d\n"), GetLastError());
}
info->encodeMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (info->encodeMutex == NULL)
{
_tprintf(_T("CreateMutex error: %d\n"), GetLastError());
}
info->can_send_mutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (info->can_send_mutex == NULL)
{
_tprintf(_T("CreateMutex error: %d\n"), GetLastError());
}
}
return info;
}
void wf_info_mirror_init(wfInfo * info, wfPeerContext* context)
{
DWORD dRes;
dRes = WaitForSingleObject(
info->mutex, // handle to mutex
INFINITE); // no time-out interval
switch(dRes)
{
case WAIT_OBJECT_0:
if(info->subscribers == 0)
{
//only the first peer needs to call this.
context->wfInfo = info;
wf_check_disp_devices(context->wfInfo);
wf_disp_device_set_attatch(context->wfInfo, 1);
wf_update_mirror_drv(context->wfInfo, 0);
wf_map_mirror_mem(context->wfInfo);
context->rfx_context = rfx_context_new();
context->rfx_context->mode = RLGR3;
context->rfx_context->width = context->wfInfo->width;
context->rfx_context->height = context->wfInfo->height;
rfx_context_set_pixel_format(context->rfx_context, RDP_PIXEL_FORMAT_B8G8R8A8);
context->s = stream_new(65536);
printf("Start Encoder\n");
ReleaseMutex(info->encodeMutex);
2012-08-18 22:34:24 +04:00
}
++info->subscribers;
if (! ReleaseMutex(info->mutex))
{
_tprintf(_T("Error releasing mutex\n"));
}
break;
default:
_tprintf(_T("Error waiting for mutex: %d\n"), dRes);
}
}
//todo: i think i can replace all the context->info here with info
2012-08-21 02:19:17 +04:00
//in fact it may not even care about subscribers
2012-08-18 22:34:24 +04:00
void wf_info_subscriber_release(wfInfo* info, wfPeerContext* context)
{
DWORD dRes;
2012-08-18 22:34:24 +04:00
WaitForSingleObject(info->mutex, INFINITE);
if (context && (info->subscribers == 1))
{
2012-08-21 02:19:17 +04:00
dRes = WaitForSingleObject(info->encodeMutex, INFINITE);
switch (dRes)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
2012-08-21 02:19:17 +04:00
--info->subscribers;
//only the last peer needs to call this
wf_mirror_cleanup(context->wfInfo);
wf_disp_device_set_attatch(context->wfInfo, 0);
wf_update_mirror_drv(context->wfInfo, 1);
stream_free(context->s);
rfx_context_free(context->rfx_context);
printf("Stop encoder\n");
break;
// The thread got ownership of an abandoned mutex
// The database is in an indeterminate state
default:
2012-08-21 02:19:17 +04:00
printf("wf_info_subscriber_release: Something else happened!!! dRes = %d\n", dRes);
}
}
2012-08-18 22:34:24 +04:00
else
{
--info->subscribers;
}
ReleaseMutex(info->mutex);
/***************
Note: if we released the last subscriber,
block the encoder until next subscriber
***************/
}
BOOL wf_info_has_subscribers(wfInfo* info)
{
int subs;
WaitForSingleObject(info->mutex, INFINITE);
subs = info->subscribers;
2012-08-18 22:34:24 +04:00
ReleaseMutex(info->mutex);
if(info->subscribers > 0)
return true;
return false;
}
BOOL wf_info_have_updates(wfInfo* info)
{
BOOL ret;
ret = true;
WaitForSingleObject(info->mutex, INFINITE);
if(info->nextUpdate == info->lastUpdate)
ret = false;
ReleaseMutex(info->mutex);
return ret;
}
void wf_info_updated(wfInfo* info)
{
WaitForSingleObject(info->mutex, INFINITE);
info->lastUpdate = info->nextUpdate;
ReleaseMutex(info->mutex);
}
void wf_info_update_changes(wfInfo* info)
{
GETCHANGESBUF* buf;
WaitForSingleObject(info->mutex, INFINITE);
buf = (GETCHANGESBUF*)info->changeBuffer;
info->nextUpdate = buf->buffer->counter;
ReleaseMutex(info->mutex);
}
void wf_info_find_invalid_region(wfInfo* info)
{
int i;
GETCHANGESBUF* buf;
WaitForSingleObject(info->mutex, INFINITE);
buf = (GETCHANGESBUF*)info->changeBuffer;
if(info->enc_data == false)
{
info->invalid_x1 = 1920;//info->width;
info->invalid_x2 = 0;
info->invalid_y1 = 1200;// info->height;
info->invalid_y2 = 0;
}
//printf("\tFIND = (%d, %d), (%d, %d)\n", info->invalid_x1, info->invalid_y1, info->invalid_x2, info->invalid_y2);
2012-08-20 19:45:48 +04:00
for(i = info->lastUpdate; i != info->nextUpdate; i = (i+1) % MAXCHANGES_BUF )
{
/*printf("\t(%d, %d), (%d, %d)\n",
buf->buffer->pointrect[i].rect.left,
buf->buffer->pointrect[i].rect.top,
buf->buffer->pointrect[i].rect.right,
buf->buffer->pointrect[i].rect.bottom);
*/
info->invalid_x1 = min(info->invalid_x1, buf->buffer->pointrect[i].rect.left);
info->invalid_x2 = max(info->invalid_x2, buf->buffer->pointrect[i].rect.right);
info->invalid_y1 = min(info->invalid_y1, buf->buffer->pointrect[i].rect.top);
info->invalid_y2 = max(info->invalid_y2, buf->buffer->pointrect[i].rect.bottom);
}
ReleaseMutex(info->mutex);
}
void wf_info_clear_invalid_region(wfInfo* info)
{
WaitForSingleObject(info->mutex, INFINITE);
info->lastUpdate = info->nextUpdate;
info->invalid_x1 = info->width;
info->invalid_x2 = 0;
info->invalid_y1 = info->height;
info->invalid_y2 = 0;
ReleaseMutex(info->mutex);
}
BOOL wf_info_have_invalid_region(wfInfo* info)
{
if((info->invalid_x1 >= info->invalid_x2) || (info->invalid_y1 >= info->invalid_y2))
return false;
return true;
}
int wf_info_get_height(wfInfo* info)
{
int ret;
WaitForSingleObject(info->mutex, INFINITE);
ret = info->height;
ReleaseMutex(info->mutex);
return ret;
}
int wf_info_get_width(wfInfo* info)
{
int ret;
WaitForSingleObject(info->mutex, INFINITE);
ret = info->width;
ReleaseMutex(info->mutex);
return ret;
2012-08-21 02:19:17 +04:00
}
int wf_info_get_thread_count(wfInfo* info)
{
int ret;
WaitForSingleObject(info->mutex, INFINITE);
ret = info->threadCnt;
ReleaseMutex(info->mutex);
return ret;
}
void wf_info_set_thread_count(wfInfo* info, int count)
{
WaitForSingleObject(info->mutex, INFINITE);
info->threadCnt = count;
ReleaseMutex(info->mutex);
}