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_FRONT_LEFT;
178 jint CHANNEL_OUT_FRONT_RIGHT;
179 jint CHANNEL_OUT_BACK_LEFT;
180 jint CHANNEL_OUT_BACK_RIGHT;
181 jint CHANNEL_OUT_FRONT_CENTER;
182 jint CHANNEL_OUT_LOW_FREQUENCY;
183 jint CHANNEL_OUT_BACK_CENTER;
184 jint CHANNEL_OUT_5POINT1;
185 jint CHANNEL_OUT_SIDE_LEFT;
186 jint CHANNEL_OUT_SIDE_RIGHT;
187 bool has_CHANNEL_OUT_SIDE;
190 jint ERROR_DEAD_OBJECT;
191 bool has_ERROR_DEAD_OBJECT;
197 jfieldID framePosition;
202 /* init all jni fields.
203 * Done only one time during the first initialisation */
205 InitJNIFields( audio_output_t *p_aout )
207 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
208 static int i_init_state = -1;
209 bool ret, b_attached = false;
214 vlc_mutex_lock( &lock );
216 if( i_init_state != -1 )
219 if( jni_get_env(&env) < 0 )
221 jni_attach_thread( &env, THREAD_NAME );
230 #define CHECK_EXCEPTION( what, critical ) do { \
231 if( (*env)->ExceptionOccurred( env ) ) \
233 msg_Err( p_aout, "%s failed", what ); \
234 (*env)->ExceptionClear( env ); \
242 #define GET_CLASS( str, critical ) do { \
243 clazz = (*env)->FindClass( env, (str) ); \
244 CHECK_EXCEPTION( str, critical ); \
246 #define GET_ID( get, id, str, args, critical ) do { \
247 jfields.id = (*env)->get( env, clazz, (str), (args) ); \
248 CHECK_EXCEPTION( #get, critical ); \
250 #define GET_CONST_INT( id, str, critical ) do { \
252 field = (*env)->GetStaticFieldID( env, clazz, (str), "I" ); \
253 CHECK_EXCEPTION( #id, critical ); \
256 jfields.id = (*env)->GetStaticIntField( env, clazz, field ); \
257 CHECK_EXCEPTION( #id, critical ); \
261 /* AudioTrack class init */
262 GET_CLASS( "android/media/AudioTrack", true );
263 jfields.AudioTrack.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
264 CHECK_EXCEPTION( "NewGlobalRef", true );
266 GET_ID( GetMethodID, AudioTrack.ctor, "<init>", "(IIIIII)V", true );
267 GET_ID( GetMethodID, AudioTrack.release, "release", "()V", true );
268 GET_ID( GetMethodID, AudioTrack.getState, "getState", "()I", true );
269 GET_ID( GetMethodID, AudioTrack.play, "play", "()V", true );
270 GET_ID( GetMethodID, AudioTrack.stop, "stop", "()V", true );
271 GET_ID( GetMethodID, AudioTrack.flush, "flush", "()V", true );
272 GET_ID( GetMethodID, AudioTrack.pause, "pause", "()V", true );
274 GET_ID( GetMethodID, AudioTrack.writeV21, "write", "(Ljava/nio/ByteBuffer;II)I", false );
275 if( jfields.AudioTrack.writeV21 )
277 jfields.AudioTrack.write = NULL;
278 GET_CONST_INT( AudioTrack.WRITE_NON_BLOCKING, "WRITE_NON_BLOCKING", true );
280 GET_ID( GetMethodID, AudioTrack.write, "write", "([BII)I", true );
282 GET_ID( GetMethodID, AudioTrack.getTimestamp,
283 "getTimestamp", "(Landroid/media/AudioTimestamp;)Z", false );
284 GET_ID( GetMethodID, AudioTrack.getPlaybackHeadPosition,
285 "getPlaybackHeadPosition", "()I", true );
287 GET_ID( GetStaticMethodID, AudioTrack.getMinBufferSize, "getMinBufferSize",
289 GET_CONST_INT( AudioTrack.STATE_INITIALIZED, "STATE_INITIALIZED", true );
290 GET_CONST_INT( AudioTrack.MODE_STREAM, "MODE_STREAM", true );
291 GET_CONST_INT( AudioTrack.ERROR, "ERROR", true );
292 GET_CONST_INT( AudioTrack.ERROR_BAD_VALUE , "ERROR_BAD_VALUE", true );
293 GET_CONST_INT( AudioTrack.ERROR_INVALID_OPERATION,
294 "ERROR_INVALID_OPERATION", true );
296 /* AudioTimestamp class init (if any) */
297 if( jfields.AudioTrack.getTimestamp )
299 GET_CLASS( "android/media/AudioTimestamp", true );
300 jfields.AudioTimestamp.clazz = (jclass) (*env)->NewGlobalRef( env,
302 CHECK_EXCEPTION( "NewGlobalRef", true );
304 GET_ID( GetMethodID, AudioTimestamp.ctor, "<init>", "()V", true );
305 GET_ID( GetFieldID, AudioTimestamp.framePosition,
306 "framePosition", "J", true );
307 GET_ID( GetFieldID, AudioTimestamp.nanoTime,
308 "nanoTime", "J", true );
311 jfields.AudioTimestamp.clazz = NULL;
312 jfields.AudioTimestamp.ctor = NULL;
313 jfields.AudioTimestamp.framePosition = NULL;
314 jfields.AudioTimestamp.nanoTime = NULL;
317 /* AudioFormat class init */
318 GET_CLASS( "android/media/AudioFormat", true );
319 GET_CONST_INT( AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT", true );
320 GET_CONST_INT( AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT", true );
321 #ifdef AUDIOTRACK_USE_FLOAT
322 GET_CONST_INT( AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT",
324 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = field != NULL;
326 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = false;
328 GET_CONST_INT( AudioFormat.CHANNEL_OUT_MONO, "CHANNEL_OUT_MONO", true );
329 GET_CONST_INT( AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO", true );
330 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_LEFT, "CHANNEL_OUT_FRONT_LEFT", true );
331 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_RIGHT, "CHANNEL_OUT_FRONT_RIGHT", true );
332 GET_CONST_INT( AudioFormat.CHANNEL_OUT_5POINT1, "CHANNEL_OUT_5POINT1", true );
333 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_LEFT, "CHANNEL_OUT_BACK_LEFT", true );
334 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_RIGHT, "CHANNEL_OUT_BACK_RIGHT", true );
335 GET_CONST_INT( AudioFormat.CHANNEL_OUT_FRONT_CENTER, "CHANNEL_OUT_FRONT_CENTER", true );
336 GET_CONST_INT( AudioFormat.CHANNEL_OUT_LOW_FREQUENCY, "CHANNEL_OUT_LOW_FREQUENCY", true );
337 GET_CONST_INT( AudioFormat.CHANNEL_OUT_BACK_CENTER, "CHANNEL_OUT_BACK_CENTER", true );
338 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_LEFT, "CHANNEL_OUT_SIDE_LEFT", false );
341 GET_CONST_INT( AudioFormat.CHANNEL_OUT_SIDE_RIGHT, "CHANNEL_OUT_SIDE_RIGHT", true );
342 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = true;
344 jfields.AudioFormat.has_CHANNEL_OUT_SIDE = false;
346 /* AudioManager class init */
347 GET_CLASS( "android/media/AudioManager", true );
348 GET_CONST_INT( AudioManager.ERROR_DEAD_OBJECT, "ERROR_DEAD_OBJECT", false );
349 jfields.AudioManager.has_ERROR_DEAD_OBJECT = field != NULL;
350 GET_CONST_INT( AudioManager.STREAM_MUSIC, "STREAM_MUSIC", true );
352 #undef CHECK_EXCEPTION
359 ret = i_init_state == 1;
361 msg_Err( p_aout, "AudioTrack jni init failed" );
364 vlc_mutex_unlock( &lock );
369 check_exception( JNIEnv *env, audio_output_t *p_aout,
372 if( (*env)->ExceptionOccurred( env ) )
374 aout_sys_t *p_sys = p_aout->sys;
376 p_sys->b_audiotrack_exception = true;
377 (*env)->ExceptionClear( env );
378 msg_Err( p_aout, "AudioTrack.%s triggered an exception !", method );
383 #define CHECK_AT_EXCEPTION( method ) check_exception( env, p_aout, method )
385 #define JNI_CALL( what, obj, method, ... ) (*env)->what( env, obj, method, ##__VA_ARGS__ )
387 #define JNI_CALL_INT( obj, method, ... ) JNI_CALL( CallIntMethod, obj, method, ##__VA_ARGS__ )
388 #define JNI_CALL_BOOL( obj, method, ... ) JNI_CALL( CallBooleanMethod, obj, method, ##__VA_ARGS__ )
389 #define JNI_CALL_VOID( obj, method, ... ) JNI_CALL( CallVoidMethod, obj, method, ##__VA_ARGS__ )
390 #define JNI_CALL_STATIC_INT( clazz, method, ... ) JNI_CALL( CallStaticIntMethod, clazz, method, ##__VA_ARGS__ )
392 #define JNI_AT_NEW( ... ) JNI_CALL( NewObject, jfields.AudioTrack.clazz, jfields.AudioTrack.ctor, ##__VA_ARGS__ )
393 #define JNI_AT_CALL_INT( method, ... ) JNI_CALL_INT( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
394 #define JNI_AT_CALL_BOOL( method, ... ) JNI_CALL_BOOL( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
395 #define JNI_AT_CALL_VOID( method, ... ) JNI_CALL_VOID( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
396 #define JNI_AT_CALL_STATIC_INT( method, ... ) JNI_CALL( CallStaticIntMethod, jfields.AudioTrack.clazz, jfields.AudioTrack.method, ##__VA_ARGS__ )
398 #define JNI_AUDIOTIMESTAMP_GET_LONG( field ) JNI_CALL( GetLongField, p_sys->p_audioTimestamp, jfields.AudioTimestamp.field )
400 static inline mtime_t
401 frames_to_us( aout_sys_t *p_sys, uint32_t i_nb_frames )
403 return i_nb_frames * CLOCK_FREQ / p_sys->fmt.i_rate;
405 #define FRAMES_TO_US(x) frames_to_us( p_sys, (x) )
407 static struct thread_cmd *
408 ThreadCmd_New( int id )
410 struct thread_cmd *p_cmd = calloc( 1, sizeof(struct thread_cmd) );
419 ThreadCmd_InsertHead( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
421 TAILQ_INSERT_HEAD( &p_sys->thread_cmd_queue, p_cmd, next);
422 vlc_cond_signal( &p_sys->cond );
426 ThreadCmd_InsertTail( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
428 TAILQ_INSERT_TAIL( &p_sys->thread_cmd_queue, p_cmd, next);
429 vlc_cond_signal( &p_sys->cond );
433 ThreadCmd_Wait( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
435 while( p_cmd->id != CMD_DONE )
436 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
438 return p_cmd->id == CMD_DONE;
442 ThreadCmd_FlushQueue( aout_sys_t *p_sys )
444 struct thread_cmd *p_cmd, *p_cmd_next;
446 for ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue );
447 p_cmd != NULL; p_cmd = p_cmd_next )
449 p_cmd_next = TAILQ_NEXT( p_cmd, next );
450 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
451 if( p_cmd->pf_destroy )
452 p_cmd->pf_destroy( p_cmd );
457 JNIThread_InitDelay( JNIEnv *env, audio_output_t *p_aout )
459 aout_sys_t *p_sys = p_aout->sys;
461 if( p_sys->p_audiotrack )
462 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
464 p_sys->i_pos_initial = 0;
466 /* HACK: On some broken devices, head position is still moving after a
467 * flush or a stop. So, wait for the head position to be stabilized. */
468 if( unlikely( p_sys->i_pos_initial != 0 ) )
472 i_last_pos = p_sys->i_pos_initial;
474 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
475 } while( p_sys->i_pos_initial != i_last_pos );
477 p_sys->i_samples_written = 0;
478 p_sys->i_samples_queued = 0;
482 JNIThread_GetAudioTrackPos( JNIEnv *env, audio_output_t *p_aout )
484 aout_sys_t *p_sys = p_aout->sys;
487 * getPlaybackHeadPosition: Returns the playback head position expressed in
488 * frames. Though the "int" type is signed 32-bits, the value should be
489 * reinterpreted as if it is unsigned 32-bits. That is, the next position
490 * after 0x7FFFFFFF is (int) 0x80000000. This is a continuously advancing
491 * counter. It will wrap (overflow) periodically, for example approximately
492 * once every 27:03:11 hours:minutes:seconds at 44.1 kHz. It is reset to
493 * zero by flush(), reload(), and stop().
496 return JNI_AT_CALL_INT( getPlaybackHeadPosition ) - p_sys->i_pos_initial;
500 JNIThread_TimeGet( JNIEnv *env, audio_output_t *p_aout, mtime_t *p_delay )
502 aout_sys_t *p_sys = p_aout->sys;
504 uint32_t i_audiotrack_delay = 0;
506 if( p_sys->i_samples_queued == 0 )
508 if( p_sys->p_audioTimestamp )
510 mtime_t i_current_time = mdate();
512 * getTimestamp: Poll for a timestamp on demand.
514 * If you need to track timestamps during initial warmup or after a
515 * routing or mode change, you should request a new timestamp once per
516 * second until the reported timestamps show that the audio clock is
517 * stable. Thereafter, query for a new timestamp approximately once
518 * every 10 seconds to once per minute. Calling this method more often
519 * is inefficient. It is also counter-productive to call this method
520 * more often than recommended, because the short-term differences
521 * between successive timestamp reports are not meaningful. If you need
522 * a high-resolution mapping between frame position and presentation
523 * time, consider implementing that at application level, based on
524 * low-resolution timestamps.
527 if( JNI_AT_CALL_BOOL( getTimestamp, p_sys->p_audioTimestamp ) )
529 jlong i_frame_time = JNI_AUDIOTIMESTAMP_GET_LONG( nanoTime ) / 1000;
530 /* frame time should be after last play time
531 * frame time shouldn't be in the future
532 * frame time should be less than 10 seconds old */
533 if( i_frame_time > p_sys->i_play_time
534 && i_current_time > i_frame_time
535 && ( i_current_time - i_frame_time ) <= INT64_C(10000000) )
537 jlong i_time_diff = i_current_time - i_frame_time;
538 jlong i_frames_diff = i_time_diff * p_sys->fmt.i_rate
540 i_frame_pos = JNI_AUDIOTIMESTAMP_GET_LONG( framePosition )
542 if( p_sys->i_samples_written > i_frame_pos )
543 i_audiotrack_delay = p_sys->i_samples_written - i_frame_pos;
547 if( i_audiotrack_delay == 0 )
549 uint32_t i_audiotrack_pos = JNIThread_GetAudioTrackPos( env, p_aout );
551 if( p_sys->i_samples_written > i_audiotrack_pos )
552 i_audiotrack_delay = p_sys->i_samples_written - i_audiotrack_pos;
555 if( i_audiotrack_delay > 0 )
557 *p_delay = FRAMES_TO_US( p_sys->i_samples_queued + i_audiotrack_delay );
564 AudioTrack_GetChanOrder( int i_mask, uint32_t p_chans_out[] )
566 #define HAS_CHAN( x ) ( ( i_mask & (jfields.AudioFormat.x) ) == (jfields.AudioFormat.x) )
567 /* samples will be in the following order: FL FR FC LFE BL BR BC SL SR */
570 if( HAS_CHAN( CHANNEL_OUT_FRONT_LEFT ) )
571 p_chans_out[i++] = AOUT_CHAN_LEFT;
572 if( HAS_CHAN( CHANNEL_OUT_FRONT_RIGHT ) )
573 p_chans_out[i++] = AOUT_CHAN_RIGHT;
575 if( HAS_CHAN( CHANNEL_OUT_FRONT_CENTER ) )
576 p_chans_out[i++] = AOUT_CHAN_CENTER;
578 if( HAS_CHAN( CHANNEL_OUT_LOW_FREQUENCY ) )
579 p_chans_out[i++] = AOUT_CHAN_LFE;
581 if( HAS_CHAN( CHANNEL_OUT_BACK_LEFT ) )
582 p_chans_out[i++] = AOUT_CHAN_REARLEFT;
583 if( HAS_CHAN( CHANNEL_OUT_BACK_RIGHT ) )
584 p_chans_out[i++] = AOUT_CHAN_REARRIGHT;
586 if( HAS_CHAN( CHANNEL_OUT_BACK_CENTER ) )
587 p_chans_out[i++] = AOUT_CHAN_REARCENTER;
589 if( jfields.AudioFormat.has_CHANNEL_OUT_SIDE )
591 if( HAS_CHAN( CHANNEL_OUT_SIDE_LEFT ) )
592 p_chans_out[i++] = AOUT_CHAN_MIDDLELEFT;
593 if( HAS_CHAN( CHANNEL_OUT_SIDE_RIGHT ) )
594 p_chans_out[i++] = AOUT_CHAN_MIDDLERIGHT;
596 assert( i <= AOUT_CHAN_MAX );
601 * Configure and create an Android AudioTrack.
602 * returns -1 on critical error, 0 on success, 1 on configuration error
605 JNIThread_Configure( JNIEnv *env, audio_output_t *p_aout )
607 struct aout_sys_t *p_sys = p_aout->sys;
608 int i_size, i_min_buffer_size, i_channel_config, i_format,
609 i_format_size, i_nb_channels;
610 uint8_t i_chans_to_reorder = 0;
611 uint8_t p_chan_table[AOUT_CHAN_MAX];
612 uint32_t p_chans_out[AOUT_CHAN_MAX];
613 jobject p_audiotrack;
614 audio_sample_format_t fmt = p_sys->fmt;
616 /* 4000 <= frequency <= 48000 */
617 fmt.i_rate = VLC_CLIP( fmt.i_rate, 4000, 48000 );
619 /* We can only accept U8, S16N, FL32 */
620 switch( fmt.i_format )
627 if( !jfields.AudioFormat.has_ENCODING_PCM_FLOAT )
628 fmt.i_format = VLC_CODEC_S16N;
631 fmt.i_format = VLC_CODEC_S16N;
634 switch( fmt.i_format )
637 i_format = jfields.AudioFormat.ENCODING_PCM_8BIT;
641 i_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
645 i_format = jfields.AudioFormat.ENCODING_PCM_FLOAT;
649 vlc_assert_unreachable();
652 i_nb_channels = aout_FormatNbChannels( &fmt );
654 /* Android AudioTrack supports only mono, stereo, 5.1 and 7.1.
655 * Android will downmix to stereo if audio output doesn't handle 5.1 or 7.1
657 if( i_nb_channels > 5 )
659 if( i_nb_channels > 7 && jfields.AudioFormat.has_CHANNEL_OUT_SIDE )
661 fmt.i_physical_channels = AOUT_CHANS_7_1;
662 /* bitmask of CHANNEL_OUT_7POINT1 doesn't correspond to 5POINT1 and
664 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1 |
665 jfields.AudioFormat.CHANNEL_OUT_SIDE_LEFT |
666 jfields.AudioFormat.CHANNEL_OUT_SIDE_RIGHT;
669 fmt.i_physical_channels = AOUT_CHANS_5_1;
670 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_5POINT1;
674 if( i_nb_channels == 1 )
676 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_MONO;
679 fmt.i_physical_channels = AOUT_CHANS_STEREO;
680 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_STEREO;
683 i_nb_channels = aout_FormatNbChannels( &fmt );
685 memset( p_chans_out, 0, sizeof(p_chans_out) );
686 AudioTrack_GetChanOrder( i_channel_config, p_chans_out );
687 i_chans_to_reorder = aout_CheckChannelReorder( NULL, p_chans_out,
688 fmt.i_physical_channels,
691 i_min_buffer_size = JNI_AT_CALL_STATIC_INT( getMinBufferSize, fmt.i_rate,
692 i_channel_config, i_format );
693 if( i_min_buffer_size <= 0 )
695 msg_Warn( p_aout, "getMinBufferSize returned an invalid size" ) ;
698 i_size = i_min_buffer_size * 4;
700 /* create AudioTrack object */
701 p_audiotrack = JNI_AT_NEW( jfields.AudioManager.STREAM_MUSIC, fmt.i_rate,
702 i_channel_config, i_format, i_size,
703 jfields.AudioTrack.MODE_STREAM );
704 if( CHECK_AT_EXCEPTION( "AudioTrack<init>" ) || !p_audiotrack )
706 p_sys->p_audiotrack = (*env)->NewGlobalRef( env, p_audiotrack );
707 (*env)->DeleteLocalRef( env, p_audiotrack );
708 if( !p_sys->p_audiotrack )
711 p_sys->i_chans_to_reorder = i_chans_to_reorder;
712 if( i_chans_to_reorder )
713 memcpy( p_sys->p_chan_table, p_chan_table, sizeof(p_sys->p_chan_table) );
714 p_sys->i_bytes_per_frame = i_nb_channels * i_format_size;
715 p_sys->i_max_audiotrack_samples = i_size / p_sys->i_bytes_per_frame;
722 JNIThread_Start( JNIEnv *env, audio_output_t *p_aout )
724 aout_sys_t *p_sys = p_aout->sys;
727 aout_FormatPrint( p_aout, "VLC is looking for:", &p_sys->fmt );
728 p_sys->fmt.i_original_channels = p_sys->fmt.i_physical_channels;
730 i_ret = JNIThread_Configure( env, p_aout );
733 /* configuration error, try to fallback to stereo */
734 if( ( p_sys->fmt.i_format != VLC_CODEC_U8 &&
735 p_sys->fmt.i_format != VLC_CODEC_S16N ) ||
736 aout_FormatNbChannels( &p_sys->fmt ) > 2 )
739 "AudioTrack configuration failed, try again in stereo" );
740 p_sys->fmt.i_format = VLC_CODEC_S16N;
741 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
743 i_ret = JNIThread_Configure( env, p_aout );
749 aout_FormatPrint( p_aout, "VLC will output:", &p_sys->fmt );
751 if( JNI_AT_CALL_INT( getState ) != jfields.AudioTrack.STATE_INITIALIZED )
753 msg_Err( p_aout, "AudioTrack init failed" );
757 #ifdef AUDIOTRACK_USE_TIMESTAMP
758 if( jfields.AudioTimestamp.clazz )
760 /* create AudioTimestamp object */
761 jobject p_audioTimestamp = JNI_CALL( NewObject,
762 jfields.AudioTimestamp.clazz,
763 jfields.AudioTimestamp.ctor );
764 if( !p_audioTimestamp )
766 p_sys->p_audioTimestamp = (*env)->NewGlobalRef( env, p_audioTimestamp );
767 (*env)->DeleteLocalRef( env, p_audioTimestamp );
768 if( !p_sys->p_audioTimestamp )
773 JNI_AT_CALL_VOID( play );
774 CHECK_AT_EXCEPTION( "play" );
775 p_sys->i_play_time = mdate();
779 if( p_sys->p_audiotrack )
781 JNI_AT_CALL_VOID( release );
782 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
783 p_sys->p_audiotrack = NULL;
789 JNIThread_Stop( JNIEnv *env, audio_output_t *p_aout )
791 aout_sys_t *p_sys = p_aout->sys;
793 if( !p_sys->b_audiotrack_exception )
795 JNI_AT_CALL_VOID( stop );
796 if( !CHECK_AT_EXCEPTION( "stop" ) )
797 JNI_AT_CALL_VOID( release );
799 p_sys->b_audiotrack_exception = false;
800 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
801 p_sys->p_audiotrack = NULL;
803 if( p_sys->p_audioTimestamp )
805 (*env)->DeleteGlobalRef( env, p_sys->p_audioTimestamp );
806 p_sys->p_audioTimestamp = NULL;
811 * Non blocking write function.
812 * Do a calculation between current position and audiotrack position and assure
813 * that we won't wait in AudioTrack.write() method
816 JNIThread_Write( JNIEnv *env, audio_output_t *p_aout, block_t *p_buffer )
818 aout_sys_t *p_sys = p_aout->sys;
819 uint8_t *p_data = p_buffer->p_buffer;
822 uint32_t i_audiotrack_pos;
823 uint32_t i_samples_pending;
825 i_audiotrack_pos = JNIThread_GetAudioTrackPos( env, p_aout );
826 if( i_audiotrack_pos > p_sys->i_samples_written )
828 msg_Warn( p_aout, "audiotrack position is ahead. Should NOT happen" );
829 JNIThread_InitDelay( env, p_aout );
832 i_samples_pending = p_sys->i_samples_written - i_audiotrack_pos;
834 /* check if audiotrack buffer is not full before writing on it. */
835 if( i_samples_pending >= p_sys->i_max_audiotrack_samples )
838 /* HACK: AudioFlinger can drop frames without notifying us and there is
839 * no way to know it. It it happens, i_audiotrack_pos won't move and
840 * the current code will be stuck because it'll assume that audiotrack
841 * internal buffer is full when it's not. It can happen only after
842 * Android 4.4.2 if we send frames too quickly. This HACK is just an
843 * other precaution since it shouldn't happen anymore thanks to the
844 * HACK in JNIThread_Play */
846 p_sys->i_audiotrack_stuck_count++;
847 if( p_sys->i_audiotrack_stuck_count > 100 )
849 msg_Warn( p_aout, "AudioFlinger underrun, force write" );
850 i_samples_pending = 0;
851 p_sys->i_audiotrack_stuck_count = 0;
854 p_sys->i_audiotrack_stuck_count = 0;
855 i_samples = __MIN( p_sys->i_max_audiotrack_samples - i_samples_pending,
856 p_buffer->i_nb_samples );
858 i_data = i_samples * p_sys->i_bytes_per_frame;
860 /* check if we need to realloc a ByteArray */
861 if( i_data > p_sys->i_bytearray_size )
863 jbyteArray p_bytearray;
865 if( p_sys->p_bytearray )
867 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
868 p_sys->p_bytearray = NULL;
871 p_bytearray = (*env)->NewByteArray( env, i_data );
874 p_sys->p_bytearray = (*env)->NewGlobalRef( env, p_bytearray );
875 (*env)->DeleteLocalRef( env, p_bytearray );
877 p_sys->i_bytearray_size = i_data;
879 if( !p_sys->p_bytearray )
880 return jfields.AudioTrack.ERROR_BAD_VALUE;
882 /* copy p_buffer in to ByteArray */
883 (*env)->SetByteArrayRegion( env, p_sys->p_bytearray, 0, i_data,
886 return JNI_AT_CALL_INT( write, p_sys->p_bytearray, 0, i_data );
890 * Non blocking write function for Lollipop and after.
891 * It calls a new write method with WRITE_NON_BLOCKING flags.
894 JNIThread_WriteV21( JNIEnv *env, audio_output_t *p_aout, block_t *p_buffer )
896 aout_sys_t *p_sys = p_aout->sys;
899 if( !p_sys->p_bytebuffer )
901 jobject p_bytebuffer;
903 p_bytebuffer = (*env)->NewDirectByteBuffer( env, p_buffer->p_buffer,
904 p_buffer->i_buffer );
906 return jfields.AudioTrack.ERROR_BAD_VALUE;
908 p_sys->p_bytebuffer = (*env)->NewGlobalRef( env, p_bytebuffer );
909 (*env)->DeleteLocalRef( env, p_bytebuffer );
911 if( !p_sys->p_bytebuffer || (*env)->ExceptionOccurred( env ) )
913 p_sys->p_bytebuffer = NULL;
914 (*env)->ExceptionClear( env );
915 return jfields.AudioTrack.ERROR_BAD_VALUE;
919 i_ret = JNI_AT_CALL_INT( writeV21, p_sys->p_bytebuffer, p_buffer->i_buffer,
920 jfields.AudioTrack.WRITE_NON_BLOCKING );
923 /* don't delete the bytebuffer if we wrote nothing, keep it for next
925 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
926 p_sys->p_bytebuffer = NULL;
932 JNIThread_Play( JNIEnv *env, audio_output_t *p_aout,
933 block_t **pp_buffer, mtime_t *p_wait )
935 aout_sys_t *p_sys = p_aout->sys;
936 block_t *p_buffer = *pp_buffer;
939 if( jfields.AudioTrack.writeV21 )
940 i_ret = JNIThread_WriteV21( env, p_aout, p_buffer );
942 i_ret = JNIThread_Write( env, p_aout, p_buffer );
945 if( jfields.AudioManager.has_ERROR_DEAD_OBJECT
946 && i_ret == jfields.AudioManager.ERROR_DEAD_OBJECT )
948 msg_Warn( p_aout, "ERROR_DEAD_OBJECT: "
949 "try recreating AudioTrack" );
950 JNIThread_Stop( env, p_aout );
951 i_ret = JNIThread_Start( env, p_aout );
955 if( i_ret == jfields.AudioTrack.ERROR_INVALID_OPERATION )
956 str = "ERROR_INVALID_OPERATION";
957 else if( i_ret == jfields.AudioTrack.ERROR_BAD_VALUE )
958 str = "ERROR_BAD_VALUE";
961 msg_Err( p_aout, "Write failed: %s", str );
963 } else if( i_ret == 0 )
965 /* audiotrack internal buffer is full, wait a little: between 10ms and
966 * 20ms depending on devices or rate */
967 *p_wait = FRAMES_TO_US( p_sys->i_max_audiotrack_samples / 20 );
970 uint32_t i_samples = i_ret / p_sys->i_bytes_per_frame;
971 p_sys->i_samples_queued -= i_samples;
972 p_sys->i_samples_written += i_samples;
974 p_buffer->p_buffer += i_ret;
975 p_buffer->i_buffer -= i_ret;
976 p_buffer->i_nb_samples -= i_samples;
977 if( p_buffer->i_buffer == 0 )
980 /* HACK: There is a known issue in audiotrack, "due to an internal
981 * timeout within the AudioTrackThread". It happens after android
982 * 4.4.2, it's not a problem for Android 5.0 since we use an other way
983 * to write samples. A working hack is to wait a little between each
984 * write. This hack is done only for API 19 (AudioTimestamp was added
987 if( jfields.AudioTimestamp.clazz && !jfields.AudioTrack.writeV21 )
988 *p_wait = FRAMES_TO_US( i_samples ) / 2;
990 return i_ret >= 0 ? VLC_SUCCESS : VLC_EGENERIC;
994 JNIThread_Pause( JNIEnv *env, audio_output_t *p_aout,
995 bool b_pause, mtime_t i_date )
997 VLC_UNUSED( i_date );
999 aout_sys_t *p_sys = p_aout->sys;
1003 JNI_AT_CALL_VOID( pause );
1004 CHECK_AT_EXCEPTION( "pause" );
1007 JNI_AT_CALL_VOID( play );
1008 CHECK_AT_EXCEPTION( "play" );
1009 p_sys->i_play_time = mdate();
1014 JNIThread_Flush( JNIEnv *env, audio_output_t *p_aout,
1017 aout_sys_t *p_sys = p_aout->sys;
1020 * stop(): Stops playing the audio data. When used on an instance created
1021 * in MODE_STREAM mode, audio will stop playing after the last buffer that
1022 * was written has been played. For an immediate stop, use pause(),
1023 * followed by flush() to discard audio data that hasn't been played back
1026 * flush(): Flushes the audio data currently queued for playback. Any data
1027 * that has not been played back will be discarded. No-op if not stopped
1028 * or paused, or if the track's creation mode is not MODE_STREAM.
1032 JNI_AT_CALL_VOID( stop );
1033 if( CHECK_AT_EXCEPTION( "stop" ) )
1037 JNI_AT_CALL_VOID( pause );
1038 if( CHECK_AT_EXCEPTION( "pause" ) )
1040 JNI_AT_CALL_VOID( flush );
1042 JNI_AT_CALL_VOID( play );
1043 CHECK_AT_EXCEPTION( "play" );
1044 p_sys->i_play_time = mdate();
1046 if( p_sys->p_bytebuffer )
1048 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
1049 p_sys->p_bytebuffer = NULL;
1054 JNIThread( void *data )
1056 audio_output_t *p_aout = data;
1057 aout_sys_t *p_sys = p_aout->sys;
1058 bool b_error = false;
1059 bool b_paused = false;
1060 block_t *p_buffer = NULL;
1061 mtime_t i_play_deadline = 0;
1064 jni_attach_thread( &env, THREAD_NAME );
1066 vlc_mutex_lock( &p_sys->mutex );
1070 while( p_sys->b_thread_run )
1072 struct thread_cmd *p_cmd;
1073 bool b_remove_cmd = true;
1075 /* wait to process a command */
1076 while( ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue ) ) == NULL
1077 && p_sys->b_thread_run )
1078 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1080 if( !p_sys->b_thread_run || p_cmd == NULL )
1083 if( b_paused && p_cmd->id == CMD_PLAY )
1085 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1089 if( p_cmd->id == CMD_PLAY && i_play_deadline > 0 )
1091 if( mdate() > i_play_deadline )
1092 i_play_deadline = 0;
1096 while( p_cmd == TAILQ_FIRST( &p_sys->thread_cmd_queue )
1097 && i_ret != ETIMEDOUT && p_sys->b_thread_run )
1098 i_ret = vlc_cond_timedwait( &p_sys->cond, &p_sys->mutex,
1104 /* process a command */
1108 assert( !p_sys->p_audiotrack );
1110 p_cmd->out.start.i_ret = -1;
1113 p_sys->fmt = *p_cmd->in.start.p_fmt;
1114 p_cmd->out.start.i_ret =
1115 JNIThread_Start( env, p_aout );
1116 JNIThread_InitDelay( env, p_aout );
1117 p_cmd->out.start.p_fmt = &p_sys->fmt;
1121 assert( p_sys->p_audiotrack );
1122 JNIThread_Stop( env, p_aout );
1123 JNIThread_InitDelay( env, p_aout );
1130 mtime_t i_play_wait = 0;
1132 assert( p_sys->p_audiotrack );
1135 if( p_buffer == NULL )
1137 p_buffer = p_cmd->in.play.p_buffer;
1138 if( p_sys->i_chans_to_reorder )
1139 aout_ChannelReorder( p_buffer->p_buffer,
1141 p_sys->i_chans_to_reorder,
1142 p_sys->p_chan_table,
1143 p_sys->fmt.i_format );
1146 b_error = JNIThread_Play( env, p_aout, &p_buffer,
1147 &i_play_wait ) != VLC_SUCCESS;
1148 if( p_buffer != NULL )
1149 b_remove_cmd = false;
1150 if( i_play_wait > 0 )
1151 i_play_deadline = mdate() + i_play_wait;
1155 assert( p_sys->p_audiotrack );
1158 JNIThread_Pause( env, p_aout,
1159 p_cmd->in.pause.b_pause,
1160 p_cmd->in.pause.i_date );
1161 b_paused = p_cmd->in.pause.b_pause;
1164 assert( p_sys->p_audiotrack );
1167 p_cmd->out.time_get.i_ret =
1168 JNIThread_TimeGet( env, p_aout,
1169 &p_cmd->out.time_get.i_delay );
1172 assert( p_sys->p_audiotrack );
1175 JNIThread_Flush( env, p_aout,
1176 p_cmd->in.flush.b_wait );
1177 JNIThread_InitDelay( env, p_aout );
1181 vlc_assert_unreachable();
1183 if( p_sys->b_audiotrack_exception )
1188 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
1189 p_cmd->id = CMD_DONE;
1190 if( p_cmd->pf_destroy )
1191 p_cmd->pf_destroy( p_cmd );
1194 /* signal that command is processed */
1195 vlc_cond_signal( &p_sys->cond );
1200 if( p_sys->p_bytearray )
1201 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
1202 if( p_sys->p_bytebuffer )
1203 (*env)->DeleteGlobalRef( env, p_sys->p_bytebuffer );
1204 jni_detach_thread();
1206 vlc_mutex_unlock( &p_sys->mutex );
1211 Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
1213 int i_ret = VLC_EGENERIC;
1214 struct thread_cmd *p_cmd;
1215 aout_sys_t *p_sys = p_aout->sys;
1217 vlc_mutex_lock( &p_sys->mutex );
1219 p_cmd = ThreadCmd_New( CMD_START );
1222 /* ask the thread to process the Start command */
1223 p_cmd->in.start.p_fmt = p_fmt;
1225 ThreadCmd_InsertHead( p_sys, p_cmd );
1226 if( ThreadCmd_Wait( p_sys, p_cmd ) )
1228 i_ret = p_cmd->out.start.i_ret;
1229 if( i_ret == VLC_SUCCESS )
1230 *p_fmt = *p_cmd->out.start.p_fmt;
1235 vlc_mutex_unlock( &p_sys->mutex );
1237 if( i_ret == VLC_SUCCESS )
1238 aout_SoftVolumeStart( p_aout );
1244 Stop( audio_output_t *p_aout )
1246 aout_sys_t *p_sys = p_aout->sys;
1247 struct thread_cmd *p_cmd;
1249 vlc_mutex_lock( &p_sys->mutex );
1251 ThreadCmd_FlushQueue( p_sys );
1253 p_cmd = ThreadCmd_New( CMD_STOP );
1256 /* ask the thread to process the Stop command */
1257 ThreadCmd_InsertHead( p_sys, p_cmd );
1258 ThreadCmd_Wait( p_sys, p_cmd );
1263 vlc_mutex_unlock( &p_sys->mutex );
1267 PlayCmd_Destroy( struct thread_cmd *p_cmd )
1269 block_Release( p_cmd->in.play.p_buffer );
1274 Play( audio_output_t *p_aout, block_t *p_buffer )
1276 aout_sys_t *p_sys = p_aout->sys;
1277 struct thread_cmd *p_cmd;
1279 vlc_mutex_lock( &p_sys->mutex );
1281 while( p_sys->i_samples_queued != 0
1282 && FRAMES_TO_US( p_sys->i_samples_queued +
1283 p_buffer->i_nb_samples ) >= MAX_QUEUE_US )
1284 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
1286 p_cmd = ThreadCmd_New( CMD_PLAY );
1289 /* ask the thread to process the Play command */
1290 p_cmd->in.play.p_buffer = p_buffer;
1291 p_cmd->pf_destroy = PlayCmd_Destroy;
1293 ThreadCmd_InsertTail( p_sys, p_cmd );
1295 p_sys->i_samples_queued += p_buffer->i_nb_samples;
1297 block_Release( p_cmd->in.play.p_buffer );
1299 vlc_mutex_unlock( &p_sys->mutex );
1303 Pause( audio_output_t *p_aout, bool b_pause, mtime_t i_date )
1305 aout_sys_t *p_sys = p_aout->sys;
1306 struct thread_cmd *p_cmd;
1308 vlc_mutex_lock( &p_sys->mutex );
1310 p_cmd = ThreadCmd_New( CMD_PAUSE );
1313 /* ask the thread to process the Pause command */
1314 p_cmd->in.pause.b_pause = b_pause;
1315 p_cmd->in.pause.i_date = i_date;
1317 ThreadCmd_InsertHead( p_sys, p_cmd );
1318 ThreadCmd_Wait( p_sys, p_cmd );
1323 vlc_mutex_unlock( &p_sys->mutex );
1327 Flush( audio_output_t *p_aout, bool b_wait )
1329 aout_sys_t *p_sys = p_aout->sys;
1330 struct thread_cmd *p_cmd;
1332 vlc_mutex_lock( &p_sys->mutex );
1334 ThreadCmd_FlushQueue( p_sys );
1336 p_cmd = ThreadCmd_New( CMD_FLUSH );
1339 /* ask the thread to process the Flush command */
1340 p_cmd->in.flush.b_wait = b_wait;
1342 ThreadCmd_InsertHead( p_sys, p_cmd );
1343 ThreadCmd_Wait( p_sys, p_cmd );
1348 vlc_mutex_unlock( &p_sys->mutex );
1352 TimeGet( audio_output_t *p_aout, mtime_t *restrict p_delay )
1354 aout_sys_t *p_sys = p_aout->sys;
1355 struct thread_cmd *p_cmd;
1358 vlc_mutex_lock( &p_sys->mutex );
1360 p_cmd = ThreadCmd_New( CMD_TIME_GET );
1363 ThreadCmd_InsertHead( p_sys, p_cmd );
1364 ThreadCmd_Wait( p_sys, p_cmd );
1366 i_ret = p_cmd->out.time_get.i_ret;
1367 *p_delay = p_cmd->out.time_get.i_delay;
1371 vlc_mutex_unlock( &p_sys->mutex );
1378 Open( vlc_object_t *obj )
1380 audio_output_t *p_aout = (audio_output_t *) obj;
1383 if( !InitJNIFields( p_aout ) )
1384 return VLC_EGENERIC;
1386 p_sys = calloc( 1, sizeof (aout_sys_t) );
1388 if( unlikely( p_sys == NULL ) )
1391 vlc_mutex_init( &p_sys->mutex );
1392 vlc_cond_init( &p_sys->cond );
1393 TAILQ_INIT( &p_sys->thread_cmd_queue );
1395 p_aout->sys = p_sys;
1396 p_aout->start = Start;
1397 p_aout->stop = Stop;
1398 p_aout->play = Play;
1399 p_aout->pause = Pause;
1400 p_aout->flush = Flush;
1401 p_aout->time_get = TimeGet;
1403 aout_SoftVolumeInit( p_aout );
1405 /* create JNIThread */
1406 p_sys->b_thread_run = true;
1407 if( vlc_clone( &p_sys->thread,
1408 JNIThread, p_aout, VLC_THREAD_PRIORITY_AUDIO ) )
1410 msg_Err( p_aout, "JNIThread creation failed" );
1411 p_sys->b_thread_run = false;
1413 return VLC_EGENERIC;
1420 Close( vlc_object_t *obj )
1422 audio_output_t *p_aout = (audio_output_t *) obj;
1423 aout_sys_t *p_sys = p_aout->sys;
1425 /* kill the thread */
1426 vlc_mutex_lock( &p_sys->mutex );
1427 if( p_sys->b_thread_run )
1429 p_sys->b_thread_run = false;
1430 vlc_cond_signal( &p_sys->cond );
1431 vlc_mutex_unlock( &p_sys->mutex );
1432 vlc_join( p_sys->thread, NULL );
1434 vlc_mutex_unlock( &p_sys->mutex );
1436 vlc_mutex_destroy( &p_sys->mutex );
1437 vlc_cond_destroy( &p_sys->cond );