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 #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
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
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__); \
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);
81 "sink_name=<name for the sink> "
82 "sink_properties=<properties for the sink> ");
84 #define DEFAULT_SINK_NAME "OpenSL ES sink"
85 #define BLOCK_USEC (PA_USEC_PER_SEC * 2)
93 pa_thread_mq thread_mq
;
101 SLObjectItf engineObject
;
102 SLEngineItf engineEngine
;
104 // output mix interfaces
105 SLObjectItf outputMixObject
;
107 // buffer queue player interfaces
108 SLObjectItf bqPlayerObject
;
109 SLPlayItf bqPlayerPlay
;
110 BufferQueueItf bqPlayerBufferQueue
;
113 static const char* const valid_modargs
[] = {
119 static int sink_process_msg(
124 pa_memchunk
*chunk
) {
126 struct userdata
*u
= PA_SINK(o
)->userdata
;
129 case PA_SINK_MESSAGE_SET_STATE
:
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();
138 case PA_SINK_MESSAGE_GET_LATENCY
: {
141 now
= pa_rtclock_now();
142 *((pa_usec_t
*) data
) = u
->timestamp
> now ? u
->timestamp
- now
: 0ULL;
148 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
151 static void sink_update_requested_latency_cb(pa_sink
*s
) {
155 pa_sink_assert_ref(s
);
156 pa_assert_se(u
= s
->userdata
);
158 u
->block_usec
= pa_sink_get_requested_latency_within_thread(s
);
160 if (u
->block_usec
== (pa_usec_t
) -1)
161 u
->block_usec
= s
->thread_info
.max_latency
;
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
);
168 void pa_init_sles_player(struct userdata
*s
)
170 if (s
== NULL
) return;
174 result
= slCreateEngine(&(s
->engineObject
), 0, NULL
, 0, NULL
, NULL
); checkResult(result
);
175 result
= (*s
->engineObject
)->Realize(s
->engineObject
, SL_BOOLEAN_FALSE
); checkResult(result
);
177 result
= (*s
->engineObject
)->GetInterface(s
->engineObject
, SL_IID_ENGINE
, &(s
->engineEngine
)); checkResult(result
);
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
);
183 // create audio player
185 SLDataLocator_OutputMix locator_outputmix
;
186 locator_outputmix
.locatorType
= SL_DATALOCATOR_OUTPUTMIX
;
187 locator_outputmix
.outputMix
= s
->outputMixObject
;
189 SLDataLocator_BufferQueue locator_bufferqueue
;
190 locator_bufferqueue
.locatorType
= DATALOCATOR_BUFFERQUEUE
;
191 locator_bufferqueue
.numBuffers
= 50;
193 SLDataFormat_PCM pcm
;
194 pcm
.formatType
= SL_DATAFORMAT_PCM
;
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
;
202 SLDataSource audiosrc
;
203 audiosrc
.pLocator
= &locator_bufferqueue
;
204 audiosrc
.pFormat
= &pcm
;
207 audiosnk
.pLocator
= &locator_outputmix
;
208 audiosnk
.pFormat
= NULL
;
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
);
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
);
218 result
= (*s
->bqPlayerPlay
)->SetPlayState(s
->bqPlayerPlay
, SL_PLAYSTATE_PLAYING
); checkResult(result
);
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
);
229 static void process_render(struct userdata
*u
, pa_usec_t now
) {
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
239 /* Fill the buffer up the latency size */
240 while (u
->timestamp
< now
+ u
->block_usec
) {
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
);
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
);
251 ate
+= u
->memchunk
.length
;
253 if (ate
>= u
->sink
->thread_info
.max_request
)
257 /* pa_log_debug("Ate in sum %lu bytes (of %lu)", (unsigned long) ate, (unsigned long) nbytes); */
260 static void thread_func(void *userdata
) {
261 struct userdata
*u
= userdata
;
265 pa_log_debug("Thread starting up");
267 pa_thread_mq_install(&u
->thread_mq
);
269 u
->timestamp
= pa_rtclock_now();
275 if (PA_SINK_IS_OPENED(u
->sink
->thread_info
.state
))
276 now
= pa_rtclock_now();
278 if (PA_UNLIKELY(u
->sink
->thread_info
.rewind_requested
))
279 pa_sink_process_rewind(u
->sink
, 0);
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
);
286 pa_rtpoll_set_timer_absolute(u
->rtpoll
, u
->timestamp
);
288 pa_rtpoll_set_timer_disabled(u
->rtpoll
);
290 /* Hmm, nothing to do. Let's sleep */
291 if ((ret
= pa_rtpoll_run(u
->rtpoll
)) < 0)
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
);
305 pa_log_debug("Thread shutting down");
308 int pa__init(pa_module
*m
) {
309 struct userdata
*u
= NULL
;
312 pa_modargs
*ma
= NULL
;
313 pa_sink_new_data data
;
318 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
319 pa_log("Failed to parse module arguments.");
323 // High rate causes glitches on some devices, this is needed to prevent it
326 ss
.format
= PA_SAMPLE_S16LE
;
327 map
= m
->core
->default_channel_map
;
330 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
333 u
->rtpoll
= pa_rtpoll_new();
334 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
, u
->rtpoll
);
336 pa_init_sles_player(u
);
337 int buff
[2] = {0, 0};
338 (*u
->bqPlayerBufferQueue
)->Enqueue(u
->bqPlayerBufferQueue
, buff
, 1);
340 pa_sink_new_data_init(&data
);
341 data
.driver
= __FILE__
;
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");
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
);
355 u
->sink
= pa_sink_new(m
->core
, &data
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
);
356 pa_sink_new_data_done(&data
);
359 pa_log("Failed to create sink object.");
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
;
367 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
368 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
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
);
375 if (!(u
->thread
= pa_thread_new("null-sink", thread_func
, u
))) {
376 pa_log("Failed to create thread.");
380 pa_sink_set_latency_range(u
->sink
, 0, BLOCK_USEC
);
382 pa_sink_put(u
->sink
);
397 int pa__get_n_used(pa_module
*m
) {
401 pa_assert_se(u
= m
->userdata
);
403 return pa_sink_linked_by(u
->sink
);
406 void pa__done(pa_module
*m
) {
411 if (!(u
= m
->userdata
))
415 pa_sink_unlink(u
->sink
);
418 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
419 pa_thread_free(u
->thread
);
422 pa_thread_mq_done(&u
->thread_mq
);
425 pa_sink_unref(u
->sink
);
428 pa_rtpoll_free(u
->rtpoll
);