FreeRDP/server/Mac/mf_rdpsnd.c

241 lines
6.9 KiB
C
Raw Normal View History

2012-11-01 07:04:31 +04:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* FreeRDP Mac OS X Server (Audio Output)
*
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <freerdp/server/rdpsnd.h>
2012-11-01 07:04:31 +04:00
#include "mf_info.h"
2012-11-01 07:04:31 +04:00
#include "mf_rdpsnd.h"
AQRecorderState recorderState;
static const AUDIO_FORMAT supported_audio_formats[] =
2012-11-01 07:04:31 +04:00
{
{ WAVE_FORMAT_PCM, 2, 44100, 176400, 4, 16, NULL },
{ WAVE_FORMAT_ALAW, 2, 22050, 44100, 2, 8, NULL }
};
2013-02-20 01:26:06 +04:00
static void mf_peer_rdpsnd_activated(RdpsndServerContext* context)
{
2013-02-20 00:06:42 +04:00
OSStatus status;
int i, j;
BOOL formatAgreed = FALSE;
AUDIO_FORMAT* agreedFormat = NULL;
2013-03-29 08:15:29 +04:00
//we should actually loop through the list of client formats here
//and see if we can send the client something that it supports...
printf("Client supports the following %d formats: \n", context->num_client_formats);
2013-03-29 08:15:29 +04:00
for (i = 0; i < context->num_client_formats; i++)
{
2013-03-29 08:15:29 +04: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) &&
(context->client_formats[i].nSamplesPerSec == context->server_formats[j].nSamplesPerSec))
{
printf("agreed on format!\n");
formatAgreed = TRUE;
agreedFormat = (AUDIO_FORMAT*)&context->server_formats[j];
break;
}
}
if (formatAgreed == TRUE)
break;
}
if (formatAgreed == FALSE)
{
printf("Could not agree on a audio format with the server\n");
return;
}
context->SelectFormat(context, i);
context->SetVolume(context, 0x7FFF, 0x7FFF);
switch (agreedFormat->wFormatTag)
{
case WAVE_FORMAT_ALAW:
recorderState.dataFormat.mFormatID = kAudioFormatDVIIntelIMA;
break;
case WAVE_FORMAT_PCM:
recorderState.dataFormat.mFormatID = kAudioFormatLinearPCM;
break;
default:
recorderState.dataFormat.mFormatID = kAudioFormatLinearPCM;
break;
}
2013-02-20 00:06:42 +04:00
recorderState.dataFormat.mSampleRate = agreedFormat->nSamplesPerSec;
recorderState.dataFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;;
recorderState.dataFormat.mBytesPerPacket = 4;
recorderState.dataFormat.mFramesPerPacket = 1;
recorderState.dataFormat.mBytesPerFrame = 4;
recorderState.dataFormat.mChannelsPerFrame = agreedFormat->nChannels;
recorderState.dataFormat.mBitsPerChannel = agreedFormat->wBitsPerSample;
2013-02-20 00:06:42 +04:00
recorderState.snd_context = context;
status = AudioQueueNewInput(&recorderState.dataFormat,
mf_peer_rdpsnd_input_callback,
&recorderState,
NULL,
kCFRunLoopCommonModes,
0,
&recorderState.queue);
if (status != noErr)
{
printf("Failed to create a new Audio Queue. Status code: %d\n", status);
}
UInt32 dataFormatSize = sizeof (recorderState.dataFormat);
AudioQueueGetProperty(recorderState.queue,
kAudioConverterCurrentInputStreamDescription,
&recorderState.dataFormat,
&dataFormatSize);
mf_rdpsnd_derive_buffer_size(recorderState.queue, &recorderState.dataFormat, 0.05, &recorderState.bufferByteSize);
2013-02-20 01:26:06 +04:00
for (i = 0; i < SND_NUMBUFFERS; ++i)
2013-02-20 00:06:42 +04:00
{
AudioQueueAllocateBuffer(recorderState.queue,
recorderState.bufferByteSize,
&recorderState.buffers[i]);
AudioQueueEnqueueBuffer(recorderState.queue,
recorderState.buffers[i],
0,
NULL);
}
recorderState.currentPacket = 0;
recorderState.isRunning = true;
AudioQueueStart (recorderState.queue, NULL);
2012-11-01 07:04:31 +04:00
}
BOOL mf_peer_rdpsnd_init(mfPeerContext* context)
{
context->rdpsnd = rdpsnd_server_context_new(context->vcm);
context->rdpsnd->data = context;
2013-02-20 00:06:42 +04:00
context->rdpsnd->server_formats = supported_audio_formats;
context->rdpsnd->num_server_formats = sizeof(supported_audio_formats) / sizeof(supported_audio_formats[0]);
2013-02-20 00:06:42 +04:00
2012-11-01 07:04:31 +04:00
context->rdpsnd->src_format.wFormatTag = 1;
context->rdpsnd->src_format.nChannels = 2;
context->rdpsnd->src_format.nSamplesPerSec = 44100;
context->rdpsnd->src_format.wBitsPerSample = 16;
2013-02-20 00:06:42 +04:00
2012-11-01 07:04:31 +04:00
context->rdpsnd->Activated = mf_peer_rdpsnd_activated;
2013-02-20 00:06:42 +04:00
2012-11-01 07:04:31 +04:00
context->rdpsnd->Initialize(context->rdpsnd);
2013-02-20 00:06:42 +04:00
2012-11-01 07:04:31 +04:00
return TRUE;
}
BOOL mf_peer_rdpsnd_stop()
{
2013-02-20 00:06:42 +04:00
recorderState.isRunning = false;
AudioQueueStop(recorderState.queue, true);
return TRUE;
}
void mf_peer_rdpsnd_input_callback (void *inUserData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer,
const AudioTimeStamp *inStartTime,
UInt32 inNumberPacketDescriptions,
const AudioStreamPacketDescription *inPacketDescs)
{
2013-02-20 00:06:42 +04:00
OSStatus status;
AQRecorderState * rState;
rState = inUserData;
if (inNumberPacketDescriptions == 0 && rState->dataFormat.mBytesPerPacket != 0)
{
inNumberPacketDescriptions = inBuffer->mAudioDataByteSize / rState->dataFormat.mBytesPerPacket;
}
if (rState->isRunning == 0)
{
return ;
}
rState->snd_context->SendSamples(rState->snd_context, inBuffer->mAudioData,
inBuffer->mAudioDataByteSize/4, (UINT16)(GetTickCount() & 0xffff));
2013-02-20 00:06:42 +04:00
status = AudioQueueEnqueueBuffer(
rState->queue,
inBuffer,
0,
NULL);
if (status != noErr)
{
printf("AudioQueueEnqueueBuffer() returned status = %d\n", status);
}
}
void mf_rdpsnd_derive_buffer_size (AudioQueueRef audioQueue,
AudioStreamBasicDescription *ASBDescription,
Float64 seconds,
UInt32 *outBufferSize)
{
2013-02-20 00:06:42 +04:00
static const int maxBufferSize = 0x50000;
int maxPacketSize = ASBDescription->mBytesPerPacket;
if (maxPacketSize == 0)
{
UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
AudioQueueGetProperty (audioQueue,
kAudioQueueProperty_MaximumOutputPacketSize,
// in Mac OS X v10.5, instead use
// kAudioConverterPropertyMaximumOutputPacketSize
&maxPacketSize,
&maxVBRPacketSize
);
}
Float64 numBytesForTime =
ASBDescription->mSampleRate * maxPacketSize * seconds;
*outBufferSize = (UInt32) (numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize);
}