xorriso: remove unnecessary iconv dependency (#2368)
[termux-packages] / packages / libpulseaudio / module-sles-sink.c
CommitLineData
674a9bc3 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
bdf36953 48//Only certain interfaces are supported by the fast mixer. These are:
49//SL_IID_ANDROIDSIMPLEBUFFERQUEUE
50//SL_IID_VOLUME
51//SL_IID_MUTESOLO
52#define USE_ANDROID_SIMPLE_BUFFER_QUEUE
53
674a9bc3 54#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
1c285bb8 55 #include <SLES/OpenSLES_Android.h>
56 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
57 #define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
58 #define BufferQueueItf SLAndroidSimpleBufferQueueItf
59 #define BufferQueueState SLAndroidSimpleBufferQueueState
60 #define IID_BUFFERQUEUE_USED SL_IID_ANDROIDSIMPLEBUFFERQUEUE
61 #define INDEX index
674a9bc3 62#else
1c285bb8 63 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE
64 #define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
65 #define BufferQueueItf SLBufferQueueItf
66 #define BufferQueueState SLBufferQueueState
67 #define IID_BUFFERQUEUE_USED IID_BUFFERQUEUE
68 #define INDEX playIndex
674a9bc3 69#endif
70
71#define checkResult(r) do { \
72 if ((r) != SL_RESULT_SUCCESS) { \
73 if ((r) == SL_RESULT_PARAMETER_INVALID) fprintf(stderr, "error SL_RESULT_PARAMETER_INVALID at %s:%d\n", __FILE__, __LINE__); \
74 else if ((r) == SL_RESULT_PRECONDITIONS_VIOLATED ) fprintf(stderr, "error SL_RESULT_PRECONDITIONS_VIOLATED at %s:%d\n", __FILE__, __LINE__); \
75 else fprintf(stderr, "error %d at %s:%d\n", (int) r, __FILE__, __LINE__); \
76 } \
77 } while (0)
674a9bc3 78
79PA_MODULE_AUTHOR("Lennart Poettering, Nathan Martynov");
80PA_MODULE_DESCRIPTION("Android OpenSL ES sink");
81PA_MODULE_VERSION(PACKAGE_VERSION);
82PA_MODULE_LOAD_ONCE(false);
83PA_MODULE_USAGE(
84 "sink_name=<name for the sink> "
1c285bb8 85 "sink_properties=<properties for the sink> "
86 "rate=<sampling rate> ");
674a9bc3 87
88#define DEFAULT_SINK_NAME "OpenSL ES sink"
89#define BLOCK_USEC (PA_USEC_PER_SEC * 2)
90
bdf36953 91typedef struct pa_memblock_queue_t {
92 pa_memblock *memblock;
93 struct pa_memblock_queue_t* next;
94} pa_memblock_queue;
95
674a9bc3 96struct userdata {
97 pa_core *core;
98 pa_module *module;
99 pa_sink *sink;
100
101 pa_thread *thread;
102 pa_thread_mq thread_mq;
103 pa_rtpoll *rtpoll;
104
105 pa_usec_t block_usec;
106 pa_usec_t timestamp;
107
108 pa_memchunk memchunk;
109
110 SLObjectItf engineObject;
111 SLEngineItf engineEngine;
112
113 // output mix interfaces
114 SLObjectItf outputMixObject;
115
116 // buffer queue player interfaces
117 SLObjectItf bqPlayerObject;
118 SLPlayItf bqPlayerPlay;
119 BufferQueueItf bqPlayerBufferQueue;
bdf36953 120
121 pa_memblock_queue* current;
122 pa_memblock_queue* last;
674a9bc3 123};
124
125static const char* const valid_modargs[] = {
126 "sink_name",
127 "sink_properties",
1c285bb8 128 "rate",
674a9bc3 129 NULL
130};
131
132static int sink_process_msg(
133 pa_msgobject *o,
134 int code,
135 void *data,
136 int64_t offset,
137 pa_memchunk *chunk) {
138
139 struct userdata *u = PA_SINK(o)->userdata;
140
141 switch (code) {
142 case PA_SINK_MESSAGE_SET_STATE:
143
144 if (pa_sink_get_state(u->sink) == PA_SINK_SUSPENDED || pa_sink_get_state(u->sink) == PA_SINK_INIT) {
145 if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING || PA_PTR_TO_UINT(data) == PA_SINK_IDLE)
146 u->timestamp = pa_rtclock_now();
147 }
148
149 break;
150
151 case PA_SINK_MESSAGE_GET_LATENCY: {
152 pa_usec_t now;
153
154 now = pa_rtclock_now();
155 *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
156
157 return 0;
158 }
159 }
160
161 return pa_sink_process_msg(o, code, data, offset, chunk);
162}
163
164static void sink_update_requested_latency_cb(pa_sink *s) {
165 struct userdata *u;
166 size_t nbytes;
167
168 pa_sink_assert_ref(s);
169 pa_assert_se(u = s->userdata);
170
171 u->block_usec = pa_sink_get_requested_latency_within_thread(s);
172
173 if (u->block_usec == (pa_usec_t) -1)
174 u->block_usec = s->thread_info.max_latency;
175
176 nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec);
177 pa_sink_set_max_rewind_within_thread(s, nbytes);
178 pa_sink_set_max_request_within_thread(s, nbytes);
179}
180
bdf36953 181static void pa_sles_callback(BufferQueueItf bq, void *context){
182 struct userdata* s = (struct userdata*) context;
183 pa_memblock_queue* next;
184 if (s->current != NULL){
185 if (s->current->memblock != NULL) pa_memblock_unref(s->current->memblock);
186 next = s->current->next;
187 free(s->current);
188 s->current = next;
189 }
190}
191
1c285bb8 192static int pa_init_sles_player(struct userdata *s, SLint32 sl_rate)
674a9bc3 193{
1c285bb8 194 if (s == NULL) return -1;
674a9bc3 195 SLresult result;
196
197 // create engine
198 result = slCreateEngine(&(s->engineObject), 0, NULL, 0, NULL, NULL); checkResult(result);
199 result = (*s->engineObject)->Realize(s->engineObject, SL_BOOLEAN_FALSE); checkResult(result);
200
201 result = (*s->engineObject)->GetInterface(s->engineObject, SL_IID_ENGINE, &(s->engineEngine)); checkResult(result);
202
203 // create output mix
204 result = (*s->engineEngine)->CreateOutputMix(s->engineEngine, &(s->outputMixObject), 0, NULL, NULL); checkResult(result);
205 result = (*s->outputMixObject)->Realize(s->outputMixObject, SL_BOOLEAN_FALSE); checkResult(result);
206
207 // create audio player
208
209 SLDataLocator_OutputMix locator_outputmix;
210 locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
211 locator_outputmix.outputMix = s->outputMixObject;
212
213 SLDataLocator_BufferQueue locator_bufferqueue;
214 locator_bufferqueue.locatorType = DATALOCATOR_BUFFERQUEUE;
215 locator_bufferqueue.numBuffers = 50;
216
bdf36953 217 if (sl_rate < SL_SAMPLINGRATE_8 || sl_rate > SL_SAMPLINGRATE_192) {
1c285bb8 218 pa_log("Incompatible sample rate");
219 return -1;
220 }
221
674a9bc3 222 SLDataFormat_PCM pcm;
223 pcm.formatType = SL_DATAFORMAT_PCM;
224 pcm.numChannels = 2;
1c285bb8 225 pcm.samplesPerSec = sl_rate;
674a9bc3 226 pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
227 pcm.containerSize = 16;
228 pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
229 pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
230
231 SLDataSource audiosrc;
232 audiosrc.pLocator = &locator_bufferqueue;
233 audiosrc.pFormat = &pcm;
234
235 SLDataSink audiosnk;
236 audiosnk.pLocator = &locator_outputmix;
237 audiosnk.pFormat = NULL;
238
239 SLInterfaceID ids[1] = {IID_BUFFERQUEUE};
240 SLboolean flags[1] = {SL_BOOLEAN_TRUE};
241 result = (*s->engineEngine)->CreateAudioPlayer(s->engineEngine, &s->bqPlayerObject, &audiosrc, &audiosnk, 1, ids, flags); checkResult(result);
242 result = (*s->bqPlayerObject)->Realize(s->bqPlayerObject, SL_BOOLEAN_FALSE); checkResult(result);
243
244 result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, SL_IID_PLAY, &s->bqPlayerPlay); checkResult(result);
245 result = (*s->bqPlayerObject)->GetInterface(s->bqPlayerObject, IID_BUFFERQUEUE_USED, &s->bqPlayerBufferQueue); checkResult(result);
246
bdf36953 247 result = (*s->bqPlayerBufferQueue)->RegisterCallback(s->bqPlayerBufferQueue, pa_sles_callback, s); checkResult(result);
248
674a9bc3 249 result = (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_PLAYING); checkResult(result);
bdf36953 250
1c285bb8 251 return 0;
674a9bc3 252}
253
1c285bb8 254static void pa_destroy_sles_player(struct userdata *s){
674a9bc3 255 if (s == NULL) return;
256 (*s->bqPlayerPlay)->SetPlayState(s->bqPlayerPlay, SL_PLAYSTATE_STOPPED);
257 (*s->bqPlayerObject)->Destroy(s->bqPlayerObject);
258 (*s->outputMixObject)->Destroy(s->outputMixObject);
259 (*s->engineObject)->Destroy(s->engineObject);
260}
261
262static void process_render(struct userdata *u, pa_usec_t now) {
bdf36953 263 pa_memblock_queue* current_block;
674a9bc3 264 size_t ate = 0;
265
266 pa_assert(u);
267
268 /* This is the configured latency. Sink inputs connected to us
269 might not have a single frame more than the maxrequest value
270 queued. Hence: at maximum read this many bytes from the sink
271 inputs. */
272
273 /* Fill the buffer up the latency size */
274 while (u->timestamp < now + u->block_usec) {
275 void *p;
276
277 pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk);
278 p = pa_memblock_acquire(u->memchunk.memblock);
279 (*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, (uint8_t*) p + u->memchunk.index, u->memchunk.length);
280 pa_memblock_release(u->memchunk.memblock);
281
674a9bc3 282 u->timestamp += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
674a9bc3 283 ate += u->memchunk.length;
bdf36953 284
285 current_block = malloc(sizeof(pa_memblock_queue));
286 memset(current_block, 0, sizeof(pa_memblock_queue));
287
288 current_block->memblock = u->memchunk.memblock;
289 if (u->current == NULL) { u->current = current_block; }
290 if (u->last == NULL) { u->last = current_block; }
291 else {
292 u->last->next = current_block;
293 u->last = current_block;
294 }
295
296 //pa_memblock_unref(u->memchunk.memblock);
297 pa_memchunk_reset(&u->memchunk);
1c285bb8 298 if (ate >= u->sink->thread_info.max_request) break;
674a9bc3 299 }
674a9bc3 300}
301
302static void thread_func(void *userdata) {
303 struct userdata *u = userdata;
304
305 pa_assert(u);
306
307 pa_log_debug("Thread starting up");
308
309 pa_thread_mq_install(&u->thread_mq);
310
311 u->timestamp = pa_rtclock_now();
312
313 for (;;) {
314 pa_usec_t now = 0;
315 int ret;
316
317 if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
318 now = pa_rtclock_now();
319
320 if (PA_UNLIKELY(u->sink->thread_info.rewind_requested))
321 pa_sink_process_rewind(u->sink, 0);
322
323 /* Render some data and drop it immediately */
324 if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) {
325 if (u->timestamp <= now)
326 process_render(u, now);
327
328 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp);
329 } else
330 pa_rtpoll_set_timer_disabled(u->rtpoll);
331
332 /* Hmm, nothing to do. Let's sleep */
333 if ((ret = pa_rtpoll_run(u->rtpoll)) < 0)
334 goto fail;
335
336 if (ret == 0)
337 goto finish;
338 }
339
340fail:
341 /* If this was no regular exit from the loop we have to continue
342 * processing messages until we received PA_MESSAGE_SHUTDOWN */
343 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
344 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
345
346finish:
347 pa_log_debug("Thread shutting down");
348}
bdf36953 349
350static int getenv_int(const char * env, size_t min_len){
351 char * got_env = getenv(env);
352 int ret = 0;
353 if (got_env != NULL && strlen(got_env) >= min_len) ret = atoi(got_env); //"8000" is 4 symbols
354 return ret;
1c285bb8 355}
674a9bc3 356
357int pa__init(pa_module*m) {
358 struct userdata *u = NULL;
359 pa_sample_spec ss;
360 pa_channel_map map;
361 pa_modargs *ma = NULL;
362 pa_sink_new_data data;
363 size_t nbytes;
364
365 pa_assert(m);
366
367 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
368 pa_log("Failed to parse module arguments.");
369 goto fail;
370 }
371
372 // High rate causes glitches on some devices, this is needed to prevent it
1c285bb8 373 //ss.rate = 32000;
374 //ss.channels = 2;
375 //ss.format = PA_SAMPLE_S16LE;
376
377 //OK. That will allow users to define sampling rate under his responsibility
378 ss = m->core->default_sample_spec;
674a9bc3 379 map = m->core->default_channel_map;
1c285bb8 380 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
381 pa_log("Invalid sample format specification or channel map");
382 goto fail;
383 }
674a9bc3 384
1c285bb8 385 //Needed. Don't touch
386 ss.channels = 2;
387 ss.format = PA_SAMPLE_S16LE;
1c285bb8 388
674a9bc3 389 m->userdata = u = pa_xnew0(struct userdata, 1);
bdf36953 390
391 int forceFormat = getenv_int("PROPERTY_OUTPUT_SAMPLE_RATE", 4); //"8000" is 4 symbols
392 if (forceFormat >= 8000 && forceFormat <= 192000) {
393 ss.rate = forceFormat;
394 pa_log_info("Sample rate was forced to be %u\n", ss.rate);
395 }
396
674a9bc3 397 u->core = m->core;
398 u->module = m;
399 u->rtpoll = pa_rtpoll_new();
400 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
401
bdf36953 402 //Pulseaudio uses samples per sec but OpenSL ES uses samples per ms
403 if (pa_init_sles_player(u, ss.rate * 1000) < 0)
1c285bb8 404 goto fail;
bdf36953 405 //int buff[2] = {0, 0};
406 //(*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, buff, 1);
674a9bc3 407
408 pa_sink_new_data_init(&data);
409 data.driver = __FILE__;
410 data.module = m;
411 pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
412 pa_sink_new_data_set_sample_spec(&data, &ss);
413 pa_sink_new_data_set_channel_map(&data, &map);
1c285bb8 414 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, _("OpenSL ES Output"));
674a9bc3 415 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
416
417 if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
418 pa_log("Invalid properties");
419 pa_sink_new_data_done(&data);
420 goto fail;
421 }
422
423 u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
424 pa_sink_new_data_done(&data);
425
426 if (!u->sink) {
427 pa_log("Failed to create sink object.");
428 goto fail;
429 }
430
431 u->sink->parent.process_msg = sink_process_msg;
432 u->sink->update_requested_latency = sink_update_requested_latency_cb;
433 u->sink->userdata = u;
434
435 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
436 pa_sink_set_rtpoll(u->sink, u->rtpoll);
437
438 u->block_usec = BLOCK_USEC;
439 nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
440 pa_sink_set_max_rewind(u->sink, nbytes);
441 pa_sink_set_max_request(u->sink, nbytes);
442
1c285bb8 443 if (!(u->thread = pa_thread_new("sles-sink", thread_func, u))) {
674a9bc3 444 pa_log("Failed to create thread.");
445 goto fail;
446 }
447
448 pa_sink_set_latency_range(u->sink, 0, BLOCK_USEC);
449
450 pa_sink_put(u->sink);
451
452 pa_modargs_free(ma);
453
454 return 0;
455
456fail:
457 if (ma)
458 pa_modargs_free(ma);
459
460 pa__done(m);
461
462 return -1;
463}
464
465int pa__get_n_used(pa_module *m) {
466 struct userdata *u;
467
468 pa_assert(m);
469 pa_assert_se(u = m->userdata);
470
471 return pa_sink_linked_by(u->sink);
472}
473
474void pa__done(pa_module*m) {
475 struct userdata *u;
476
477 pa_assert(m);
478
479 if (!(u = m->userdata))
480 return;
481
482 if (u->sink)
483 pa_sink_unlink(u->sink);
484
485 if (u->thread) {
486 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
487 pa_thread_free(u->thread);
488 }
bdf36953 489
490 if (u->engineObject){
491 pa_destroy_sles_player(u);
492 }
674a9bc3 493
494 pa_thread_mq_done(&u->thread_mq);
495
496 if (u->sink)
497 pa_sink_unref(u->sink);
498
499 if (u->rtpoll)
500 pa_rtpoll_free(u->rtpoll);
501
502 pa_xfree(u);
503}