PulseAudio SLES module improvements (#6290)

* pulseaudio: Improve error msg when module-sles-source is missing the RECORD_AUDIO permission

* pulseaudio: Refactor and improve module-sles-{source,sink}

* More readable and consistent code style.
* Fix subtle bugs and typos (e.g. duplicate or ineffectual assignments,
  mishandled enums, etc).
* Fix stereo input channel mask.
* Fix wrong self object being passed to SLES functions.
* Handle more sample formats.
* Better logging.
* Fix potential freeing of undefined pointers.
* Fixed module usage (channel_map was on source instead of sink,
  some items were missing the trailing space).
* Other minor fixes.

* pulseaudio: Implement PA_SINK_MESSAGE_GET_LATENCY message

Every built-in module implements this and without it, PA will crash
in some cases (e.g. when opening pavucontrol).

* pulseaudio: Increment TERMUX_PKG_REVISION
This commit is contained in:
Patrick Gaskin 2021-01-24 11:51:02 -05:00 committed by GitHub
parent 975f908da5
commit fc007e73f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 429 additions and 384 deletions

View File

@ -4,7 +4,7 @@ TERMUX_PKG_LICENSE="GPL-2.0"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_SRCURL=https://github.com/pulseaudio/pulseaudio.git
TERMUX_PKG_VERSION=14.2
TERMUX_PKG_REVISION=1
TERMUX_PKG_REVISION=2
TERMUX_PKG_DEPENDS="libltdl, libsndfile, libandroid-glob, libsoxr, speexdsp, libwebrtc-audio-processing"
TERMUX_PKG_BREAKS="libpulseaudio-dev, libpulseaudio"
TERMUX_PKG_REPLACES="libpulseaudio-dev, libpulseaudio"

View File

@ -21,25 +21,24 @@
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
#include <pulsecore/i18n.h>
#include <pulsecore/macro.h>
#include <pulsecore/sink.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
#include <pulsecore/thread.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/macro.h>
#include <pulsecore/modargs.h>
#include <pulsecore/module.h>
#include <pulsecore/rtpoll.h>
#include <pulsecore/sink.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/thread.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
@ -51,16 +50,14 @@ PA_MODULE_LOAD_ONCE(false);
PA_MODULE_USAGE(
"sink_name=<name for the sink> "
"sink_properties=<properties for the sink> "
"rate=<sampling rate> "
"rate=<sample rate> "
"channel_map=<channel map> "
"latency=<buffer length> "
);
#define DEFAULT_SINK_NAME "OpenSL ES sink"
#define BLOCK_USEC (PA_USEC_PER_MSEC * 125)
enum {
SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX
};
enum { SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX };
struct userdata {
pa_core *core;
@ -79,13 +76,13 @@ struct userdata {
void *buf;
size_t nbytes;
SLObjectItf engineObject;
SLObjectItf outputMixObject;
SLObjectItf bqPlayerObject;
SLObjectItf EngineObject;
SLObjectItf OutputMixObject;
SLObjectItf PlayerObject;
SLEngineItf engineEngine;
SLPlayItf bqPlayerPlay;
SLBufferQueueItf bqPlayerBufferQueue;
SLEngineItf EngineItf;
SLPlayItf PlayItf;
SLBufferQueueItf BufferQueueItf;
};
static const char* const valid_modargs[] = {
@ -98,7 +95,6 @@ static const char* const valid_modargs[] = {
static void process_render(void *userdata) {
struct userdata* u = userdata;
pa_assert(u);
/* a render message could be queued after a set state message */
@ -107,7 +103,7 @@ static void process_render(void *userdata) {
u->memchunk.length = u->nbytes;
pa_sink_render_into(u->sink, &u->memchunk);
(*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, u->buf, u->memchunk.length);
(*u->BufferQueueItf)->Enqueue(u->BufferQueueItf, u->buf, u->memchunk.length);
}
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) {
@ -115,6 +111,9 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
case SINK_MESSAGE_RENDER:
process_render(data);
return 0;
case PA_SINK_MESSAGE_GET_LATENCY:
code = PA_SINK_MESSAGE_GET_FIXED_LATENCY; // FIXME: is there a way to get the real latency?
break;
}
return pa_sink_process_msg(o, code, data, offset, memchunk);
@ -122,114 +121,153 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
static void sles_callback(SLBufferQueueItf bqPlayerBufferQueue, void *userdata) {
struct userdata* u = userdata;
pa_assert(u);
pa_assert_se(pa_asyncmsgq_send(u->sles_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RENDER, u, 0, NULL) == 0);
}
#define CHK(stmt) { \
SLresult res = stmt; \
if (res != SL_RESULT_SUCCESS) { \
fprintf(stderr, "error %d at %s:%d\n", res, __FILE__, __LINE__); \
goto fail; \
} \
static int pa_sample_spec_to_sl_format(pa_sample_spec *ss, SLAndroidDataFormat_PCM_EX *sl) {
pa_assert(ss);
pa_assert(sl);
*sl = (SLAndroidDataFormat_PCM_EX){0};
switch ((sl->numChannels = ss->channels)) {
case 1:
sl->channelMask = SL_SPEAKER_FRONT_CENTER;
break;
case 2:
sl->channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
break;
default:
pa_log_error("Unsupported sample format: only stereo or mono channels are supported.");
return 1;
}
sl->sampleRate = ss->rate * 1000; // Hz to mHz
switch (ss->format) {
case PA_SAMPLE_S16LE: // fallthrough
case PA_SAMPLE_S24LE: // fallthrough
case PA_SAMPLE_S32LE: // fallthrough
case PA_SAMPLE_S16BE: // fallthrough
case PA_SAMPLE_S24BE: // fallthrough
case PA_SAMPLE_S32BE:
sl->formatType = SL_DATAFORMAT_PCM;
sl->representation = SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT;
break;
case PA_SAMPLE_FLOAT32LE: // fallthrough
case PA_SAMPLE_FLOAT32BE:
sl->formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
sl->representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
break;
default:
pa_log_error("Unsupported sample format: only s16/24/32 and f32 formats are supported.");
return 1;
}
sl->endianness = pa_sample_format_is_le(ss->format)
? SL_BYTEORDER_LITTLEENDIAN
: SL_BYTEORDER_BIGENDIAN;
sl->bitsPerSample = sl->containerSize = pa_sample_size(ss) * 8;
return 0;
}
static int pa_init_sles_player(struct userdata *u, pa_sample_spec *ss) {
CHK(slCreateEngine(&(u->engineObject), 0, NULL, 0, NULL, NULL));
CHK((*u->engineObject)->Realize(u->engineObject, SL_BOOLEAN_FALSE));
CHK((*u->engineObject)->GetInterface(u->engineObject, SL_IID_ENGINE, &(u->engineEngine)));
CHK((*u->engineEngine)->CreateOutputMix(u->engineEngine, &(u->outputMixObject), 0, NULL, NULL));
CHK((*u->outputMixObject)->Realize(u->outputMixObject, SL_BOOLEAN_FALSE));
SLDataLocator_BufferQueue locator_bufferqueue;
locator_bufferqueue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
locator_bufferqueue.numBuffers = 8;
pa_assert(u);
// check and convert the sample spec
SLAndroidDataFormat_PCM_EX pcm;
if (ss->format == PA_SAMPLE_FLOAT32LE) {
pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
} else {
pcm.formatType = SL_DATAFORMAT_PCM;
if (pa_sample_spec_to_sl_format(ss, &pcm))
return 1;
// common sles error handling
#define CHK(stmt) { \
SLresult res = (stmt); \
if (res != SL_RESULT_SUCCESS) { \
pa_log_error("Failed to initialize OpenSL ES: error %d at %s:%d", \
res, __FILE__, __LINE__); \
return 1; \
} \
}
pcm.numChannels = ss->channels;
pcm.sampleRate = ss->rate * 1000;
pcm.bitsPerSample = pcm.containerSize = pa_sample_size(ss) * 8;
pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
SLDataSource audiosrc;
audiosrc.pLocator = &locator_bufferqueue;
audiosrc.pFormat = &pcm;
// create the engine
CHK(slCreateEngine(&u->EngineObject, 0, NULL, 0, NULL, NULL));
CHK((*u->EngineObject)->Realize(u->EngineObject, SL_BOOLEAN_FALSE));
SLDataLocator_OutputMix locator_outputmix;
locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
locator_outputmix.outputMix = u->outputMixObject;
// create the outputmix
CHK((*u->EngineObject)->GetInterface(u->EngineObject, SL_IID_ENGINE, &(u->EngineItf)));
CHK((*u->EngineItf)->CreateOutputMix(u->EngineItf, &u->OutputMixObject, 0, NULL, NULL));
CHK((*u->OutputMixObject)->Realize(u->OutputMixObject, SL_BOOLEAN_FALSE));
SLDataSink audiosnk;
audiosnk.pLocator = &locator_outputmix;
audiosnk.pFormat = NULL;
// create the player
CHK(({
SLresult r = (*u->EngineItf)->CreateAudioPlayer(u->EngineItf,
&u->PlayerObject,
&(SLDataSource){
.pLocator = &(SLDataLocator_BufferQueue){
.locatorType = SL_DATALOCATOR_BUFFERQUEUE,
.numBuffers = 8,
},
.pFormat = &pcm,
},
&(SLDataSink){
.pLocator = &(SLDataLocator_OutputMix){
.locatorType = SL_DATALOCATOR_OUTPUTMIX,
.outputMix = u->OutputMixObject,
},
.pFormat = NULL,
},
1,
(SLInterfaceID[]){SL_IID_BUFFERQUEUE},
(SLboolean[]){SL_BOOLEAN_TRUE}
);
if (r == SL_RESULT_CONTENT_UNSUPPORTED)
pa_log(
"Failed to initialize OpenSL ES; try checking logcat and "
"searching for messages with 'libOpenSLES:'"
);
r;
}));
SLInterfaceID ids[1] = {SL_IID_BUFFERQUEUE};
SLboolean flags[1] = {SL_BOOLEAN_TRUE};
// realize the player
CHK((*u->PlayerObject)->Realize(u->PlayerObject, SL_BOOLEAN_FALSE));
CHK((*u->PlayerObject)->GetInterface(u->PlayerObject, SL_IID_PLAY, &u->PlayItf));
CHK((*u->engineEngine)->CreateAudioPlayer(u->engineEngine, &u->bqPlayerObject, &audiosrc, &audiosnk, 1, ids, flags));
CHK((*u->bqPlayerObject)->Realize(u->bqPlayerObject, SL_BOOLEAN_FALSE));
// register the callback
CHK((*u->PlayerObject)->GetInterface(u->PlayerObject, SL_IID_BUFFERQUEUE, &u->BufferQueueItf));
CHK((*u->BufferQueueItf)->RegisterCallback(u->BufferQueueItf, sles_callback, u));
CHK((*u->bqPlayerObject)->GetInterface(u->bqPlayerObject, SL_IID_PLAY, &u->bqPlayerPlay));
CHK((*u->bqPlayerObject)->GetInterface(u->bqPlayerObject, SL_IID_BUFFERQUEUE, &u->bqPlayerBufferQueue));
CHK((*u->bqPlayerBufferQueue)->RegisterCallback(u->bqPlayerBufferQueue, sles_callback, u));
// cleanup
#undef CHK
return 0;
fail:
return -1;
}
#undef CHK
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);
for (;;) {
int ret;
if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
goto fail;
if (ret == 0)
goto finish;
if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) {
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);
break;
}
if (ret == 0) {
break;
}
}
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");
}
static int state_func(pa_sink *s, pa_sink_state_t state, pa_suspend_cause_t suspend_cause) {
struct userdata *u = s->userdata;
pa_assert(u);
if (PA_SINK_IS_OPENED(s->state) &&
(state == PA_SINK_SUSPENDED || state == PA_SINK_UNLINKED))
(*u->bqPlayerPlay)->SetPlayState(u->bqPlayerPlay, SL_PLAYSTATE_STOPPED);
else if ((s->state == PA_SINK_SUSPENDED || s->state == PA_SINK_INIT) &&
PA_SINK_IS_OPENED(state))
(*u->bqPlayerPlay)->SetPlayState(u->bqPlayerPlay, SL_PLAYSTATE_PLAYING);
if (PA_SINK_IS_OPENED(s->state) && (state == PA_SINK_SUSPENDED || state == PA_SINK_UNLINKED))
(*u->PlayItf)->SetPlayState(u->PlayItf, SL_PLAYSTATE_STOPPED);
else if ((s->state == PA_SINK_SUSPENDED || s->state == PA_SINK_INIT) && PA_SINK_IS_OPENED(state))
(*u->PlayItf)->SetPlayState(u->PlayItf, SL_PLAYSTATE_PLAYING);
return 0;
}
@ -239,99 +277,81 @@ static void process_rewind(pa_sink *s) {
int pa__init(pa_module*m) {
struct userdata *u = NULL;
pa_modargs *ma = NULL;
pa_sink_new_data sink_data;
pa_sample_spec ss;
pa_channel_map map;
pa_modargs *ma = NULL;
pa_sink_new_data data;
uint32_t latency = 0;
pa_assert(m);
m->userdata = u = pa_xnew0(struct userdata, 1);
u->core = m->core;
u->module = m;
u->rtpoll = pa_rtpoll_new();
if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
pa_log("pa_thread_mq_init() failed.");
goto fail;
}
/* The queue linking the AudioTrack thread and our RT thread */
u->sles_msgq = pa_asyncmsgq_new(0);
if (!u->sles_msgq) {
pa_log("pa_asyncmsgq_new() failed.");
goto fail;
}
/* The msgq from the AudioTrack RT thread should have an even higher
* priority than the normal message queues, to match the guarantee
* all other drivers make: supplying the audio device with data is
* the top priority -- and as long as that is possible we don't do
* anything else */
u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->sles_msgq);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
pa_log("Failed to parse module arguments.");
goto fail;
}
ss = m->core->default_sample_spec;
pa_channel_map_init_stereo(&map);
u->core = m->core;
u->module = m;
switch (ss.format) {
case PA_SAMPLE_S16LE:
case PA_SAMPLE_S24LE:
case PA_SAMPLE_S32LE:
case PA_SAMPLE_FLOAT32LE:
break;
default:
pa_log("Sample format not supported");
u->rtpoll = pa_rtpoll_new();
if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
pa_log("pa_thread_mq_init() failed.");
goto fail;
}
pa_modargs_get_sample_rate(ma, &ss.rate);
if (!(u->sles_msgq = pa_asyncmsgq_new(0))) {
pa_log("pa_asyncmsgq_new() failed.");
goto fail;
}
u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->sles_msgq);
pa_sink_new_data_init(&sink_data);
sink_data.driver = __FILE__;
sink_data.module = m;
ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_rate(ma, &ss.rate) < 0) {
pa_log("Invalid rate specification.");
goto fail;
}
pa_channel_map_init_stereo(&map);
ss.channels = map.channels;
if (pa_init_sles_player(u, &ss) < 0)
if (pa_init_sles_player(u, &ss))
goto fail;
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, _("OpenSL ES Output"));
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
pa_sink_new_data_set_name(&sink_data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
pa_sink_new_data_set_sample_spec(&sink_data, &ss);
pa_sink_new_data_set_channel_map(&sink_data, &map);
pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "OpenSL ES Output");
pa_proplist_sets(sink_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);
if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
pa_log("Invalid properties.");
pa_sink_new_data_done(&sink_data);
goto fail;
}
u->sink = pa_sink_new(m->core, &data, 0);
pa_sink_new_data_done(&data);
if (!u->sink) {
if (!(u->sink = pa_sink_new(m->core, &sink_data, 0))) {
pa_log("Failed to create sink object.");
pa_sink_new_data_done(&sink_data);
goto fail;
}
pa_sink_new_data_done(&sink_data);
u->sink->userdata = u;
u->sink->parent.process_msg = sink_process_msg;
u->sink->set_state_in_main_thread = state_func;
u->sink->request_rewind = process_rewind;
u->sink->userdata = u;
pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
pa_sink_set_rtpoll(u->sink, u->rtpoll);
pa_modargs_get_value_u32(ma, "latency", &latency);
if (latency)
u->block_usec = latency * PA_USEC_PER_MSEC;
else
u->block_usec = BLOCK_USEC;
u->block_usec = latency
? PA_USEC_PER_MSEC * latency
: PA_USEC_PER_MSEC * 125;
pa_sink_set_fixed_latency(u->sink, u->block_usec);
u->nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
@ -344,7 +364,7 @@ int pa__init(pa_module*m) {
}
pa_sink_put(u->sink);
sles_callback(u->bqPlayerBufferQueue, u);
sles_callback(u->BufferQueueItf, u);
pa_modargs_free(ma);
@ -353,7 +373,6 @@ int pa__init(pa_module*m) {
fail:
if (ma)
pa_modargs_free(ma);
pa__done(m);
return -1;
@ -368,8 +387,6 @@ int pa__get_n_used(pa_module *m) {
return pa_sink_linked_by(u->sink);
}
#define DESTROY(object) if (u->object) (*u->object)->Destroy(u->object);
void pa__done(pa_module*m) {
struct userdata *u;
@ -381,33 +398,31 @@ void pa__done(pa_module*m) {
if (u->sink)
pa_sink_unlink(u->sink);
DESTROY(bqPlayerObject);
DESTROY(outputMixObject);
DESTROY(engineObject);
if (u->PlayerObject)
(*u->PlayerObject)->Destroy(u->PlayerObject);
if (u->OutputMixObject)
(*u->OutputMixObject)->Destroy(u->OutputMixObject);
if (u->EngineObject)
(*u->EngineObject)->Destroy(u->EngineObject);
pa_memblock_unref_fixed(u->memchunk.memblock);
if (u->memchunk.memblock)
pa_memblock_unref_fixed(u->memchunk.memblock);
free(u->buf);
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_item)
pa_rtpoll_item_free(u->rtpoll_item);
if (u->sles_msgq)
pa_asyncmsgq_unref(u->sles_msgq);
if (u->rtpoll)
pa_rtpoll_free(u->rtpoll);
pa_xfree(u);
}
#undef DESTROY

View File

@ -21,323 +21,352 @@
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pulse/rtclock.h>
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
#include <pulsecore/source.h>
#include <pulsecore/module.h>
#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/log.h>
#include <pulsecore/thread.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/modargs.h>
#include <pulsecore/module.h>
#include <pulsecore/rtpoll.h>
#include <pulsecore/source.h>
#include <pulsecore/thread-mq.h>
#include <pulsecore/thread.h>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <SLES/OpenSLES_AndroidConfiguration.h>
PA_MODULE_AUTHOR("Lennart Poettering");
PA_MODULE_DESCRIPTION("Android OpenSL ES source");
PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_LOAD_ONCE(false);
PA_MODULE_USAGE(
"source_name=<name for the source> "
"source_properties=<properties for the source> "
"rate=<sample rate> "
"channel_map=<channel map>"
"latency=<buffer length>"
"source_name=<name for the source> "
"source_properties=<properties for the source> "
"rate=<sample rate> "
"latency=<buffer length> "
);
#define CHK(stmt) { \
SLresult res = stmt; \
if (res != SL_RESULT_SUCCESS) { \
fprintf(stderr, "error %d at %s:%d\n", res, __FILE__, __LINE__); \
goto fail; \
} \
}
#define DEFAULT_SOURCE_NAME "OpenSL ES source"
#define BLOCK_USEC (PA_USEC_PER_MSEC * 125)
enum {
SOURCE_MESSAGE_RENDER = PA_SOURCE_MESSAGE_MAX
};
enum { SOURCE_MESSAGE_RENDER = PA_SOURCE_MESSAGE_MAX };
struct userdata {
pa_core *core;
pa_module *module;
pa_source *source;
pa_thread *thread;
pa_thread_mq thread_mq;
pa_rtpoll *rtpoll;
pa_rtpoll_item *rtpoll_item;
pa_asyncmsgq *sles_msgq;
pa_usec_t block_usec;
pa_memchunk memchunk;
pa_usec_t block_usec; /* how much to push at once */
pa_usec_t timestamp; /* when to push next */
SLObjectItf engineObject;
SLObjectItf RecorderObject;
SLEngineItf EngineItf;
SLRecordItf recordItf;
SLAndroidSimpleBufferQueueItf recBuffQueueItf;
pa_asyncmsgq *sles_msgq;
void *buf;
size_t nbytes;
SLObjectItf EngineObject;
SLObjectItf RecorderObject;
SLEngineItf EngineItf;
SLRecordItf RecordItf;
SLAndroidConfigurationItf ConfigurationItf;
SLAndroidSimpleBufferQueueItf BufferQueueItf;
};
static const char* const valid_modargs[] = {
static const char *const valid_modargs[] = {
"source_name",
"source_properties",
"rate",
"latency",
NULL
};
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);
for (;;) {
int ret;
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");
}
static int state_func(pa_source *s, pa_source_state_t state, pa_suspend_cause_t suspend_cause) {
struct userdata *u = s->userdata;
if (PA_SOURCE_IS_OPENED(s->state) &&
(state == PA_SOURCE_SUSPENDED || state == PA_SOURCE_UNLINKED))
(*u->recordItf)->SetRecordState(u->recordItf, SL_RECORDSTATE_STOPPED);
else if ((s->state == PA_SOURCE_SUSPENDED || s->state == PA_SOURCE_INIT) &&
PA_SOURCE_IS_OPENED(state))
(*u->recordItf)->SetRecordState(u->recordItf, SL_RECORDSTATE_RECORDING);
return 0;
}
static void process_render(void *userdata) {
struct userdata* u = userdata;
struct userdata *u = userdata;
pa_assert(u);
(*u->recordItf)->SetRecordState(u->recordItf, SL_RECORDSTATE_RECORDING);
(*u->RecordItf)->SetRecordState(u->RecordItf, SL_RECORDSTATE_RECORDING);
if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state))
return;
u->memchunk.length = u->nbytes;
(*u->recBuffQueueItf)->Enqueue(u->recBuffQueueItf, u->buf, u->memchunk.length);
pa_source_post(u->source, &u->memchunk);
(*u->BufferQueueItf)->Enqueue(u->BufferQueueItf, u->buf, u->memchunk.length);
pa_source_post(u->source, &u->memchunk);
}
static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) {
switch (code) {
case SOURCE_MESSAGE_RENDER:
process_render(data);
return 0; }
return pa_source_process_msg(o, code, data, offset, memchunk);
return 0;
case PA_SINK_MESSAGE_GET_LATENCY:
code = PA_SINK_MESSAGE_GET_FIXED_LATENCY; // FIXME: is there a way to get the real latency?
break;
}
return pa_source_process_msg(o, code, data, offset, memchunk);
};
static void sles_callback(SLAndroidSimpleBufferQueueItf recBuffQueueItf , void *userdata) {
struct userdata* u = userdata;
static void sles_callback(SLAndroidSimpleBufferQueueItf recBuffQueueItf, void *userdata) {
struct userdata *u = userdata;
pa_assert(u);
pa_assert_se(pa_asyncmsgq_send(u->sles_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_RENDER, u, 0, NULL) == 0);
pa_assert_se(pa_asyncmsgq_send(u->sles_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_RENDER, u, 0, NULL) == 0);
}
static int pa_sample_spec_to_sl_format(pa_sample_spec *ss, SLAndroidDataFormat_PCM_EX *sl) {
pa_assert(ss);
pa_assert(sl);
*sl = (SLAndroidDataFormat_PCM_EX){0};
switch ((sl->numChannels = ss->channels)) {
case 1:
sl->channelMask = SL_SPEAKER_FRONT_LEFT;
break;
case 2:
sl->channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
break;
default:
pa_log_error("Unsupported sample format: only stereo or mono channels are supported.");
return 1;
}
sl->sampleRate = ss->rate * 1000; // Hz to mHz
switch (ss->format) {
case PA_SAMPLE_S16LE: // fallthrough
case PA_SAMPLE_S24LE: // fallthrough
case PA_SAMPLE_S32LE: // fallthrough
case PA_SAMPLE_S16BE: // fallthrough
case PA_SAMPLE_S24BE: // fallthrough
case PA_SAMPLE_S32BE:
sl->formatType = SL_DATAFORMAT_PCM;
sl->representation = SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT;
break;
case PA_SAMPLE_FLOAT32LE: // fallthrough
case PA_SAMPLE_FLOAT32BE:
sl->formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
sl->representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
break;
default:
pa_log_error("Unsupported sample format: only s16/24/32 and f32 formats are supported.");
return 1;
}
sl->endianness = pa_sample_format_is_le(ss->format)
? SL_BYTEORDER_LITTLEENDIAN
: SL_BYTEORDER_BIGENDIAN;
sl->bitsPerSample = sl->containerSize = pa_sample_size(ss) * 8;
return 0;
}
static int pa_init_sles_record(struct userdata *u, pa_sample_spec *ss) {
pa_assert(u);
SLboolean required[2];
SLInterfaceID iidArray[2];
required[0] = SL_BOOLEAN_FALSE;
iidArray[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
required[1] = SL_BOOLEAN_FALSE;
iidArray[1] = SL_IID_ANDROIDCONFIGURATION;
pa_assert(u);
SLAndroidConfigurationItf configItf;
SLDataSource recSource;
SLDataLocator_IODevice ioDevice = {SL_DATALOCATOR_IODEVICE,
SL_IODEVICE_AUDIOINPUT,
SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
SLDataSink recDest;
SLDataLocator_AndroidSimpleBufferQueue recBuffQueue;
ioDevice.locatorType = SL_DATALOCATOR_IODEVICE;
ioDevice.deviceType = SL_IODEVICE_AUDIOINPUT;
ioDevice.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT;
ioDevice.device = NULL;
recSource.pLocator = (void *) &ioDevice;
recBuffQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE;
recBuffQueue.numBuffers = 8;
// check and convert the sample spec
SLAndroidDataFormat_PCM_EX pcm;
if (ss->format == PA_SAMPLE_FLOAT32LE) {
pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
} else {
pcm.formatType = SL_DATAFORMAT_PCM;
if (pa_sample_spec_to_sl_format(ss, &pcm))
return 1;
// common sles error handling
#define CHK(stmt) { \
SLresult res = (stmt); \
if (res != SL_RESULT_SUCCESS) { \
pa_log_error("Failed to initialize OpenSL ES: error %d at %s:%d", \
res, __FILE__, __LINE__); \
return 1; \
} \
}
pcm.numChannels = ss->channels;
pcm.sampleRate = ss->rate * 1000;
pcm.bitsPerSample = pcm.containerSize = pa_sample_size(ss) * 8;
pcm.channelMask = SL_SPEAKER_FRONT_LEFT;
pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
recDest.pLocator = (void *) &recBuffQueue;
recDest.pFormat = (void * ) &pcm;
CHK(slCreateEngine( &u->engineObject, 0, NULL, 0, NULL, NULL));
CHK((*u->engineObject)->Realize(u->engineObject, SL_BOOLEAN_FALSE));
CHK((*u->engineObject)->GetInterface(u->engineObject, SL_IID_ENGINE, (void*)&u->EngineItf));
CHK((*u->EngineItf)->CreateAudioRecorder(u->EngineItf, &u->RecorderObject, &recSource, &recDest,
2, iidArray, required));
CHK((*u->engineObject)->GetInterface(u->RecorderObject, SL_IID_ANDROIDCONFIGURATION, (void*)&configItf));
SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION;
CHK((*configItf)->SetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET,
&presetValue, sizeof(SLuint32)));
presetValue = SL_ANDROID_RECORDING_PRESET_NONE;
SLuint32 presetSize = 2*sizeof(SLuint32); // intentionally too big
CHK((*configItf)->GetConfiguration(configItf, SL_ANDROID_KEY_RECORDING_PRESET,
&presetSize, (void*)&presetValue));
if (presetValue != SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION) {
fprintf(stderr, "Error retrieved recording preset\n");
// create the engine
CHK(slCreateEngine(&u->EngineObject, 0, NULL, 0, NULL, NULL));
CHK(({
SLresult r = (*u->EngineObject)->Realize(u->EngineObject, SL_BOOLEAN_FALSE);
if (r == SL_RESULT_CONTENT_UNSUPPORTED)
pa_log(
"Failed to initialize OpenSL ES; did you grant Termux the "
"RECORD_AUDIO permission (you can use termux-microphone-record "
"from Termux:API for this)?"
);
r;
}));
// create the recorder
CHK((*u->EngineObject)->GetInterface(u->EngineObject, SL_IID_ENGINE, &u->EngineItf));
CHK(({
SLresult r = (*u->EngineItf)->CreateAudioRecorder(u->EngineItf,
&u->RecorderObject,
// source/sink
&(SLDataSource){
.pLocator = &(SLDataLocator_IODevice){
.locatorType = SL_DATALOCATOR_IODEVICE,
.deviceType = SL_IODEVICE_AUDIOINPUT,
.deviceID = SL_DEFAULTDEVICEID_AUDIOINPUT,
.device = NULL,
},
.pFormat = NULL,
},
&(SLDataSink){
.pLocator = &(SLDataLocator_AndroidSimpleBufferQueue){
.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
.numBuffers = 8,
},
.pFormat = &pcm,
},
// required interfaces
2,
(SLInterfaceID[]){SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION},
(SLboolean[]) {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}
);
if (r == SL_RESULT_CONTENT_UNSUPPORTED)
pa_log(
"Failed to initialize OpenSL ES; try checking logcat and "
"searching for messages with 'libOpenSLES:'"
);
r;
}));
// update the configuration
CHK((*u->RecorderObject)->GetInterface(u->RecorderObject, SL_IID_ANDROIDCONFIGURATION, &u->ConfigurationItf));
CHK((*u->ConfigurationItf)->SetConfiguration(u->ConfigurationItf, SL_ANDROID_KEY_RECORDING_PRESET, &(SLuint32){SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION}, sizeof(SLuint32)));
// realize the recorder
CHK((*u->RecorderObject)->Realize(u->RecorderObject, SL_BOOLEAN_FALSE));
CHK((*u->RecorderObject)->GetInterface(u->RecorderObject, SL_IID_RECORD, &u->RecordItf));
// register the callback
CHK((*u->RecorderObject)->GetInterface(u->RecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &u->BufferQueueItf));
CHK((*u->BufferQueueItf)->RegisterCallback(u->BufferQueueItf, sles_callback, u));
// cleanup
#undef CHK
return 0;
}
CHK((*u->engineObject)->Realize(u->RecorderObject, SL_BOOLEAN_FALSE));
CHK((*u->RecorderObject)->GetInterface(u->RecorderObject, SL_IID_RECORD, (void*)&u->recordItf));
CHK((*u->RecorderObject)->GetInterface(u->RecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
(void*)&u->recBuffQueueItf));
CHK((*u->recBuffQueueItf)->RegisterCallback(u->recBuffQueueItf, sles_callback, u));
return 0;
static void thread_func(void *userdata) {
struct userdata *u = userdata;
pa_assert(u);
fail:
return -1;
pa_thread_mq_install(&u->thread_mq);
for (;;) {
int ret;
if ((ret = pa_rtpoll_run(u->rtpoll)) < 0) {
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);
break;
}
if (ret == 0) {
break;
}
}
}
static int state_func(pa_source *s, pa_source_state_t state, pa_suspend_cause_t suspend_cause) {
struct userdata *u = s->userdata;
pa_assert(u);
int pa__init(pa_module*m) {
if (PA_SOURCE_IS_OPENED(s->state) && (state == PA_SOURCE_SUSPENDED || state == PA_SOURCE_UNLINKED))
(*u->RecordItf)->SetRecordState(u->RecordItf, SL_RECORDSTATE_STOPPED);
else if ((s->state == PA_SOURCE_SUSPENDED || s->state == PA_SOURCE_INIT) && PA_SOURCE_IS_OPENED(state))
(*u->RecordItf)->SetRecordState(u->RecordItf, SL_RECORDSTATE_RECORDING);
return 0;
}
int pa__init(pa_module *m) {
struct userdata *u = NULL;
pa_modargs *ma;
pa_source_new_data data;
pa_modargs *ma = NULL;
pa_source_new_data source_data;
pa_sample_spec ss;
uint32_t latency = 0;
pa_assert(m);
m->userdata = u = pa_xnew0(struct userdata, 1);
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
pa_log("failed to parse module arguments.");
goto fail;
}
ss = m->core->default_sample_spec;
switch (ss.format) {
case PA_SAMPLE_S16LE:
case PA_SAMPLE_S24LE:
case PA_SAMPLE_S32LE:
case PA_SAMPLE_FLOAT32LE:
break;
default:
pa_log("Sample format not supported");
goto fail;
}
ss.channels = 1;
if (pa_modargs_get_sample_rate(ma, &ss.rate) < 0) {
pa_log("Invalid rate specification");
pa_log("Failed to parse module arguments.");
goto fail;
}
u->core = m->core;
u->core = m->core;
u->module = m;
u->rtpoll = pa_rtpoll_new();
if (pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll) < 0) {
pa_log("pa_thread_mq_init() failed.");
goto fail;
}
u->sles_msgq = pa_asyncmsgq_new(0);
if (!u->sles_msgq) {
if (!(u->sles_msgq = pa_asyncmsgq_new(0))) {
pa_log("pa_asyncmsgq_new() failed.");
goto fail;
}
u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->sles_msgq);
pa_source_new_data_init(&data);
data.driver = __FILE__;
data.module = m;
pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("OpenSL ES Input"));
pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
pa_source_new_data_set_sample_spec(&data, &ss);
u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY - 1, u->sles_msgq);
if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
pa_log("Invalid properties");
pa_source_new_data_done(&data);
pa_source_new_data_init(&source_data);
source_data.driver = __FILE__;
source_data.module = m;
ss = m->core->default_sample_spec;
if (pa_modargs_get_sample_rate(ma, &ss.rate) < 0) {
pa_log("Invalid rate specification.");
goto fail;
}
u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
pa_source_new_data_done(&data);
if (!u->source) {
if (pa_init_sles_record(u, &ss))
goto fail;
pa_source_new_data_set_name(&source_data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
pa_source_new_data_set_sample_spec(&source_data, &ss);
pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "OpenSL ES Input");
pa_proplist_sets(source_data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
if (pa_modargs_get_proplist(ma, "source_properties", source_data.proplist, PA_UPDATE_REPLACE) < 0) {
pa_log("Invalid properties.");
pa_source_new_data_done(&source_data);
goto fail;
}
if (!(u->source = pa_source_new(m->core, &source_data, PA_SOURCE_LATENCY))) {
pa_log("Failed to create source.");
pa_source_new_data_done(&source_data);
goto fail;
}
pa_source_new_data_done(&source_data);
u->source->userdata = u;
u->source->parent.process_msg = source_process_msg;
u->source->set_state_in_main_thread = state_func;
u->source->userdata = u;
u->block_usec = BLOCK_USEC;
pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
pa_source_set_rtpoll(u->source, u->rtpoll);
pa_modargs_get_value_u32(ma, "latency", &latency);
if (latency)
u->block_usec = latency * PA_USEC_PER_MSEC;
else
u->block_usec = BLOCK_USEC;
u->block_usec = latency
? PA_USEC_PER_MSEC * latency
: PA_USEC_PER_MSEC * 125;
pa_source_set_fixed_latency(u->source, u->block_usec);
u->nbytes = pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
u->buf = calloc(8, u->nbytes);
u->buf = calloc(1, u->nbytes);
u->memchunk.memblock = pa_memblock_new_fixed(m->core->mempool, u->buf, u->nbytes, false);
if (pa_init_sles_record(u, &ss) < 0)
goto fail;
if (!(u->thread = pa_thread_new("sles_source", thread_func, u))) {
if (!(u->thread = pa_thread_new("sles-source", thread_func, u))) {
pa_log("Failed to create thread.");
goto fail;
}
pa_source_put(u->source);
sles_callback(u->recBuffQueueItf, u);
sles_callback(u->BufferQueueItf, u);
pa_modargs_free(ma);
return 0;
@ -345,14 +374,11 @@ int pa__init(pa_module*m) {
fail:
if (ma)
pa_modargs_free(ma);
pa__done(m);
return -1;
}
#undef CHK
int pa__get_n_used(pa_module *m) {
struct userdata *u;
@ -361,9 +387,8 @@ int pa__get_n_used(pa_module *m) {
return pa_source_linked_by(u->source);
}
#define DESTROY(object) if (u->object) (*u->object)->Destroy(u->object);
void pa__done(pa_module*m) {
void pa__done(pa_module *m) {
struct userdata *u;
pa_assert(m);
@ -374,22 +399,27 @@ void pa__done(pa_module*m) {
if (u->source)
pa_source_unlink(u->source);
DESTROY(RecorderObject);
DESTROY(engineObject);
if (u->RecorderObject)
(*u->RecorderObject)->Destroy(u->RecorderObject);
if (u->EngineObject)
(*u->EngineObject)->Destroy(u->EngineObject);
if (u->memchunk.memblock)
pa_memblock_unref_fixed(u->memchunk.memblock);
free(u->buf);
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->source)
pa_source_unref(u->source);
if (u->memchunk.memblock)
pa_memblock_unref(u->memchunk.memblock);
if (u->rtpoll_item)
pa_rtpoll_item_free(u->rtpoll_item);
if (u->sles_msgq)
pa_asyncmsgq_unref(u->sles_msgq);
if (u->rtpoll)
pa_rtpoll_free(u->rtpoll);