FreeRDP/server/Windows/wf_rdpsnd.c

155 lines
3.6 KiB
C
Raw Normal View History

2012-10-11 04:44:27 +04:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* FreeRDP Windows Server (Audio Output)
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2013 Corey Clayton <can.of.tuna@gmail.com>
2012-10-11 04:44:27 +04:00
*
* 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.
*/
2022-02-16 13:20:38 +03:00
#include <freerdp/config.h>
2012-10-11 04:44:27 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <winpr/windows.h>
2018-09-25 18:05:12 +03:00
#include <freerdp/server/server-common.h>
2012-10-11 04:44:27 +04:00
#include "wf_rdpsnd.h"
#include "wf_info.h"
#ifdef WITH_RDPSND_DSOUND
#include "wf_directsound.h"
#else
#include "wf_wasapi.h"
#endif
#include <freerdp/log.h>
#define TAG SERVER_TAG("windows")
static void wf_peer_rdpsnd_activated(RdpsndServerContext* context)
2012-10-11 04:44:27 +04:00
{
wfInfo* wfi;
size_t i;
wfi = wf_info_get_instance();
wfi->agreed_format = NULL;
2014-09-12 19:38:12 +04:00
WLog_DBG(TAG, "Client supports the following %d formats:", context->num_client_formats);
for (i = 0; i < context->num_client_formats; i++)
{
size_t j;
2019-11-06 17:24:51 +03:00
// TODO: improve the way we agree on a format
for (j = 0; j < context->num_server_formats; j++)
{
if ((context->client_formats[i].wFormatTag == context->server_formats[j].wFormatTag) &&
(context->client_formats[i].nChannels == context->server_formats[j].nChannels) &&
2019-11-06 17:24:51 +03:00
(context->client_formats[i].nSamplesPerSec ==
context->server_formats[j].nSamplesPerSec))
{
2014-09-12 19:38:12 +04:00
WLog_DBG(TAG, "agreed on format!");
2019-11-06 17:24:51 +03:00
wfi->agreed_format = (AUDIO_FORMAT*)&context->server_formats[j];
break;
}
}
2018-09-25 18:05:12 +03:00
if (wfi->agreed_format != NULL)
break;
}
2018-09-25 18:05:12 +03:00
if (wfi->agreed_format == NULL)
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Could not agree on a audio format with the server");
return;
}
2018-09-25 18:05:12 +03:00
context->SelectFormat(context, i);
context->SetVolume(context, 0x7FFF, 0x7FFF);
#ifdef WITH_RDPSND_DSOUND
wf_directsound_activate(context);
#else
2013-02-16 03:50:00 +04:00
wf_wasapi_activate(context);
#endif
}
int wf_rdpsnd_lock()
{
DWORD dRes;
wfInfo* wfi;
wfi = wf_info_get_instance();
dRes = WaitForSingleObject(wfi->snd_mutex, INFINITE);
switch (dRes)
{
2018-09-25 18:05:12 +03:00
case WAIT_ABANDONED:
case WAIT_OBJECT_0:
return TRUE;
break;
2018-09-25 18:05:12 +03:00
case WAIT_TIMEOUT:
return FALSE;
break;
2018-09-25 18:05:12 +03:00
case WAIT_FAILED:
WLog_ERR(TAG, "wf_rdpsnd_lock failed with 0x%08lX", GetLastError());
return -1;
break;
}
return -1;
}
int wf_rdpsnd_unlock()
{
wfInfo* wfi;
wfi = wf_info_get_instance();
if (ReleaseMutex(wfi->snd_mutex) == 0)
{
WLog_DBG(TAG, "wf_rdpsnd_unlock failed with 0x%08lX", GetLastError());
return -1;
}
return TRUE;
2012-10-11 04:44:27 +04:00
}
BOOL wf_peer_rdpsnd_init(wfPeerContext* context)
{
2015-05-05 18:25:17 +03:00
wfInfo* wfi = wf_info_get_instance();
2018-09-25 18:05:12 +03:00
2015-05-05 18:25:17 +03:00
if (!wfi)
return FALSE;
if (!(wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL)))
return FALSE;
2012-10-11 04:44:27 +04:00
context->rdpsnd = rdpsnd_server_context_new(context->vcm);
context->rdpsnd->rdpcontext = &context->_p;
2012-10-11 04:44:27 +04:00
context->rdpsnd->data = context;
2019-11-06 17:24:51 +03:00
context->rdpsnd->num_server_formats =
server_rdpsnd_get_formats(&context->rdpsnd->server_formats);
2012-10-11 04:44:27 +04:00
2018-09-25 18:05:12 +03:00
if (context->rdpsnd->num_server_formats > 0)
context->rdpsnd->src_format = &context->rdpsnd->server_formats[0];
2012-10-11 04:44:27 +04:00
context->rdpsnd->Activated = wf_peer_rdpsnd_activated;
context->rdpsnd->Initialize(context->rdpsnd, TRUE);
wf_rdpsnd_set_latest_peer(context);
wfi->snd_stop = FALSE;
2012-10-11 04:44:27 +04:00
return TRUE;
}