opensles output for libpulseaudio from @twaik (#1968)
[termux-packages] / packages / libpulseaudio / module-sles-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
32
33 #include <pulsecore/i18n.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/rtpoll.h>
43
44 #include <SLES/OpenSLES.h>
45
46 #include "module-sles-sink-symdef.h"
47
48 #ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
49 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
50 #define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
51 #define BufferQueueItf SLAndroidSimpleBufferQueueItf
52 #define BufferQueueState SLAndroidSimpleBufferQueueState
53 #define IID_BUFFERQUEUE_USED SL_IID_ANDROIDSIMPLEBUFFERQUEUE
54 #define INDEX index
55 #else
56 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE
57 #define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
58 #define BufferQueueItf SLBufferQueueItf
59 #define BufferQueueState SLBufferQueueState
60 #define IID_BUFFERQUEUE_USED IID_BUFFERQUEUE
61 #define INDEX playIndex
62 #endif
63
64 #define checkResult(r) do { \
65 if ((r) != SL_RESULT_SUCCESS) { \
66 if ((r) == SL_RESULT_PARAMETER_INVALID) fprintf(stderr, "error SL_RESULT_PARAMETER_INVALID at %s:%d\n", __FILE__, __LINE__); \
67 else if ((r) == SL_RESULT_PRECONDITIONS_VIOLATED ) fprintf(stderr, "error SL_RESULT_PRECONDITIONS_VIOLATED at %s:%d\n", __FILE__, __LINE__); \
68 else fprintf(stderr, "error %d at %s:%d\n", (int) r, __FILE__, __LINE__); \
69 } \
70 } while (0)
71 typedef struct {
72 short left;
73 short right;
74 } frame_t;
75
76 PA_MODULE_AUTHOR("Lennart Poettering, Nathan Martynov");
77 PA_MODULE_DESCRIPTION("Android OpenSL ES sink");
78 PA_MODULE_VERSION(PACKAGE_VERSION);
79 PA_MODULE_LOAD_ONCE(false);
80 PA_MODULE_USAGE(
81 "sink_name=<name for the sink> "
82 "sink_properties=<properties for the sink> ");
83
84 #define DEFAULT_SINK_NAME "OpenSL ES sink"
85 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
86
87 struct userdata {
88 pa_core *core;
89 pa_module *module;
90 pa_sink *sink;
91
92 pa_thread *thread;
93 pa_thread_mq thread_mq;
94 pa_rtpoll *rtpoll;
95
96 pa_usec_t block_usec;
97 pa_usec_t timestamp;
98
99 pa_memchunk memchunk;
100
101 SLObjectItf engineObject;
102 SLEngineItf engineEngine;
103
104 // output mix interfaces
105 SLObjectItf outputMixObject;
106
107 // buffer queue player interfaces
108 SLObjectItf bqPlayerObject;
109 SLPlayItf bqPlayerPlay;
110 BufferQueueItf bqPlayerBufferQueue;
111 };
112
113 static const char* const valid_modargs[] = {
114 "sink_name",
115 "sink_properties",
116 NULL
117 };
118
119 static int sink_process_msg(
120 pa_msgobject *o,
121 int code,
122 void *data,
123 int64_t offset,
124 pa_memchunk *chunk) {
125
126 struct userdata *u = PA_SINK(o)->userdata;
127
128 switch (code) {
129 case PA_SINK_MESSAGE_SET_STATE:
130
131 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED || pa_sink_get_state(u->sink) == PA_SINK_INIT) {
132 if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING || PA_PTR_TO_UINT(data) == PA_SINK_IDLE)
133 u->timestamp = pa_rtclock_now();
134 }
135
136 break;
137
138 case PA_SINK_MESSAGE_GET_LATENCY: {
139 pa_usec_t now;
140
141 now = pa_rtclock_now();
142 *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
143
144 return 0;
145 }
146 }
147
148 return pa_sink_process_msg(o, code, data, offset, chunk);
149 }
150
151 static void sink_update_requested_latency_cb(pa_sink *s) {
152 struct userdata *u;
153 size_t nbytes;
154
155 pa_sink_assert_ref(s);
156 pa_assert_se(u = s->userdata);
157
158 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
159
160 if (u->block_usec == (pa_usec_t) -1)
161 u->block_usec = s->thread_info.max_latency;
162
163 nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
164 pa_sink_set_max_rewind_within_thread(s, nbytes);
165 pa_sink_set_max_request_within_thread(s, nbytes);
166 }
167
168 void pa_init_sles_player(struct userdata *s)
169 {
170 if (s == NULL) return;
171 SLresult result;
172
173 // create engine
174 result = slCreateEngine(&(s->engineObject), 0, NULL, 0, NULL, NULL); checkResult(result);
175 result = (*s->engineObject)->Realize(s->engineObject, SL_BOOLEAN_FALSE); checkResult(result);
176
177 result = (*s->engineObject)->GetInterface(s->engineObject, SL_IID_ENGINE, &(s->engineEngine)); checkResult(result);
178
179 // create output mix
180 result = (*s->engineEngine)->CreateOutputMix(s->engineEngine, &(s->outputMixObject), 0, NULL, NULL); checkResult(result);
181 result = (*s->outputMixObject)->Realize(s->outputMixObject, SL_BOOLEAN_FALSE); checkResult(result);
182
183 // create audio player
184
185 SLDataLocator_OutputMix locator_outputmix;
186 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
187 locator_outputmix.outputMix = s->outputMixObject;
188
189 SLDataLocator_BufferQueue locator_bufferqueue;
190 locator_bufferqueue.locatorType = DATALOCATOR_BUFFERQUEUE;
191 locator_bufferqueue.numBuffers = 50;
192
193 SLDataFormat_PCM pcm;
194 pcm.formatType = SL_DATAFORMAT_PCM;
195 pcm.numChannels = 2;
196 pcm.samplesPerSec = SL_SAMPLINGRATE_32;
197 pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
198 pcm.containerSize = 16;
199 pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
200 pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
201
202 SLDataSource audiosrc;
203 audiosrc.pLocator = &locator_bufferqueue;
204 audiosrc.pFormat = &pcm;
205
206 SLDataSink audiosnk;
207 audiosnk.pLocator = &locator_outputmix;
208 audiosnk.pFormat = NULL;
209
210 SLInterfaceID ids[1] = {IID_BUFFERQUEUE};
211 SLboolean flags[1] = {SL_BOOLEAN_TRUE};
212 result = (*s->engineEngine)->CreateAudioPlayer(s->engineEngine, &s->bqPlayerObject, &audiosrc, &audiosnk, 1, ids, flags); checkResult(result);
213 result = (*s->bqPlayerObject)->Realize(s->bqPlayerObject, SL_BOOLEAN_FALSE); checkResult(result);
214
215 result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, SL_IID_PLAY, &s->bqPlayerPlay); checkResult(result);
216 result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, IID_BUFFERQUEUE_USED, &s->bqPlayerBufferQueue); checkResult(result);
217
218 result = (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_PLAYING); checkResult(result);
219 }
220
221 void pa_destroy_sles_player(struct userdata *s){
222 if (s == NULL) return;
223 (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_STOPPED);
224 (*s->bqPlayerObject)->Destroy(s->bqPlayerObject);
225 (*s->outputMixObject)->Destroy(s->outputMixObject);
226 (*s->engineObject)->Destroy(s->engineObject);
227 }
228
229 static void process_render(struct userdata *u, pa_usec_t now) {
230 size_t ate = 0;
231
232 pa_assert(u);
233
234 /* This is the configured latency. Sink inputs connected to us
235 might not have a single frame more than the maxrequest value
236 queued. Hence: at maximum read this many bytes from the sink
237 inputs. */
238
239 /* Fill the buffer up the latency size */
240 while (u->timestamp < now + u->block_usec) {
241 void *p;
242
243 pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk);
244 p = pa_memblock_acquire(u->memchunk.memblock);
245 (*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, (uint8_t*) p + u->memchunk.index, u->memchunk.length);
246 pa_memblock_release(u->memchunk.memblock);
247
248 /* pa_log_debug("Ate %lu bytes.", (unsigned long) chunk.length); */
249 u->timestamp += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
250
251 ate += u->memchunk.length;
252
253 if (ate >= u->sink->thread_info.max_request)
254 break;
255 }
256
257 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
258 }
259
260 static void thread_func(void *userdata) {
261 struct userdata *u = userdata;
262
263 pa_assert(u);
264
265 pa_log_debug("Thread starting up");
266
267 pa_thread_mq_install(&u->thread_mq);
268
269 u->timestamp = pa_rtclock_now();
270
271 for (;;) {
272 pa_usec_t now = 0;
273 int ret;
274
275 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
276 now = pa_rtclock_now();
277
278 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
279 pa_sink_process_rewind(u->sink, 0);
280
281 /* Render some data and drop it immediately */
282 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
283 if (u->timestamp <= now)
284 process_render(u, now);
285
286 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
287 } else
288 pa_rtpoll_set_timer_disabled(u->rtpoll);
289
290 /* Hmm, nothing to do. Let's sleep */
291 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
292 goto fail;
293
294 if (ret == 0)
295 goto finish;
296 }
297
298 fail:
299 /* If this was no regular exit from the loop we have to continue
300 * processing messages until we received PA_MESSAGE_SHUTDOWN */
301 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
302 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
303
304 finish:
305 pa_log_debug("Thread shutting down");
306 }
307
308 int pa__init(pa_module*m) {
309 struct userdata *u = NULL;
310 pa_sample_spec ss;
311 pa_channel_map map;
312 pa_modargs *ma = NULL;
313 pa_sink_new_data data;
314 size_t nbytes;
315
316 pa_assert(m);
317
318 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
319 pa_log("Failed to parse module arguments.");
320 goto fail;
321 }
322
323 // High rate causes glitches on some devices, this is needed to prevent it
324 ss.rate = 32000;
325 ss.channels = 2;
326 ss.format = PA_SAMPLE_S16LE;
327 map = m->core->default_channel_map;
328
329
330 m->userdata = u = pa_xnew0(struct userdata, 1);
331 u->core = m->core;
332 u->module = m;
333 u->rtpoll = pa_rtpoll_new();
334 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
335
336 pa_init_sles_player(u);
337 int buff[2] = {0, 0};
338 (*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, buff, 1);
339
340 pa_sink_new_data_init(&data);
341 data.driver = __FILE__;
342 data.module = m;
343 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
344 pa_sink_new_data_set_sample_spec(&data, &ss);
345 pa_sink_new_data_set_channel_map(&data, &map);
346 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("Null Output"));
347 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
348
349 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
350 pa_log("Invalid properties");
351 pa_sink_new_data_done(&data);
352 goto fail;
353 }
354
355 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
356 pa_sink_new_data_done(&data);
357
358 if (!u->sink) {
359 pa_log("Failed to create sink object.");
360 goto fail;
361 }
362
363 u->sink->parent.process_msg = sink_process_msg;
364 u->sink->update_requested_latency = sink_update_requested_latency_cb;
365 u->sink->userdata = u;
366
367 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
368 pa_sink_set_rtpoll(u->sink, u->rtpoll);
369
370 u->block_usec = BLOCK_USEC;
371 nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
372 pa_sink_set_max_rewind(u->sink, nbytes);
373 pa_sink_set_max_request(u->sink, nbytes);
374
375 if (!(u->thread = pa_thread_new("null-sink", thread_func, u))) {
376 pa_log("Failed to create thread.");
377 goto fail;
378 }
379
380 pa_sink_set_latency_range(u->sink, 0, BLOCK_USEC);
381
382 pa_sink_put(u->sink);
383
384 pa_modargs_free(ma);
385
386 return 0;
387
388 fail:
389 if (ma)
390 pa_modargs_free(ma);
391
392 pa__done(m);
393
394 return -1;
395 }
396
397 int pa__get_n_used(pa_module *m) {
398 struct userdata *u;
399
400 pa_assert(m);
401 pa_assert_se(u = m->userdata);
402
403 return pa_sink_linked_by(u->sink);
404 }
405
406 void pa__done(pa_module*m) {
407 struct userdata *u;
408
409 pa_assert(m);
410
411 if (!(u = m->userdata))
412 return;
413
414 if (u->sink)
415 pa_sink_unlink(u->sink);
416
417 if (u->thread) {
418 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
419 pa_thread_free(u->thread);
420 }
421
422 pa_thread_mq_done(&u->thread_mq);
423
424 if (u->sink)
425 pa_sink_unref(u->sink);
426
427 if (u->rtpoll)
428 pa_rtpoll_free(u->rtpoll);
429
430 pa_xfree(u);
431 }