Add ADPCM codec for Mac client

This commit is contained in:
David PHAM-VAN 2016-01-25 13:53:19 -08:00
parent e83f97b64a
commit 8ef64aee96
1 changed files with 85 additions and 16 deletions

View File

@ -5,6 +5,8 @@
* Copyright 2012 Laxmikant Rashinkar <LK.Rashinkar@gmail.com> * Copyright 2012 Laxmikant Rashinkar <LK.Rashinkar@gmail.com>
* Copyright 2015 Thincast Technologies GmbH * Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
* Copyright 2016 Inuvika Inc.
* Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,6 +56,12 @@ struct rdpsnd_mac_plugin
AudioQueueRef audioQueue; AudioQueueRef audioQueue;
AudioStreamBasicDescription audioFormat; AudioStreamBasicDescription audioFormat;
AudioQueueBufferRef audioBuffers[MAC_AUDIO_QUEUE_NUM_BUFFERS]; AudioQueueBufferRef audioBuffers[MAC_AUDIO_QUEUE_NUM_BUFFERS];
Float64 lastStartTime;
int wformat;
int block_size;
FREERDP_DSP_CONTEXT* dsp_context;
}; };
typedef struct rdpsnd_mac_plugin rdpsndMacPlugin; typedef struct rdpsnd_mac_plugin rdpsndMacPlugin;
@ -69,6 +77,15 @@ static BOOL rdpsnd_mac_set_format(rdpsndDevicePlugin* device, AUDIO_FORMAT* form
mac->latency = (UINT32) latency; mac->latency = (UINT32) latency;
CopyMemory(&(mac->format), format, sizeof(AUDIO_FORMAT)); CopyMemory(&(mac->format), format, sizeof(AUDIO_FORMAT));
mac->audioFormat.mSampleRate = format->nSamplesPerSec;
mac->audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
mac->audioFormat.mFramesPerPacket = 1;
mac->audioFormat.mChannelsPerFrame = format->nChannels;
mac->audioFormat.mBitsPerChannel = format->wBitsPerSample;
mac->audioFormat.mBytesPerFrame = (format->wBitsPerSample * format->nChannels) / 8;
mac->audioFormat.mBytesPerPacket = format->nBlockAlign;
mac->audioFormat.mReserved = 0;
switch (format->wFormatTag) switch (format->wFormatTag)
{ {
case WAVE_FORMAT_ALAW: case WAVE_FORMAT_ALAW:
@ -83,6 +100,14 @@ static BOOL rdpsnd_mac_set_format(rdpsndDevicePlugin* device, AUDIO_FORMAT* form
mac->audioFormat.mFormatID = kAudioFormatLinearPCM; mac->audioFormat.mFormatID = kAudioFormatLinearPCM;
break; break;
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_DVI_ADPCM:
mac->audioFormat.mFormatID = kAudioFormatLinearPCM;
mac->audioFormat.mBitsPerChannel = 16;
mac->audioFormat.mBytesPerFrame = (16 * format->nChannels) / 8;
mac->audioFormat.mBytesPerPacket = mac->audioFormat.mFramesPerPacket * mac->audioFormat.mBytesPerFrame;
break;
case WAVE_FORMAT_GSM610: case WAVE_FORMAT_GSM610:
mac->audioFormat.mFormatID = kAudioFormatMicrosoftGSM; mac->audioFormat.mFormatID = kAudioFormatMicrosoftGSM;
break; break;
@ -91,14 +116,8 @@ static BOOL rdpsnd_mac_set_format(rdpsndDevicePlugin* device, AUDIO_FORMAT* form
break; break;
} }
mac->audioFormat.mSampleRate = format->nSamplesPerSec; mac->wformat = format->wFormatTag;
mac->audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked; mac->block_size = format->nBlockAlign;
mac->audioFormat.mFramesPerPacket = 1;
mac->audioFormat.mChannelsPerFrame = format->nChannels;
mac->audioFormat.mBitsPerChannel = format->wBitsPerSample;
mac->audioFormat.mBytesPerFrame = (format->wBitsPerSample * format->nChannels) / 8;
mac->audioFormat.mBytesPerPacket = format->nBlockAlign;
mac->audioFormat.mReserved = 0;
rdpsnd_print_audio_format(format); rdpsnd_print_audio_format(format);
return TRUE; return TRUE;
@ -122,6 +141,8 @@ static BOOL rdpsnd_mac_open(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, in
return FALSE; return FALSE;
} }
freerdp_dsp_context_reset_adpcm(mac->dsp_context);
status = AudioQueueNewOutput(&(mac->audioFormat), status = AudioQueueNewOutput(&(mac->audioFormat),
mac_audio_queue_output_cb, mac, mac_audio_queue_output_cb, mac,
NULL, NULL, 0, &(mac->audioQueue)); NULL, NULL, 0, &(mac->audioQueue));
@ -157,6 +178,8 @@ static BOOL rdpsnd_mac_open(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, in
} }
} }
mac->lastStartTime = 0;
mac->isOpen = TRUE; mac->isOpen = TRUE;
return TRUE; return TRUE;
} }
@ -184,6 +207,8 @@ static void rdpsnd_mac_free(rdpsndDevicePlugin* device)
device->Close(device); device->Close(device);
freerdp_dsp_context_free(mac->dsp_context);
free(mac); free(mac);
} }
@ -205,6 +230,10 @@ static BOOL rdpsnd_mac_format_supported(rdpsndDevicePlugin* device, AUDIO_FORMAT
{ {
return FALSE; return FALSE;
} }
else if (format->wFormatTag == WAVE_FORMAT_ADPCM || format->wFormatTag == WAVE_FORMAT_DVI_ADPCM)
{
return TRUE;
}
return FALSE; return FALSE;
} }
@ -258,10 +287,42 @@ static void rdpsnd_mac_start(rdpsndDevicePlugin* device)
} }
} }
static void rdpsnd_mac_play(rdpsndDevicePlugin* device, BYTE* data, int size) static BOOL rdpsnd_mac_wave_decode(rdpsndDevicePlugin* device, RDPSND_WAVE* wave)
{
int length;
BYTE* data;
rdpsndMacPlugin* mac = (rdpsndMacPlugin*) device;
if (mac->wformat == WAVE_FORMAT_ADPCM)
{
mac->dsp_context->decode_ms_adpcm(mac->dsp_context, wave->data, wave->length, mac->format.nChannels, mac->block_size);
length = mac->dsp_context->adpcm_size;
data = mac->dsp_context->adpcm_buffer;
}
else if (mac->wformat == WAVE_FORMAT_DVI_ADPCM)
{
mac->dsp_context->decode_ima_adpcm(mac->dsp_context, wave->data, wave->length, mac->format.nChannels, mac->block_size);
length = mac->dsp_context->adpcm_size;
data = mac->dsp_context->adpcm_buffer;
}
else
{
length = wave->length;
data = wave->data;
}
wave->data = (BYTE*) malloc(length);
CopyMemory(wave->data, data, length);
wave->length = length;
return TRUE;
}
static void rdpsnd_mac_waveplay(rdpsndDevicePlugin* device, RDPSND_WAVE* wave)
{ {
int length; int length;
AudioQueueBufferRef audioBuffer; AudioQueueBufferRef audioBuffer;
AudioTimeStamp outActualStartTime;
rdpsndMacPlugin* mac = (rdpsndMacPlugin*) device; rdpsndMacPlugin* mac = (rdpsndMacPlugin*) device;
if (!mac->isOpen) if (!mac->isOpen)
@ -269,12 +330,17 @@ static void rdpsnd_mac_play(rdpsndDevicePlugin* device, BYTE* data, int size)
audioBuffer = mac->audioBuffers[mac->audioBufferIndex]; audioBuffer = mac->audioBuffers[mac->audioBufferIndex];
length = size > audioBuffer->mAudioDataBytesCapacity ? audioBuffer->mAudioDataBytesCapacity : size; length = wave->length > audioBuffer->mAudioDataBytesCapacity ? audioBuffer->mAudioDataBytesCapacity : wave->length;
CopyMemory(audioBuffer->mAudioData, data, length); CopyMemory(audioBuffer->mAudioData, wave->data, length);
audioBuffer->mAudioDataByteSize = length; audioBuffer->mAudioDataByteSize = length;
audioBuffer->mUserData = wave;
AudioQueueEnqueueBuffer(mac->audioQueue, audioBuffer, 0, 0); AudioQueueEnqueueBufferWithParameters(mac->audioQueue, audioBuffer, 0, 0, 0, 0, 0, NULL, NULL, &outActualStartTime);
UInt64 startTimeDelta = (outActualStartTime.mSampleTime - mac->lastStartTime) / 100.0;
wave->wLocalTimeB = wave->wLocalTimeA + startTimeDelta + wave->wAudioLength;
wave->wTimeStampB = wave->wTimeStampA + wave->wLocalTimeB - wave->wLocalTimeA;
mac->lastStartTime = outActualStartTime.mSampleTime;
mac->audioBufferIndex++; mac->audioBufferIndex++;
@ -308,11 +374,14 @@ UINT freerdp_rdpsnd_client_subsystem_entry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS p
mac->device.FormatSupported = rdpsnd_mac_format_supported; mac->device.FormatSupported = rdpsnd_mac_format_supported;
mac->device.SetFormat = rdpsnd_mac_set_format; mac->device.SetFormat = rdpsnd_mac_set_format;
mac->device.SetVolume = rdpsnd_mac_set_volume; mac->device.SetVolume = rdpsnd_mac_set_volume;
mac->device.Play = rdpsnd_mac_play; mac->device.WaveDecode = rdpsnd_mac_wave_decode;
mac->device.WavePlay = rdpsnd_mac_waveplay;
mac->device.Start = rdpsnd_mac_start; mac->device.Start = rdpsnd_mac_start;
mac->device.Close = rdpsnd_mac_close; mac->device.Close = rdpsnd_mac_close;
mac->device.Free = rdpsnd_mac_free; mac->device.Free = rdpsnd_mac_free;
mac->dsp_context = freerdp_dsp_context_new();
pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*) mac); pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*) mac);
return CHANNEL_RC_OK; return CHANNEL_RC_OK;