Compare commits

...
Sign in to create a new pull request.
Author SHA1 Message Date
sanne raymaekers
9f31e3931b Changed configure.in to recognize linux-android instead of linux-androideabi
That way specifying non-arm architectures makes more sense.
2017-08-27 14:16:48 +02:00
Sanne
3833a43a7d Portaudio android opensles implementation 2017-03-25 17:05:19 +01:00
8 changed files with 1536 additions and 3 deletions

View file

@ -44,7 +44,7 @@ PALIB = libportaudio.la
PAINC = include/portaudio.h
PA_LDFLAGS = $(LDFLAGS) $(SHARED_FLAGS) -rpath $(libdir) -no-undefined \
-export-symbols-regex "(Pa|PaMacCore|PaJack|PaAlsa|PaAsio|PaOSS)_.*" \
-export-symbols-regex "(Pa|PaMacCore|PaJack|PaAlsa|PaAsio|PaOSS|PaOpenSLES)_.*" \
-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE)
COMMON_OBJS = \
@ -149,6 +149,7 @@ SRC_DIRS = \
src/hostapi/wasapi \
src/hostapi/wdmks \
src/hostapi/wmme \
src/hostapi/opensles \
src/os/unix \
src/os/win

BIN
configure vendored

Binary file not shown.

View file

@ -32,6 +32,10 @@ AC_ARG_WITH(oss,
AS_HELP_STRING([--with-oss], [Enable support for OSS @<:@autodetect@:>@]),
[with_oss=$withval])
AC_ARG_WITH(opensles,
AS_HELP_STRING([--with-opensles], [Enable support for OpenSLES @<:@autodetect@:>@]),
[with_opensles=$withval])
AC_ARG_WITH(asihpi,
AS_HELP_STRING([--with-asihpi], [Enable support for ASIHPI @<:@autodetect@:>@]),
[with_asihpi=$withval])
@ -137,6 +141,11 @@ if test "x$with_jack" != "xno"; then
PKG_CHECK_MODULES(JACK, jack, have_jack=yes, have_jack=no)
fi
have_opensles=no
if test "x$with_opensles" != "xno"; then
AC_CHECK_LIB(OpenSLES, slCreateEngine, have_opensles=yes, have_opensles=no)
fi
dnl sizeof checks: we will need a 16-bit and a 32-bit type
@ -390,11 +399,28 @@ case "${host_os}" in
SHARED_FLAGS=""
;;
linux-android* )
dnl Android configuration, this is separate because we don't need to link pthread explicitly
CFLAGS="$CFLAGS -I\$(top_srcdir)/src/os/unix"
if [[ "$have_opensles" = "yes" ] && [ "$with_opensles" != "no" ]]; then
LIBS="$LIBS -lOpenSLES"
DLL_LIBS="$DLL_LIBS -lOpenSLES"
OTHER_OBJS="$OTHER_OBJS src/hostapi/opensles/pa_opensles.o"
INCLUDES="$INCLUDES pa_opensles.h"
AC_DEFINE(PA_USE_OPENSLES,1)
fi
DLL_LIBS="$DLL_LIBS -lm"
LIBS="$LIBS -lm"
PADLL="libportaudio.so"
OTHER_OBJS="$OTHER_OBJS src/os/unix/pa_unix_hostapis.o src/os/unix/pa_unix_util.o"
;;
*)
dnl Unix configuration
CFLAGS="$CFLAGS -I\$(top_srcdir)/src/os/unix"
AC_CHECK_LIB(pthread, pthread_create,[have_pthread="yes"],
AC_MSG_ERROR([libpthread not found!]))
@ -505,6 +531,11 @@ case "$target_os" in
*darwin*)
AC_MSG_RESULT([
Mac debug flags ............. $enable_mac_debug
])
;;
*linux-androideabi*)
AC_MSG_RESULT([
OpenSLES .................... $have_opensles
])
;;
*)

78
include/pa_opensles.h Normal file
View file

@ -0,0 +1,78 @@
#ifndef PA_OPENSLES_H
#define PA_OPENSLES_H
/*
* $Id:
* PortAudio Portable Real-Time Audio Library
* Android OpenSLES-specific extensions
*
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
/** @file
@ingroup public_header
@brief Android OpenSLES-specific PortAudio API extension header file.
*/
#include <SLES/OpenSLES.h>
#ifdef __cplusplus
extern "C" {
#endif
/** The android stream type and recording preset as defined in
* OpenSLES_AndroidConfiguration.h
*/
typedef struct PaOpenslesStreamInfo {
SLint32 androidPlaybackStreamType;
SLint32 androidRecordingPreset;
} PaOpenslesStreamInfo;
/** Provide PA OpenSLES with native buffer information. This function must be called before Pa_Initialize.
* To have optimal latency, this function should be called. Otherwise PA OpenSLES will use non-optimal values
* as default.
* @param bufferSize the native buffersize as returned by AudioManager's PROPERTY_OUTPUT_FRAMES_PER_BUFFER. It is recommended you set the number of buffers to 1 if API>17 as well, and use the sample rate defined in AudioManager's android.media.property.OUTPUT_SAMPLE_RATE. All three together will enable the AUDIO_OUTPUT_FLAG_FAST flag.
*/
void PaOpenSLES_SetNativeBufferSize( unsigned long bufferSize );
/** Provide PA OpenSLES with native buffer information. This function must be called before Pa_Initialize.
* To have optimal latency and enable the AUDIO_OUTPUT_FLAG_FAST flag, this function should be called. Otherwise PA OpenSLES will use non-optimal values (2) as default.
* @param The number of buffers can be reduced to 1 on API >17. Make sure you set the native buffer size when doing this, and use the sample rate defined in AudioManager's android.media.property.OUTPUT_SAMPLE_RATE.
*/
void PaOpenSLES_SetNumberOfBuffers( unsigned buffers );
#ifdef __cplusplus
}
#endif
#endif

View file

@ -153,6 +153,13 @@ are defaulted to 1.
#define PA_USE_ASIHPI 1
#endif
#ifndef PA_USE_OPENSLES
#define PA_USE_OPENSLES 0
#elif (PA_USE_OPENSLES != 0) && (PA_USE_OPENSLES != 1)
#undef PA_USE_OPENSLES
#define PA_USE_OPENSLES 1
#endif
#ifdef __cplusplus
extern "C"
{

View file

@ -0,0 +1,38 @@
Portaudio implementation for android using opensles.
Building
----
To build portaudio with opensles an android NDK is needed to crosscompile it, version r9b has been used during development.
TODOs
----
Testing, loads of it.
Misc
----
Audio fast track:
Opensles on android has an audio_output_fast_flag which will be automatically set when you open a device with the right parameters. When this flag is set latency will be reduced dramatically between opensles and the android HAL. The samplerate needed for the fast track flag can be queried using AudioManager's getProperty(PROPERTY_OUTPUT_SAMPLE_RATE).
Latency:
Default latencies and precise stream latencies are hard to calculate since these will depend on device, android version, and whether or not the audio fast track flag has been set. At the moment this implementation only takes into account the user buffers and the opensles buffers.
Buffer sizes:
When using portaudio opensles in an android application it is recommended to query the AudioManager's getProperty(PROPERTY_OUTPUT_FRAMES_PER_BUFFER). Using this buffer size or a multiple of this buffer size is recommended. The implementation does not have any lower bound on the framesPerBuffer argument in Pa_Openstream. This way newer devices can take advantage of improving audio latency on android.
Querying audio capabilities:
OpenSL ES on android does not support the OpenSL ES SLAudioIODeviceCapabilitiesItf. As a result PaOpenSLES_Initialize populates the devices with some amount of heuristics. We simply use one output/input device. And starting API 14 use the SLAndroidConfigurationItf to configure the stream type of said devices. This also means that testing for the support of a certain samplerate or amount of channels quikcly opens and closes the device and checks for errors. This is a pretty dirty way of doing this, suggestions welcomed.
Prefetch underflow assert that fails:
There is an assert(SL_PREFETCHSTATUS_UNDERFLOW == ap->mPrefetchStatus.mStatus) that fails (Src https://android.googlesource.com/platform/frameworks/wilhelm/+/91145ef159d3e165a461cbd76341ff8ed3d72bae/src/android/AudioPlayer_to_android.cpp) sometimes with a non-blocking audio output.
This is an assert which tries to make sure that the prefetchitf was in SL_PREFETCHSTATUS_UNDERFLOW, when trying to re-enqueue/restart the buffers because the user didn't enqueue buffers for a while (and the buffers underflowed). This doesn't happen when you can supply the buffer fast enough of course. But the specification states that if you stop enqueueing buffers, it should just stop playing, if you enqueue buffers again, it should start up again, and it shouldn't abort the entire application.
As a result I disabled the prefetchstatus callback for now, so there is no formation available to the user whether the buffers underflowed or overflowed, but at least portaudio just keeps running.
Number of buffers:
As mentioned in the android opensles documentation under performance, the number of buffer sizes can be 1 on api >17

File diff suppressed because it is too large Load diff

View file

@ -52,6 +52,8 @@ PaError PaAsiHpi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiInd
PaError PaMacCore_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
PaError PaSkeleton_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
PaError PaOpenSLES_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
/** Note that on Linux, ALSA is placed before OSS so that the former is preferred over the latter.
*/
@ -99,5 +101,9 @@ PaUtilHostApiInitializer *paHostApiInitializers[] =
PaSkeleton_Initialize,
#endif
#if PA_USE_OPENSLES
PaOpenSLES_Initialize,
#endif
0 /* NULL terminated array */
};