Blame server/Windows/wf_rdpsnd.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * FreeRDP Windows Server (Audio Output)
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2013 Corey Clayton <can.of.tuna@gmail.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/windows.h>
Packit 1fb8d4
#include <freerdp/server/server-common.h>
Packit 1fb8d4
Packit 1fb8d4
#include "wf_rdpsnd.h"
Packit 1fb8d4
#include "wf_info.h"
Packit 1fb8d4
Packit 1fb8d4
#ifdef WITH_RDPSND_DSOUND
Packit 1fb8d4
Packit 1fb8d4
#include "wf_directsound.h"
Packit 1fb8d4
Packit 1fb8d4
#else
Packit 1fb8d4
Packit 1fb8d4
#include "wf_wasapi.h"
Packit 1fb8d4
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#define TAG SERVER_TAG("windows")
Packit 1fb8d4
Packit 1fb8d4
static void wf_peer_rdpsnd_activated(RdpsndServerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wfInfo* wfi;
Packit 1fb8d4
	int i, j;
Packit 1fb8d4
	wfi = wf_info_get_instance();
Packit 1fb8d4
	wfi->agreed_format = NULL;
Packit 1fb8d4
	WLog_DBG(TAG, "Client supports the following %d formats:", context->num_client_formats);
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < context->num_client_formats; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		//TODO: improve the way we agree on a format
Packit 1fb8d4
		for (j = 0; j < context->num_server_formats; j++)
Packit 1fb8d4
		{
Packit 1fb8d4
			if ((context->client_formats[i].wFormatTag == context->server_formats[j].wFormatTag) &&
Packit 1fb8d4
			    (context->client_formats[i].nChannels == context->server_formats[j].nChannels) &&
Packit 1fb8d4
			    (context->client_formats[i].nSamplesPerSec == context->server_formats[j].nSamplesPerSec))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_DBG(TAG, "agreed on format!");
Packit 1fb8d4
				wfi->agreed_format = (AUDIO_FORMAT*) &context->server_formats[j];
Packit 1fb8d4
				break;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (wfi->agreed_format != NULL)
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (wfi->agreed_format == NULL)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Could not agree on a audio format with the server");
Packit 1fb8d4
		return;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	context->SelectFormat(context, i);
Packit 1fb8d4
	context->SetVolume(context, 0x7FFF, 0x7FFF);
Packit 1fb8d4
#ifdef WITH_RDPSND_DSOUND
Packit 1fb8d4
	wf_directsound_activate(context);
Packit 1fb8d4
#else
Packit 1fb8d4
	wf_wasapi_activate(context);
Packit 1fb8d4
#endif
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int wf_rdpsnd_lock()
Packit 1fb8d4
{
Packit 1fb8d4
	DWORD dRes;
Packit 1fb8d4
	wfInfo* wfi;
Packit 1fb8d4
	wfi = wf_info_get_instance();
Packit 1fb8d4
	dRes = WaitForSingleObject(wfi->snd_mutex, INFINITE);
Packit 1fb8d4
Packit 1fb8d4
	switch (dRes)
Packit 1fb8d4
	{
Packit 1fb8d4
		case WAIT_ABANDONED:
Packit 1fb8d4
		case WAIT_OBJECT_0:
Packit 1fb8d4
			return TRUE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case WAIT_TIMEOUT:
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case WAIT_FAILED:
Packit 1fb8d4
			WLog_ERR(TAG, "wf_rdpsnd_lock failed with 0x%08lX", GetLastError());
Packit 1fb8d4
			return -1;
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return -1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int wf_rdpsnd_unlock()
Packit 1fb8d4
{
Packit 1fb8d4
	wfInfo* wfi;
Packit 1fb8d4
	wfi = wf_info_get_instance();
Packit 1fb8d4
Packit 1fb8d4
	if (ReleaseMutex(wfi->snd_mutex) == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_DBG(TAG, "wf_rdpsnd_unlock failed with 0x%08lX", GetLastError());
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL wf_peer_rdpsnd_init(wfPeerContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	wfInfo* wfi = wf_info_get_instance();
Packit 1fb8d4
Packit 1fb8d4
	if (!wfi)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!(wfi->snd_mutex = CreateMutex(NULL, FALSE, NULL)))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	context->rdpsnd = rdpsnd_server_context_new(context->vcm);
Packit 1fb8d4
	context->rdpsnd->rdpcontext = &context->_p;
Packit 1fb8d4
	context->rdpsnd->data = context;
Packit 1fb8d4
	context->rdpsnd->num_server_formats = server_rdpsnd_get_formats(&context->rdpsnd->server_formats);
Packit 1fb8d4
Packit 1fb8d4
	if (context->rdpsnd->num_server_formats > 0)
Packit 1fb8d4
		context->rdpsnd->src_format = &context->rdpsnd->server_formats[0];
Packit 1fb8d4
Packit 1fb8d4
	context->rdpsnd->Activated = wf_peer_rdpsnd_activated;
Packit 1fb8d4
	context->rdpsnd->Initialize(context->rdpsnd, TRUE);
Packit 1fb8d4
	wf_rdpsnd_set_latest_peer(context);
Packit 1fb8d4
	wfi->snd_stop = FALSE;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4