From ffdcb8fc0e504188b818ee207c6fd4cec897e599 Mon Sep 17 00:00:00 2001 From: its-pointless Date: Fri, 29 Dec 2017 10:52:17 +1100 Subject: [PATCH] opensles output for libpulseaudio from @twaik (#1968) --- packages/cmus/build.sh | 1 + packages/libpulseaudio/build.sh | 45 ++- packages/libpulseaudio/makefile.am.patch | 23 ++ packages/libpulseaudio/module-sles-sink.c | 431 ++++++++++++++++++++++ packages/sox/build.sh | 14 +- 5 files changed, 498 insertions(+), 16 deletions(-) create mode 100644 packages/libpulseaudio/makefile.am.patch create mode 100644 packages/libpulseaudio/module-sles-sink.c diff --git a/packages/cmus/build.sh b/packages/cmus/build.sh index 6e3250119..d1eb50f6c 100644 --- a/packages/cmus/build.sh +++ b/packages/cmus/build.sh @@ -4,6 +4,7 @@ TERMUX_PKG_VERSION=2.7.1 TERMUX_PKG_DEPENDS="libandroid-support, ncurses, libflac, libmad, libvorbis, opusfile, libcue, libpulseaudio" TERMUX_PKG_SRCURL=https://github.com/cmus/cmus/archive/v${TERMUX_PKG_VERSION}.tar.gz TERMUX_PKG_SHA256=8179a7a843d257ddb585f4c65599844bc0e516fe85e97f6f87a7ceade4eb5165 +TERMUX_PKG_REVISION=1 TERMUX_PKG_BUILD_IN_SRC=yes termux_step_pre_configure() { diff --git a/packages/libpulseaudio/build.sh b/packages/libpulseaudio/build.sh index f778e4cd5..3b081389f 100644 --- a/packages/libpulseaudio/build.sh +++ b/packages/libpulseaudio/build.sh @@ -1,7 +1,7 @@ TERMUX_PKG_HOMEPAGE=https://www.freedesktop.org/wiki/Software/PulseAudio TERMUX_PKG_DESCRIPTION="A featureful, general-purpose sound server - shared libraries" TERMUX_PKG_VERSION=11.1 -TERMUX_PKG_REVISION=1 +TERMUX_PKG_REVISION=2 TERMUX_PKG_SHA256=f2521c525a77166189e3cb9169f75c2ee2b82fa3fcf9476024fbc2c3a6c9cd9e TERMUX_PKG_SRCURL=https://www.freedesktop.org/software/pulseaudio/releases/pulseaudio-${TERMUX_PKG_VERSION}.tar.xz TERMUX_PKG_DEPENDS="libltdl, libsndfile, libandroid-glob" @@ -15,21 +15,42 @@ TERMUX_PKG_EXTRA_CONFIGURE_ARGS="--disable-neon-opt --without-caps --with-database=simple --disable-memfd" -TERMUX_PKG_CONFFILES="etc/pulse/client.conf etc/pulse/daemon.conf etc/pulse/dafault.pa etc/pulse/system.pa" +TERMUX_PKG_CONFFILES="etc/pulse/client.conf etc/pulse/daemon.conf etc/pulse/default.pa etc/pulse/system.pa" termux_step_pre_configure () { - LDFLAGS+=" -llog -landroid-glob" + mkdir $TERMUX_PKG_SRCDIR/src/modules/sles + cp $TERMUX_PKG_BUILDER_DIR/module-sles-sink.c $TERMUX_PKG_SRCDIR/src/modules/sles + intltoolize --automake --copy --force + LDFLAGS+=" -llog -landroid-glob" } termux_step_post_make_install () { - # Some binaries link against these: - cd $TERMUX_PREFIX/lib - for lib in pulseaudio/lib*.so* pulse-${TERMUX_PKG_VERSION}/modules/lib*.so*; do - ln -s -f $lib `basename $lib` - done + # Some binaries link against these: + cd $TERMUX_PREFIX/lib + for lib in pulseaudio/lib*.so* pulse-${TERMUX_PKG_VERSION}/modules/lib*.so*; do + ln -s -f $lib `basename $lib` + done + if [ $TERMUX_ARCH_BITS = "32" ]; then + SYSTEM_LIB=lib + else + SYSTEM_LIB=lib64 + fi + # Pulseaudio fails to start when it cannot detect any sound hardware + # so disable hardware detection. + sed -i $TERMUX_PREFIX/etc/pulse/default.pa \ + -e '/^load-module module-detect$/s/^/#/' + echo "load-module module-sles-sink" >> $TERMUX_PREFIX/etc/pulse/default.pa + cd $TERMUX_PREFIX/bin - # Pulseaudio fails to start when it cannot detect any sound hardware - # so disable hardware detection. - sed -i $TERMUX_PREFIX/etc/pulse/default.pa \ - -e '/^load-module module-detect$/s/^/#/' + for bin in esdcompat pacat pacmd pactl pasuspender pulseaudio; do + mv $bin ../libexec + local PA_LIBS="" lib + for lib in android-glob pulse pulsecommon-11.1 pulsecore-11.1; do + if [ -n "$PA_LIBS" ]; then PA_LIBS+=":"; fi + PA_LIBS+="$TERMUX_PREFIX/lib/lib${lib}.so" + done + echo "export LD_PRELOAD=$PA_LIBS" >> $TERMUX_PREFIX/bin/$bin + echo "LD_LIBRARY_PATH=/system/$SYSTEM_LIB:/system/vendor/$SYSTEM_LIB:/data/data/com.termux/files/usr/lib /data/data/com.termux/files/usr/libexec/$bin \$@" >> $TERMUX_PREFIX/bin/$bin + chmod +x $bin + done } diff --git a/packages/libpulseaudio/makefile.am.patch b/packages/libpulseaudio/makefile.am.patch new file mode 100644 index 000000000..1bb01f069 --- /dev/null +++ b/packages/libpulseaudio/makefile.am.patch @@ -0,0 +1,23 @@ +--- ../cache/pulseaudio-11.1/src/Makefile.am 2017-09-18 10:41:02.000000000 +0000 ++++ ./src/Makefile.am 2017-12-22 22:51:30.628573929 +0000 +@@ -1559,7 +1559,8 @@ + module-switch-on-port-available-symdef.h \ + module-filter-apply-symdef.h \ + module-filter-heuristics-symdef.h \ +- module-allow-passthrough-symdef.h ++ module-allow-passthrough-symdef.h \ ++ module-sles-sink-symdef.h + + if HAVE_ESOUND + SYMDEF_FILES += \ +@@ -1594,6 +1595,10 @@ + module_simple_protocol_unix_la_LIBADD = $(MODULE_LIBADD) libprotocol-simple.la + + # CLI protocol ++modlibexec_LTLIBRARIES += module-sles-sink.la ++module_sles_sink_la_SOURCES = modules/sles/module-sles-sink.c ++module_sles_sink_la_LDFLAGS = $(MODULE_LDFLAGS) -lOpenSLES ++module_sles_sink_la_LIBADD = $(MODULE_LIBADD) + + module_cli_la_SOURCES = modules/module-cli.c + module_cli_la_LDFLAGS = $(MODULE_LDFLAGS) diff --git a/packages/libpulseaudio/module-sles-sink.c b/packages/libpulseaudio/module-sles-sink.c new file mode 100644 index 000000000..5e1961316 --- /dev/null +++ b/packages/libpulseaudio/module-sles-sink.c @@ -0,0 +1,431 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2008 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, see . +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "module-sles-sink-symdef.h" + +#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE +#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE +#define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE +#define BufferQueueItf SLAndroidSimpleBufferQueueItf +#define BufferQueueState SLAndroidSimpleBufferQueueState +#define IID_BUFFERQUEUE_USED SL_IID_ANDROIDSIMPLEBUFFERQUEUE +#define INDEX index +#else +#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE +#define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE +#define BufferQueueItf SLBufferQueueItf +#define BufferQueueState SLBufferQueueState +#define IID_BUFFERQUEUE_USED IID_BUFFERQUEUE +#define INDEX playIndex +#endif + +#define checkResult(r) do { \ + if ((r) != SL_RESULT_SUCCESS) { \ + if ((r) == SL_RESULT_PARAMETER_INVALID) fprintf(stderr, "error SL_RESULT_PARAMETER_INVALID at %s:%d\n", __FILE__, __LINE__); \ + else if ((r) == SL_RESULT_PRECONDITIONS_VIOLATED ) fprintf(stderr, "error SL_RESULT_PRECONDITIONS_VIOLATED at %s:%d\n", __FILE__, __LINE__); \ + else fprintf(stderr, "error %d at %s:%d\n", (int) r, __FILE__, __LINE__); \ + } \ + } while (0) +typedef struct { + short left; + short right; +} frame_t; + +PA_MODULE_AUTHOR("Lennart Poettering, Nathan Martynov"); +PA_MODULE_DESCRIPTION("Android OpenSL ES sink"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(false); +PA_MODULE_USAGE( + "sink_name= " + "sink_properties= "); + +#define DEFAULT_SINK_NAME "OpenSL ES sink" +#define BLOCK_USEC (PA_USEC_PER_SEC * 2) + +struct userdata { + pa_core *core; + pa_module *module; + pa_sink *sink; + + pa_thread *thread; + pa_thread_mq thread_mq; + pa_rtpoll *rtpoll; + + pa_usec_t block_usec; + pa_usec_t timestamp; + + pa_memchunk memchunk; + + SLObjectItf engineObject; + SLEngineItf engineEngine; + + // output mix interfaces + SLObjectItf outputMixObject; + + // buffer queue player interfaces + SLObjectItf bqPlayerObject; + SLPlayItf bqPlayerPlay; + BufferQueueItf bqPlayerBufferQueue; +}; + +static const char* const valid_modargs[] = { + "sink_name", + "sink_properties", + NULL +}; + +static int sink_process_msg( + pa_msgobject *o, + int code, + void *data, + int64_t offset, + pa_memchunk *chunk) { + + struct userdata *u = PA_SINK(o)->userdata; + + switch (code) { + case PA_SINK_MESSAGE_SET_STATE: + + if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED || pa_sink_get_state(u->sink) == PA_SINK_INIT) { + if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING || PA_PTR_TO_UINT(data) == PA_SINK_IDLE) + u->timestamp = pa_rtclock_now(); + } + + break; + + case PA_SINK_MESSAGE_GET_LATENCY: { + pa_usec_t now; + + now = pa_rtclock_now(); + *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL; + + return 0; + } + } + + return pa_sink_process_msg(o, code, data, offset, chunk); +} + +static void sink_update_requested_latency_cb(pa_sink *s) { + struct userdata *u; + size_t nbytes; + + pa_sink_assert_ref(s); + pa_assert_se(u = s->userdata); + + u->block_usec = pa_sink_get_requested_latency_within_thread(s); + + if (u->block_usec == (pa_usec_t) -1) + u->block_usec = s->thread_info.max_latency; + + nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec); + pa_sink_set_max_rewind_within_thread(s, nbytes); + pa_sink_set_max_request_within_thread(s, nbytes); +} + +void pa_init_sles_player(struct userdata *s) +{ + if (s == NULL) return; + SLresult result; + + // create engine + result = slCreateEngine(&(s->engineObject), 0, NULL, 0, NULL, NULL); checkResult(result); + result = (*s->engineObject)->Realize(s->engineObject, SL_BOOLEAN_FALSE); checkResult(result); + + result = (*s->engineObject)->GetInterface(s->engineObject, SL_IID_ENGINE, &(s->engineEngine)); checkResult(result); + + // create output mix + result = (*s->engineEngine)->CreateOutputMix(s->engineEngine, &(s->outputMixObject), 0, NULL, NULL); checkResult(result); + result = (*s->outputMixObject)->Realize(s->outputMixObject, SL_BOOLEAN_FALSE); checkResult(result); + + // create audio player + + SLDataLocator_OutputMix locator_outputmix; + locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; + locator_outputmix.outputMix = s->outputMixObject; + + SLDataLocator_BufferQueue locator_bufferqueue; + locator_bufferqueue.locatorType = DATALOCATOR_BUFFERQUEUE; + locator_bufferqueue.numBuffers = 50; + + SLDataFormat_PCM pcm; + pcm.formatType = SL_DATAFORMAT_PCM; + pcm.numChannels = 2; + pcm.samplesPerSec = SL_SAMPLINGRATE_32; + pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; + pcm.containerSize = 16; + pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; + pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; + + SLDataSource audiosrc; + audiosrc.pLocator = &locator_bufferqueue; + audiosrc.pFormat = &pcm; + + SLDataSink audiosnk; + audiosnk.pLocator = &locator_outputmix; + audiosnk.pFormat = NULL; + + SLInterfaceID ids[1] = {IID_BUFFERQUEUE}; + SLboolean flags[1] = {SL_BOOLEAN_TRUE}; + result = (*s->engineEngine)->CreateAudioPlayer(s->engineEngine, &s->bqPlayerObject, &audiosrc, &audiosnk, 1, ids, flags); checkResult(result); + result = (*s->bqPlayerObject)->Realize(s->bqPlayerObject, SL_BOOLEAN_FALSE); checkResult(result); + + result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, SL_IID_PLAY, &s->bqPlayerPlay); checkResult(result); + result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, IID_BUFFERQUEUE_USED, &s->bqPlayerBufferQueue); checkResult(result); + + result = (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_PLAYING); checkResult(result); +} + +void pa_destroy_sles_player(struct userdata *s){ + if (s == NULL) return; + (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_STOPPED); + (*s->bqPlayerObject)->Destroy(s->bqPlayerObject); + (*s->outputMixObject)->Destroy(s->outputMixObject); + (*s->engineObject)->Destroy(s->engineObject); +} + +static void process_render(struct userdata *u, pa_usec_t now) { + size_t ate = 0; + + pa_assert(u); + + /* This is the configured latency. Sink inputs connected to us + might not have a single frame more than the maxrequest value + queued. Hence: at maximum read this many bytes from the sink + inputs. */ + + /* Fill the buffer up the latency size */ + while (u->timestamp < now + u->block_usec) { + void *p; + + pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk); + p = pa_memblock_acquire(u->memchunk.memblock); + (*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, (uint8_t*) p + u->memchunk.index, u->memchunk.length); + pa_memblock_release(u->memchunk.memblock); + +/* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */ + u->timestamp += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec); + + ate += u->memchunk.length; + + if (ate >= u->sink->thread_info.max_request) + break; + } + +/* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */ +} + +static void thread_func(void *userdata) { + struct userdata *u = userdata; + + pa_assert(u); + + pa_log_debug("Thread starting up"); + + pa_thread_mq_install(&u->thread_mq); + + u->timestamp = pa_rtclock_now(); + + for (;;) { + pa_usec_t now = 0; + int ret; + + if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) + now = pa_rtclock_now(); + + if (PA_UNLIKELY(u->sink->thread_info.rewind_requested)) + pa_sink_process_rewind(u->sink, 0); + + /* Render some data and drop it immediately */ + if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) { + if (u->timestamp <= now) + process_render(u, now); + + pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp); + } else + pa_rtpoll_set_timer_disabled(u->rtpoll); + + /* Hmm, nothing to do. Let's sleep */ + if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) + goto fail; + + if (ret == 0) + goto finish; + } + +fail: + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); + +finish: + pa_log_debug("Thread shutting down"); +} + +int pa__init(pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_channel_map map; + pa_modargs *ma = NULL; + pa_sink_new_data data; + size_t nbytes; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); + goto fail; + } + + // High rate causes glitches on some devices, this is needed to prevent it + ss.rate = 32000; + ss.channels = 2; + ss.format = PA_SAMPLE_S16LE; + map = m->core->default_channel_map; + + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); + + pa_init_sles_player(u); + int buff[2] = {0, 0}; + (*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, buff, 1); + + pa_sink_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); + pa_sink_new_data_set_sample_spec(&data, &ss); + pa_sink_new_data_set_channel_map(&data, &map); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Null Output")); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract"); + + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY); + pa_sink_new_data_done(&data); + + if (!u->sink) { + pa_log("Failed to create sink object."); + goto fail; + } + + u->sink->parent.process_msg = sink_process_msg; + u->sink->update_requested_latency = sink_update_requested_latency_cb; + u->sink->userdata = u; + + pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); + pa_sink_set_rtpoll(u->sink, u->rtpoll); + + u->block_usec = BLOCK_USEC; + nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec); + pa_sink_set_max_rewind(u->sink, nbytes); + pa_sink_set_max_request(u->sink, nbytes); + + if (!(u->thread = pa_thread_new("null-sink", thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; + } + + pa_sink_set_latency_range(u->sink, 0, BLOCK_USEC); + + pa_sink_put(u->sink); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +int pa__get_n_used(pa_module *m) { + struct userdata *u; + + pa_assert(m); + pa_assert_se(u = m->userdata); + + return pa_sink_linked_by(u->sink); +} + +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->sink) + pa_sink_unlink(u->sink); + + if (u->thread) { + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + pa_thread_free(u->thread); + } + + pa_thread_mq_done(&u->thread_mq); + + if (u->sink) + pa_sink_unref(u->sink); + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + + pa_xfree(u); +} diff --git a/packages/sox/build.sh b/packages/sox/build.sh index ab1221437..57716bdd0 100644 --- a/packages/sox/build.sh +++ b/packages/sox/build.sh @@ -1,21 +1,27 @@ TERMUX_PKG_HOMEPAGE=http://sox.sourceforge.net/ TERMUX_PKG_DESCRIPTION="Command line utility for converting between and applying effects to various audio files formats" TERMUX_PKG_VERSION=14.4.2 -TERMUX_PKG_REVISION=4 +TERMUX_PKG_REVISION=5 TERMUX_PKG_SRCURL=http://downloads.sourceforge.net/project/sox/sox/${TERMUX_PKG_VERSION}/sox-${TERMUX_PKG_VERSION}.tar.bz2 TERMUX_PKG_SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c TERMUX_PKG_EXTRA_CONFIGURE_ARGS=" --with-oggvorbis ---without-libltdl --without-opus ---without-pulseaudio +--with-pulseaudio=dyn --without-sndfile " TERMUX_PKG_RM_AFTER_INSTALL="bin/play bin/rec share/man/man1/play.1 share/man/man1/rec.1" # Depend on file for libmagic.so linking: -TERMUX_PKG_DEPENDS="file, libpng, libmp3lame, libogg, libvorbis, libandroid-glob, libflac, libid3tag, libmad" +TERMUX_PKG_DEPENDS="libltdl, file, libpng, libmp3lame, libogg, libvorbis, libandroid-glob, libflac, libid3tag, libmad, pulseaudio" termux_step_pre_configure() { LDFLAGS+=" -landroid-glob" CPPFLAGS+=" -D_FSTDIO" } + + +termux_step_post_massage() { + cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX/bin" + ln -sf sox play + ln -sf sox rec +}