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 #ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
49 #include <SLES/OpenSLES_Android.h>
50 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
51 #define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
52 #define BufferQueueItf SLAndroidSimpleBufferQueueItf
53 #define BufferQueueState SLAndroidSimpleBufferQueueState
54 #define IID_BUFFERQUEUE_USED SL_IID_ANDROIDSIMPLEBUFFERQUEUE
57 #define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE
58 #define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
59 #define BufferQueueItf SLBufferQueueItf
60 #define BufferQueueState SLBufferQueueState
61 #define IID_BUFFERQUEUE_USED IID_BUFFERQUEUE
62 #define INDEX playIndex
65 #define checkResult(r) do { \
66 if ((r) != SL_RESULT_SUCCESS) { \
67 if ((r) == SL_RESULT_PARAMETER_INVALID) fprintf(stderr, "error SL_RESULT_PARAMETER_INVALID at %s:%d\n", __FILE__, __LINE__); \
68 else if ((r) == SL_RESULT_PRECONDITIONS_VIOLATED ) fprintf(stderr, "error SL_RESULT_PRECONDITIONS_VIOLATED at %s:%d\n", __FILE__, __LINE__); \
69 else fprintf(stderr, "error %d at %s:%d\n", (int) r, __FILE__, __LINE__); \
77 PA_MODULE_AUTHOR("Lennart Poettering, Nathan Martynov");
78 PA_MODULE_DESCRIPTION("Android OpenSL ES sink");
79 PA_MODULE_VERSION(PACKAGE_VERSION
);
80 PA_MODULE_LOAD_ONCE(false);
82 "sink_name=<name for the sink> "
83 "sink_properties=<properties for the sink> "
84 "rate=<sampling rate> ");
86 #define DEFAULT_SINK_NAME "OpenSL ES sink"
87 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
95 pa_thread_mq thread_mq
;
101 pa_memchunk memchunk
;
103 SLObjectItf engineObject
;
104 SLEngineItf engineEngine
;
106 // output mix interfaces
107 SLObjectItf outputMixObject
;
109 // buffer queue player interfaces
110 SLObjectItf bqPlayerObject
;
111 SLPlayItf bqPlayerPlay
;
112 BufferQueueItf bqPlayerBufferQueue
;
115 static const char* const valid_modargs
[] = {
122 static int sink_process_msg(
127 pa_memchunk
*chunk
) {
129 struct userdata
*u
= PA_SINK(o
)->userdata
;
132 case PA_SINK_MESSAGE_SET_STATE
:
134 if (pa_sink_get_state(u
->sink
) == PA_SINK_SUSPENDED
|| pa_sink_get_state(u
->sink
) == PA_SINK_INIT
) {
135 if (PA_PTR_TO_UINT(data
) == PA_SINK_RUNNING
|| PA_PTR_TO_UINT(data
) == PA_SINK_IDLE
)
136 u
->timestamp
= pa_rtclock_now();
141 case PA_SINK_MESSAGE_GET_LATENCY
: {
144 now
= pa_rtclock_now();
145 *((pa_usec_t
*) data
) = u
->timestamp
> now ? u
->timestamp
- now
: 0ULL;
151 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
154 static void sink_update_requested_latency_cb(pa_sink
*s
) {
158 pa_sink_assert_ref(s
);
159 pa_assert_se(u
= s
->userdata
);
161 u
->block_usec
= pa_sink_get_requested_latency_within_thread(s
);
163 if (u
->block_usec
== (pa_usec_t
) -1)
164 u
->block_usec
= s
->thread_info
.max_latency
;
166 nbytes
= pa_usec_to_bytes(u
->block_usec
, &s
->sample_spec
);
167 pa_sink_set_max_rewind_within_thread(s
, nbytes
);
168 pa_sink_set_max_request_within_thread(s
, nbytes
);
171 static int pa_init_sles_player(struct userdata
*s
, SLint32 sl_rate
)
173 if (s
== NULL
) return -1;
177 result
= slCreateEngine(&(s
->engineObject
), 0, NULL
, 0, NULL
, NULL
); checkResult(result
);
178 result
= (*s
->engineObject
)->Realize(s
->engineObject
, SL_BOOLEAN_FALSE
); checkResult(result
);
180 result
= (*s
->engineObject
)->GetInterface(s
->engineObject
, SL_IID_ENGINE
, &(s
->engineEngine
)); checkResult(result
);
183 result
= (*s
->engineEngine
)->CreateOutputMix(s
->engineEngine
, &(s
->outputMixObject
), 0, NULL
, NULL
); checkResult(result
);
184 result
= (*s
->outputMixObject
)->Realize(s
->outputMixObject
, SL_BOOLEAN_FALSE
); checkResult(result
);
186 // create audio player
188 SLDataLocator_OutputMix locator_outputmix
;
189 locator_outputmix
.locatorType
= SL_DATALOCATOR_OUTPUTMIX
;
190 locator_outputmix
.outputMix
= s
->outputMixObject
;
192 SLDataLocator_BufferQueue locator_bufferqueue
;
193 locator_bufferqueue
.locatorType
= DATALOCATOR_BUFFERQUEUE
;
194 locator_bufferqueue
.numBuffers
= 50;
197 pa_log("Incompatible sample rate");
201 SLDataFormat_PCM pcm
;
202 pcm
.formatType
= SL_DATAFORMAT_PCM
;
204 pcm
.samplesPerSec
= sl_rate
;
205 pcm
.bitsPerSample
= SL_PCMSAMPLEFORMAT_FIXED_16
;
206 pcm
.containerSize
= 16;
207 pcm
.channelMask
= SL_SPEAKER_FRONT_LEFT
| SL_SPEAKER_FRONT_RIGHT
;
208 pcm
.endianness
= SL_BYTEORDER_LITTLEENDIAN
;
210 SLDataSource audiosrc
;
211 audiosrc
.pLocator
= &locator_bufferqueue
;
212 audiosrc
.pFormat
= &pcm
;
215 audiosnk
.pLocator
= &locator_outputmix
;
216 audiosnk
.pFormat
= NULL
;
218 SLInterfaceID ids
[1] = {IID_BUFFERQUEUE
};
219 SLboolean flags
[1] = {SL_BOOLEAN_TRUE
};
220 result
= (*s
->engineEngine
)->CreateAudioPlayer(s
->engineEngine
, &s
->bqPlayerObject
, &audiosrc
, &audiosnk
, 1, ids
, flags
); checkResult(result
);
221 result
= (*s
->bqPlayerObject
)->Realize(s
->bqPlayerObject
, SL_BOOLEAN_FALSE
); checkResult(result
);
223 result
= (*s
->bqPlayerObject
)->GetInterface(s
->bqPlayerObject
, SL_IID_PLAY
, &s
->bqPlayerPlay
); checkResult(result
);
224 result
= (*s
->bqPlayerObject
)->GetInterface(s
->bqPlayerObject
, IID_BUFFERQUEUE_USED
, &s
->bqPlayerBufferQueue
); checkResult(result
);
226 result
= (*s
->bqPlayerPlay
)->SetPlayState(s
->bqPlayerPlay
, SL_PLAYSTATE_PLAYING
); checkResult(result
);
230 static void pa_destroy_sles_player(struct userdata
*s
){
231 if (s
== NULL
) return;
232 (*s
->bqPlayerPlay
)->SetPlayState(s
->bqPlayerPlay
, SL_PLAYSTATE_STOPPED
);
233 (*s
->bqPlayerObject
)->Destroy(s
->bqPlayerObject
);
234 (*s
->outputMixObject
)->Destroy(s
->outputMixObject
);
235 (*s
->engineObject
)->Destroy(s
->engineObject
);
238 static void process_render(struct userdata
*u
, pa_usec_t now
) {
243 /* This is the configured latency. Sink inputs connected to us
244 might not have a single frame more than the maxrequest value
245 queued. Hence: at maximum read this many bytes from the sink
248 /* Fill the buffer up the latency size */
249 while (u
->timestamp
< now
+ u
->block_usec
) {
252 pa_sink_render(u
->sink
, u
->sink
->thread_info
.max_request
, &u
->memchunk
);
253 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
254 (*u
->bqPlayerBufferQueue
)->Enqueue(u
->bqPlayerBufferQueue
, (uint8_t*) p
+ u
->memchunk
.index
, u
->memchunk
.length
);
255 pa_memblock_release(u
->memchunk
.memblock
);
257 u
->timestamp
+= pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
258 ate
+= u
->memchunk
.length
;
259 if (ate
>= u
->sink
->thread_info
.max_request
) break;
263 static void thread_func(void *userdata
) {
264 struct userdata
*u
= userdata
;
268 pa_log_debug("Thread starting up");
270 pa_thread_mq_install(&u
->thread_mq
);
272 u
->timestamp
= pa_rtclock_now();
278 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
279 now
= pa_rtclock_now();
281 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
282 pa_sink_process_rewind(u
->sink
, 0);
284 /* Render some data and drop it immediately */
285 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
)) {
286 if (u
->timestamp
<= now
)
287 process_render(u
, now
);
289 pa_rtpoll_set_timer_absolute(u
->rtpoll
, u
->timestamp
);
291 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
293 /* Hmm, nothing to do. Let's sleep */
294 if ((ret
= pa_rtpoll_run(u
->rtpoll
)) < 0)
302 /* If this was no regular exit from the loop we have to continue
303 * processing messages until we received PA_MESSAGE_SHUTDOWN */
304 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
305 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
308 pa_log_debug("Thread shutting down");
311 static SLint32
PA2SLrate(int32_t rate
){
312 if (!(rate
>= 8000 && rate
<= 192000)) return -1;
315 return SL_SAMPLINGRATE_8
;
317 return SL_SAMPLINGRATE_11_025
;
319 return SL_SAMPLINGRATE_12
;
321 return SL_SAMPLINGRATE_16
;
323 return SL_SAMPLINGRATE_22_05
;
325 return SL_SAMPLINGRATE_24
;
327 return SL_SAMPLINGRATE_32
;
329 return SL_SAMPLINGRATE_44_1
;
331 return SL_SAMPLINGRATE_48
;
333 return SL_SAMPLINGRATE_64
;
335 return SL_SAMPLINGRATE_88_2
;
337 return SL_SAMPLINGRATE_96
;
339 return SL_SAMPLINGRATE_192
;
345 int pa__init(pa_module
*m
) {
346 struct userdata
*u
= NULL
;
349 pa_modargs
*ma
= NULL
;
350 pa_sink_new_data data
;
355 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
356 pa_log("Failed to parse module arguments.");
360 // High rate causes glitches on some devices, this is needed to prevent it
363 //ss.format = PA_SAMPLE_S16LE;
365 //OK. That will allow users to define sampling rate under his responsibility
366 ss
= m
->core
->default_sample_spec
;
367 map
= m
->core
->default_channel_map
;
368 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
369 pa_log("Invalid sample format specification or channel map");
373 //Needed. Don't touch
375 ss
.format
= PA_SAMPLE_S16LE
;
376 int forceFormat
= atoi(getenv("PROPERTY_OUTPUT_SAMPLE_RATE"));
377 if (forceFormat
>= 8000 && forceFormat
<= 192000)
378 ss
.rate
= forceFormat
;
380 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
383 u
->rtpoll
= pa_rtpoll_new();
384 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
386 if (pa_init_sles_player(u
, PA2SLrate(ss
.rate
)) < 0)
388 int buff
[2] = {0, 0};
389 (*u
->bqPlayerBufferQueue
)->Enqueue(u
->bqPlayerBufferQueue
, buff
, 1);
391 pa_sink_new_data_init(&data
);
392 data
.driver
= __FILE__
;
394 pa_sink_new_data_set_name(&data
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
));
395 pa_sink_new_data_set_sample_spec(&data
, &ss
);
396 pa_sink_new_data_set_channel_map(&data
, &map
);
397 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, _("OpenSL ES Output"));
398 pa_proplist_sets(data
.proplist
, PA_PROP_DEVICE_CLASS
, "abstract");
400 if (pa_modargs_get_proplist(ma
, "sink_properties", data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
401 pa_log("Invalid properties");
402 pa_sink_new_data_done(&data
);
406 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
);
407 pa_sink_new_data_done(&data
);
410 pa_log("Failed to create sink object.");
414 u
->sink
->parent
.process_msg
= sink_process_msg
;
415 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
416 u
->sink
->userdata
= u
;
418 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
419 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
421 u
->block_usec
= BLOCK_USEC
;
422 nbytes
= pa_usec_to_bytes(u
->block_usec
, &u
->sink
->sample_spec
);
423 pa_sink_set_max_rewind(u
->sink
, nbytes
);
424 pa_sink_set_max_request(u
->sink
, nbytes
);
426 if (!(u
->thread
= pa_thread_new("sles-sink", thread_func
, u
))) {
427 pa_log("Failed to create thread.");
431 pa_sink_set_latency_range(u
->sink
, 0, BLOCK_USEC
);
433 pa_sink_put(u
->sink
);
448 int pa__get_n_used(pa_module
*m
) {
452 pa_assert_se(u
= m
->userdata
);
454 return pa_sink_linked_by(u
->sink
);
457 void pa__done(pa_module
*m
) {
462 if (!(u
= m
->userdata
))
466 pa_sink_unlink(u
->sink
);
469 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
470 pa_thread_free(u
->thread
);
473 pa_thread_mq_done(&u
->thread_mq
);
476 pa_sink_unref(u
->sink
);
479 pa_rtpoll_free(u
->rtpoll
);