2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
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.
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.
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/>.
29 #include <pulse/rtclock.h>
30 #include <pulse/timeval.h>
31 #include <pulse/xmalloc.h>
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>
44 #include <SLES/OpenSLES.h>
46 #include "module-sles-sink-symdef.h"
48 //Only certain interfaces are supported by the fast mixer. These are:
49 //SL_IID_ANDROIDSIMPLEBUFFERQUEUE
52 #define USE_ANDROID_SIMPLE_BUFFER_QUEUE
54 #ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
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
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
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__); \
79 PA_MODULE_AUTHOR("Lennart Poettering, Nathan Martynov");
80 PA_MODULE_DESCRIPTION("Android OpenSL ES sink");
81 PA_MODULE_VERSION(PACKAGE_VERSION
);
82 PA_MODULE_LOAD_ONCE(false);
84 "sink_name=<name for the sink> "
85 "sink_properties=<properties for the sink> "
86 "rate=<sampling rate> ");
88 #define DEFAULT_SINK_NAME "OpenSL ES sink"
89 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
91 typedef struct pa_memblock_queue_t
{
92 pa_memblock
*memblock
;
93 struct pa_memblock_queue_t
* next
;
102 pa_thread_mq thread_mq
;
105 pa_usec_t block_usec
;
108 pa_memchunk memchunk
;
110 SLObjectItf engineObject
;
111 SLEngineItf engineEngine
;
113 // output mix interfaces
114 SLObjectItf outputMixObject
;
116 // buffer queue player interfaces
117 SLObjectItf bqPlayerObject
;
118 SLPlayItf bqPlayerPlay
;
119 BufferQueueItf bqPlayerBufferQueue
;
121 pa_memblock_queue
* current
;
122 pa_memblock_queue
* last
;
125 static const char* const valid_modargs
[] = {
132 static int sink_process_msg(
137 pa_memchunk
*chunk
) {
139 struct userdata
*u
= PA_SINK(o
)->userdata
;
142 case PA_SINK_MESSAGE_SET_STATE
:
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();
151 case PA_SINK_MESSAGE_GET_LATENCY
: {
154 now
= pa_rtclock_now();
155 *((pa_usec_t
*) data
) = u
->timestamp
> now ? u
->timestamp
- now
: 0ULL;
161 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
164 static void sink_update_requested_latency_cb(pa_sink
*s
) {
168 pa_sink_assert_ref(s
);
169 pa_assert_se(u
= s
->userdata
);
171 u
->block_usec
= pa_sink_get_requested_latency_within_thread(s
);
173 if (u
->block_usec
== (pa_usec_t
) -1)
174 u
->block_usec
= s
->thread_info
.max_latency
;
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
);
181 static 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
;
192 static int pa_init_sles_player(struct userdata
*s
, SLint32 sl_rate
)
194 if (s
== NULL
) return -1;
198 result
= slCreateEngine(&(s
->engineObject
), 0, NULL
, 0, NULL
, NULL
); checkResult(result
);
199 result
= (*s
->engineObject
)->Realize(s
->engineObject
, SL_BOOLEAN_FALSE
); checkResult(result
);
201 result
= (*s
->engineObject
)->GetInterface(s
->engineObject
, SL_IID_ENGINE
, &(s
->engineEngine
)); checkResult(result
);
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
);
207 // create audio player
209 SLDataLocator_OutputMix locator_outputmix
;
210 locator_outputmix
.locatorType
= SL_DATALOCATOR_OUTPUTMIX
;
211 locator_outputmix
.outputMix
= s
->outputMixObject
;
213 SLDataLocator_BufferQueue locator_bufferqueue
;
214 locator_bufferqueue
.locatorType
= DATALOCATOR_BUFFERQUEUE
;
215 locator_bufferqueue
.numBuffers
= 50;
217 if (sl_rate
< SL_SAMPLINGRATE_8
|| sl_rate
> SL_SAMPLINGRATE_192
) {
218 pa_log("Incompatible sample rate");
222 SLDataFormat_PCM pcm
;
223 pcm
.formatType
= SL_DATAFORMAT_PCM
;
225 pcm
.samplesPerSec
= sl_rate
;
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
;
231 SLDataSource audiosrc
;
232 audiosrc
.pLocator
= &locator_bufferqueue
;
233 audiosrc
.pFormat
= &pcm
;
236 audiosnk
.pLocator
= &locator_outputmix
;
237 audiosnk
.pFormat
= NULL
;
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
);
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
);
247 result
= (*s
->bqPlayerBufferQueue
)->RegisterCallback(s
->bqPlayerBufferQueue
, pa_sles_callback
, s
); checkResult(result
);
249 result
= (*s
->bqPlayerPlay
)->SetPlayState(s
->bqPlayerPlay
, SL_PLAYSTATE_PLAYING
); checkResult(result
);
254 static void pa_destroy_sles_player(struct userdata
*s
){
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
);
262 static void process_render(struct userdata
*u
, pa_usec_t now
) {
263 pa_memblock_queue
* current_block
;
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
273 /* Fill the buffer up the latency size */
274 while (u
->timestamp
< now
+ u
->block_usec
) {
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
);
282 u
->timestamp
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
283 ate
+= u
->memchunk
.length
;
285 current_block
= malloc(sizeof(pa_memblock_queue
));
286 memset(current_block
, 0, sizeof(pa_memblock_queue
));
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
; }
292 u
->last
->next
= current_block
;
293 u
->last
= current_block
;
296 //pa_memblock_unref(u->memchunk.memblock);
297 pa_memchunk_reset(&u
->memchunk
);
298 if (ate
>= u
->sink
->thread_info
.max_request
) break;
302 static void thread_func(void *userdata
) {
303 struct userdata
*u
= userdata
;
307 pa_log_debug("Thread starting up");
309 pa_thread_mq_install(&u
->thread_mq
);
311 u
->timestamp
= pa_rtclock_now();
317 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
318 now
= pa_rtclock_now();
320 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
321 pa_sink_process_rewind(u
->sink
, 0);
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
);
328 pa_rtpoll_set_timer_absolute(u
->rtpoll
, u
->timestamp
);
330 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
332 /* Hmm, nothing to do. Let's sleep */
333 if ((ret
= pa_rtpoll_run(u
->rtpoll
)) < 0)
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
);
347 pa_log_debug("Thread shutting down");
350 static int getenv_int(const char * env
, size_t min_len
){
351 char * got_env
= getenv(env
);
353 if (got_env
!= NULL
&& strlen(got_env
) >= min_len
) ret
= atoi(got_env
); //"8000" is 4 symbols
357 int pa__init(pa_module
*m
) {
358 struct userdata
*u
= NULL
;
361 pa_modargs
*ma
= NULL
;
362 pa_sink_new_data data
;
367 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
368 pa_log("Failed to parse module arguments.");
372 // High rate causes glitches on some devices, this is needed to prevent it
375 //ss.format = PA_SAMPLE_S16LE;
377 //OK. That will allow users to define sampling rate under his responsibility
378 ss
= m
->core
->default_sample_spec
;
379 map
= m
->core
->default_channel_map
;
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");
385 //Needed. Don't touch
387 ss
.format
= PA_SAMPLE_S16LE
;
389 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
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
);
399 u
->rtpoll
= pa_rtpoll_new();
400 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
402 //Pulseaudio uses samples per sec but OpenSL ES uses samples per ms
403 if (pa_init_sles_player(u
, ss
.rate
* 1000) < 0)
405 //int buff[2] = {0, 0};
406 //(*u->bqPlayerBufferQueue)->Enqueue(u->bqPlayerBufferQueue, buff, 1);
408 pa_sink_new_data_init(&data
);
409 data
.driver
= __FILE__
;
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
);
414 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, _("OpenSL ES Output"));
415 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "abstract");
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
);
423 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
);
424 pa_sink_new_data_done(&data
);
427 pa_log("Failed to create sink object.");
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
;
435 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
436 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
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
);
443 if (!(u
->thread
= pa_thread_new("sles-sink", thread_func
, u
))) {
444 pa_log("Failed to create thread.");
448 pa_sink_set_latency_range(u
->sink
, 0, BLOCK_USEC
);
450 pa_sink_put(u
->sink
);
465 int pa__get_n_used(pa_module
*m
) {
469 pa_assert_se(u
= m
->userdata
);
471 return pa_sink_linked_by(u
->sink
);
474 void pa__done(pa_module
*m
) {
479 if (!(u
= m
->userdata
))
483 pa_sink_unlink(u
->sink
);
486 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
487 pa_thread_free(u
->thread
);
490 if (u
->engineObject
){
491 pa_destroy_sles_player(u
);
494 pa_thread_mq_done(&u
->thread_mq
);
497 pa_sink_unref(u
->sink
);
500 pa_rtpoll_free(u
->rtpoll
);