audiotrack: use AudioTrack.getLatency()
[vlc.git] / modules / audio_output / audiotrack.c
bloba371104730e000ca7f4a47265fd856fd973fb5ab
1 /*****************************************************************************
2 * audiotrack.c: Android Java AudioTrack audio output module
3 *****************************************************************************
4 * Copyright © 2012-2015 VLC authors and VideoLAN, VideoLabs
6 * Authors: Thomas Guillem <thomas@gllm.fr>
7 * Ming Hu <tewilove@gmail.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <jni.h>
30 #include <dlfcn.h>
31 #include <stdbool.h>
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_aout.h>
36 #include "../video_output/android/utils.h"
38 #define SMOOTHPOS_SAMPLE_COUNT 10
39 #define SMOOTHPOS_INTERVAL_US VLC_TICK_FROM_MS(30) // 30ms
41 #define AUDIOTIMESTAMP_INTERVAL_US VLC_TICK_FROM_MS(500) // 500ms
43 static int Open( vlc_object_t * );
44 static void Close( vlc_object_t * );
45 static void Stop( audio_output_t * );
46 static int Start( audio_output_t *, audio_sample_format_t * );
47 static void *AudioTrack_Thread( void * );
49 /* There is an undefined behavior when configuring AudioTrack with SPDIF or
50 * more than 2 channels when there is no HDMI out. It may succeed and the
51 * Android ressampler will be used to downmix to stereo. It may fails cleanly,
52 * and this module will be able to recover and fallback to stereo. Finally, in
53 * some rare cases, it may crash during init or while ressampling. Because of
54 * the last case we don't try up to 8 channels and we use AT_DEV_STEREO device
55 * per default */
56 enum at_dev {
57 AT_DEV_STEREO = 0,
58 AT_DEV_PCM,
59 AT_DEV_ENCODED,
61 #define AT_DEV_DEFAULT AT_DEV_STEREO
62 #define AT_DEV_MAX_CHANNELS 8
64 static const struct {
65 const char *id;
66 const char *name;
67 enum at_dev at_dev;
68 } at_devs[] = {
69 { "stereo", "Up to 2 channels (compat mode).", AT_DEV_STEREO },
70 { "pcm", "Up to 8 channels.", AT_DEV_PCM },
72 /* With "encoded", the module will try to play every audio codecs via
73 * passthrough.
75 * With "encoded:ENCODING_FLAGS_MASK", the module will try to play only
76 * codecs specified by ENCODING_FLAGS_MASK. This extra value is a long long
77 * that contains binary-shifted AudioFormat.ENCODING_* values. */
78 { "encoded", "Up to 8 channels, passthrough if available.", AT_DEV_ENCODED },
79 { NULL, NULL, AT_DEV_DEFAULT },
82 typedef struct
84 enum at_dev at_dev;
86 jobject p_audiotrack; /* AudioTrack ref */
87 float volume;
88 bool mute;
90 audio_sample_format_t fmt; /* fmt setup by Start */
92 struct {
93 unsigned int i_rate;
94 int i_channel_config;
95 int i_format;
96 int i_size;
97 } audiotrack_args;
99 /* Used by AudioTrack_getPlaybackHeadPosition */
100 struct {
101 uint32_t i_wrap_count;
102 uint32_t i_last;
103 } headpos;
105 /* Used by AudioTrack_GetTimestampPositionUs */
106 struct {
107 jobject p_obj; /* AudioTimestamp ref */
108 vlc_tick_t i_frame_us;
109 jlong i_frame_pos;
110 vlc_tick_t i_play_time; /* time when play was called */
111 vlc_tick_t i_last_time;
112 } timestamp;
114 /* Used by AudioTrack_GetSmoothPositionUs */
115 struct {
116 uint32_t i_idx;
117 uint32_t i_count;
118 vlc_tick_t p_us[SMOOTHPOS_SAMPLE_COUNT];
119 vlc_tick_t i_us;
120 vlc_tick_t i_last_time;
121 } smoothpos;
123 uint32_t i_max_audiotrack_samples;
124 long long i_encoding_flags;
125 bool b_passthrough;
126 uint8_t i_chans_to_reorder; /* do we need channel reordering */
127 uint8_t p_chan_table[AOUT_CHAN_MAX];
129 enum {
130 WRITE_BYTEARRAY,
131 WRITE_BYTEARRAYV23,
132 WRITE_SHORTARRAYV23,
133 WRITE_BYTEBUFFER,
134 WRITE_FLOATARRAY
135 } i_write_type;
137 vlc_thread_t thread; /* AudioTrack_Thread */
138 vlc_mutex_t lock;
139 vlc_cond_t aout_cond; /* cond owned by aout */
140 vlc_cond_t thread_cond; /* cond owned by AudioTrack_Thread */
142 /* These variables need locking on read and write */
143 bool b_thread_running; /* Set to false by aout to stop the thread */
144 bool b_thread_paused; /* If true, the thread won't process any data, see
145 * Pause() */
146 bool b_thread_waiting; /* If true, the thread is waiting for enough spaces
147 * in AudioTrack internal buffers */
149 uint64_t i_samples_written; /* Number of samples written since last flush */
150 bool b_audiotrack_exception; /* True if audiotrack threw an exception */
151 bool b_error; /* generic error */
153 struct {
154 uint64_t i_read; /* Number of bytes read */
155 uint64_t i_write; /* Number of bytes written */
156 size_t i_size; /* Size of the circular buffer in bytes */
157 union {
158 jbyteArray p_bytearray;
159 jfloatArray p_floatarray;
160 jshortArray p_shortarray;
161 struct {
162 uint8_t *p_data;
163 jobject p_obj;
164 } bytebuffer;
165 } u;
166 } circular;
167 } aout_sys_t;
170 // Don't use Float for now since 5.1/7.1 Float is down sampled to Stereo Float
171 //#define AUDIOTRACK_USE_FLOAT
172 //#define AUDIOTRACK_HW_LATENCY
174 /* Get AudioTrack native sample rate: if activated, most of the resampling
175 * will be done by VLC */
176 #define AUDIOTRACK_NATIVE_SAMPLERATE
178 #define AUDIOTRACK_SESSION_ID_TEXT " Id of audio session the AudioTrack must be attached to"
180 vlc_module_begin ()
181 set_shortname( "AudioTrack" )
182 set_description( "Android AudioTrack audio output" )
183 set_capability( "audio output", 180 )
184 set_category( CAT_AUDIO )
185 set_subcategory( SUBCAT_AUDIO_AOUT )
186 add_integer( "audiotrack-session-id", 0,
187 AUDIOTRACK_SESSION_ID_TEXT, NULL, true )
188 change_private()
189 add_shortcut( "audiotrack" )
190 set_callbacks( Open, Close )
191 vlc_module_end ()
193 #define THREAD_NAME "android_audiotrack"
194 #define GET_ENV() android_getEnv( VLC_OBJECT(p_aout), THREAD_NAME )
196 static struct
198 struct {
199 jclass clazz;
200 jmethodID ctor;
201 bool has_ctor_21;
202 jmethodID release;
203 jmethodID getState;
204 jmethodID play;
205 jmethodID stop;
206 jmethodID flush;
207 jmethodID pause;
208 jmethodID write;
209 jmethodID writeV23;
210 jmethodID writeShortV23;
211 jmethodID writeBufferV21;
212 jmethodID writeFloat;
213 jmethodID getLatency;
214 jmethodID getPlaybackHeadPosition;
215 jmethodID getTimestamp;
216 jmethodID getMinBufferSize;
217 jmethodID getNativeOutputSampleRate;
218 jmethodID setVolume;
219 jmethodID setStereoVolume;
220 jint STATE_INITIALIZED;
221 jint MODE_STREAM;
222 jint ERROR;
223 jint ERROR_BAD_VALUE;
224 jint ERROR_INVALID_OPERATION;
225 jint WRITE_NON_BLOCKING;
226 } AudioTrack;
227 struct {
228 jclass clazz;
229 jmethodID ctor;
230 jmethodID build;
231 jmethodID setLegacyStreamType;
232 } AudioAttributes_Builder;
233 struct {
234 jint ENCODING_PCM_8BIT;
235 jint ENCODING_PCM_16BIT;
236 jint ENCODING_PCM_FLOAT;
237 bool has_ENCODING_PCM_FLOAT;
238 jint ENCODING_AC3;
239 bool has_ENCODING_AC3;
240 jint ENCODING_E_AC3;
241 bool has_ENCODING_E_AC3;
242 jint ENCODING_DOLBY_TRUEHD;
243 bool has_ENCODING_DOLBY_TRUEHD;
244 jint ENCODING_DTS;
245 bool has_ENCODING_DTS;
246 jint ENCODING_DTS_HD;
247 bool has_ENCODING_DTS_HD;
248 jint ENCODING_IEC61937;
249 bool has_ENCODING_IEC61937;
250 jint CHANNEL_OUT_MONO;
251 jint CHANNEL_OUT_STEREO;
252 jint CHANNEL_OUT_FRONT_LEFT;
253 jint CHANNEL_OUT_FRONT_RIGHT;
254 jint CHANNEL_OUT_BACK_LEFT;
255 jint CHANNEL_OUT_BACK_RIGHT;
256 jint CHANNEL_OUT_FRONT_CENTER;
257 jint CHANNEL_OUT_LOW_FREQUENCY;
258 jint CHANNEL_OUT_BACK_CENTER;
259 jint CHANNEL_OUT_5POINT1;
260 jint CHANNEL_OUT_SIDE_LEFT;
261 jint CHANNEL_OUT_SIDE_RIGHT;
262 bool has_CHANNEL_OUT_SIDE;
263 } AudioFormat;
264 struct {
265 jclass clazz;
266 jmethodID ctor;
267 jmethodID build;
268 jmethodID setChannelMask;
269 jmethodID setEncoding;
270 jmethodID setSampleRate;
271 } AudioFormat_Builder;
272 struct {
273 jint ERROR_DEAD_OBJECT;
274 bool has_ERROR_DEAD_OBJECT;
275 jint STREAM_MUSIC;
276 } AudioManager;
277 struct {
278 jclass clazz;
279 jmethodID ctor;
280 jfieldID framePosition;
281 jfieldID nanoTime;
282 } AudioTimestamp;
283 } jfields;
285 /* init all jni fields.
286 * Done only one time during the first initialisation */
287 static bool
288 InitJNIFields( audio_output_t *p_aout, JNIEnv* env )
290 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
291 static int i_init_state = -1;
292 bool ret;
293 jclass clazz;
294 jfieldID field;
296 vlc_mutex_lock( &lock );
298 if( i_init_state != -1 )
299 goto end;
301 #define CHECK_EXCEPTION( what, critical ) do { \
302 if( (*env)->ExceptionCheck( env ) ) \
304 msg_Err( p_aout, "%s failed", what ); \
305 (*env)->ExceptionClear( env ); \
306 if( (critical) ) \
308 i_init_state = 0; \
309 goto end; \
312 } while( 0 )
313 #define GET_CLASS( str, critical ) do { \
314 clazz = (*env)->FindClass( env, (str) ); \
315 CHECK_EXCEPTION( "FindClass(" str ")", critical ); \
316 } while( 0 )
317 #define GET_ID( get, id, str, args, critical ) do { \
318 jfields.id = (*env)->get( env, clazz, (str), (args) ); \
319 CHECK_EXCEPTION( #get "(" #id ")", critical ); \
320 } while( 0 )
321 #define GET_CONST_INT( id, str, critical ) do { \
322 field = NULL; \
323 field = (*env)->GetStaticFieldID( env, clazz, (str), "I" ); \
324 CHECK_EXCEPTION( "GetStaticFieldID(" #id ")", critical ); \
325 if( field ) \
327 jfields.id = (*env)->GetStaticIntField( env, clazz, field ); \
328 CHECK_EXCEPTION( #id, critical ); \
330 } while( 0 )
332 /* AudioTrack class init */
333 GET_CLASS( "android/media/AudioTrack", true );
334 jfields.AudioTrack.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
335 CHECK_EXCEPTION( "NewGlobalRef", true );
337 GET_ID( GetMethodID, AudioTrack.ctor, "<init>",
338 "(Landroid/media/AudioAttributes;Landroid/media/AudioFormat;III)V", false );
339 jfields.AudioTrack.has_ctor_21 = jfields.AudioTrack.ctor != NULL;
340 if( !jfields.AudioTrack.has_ctor_21 )
341 GET_ID( GetMethodID, AudioTrack.ctor, "<init>", "(IIIIIII)V", true );
342 GET_ID( GetMethodID, AudioTrack.release, "release", "()V", true );
343 GET_ID( GetMethodID, AudioTrack.getState, "getState", "()I", true );
344 GET_ID( GetMethodID, AudioTrack.play, "play", "()V", true );
345 GET_ID( GetMethodID, AudioTrack.stop, "stop", "()V", true );
346 GET_ID( GetMethodID, AudioTrack.flush, "flush", "()V", true );
347 GET_ID( GetMethodID, AudioTrack.pause, "pause", "()V", true );
349 GET_ID( GetMethodID, AudioTrack.writeV23, "write", "([BIII)I", false );
350 GET_ID( GetMethodID, AudioTrack.writeShortV23, "write", "([SIII)I", false );
351 if( !jfields.AudioTrack.writeV23 )
352 GET_ID( GetMethodID, AudioTrack.writeBufferV21, "write", "(Ljava/nio/ByteBuffer;II)I", false );
354 if( jfields.AudioTrack.writeV23 || jfields.AudioTrack.writeBufferV21 )
356 GET_CONST_INT( AudioTrack.WRITE_NON_BLOCKING, "WRITE_NON_BLOCKING", true );
357 #ifdef AUDIOTRACK_USE_FLOAT
358 GET_ID( GetMethodID, AudioTrack.writeFloat, "write", "([FIII)I", true );
359 #endif
360 } else
361 GET_ID( GetMethodID, AudioTrack.write, "write", "([BII)I", true );
363 #ifdef AUDIOTRACK_HW_LATENCY
364 GET_ID( GetMethodID, AudioTrack.getLatency, "getLatency", "()I", false );
365 #endif
367 GET_ID( GetMethodID, AudioTrack.getTimestamp,
368 "getTimestamp", "(Landroid/media/AudioTimestamp;)Z", false );
369 GET_ID( GetMethodID, AudioTrack.getPlaybackHeadPosition,
370 "getPlaybackHeadPosition", "()I", true );
372 GET_ID( GetStaticMethodID, AudioTrack.getMinBufferSize, "getMinBufferSize",
373 "(III)I", true );
374 #ifdef AUDIOTRACK_NATIVE_SAMPLERATE
375 GET_ID( GetStaticMethodID, AudioTrack.getNativeOutputSampleRate,
376 "getNativeOutputSampleRate", "(I)I", true );
377 #endif
378 GET_ID( GetMethodID, AudioTrack.setVolume,
379 "setVolume", "(F)I", false );
380 if( !jfields.AudioTrack.setVolume )
381 GET_ID( GetMethodID, AudioTrack.setStereoVolume,
382 "setStereoVolume", "(FF)I", true );
383 GET_CONST_INT( AudioTrack.STATE_INITIALIZED, "STATE_INITIALIZED", true );
384 GET_CONST_INT( AudioTrack.MODE_STREAM, "MODE_STREAM", true );
385 GET_CONST_INT( AudioTrack.ERROR, "ERROR", true );
386 GET_CONST_INT( AudioTrack.ERROR_BAD_VALUE , "ERROR_BAD_VALUE", true );
387 GET_CONST_INT( AudioTrack.ERROR_INVALID_OPERATION,
388 "ERROR_INVALID_OPERATION", true );
390 if( jfields.AudioTrack.has_ctor_21 )
392 /* AudioAttributes_Builder class init */
393 GET_CLASS( "android/media/AudioAttributes$Builder", true );
394 jfields.AudioAttributes_Builder.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
395 CHECK_EXCEPTION( "NewGlobalRef", true );
396 GET_ID( GetMethodID, AudioAttributes_Builder.ctor, "<init>",
397 "()V", true );
398 GET_ID( GetMethodID, AudioAttributes_Builder.build, "build",
399 "()Landroid/media/AudioAttributes;", true );
400 GET_ID( GetMethodID, AudioAttributes_Builder.setLegacyStreamType, "setLegacyStreamType",
401 "(I)Landroid/media/AudioAttributes$Builder;", true );
403 /* AudioFormat_Builder class init */
404 GET_CLASS( "android/media/AudioFormat$Builder", true );
405 jfields.AudioFormat_Builder.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
406 CHECK_EXCEPTION( "NewGlobalRef", true );
407 GET_ID( GetMethodID, AudioFormat_Builder.ctor, "<init>",
408 "()V", true );
409 GET_ID( GetMethodID, AudioFormat_Builder.build, "build",
410 "()Landroid/media/AudioFormat;", true );
411 GET_ID( GetMethodID, AudioFormat_Builder.setChannelMask, "setChannelMask",
412 "(I)Landroid/media/AudioFormat$Builder;", true );
413 GET_ID( GetMethodID, AudioFormat_Builder.setEncoding, "setEncoding",
414 "(I)Landroid/media/AudioFormat$Builder;", true );
415 GET_ID( GetMethodID, AudioFormat_Builder.setSampleRate, "setSampleRate",
416 "(I)Landroid/media/AudioFormat$Builder;", true );
419 /* AudioTimestamp class init (if any) */
420 if( jfields.AudioTrack.getTimestamp )
422 GET_CLASS( "android/media/AudioTimestamp", true );
423 jfields.AudioTimestamp.clazz = (jclass) (*env)->NewGlobalRef( env,
424 clazz );
425 CHECK_EXCEPTION( "NewGlobalRef", true );
427 GET_ID( GetMethodID, AudioTimestamp.ctor, "<init>", "()V", true );
428 GET_ID( GetFieldID, AudioTimestamp.framePosition,
429 "framePosition", "J", true );
430 GET_ID( GetFieldID, AudioTimestamp.nanoTime,
431 "nanoTime", "J", true );
434 /* AudioFormat class init */
435 GET_CLASS( "android/media/AudioFormat", true );
436 GET_CONST_INT( AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT", true );
437 GET_CONST_INT( AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT", true );
438 #ifdef AUDIOTRACK_USE_FLOAT
439 GET_CONST_INT( AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT",
440 false );
441 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = field != NULL &&
442 jfields.AudioTrack.writeFloat;
443 #else
444 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = false;
445 #endif
447 if( jfields.AudioTrack.writeShortV23 )
449 GET_CONST_INT( AudioFormat.ENCODING_IEC61937, "ENCODING_IEC61937", false );
450 jfields.AudioFormat.has_ENCODING_IEC61937 = field != NULL;
452 else
453 jfields.AudioFormat.has_ENCODING_IEC61937 = false;
455 GET_CONST_INT( AudioFormat.ENCODING_AC3, "ENCODING_AC3", false );
456 jfields.AudioFormat.has_ENCODING_AC3 = field != NULL;
457 GET_CONST_INT( AudioFormat.ENCODING_E_AC3, "ENCODING_E_AC3", false );
458 jfields.AudioFormat.has_ENCODING_E_AC3 = field != NULL;
460 GET_CONST_INT( AudioFormat.ENCODING_DTS, "ENCODING_DTS", false );
461 jfields.AudioFormat.has_ENCODING_DTS = field != NULL;
462 GET_CONST_INT( AudioFormat.ENCODING_DTS_HD, "ENCODING_DTS_HD", false );
463 jfields.AudioFormat.has_ENCODING_DTS_HD = field != NULL;
465 GET_CONST_INT( AudioFormat.ENCODING_DOLBY_TRUEHD, "ENCODING_DOLBY_TRUEHD",
466 false );
467 jfields.AudioFormat.has_ENCODING_DOLBY_TRUEHD = field != NULL;
469 GET_CONST_INT( AudioFormat.CHANNEL_OUT_MONO, "CHANNEL_OUT_MONO", true );
470 GET_CONST_INT( AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO", true );
471 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_LEFT, "CHANNEL_OUT_FRONT_LEFT", true );
472 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_RIGHT, "CHANNEL_OUT_FRONT_RIGHT", true );
473 GET_CONST_INT( AudioFormat.CHANNEL_OUT_5POINT1, "CHANNEL_OUT_5POINT1", true );
474 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_LEFT, "CHANNEL_OUT_BACK_LEFT", true );
475 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_RIGHT, "CHANNEL_OUT_BACK_RIGHT", true );
476 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_CENTER, "CHANNEL_OUT_FRONT_CENTER", true );
477 GET_CONST_INT( AudioFormat.CHANNEL_OUT_LOW_FREQUENCY, "CHANNEL_OUT_LOW_FREQUENCY", true );
478 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_CENTER, "CHANNEL_OUT_BACK_CENTER", true );
479 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_LEFT, "CHANNEL_OUT_SIDE_LEFT", false );
480 if( field != NULL )
482 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_RIGHT, "CHANNEL_OUT_SIDE_RIGHT", true );
483 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = true;
484 } else
485 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = false;
487 /* AudioManager class init */
488 GET_CLASS( "android/media/AudioManager", true );
489 GET_CONST_INT( AudioManager.ERROR_DEAD_OBJECT, "ERROR_DEAD_OBJECT", false );
490 jfields.AudioManager.has_ERROR_DEAD_OBJECT = field != NULL;
491 GET_CONST_INT( AudioManager.STREAM_MUSIC, "STREAM_MUSIC", true );
493 #undef CHECK_EXCEPTION
494 #undef GET_CLASS
495 #undef GET_ID
496 #undef GET_CONST_INT
498 i_init_state = 1;
499 end:
500 ret = i_init_state == 1;
501 if( !ret )
502 msg_Err( p_aout, "AudioTrack jni init failed" );
503 vlc_mutex_unlock( &lock );
504 return ret;
507 static inline bool
508 check_exception( JNIEnv *env, audio_output_t *p_aout,
509 const char *class, const char *method )
511 if( (*env)->ExceptionCheck( env ) )
513 aout_sys_t *p_sys = p_aout->sys;
515 p_sys->b_audiotrack_exception = true;
516 p_sys->b_error = true;
517 (*env)->ExceptionDescribe( env );
518 (*env)->ExceptionClear( env );
519 msg_Err( p_aout, "%s.%s triggered an exception !", class, method );
520 return true;
521 } else
522 return false;
525 #define CHECK_EXCEPTION( class, method ) check_exception( env, p_aout, class, method )
526 #define CHECK_AT_EXCEPTION( method ) check_exception( env, p_aout, "AudioTrack", method )
528 #define JNI_CALL( what, obj, method, ... ) (*env)->what( env, obj, method, ##__VA_ARGS__ )
530 #define JNI_CALL_INT( obj, method, ... ) JNI_CALL( CallIntMethod, obj, method, ##__VA_ARGS__ )
531 #define JNI_CALL_BOOL( obj, method, ... ) JNI_CALL( CallBooleanMethod, obj, method, ##__VA_ARGS__ )
532 #define JNI_CALL_OBJECT( obj, method, ... ) JNI_CALL( CallObjectMethod, obj, method, ##__VA_ARGS__ )
533 #define JNI_CALL_VOID( obj, method, ... ) JNI_CALL( CallVoidMethod, obj, method, ##__VA_ARGS__ )
534 #define JNI_CALL_STATIC_INT( clazz, method, ... ) JNI_CALL( CallStaticIntMethod, clazz, method, ##__VA_ARGS__ )
536 #define JNI_AT_NEW( ... ) JNI_CALL( NewObject, jfields.AudioTrack.clazz, jfields.AudioTrack.ctor, ##__VA_ARGS__ )
537 #define JNI_AT_CALL_INT( method, ... ) JNI_CALL_INT( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
538 #define JNI_AT_CALL_BOOL( method, ... ) JNI_CALL_BOOL( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
539 #define JNI_AT_CALL_VOID( method, ... ) JNI_CALL_VOID( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
540 #define JNI_AT_CALL_STATIC_INT( method, ... ) JNI_CALL( CallStaticIntMethod, jfields.AudioTrack.clazz, jfields.AudioTrack.method, ##__VA_ARGS__ )
542 #define JNI_AUDIOTIMESTAMP_GET_LONG( field ) JNI_CALL( GetLongField, p_sys->timestamp.p_obj, jfields.AudioTimestamp.field )
544 static inline vlc_tick_t
545 frames_to_us( aout_sys_t *p_sys, uint64_t i_nb_frames )
547 return vlc_tick_from_samples(i_nb_frames, p_sys->fmt.i_rate);
549 #define FRAMES_TO_US(x) frames_to_us( p_sys, (x) )
551 static inline uint64_t
552 bytes_to_frames( aout_sys_t *p_sys, size_t i_bytes )
554 return i_bytes * p_sys->fmt.i_frame_length / p_sys->fmt.i_bytes_per_frame;
556 #define BYTES_TO_FRAMES(x) bytes_to_frames( p_sys, (x) )
557 #define BYTES_TO_US(x) frames_to_us( p_sys, bytes_to_frames( p_sys, (x) ) )
559 static inline size_t
560 frames_to_bytes( aout_sys_t *p_sys, uint64_t i_frames )
562 return i_frames * p_sys->fmt.i_bytes_per_frame / p_sys->fmt.i_frame_length;
564 #define FRAMES_TO_BYTES(x) frames_to_bytes( p_sys, (x) )
567 * Get the AudioTrack position
569 * The doc says that the position is reset to zero after flush but it's not
570 * true for all devices or Android versions.
572 static uint64_t
573 AudioTrack_getPlaybackHeadPosition( JNIEnv *env, audio_output_t *p_aout )
575 /* Android doc:
576 * getPlaybackHeadPosition: Returns the playback head position expressed in
577 * frames. Though the "int" type is signed 32-bits, the value should be
578 * reinterpreted as if it is unsigned 32-bits. That is, the next position
579 * after 0x7FFFFFFF is (int) 0x80000000. This is a continuously advancing
580 * counter. It will wrap (overflow) periodically, for example approximately
581 * once every 27:03:11 hours:minutes:seconds at 44.1 kHz. It is reset to
582 * zero by flush(), reload(), and stop().
585 aout_sys_t *p_sys = p_aout->sys;
586 uint32_t i_pos;
588 /* int32_t to uint32_t */
589 i_pos = 0xFFFFFFFFL & JNI_AT_CALL_INT( getPlaybackHeadPosition );
591 /* uint32_t to uint64_t */
592 if( p_sys->headpos.i_last > i_pos )
593 p_sys->headpos.i_wrap_count++;
594 p_sys->headpos.i_last = i_pos;
595 return p_sys->headpos.i_last + ((uint64_t)p_sys->headpos.i_wrap_count << 32);
599 * Reset AudioTrack position
601 * Called after flush, or start
603 static void
604 AudioTrack_ResetPlaybackHeadPosition( JNIEnv *env, audio_output_t *p_aout )
606 (void) env;
607 aout_sys_t *p_sys = p_aout->sys;
609 p_sys->headpos.i_last = 0;
610 p_sys->headpos.i_wrap_count = 0;
614 * Reset AudioTrack SmoothPosition and TimestampPosition
616 static void
617 AudioTrack_ResetPositions( JNIEnv *env, audio_output_t *p_aout )
619 aout_sys_t *p_sys = p_aout->sys;
620 VLC_UNUSED( env );
622 p_sys->timestamp.i_play_time = vlc_tick_now();
623 p_sys->timestamp.i_last_time = 0;
624 p_sys->timestamp.i_frame_us = 0;
625 p_sys->timestamp.i_frame_pos = 0;
627 p_sys->smoothpos.i_count = 0;
628 p_sys->smoothpos.i_idx = 0;
629 p_sys->smoothpos.i_last_time = 0;
630 p_sys->smoothpos.i_us = 0;
634 * Reset all AudioTrack positions and internal state
636 static void
637 AudioTrack_Reset( JNIEnv *env, audio_output_t *p_aout )
639 aout_sys_t *p_sys = p_aout->sys;
641 AudioTrack_ResetPositions( env, p_aout );
642 AudioTrack_ResetPlaybackHeadPosition( env, p_aout );
643 p_sys->i_samples_written = 0;
646 static vlc_tick_t
647 AudioTrack_GetLatencyUs( JNIEnv *env, audio_output_t *p_aout )
649 aout_sys_t *p_sys = p_aout->sys;
651 if( jfields.AudioTrack.getLatency )
653 int i_latency_ms = JNI_AT_CALL_INT( getLatency );
655 /* getLatency() includes the latency due to AudioTrack buffer size,
656 * AudioMixer (if any) and audio hardware driver. We only need the
657 * audio hardware latency */
658 if( i_latency_ms > 0 )
660 vlc_tick_t i_latency_us = VLC_TICK_FROM_MS( i_latency_ms )
661 - FRAMES_TO_US( p_sys->i_max_audiotrack_samples );
662 return i_latency_us >= 0 ? i_latency_us : 0;
666 return 0;
670 * Get a smooth AudioTrack position
672 * This function smooth out the AudioTrack position since it has a very bad
673 * precision (+/- 20ms on old devices).
675 static vlc_tick_t
676 AudioTrack_GetSmoothPositionUs( JNIEnv *env, audio_output_t *p_aout )
678 aout_sys_t *p_sys = p_aout->sys;
679 uint64_t i_audiotrack_us;
680 vlc_tick_t i_now = vlc_tick_now();
682 /* Fetch an AudioTrack position every SMOOTHPOS_INTERVAL_US (30ms) */
683 if( i_now - p_sys->smoothpos.i_last_time >= SMOOTHPOS_INTERVAL_US )
685 i_audiotrack_us = FRAMES_TO_US( AudioTrack_getPlaybackHeadPosition( env, p_aout ) );
687 p_sys->smoothpos.i_last_time = i_now;
689 /* Base the position off the current time */
690 p_sys->smoothpos.p_us[p_sys->smoothpos.i_idx] = i_audiotrack_us - i_now;
691 p_sys->smoothpos.i_idx = (p_sys->smoothpos.i_idx + 1)
692 % SMOOTHPOS_SAMPLE_COUNT;
693 if( p_sys->smoothpos.i_count < SMOOTHPOS_SAMPLE_COUNT )
694 p_sys->smoothpos.i_count++;
696 /* Calculate the average position based off the current time */
697 p_sys->smoothpos.i_us = 0;
698 for( uint32_t i = 0; i < p_sys->smoothpos.i_count; ++i )
699 p_sys->smoothpos.i_us += p_sys->smoothpos.p_us[i];
700 p_sys->smoothpos.i_us /= p_sys->smoothpos.i_count;
703 if( p_sys->smoothpos.i_us != 0 )
704 return p_sys->smoothpos.i_us + i_now - AudioTrack_GetLatencyUs( env, p_aout );
705 else
706 return 0;
709 static vlc_tick_t
710 AudioTrack_GetTimestampPositionUs( JNIEnv *env, audio_output_t *p_aout )
712 aout_sys_t *p_sys = p_aout->sys;
713 vlc_tick_t i_now;
715 if( !p_sys->timestamp.p_obj )
716 return 0;
718 i_now = vlc_tick_now();
720 /* Android doc:
721 * getTimestamp: Poll for a timestamp on demand.
723 * If you need to track timestamps during initial warmup or after a
724 * routing or mode change, you should request a new timestamp once per
725 * second until the reported timestamps show that the audio clock is
726 * stable. Thereafter, query for a new timestamp approximately once
727 * every 10 seconds to once per minute. Calling this method more often
728 * is inefficient. It is also counter-productive to call this method
729 * more often than recommended, because the short-term differences
730 * between successive timestamp reports are not meaningful. If you need
731 * a high-resolution mapping between frame position and presentation
732 * time, consider implementing that at application level, based on
733 * low-resolution timestamps.
736 /* Fetch an AudioTrack timestamp every AUDIOTIMESTAMP_INTERVAL_US (500ms) */
737 if( i_now - p_sys->timestamp.i_last_time >= AUDIOTIMESTAMP_INTERVAL_US )
739 p_sys->timestamp.i_last_time = i_now;
741 if( JNI_AT_CALL_BOOL( getTimestamp, p_sys->timestamp.p_obj ) )
743 p_sys->timestamp.i_frame_us = VLC_TICK_FROM_NS(JNI_AUDIOTIMESTAMP_GET_LONG( nanoTime ));
744 p_sys->timestamp.i_frame_pos = JNI_AUDIOTIMESTAMP_GET_LONG( framePosition );
746 else
748 p_sys->timestamp.i_frame_us = 0;
749 p_sys->timestamp.i_frame_pos = 0;
753 /* frame time should be after last play time
754 * frame time shouldn't be in the future
755 * frame time should be less than 10 seconds old */
756 if( p_sys->timestamp.i_frame_us != 0 && p_sys->timestamp.i_frame_pos != 0
757 && p_sys->timestamp.i_frame_us > p_sys->timestamp.i_play_time
758 && i_now > p_sys->timestamp.i_frame_us
759 && ( i_now - p_sys->timestamp.i_frame_us ) <= VLC_TICK_FROM_SEC(10) )
761 vlc_tick_t i_time_diff = i_now - p_sys->timestamp.i_frame_us;
762 jlong i_frames_diff = samples_from_vlc_tick(i_time_diff, p_sys->fmt.i_rate);
763 return FRAMES_TO_US( p_sys->timestamp.i_frame_pos + i_frames_diff );
764 } else
765 return 0;
768 static int
769 TimeGet( audio_output_t *p_aout, vlc_tick_t *restrict p_delay )
771 aout_sys_t *p_sys = p_aout->sys;
772 vlc_tick_t i_audiotrack_us;
773 JNIEnv *env;
775 if( p_sys->b_passthrough )
776 return -1;
778 vlc_mutex_lock( &p_sys->lock );
780 if( p_sys->b_error || !p_sys->i_samples_written || !( env = GET_ENV() ) )
781 goto bailout;
783 i_audiotrack_us = AudioTrack_GetTimestampPositionUs( env, p_aout );
785 if( i_audiotrack_us <= 0 )
786 i_audiotrack_us = AudioTrack_GetSmoothPositionUs(env, p_aout );
788 /* Debug log for both delays */
789 #if 0
791 vlc_tick_t i_ts_us = AudioTrack_GetTimestampPositionUs( env, p_aout );
792 vlc_tick_t i_smooth_us = AudioTrack_GetSmoothPositionUs(env, p_aout );
793 vlc_tick_t i_latency_us = AudioTrack_GetLatencyUs( env, p_aout );
795 msg_Err( p_aout, "TimeGet: TimeStamp: %"PRId64", Smooth: %"PRId64" (latency: %"PRId64")",
796 i_ts_us, i_smooth_us, i_latency_us );
798 #endif
800 if( i_audiotrack_us > 0 )
802 /* AudioTrack delay */
803 vlc_tick_t i_delay = FRAMES_TO_US( p_sys->i_samples_written )
804 - i_audiotrack_us;
805 if( i_delay >= 0 )
807 /* Circular buffer delay */
808 i_delay += BYTES_TO_US( p_sys->circular.i_write
809 - p_sys->circular.i_read );
810 *p_delay = i_delay;
811 vlc_mutex_unlock( &p_sys->lock );
812 return 0;
814 else
816 msg_Warn( p_aout, "timing screwed, reset positions" );
817 AudioTrack_ResetPositions( env, p_aout );
821 bailout:
822 vlc_mutex_unlock( &p_sys->lock );
823 return -1;
826 static void
827 AudioTrack_GetChanOrder( uint16_t i_physical_channels, uint32_t p_chans_out[] )
829 #define HAS_CHAN( x ) ( ( i_physical_channels & (x) ) == (x) )
830 /* samples will be in the following order: FL FR FC LFE BL BR BC SL SR */
831 int i = 0;
833 if( HAS_CHAN( AOUT_CHAN_LEFT ) )
834 p_chans_out[i++] = AOUT_CHAN_LEFT;
835 if( HAS_CHAN( AOUT_CHAN_RIGHT ) )
836 p_chans_out[i++] = AOUT_CHAN_RIGHT;
838 if( HAS_CHAN( AOUT_CHAN_CENTER ) )
839 p_chans_out[i++] = AOUT_CHAN_CENTER;
841 if( HAS_CHAN( AOUT_CHAN_LFE ) )
842 p_chans_out[i++] = AOUT_CHAN_LFE;
844 if( HAS_CHAN( AOUT_CHAN_REARLEFT ) )
845 p_chans_out[i++] = AOUT_CHAN_REARLEFT;
846 if( HAS_CHAN( AOUT_CHAN_REARRIGHT ) )
847 p_chans_out[i++] = AOUT_CHAN_REARRIGHT;
849 if( HAS_CHAN( AOUT_CHAN_REARCENTER ) )
850 p_chans_out[i++] = AOUT_CHAN_REARCENTER;
852 if( HAS_CHAN( AOUT_CHAN_MIDDLELEFT ) )
853 p_chans_out[i++] = AOUT_CHAN_MIDDLELEFT;
854 if( HAS_CHAN( AOUT_CHAN_MIDDLERIGHT ) )
855 p_chans_out[i++] = AOUT_CHAN_MIDDLERIGHT;
857 assert( i <= AOUT_CHAN_MAX );
858 #undef HAS_CHAN
861 static jobject
862 AudioTrack_New21( JNIEnv *env, audio_output_t *p_aout, unsigned int i_rate,
863 int i_channel_config, int i_format, int i_size,
864 jint session_id )
866 jobject p_audiotrack = NULL;
867 jobject p_aattr_builder = NULL;
868 jobject p_audio_attributes = NULL;
869 jobject p_afmt_builder = NULL;
870 jobject p_audio_format = NULL;
871 jobject ref;
873 p_aattr_builder =
874 JNI_CALL( NewObject,
875 jfields.AudioAttributes_Builder.clazz,
876 jfields.AudioAttributes_Builder.ctor );
877 if( !p_aattr_builder )
878 return NULL;
880 ref = JNI_CALL_OBJECT( p_aattr_builder,
881 jfields.AudioAttributes_Builder.setLegacyStreamType,
882 jfields.AudioManager.STREAM_MUSIC );
883 (*env)->DeleteLocalRef( env, ref );
885 p_audio_attributes =
886 JNI_CALL_OBJECT( p_aattr_builder,
887 jfields.AudioAttributes_Builder.build );
888 if( !p_audio_attributes )
889 goto del_local_refs;
891 p_afmt_builder = JNI_CALL( NewObject,
892 jfields.AudioFormat_Builder.clazz,
893 jfields.AudioFormat_Builder.ctor );
894 if( !p_afmt_builder )
895 goto del_local_refs;
897 ref = JNI_CALL_OBJECT( p_afmt_builder,
898 jfields.AudioFormat_Builder.setChannelMask,
899 i_channel_config );
900 if( CHECK_EXCEPTION( "AudioFormat.Builder", "setChannelMask" ) )
902 (*env)->DeleteLocalRef( env, ref );
903 goto del_local_refs;
905 (*env)->DeleteLocalRef( env, ref );
907 ref = JNI_CALL_OBJECT( p_afmt_builder,
908 jfields.AudioFormat_Builder.setEncoding,
909 i_format );
910 if( CHECK_EXCEPTION( "AudioFormat.Builder", "setEncoding" ) )
912 (*env)->DeleteLocalRef( env, ref );
913 goto del_local_refs;
915 (*env)->DeleteLocalRef( env, ref );
917 ref = JNI_CALL_OBJECT( p_afmt_builder,
918 jfields.AudioFormat_Builder.setSampleRate,
919 i_rate );
920 if( CHECK_EXCEPTION( "AudioFormat.Builder", "setSampleRate" ) )
922 (*env)->DeleteLocalRef( env, ref );
923 goto del_local_refs;
925 (*env)->DeleteLocalRef( env, ref );
927 p_audio_format = JNI_CALL_OBJECT( p_afmt_builder,
928 jfields.AudioFormat_Builder.build );
929 if(!p_audio_format)
930 goto del_local_refs;
932 p_audiotrack = JNI_AT_NEW( p_audio_attributes, p_audio_format, i_size,
933 jfields.AudioTrack.MODE_STREAM, session_id );
935 del_local_refs:
936 (*env)->DeleteLocalRef( env, p_aattr_builder );
937 (*env)->DeleteLocalRef( env, p_audio_attributes );
938 (*env)->DeleteLocalRef( env, p_afmt_builder );
939 (*env)->DeleteLocalRef( env, p_audio_format );
940 return p_audiotrack;
943 static jobject
944 AudioTrack_NewLegacy( JNIEnv *env, audio_output_t *p_aout, unsigned int i_rate,
945 int i_channel_config, int i_format, int i_size,
946 jint session_id )
948 VLC_UNUSED( p_aout );
949 return JNI_AT_NEW( jfields.AudioManager.STREAM_MUSIC, i_rate,
950 i_channel_config, i_format, i_size,
951 jfields.AudioTrack.MODE_STREAM, session_id );
955 * Create an Android AudioTrack.
956 * returns -1 on error, 0 on success.
958 static int
959 AudioTrack_New( JNIEnv *env, audio_output_t *p_aout, unsigned int i_rate,
960 int i_channel_config, int i_format, int i_size )
962 aout_sys_t *p_sys = p_aout->sys;
963 jint session_id = var_InheritInteger( p_aout, "audiotrack-session-id" );
965 jobject p_audiotrack;
966 if( jfields.AudioTrack.has_ctor_21 )
967 p_audiotrack = AudioTrack_New21( env, p_aout, i_rate, i_channel_config,
968 i_format, i_size, session_id );
969 else
970 p_audiotrack = AudioTrack_NewLegacy( env, p_aout, i_rate,
971 i_channel_config, i_format, i_size,
972 session_id );
973 if( CHECK_AT_EXCEPTION( "AudioTrack<init>" ) || !p_audiotrack )
975 msg_Warn( p_aout, "AudioTrack Init failed" );
976 return -1;
978 if( JNI_CALL_INT( p_audiotrack, jfields.AudioTrack.getState )
979 != jfields.AudioTrack.STATE_INITIALIZED )
981 JNI_CALL_VOID( p_audiotrack, jfields.AudioTrack.release );
982 (*env)->DeleteLocalRef( env, p_audiotrack );
983 msg_Err( p_aout, "AudioTrack getState failed" );
984 return -1;
987 p_sys->p_audiotrack = (*env)->NewGlobalRef( env, p_audiotrack );
988 (*env)->DeleteLocalRef( env, p_audiotrack );
989 if( !p_sys->p_audiotrack )
990 return -1;
992 return 0;
996 * Destroy and recreate an Android AudioTrack using the same arguments.
997 * returns -1 on error, 0 on success.
999 static int
1000 AudioTrack_Recreate( JNIEnv *env, audio_output_t *p_aout )
1002 aout_sys_t *p_sys = p_aout->sys;
1004 JNI_AT_CALL_VOID( release );
1005 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
1006 p_sys->p_audiotrack = NULL;
1007 return AudioTrack_New( env, p_aout, p_sys->audiotrack_args.i_rate,
1008 p_sys->audiotrack_args.i_channel_config,
1009 p_sys->audiotrack_args.i_format,
1010 p_sys->audiotrack_args.i_size );
1014 * Configure and create an Android AudioTrack.
1015 * returns -1 on configuration error, 0 on success.
1017 static int
1018 AudioTrack_Create( JNIEnv *env, audio_output_t *p_aout,
1019 unsigned int i_rate,
1020 int i_format,
1021 uint16_t i_physical_channels )
1023 aout_sys_t *p_sys = p_aout->sys;
1024 int i_size, i_min_buffer_size, i_channel_config;
1026 switch( i_physical_channels )
1028 case AOUT_CHANS_7_1:
1029 /* bitmask of CHANNEL_OUT_7POINT1 doesn't correspond to 5POINT1 and
1030 * SIDES */
1031 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1 |
1032 jfields.AudioFormat.CHANNEL_OUT_SIDE_LEFT |
1033 jfields.AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
1034 break;
1035 case AOUT_CHANS_5_1:
1036 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1;
1037 break;
1038 case AOUT_CHAN_LEFT:
1039 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_MONO;
1040 break;
1041 case AOUT_CHANS_STEREO:
1042 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_STEREO;
1043 break;
1044 default:
1045 vlc_assert_unreachable();
1048 i_min_buffer_size = JNI_AT_CALL_STATIC_INT( getMinBufferSize, i_rate,
1049 i_channel_config, i_format );
1050 if( i_min_buffer_size <= 0 )
1052 msg_Warn( p_aout, "getMinBufferSize returned an invalid size" ) ;
1053 return -1;
1055 i_size = i_min_buffer_size * 2;
1057 /* create AudioTrack object */
1058 if( AudioTrack_New( env, p_aout, i_rate, i_channel_config,
1059 i_format , i_size ) != 0 )
1060 return -1;
1062 p_sys->audiotrack_args.i_rate = i_rate;
1063 p_sys->audiotrack_args.i_channel_config = i_channel_config;
1064 p_sys->audiotrack_args.i_format = i_format;
1065 p_sys->audiotrack_args.i_size = i_size;
1067 return 0;
1070 static bool
1071 AudioTrack_HasEncoding( audio_output_t *p_aout, vlc_fourcc_t i_format )
1073 aout_sys_t *p_sys = p_aout->sys;
1075 #define MATCH_ENCODING_FLAG(x) jfields.AudioFormat.has_##x && \
1076 ( p_sys->i_encoding_flags == 0 || p_sys->i_encoding_flags & (1 << jfields.AudioFormat.x) )
1078 switch( i_format )
1080 case VLC_CODEC_DTSHD:
1081 return MATCH_ENCODING_FLAG( ENCODING_DTS_HD );
1082 case VLC_CODEC_DTS:
1083 return MATCH_ENCODING_FLAG( ENCODING_DTS );
1084 case VLC_CODEC_A52:
1085 return MATCH_ENCODING_FLAG( ENCODING_AC3 );
1086 case VLC_CODEC_EAC3:
1087 return MATCH_ENCODING_FLAG( ENCODING_E_AC3 );
1088 case VLC_CODEC_TRUEHD:
1089 case VLC_CODEC_MLP:
1090 return MATCH_ENCODING_FLAG( ENCODING_DOLBY_TRUEHD );
1091 default:
1092 return false;
1096 static int
1097 StartPassthrough( JNIEnv *env, audio_output_t *p_aout )
1099 aout_sys_t *p_sys = p_aout->sys;
1100 int i_at_format;
1102 if( !AudioTrack_HasEncoding( p_aout, p_sys->fmt.i_format ) )
1103 return VLC_EGENERIC;
1105 if( jfields.AudioFormat.has_ENCODING_IEC61937 )
1107 i_at_format = jfields.AudioFormat.ENCODING_IEC61937;
1108 switch( p_sys->fmt.i_format )
1110 case VLC_CODEC_TRUEHD:
1111 case VLC_CODEC_MLP:
1112 p_sys->fmt.i_rate = 192000;
1113 p_sys->fmt.i_bytes_per_frame = 16;
1115 /* AudioFormat.ENCODING_IEC61937 documentation says that the
1116 * channel layout must be stereo. Well, not for TrueHD
1117 * apparently */
1118 p_sys->fmt.i_physical_channels = AOUT_CHANS_7_1;
1119 break;
1120 case VLC_CODEC_DTS:
1121 p_sys->fmt.i_bytes_per_frame = 4;
1122 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1123 break;
1124 case VLC_CODEC_DTSHD:
1125 p_sys->fmt.i_bytes_per_frame = 4;
1126 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1127 p_sys->fmt.i_rate = 192000;
1128 p_sys->fmt.i_bytes_per_frame = 16;
1129 break;
1130 case VLC_CODEC_EAC3:
1131 p_sys->fmt.i_rate = 192000;
1132 case VLC_CODEC_A52:
1133 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1134 p_sys->fmt.i_bytes_per_frame = 4;
1135 break;
1136 default:
1137 return VLC_EGENERIC;
1139 p_sys->fmt.i_frame_length = 1;
1140 p_sys->fmt.i_channels = aout_FormatNbChannels( &p_sys->fmt );
1141 p_sys->fmt.i_format = VLC_CODEC_SPDIFL;
1143 else
1145 switch( p_sys->fmt.i_format )
1147 case VLC_CODEC_A52:
1148 if( !jfields.AudioFormat.has_ENCODING_AC3 )
1149 return VLC_EGENERIC;
1150 i_at_format = jfields.AudioFormat.ENCODING_AC3;
1151 break;
1152 case VLC_CODEC_EAC3:
1153 if( !jfields.AudioFormat.has_ENCODING_E_AC3 )
1154 return VLC_EGENERIC;
1155 i_at_format = jfields.AudioFormat.ENCODING_E_AC3;
1156 break;
1157 case VLC_CODEC_DTS:
1158 if( !jfields.AudioFormat.has_ENCODING_DTS )
1159 return VLC_EGENERIC;
1160 i_at_format = jfields.AudioFormat.ENCODING_DTS;
1161 break;
1162 default:
1163 return VLC_EGENERIC;
1165 p_sys->fmt.i_bytes_per_frame = 4;
1166 p_sys->fmt.i_frame_length = 1;
1167 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1168 p_sys->fmt.i_channels = 2;
1169 p_sys->fmt.i_format = VLC_CODEC_SPDIFB;
1172 int i_ret = AudioTrack_Create( env, p_aout, p_sys->fmt.i_rate, i_at_format,
1173 p_sys->fmt.i_physical_channels );
1174 if( i_ret != VLC_SUCCESS )
1175 msg_Warn( p_aout, "SPDIF configuration failed" );
1176 else
1178 p_sys->b_passthrough = true;
1179 p_sys->i_chans_to_reorder = 0;
1182 return i_ret;
1185 static int
1186 StartPCM( JNIEnv *env, audio_output_t *p_aout, unsigned i_max_channels )
1188 aout_sys_t *p_sys = p_aout->sys;
1189 unsigned i_nb_channels;
1190 int i_at_format, i_ret;
1192 if (jfields.AudioTrack.getNativeOutputSampleRate)
1193 p_sys->fmt.i_rate =
1194 JNI_AT_CALL_STATIC_INT( getNativeOutputSampleRate,
1195 jfields.AudioManager.STREAM_MUSIC );
1196 else
1197 p_sys->fmt.i_rate = VLC_CLIP( p_sys->fmt.i_rate, 4000, 48000 );
1201 /* We can only accept U8, S16N, FL32, and AC3 */
1202 switch( p_sys->fmt.i_format )
1204 case VLC_CODEC_U8:
1205 i_at_format = jfields.AudioFormat.ENCODING_PCM_8BIT;
1206 break;
1207 case VLC_CODEC_S16N:
1208 i_at_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
1209 break;
1210 case VLC_CODEC_FL32:
1211 if( jfields.AudioFormat.has_ENCODING_PCM_FLOAT )
1212 i_at_format = jfields.AudioFormat.ENCODING_PCM_FLOAT;
1213 else
1215 p_sys->fmt.i_format = VLC_CODEC_S16N;
1216 i_at_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
1218 break;
1219 default:
1220 p_sys->fmt.i_format = VLC_CODEC_S16N;
1221 i_at_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
1222 break;
1225 /* Android AudioTrack supports only mono, stereo, 5.1 and 7.1.
1226 * Android will downmix to stereo if audio output doesn't handle 5.1 or 7.1
1229 i_nb_channels = aout_FormatNbChannels( &p_sys->fmt );
1230 if( i_nb_channels == 0 )
1231 return VLC_EGENERIC;
1232 if( AOUT_FMT_LINEAR( &p_sys->fmt ) )
1233 i_nb_channels = __MIN( i_max_channels, i_nb_channels );
1234 if( i_nb_channels > 5 )
1236 if( i_nb_channels > 7 && jfields.AudioFormat.has_CHANNEL_OUT_SIDE )
1237 p_sys->fmt.i_physical_channels = AOUT_CHANS_7_1;
1238 else
1239 p_sys->fmt.i_physical_channels = AOUT_CHANS_5_1;
1240 } else
1242 if( i_nb_channels == 1 )
1243 p_sys->fmt.i_physical_channels = AOUT_CHAN_LEFT;
1244 else
1245 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1248 /* Try to create an AudioTrack with the most advanced channel and
1249 * format configuration. If AudioTrack_Create fails, try again with a
1250 * less advanced format (PCM S16N). If it fails again, try again with
1251 * Stereo channels. */
1252 i_ret = AudioTrack_Create( env, p_aout, p_sys->fmt.i_rate, i_at_format,
1253 p_sys->fmt.i_physical_channels );
1254 if( i_ret != 0 )
1256 if( p_sys->fmt.i_format == VLC_CODEC_FL32 )
1258 msg_Warn( p_aout, "FL32 configuration failed, "
1259 "fallback to S16N PCM" );
1260 p_sys->fmt.i_format = VLC_CODEC_S16N;
1262 else if( p_sys->fmt.i_physical_channels & AOUT_CHANS_5_1 )
1264 msg_Warn( p_aout, "5.1 or 7.1 configuration failed, "
1265 "fallback to Stereo" );
1266 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1268 else
1269 break;
1271 } while( i_ret != 0 );
1273 if( i_ret != VLC_SUCCESS )
1274 return i_ret;
1276 uint32_t p_chans_out[AOUT_CHAN_MAX];
1277 memset( p_chans_out, 0, sizeof(p_chans_out) );
1278 AudioTrack_GetChanOrder( p_sys->fmt.i_physical_channels, p_chans_out );
1279 p_sys->i_chans_to_reorder =
1280 aout_CheckChannelReorder( NULL, p_chans_out,
1281 p_sys->fmt.i_physical_channels,
1282 p_sys->p_chan_table );
1283 aout_FormatPrepare( &p_sys->fmt );
1284 return VLC_SUCCESS;
1287 static int
1288 Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
1290 aout_sys_t *p_sys = p_aout->sys;
1291 JNIEnv *env;
1292 int i_ret;
1293 bool b_try_passthrough;
1294 unsigned i_max_channels;
1296 if( p_sys->at_dev == AT_DEV_ENCODED )
1298 b_try_passthrough = true;
1299 i_max_channels = AT_DEV_MAX_CHANNELS;
1301 else
1303 b_try_passthrough = var_InheritBool( p_aout, "spdif" );
1304 i_max_channels = p_sys->at_dev == AT_DEV_STEREO ? 2 : AT_DEV_MAX_CHANNELS;
1307 if( !( env = GET_ENV() ) )
1308 return VLC_EGENERIC;
1310 p_sys->fmt = *p_fmt;
1312 aout_FormatPrint( p_aout, "VLC is looking for:", &p_sys->fmt );
1314 bool low_latency = false;
1315 if (p_sys->fmt.channel_type == AUDIO_CHANNEL_TYPE_AMBISONICS)
1317 p_sys->fmt.channel_type = AUDIO_CHANNEL_TYPE_BITMAP;
1319 /* TODO: detect sink channel layout */
1320 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
1321 aout_FormatPrepare(&p_sys->fmt);
1322 low_latency = true;
1325 if( AOUT_FMT_LINEAR( &p_sys->fmt ) )
1326 i_ret = StartPCM( env, p_aout, i_max_channels );
1327 else if( b_try_passthrough )
1328 i_ret = StartPassthrough( env, p_aout );
1329 else
1330 return VLC_EGENERIC;
1332 if( i_ret != 0 )
1333 return VLC_EGENERIC;
1335 p_sys->i_max_audiotrack_samples = BYTES_TO_FRAMES( p_sys->audiotrack_args.i_size );
1337 #ifdef AUDIOTRACK_HW_LATENCY
1338 if( jfields.AudioTimestamp.clazz )
1340 /* create AudioTimestamp object */
1341 jobject p_obj = JNI_CALL( NewObject, jfields.AudioTimestamp.clazz,
1342 jfields.AudioTimestamp.ctor );
1343 if( p_obj )
1345 p_sys->timestamp.p_obj = (*env)->NewGlobalRef( env, p_obj );
1346 (*env)->DeleteLocalRef( env, p_obj );
1348 if( !p_sys->timestamp.p_obj )
1349 goto error;
1351 #endif
1353 AudioTrack_Reset( env, p_aout );
1355 if( p_sys->fmt.i_format == VLC_CODEC_FL32 )
1357 msg_Dbg( p_aout, "using WRITE_FLOATARRAY");
1358 p_sys->i_write_type = WRITE_FLOATARRAY;
1360 else if( p_sys->fmt.i_format == VLC_CODEC_SPDIFL )
1362 assert( jfields.AudioFormat.has_ENCODING_IEC61937 );
1363 msg_Dbg( p_aout, "using WRITE_SHORTARRAYV23");
1364 p_sys->i_write_type = WRITE_SHORTARRAYV23;
1366 else if( jfields.AudioTrack.writeV23 )
1368 msg_Dbg( p_aout, "using WRITE_BYTEARRAYV23");
1369 p_sys->i_write_type = WRITE_BYTEARRAYV23;
1371 else if( jfields.AudioTrack.writeBufferV21 )
1373 msg_Dbg( p_aout, "using WRITE_BYTEBUFFER");
1374 p_sys->i_write_type = WRITE_BYTEBUFFER;
1376 else
1378 msg_Dbg( p_aout, "using WRITE_BYTEARRAY");
1379 p_sys->i_write_type = WRITE_BYTEARRAY;
1382 p_sys->circular.i_read = p_sys->circular.i_write = 0;
1383 p_sys->circular.i_size = (int)p_sys->fmt.i_rate
1384 * p_sys->fmt.i_bytes_per_frame
1385 / p_sys->fmt.i_frame_length;
1386 if (low_latency)
1388 /* 40 ms of buffering */
1389 p_sys->circular.i_size = p_sys->circular.i_size / 25;
1391 else
1393 /* 2 seconds of buffering */
1394 p_sys->circular.i_size = samples_from_vlc_tick(AOUT_MAX_PREPARE_TIME, p_sys->circular.i_size);
1397 /* Allocate circular buffer */
1398 switch( p_sys->i_write_type )
1400 case WRITE_BYTEARRAY:
1401 case WRITE_BYTEARRAYV23:
1403 jbyteArray p_bytearray;
1405 p_bytearray = (*env)->NewByteArray( env, p_sys->circular.i_size );
1406 if( p_bytearray )
1408 p_sys->circular.u.p_bytearray = (*env)->NewGlobalRef( env, p_bytearray );
1409 (*env)->DeleteLocalRef( env, p_bytearray );
1412 if( !p_sys->circular.u.p_bytearray )
1414 msg_Err(p_aout, "byte array allocation failed");
1415 goto error;
1417 break;
1419 case WRITE_SHORTARRAYV23:
1421 jshortArray p_shortarray;
1423 p_shortarray = (*env)->NewShortArray( env,
1424 p_sys->circular.i_size / 2 );
1425 if( p_shortarray )
1427 p_sys->circular.u.p_shortarray = (*env)->NewGlobalRef( env, p_shortarray );
1428 (*env)->DeleteLocalRef( env, p_shortarray );
1430 if( !p_sys->circular.u.p_shortarray )
1432 msg_Err(p_aout, "short array allocation failed");
1433 goto error;
1435 break;
1437 case WRITE_FLOATARRAY:
1439 jfloatArray p_floatarray;
1441 p_floatarray = (*env)->NewFloatArray( env,
1442 p_sys->circular.i_size / 4 );
1443 if( p_floatarray )
1445 p_sys->circular.u.p_floatarray = (*env)->NewGlobalRef( env, p_floatarray );
1446 (*env)->DeleteLocalRef( env, p_floatarray );
1448 if( !p_sys->circular.u.p_floatarray )
1450 msg_Err(p_aout, "float array allocation failed");
1451 goto error;
1453 break;
1455 case WRITE_BYTEBUFFER:
1456 p_sys->circular.u.bytebuffer.p_data = malloc( p_sys->circular.i_size );
1457 if( !p_sys->circular.u.bytebuffer.p_data )
1459 msg_Err(p_aout, "bytebuffer allocation failed");
1460 goto error;
1462 break;
1465 /* Run AudioTrack_Thread */
1466 p_sys->b_thread_running = true;
1467 p_sys->b_thread_paused = false;
1468 if ( vlc_clone( &p_sys->thread, AudioTrack_Thread, p_aout,
1469 VLC_THREAD_PRIORITY_LOW ) )
1471 msg_Err(p_aout, "vlc clone failed");
1472 goto error;
1475 JNI_AT_CALL_VOID( play );
1476 CHECK_AT_EXCEPTION( "play" );
1478 *p_fmt = p_sys->fmt;
1480 p_aout->volume_set(p_aout, p_sys->volume);
1481 if (p_sys->mute)
1482 p_aout->mute_set(p_aout, true);
1483 aout_FormatPrint( p_aout, "VLC will output:", &p_sys->fmt );
1485 return VLC_SUCCESS;
1487 error:
1488 Stop( p_aout );
1489 return VLC_EGENERIC;
1492 static void
1493 Stop( audio_output_t *p_aout )
1495 aout_sys_t *p_sys = p_aout->sys;
1496 JNIEnv *env;
1498 if( !( env = GET_ENV() ) )
1499 return;
1501 /* Stop the AudioTrack thread */
1502 vlc_mutex_lock( &p_sys->lock );
1503 if( p_sys->b_thread_running )
1505 p_sys->b_thread_running = false;
1506 vlc_cond_signal( &p_sys->thread_cond );
1507 vlc_mutex_unlock( &p_sys->lock );
1508 vlc_join( p_sys->thread, NULL );
1510 else
1511 vlc_mutex_unlock( &p_sys->lock );
1513 /* Release the AudioTrack object */
1514 if( p_sys->p_audiotrack )
1516 if( !p_sys->b_audiotrack_exception )
1518 JNI_AT_CALL_VOID( stop );
1519 if( !CHECK_AT_EXCEPTION( "stop" ) )
1520 JNI_AT_CALL_VOID( release );
1522 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
1523 p_sys->p_audiotrack = NULL;
1526 /* Release the timestamp object */
1527 if( p_sys->timestamp.p_obj )
1529 (*env)->DeleteGlobalRef( env, p_sys->timestamp.p_obj );
1530 p_sys->timestamp.p_obj = NULL;
1533 /* Release the Circular buffer data */
1534 switch( p_sys->i_write_type )
1536 case WRITE_BYTEARRAY:
1537 case WRITE_BYTEARRAYV23:
1538 if( p_sys->circular.u.p_bytearray )
1540 (*env)->DeleteGlobalRef( env, p_sys->circular.u.p_bytearray );
1541 p_sys->circular.u.p_bytearray = NULL;
1543 break;
1544 case WRITE_SHORTARRAYV23:
1545 if( p_sys->circular.u.p_shortarray )
1547 (*env)->DeleteGlobalRef( env, p_sys->circular.u.p_shortarray );
1548 p_sys->circular.u.p_shortarray = NULL;
1550 break;
1551 case WRITE_FLOATARRAY:
1552 if( p_sys->circular.u.p_floatarray )
1554 (*env)->DeleteGlobalRef( env, p_sys->circular.u.p_floatarray );
1555 p_sys->circular.u.p_floatarray = NULL;
1557 break;
1558 case WRITE_BYTEBUFFER:
1559 free( p_sys->circular.u.bytebuffer.p_data );
1560 p_sys->circular.u.bytebuffer.p_data = NULL;
1561 break;
1564 p_sys->b_audiotrack_exception = false;
1565 p_sys->b_error = false;
1566 p_sys->b_passthrough = false;
1570 * Non blocking write function, run from AudioTrack_Thread.
1571 * Do a calculation between current position and audiotrack position and assure
1572 * that we won't wait in AudioTrack.write() method.
1574 static int
1575 AudioTrack_PlayByteArray( JNIEnv *env, audio_output_t *p_aout,
1576 size_t i_data_size, size_t i_data_offset,
1577 bool b_force )
1579 aout_sys_t *p_sys = p_aout->sys;
1580 uint64_t i_samples;
1581 uint64_t i_audiotrack_pos;
1582 uint64_t i_samples_pending;
1584 i_audiotrack_pos = AudioTrack_getPlaybackHeadPosition( env, p_aout );
1586 assert( i_audiotrack_pos <= p_sys->i_samples_written );
1587 if( i_audiotrack_pos > p_sys->i_samples_written )
1589 msg_Err( p_aout, "audiotrack position is ahead. Should NOT happen" );
1590 p_sys->i_samples_written = 0;
1591 p_sys->b_error = true;
1592 return 0;
1594 i_samples_pending = p_sys->i_samples_written - i_audiotrack_pos;
1596 /* check if audiotrack buffer is not full before writing on it. */
1597 if( b_force )
1599 msg_Warn( p_aout, "Force write. It may block..." );
1600 i_samples_pending = 0;
1601 } else if( i_samples_pending >= p_sys->i_max_audiotrack_samples )
1602 return 0;
1604 i_samples = __MIN( p_sys->i_max_audiotrack_samples - i_samples_pending,
1605 BYTES_TO_FRAMES( i_data_size ) );
1607 i_data_size = FRAMES_TO_BYTES( i_samples );
1609 return JNI_AT_CALL_INT( write, p_sys->circular.u.p_bytearray,
1610 i_data_offset, i_data_size );
1614 * Non blocking write function for Android M and after, run from
1615 * AudioTrack_Thread. It calls a new write method with WRITE_NON_BLOCKING
1616 * flags.
1618 static int
1619 AudioTrack_PlayByteArrayV23( JNIEnv *env, audio_output_t *p_aout,
1620 size_t i_data_size, size_t i_data_offset )
1622 aout_sys_t *p_sys = p_aout->sys;
1624 return JNI_AT_CALL_INT( writeV23, p_sys->circular.u.p_bytearray,
1625 i_data_offset, i_data_size,
1626 jfields.AudioTrack.WRITE_NON_BLOCKING );
1630 * Non blocking play function for Lollipop and after, run from
1631 * AudioTrack_Thread. It calls a new write method with WRITE_NON_BLOCKING
1632 * flags.
1634 static int
1635 AudioTrack_PlayByteBuffer( JNIEnv *env, audio_output_t *p_aout,
1636 size_t i_data_size, size_t i_data_offset )
1638 aout_sys_t *p_sys = p_aout->sys;
1640 /* The same DirectByteBuffer will be used until the data_offset reaches 0.
1641 * The internal position of this buffer is moved by the writeBufferV21
1642 * wall. */
1643 if( i_data_offset == 0 )
1645 /* No need to get a global ref, this object will be only used from the
1646 * same Thread */
1647 if( p_sys->circular.u.bytebuffer.p_obj )
1648 (*env)->DeleteLocalRef( env, p_sys->circular.u.bytebuffer.p_obj );
1650 p_sys->circular.u.bytebuffer.p_obj = (*env)->NewDirectByteBuffer( env,
1651 p_sys->circular.u.bytebuffer.p_data,
1652 p_sys->circular.i_size );
1653 if( !p_sys->circular.u.bytebuffer.p_obj )
1655 if( (*env)->ExceptionCheck( env ) )
1656 (*env)->ExceptionClear( env );
1657 return jfields.AudioTrack.ERROR;
1661 return JNI_AT_CALL_INT( writeBufferV21, p_sys->circular.u.bytebuffer.p_obj,
1662 i_data_size,
1663 jfields.AudioTrack.WRITE_NON_BLOCKING );
1667 * Non blocking short write function for Android M and after, run from
1668 * AudioTrack_Thread. It calls a new write method with WRITE_NON_BLOCKING
1669 * flags.
1671 static int
1672 AudioTrack_PlayShortArrayV23( JNIEnv *env, audio_output_t *p_aout,
1673 size_t i_data_size, size_t i_data_offset )
1675 aout_sys_t *p_sys = p_aout->sys;
1676 int i_ret;
1678 i_ret = JNI_AT_CALL_INT( writeShortV23, p_sys->circular.u.p_shortarray,
1679 i_data_offset / 2, i_data_size / 2,
1680 jfields.AudioTrack.WRITE_NON_BLOCKING );
1681 if( i_ret < 0 )
1682 return i_ret;
1683 else
1684 return i_ret * 2;
1688 * Non blocking play float function for Lollipop and after, run from
1689 * AudioTrack_Thread. It calls a new write method with WRITE_NON_BLOCKING
1690 * flags.
1692 static int
1693 AudioTrack_PlayFloatArray( JNIEnv *env, audio_output_t *p_aout,
1694 size_t i_data_size, size_t i_data_offset )
1696 aout_sys_t *p_sys = p_aout->sys;
1697 int i_ret;
1699 i_ret = JNI_AT_CALL_INT( writeFloat, p_sys->circular.u.p_floatarray,
1700 i_data_offset / 4, i_data_size / 4,
1701 jfields.AudioTrack.WRITE_NON_BLOCKING );
1702 if( i_ret < 0 )
1703 return i_ret;
1704 else
1705 return i_ret * 4;
1708 static int
1709 AudioTrack_Play( JNIEnv *env, audio_output_t *p_aout, size_t i_data_size,
1710 size_t i_data_offset, bool b_force )
1712 aout_sys_t *p_sys = p_aout->sys;
1713 int i_ret;
1715 switch( p_sys->i_write_type )
1717 case WRITE_BYTEARRAYV23:
1718 i_ret = AudioTrack_PlayByteArrayV23( env, p_aout, i_data_size,
1719 i_data_offset );
1720 break;
1721 case WRITE_BYTEBUFFER:
1722 i_ret = AudioTrack_PlayByteBuffer( env, p_aout, i_data_size,
1723 i_data_offset );
1724 break;
1725 case WRITE_SHORTARRAYV23:
1726 i_ret = AudioTrack_PlayShortArrayV23( env, p_aout, i_data_size,
1727 i_data_offset );
1728 break;
1729 case WRITE_BYTEARRAY:
1730 i_ret = AudioTrack_PlayByteArray( env, p_aout, i_data_size,
1731 i_data_offset, b_force );
1732 break;
1733 case WRITE_FLOATARRAY:
1734 i_ret = AudioTrack_PlayFloatArray( env, p_aout, i_data_size,
1735 i_data_offset );
1736 break;
1737 default:
1738 vlc_assert_unreachable();
1741 if( i_ret < 0 ) {
1742 if( jfields.AudioManager.has_ERROR_DEAD_OBJECT
1743 && i_ret == jfields.AudioManager.ERROR_DEAD_OBJECT )
1745 msg_Warn( p_aout, "ERROR_DEAD_OBJECT: "
1746 "try recreating AudioTrack" );
1747 if( ( i_ret = AudioTrack_Recreate( env, p_aout ) ) == 0 )
1749 AudioTrack_Reset( env, p_aout );
1750 JNI_AT_CALL_VOID( play );
1751 CHECK_AT_EXCEPTION( "play" );
1753 } else
1755 const char *str;
1756 if( i_ret == jfields.AudioTrack.ERROR_INVALID_OPERATION )
1757 str = "ERROR_INVALID_OPERATION";
1758 else if( i_ret == jfields.AudioTrack.ERROR_BAD_VALUE )
1759 str = "ERROR_BAD_VALUE";
1760 else
1761 str = "ERROR";
1762 msg_Err( p_aout, "Write failed: %s", str );
1763 p_sys->b_error = true;
1765 } else
1766 p_sys->i_samples_written += BYTES_TO_FRAMES( i_ret );
1767 return i_ret;
1771 * This thread will play the data coming from the circular buffer.
1773 static void *
1774 AudioTrack_Thread( void *p_data )
1776 audio_output_t *p_aout = p_data;
1777 aout_sys_t *p_sys = p_aout->sys;
1778 JNIEnv *env = GET_ENV();
1779 vlc_tick_t i_play_deadline = 0;
1780 vlc_tick_t i_last_time_blocked = 0;
1782 if( !env )
1783 return NULL;
1785 for( ;; )
1787 int i_ret = 0;
1788 bool b_forced;
1789 size_t i_data_offset;
1790 size_t i_data_size;
1792 vlc_mutex_lock( &p_sys->lock );
1794 /* Wait for free space in Audiotrack internal buffer */
1795 if( i_play_deadline != 0 && vlc_tick_now() < i_play_deadline )
1797 /* Don't wake up the thread when there is new data since we are
1798 * waiting for more space */
1799 p_sys->b_thread_waiting = true;
1800 while( p_sys->b_thread_running && i_ret == 0 )
1801 i_ret = vlc_cond_timedwait( &p_sys->thread_cond,
1802 &p_sys->lock,
1803 i_play_deadline );
1804 i_play_deadline = 0;
1805 p_sys->b_thread_waiting = false;
1808 /* Wait for not paused state */
1809 while( p_sys->b_thread_running && p_sys->b_thread_paused )
1811 i_last_time_blocked = 0;
1812 vlc_cond_wait( &p_sys->thread_cond, &p_sys->lock );
1815 /* Wait for more data in the circular buffer */
1816 while( p_sys->b_thread_running
1817 && p_sys->circular.i_read >= p_sys->circular.i_write )
1818 vlc_cond_wait( &p_sys->thread_cond, &p_sys->lock );
1820 if( !p_sys->b_thread_running || p_sys->b_error )
1822 vlc_mutex_unlock( &p_sys->lock );
1823 break;
1826 /* HACK: AudioFlinger can drop frames without notifying us and there is
1827 * no way to know it. If it happens, i_audiotrack_pos won't move and
1828 * the current code will be stuck because it'll assume that audiotrack
1829 * internal buffer is full when it's not. It may happen only after
1830 * Android 4.4.2 if we send frames too quickly. To fix this issue,
1831 * force the writing of the buffer after a certain delay. */
1832 if( i_last_time_blocked != 0 )
1833 b_forced = vlc_tick_now() - i_last_time_blocked >
1834 FRAMES_TO_US( p_sys->i_max_audiotrack_samples ) * 2;
1835 else
1836 b_forced = false;
1838 i_data_offset = p_sys->circular.i_read % p_sys->circular.i_size;
1839 i_data_size = __MIN( p_sys->circular.i_size - i_data_offset,
1840 p_sys->circular.i_write - p_sys->circular.i_read );
1842 i_ret = AudioTrack_Play( env, p_aout, i_data_size, i_data_offset,
1843 b_forced );
1844 if( i_ret >= 0 )
1846 if( p_sys->i_write_type == WRITE_BYTEARRAY )
1848 if( i_ret != 0 )
1849 i_last_time_blocked = 0;
1850 else if( i_last_time_blocked == 0 )
1851 i_last_time_blocked = vlc_tick_now();
1854 if( i_ret == 0 )
1855 i_play_deadline = vlc_tick_now() + __MAX( 10000, FRAMES_TO_US(
1856 p_sys->i_max_audiotrack_samples / 5 ) );
1857 else
1858 p_sys->circular.i_read += i_ret;
1861 vlc_cond_signal( &p_sys->aout_cond );
1862 vlc_mutex_unlock( &p_sys->lock );
1865 if( p_sys->circular.u.bytebuffer.p_obj )
1867 (*env)->DeleteLocalRef( env, p_sys->circular.u.bytebuffer.p_obj );
1868 p_sys->circular.u.bytebuffer.p_obj = NULL;
1871 return NULL;
1874 static int
1875 ConvertFromIEC61937( audio_output_t *p_aout, block_t *p_buffer )
1877 /* This function is only used for Android API 23 when AudioTrack is
1878 * configured with ENCODING_ AC3/E_AC3/DTS. In that case, only the codec
1879 * data is needed (without the IEC61937 encapsulation). This function
1880 * recovers the codec data from an EC61937 frame. It is the opposite of the
1881 * code found in converter/tospdif.c. We could also request VLC core to
1882 * send us the codec data directly, but in that case, we wouldn't benefit
1883 * from the eac3 block merger of tospdif.c. */
1885 VLC_UNUSED( p_aout );
1886 uint8_t i_length_mul;
1888 if( p_buffer->i_buffer < 6 )
1889 return -1;
1891 switch( GetWBE( &p_buffer->p_buffer[4] ) & 0xFF )
1893 case 0x01: /* IEC61937_AC3 */
1894 i_length_mul = 8;
1895 break;
1896 case 0x15: /* IEC61937_EAC3 */
1897 i_length_mul = 1;
1898 break;
1899 case 0x0B: /* IEC61937_DTS1 */
1900 case 0x0C: /* IEC61937_DTS2 */
1901 case 0x0D: /* IEC61937_DTS3 */
1902 i_length_mul = 8;
1903 break;
1904 case 0x11: /* IEC61937_DTSHD */
1905 i_length_mul = 1;
1906 break;
1907 default:
1908 vlc_assert_unreachable();
1910 uint16_t i_length = GetWBE( &p_buffer->p_buffer[6] );
1911 if( i_length == 0 )
1912 return -1;
1914 i_length /= i_length_mul;
1915 if( i_length > p_buffer->i_buffer - 8 )
1916 return -1;
1918 p_buffer->p_buffer += 8; /* SPDIF_HEADER_SIZE */
1919 p_buffer->i_buffer = i_length;
1921 return 0;
1924 static void
1925 Play( audio_output_t *p_aout, block_t *p_buffer, vlc_tick_t i_date )
1927 JNIEnv *env = NULL;
1928 size_t i_buffer_offset = 0;
1929 aout_sys_t *p_sys = p_aout->sys;
1931 if( p_sys->b_passthrough && p_sys->fmt.i_format == VLC_CODEC_SPDIFB
1932 && ConvertFromIEC61937( p_aout, p_buffer ) != 0 )
1934 block_Release(p_buffer);
1935 return;
1938 vlc_mutex_lock( &p_sys->lock );
1940 if( p_sys->b_error || !( env = GET_ENV() ) )
1941 goto bailout;
1943 if( p_sys->i_chans_to_reorder )
1944 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
1945 p_sys->i_chans_to_reorder, p_sys->p_chan_table,
1946 p_sys->fmt.i_format );
1948 while( i_buffer_offset < p_buffer->i_buffer && !p_sys->b_error )
1950 size_t i_circular_free;
1951 size_t i_data_offset;
1952 size_t i_data_size;
1954 /* Wait for enough room in circular buffer */
1955 while( !p_sys->b_error && ( i_circular_free = p_sys->circular.i_size -
1956 ( p_sys->circular.i_write - p_sys->circular.i_read ) ) == 0 )
1957 vlc_cond_wait( &p_sys->aout_cond, &p_sys->lock );
1958 if( p_sys->b_error )
1959 goto bailout;
1961 i_data_offset = p_sys->circular.i_write % p_sys->circular.i_size;
1962 i_data_size = __MIN( p_buffer->i_buffer - i_buffer_offset,
1963 p_sys->circular.i_size - i_data_offset );
1964 i_data_size = __MIN( i_data_size, i_circular_free );
1966 switch( p_sys->i_write_type )
1968 case WRITE_BYTEARRAY:
1969 case WRITE_BYTEARRAYV23:
1970 (*env)->SetByteArrayRegion( env, p_sys->circular.u.p_bytearray,
1971 i_data_offset, i_data_size,
1972 (jbyte *)p_buffer->p_buffer
1973 + i_buffer_offset);
1974 break;
1975 case WRITE_SHORTARRAYV23:
1976 i_data_offset &= ~1;
1977 i_data_size &= ~1;
1978 (*env)->SetShortArrayRegion( env, p_sys->circular.u.p_shortarray,
1979 i_data_offset / 2, i_data_size / 2,
1980 (jshort *)p_buffer->p_buffer
1981 + i_buffer_offset / 2);
1982 break;
1983 case WRITE_FLOATARRAY:
1984 i_data_offset &= ~3;
1985 i_data_size &= ~3;
1986 (*env)->SetFloatArrayRegion( env, p_sys->circular.u.p_floatarray,
1987 i_data_offset / 4, i_data_size / 4,
1988 (jfloat *)p_buffer->p_buffer
1989 + i_buffer_offset / 4);
1991 break;
1992 case WRITE_BYTEBUFFER:
1993 memcpy( p_sys->circular.u.bytebuffer.p_data + i_data_offset,
1994 p_buffer->p_buffer + i_buffer_offset, i_data_size );
1995 break;
1998 i_buffer_offset += i_data_size;
1999 p_sys->circular.i_write += i_data_size;
2001 if( !p_sys->b_thread_waiting )
2002 vlc_cond_signal( &p_sys->thread_cond );
2005 bailout:
2006 vlc_mutex_unlock( &p_sys->lock );
2007 block_Release( p_buffer );
2008 (void) i_date;
2011 static void
2012 Pause( audio_output_t *p_aout, bool b_pause, vlc_tick_t i_date )
2014 aout_sys_t *p_sys = p_aout->sys;
2015 JNIEnv *env;
2016 VLC_UNUSED( i_date );
2018 vlc_mutex_lock( &p_sys->lock );
2020 if( p_sys->b_error || !( env = GET_ENV() ) )
2021 goto bailout;
2023 if( b_pause )
2025 p_sys->b_thread_paused = true;
2026 JNI_AT_CALL_VOID( pause );
2027 CHECK_AT_EXCEPTION( "pause" );
2028 } else
2030 p_sys->b_thread_paused = false;
2031 AudioTrack_ResetPositions( env, p_aout );
2032 JNI_AT_CALL_VOID( play );
2033 CHECK_AT_EXCEPTION( "play" );
2036 bailout:
2037 vlc_mutex_unlock( &p_sys->lock );
2040 static void
2041 Flush( audio_output_t *p_aout )
2043 aout_sys_t *p_sys = p_aout->sys;
2044 JNIEnv *env;
2046 vlc_mutex_lock( &p_sys->lock );
2048 if( p_sys->b_error || !( env = GET_ENV() ) )
2049 goto bailout;
2051 /* Android doc:
2052 * stop(): Stops playing the audio data. When used on an instance created
2053 * in MODE_STREAM mode, audio will stop playing after the last buffer that
2054 * was written has been played. For an immediate stop, use pause(),
2055 * followed by flush() to discard audio data that hasn't been played back
2056 * yet.
2058 * flush(): Flushes the audio data currently queued for playback. Any data
2059 * that has not been played back will be discarded. No-op if not stopped
2060 * or paused, or if the track's creation mode is not MODE_STREAM.
2062 JNI_AT_CALL_VOID( pause );
2063 if( CHECK_AT_EXCEPTION( "pause" ) )
2064 goto bailout;
2065 JNI_AT_CALL_VOID( flush );
2066 p_sys->circular.i_read = p_sys->circular.i_write = 0;
2068 /* HACK: Before Android 4.4, the head position is not reset to zero and is
2069 * still moving after a flush or a stop. This prevents to get a precise
2070 * head position and there is no way to know when it stabilizes. Therefore
2071 * recreate an AudioTrack object in that case. The AudioTimestamp class was
2072 * added in API Level 19, so if this class is not found, the Android
2073 * Version is 4.3 or before */
2074 if( !jfields.AudioTimestamp.clazz && p_sys->i_samples_written > 0 )
2076 if( AudioTrack_Recreate( env, p_aout ) != 0 )
2078 p_sys->b_error = true;
2079 goto bailout;
2082 AudioTrack_Reset( env, p_aout );
2083 JNI_AT_CALL_VOID( play );
2084 CHECK_AT_EXCEPTION( "play" );
2086 bailout:
2087 vlc_mutex_unlock( &p_sys->lock );
2090 static int
2091 VolumeSet( audio_output_t *p_aout, float volume )
2093 aout_sys_t *p_sys = p_aout->sys;
2094 JNIEnv *env;
2095 float gain = 1.0f;
2097 if (volume > 1.f)
2099 p_sys->volume = 1.f;
2100 gain = volume;
2102 else
2103 p_sys->volume = volume;
2105 if( !p_sys->b_error && p_sys->p_audiotrack != NULL && ( env = GET_ENV() ) )
2107 if( jfields.AudioTrack.setVolume )
2109 JNI_AT_CALL_INT( setVolume, volume );
2110 CHECK_AT_EXCEPTION( "setVolume" );
2111 } else
2113 JNI_AT_CALL_INT( setStereoVolume, volume, volume );
2114 CHECK_AT_EXCEPTION( "setStereoVolume" );
2117 aout_VolumeReport(p_aout, volume);
2118 aout_GainRequest(p_aout, gain * gain * gain);
2119 return 0;
2122 static int
2123 MuteSet( audio_output_t *p_aout, bool mute )
2125 aout_sys_t *p_sys = p_aout->sys;
2126 JNIEnv *env;
2127 p_sys->mute = mute;
2129 if( !p_sys->b_error && p_sys->p_audiotrack != NULL && ( env = GET_ENV() ) )
2131 if( jfields.AudioTrack.setVolume )
2133 JNI_AT_CALL_INT( setVolume, mute ? 0.0f : p_sys->volume );
2134 CHECK_AT_EXCEPTION( "setVolume" );
2135 } else
2137 JNI_AT_CALL_INT( setStereoVolume, mute ? 0.0f : p_sys->volume, mute ? 0.0f : p_sys->volume );
2138 CHECK_AT_EXCEPTION( "setStereoVolume" );
2141 aout_MuteReport(p_aout, mute);
2142 return 0;
2145 static int DeviceSelect(audio_output_t *p_aout, const char *p_id)
2147 aout_sys_t *p_sys = p_aout->sys;
2148 enum at_dev at_dev = AT_DEV_DEFAULT;
2150 if( p_id )
2152 for( unsigned int i = 0; at_devs[i].id; ++i )
2154 if( strncmp( p_id, at_devs[i].id, strlen( at_devs[i].id ) ) == 0 )
2156 at_dev = at_devs[i].at_dev;
2157 break;
2162 long long i_encoding_flags = 0;
2163 if( at_dev == AT_DEV_ENCODED )
2165 const size_t i_prefix_size = strlen( "encoded:" );
2166 if( strncmp( p_id, "encoded:", i_prefix_size ) == 0 )
2167 i_encoding_flags = atoll( p_id + i_prefix_size );
2170 if( at_dev != p_sys->at_dev || i_encoding_flags != p_sys->i_encoding_flags )
2172 p_sys->at_dev = at_dev;
2173 p_sys->i_encoding_flags = i_encoding_flags;
2174 aout_RestartRequest( p_aout, AOUT_RESTART_OUTPUT );
2175 msg_Dbg( p_aout, "selected device: %s", p_id );
2177 if( at_dev == AT_DEV_ENCODED )
2179 static const vlc_fourcc_t enc_fourccs[] = {
2180 VLC_CODEC_DTS, VLC_CODEC_DTSHD, VLC_CODEC_A52, VLC_CODEC_EAC3,
2181 VLC_CODEC_TRUEHD,
2183 for( size_t i = 0;
2184 i < sizeof( enc_fourccs ) / sizeof( enc_fourccs[0] ); ++i )
2186 if( AudioTrack_HasEncoding( p_aout, enc_fourccs[i] ) )
2187 msg_Dbg( p_aout, "device has %4.4s passthrough support",
2188 (const char *)&enc_fourccs[i] );
2192 aout_DeviceReport( p_aout, p_id );
2193 return VLC_SUCCESS;
2196 static int
2197 Open( vlc_object_t *obj )
2199 audio_output_t *p_aout = (audio_output_t *) obj;
2200 aout_sys_t *p_sys;
2201 JNIEnv *env = GET_ENV();
2203 if( !env || !InitJNIFields( p_aout, env ) )
2204 return VLC_EGENERIC;
2206 p_sys = calloc( 1, sizeof (aout_sys_t) );
2208 if( unlikely( p_sys == NULL ) )
2209 return VLC_ENOMEM;
2211 p_sys->at_dev = AT_DEV_DEFAULT;
2212 vlc_mutex_init(&p_sys->lock);
2213 vlc_cond_init(&p_sys->aout_cond);
2214 vlc_cond_init(&p_sys->thread_cond);
2216 p_aout->sys = p_sys;
2217 p_aout->start = Start;
2218 p_aout->stop = Stop;
2219 p_aout->play = Play;
2220 p_aout->pause = Pause;
2221 p_aout->flush = Flush;
2222 p_aout->time_get = TimeGet;
2223 p_aout->device_select = DeviceSelect;
2225 for( unsigned int i = 0; at_devs[i].id; ++i )
2226 aout_HotplugReport(p_aout, at_devs[i].id, at_devs[i].name);
2228 p_aout->volume_set = VolumeSet;
2229 p_aout->mute_set = MuteSet;
2230 p_sys->volume = 1.0f;
2231 p_sys->mute = false;
2233 return VLC_SUCCESS;
2236 static void
2237 Close( vlc_object_t *obj )
2239 audio_output_t *p_aout = (audio_output_t *) obj;
2240 aout_sys_t *p_sys = p_aout->sys;
2242 free( p_sys );