summaryrefslogtreecommitdiffstats
path: root/channels/audin/client/opensles
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
commita9bcc81f821d7c66f623779fa5147e728eb3c388 (patch)
tree98676963bcdd537ae5908a067a8eb110b93486a6 /channels/audin/client/opensles
parentInitial commit. (diff)
downloadfreerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.tar.xz
freerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.zip
Adding upstream version 3.3.0+dfsg1.upstream/3.3.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'channels/audin/client/opensles')
-rw-r--r--channels/audin/client/opensles/CMakeLists.txt36
-rw-r--r--channels/audin/client/opensles/audin_opensl_es.c336
-rw-r--r--channels/audin/client/opensles/opensl_io.c388
-rw-r--r--channels/audin/client/opensles/opensl_io.h65
4 files changed, 825 insertions, 0 deletions
diff --git a/channels/audin/client/opensles/CMakeLists.txt b/channels/audin/client/opensles/CMakeLists.txt
new file mode 100644
index 0000000..9b2a287
--- /dev/null
+++ b/channels/audin/client/opensles/CMakeLists.txt
@@ -0,0 +1,36 @@
+# FreeRDP: A Remote Desktop Protocol Implementation
+# FreeRDP cmake build script
+#
+# Copyright 2013 Armin Novak <armin.novak@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.
+
+define_channel_client_subsystem("audin" "opensles" "")
+
+find_package(OpenSLES REQUIRED)
+
+set(${MODULE_PREFIX}_SRCS
+ opensl_io.c
+ audin_opensl_es.c
+)
+
+set(${MODULE_PREFIX}_LIBS
+ winpr
+ freerdp
+ ${OpenSLES_LIBRARIES}
+)
+
+include_directories(..)
+include_directories(${OpenSLES_INCLUDE_DIRS})
+
+add_channel_client_subsystem_library(${MODULE_PREFIX} ${MODULE_NAME} ${CHANNEL_NAME} "" TRUE "")
diff --git a/channels/audin/client/opensles/audin_opensl_es.c b/channels/audin/client/opensles/audin_opensl_es.c
new file mode 100644
index 0000000..a59fe05
--- /dev/null
+++ b/channels/audin/client/opensles/audin_opensl_es.c
@@ -0,0 +1,336 @@
+/**
+ * FreeRDP: A Remote Desktop Protocol Implementation
+ * Audio Input Redirection Virtual Channel - OpenSL ES implementation
+ *
+ * Copyright 2013 Armin Novak <armin.novak@gmail.com>
+ * Copyright 2015 Thincast Technologies GmbH
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.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 <freerdp/config.h>
+
+#include <winpr/assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <winpr/crt.h>
+#include <winpr/cmdline.h>
+
+#include <freerdp/freerdp.h>
+#include <freerdp/addin.h>
+#include <freerdp/channels/rdpsnd.h>
+
+#include <SLES/OpenSLES.h>
+#include <freerdp/client/audin.h>
+
+#include "audin_main.h"
+#include "opensl_io.h"
+
+typedef struct
+{
+ IAudinDevice iface;
+
+ char* device_name;
+ OPENSL_STREAM* stream;
+
+ AUDIO_FORMAT format;
+ UINT32 frames_per_packet;
+
+ UINT32 bytes_per_channel;
+
+ AudinReceive receive;
+
+ void* user_data;
+
+ rdpContext* rdpcontext;
+ wLog* log;
+} AudinOpenSLESDevice;
+
+static UINT audin_opensles_close(IAudinDevice* device);
+
+static void audin_receive(void* context, const void* data, size_t size)
+{
+ UINT error;
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)context;
+
+ if (!opensles || !data)
+ {
+ WLog_ERR(TAG, "Invalid arguments context=%p, data=%p", opensles, data);
+ return;
+ }
+
+ error = opensles->receive(&opensles->format, data, size, opensles->user_data);
+
+ if (error && opensles->rdpcontext)
+ setChannelError(opensles->rdpcontext, error, "audin_receive reported an error");
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT audin_opensles_free(IAudinDevice* device)
+{
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+
+ if (!opensles)
+ return ERROR_INVALID_PARAMETER;
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p", (void*)device);
+
+ free(opensles->device_name);
+ free(opensles);
+ return CHANNEL_RC_OK;
+}
+
+static BOOL audin_opensles_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
+{
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+
+ if (!opensles || !format)
+ return FALSE;
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p, format=%p", (void*)opensles, (void*)format);
+ WINPR_ASSERT(format);
+
+ switch (format->wFormatTag)
+ {
+ case WAVE_FORMAT_PCM: /* PCM */
+ if (format->cbSize == 0 && (format->nSamplesPerSec <= 48000) &&
+ (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
+ (format->nChannels >= 1 && format->nChannels <= 2))
+ {
+ return TRUE;
+ }
+
+ break;
+
+ default:
+ WLog_Print(opensles->log, WLOG_DEBUG, "Encoding '%s' [0x%04" PRIX16 "] not supported",
+ audio_format_get_tag_string(format->wFormatTag), format->wFormatTag);
+ break;
+ }
+
+ return FALSE;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT audin_opensles_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
+ UINT32 FramesPerPacket)
+{
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+
+ if (!opensles || !format)
+ return ERROR_INVALID_PARAMETER;
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p, format=%p, FramesPerPacket=%" PRIu32 "",
+ (void*)device, (void*)format, FramesPerPacket);
+ WINPR_ASSERT(format);
+
+ opensles->format = *format;
+
+ switch (format->wFormatTag)
+ {
+ case WAVE_FORMAT_PCM:
+ opensles->frames_per_packet = FramesPerPacket;
+
+ switch (format->wBitsPerSample)
+ {
+ case 4:
+ opensles->bytes_per_channel = 1;
+ break;
+
+ case 8:
+ opensles->bytes_per_channel = 1;
+ break;
+
+ case 16:
+ opensles->bytes_per_channel = 2;
+ break;
+
+ default:
+ return ERROR_UNSUPPORTED_TYPE;
+ }
+
+ break;
+
+ default:
+ WLog_Print(opensles->log, WLOG_ERROR,
+ "Encoding '%" PRIu16 "' [%04" PRIX16 "] not supported", format->wFormatTag,
+ format->wFormatTag);
+ return ERROR_UNSUPPORTED_TYPE;
+ }
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "frames_per_packet=%" PRIu32,
+ opensles->frames_per_packet);
+ return CHANNEL_RC_OK;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT audin_opensles_open(IAudinDevice* device, AudinReceive receive, void* user_data)
+{
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+
+ if (!opensles || !receive || !user_data)
+ return ERROR_INVALID_PARAMETER;
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p, receive=%p, user_data=%p", (void*)device,
+ (void*)receive, (void*)user_data);
+
+ if (opensles->stream)
+ goto error_out;
+
+ if (!(opensles->stream = android_OpenRecDevice(
+ opensles, audin_receive, opensles->format.nSamplesPerSec, opensles->format.nChannels,
+ opensles->frames_per_packet, opensles->format.wBitsPerSample)))
+ {
+ WLog_Print(opensles->log, WLOG_ERROR, "android_OpenRecDevice failed!");
+ goto error_out;
+ }
+
+ opensles->receive = receive;
+ opensles->user_data = user_data;
+ return CHANNEL_RC_OK;
+error_out:
+ audin_opensles_close(device);
+ return ERROR_INTERNAL_ERROR;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+UINT audin_opensles_close(IAudinDevice* device)
+{
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+
+ if (!opensles)
+ return ERROR_INVALID_PARAMETER;
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p", (void*)device);
+ android_CloseRecDevice(opensles->stream);
+ opensles->receive = NULL;
+ opensles->user_data = NULL;
+ opensles->stream = NULL;
+ return CHANNEL_RC_OK;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+static UINT audin_opensles_parse_addin_args(AudinOpenSLESDevice* device, const ADDIN_ARGV* args)
+{
+ UINT status;
+ DWORD flags;
+ const COMMAND_LINE_ARGUMENT_A* arg;
+ AudinOpenSLESDevice* opensles = (AudinOpenSLESDevice*)device;
+ COMMAND_LINE_ARGUMENT_A audin_opensles_args[] = {
+ { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL,
+ "audio device name" },
+ { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
+ };
+
+ WLog_Print(opensles->log, WLOG_DEBUG, "device=%p, args=%p", (void*)device, (void*)args);
+ flags =
+ COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
+ status = CommandLineParseArgumentsA(args->argc, args->argv, audin_opensles_args, flags,
+ opensles, NULL, NULL);
+
+ if (status < 0)
+ return status;
+
+ arg = audin_opensles_args;
+
+ do
+ {
+ if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
+ continue;
+
+ CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
+ {
+ opensles->device_name = _strdup(arg->Value);
+
+ if (!opensles->device_name)
+ {
+ WLog_Print(opensles->log, WLOG_ERROR, "_strdup failed!");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+ }
+ CommandLineSwitchEnd(arg)
+ } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
+
+ return CHANNEL_RC_OK;
+}
+
+/**
+ * Function description
+ *
+ * @return 0 on success, otherwise a Win32 error code
+ */
+FREERDP_ENTRY_POINT(UINT opensles_freerdp_audin_client_subsystem_entry(
+ PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints))
+{
+ const ADDIN_ARGV* args;
+ AudinOpenSLESDevice* opensles;
+ UINT error;
+ opensles = (AudinOpenSLESDevice*)calloc(1, sizeof(AudinOpenSLESDevice));
+
+ if (!opensles)
+ {
+ WLog_ERR(TAG, "calloc failed!");
+ return CHANNEL_RC_NO_MEMORY;
+ }
+
+ opensles->log = WLog_Get(TAG);
+ opensles->iface.Open = audin_opensles_open;
+ opensles->iface.FormatSupported = audin_opensles_format_supported;
+ opensles->iface.SetFormat = audin_opensles_set_format;
+ opensles->iface.Close = audin_opensles_close;
+ opensles->iface.Free = audin_opensles_free;
+ opensles->rdpcontext = pEntryPoints->rdpcontext;
+ args = pEntryPoints->args;
+
+ if ((error = audin_opensles_parse_addin_args(opensles, args)))
+ {
+ WLog_Print(opensles->log, WLOG_ERROR,
+ "audin_opensles_parse_addin_args failed with errorcode %" PRIu32 "!", error);
+ goto error_out;
+ }
+
+ if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)opensles)))
+ {
+ WLog_Print(opensles->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
+ error);
+ goto error_out;
+ }
+
+ return CHANNEL_RC_OK;
+error_out:
+ free(opensles);
+ return error;
+}
diff --git a/channels/audin/client/opensles/opensl_io.c b/channels/audin/client/opensles/opensl_io.c
new file mode 100644
index 0000000..39b3dc8
--- /dev/null
+++ b/channels/audin/client/opensles/opensl_io.c
@@ -0,0 +1,388 @@
+/*
+opensl_io.c:
+Android OpenSL input/output module
+Copyright (c) 2012, Victor Lazzarini
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <winpr/assert.h>
+
+#include "audin_main.h"
+#include "opensl_io.h"
+#define CONV16BIT 32768
+#define CONVMYFLT (1. / 32768.)
+
+typedef struct
+{
+ size_t size;
+ void* data;
+} queue_element;
+
+struct opensl_stream
+{
+ // engine interfaces
+ SLObjectItf engineObject;
+ SLEngineItf engineEngine;
+
+ // device interfaces
+ SLDeviceVolumeItf deviceVolume;
+
+ // recorder interfaces
+ SLObjectItf recorderObject;
+ SLRecordItf recorderRecord;
+ SLAndroidSimpleBufferQueueItf recorderBufferQueue;
+
+ unsigned int inchannels;
+ unsigned int sr;
+ unsigned int buffersize;
+ unsigned int bits_per_sample;
+
+ queue_element* prep;
+ queue_element* next;
+
+ void* context;
+ opensl_receive_t receive;
+};
+
+static void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void* context);
+
+// creates the OpenSL ES audio engine
+static SLresult openSLCreateEngine(OPENSL_STREAM* p)
+{
+ SLresult result;
+ // create engine
+ result = slCreateEngine(&(p->engineObject), 0, NULL, 0, NULL, NULL);
+
+ if (result != SL_RESULT_SUCCESS)
+ goto engine_end;
+
+ // realize the engine
+ result = (*p->engineObject)->Realize(p->engineObject, SL_BOOLEAN_FALSE);
+
+ if (result != SL_RESULT_SUCCESS)
+ goto engine_end;
+
+ // get the engine interface, which is needed in order to create other objects
+ result = (*p->engineObject)->GetInterface(p->engineObject, SL_IID_ENGINE, &(p->engineEngine));
+
+ if (result != SL_RESULT_SUCCESS)
+ goto engine_end;
+
+ // get the volume interface - important, this is optional!
+ result =
+ (*p->engineObject)->GetInterface(p->engineObject, SL_IID_DEVICEVOLUME, &(p->deviceVolume));
+
+ if (result != SL_RESULT_SUCCESS)
+ {
+ p->deviceVolume = NULL;
+ result = SL_RESULT_SUCCESS;
+ }
+
+engine_end:
+ WINPR_ASSERT(SL_RESULT_SUCCESS == result);
+ return result;
+}
+
+// Open the OpenSL ES device for input
+static SLresult openSLRecOpen(OPENSL_STREAM* p)
+{
+ SLresult result;
+ SLuint32 sr = p->sr;
+ SLuint32 channels = p->inchannels;
+ WINPR_ASSERT(!p->recorderObject);
+
+ if (channels)
+ {
+ switch (sr)
+ {
+ case 8000:
+ sr = SL_SAMPLINGRATE_8;
+ break;
+
+ case 11025:
+ sr = SL_SAMPLINGRATE_11_025;
+ break;
+
+ case 16000:
+ sr = SL_SAMPLINGRATE_16;
+ break;
+
+ case 22050:
+ sr = SL_SAMPLINGRATE_22_05;
+ break;
+
+ case 24000:
+ sr = SL_SAMPLINGRATE_24;
+ break;
+
+ case 32000:
+ sr = SL_SAMPLINGRATE_32;
+ break;
+
+ case 44100:
+ sr = SL_SAMPLINGRATE_44_1;
+ break;
+
+ case 48000:
+ sr = SL_SAMPLINGRATE_48;
+ break;
+
+ case 64000:
+ sr = SL_SAMPLINGRATE_64;
+ break;
+
+ case 88200:
+ sr = SL_SAMPLINGRATE_88_2;
+ break;
+
+ case 96000:
+ sr = SL_SAMPLINGRATE_96;
+ break;
+
+ case 192000:
+ sr = SL_SAMPLINGRATE_192;
+ break;
+
+ default:
+ return -1;
+ }
+
+ // configure audio source
+ SLDataLocator_IODevice loc_dev = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT,
+ SL_DEFAULTDEVICEID_AUDIOINPUT, NULL };
+ SLDataSource audioSrc = { &loc_dev, NULL };
+ // configure audio sink
+ int speakers;
+
+ if (channels > 1)
+ speakers = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
+ else
+ speakers = SL_SPEAKER_FRONT_CENTER;
+
+ SLDataLocator_AndroidSimpleBufferQueue loc_bq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
+ 2 };
+ SLDataFormat_PCM format_pcm;
+ format_pcm.formatType = SL_DATAFORMAT_PCM;
+ format_pcm.numChannels = channels;
+ format_pcm.samplesPerSec = sr;
+ format_pcm.channelMask = speakers;
+ format_pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
+
+ if (16 == p->bits_per_sample)
+ {
+ format_pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
+ format_pcm.containerSize = 16;
+ }
+ else if (8 == p->bits_per_sample)
+ {
+ format_pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_8;
+ format_pcm.containerSize = 8;
+ }
+ else
+ WINPR_ASSERT(0);
+
+ SLDataSink audioSnk = { &loc_bq, &format_pcm };
+ // create audio recorder
+ // (requires the RECORD_AUDIO permission)
+ const SLInterfaceID id[] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE };
+ const SLboolean req[] = { SL_BOOLEAN_TRUE };
+ result = (*p->engineEngine)
+ ->CreateAudioRecorder(p->engineEngine, &(p->recorderObject), &audioSrc,
+ &audioSnk, 1, id, req);
+ WINPR_ASSERT(!result);
+
+ if (SL_RESULT_SUCCESS != result)
+ goto end_recopen;
+
+ // realize the audio recorder
+ result = (*p->recorderObject)->Realize(p->recorderObject, SL_BOOLEAN_FALSE);
+ WINPR_ASSERT(!result);
+
+ if (SL_RESULT_SUCCESS != result)
+ goto end_recopen;
+
+ // get the record interface
+ result = (*p->recorderObject)
+ ->GetInterface(p->recorderObject, SL_IID_RECORD, &(p->recorderRecord));
+ WINPR_ASSERT(!result);
+
+ if (SL_RESULT_SUCCESS != result)
+ goto end_recopen;
+
+ // get the buffer queue interface
+ result = (*p->recorderObject)
+ ->GetInterface(p->recorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
+ &(p->recorderBufferQueue));
+ WINPR_ASSERT(!result);
+
+ if (SL_RESULT_SUCCESS != result)
+ goto end_recopen;
+
+ // register callback on the buffer queue
+ result = (*p->recorderBufferQueue)
+ ->RegisterCallback(p->recorderBufferQueue, bqRecorderCallback, p);
+ WINPR_ASSERT(!result);
+
+ if (SL_RESULT_SUCCESS != result)
+ goto end_recopen;
+
+ end_recopen:
+ return result;
+ }
+ else
+ return SL_RESULT_SUCCESS;
+}
+
+// close the OpenSL IO and destroy the audio engine
+static void openSLDestroyEngine(OPENSL_STREAM* p)
+{
+ // destroy audio recorder object, and invalidate all associated interfaces
+ if (p->recorderObject != NULL)
+ {
+ (*p->recorderObject)->Destroy(p->recorderObject);
+ p->recorderObject = NULL;
+ p->recorderRecord = NULL;
+ p->recorderBufferQueue = NULL;
+ }
+
+ // destroy engine object, and invalidate all associated interfaces
+ if (p->engineObject != NULL)
+ {
+ (*p->engineObject)->Destroy(p->engineObject);
+ p->engineObject = NULL;
+ p->engineEngine = NULL;
+ }
+}
+
+static queue_element* opensles_queue_element_new(size_t size)
+{
+ queue_element* q = calloc(1, sizeof(queue_element));
+
+ if (!q)
+ goto fail;
+
+ q->size = size;
+ q->data = malloc(size);
+
+ if (!q->data)
+ goto fail;
+
+ return q;
+fail:
+ free(q);
+ return NULL;
+}
+
+static void opensles_queue_element_free(void* obj)
+{
+ queue_element* e = (queue_element*)obj;
+
+ if (e)
+ free(e->data);
+
+ free(e);
+}
+
+// open the android audio device for input
+OPENSL_STREAM* android_OpenRecDevice(void* context, opensl_receive_t receive, int sr,
+ int inchannels, int bufferframes, int bits_per_sample)
+{
+ OPENSL_STREAM* p;
+
+ if (!context || !receive)
+ return NULL;
+
+ p = (OPENSL_STREAM*)calloc(1, sizeof(OPENSL_STREAM));
+
+ if (!p)
+ return NULL;
+
+ p->context = context;
+ p->receive = receive;
+ p->inchannels = inchannels;
+ p->sr = sr;
+ p->buffersize = bufferframes;
+ p->bits_per_sample = bits_per_sample;
+
+ if ((p->bits_per_sample != 8) && (p->bits_per_sample != 16))
+ goto fail;
+
+ if (openSLCreateEngine(p) != SL_RESULT_SUCCESS)
+ goto fail;
+
+ if (openSLRecOpen(p) != SL_RESULT_SUCCESS)
+ goto fail;
+
+ /* Create receive buffers, prepare them and start recording */
+ p->prep = opensles_queue_element_new(p->buffersize * p->bits_per_sample / 8);
+ p->next = opensles_queue_element_new(p->buffersize * p->bits_per_sample / 8);
+
+ if (!p->prep || !p->next)
+ goto fail;
+
+ (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue, p->next->data, p->next->size);
+ (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue, p->prep->data, p->prep->size);
+ (*p->recorderRecord)->SetRecordState(p->recorderRecord, SL_RECORDSTATE_RECORDING);
+ return p;
+fail:
+ android_CloseRecDevice(p);
+ return NULL;
+}
+
+// close the android audio device
+void android_CloseRecDevice(OPENSL_STREAM* p)
+{
+ if (p == NULL)
+ return;
+
+ opensles_queue_element_free(p->next);
+ opensles_queue_element_free(p->prep);
+ openSLDestroyEngine(p);
+ free(p);
+}
+
+// this callback handler is called every time a buffer finishes recording
+static void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void* context)
+{
+ OPENSL_STREAM* p = (OPENSL_STREAM*)context;
+ queue_element* e;
+
+ if (!p)
+ return;
+
+ e = p->next;
+
+ if (!e)
+ return;
+
+ if (!p->context || !p->receive)
+ WLog_WARN(TAG, "Missing receive callback=%p, context=%p", p->receive, p->context);
+ else
+ p->receive(p->context, e->data, e->size);
+
+ p->next = p->prep;
+ p->prep = e;
+ (*p->recorderBufferQueue)->Enqueue(p->recorderBufferQueue, e->data, e->size);
+}
diff --git a/channels/audin/client/opensles/opensl_io.h b/channels/audin/client/opensles/opensl_io.h
new file mode 100644
index 0000000..e99522c
--- /dev/null
+++ b/channels/audin/client/opensles/opensl_io.h
@@ -0,0 +1,65 @@
+/*
+opensl_io.c:
+Android OpenSL input/output module header
+Copyright (c) 2012, Victor Lazzarini
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the <organization> nor the
+ names of its contributors may be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef FREERDP_CHANNEL_AUDIN_CLIENT_OPENSL_IO_H
+#define FREERDP_CHANNEL_AUDIN_CLIENT_OPENSL_IO_H
+
+#include <SLES/OpenSLES.h>
+#include <SLES/OpenSLES_Android.h>
+
+#include <freerdp/api.h>
+
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+ typedef struct opensl_stream OPENSL_STREAM;
+
+ typedef void (*opensl_receive_t)(void* context, const void* data, size_t size);
+
+ /*
+ Open the audio device with a given sampling rate (sr), input and output channels and IO buffer
+ size in frames. Returns a handle to the OpenSL stream
+ */
+ FREERDP_LOCAL OPENSL_STREAM* android_OpenRecDevice(void* context, opensl_receive_t receive,
+ int sr, int inchannels, int bufferframes,
+ int bits_per_sample);
+ /*
+ Close the audio device
+ */
+ FREERDP_LOCAL void android_CloseRecDevice(OPENSL_STREAM* p);
+
+#ifdef __cplusplus
+};
+#endif
+
+#endif /* FREERDP_CHANNEL_AUDIN_CLIENT_OPENSL_IO_H */