]> git.sesse.net Git - vlc/blob - modules/audio_output/opensles_android.c
direct3d11: catch texture mapping errors
[vlc] / modules / audio_output / opensles_android.c
1 /*****************************************************************************
2  * opensles_android.c : audio output for android native code
3  *****************************************************************************
4  * Copyright © 2011-2012 VideoLAN
5  *
6  * Authors: Dominique Martinet <asmadeus@codewreck.org>
7  *          Hugo Beauzée-Luyssen <beauze.h@gmail.com>
8  *          Rafaël Carré <funman@videolanorg>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include <assert.h>
37 #include <dlfcn.h>
38 #include <math.h>
39
40 // For native audio
41 #include <SLES/OpenSLES.h>
42 #include <SLES/OpenSLES_Android.h>
43
44 int aout_get_native_sample_rate(void);
45
46 #define OPENSLES_BUFFERS 255 /* maximum number of buffers */
47 #define OPENSLES_BUFLEN  10   /* ms */
48 /*
49  * 10ms of precision when mesasuring latency should be enough,
50  * with 255 buffers we can buffer 2.55s of audio.
51  */
52
53 #define CHECK_OPENSL_ERROR(msg)                \
54     if (unlikely(result != SL_RESULT_SUCCESS)) \
55     {                                          \
56         msg_Err(aout, msg" (%lu)", result);    \
57         goto error;                            \
58     }
59
60 typedef SLresult (*slCreateEngine_t)(
61         SLObjectItf*, SLuint32, const SLEngineOption*, SLuint32,
62         const SLInterfaceID*, const SLboolean*);
63
64 #define Destroy(a) (*a)->Destroy(a);
65 #define SetPlayState(a, b) (*a)->SetPlayState(a, b)
66 #define RegisterCallback(a, b, c) (*a)->RegisterCallback(a, b, c)
67 #define GetInterface(a, b, c) (*a)->GetInterface(a, b, c)
68 #define Realize(a, b) (*a)->Realize(a, b)
69 #define CreateOutputMix(a, b, c, d, e) (*a)->CreateOutputMix(a, b, c, d, e)
70 #define CreateAudioPlayer(a, b, c, d, e, f, g) \
71     (*a)->CreateAudioPlayer(a, b, c, d, e, f, g)
72 #define Enqueue(a, b, c) (*a)->Enqueue(a, b, c)
73 #define Clear(a) (*a)->Clear(a)
74 #define GetState(a, b) (*a)->GetState(a, b)
75 #define SetPositionUpdatePeriod(a, b) (*a)->SetPositionUpdatePeriod(a, b)
76 #define SetVolumeLevel(a, b) (*a)->SetVolumeLevel(a, b)
77 #define SetMute(a, b) (*a)->SetMute(a, b)
78
79 /*****************************************************************************
80  *
81  *****************************************************************************/
82 struct aout_sys_t
83 {
84     /* OpenSL objects */
85     SLObjectItf                     engineObject;
86     SLObjectItf                     outputMixObject;
87     SLAndroidSimpleBufferQueueItf   playerBufferQueue;
88     SLObjectItf                     playerObject;
89     SLVolumeItf                     volumeItf;
90     SLEngineItf                     engineEngine;
91     SLPlayItf                       playerPlay;
92
93     /* OpenSL symbols */
94     void                           *p_so_handle;
95
96     slCreateEngine_t                slCreateEnginePtr;
97     SLInterfaceID                   SL_IID_ENGINE;
98     SLInterfaceID                   SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
99     SLInterfaceID                   SL_IID_VOLUME;
100     SLInterfaceID                   SL_IID_PLAY;
101
102     /* */
103
104     vlc_mutex_t                     lock;
105
106     /* audio buffered through opensles */
107     uint8_t                        *buf;
108     size_t                          samples_per_buf;
109     int                             next_buf;
110
111     int                             rate;
112
113     /* if we can measure latency already */
114     bool                            started;
115
116     /* audio not yet buffered through opensles */
117     block_t                        *p_buffer_chain;
118     block_t                       **pp_buffer_last;
119     size_t                          samples;
120 };
121
122 /*****************************************************************************
123  * Local prototypes.
124  *****************************************************************************/
125 static int  Open  (vlc_object_t *);
126 static void Close (vlc_object_t *);
127
128 /*****************************************************************************
129  * Module descriptor
130  *****************************************************************************/
131
132 vlc_module_begin ()
133     set_description(N_("OpenSLES audio output"))
134     set_shortname(N_("OpenSLES"))
135     set_category(CAT_AUDIO)
136     set_subcategory(SUBCAT_AUDIO_AOUT)
137
138     set_capability("audio output", 170)
139     add_shortcut("opensles", "android")
140     set_callbacks(Open, Close)
141 vlc_module_end ()
142
143 /*****************************************************************************
144  *
145  *****************************************************************************/
146
147 static inline int bytesPerSample(void)
148 {
149     return 2 /* S16 */ * 2 /* stereo */;
150 }
151
152 static int TimeGet(audio_output_t* aout, mtime_t* restrict drift)
153 {
154     aout_sys_t *sys = aout->sys;
155
156     SLAndroidSimpleBufferQueueState st;
157     SLresult res = GetState(sys->playerBufferQueue, &st);
158     if (unlikely(res != SL_RESULT_SUCCESS)) {
159         msg_Err(aout, "Could not query buffer queue state in TimeGet (%lu)", res);
160         return -1;
161     }
162
163     vlc_mutex_lock(&sys->lock);
164     bool started = sys->started;
165     vlc_mutex_unlock(&sys->lock);
166
167     if (!started)
168         return -1;
169
170     *drift = (CLOCK_FREQ * OPENSLES_BUFLEN * st.count / 1000)
171         + sys->samples * CLOCK_FREQ / sys->rate;
172
173     /* msg_Dbg(aout, "latency %"PRId64" ms, %d/%d buffers", *drift / 1000,
174         (int)st.count, OPENSLES_BUFFERS); */
175
176     return 0;
177 }
178
179 static void Flush(audio_output_t *aout, bool drain)
180 {
181     aout_sys_t *sys = aout->sys;
182
183     if (drain) {
184         mtime_t delay;
185         if (!TimeGet(aout, &delay))
186             msleep(delay);
187     } else {
188         vlc_mutex_lock(&sys->lock);
189         SetPlayState(sys->playerPlay, SL_PLAYSTATE_STOPPED);
190         Clear(sys->playerBufferQueue);
191         SetPlayState(sys->playerPlay, SL_PLAYSTATE_PLAYING);
192
193         /* release audio data not yet written to opensles */
194         block_ChainRelease(sys->p_buffer_chain);
195         sys->p_buffer_chain = NULL;
196         sys->pp_buffer_last = &sys->p_buffer_chain;
197
198         sys->samples = 0;
199         sys->started = false;
200
201         vlc_mutex_unlock(&sys->lock);
202     }
203 }
204
205 static int VolumeSet(audio_output_t *aout, float vol)
206 {
207     if (!aout->sys->volumeItf)
208         return -1;
209
210     /* Convert UI volume to linear factor (cube) */
211     vol = vol * vol * vol;
212
213     /* millibels from linear amplification */
214     int mb = lroundf(2000.f * log10f(vol));
215     if (mb < SL_MILLIBEL_MIN)
216         mb = SL_MILLIBEL_MIN;
217     else if (mb > 0)
218         mb = 0; /* maximum supported level could be higher: GetMaxVolumeLevel */
219
220     SLresult r = SetVolumeLevel(aout->sys->volumeItf, mb);
221     return (r == SL_RESULT_SUCCESS) ? 0 : -1;
222 }
223
224 static int MuteSet(audio_output_t *aout, bool mute)
225 {
226     if (!aout->sys->volumeItf)
227         return -1;
228
229     SLresult r = SetMute(aout->sys->volumeItf, mute);
230     return (r == SL_RESULT_SUCCESS) ? 0 : -1;
231 }
232
233 static void Pause(audio_output_t *aout, bool pause, mtime_t date)
234 {
235     (void)date;
236     aout_sys_t *sys = aout->sys;
237     SetPlayState(sys->playerPlay,
238         pause ? SL_PLAYSTATE_PAUSED : SL_PLAYSTATE_PLAYING);
239 }
240
241 static int WriteBuffer(audio_output_t *aout)
242 {
243     aout_sys_t *sys = aout->sys;
244     const size_t unit_size = sys->samples_per_buf * bytesPerSample();
245
246     block_t *b = sys->p_buffer_chain;
247     if (!b)
248         return false;
249
250     /* Check if we can fill at least one buffer unit by chaining blocks */
251     if (b->i_buffer < unit_size) {
252         if (!b->p_next)
253             return false;
254         ssize_t needed = unit_size - b->i_buffer;
255         for (block_t *next = b->p_next; next; next = next->p_next) {
256             needed -= next->i_buffer;
257             if (needed <= 0)
258                 break;
259         }
260
261         if (needed > 0)
262             return false;
263     }
264
265     SLAndroidSimpleBufferQueueState st;
266     SLresult res = GetState(sys->playerBufferQueue, &st);
267     if (unlikely(res != SL_RESULT_SUCCESS)) {
268         msg_Err(aout, "Could not query buffer queue state in %s (%lu)", __func__, res);
269         return false;
270     }
271
272     if (st.count == OPENSLES_BUFFERS)
273         return false;
274
275     size_t done = 0;
276     while (done < unit_size) {
277         size_t cur = b->i_buffer;
278         if (cur > unit_size - done)
279             cur = unit_size - done;
280
281         memcpy(&sys->buf[unit_size * sys->next_buf + done], b->p_buffer, cur);
282         b->i_buffer -= cur;
283         b->p_buffer += cur;
284         done += cur;
285
286         block_t *next = b->p_next;
287         if (b->i_buffer == 0) {
288             block_Release(b);
289             b = NULL;
290         }
291
292         if (done == unit_size)
293             break;
294         else
295             b = next;
296     }
297
298     sys->p_buffer_chain = b;
299     if (!b)
300         sys->pp_buffer_last = &sys->p_buffer_chain;
301
302     SLresult r = Enqueue(sys->playerBufferQueue,
303         &sys->buf[unit_size * sys->next_buf], unit_size);
304
305     sys->samples -= sys->samples_per_buf;
306
307     if (r == SL_RESULT_SUCCESS) {
308         if (++sys->next_buf == OPENSLES_BUFFERS)
309             sys->next_buf = 0;
310         return true;
311     } else {
312         /* XXX : if writing fails, we don't retry */
313         msg_Err(aout, "error %lu when writing %d bytes %s",
314                 r, b->i_buffer,
315                 (r == SL_RESULT_BUFFER_INSUFFICIENT) ? " (buffer insufficient)" : "");
316         return false;
317     }
318 }
319
320 /*****************************************************************************
321  * Play: play a sound
322  *****************************************************************************/
323 static void Play(audio_output_t *aout, block_t *p_buffer)
324 {
325     aout_sys_t *sys = aout->sys;
326
327     p_buffer->p_next = NULL; /* Make sur our linked list doesn't use old references */
328     vlc_mutex_lock(&sys->lock);
329
330     sys->samples += p_buffer->i_buffer / bytesPerSample();
331
332     /* Hold this block until we can write it into the OpenSL buffer */
333     block_ChainLastAppend(&sys->pp_buffer_last, p_buffer);
334
335     /* Fill OpenSL buffer */
336     while (WriteBuffer(aout))
337         ;
338
339     vlc_mutex_unlock(&sys->lock);
340 }
341
342 static void PlayedCallback (SLAndroidSimpleBufferQueueItf caller, void *pContext)
343 {
344     (void)caller;
345     audio_output_t *aout = pContext;
346     aout_sys_t *sys = aout->sys;
347
348     assert (caller == sys->playerBufferQueue);
349
350     vlc_mutex_lock(&sys->lock);
351     sys->started = true;
352     vlc_mutex_unlock(&sys->lock);
353 }
354 /*****************************************************************************
355  *
356  *****************************************************************************/
357 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
358 {
359     SLresult       result;
360
361     aout_sys_t *sys = aout->sys;
362
363     // configure audio source - this defines the number of samples you can enqueue.
364     SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {
365         SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
366         OPENSLES_BUFFERS
367     };
368
369     SLDataFormat_PCM format_pcm;
370     format_pcm.formatType       = SL_DATAFORMAT_PCM;
371     format_pcm.numChannels      = 2;
372     format_pcm.samplesPerSec    = ((SLuint32) fmt->i_rate * 1000) ;
373     format_pcm.bitsPerSample    = SL_PCMSAMPLEFORMAT_FIXED_16;
374     format_pcm.containerSize    = SL_PCMSAMPLEFORMAT_FIXED_16;
375     format_pcm.channelMask      = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
376     format_pcm.endianness       = SL_BYTEORDER_LITTLEENDIAN;
377
378     SLDataSource audioSrc = {&loc_bufq, &format_pcm};
379
380     // configure audio sink
381     SLDataLocator_OutputMix loc_outmix = {
382         SL_DATALOCATOR_OUTPUTMIX,
383         sys->outputMixObject
384     };
385     SLDataSink audioSnk = {&loc_outmix, NULL};
386
387     //create audio player
388     const SLInterfaceID ids2[] = { sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE, sys->SL_IID_VOLUME };
389     static const SLboolean req2[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
390
391     if (aout_get_native_sample_rate() >= fmt->i_rate) {
392         result = CreateAudioPlayer(sys->engineEngine, &sys->playerObject, &audioSrc,
393                                     &audioSnk, sizeof(ids2) / sizeof(*ids2),
394                                     ids2, req2);
395     } else {
396         // Don't try to play back a sample rate higher than the native one,
397         // since OpenSL ES will try to use the fast path, which AudioFlinger
398         // will reject (fast path can't do resampling), and will end up with
399         // too small buffers for the resampling. See http://b.android.com/59453
400         // for details. This bug is still present in 4.4. If it is fixed later
401         // this workaround could be made conditional.
402         result = SL_RESULT_UNKNOWN_ERROR;
403     }
404     if (unlikely(result != SL_RESULT_SUCCESS)) {
405         /* Try again with a more sensible samplerate */
406         fmt->i_rate = 44100;
407         format_pcm.samplesPerSec = ((SLuint32) 44100 * 1000) ;
408         result = CreateAudioPlayer(sys->engineEngine, &sys->playerObject, &audioSrc,
409                 &audioSnk, sizeof(ids2) / sizeof(*ids2),
410                 ids2, req2);
411     }
412     CHECK_OPENSL_ERROR("Failed to create audio player");
413
414     result = Realize(sys->playerObject, SL_BOOLEAN_FALSE);
415     CHECK_OPENSL_ERROR("Failed to realize player object.");
416
417     result = GetInterface(sys->playerObject, sys->SL_IID_PLAY, &sys->playerPlay);
418     CHECK_OPENSL_ERROR("Failed to get player interface.");
419
420     result = GetInterface(sys->playerObject, sys->SL_IID_VOLUME, &sys->volumeItf);
421     CHECK_OPENSL_ERROR("failed to get volume interface.");
422
423     result = GetInterface(sys->playerObject, sys->SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
424                                                   &sys->playerBufferQueue);
425     CHECK_OPENSL_ERROR("Failed to get buff queue interface");
426
427     result = RegisterCallback(sys->playerBufferQueue, PlayedCallback,
428                                    (void*)aout);
429     CHECK_OPENSL_ERROR("Failed to register buff queue callback.");
430
431     // set the player's state to playing
432     result = SetPlayState(sys->playerPlay, SL_PLAYSTATE_PLAYING);
433     CHECK_OPENSL_ERROR("Failed to switch to playing state");
434
435     /* XXX: rounding shouldn't affect us at normal sampling rate */
436     sys->rate = fmt->i_rate;
437     sys->samples_per_buf = OPENSLES_BUFLEN * fmt->i_rate / 1000;
438     sys->buf = malloc(OPENSLES_BUFFERS * sys->samples_per_buf * bytesPerSample());
439     if (!sys->buf)
440         goto error;
441
442     sys->started = false;
443     sys->next_buf = 0;
444
445     sys->p_buffer_chain = NULL;
446     sys->pp_buffer_last = &sys->p_buffer_chain;
447     sys->samples = 0;
448
449     // we want 16bit signed data native endian.
450     fmt->i_format              = VLC_CODEC_S16N;
451     fmt->i_physical_channels   = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
452
453     SetPositionUpdatePeriod(sys->playerPlay, AOUT_MIN_PREPARE_TIME * 1000 / CLOCK_FREQ);
454
455     aout_FormatPrepare(fmt);
456
457     return VLC_SUCCESS;
458
459 error:
460     if (sys->playerObject) {
461         Destroy(sys->playerObject);
462         sys->playerObject = NULL;
463     }
464
465     return VLC_EGENERIC;
466 }
467
468 static void Stop(audio_output_t *aout)
469 {
470     aout_sys_t *sys = aout->sys;
471
472     SetPlayState(sys->playerPlay, SL_PLAYSTATE_STOPPED);
473     //Flush remaining buffers if any.
474     Clear(sys->playerBufferQueue);
475
476     free(sys->buf);
477     block_ChainRelease(sys->p_buffer_chain);
478
479     Destroy(sys->playerObject);
480     sys->playerObject = NULL;
481 }
482
483 /*****************************************************************************
484  *
485  *****************************************************************************/
486 static void Close(vlc_object_t *obj)
487 {
488     audio_output_t *aout = (audio_output_t *)obj;
489     aout_sys_t *sys = aout->sys;
490
491     Destroy(sys->outputMixObject);
492     Destroy(sys->engineObject);
493     dlclose(sys->p_so_handle);
494     vlc_mutex_destroy(&sys->lock);
495     free(sys);
496 }
497
498 static int Open (vlc_object_t *obj)
499 {
500     audio_output_t *aout = (audio_output_t *)obj;
501     aout_sys_t *sys;
502     SLresult result;
503
504     aout->sys = sys = calloc(1, sizeof(*sys));
505     if (unlikely(sys == NULL))
506         return VLC_ENOMEM;
507
508     sys->p_so_handle = dlopen("libOpenSLES.so", RTLD_NOW);
509     if (sys->p_so_handle == NULL)
510     {
511         msg_Err(aout, "Failed to load libOpenSLES");
512         goto error;
513     }
514
515     sys->slCreateEnginePtr = dlsym(sys->p_so_handle, "slCreateEngine");
516     if (unlikely(sys->slCreateEnginePtr == NULL))
517     {
518         msg_Err(aout, "Failed to load symbol slCreateEngine");
519         goto error;
520     }
521
522 #define OPENSL_DLSYM(dest, name)                       \
523     do {                                                       \
524         const SLInterfaceID *sym = dlsym(sys->p_so_handle, "SL_IID_"name);        \
525         if (unlikely(sym == NULL))                             \
526         {                                                      \
527             msg_Err(aout, "Failed to load symbol SL_IID_"name); \
528             goto error;                                        \
529         }                                                      \
530         sys->dest = *sym;                                           \
531     } while(0)
532
533     OPENSL_DLSYM(SL_IID_ANDROIDSIMPLEBUFFERQUEUE, "ANDROIDSIMPLEBUFFERQUEUE");
534     OPENSL_DLSYM(SL_IID_ENGINE, "ENGINE");
535     OPENSL_DLSYM(SL_IID_PLAY, "PLAY");
536     OPENSL_DLSYM(SL_IID_VOLUME, "VOLUME");
537 #undef OPENSL_DLSYM
538
539     // create engine
540     result = sys->slCreateEnginePtr(&sys->engineObject, 0, NULL, 0, NULL, NULL);
541     CHECK_OPENSL_ERROR("Failed to create engine");
542
543     // realize the engine in synchronous mode
544     result = Realize(sys->engineObject, SL_BOOLEAN_FALSE);
545     CHECK_OPENSL_ERROR("Failed to realize engine");
546
547     // get the engine interface, needed to create other objects
548     result = GetInterface(sys->engineObject, sys->SL_IID_ENGINE, &sys->engineEngine);
549     CHECK_OPENSL_ERROR("Failed to get the engine interface");
550
551     // create output mix, with environmental reverb specified as a non-required interface
552     const SLInterfaceID ids1[] = { sys->SL_IID_VOLUME };
553     const SLboolean req1[] = { SL_BOOLEAN_FALSE };
554     result = CreateOutputMix(sys->engineEngine, &sys->outputMixObject, 1, ids1, req1);
555     CHECK_OPENSL_ERROR("Failed to create output mix");
556
557     // realize the output mix in synchronous mode
558     result = Realize(sys->outputMixObject, SL_BOOLEAN_FALSE);
559     CHECK_OPENSL_ERROR("Failed to realize output mix");
560
561     vlc_mutex_init(&sys->lock);
562
563     aout->start      = Start;
564     aout->stop       = Stop;
565     aout->time_get   = TimeGet;
566     aout->play       = Play;
567     aout->pause      = Pause;
568     aout->flush      = Flush;
569     aout->mute_set   = MuteSet;
570     aout->volume_set = VolumeSet;
571
572     return VLC_SUCCESS;
573
574 error:
575     if (sys->outputMixObject)
576         Destroy(sys->outputMixObject);
577     if (sys->engineObject)
578         Destroy(sys->engineObject);
579     if (sys->p_so_handle)
580         dlclose(sys->p_so_handle);
581     free(sys);
582     return VLC_EGENERIC;
583 }