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 *****************************************************************************/
32 #include <sys/queue.h>
34 #include <vlc_atomic.h>
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_threads.h>
40 /* Maximum VLC buffers queued by the internal queue in microseconds. This delay
41 * doesn't include audiotrack delay */
42 #define MAX_QUEUE_US INT64_C(1000000) // 1000ms
44 static int Open( vlc_object_t * );
45 static void Close( vlc_object_t * );
48 typedef TAILQ_HEAD(, thread_cmd) THREAD_CMD_QUEUE;
55 /* Owned by JNIThread */
56 jobject p_audiotrack; /* AudioTrack ref */
57 jobject p_audioTimestamp; /* AudioTimestamp ref */
58 jbyteArray p_bytearray; /* ByteArray ref (for Write) */
59 size_t i_bytearray_size; /* size of the ByteArray */
60 jobject p_bytebuffer; /* ByteBuffer ref (for WriteV21) */
61 audio_sample_format_t fmt; /* fmt setup by Start */
62 uint32_t i_pos_initial; /* initial position set by getPlaybackHeadPosition */
63 uint32_t i_samples_written; /* number of samples written since last flush */
64 uint32_t i_bytes_per_frame; /* byte per frame */
65 uint32_t i_max_audiotrack_samples;
66 mtime_t i_play_time; /* time when play was called */
67 bool b_audiotrack_exception; /* true if audiotrack throwed an exception */
68 int i_audiotrack_stuck_count;
69 uint8_t i_chans_to_reorder; /* do we need channel reordering */
70 uint8_t p_chan_table[AOUT_CHAN_MAX];
72 /* JNIThread control */
77 /* Shared between two threads, must be locked */
78 bool b_thread_run; /* is thread alive */
79 THREAD_CMD_QUEUE thread_cmd_queue; /* thread cmd queue */
80 uint32_t i_samples_queued; /* number of samples queued */
83 /* Soft volume helper */
84 #include "audio_output/volume.h"
86 //#define AUDIOTRACK_USE_FLOAT
87 // TODO: activate getTimestamp for new android versions
88 //#define AUDIOTRACK_USE_TIMESTAMP
91 set_shortname( "AudioTrack" )
92 set_description( N_( "Android AudioTrack audio output" ) )
93 set_capability( "audio output", 180 )
94 set_category( CAT_AUDIO )
95 set_subcategory( SUBCAT_AUDIO_AOUT )
97 add_shortcut( "audiotrack" )
98 set_callbacks( Open, Close )
103 TAILQ_ENTRY(thread_cmd) next;
115 audio_sample_format_t *p_fmt;
131 audio_sample_format_t *p_fmt;
138 void ( *pf_destroy )( struct thread_cmd * );
141 #define THREAD_NAME "android_audiotrack"
143 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
144 extern void jni_detach_thread();
145 extern int jni_get_env(JNIEnv **env);
160 jmethodID getPlaybackHeadPosition;
161 jmethodID getTimestamp;
162 jmethodID getMinBufferSize;
163 jint STATE_INITIALIZED;
166 jint ERROR_BAD_VALUE;
167 jint ERROR_INVALID_OPERATION;
168 jint WRITE_NON_BLOCKING;
171 jint ENCODING_PCM_8BIT;
172 jint ENCODING_PCM_16BIT;
173 jint ENCODING_PCM_FLOAT;
174 bool has_ENCODING_PCM_FLOAT;
175 jint CHANNEL_OUT_MONO;
176 jint CHANNEL_OUT_STEREO;
177 jint CHANNEL_OUT_BACK_LEFT;
178 jint CHANNEL_OUT_BACK_RIGHT;
179 jint CHANNEL_OUT_FRONT_CENTER;
180 jint CHANNEL_OUT_LOW_FREQUENCY;
181 jint CHANNEL_OUT_BACK_CENTER;
182 jint CHANNEL_OUT_5POINT1;
183 jint CHANNEL_OUT_SIDE_LEFT;
184 jint CHANNEL_OUT_SIDE_RIGHT;
185 bool has_CHANNEL_OUT_SIDE;
188 jint ERROR_DEAD_OBJECT;
189 bool has_ERROR_DEAD_OBJECT;
195 jfieldID framePosition;
200 /* init all jni fields.
201 * Done only one time during the first initialisation */
203 InitJNIFields( audio_output_t *p_aout )
205 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
206 static int i_init_state = -1;
207 bool ret, b_attached = false;
212 vlc_mutex_lock( &lock );
214 if( i_init_state != -1 )
217 if( jni_get_env(&env) < 0 )
219 jni_attach_thread( &env, THREAD_NAME );
228 #define CHECK_EXCEPTION( what, critical ) do { \
229 if( (*env)->ExceptionOccurred( env ) ) \
231 msg_Err( p_aout, "%s failed", what ); \
232 (*env)->ExceptionClear( env ); \
240 #define GET_CLASS( str, critical ) do { \
241 clazz = (*env)->FindClass( env, (str) ); \
242 CHECK_EXCEPTION( str, critical ); \
244 #define GET_ID( get, id, str, args, critical ) do { \
245 jfields.id = (*env)->get( env, clazz, (str), (args) ); \
246 CHECK_EXCEPTION( #get, critical ); \
248 #define GET_CONST_INT( id, str, critical ) do { \
250 field = (*env)->GetStaticFieldID( env, clazz, (str), "I" ); \
251 CHECK_EXCEPTION( #id, critical ); \
254 jfields.id = (*env)->GetStaticIntField( env, clazz, field ); \
255 CHECK_EXCEPTION( #id, critical ); \
259 /* AudioTrack class init */
260 GET_CLASS( "android/media/AudioTrack", true );
261 jfields.AudioTrack.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
262 CHECK_EXCEPTION( "NewGlobalRef", true );
264 GET_ID( GetMethodID, AudioTrack.ctor, "<init>", "(IIIIII)V", true );
265 GET_ID( GetMethodID, AudioTrack.release, "release", "()V", true );
266 GET_ID( GetMethodID, AudioTrack.getState, "getState", "()I", true );
267 GET_ID( GetMethodID, AudioTrack.play, "play", "()V", true );
268 GET_ID( GetMethodID, AudioTrack.stop, "stop", "()V", true );
269 GET_ID( GetMethodID, AudioTrack.flush, "flush", "()V", true );
270 GET_ID( GetMethodID, AudioTrack.pause, "pause", "()V", true );
272 GET_ID( GetMethodID, AudioTrack.writeV21, "write", "(Ljava/nio/ByteBuffer;II)I", false );
273 if( jfields.AudioTrack.writeV21 )
275 jfields.AudioTrack.write = NULL;
276 GET_CONST_INT( AudioTrack.WRITE_NON_BLOCKING, "WRITE_NON_BLOCKING", true );
278 GET_ID( GetMethodID, AudioTrack.write, "write", "([BII)I", true );
280 GET_ID( GetMethodID, AudioTrack.getTimestamp,
281 "getTimestamp", "(Landroid/media/AudioTimestamp;)Z", false );
282 GET_ID( GetMethodID, AudioTrack.getPlaybackHeadPosition,
283 "getPlaybackHeadPosition", "()I", true );
285 GET_ID( GetStaticMethodID, AudioTrack.getMinBufferSize, "getMinBufferSize",
287 GET_CONST_INT( AudioTrack.STATE_INITIALIZED, "STATE_INITIALIZED", true );
288 GET_CONST_INT( AudioTrack.MODE_STREAM, "MODE_STREAM", true );
289 GET_CONST_INT( AudioTrack.ERROR, "ERROR", true );
290 GET_CONST_INT( AudioTrack.ERROR_BAD_VALUE , "ERROR_BAD_VALUE", true );
291 GET_CONST_INT( AudioTrack.ERROR_INVALID_OPERATION,
292 "ERROR_INVALID_OPERATION", true );
294 /* AudioTimestamp class init (if any) */
295 if( jfields.AudioTrack.getTimestamp )
297 GET_CLASS( "android/media/AudioTimestamp", true );
298 jfields.AudioTimestamp.clazz = (jclass) (*env)->NewGlobalRef( env,
300 CHECK_EXCEPTION( "NewGlobalRef", true );
302 GET_ID( GetMethodID, AudioTimestamp.ctor, "<init>", "()V", true );
303 GET_ID( GetFieldID, AudioTimestamp.framePosition,
304 "framePosition", "J", true );
305 GET_ID( GetFieldID, AudioTimestamp.nanoTime,
306 "nanoTime", "J", true );
309 jfields.AudioTimestamp.clazz = NULL;
310 jfields.AudioTimestamp.ctor = NULL;
311 jfields.AudioTimestamp.framePosition = NULL;
312 jfields.AudioTimestamp.nanoTime = NULL;
315 /* AudioFormat class init */
316 GET_CLASS( "android/media/AudioFormat", true );
317 GET_CONST_INT( AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT", true );
318 GET_CONST_INT( AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT", true );
319 #ifdef AUDIOTRACK_USE_FLOAT
320 GET_CONST_INT( AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT",
322 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = field != NULL;
324 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = false;
326 GET_CONST_INT( AudioFormat.CHANNEL_OUT_MONO, "CHANNEL_OUT_MONO", true );
327 GET_CONST_INT( AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO", true );
328 GET_CONST_INT( AudioFormat.CHANNEL_OUT_5POINT1, "CHANNEL_OUT_5POINT1", true );
329 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_LEFT, "CHANNEL_OUT_BACK_LEFT", true );
330 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_RIGHT, "CHANNEL_OUT_BACK_RIGHT", true );
331 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_CENTER, "CHANNEL_OUT_FRONT_CENTER", true );
332 GET_CONST_INT( AudioFormat.CHANNEL_OUT_LOW_FREQUENCY, "CHANNEL_OUT_LOW_FREQUENCY", true );
333 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_CENTER, "CHANNEL_OUT_BACK_CENTER", true );
334 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_LEFT, "CHANNEL_OUT_SIDE_LEFT", false );
337 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_RIGHT, "CHANNEL_OUT_SIDE_RIGHT", true );
338 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = true;
340 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = false;
342 /* AudioManager class init */
343 GET_CLASS( "android/media/AudioManager", true );
344 GET_CONST_INT( AudioManager.ERROR_DEAD_OBJECT, "ERROR_DEAD_OBJECT", false );
345 jfields.AudioManager.has_ERROR_DEAD_OBJECT = field != NULL;
346 GET_CONST_INT( AudioManager.STREAM_MUSIC, "STREAM_MUSIC", true );
348 #undef CHECK_EXCEPTION
355 ret = i_init_state == 1;
357 msg_Err( p_aout, "AudioTrack jni init failed" );
360 vlc_mutex_unlock( &lock );
365 check_exception( JNIEnv *env, audio_output_t *p_aout,
368 if( (*env)->ExceptionOccurred( env ) )
370 aout_sys_t *p_sys = p_aout->sys;
372 p_sys->b_audiotrack_exception = true;
373 (*env)->ExceptionClear( env );
374 msg_Err( p_aout, "AudioTrack.%s triggered an exception !", method );
379 #define CHECK_AT_EXCEPTION( method ) check_exception( env, p_aout, method )
381 #define JNI_CALL( what, obj, method, ... ) (*env)->what( env, obj, method, ##__VA_ARGS__ )
383 #define JNI_CALL_INT( obj, method, ... ) JNI_CALL( CallIntMethod, obj, method, ##__VA_ARGS__ )
384 #define JNI_CALL_BOOL( obj, method, ... ) JNI_CALL( CallBooleanMethod, obj, method, ##__VA_ARGS__ )
385 #define JNI_CALL_VOID( obj, method, ... ) JNI_CALL( CallVoidMethod, obj, method, ##__VA_ARGS__ )
386 #define JNI_CALL_STATIC_INT( clazz, method, ... ) JNI_CALL( CallStaticIntMethod, clazz, method, ##__VA_ARGS__ )
388 #define JNI_AT_NEW( ... ) JNI_CALL( NewObject, jfields.AudioTrack.clazz, jfields.AudioTrack.ctor, ##__VA_ARGS__ )
389 #define JNI_AT_CALL_INT( method, ... ) JNI_CALL_INT( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
390 #define JNI_AT_CALL_BOOL( method, ... ) JNI_CALL_BOOL( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
391 #define JNI_AT_CALL_VOID( method, ... ) JNI_CALL_VOID( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
392 #define JNI_AT_CALL_STATIC_INT( method, ... ) JNI_CALL( CallStaticIntMethod, jfields.AudioTrack.clazz, jfields.AudioTrack.method, ##__VA_ARGS__ )
394 #define JNI_AUDIOTIMESTAMP_GET_LONG( field ) JNI_CALL( GetLongField, p_sys->p_audioTimestamp, jfields.AudioTimestamp.field )
396 static inline mtime_t
397 frames_to_us( aout_sys_t *p_sys, uint32_t i_nb_frames )
399 return i_nb_frames * CLOCK_FREQ / p_sys->fmt.i_rate;
401 #define FRAMES_TO_US(x) frames_to_us( p_sys, (x) )
403 static struct thread_cmd *
404 ThreadCmd_New( int id )
406 struct thread_cmd *p_cmd = calloc( 1, sizeof(struct thread_cmd) );
415 ThreadCmd_InsertHead( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
417 TAILQ_INSERT_HEAD( &p_sys->thread_cmd_queue, p_cmd, next);
418 vlc_cond_signal( &p_sys->cond );
422 ThreadCmd_InsertTail( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
424 TAILQ_INSERT_TAIL( &p_sys->thread_cmd_queue, p_cmd, next);
425 vlc_cond_signal( &p_sys->cond );
429 ThreadCmd_Wait( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
431 while( p_cmd->id != CMD_DONE )
432 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
434 return p_cmd->id == CMD_DONE;
438 ThreadCmd_FlushQueue( aout_sys_t *p_sys )
440 struct thread_cmd *p_cmd, *p_cmd_next;
442 for ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue );
443 p_cmd != NULL; p_cmd = p_cmd_next )
445 p_cmd_next = TAILQ_NEXT( p_cmd, next );
446 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
447 if( p_cmd->pf_destroy )
448 p_cmd->pf_destroy( p_cmd );
453 JNIThread_InitDelay( JNIEnv *env, audio_output_t *p_aout )
455 aout_sys_t *p_sys = p_aout->sys;
457 if( p_sys->p_audiotrack )
458 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
460 p_sys->i_pos_initial = 0;
462 /* HACK: On some broken devices, head position is still moving after a
463 * flush or a stop. So, wait for the head position to be stabilized. */
464 if( unlikely( p_sys->i_pos_initial != 0 ) )
468 i_last_pos = p_sys->i_pos_initial;
470 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
471 } while( p_sys->i_pos_initial != i_last_pos );
473 p_sys->i_samples_written = 0;
474 p_sys->i_samples_queued = 0;
478 JNIThread_GetAudioTrackPos( JNIEnv *env, audio_output_t *p_aout )
480 aout_sys_t *p_sys = p_aout->sys;
483 * getPlaybackHeadPosition: Returns the playback head position expressed in
484 * frames. Though the "int" type is signed 32-bits, the value should be
485 * reinterpreted as if it is unsigned 32-bits. That is, the next position
486 * after 0x7FFFFFFF is (int) 0x80000000. This is a continuously advancing
487 * counter. It will wrap (overflow) periodically, for example approximately
488 * once every 27:03:11 hours:minutes:seconds at 44.1 kHz. It is reset to
489 * zero by flush(), reload(), and stop().
492 return JNI_AT_CALL_INT( getPlaybackHeadPosition ) - p_sys->i_pos_initial;
496 JNIThread_TimeGet( JNIEnv *env, audio_output_t *p_aout, mtime_t *p_delay )
498 aout_sys_t *p_sys = p_aout->sys;
500 uint32_t i_audiotrack_delay = 0;
502 if( p_sys->i_samples_queued == 0 )
504 if( p_sys->p_audioTimestamp )
506 mtime_t i_current_time = mdate();
508 * getTimestamp: Poll for a timestamp on demand.
510 * If you need to track timestamps during initial warmup or after a
511 * routing or mode change, you should request a new timestamp once per
512 * second until the reported timestamps show that the audio clock is
513 * stable. Thereafter, query for a new timestamp approximately once
514 * every 10 seconds to once per minute. Calling this method more often
515 * is inefficient. It is also counter-productive to call this method
516 * more often than recommended, because the short-term differences
517 * between successive timestamp reports are not meaningful. If you need
518 * a high-resolution mapping between frame position and presentation
519 * time, consider implementing that at application level, based on
520 * low-resolution timestamps.
523 if( JNI_AT_CALL_BOOL( getTimestamp, p_sys->p_audioTimestamp ) )
525 jlong i_frame_time = JNI_AUDIOTIMESTAMP_GET_LONG( nanoTime ) / 1000;
526 /* frame time should be after last play time
527 * frame time shouldn't be in the future
528 * frame time should be less than 10 seconds old */
529 if( i_frame_time > p_sys->i_play_time
530 && i_current_time > i_frame_time
531 && ( i_current_time - i_frame_time ) <= INT64_C(10000000) )
533 jlong i_time_diff = i_current_time - i_frame_time;
534 jlong i_frames_diff = i_time_diff * p_sys->fmt.i_rate
536 i_frame_pos = JNI_AUDIOTIMESTAMP_GET_LONG( framePosition )
538 if( p_sys->i_samples_written > i_frame_pos )
539 i_audiotrack_delay = p_sys->i_samples_written - i_frame_pos;
543 if( i_audiotrack_delay == 0 )
545 uint32_t i_audiotrack_pos = JNIThread_GetAudioTrackPos( env, p_aout );
547 if( p_sys->i_samples_written > i_audiotrack_pos )
548 i_audiotrack_delay = p_sys->i_samples_written - i_audiotrack_pos;
551 if( i_audiotrack_delay > 0 )
553 *p_delay = FRAMES_TO_US( p_sys->i_samples_queued + i_audiotrack_delay );
560 AudioTrack_GetChanOrder( int i_mask, uint32_t p_chans_out[] )
562 #define HAS_CHAN( x ) ( ( i_mask & (jfields.AudioFormat.x) ) == (jfields.AudioFormat.x) )
563 const int i_sides = jfields.AudioFormat.CHANNEL_OUT_SIDE_LEFT |
564 jfields.AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
565 const int i_backs = jfields.AudioFormat.CHANNEL_OUT_BACK_LEFT |
566 jfields.AudioFormat.CHANNEL_OUT_BACK_RIGHT;
568 /* verify has FL/FR */
569 if ( !HAS_CHAN( CHANNEL_OUT_STEREO ) )
572 /* verify uses SIDE as a pair (ok if not using SIDE at all) */
573 bool b_has_sides = false;
574 if( jfields.AudioFormat.has_CHANNEL_OUT_SIDE && ( i_mask & i_sides ) != 0 )
576 if( ( i_mask & i_sides ) != i_sides )
580 /* verify uses BACK as a pair (ok if not using BACK at all) */
581 bool b_has_backs = false;
582 if( ( i_mask & i_backs ) != 0 )
584 if( ( i_mask & i_backs ) != i_backs )
589 const bool b_has_FC = HAS_CHAN( CHANNEL_OUT_FRONT_CENTER );
590 const bool b_has_LFE = HAS_CHAN( CHANNEL_OUT_LOW_FREQUENCY );
591 const bool b_has_BC = HAS_CHAN( CHANNEL_OUT_BACK_CENTER );
593 /* compute at what index each channel is: samples will be in the following
594 * order: FL FR FC LFE BL BR BC SL SR
595 * when a channel is not present, its index is set to the same as the index
596 * of the preceding channel. */
598 const int i_FC = b_has_FC ? 2 : 1;
599 const int i_LFE = b_has_LFE ? i_FC + 1 : i_FC;
600 const int i_BL = b_has_backs ? i_LFE + 1 : i_LFE;
601 const int i_BR = b_has_backs ? i_BL + 1 : i_BL;
602 const int i_BC = b_has_BC ? i_BR + 1 : i_BR;
603 const int i_SL = b_has_sides ? i_BC + 1 : i_BC;
604 const int i_SR = b_has_sides ? i_SL + 1 : i_SL;
606 p_chans_out[0] = AOUT_CHAN_LEFT;
607 p_chans_out[1] = AOUT_CHAN_RIGHT;
609 p_chans_out[i_FC] = AOUT_CHAN_CENTER;
611 p_chans_out[i_LFE] = AOUT_CHAN_LFE;
614 p_chans_out[i_BL] = AOUT_CHAN_REARLEFT;
615 p_chans_out[i_BR] = AOUT_CHAN_REARRIGHT;
618 p_chans_out[i_BC] = AOUT_CHAN_REARCENTER;
621 p_chans_out[i_SL] = AOUT_CHAN_MIDDLELEFT;
622 p_chans_out[i_SR] = AOUT_CHAN_MIDDLERIGHT;
631 * Configure and create an Android AudioTrack.
632 * returns -1 on critical error, 0 on success, 1 on configuration error
635 JNIThread_Configure( JNIEnv *env, audio_output_t *p_aout )
637 struct aout_sys_t *p_sys = p_aout->sys;
638 int i_size, i_min_buffer_size, i_channel_config, i_format,
639 i_format_size, i_nb_channels;
640 uint8_t i_chans_to_reorder = 0;
641 uint8_t p_chan_table[AOUT_CHAN_MAX];
642 jobject p_audiotrack;
643 audio_sample_format_t fmt = p_sys->fmt;
645 /* 4000 <= frequency <= 48000 */
646 fmt.i_rate = VLC_CLIP( fmt.i_rate, 4000, 48000 );
648 /* We can only accept U8, S16N, FL32 */
649 switch( fmt.i_format )
656 if( !jfields.AudioFormat.has_ENCODING_PCM_FLOAT )
657 fmt.i_format = VLC_CODEC_S16N;
660 fmt.i_format = VLC_CODEC_S16N;
663 switch( fmt.i_format )
666 i_format = jfields.AudioFormat.ENCODING_PCM_8BIT;
670 i_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
674 i_format = jfields.AudioFormat.ENCODING_PCM_FLOAT;
678 vlc_assert_unreachable();
681 i_nb_channels = aout_FormatNbChannels( &fmt );
683 /* Android AudioTrack supports only mono, stereo, 5.1 and 7.1.
684 * Android will downmix to stereo if audio output doesn't handle 5.1 or 7.1
686 if( i_nb_channels > 5 )
688 uint32_t p_chans_out[AOUT_CHAN_MAX];
690 if( i_nb_channels > 7 && jfields.AudioFormat.has_CHANNEL_OUT_SIDE )
692 fmt.i_physical_channels = AOUT_CHANS_7_1;
693 /* bitmask of CHANNEL_OUT_7POINT1 doesn't correspond to 5POINT1 and
695 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1 |
696 jfields.AudioFormat.CHANNEL_OUT_SIDE_LEFT |
697 jfields.AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
700 fmt.i_physical_channels = AOUT_CHANS_5_1;
701 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1;
703 if( AudioTrack_GetChanOrder( i_channel_config, p_chans_out ) )
705 aout_CheckChannelReorder( NULL, p_chans_out,
706 fmt.i_physical_channels,
712 if( i_nb_channels == 1 )
714 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_MONO;
717 fmt.i_physical_channels = AOUT_CHANS_STEREO;
718 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_STEREO;
721 i_nb_channels = aout_FormatNbChannels( &fmt );
723 i_min_buffer_size = JNI_AT_CALL_STATIC_INT( getMinBufferSize, fmt.i_rate,
724 i_channel_config, i_format );
725 if( i_min_buffer_size <= 0 )
727 msg_Warn( p_aout, "getMinBufferSize returned an invalid size" ) ;
730 i_size = i_min_buffer_size * 4;
732 /* create AudioTrack object */
733 p_audiotrack = JNI_AT_NEW( jfields.AudioManager.STREAM_MUSIC, fmt.i_rate,
734 i_channel_config, i_format, i_size,
735 jfields.AudioTrack.MODE_STREAM );
736 if( CHECK_AT_EXCEPTION( "AudioTrack<init>" ) || !p_audiotrack )
738 p_sys->p_audiotrack = (*env)->NewGlobalRef( env, p_audiotrack );
739 (*env)->DeleteLocalRef( env, p_audiotrack );
740 if( !p_sys->p_audiotrack )
743 p_sys->i_chans_to_reorder = i_chans_to_reorder;
744 if( i_chans_to_reorder )
745 memcpy( p_sys->p_chan_table, p_chan_table, sizeof(p_sys->p_chan_table) );
746 p_sys->i_bytes_per_frame = i_nb_channels * i_format_size;
747 p_sys->i_max_audiotrack_samples = i_size / p_sys->i_bytes_per_frame;
754 JNIThread_Start( JNIEnv *env, audio_output_t *p_aout )
756 aout_sys_t *p_sys = p_aout->sys;
759 aout_FormatPrint( p_aout, "VLC is looking for:", &p_sys->fmt );
760 p_sys->fmt.i_original_channels = p_sys->fmt.i_physical_channels;
762 i_ret = JNIThread_Configure( env, p_aout );
765 /* configuration error, try to fallback to stereo */
766 if( ( p_sys->fmt.i_format != VLC_CODEC_U8 &&
767 p_sys->fmt.i_format != VLC_CODEC_S16N ) ||
768 aout_FormatNbChannels( &p_sys->fmt ) > 2 )
771 "AudioTrack configuration failed, try again in stereo" );
772 p_sys->fmt.i_format = VLC_CODEC_S16N;
773 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
775 i_ret = JNIThread_Configure( env, p_aout );
781 aout_FormatPrint( p_aout, "VLC will output:", &p_sys->fmt );
783 if( JNI_AT_CALL_INT( getState ) != jfields.AudioTrack.STATE_INITIALIZED )
785 msg_Err( p_aout, "AudioTrack init failed" );
789 #ifdef AUDIOTRACK_USE_TIMESTAMP
790 if( jfields.AudioTimestamp.clazz )
792 /* create AudioTimestamp object */
793 jobject p_audioTimestamp = JNI_CALL( NewObject,
794 jfields.AudioTimestamp.clazz,
795 jfields.AudioTimestamp.ctor );
796 if( !p_audioTimestamp )
798 p_sys->p_audioTimestamp = (*env)->NewGlobalRef( env, p_audioTimestamp );
799 (*env)->DeleteLocalRef( env, p_audioTimestamp );
800 if( !p_sys->p_audioTimestamp )
805 JNI_AT_CALL_VOID( play );
806 CHECK_AT_EXCEPTION( "play" );
807 p_sys->i_play_time = mdate();
811 if( p_sys->p_audiotrack )
813 JNI_AT_CALL_VOID( release );
814 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
815 p_sys->p_audiotrack = NULL;
821 JNIThread_Stop( JNIEnv *env, audio_output_t *p_aout )
823 aout_sys_t *p_sys = p_aout->sys;
825 if( !p_sys->b_audiotrack_exception )
827 JNI_AT_CALL_VOID( stop );
828 if( !CHECK_AT_EXCEPTION( "stop" ) )
829 JNI_AT_CALL_VOID( release );
831 p_sys->b_audiotrack_exception = false;
832 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
833 p_sys->p_audiotrack = NULL;
835 if( p_sys->p_audioTimestamp )
837 (*env)->DeleteGlobalRef( env, p_sys->p_audioTimestamp );
838 p_sys->p_audioTimestamp = NULL;
843 * Non blocking write function.
844 * Do a calculation between current position and audiotrack position and assure
845 * that we won't wait in AudioTrack.write() method
848 JNIThread_Write( JNIEnv *env, audio_output_t *p_aout, block_t *p_buffer )
850 aout_sys_t *p_sys = p_aout->sys;
851 uint8_t *p_data = p_buffer->p_buffer;
854 uint32_t i_audiotrack_pos;
855 uint32_t i_samples_pending;
857 i_audiotrack_pos = JNIThread_GetAudioTrackPos( env, p_aout );
858 if( i_audiotrack_pos > p_sys->i_samples_written )
860 msg_Warn( p_aout, "audiotrack position is ahead. Should NOT happen" );
861 JNIThread_InitDelay( env, p_aout );
864 i_samples_pending = p_sys->i_samples_written - i_audiotrack_pos;
866 /* check if audiotrack buffer is not full before writing on it. */
867 if( i_samples_pending >= p_sys->i_max_audiotrack_samples )
870 /* HACK: AudioFlinger can drop frames without notifying us and there is
871 * no way to know it. It it happens, i_audiotrack_pos won't move and
872 * the current code will be stuck because it'll assume that audiotrack
873 * internal buffer is full when it's not. It can happen only after
874 * Android 4.4.2 if we send frames too quickly. This HACK is just an
875 * other precaution since it shouldn't happen anymore thanks to the
876 * HACK in JNIThread_Play */
878 p_sys->i_audiotrack_stuck_count++;
879 if( p_sys->i_audiotrack_stuck_count > 100 )
881 msg_Warn( p_aout, "AudioFlinger underrun, force write" );
882 i_samples_pending = 0;
883 p_sys->i_audiotrack_stuck_count = 0;
886 p_sys->i_audiotrack_stuck_count = 0;
887 i_samples = __MIN( p_sys->i_max_audiotrack_samples - i_samples_pending,
888 p_buffer->i_nb_samples );
890 i_data = i_samples * p_sys->i_bytes_per_frame;
892 /* check if we need to realloc a ByteArray */
893 if( i_data > p_sys->i_bytearray_size )
895 jbyteArray p_bytearray;
897 if( p_sys->p_bytearray )
899 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
900 p_sys->p_bytearray = NULL;
903 p_bytearray = (*env)->NewByteArray( env, i_data );
906 p_sys->p_bytearray = (*env)->NewGlobalRef( env, p_bytearray );
907 (*env)->DeleteLocalRef( env, p_bytearray );
909 p_sys->i_bytearray_size = i_data;
911 if( !p_sys->p_bytearray )
912 return jfields.AudioTrack.ERROR_BAD_VALUE;
914 /* copy p_buffer in to ByteArray */
915 (*env)->SetByteArrayRegion( env, p_sys->p_bytearray, 0, i_data,
918 return JNI_AT_CALL_INT( write, p_sys->p_bytearray, 0, i_data );
922 * Non blocking write function for Lollipop and after.
923 * It calls a new write method with WRITE_NON_BLOCKING flags.
926 JNIThread_WriteV21( JNIEnv *env, audio_output_t *p_aout, block_t *p_buffer )
928 aout_sys_t *p_sys = p_aout->sys;
931 if( !p_sys->p_bytebuffer )
933 jobject p_bytebuffer;
935 p_bytebuffer = (*env)->NewDirectByteBuffer( env, p_buffer->p_buffer,
936 p_buffer->i_buffer );
938 return jfields.AudioTrack.ERROR_BAD_VALUE;
940 p_sys->p_bytebuffer = (*env)->NewGlobalRef( env, p_bytebuffer );
941 (*env)->DeleteLocalRef( env, p_bytebuffer );
943 if( !p_sys->p_bytebuffer || (*env)->ExceptionOccurred( env ) )
945 p_sys->p_bytebuffer = NULL;
946 (*env)->ExceptionClear( env );
947 return jfields.AudioTrack.ERROR_BAD_VALUE;
951 i_ret = JNI_AT_CALL_INT( writeV21, p_sys->p_bytebuffer, p_buffer->i_buffer,
952 jfields.AudioTrack.WRITE_NON_BLOCKING );
955 /* don't delete the bytebuffer if we wrote nothing, keep it for next
957 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
958 p_sys->p_bytebuffer = NULL;
964 JNIThread_Play( JNIEnv *env, audio_output_t *p_aout,
965 block_t **pp_buffer, mtime_t *p_wait )
967 aout_sys_t *p_sys = p_aout->sys;
968 block_t *p_buffer = *pp_buffer;
971 if( jfields.AudioTrack.writeV21 )
972 i_ret = JNIThread_WriteV21( env, p_aout, p_buffer );
974 i_ret = JNIThread_Write( env, p_aout, p_buffer );
977 if( jfields.AudioManager.has_ERROR_DEAD_OBJECT
978 && i_ret == jfields.AudioManager.ERROR_DEAD_OBJECT )
980 msg_Warn( p_aout, "ERROR_DEAD_OBJECT: "
981 "try recreating AudioTrack" );
982 JNIThread_Stop( env, p_aout );
983 i_ret = JNIThread_Start( env, p_aout );
987 if( i_ret == jfields.AudioTrack.ERROR_INVALID_OPERATION )
988 str = "ERROR_INVALID_OPERATION";
989 else if( i_ret == jfields.AudioTrack.ERROR_BAD_VALUE )
990 str = "ERROR_BAD_VALUE";
993 msg_Err( p_aout, "Write failed: %s", str );
995 } else if( i_ret == 0 )
997 /* audiotrack internal buffer is full, wait a little: between 10ms and
998 * 20ms depending on devices or rate */
999 *p_wait = FRAMES_TO_US( p_sys->i_max_audiotrack_samples / 20 );
1002 uint32_t i_samples = i_ret / p_sys->i_bytes_per_frame;
1003 p_sys->i_samples_queued -= i_samples;
1004 p_sys->i_samples_written += i_samples;
1006 p_buffer->p_buffer += i_ret;
1007 p_buffer->i_buffer -= i_ret;
1008 p_buffer->i_nb_samples -= i_samples;
1009 if( p_buffer->i_buffer == 0 )
1012 /* HACK: There is a known issue in audiotrack, "due to an internal
1013 * timeout within the AudioTrackThread". It happens after android
1014 * 4.4.2, it's not a problem for Android 5.0 since we use an other way
1015 * to write samples. A working hack is to wait a little between each
1016 * write. This hack is done only for API 19 (AudioTimestamp was added
1019 if( jfields.AudioTimestamp.clazz && !jfields.AudioTrack.writeV21 )
1020 *p_wait = FRAMES_TO_US( i_samples ) / 2;
1022 return i_ret >= 0 ? VLC_SUCCESS : VLC_EGENERIC;
1026 JNIThread_Pause( JNIEnv *env, audio_output_t *p_aout,
1027 bool b_pause, mtime_t i_date )
1029 VLC_UNUSED( i_date );
1031 aout_sys_t *p_sys = p_aout->sys;
1035 JNI_AT_CALL_VOID( pause );
1036 CHECK_AT_EXCEPTION( "pause" );
1039 JNI_AT_CALL_VOID( play );
1040 CHECK_AT_EXCEPTION( "play" );
1041 p_sys->i_play_time = mdate();
1046 JNIThread_Flush( JNIEnv *env, audio_output_t *p_aout,
1049 aout_sys_t *p_sys = p_aout->sys;
1052 * stop(): Stops playing the audio data. When used on an instance created
1053 * in MODE_STREAM mode, audio will stop playing after the last buffer that
1054 * was written has been played. For an immediate stop, use pause(),
1055 * followed by flush() to discard audio data that hasn't been played back
1058 * flush(): Flushes the audio data currently queued for playback. Any data
1059 * that has not been played back will be discarded. No-op if not stopped
1060 * or paused, or if the track's creation mode is not MODE_STREAM.
1064 JNI_AT_CALL_VOID( stop );
1065 if( CHECK_AT_EXCEPTION( "stop" ) )
1069 JNI_AT_CALL_VOID( pause );
1070 if( CHECK_AT_EXCEPTION( "pause" ) )
1072 JNI_AT_CALL_VOID( flush );
1074 JNI_AT_CALL_VOID( play );
1075 CHECK_AT_EXCEPTION( "play" );
1076 p_sys->i_play_time = mdate();
1078 if( p_sys->p_bytebuffer )
1080 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
1081 p_sys->p_bytebuffer = NULL;
1086 JNIThread( void *data )
1088 audio_output_t *p_aout = data;
1089 aout_sys_t *p_sys = p_aout->sys;
1090 bool b_error = false;
1091 bool b_paused = false;
1092 block_t *p_buffer = NULL;
1093 mtime_t i_play_deadline = 0;
1096 jni_attach_thread( &env, THREAD_NAME );
1098 vlc_mutex_lock( &p_sys->mutex );
1102 while( p_sys->b_thread_run )
1104 struct thread_cmd *p_cmd;
1105 bool b_remove_cmd = true;
1107 /* wait to process a command */
1108 while( ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue ) ) == NULL
1109 && p_sys->b_thread_run )
1110 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1112 if( !p_sys->b_thread_run || p_cmd == NULL )
1115 if( b_paused && p_cmd->id == CMD_PLAY )
1117 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1121 if( p_cmd->id == CMD_PLAY && i_play_deadline > 0 )
1123 if( mdate() > i_play_deadline )
1124 i_play_deadline = 0;
1128 while( p_cmd == TAILQ_FIRST( &p_sys->thread_cmd_queue )
1129 && i_ret != ETIMEDOUT && p_sys->b_thread_run )
1130 i_ret = vlc_cond_timedwait( &p_sys->cond, &p_sys->mutex,
1136 /* process a command */
1140 assert( !p_sys->p_audiotrack );
1142 p_cmd->out.start.i_ret = -1;
1145 p_sys->fmt = *p_cmd->in.start.p_fmt;
1146 p_cmd->out.start.i_ret =
1147 JNIThread_Start( env, p_aout );
1148 JNIThread_InitDelay( env, p_aout );
1149 p_cmd->out.start.p_fmt = &p_sys->fmt;
1153 assert( p_sys->p_audiotrack );
1154 JNIThread_Stop( env, p_aout );
1155 JNIThread_InitDelay( env, p_aout );
1162 mtime_t i_play_wait = 0;
1164 assert( p_sys->p_audiotrack );
1167 if( p_buffer == NULL )
1169 p_buffer = p_cmd->in.play.p_buffer;
1170 if( p_sys->i_chans_to_reorder )
1171 aout_ChannelReorder( p_buffer->p_buffer,
1173 p_sys->i_chans_to_reorder,
1174 p_sys->p_chan_table,
1175 p_sys->fmt.i_format );
1178 b_error = JNIThread_Play( env, p_aout, &p_buffer,
1179 &i_play_wait ) != VLC_SUCCESS;
1180 if( p_buffer != NULL )
1181 b_remove_cmd = false;
1182 if( i_play_wait > 0 )
1183 i_play_deadline = mdate() + i_play_wait;
1187 assert( p_sys->p_audiotrack );
1190 JNIThread_Pause( env, p_aout,
1191 p_cmd->in.pause.b_pause,
1192 p_cmd->in.pause.i_date );
1193 b_paused = p_cmd->in.pause.b_pause;
1196 assert( p_sys->p_audiotrack );
1199 p_cmd->out.time_get.i_ret =
1200 JNIThread_TimeGet( env, p_aout,
1201 &p_cmd->out.time_get.i_delay );
1204 assert( p_sys->p_audiotrack );
1207 JNIThread_Flush( env, p_aout,
1208 p_cmd->in.flush.b_wait );
1209 JNIThread_InitDelay( env, p_aout );
1213 vlc_assert_unreachable();
1215 if( p_sys->b_audiotrack_exception )
1220 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
1221 p_cmd->id = CMD_DONE;
1222 if( p_cmd->pf_destroy )
1223 p_cmd->pf_destroy( p_cmd );
1226 /* signal that command is processed */
1227 vlc_cond_signal( &p_sys->cond );
1232 if( p_sys->p_bytearray )
1233 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
1234 if( p_sys->p_bytebuffer )
1235 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
1236 jni_detach_thread();
1238 vlc_mutex_unlock( &p_sys->mutex );
1243 Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
1245 int i_ret = VLC_EGENERIC;
1246 struct thread_cmd *p_cmd;
1247 aout_sys_t *p_sys = p_aout->sys;
1249 vlc_mutex_lock( &p_sys->mutex );
1251 p_cmd = ThreadCmd_New( CMD_START );
1254 /* ask the thread to process the Start command */
1255 p_cmd->in.start.p_fmt = p_fmt;
1257 ThreadCmd_InsertHead( p_sys, p_cmd );
1258 if( ThreadCmd_Wait( p_sys, p_cmd ) )
1260 i_ret = p_cmd->out.start.i_ret;
1261 if( i_ret == VLC_SUCCESS )
1262 *p_fmt = *p_cmd->out.start.p_fmt;
1267 vlc_mutex_unlock( &p_sys->mutex );
1269 if( i_ret == VLC_SUCCESS )
1270 aout_SoftVolumeStart( p_aout );
1276 Stop( audio_output_t *p_aout )
1278 aout_sys_t *p_sys = p_aout->sys;
1279 struct thread_cmd *p_cmd;
1281 vlc_mutex_lock( &p_sys->mutex );
1283 ThreadCmd_FlushQueue( p_sys );
1285 p_cmd = ThreadCmd_New( CMD_STOP );
1288 /* ask the thread to process the Stop command */
1289 ThreadCmd_InsertHead( p_sys, p_cmd );
1290 ThreadCmd_Wait( p_sys, p_cmd );
1295 vlc_mutex_unlock( &p_sys->mutex );
1299 PlayCmd_Destroy( struct thread_cmd *p_cmd )
1301 block_Release( p_cmd->in.play.p_buffer );
1306 Play( audio_output_t *p_aout, block_t *p_buffer )
1308 aout_sys_t *p_sys = p_aout->sys;
1309 struct thread_cmd *p_cmd;
1311 vlc_mutex_lock( &p_sys->mutex );
1313 while( p_sys->i_samples_queued != 0
1314 && FRAMES_TO_US( p_sys->i_samples_queued +
1315 p_buffer->i_nb_samples ) >= MAX_QUEUE_US )
1316 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1318 p_cmd = ThreadCmd_New( CMD_PLAY );
1321 /* ask the thread to process the Play command */
1322 p_cmd->in.play.p_buffer = p_buffer;
1323 p_cmd->pf_destroy = PlayCmd_Destroy;
1325 ThreadCmd_InsertTail( p_sys, p_cmd );
1327 p_sys->i_samples_queued += p_buffer->i_nb_samples;
1329 block_Release( p_cmd->in.play.p_buffer );
1331 vlc_mutex_unlock( &p_sys->mutex );
1335 Pause( audio_output_t *p_aout, bool b_pause, mtime_t i_date )
1337 aout_sys_t *p_sys = p_aout->sys;
1338 struct thread_cmd *p_cmd;
1340 vlc_mutex_lock( &p_sys->mutex );
1342 p_cmd = ThreadCmd_New( CMD_PAUSE );
1345 /* ask the thread to process the Pause command */
1346 p_cmd->in.pause.b_pause = b_pause;
1347 p_cmd->in.pause.i_date = i_date;
1349 ThreadCmd_InsertHead( p_sys, p_cmd );
1350 ThreadCmd_Wait( p_sys, p_cmd );
1355 vlc_mutex_unlock( &p_sys->mutex );
1359 Flush( audio_output_t *p_aout, bool b_wait )
1361 aout_sys_t *p_sys = p_aout->sys;
1362 struct thread_cmd *p_cmd;
1364 vlc_mutex_lock( &p_sys->mutex );
1366 ThreadCmd_FlushQueue( p_sys );
1368 p_cmd = ThreadCmd_New( CMD_FLUSH );
1371 /* ask the thread to process the Flush command */
1372 p_cmd->in.flush.b_wait = b_wait;
1374 ThreadCmd_InsertHead( p_sys, p_cmd );
1375 ThreadCmd_Wait( p_sys, p_cmd );
1380 vlc_mutex_unlock( &p_sys->mutex );
1384 TimeGet( audio_output_t *p_aout, mtime_t *restrict p_delay )
1386 aout_sys_t *p_sys = p_aout->sys;
1387 struct thread_cmd *p_cmd;
1390 vlc_mutex_lock( &p_sys->mutex );
1392 p_cmd = ThreadCmd_New( CMD_TIME_GET );
1395 ThreadCmd_InsertHead( p_sys, p_cmd );
1396 ThreadCmd_Wait( p_sys, p_cmd );
1398 i_ret = p_cmd->out.time_get.i_ret;
1399 *p_delay = p_cmd->out.time_get.i_delay;
1403 vlc_mutex_unlock( &p_sys->mutex );
1410 Open( vlc_object_t *obj )
1412 audio_output_t *p_aout = (audio_output_t *) obj;
1415 if( !InitJNIFields( p_aout ) )
1416 return VLC_EGENERIC;
1418 p_sys = calloc( 1, sizeof (aout_sys_t) );
1420 if( unlikely( p_sys == NULL ) )
1423 vlc_mutex_init( &p_sys->mutex );
1424 vlc_cond_init( &p_sys->cond );
1425 TAILQ_INIT( &p_sys->thread_cmd_queue );
1427 p_aout->sys = p_sys;
1428 p_aout->start = Start;
1429 p_aout->stop = Stop;
1430 p_aout->play = Play;
1431 p_aout->pause = Pause;
1432 p_aout->flush = Flush;
1433 p_aout->time_get = TimeGet;
1435 aout_SoftVolumeInit( p_aout );
1437 /* create JNIThread */
1438 p_sys->b_thread_run = true;
1439 if( vlc_clone( &p_sys->thread,
1440 JNIThread, p_aout, VLC_THREAD_PRIORITY_AUDIO ) )
1442 msg_Err( p_aout, "JNIThread creation failed" );
1443 p_sys->b_thread_run = false;
1445 return VLC_EGENERIC;
1452 Close( vlc_object_t *obj )
1454 audio_output_t *p_aout = (audio_output_t *) obj;
1455 aout_sys_t *p_sys = p_aout->sys;
1457 /* kill the thread */
1458 vlc_mutex_lock( &p_sys->mutex );
1459 if( p_sys->b_thread_run )
1461 p_sys->b_thread_run = false;
1462 vlc_cond_signal( &p_sys->cond );
1463 vlc_mutex_unlock( &p_sys->mutex );
1464 vlc_join( p_sys->thread, NULL );
1466 vlc_mutex_unlock( &p_sys->mutex );
1468 vlc_mutex_destroy( &p_sys->mutex );
1469 vlc_cond_destroy( &p_sys->cond );