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 */
59 size_t i_bytearray_size; /* size of the ByteArray */
60 audio_sample_format_t fmt; /* fmt setup by Start */
61 uint32_t i_pos_initial; /* initial position set by getPlaybackHeadPosition */
62 uint32_t i_samples_written; /* number of samples written since last flush */
63 mtime_t i_play_time; /* time when play was called */
64 bool b_audiotrack_exception; /* true if audiotrack throwed an exception */
66 /* JNIThread control */
71 /* Shared between two threads, must be locked */
72 bool b_thread_run; /* is thread alive */
73 THREAD_CMD_QUEUE thread_cmd_queue; /* thread cmd queue */
74 uint32_t i_samples_queued; /* number of samples queued */
75 uint32_t i_audiotrack_delay; /* audiotrack delay in samples */
78 /* Soft volume helper */
79 #include "audio_output/volume.h"
81 //#define AUDIOTRACK_USE_FLOAT
84 set_shortname( "AudioTrack" )
85 set_description( N_( "Android AudioTrack audio output" ) )
86 set_capability( "audio output", 180 )
87 set_category( CAT_AUDIO )
88 set_subcategory( SUBCAT_AUDIO_AOUT )
90 add_shortcut( "audiotrack" )
91 set_callbacks( Open, Close )
96 TAILQ_ENTRY(thread_cmd) next;
107 audio_sample_format_t *p_fmt;
123 audio_sample_format_t *p_fmt;
126 void ( *pf_destroy )( struct thread_cmd * );
129 #define THREAD_NAME "android_audiotrack"
131 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
132 extern void jni_detach_thread();
133 extern int jni_get_env(JNIEnv **env);
146 jmethodID getPlaybackHeadPosition;
147 jmethodID getTimestamp;
148 jmethodID getMinBufferSize;
151 jint ERROR_BAD_VALUE;
152 jint ERROR_INVALID_OPERATION;
155 jint ENCODING_PCM_8BIT;
156 jint ENCODING_PCM_16BIT;
157 jint ENCODING_PCM_FLOAT;
158 bool has_ENCODING_PCM_FLOAT;
159 jint CHANNEL_OUT_MONO;
160 jint CHANNEL_OUT_STEREO;
163 jint ERROR_DEAD_OBJECT;
164 bool has_ERROR_DEAD_OBJECT;
170 jfieldID framePosition;
175 /* init all jni fields.
176 * Done only one time during the first initialisation */
178 InitJNIFields( audio_output_t *p_aout )
180 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
181 static int i_init_state = -1;
182 bool ret, b_attached = false;
187 vlc_mutex_lock( &lock );
189 if( i_init_state != -1 )
192 if( jni_get_env(&env) < 0 )
194 jni_attach_thread( &env, THREAD_NAME );
203 #define CHECK_EXCEPTION( what, critical ) do { \
204 if( (*env)->ExceptionOccurred( env ) ) \
206 msg_Err( p_aout, "%s failed", what ); \
207 (*env)->ExceptionClear( env ); \
215 #define GET_CLASS( str, critical ) do { \
216 clazz = (*env)->FindClass( env, (str) ); \
217 CHECK_EXCEPTION( str, critical ); \
219 #define GET_ID( get, id, str, args, critical ) do { \
220 jfields.id = (*env)->get( env, clazz, (str), (args) ); \
221 CHECK_EXCEPTION( #get, critical ); \
223 #define GET_CONST_INT( id, str, critical ) do { \
225 field = (*env)->GetStaticFieldID( env, clazz, (str), "I" ); \
226 CHECK_EXCEPTION( #id, critical ); \
229 jfields.id = (*env)->GetStaticIntField( env, clazz, field ); \
230 CHECK_EXCEPTION( #id, critical ); \
234 /* AudioTrack class init */
235 GET_CLASS( "android/media/AudioTrack", true );
236 jfields.AudioTrack.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
237 CHECK_EXCEPTION( "NewGlobalRef", true );
239 GET_ID( GetMethodID, AudioTrack.ctor, "<init>", "(IIIIII)V", true );
240 GET_ID( GetMethodID, AudioTrack.release, "release", "()V", true );
241 GET_ID( GetMethodID, AudioTrack.play, "play", "()V", true );
242 GET_ID( GetMethodID, AudioTrack.stop, "stop", "()V", true );
243 GET_ID( GetMethodID, AudioTrack.flush, "flush", "()V", true );
244 GET_ID( GetMethodID, AudioTrack.pause, "pause", "()V", true );
245 GET_ID( GetMethodID, AudioTrack.write, "write", "([BII)I", true );
247 GET_ID( GetMethodID, AudioTrack.getTimestamp,
248 "getTimestamp", "(Landroid/media/AudioTimestamp;)Z", false );
249 GET_ID( GetMethodID, AudioTrack.getPlaybackHeadPosition,
250 "getPlaybackHeadPosition", "()I", true );
252 GET_ID( GetStaticMethodID, AudioTrack.getMinBufferSize, "getMinBufferSize",
254 GET_CONST_INT( AudioTrack.MODE_STREAM, "MODE_STREAM", true );
255 GET_CONST_INT( AudioTrack.ERROR, "ERROR", true );
256 GET_CONST_INT( AudioTrack.ERROR_BAD_VALUE , "ERROR_BAD_VALUE", true );
257 GET_CONST_INT( AudioTrack.ERROR_INVALID_OPERATION ,
258 "ERROR_INVALID_OPERATION", true );
260 /* AudioTimestamp class init (if any) */
261 if( jfields.AudioTrack.getTimestamp )
263 GET_CLASS( "android/media/AudioTimestamp", true );
264 jfields.AudioTimestamp.clazz = (jclass) (*env)->NewGlobalRef( env,
266 CHECK_EXCEPTION( "NewGlobalRef", true );
268 GET_ID( GetMethodID, AudioTimestamp.ctor, "<init>", "()V", true );
269 GET_ID( GetFieldID, AudioTimestamp.framePosition,
270 "framePosition", "J", true );
271 GET_ID( GetFieldID, AudioTimestamp.nanoTime,
272 "nanoTime", "J", true );
275 jfields.AudioTimestamp.clazz = NULL;
276 jfields.AudioTimestamp.ctor = NULL;
277 jfields.AudioTimestamp.framePosition = NULL;
278 jfields.AudioTimestamp.nanoTime = NULL;
281 /* AudioFormat class init */
282 GET_CLASS( "android/media/AudioFormat", true );
283 GET_CONST_INT( AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT", true );
284 GET_CONST_INT( AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT", true );
285 #ifdef AUDIOTRACK_USE_FLOAT
286 GET_CONST_INT( AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT",
288 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = field != NULL;
290 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = false;
292 GET_CONST_INT( AudioFormat.CHANNEL_OUT_MONO, "CHANNEL_OUT_MONO", true );
293 GET_CONST_INT( AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO", true );
295 /* AudioManager class init */
296 GET_CLASS( "android/media/AudioManager", true );
297 GET_CONST_INT( AudioManager.ERROR_DEAD_OBJECT, "ERROR_DEAD_OBJECT", false );
298 jfields.AudioManager.has_ERROR_DEAD_OBJECT = field != NULL;
299 GET_CONST_INT( AudioManager.STREAM_MUSIC, "STREAM_MUSIC", true );
301 #undef CHECK_EXCEPTION
308 ret = i_init_state == 1;
310 msg_Err( p_aout, "AudioTrack jni init failed" );
313 vlc_mutex_unlock( &lock );
318 check_exception( JNIEnv *env, audio_output_t *p_aout,
321 if( (*env)->ExceptionOccurred( env ) )
323 aout_sys_t *p_sys = p_aout->sys;
325 p_sys->b_audiotrack_exception = true;
326 (*env)->ExceptionClear( env );
327 msg_Err( p_aout, "AudioTrack.%s triggered an exception !", method );
332 #define CHECK_AT_EXCEPTION( method ) check_exception( env, p_aout, method )
334 #define JNI_CALL( what, obj, method, ... ) (*env)->what( env, obj, method, ##__VA_ARGS__ )
336 #define JNI_CALL_INT( obj, method, ... ) JNI_CALL( CallIntMethod, obj, method, ##__VA_ARGS__ )
337 #define JNI_CALL_BOOL( obj, method, ... ) JNI_CALL( CallBooleanMethod, obj, method, ##__VA_ARGS__ )
338 #define JNI_CALL_VOID( obj, method, ... ) JNI_CALL( CallVoidMethod, obj, method, ##__VA_ARGS__ )
339 #define JNI_CALL_STATIC_INT( clazz, method, ... ) JNI_CALL( CallStaticIntMethod, clazz, method, ##__VA_ARGS__ )
341 #define JNI_AT_NEW( ... ) JNI_CALL( NewObject, jfields.AudioTrack.clazz, jfields.AudioTrack.ctor, ##__VA_ARGS__ )
342 #define JNI_AT_CALL_INT( method, ... ) JNI_CALL_INT( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
343 #define JNI_AT_CALL_BOOL( method, ... ) JNI_CALL_BOOL( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
344 #define JNI_AT_CALL_VOID( method, ... ) JNI_CALL_VOID( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
345 #define JNI_AT_CALL_STATIC_INT( method, ... ) JNI_CALL( CallStaticIntMethod, jfields.AudioTrack.clazz, jfields.AudioTrack.method, ##__VA_ARGS__ )
347 #define JNI_AUDIOTIMESTAMP_GET_LONG( field ) JNI_CALL( GetLongField, p_sys->p_audioTimestamp, jfields.AudioTimestamp.field )
349 static inline mtime_t
350 frames_to_us( aout_sys_t *p_sys, uint32_t i_nb_frames )
352 return i_nb_frames * CLOCK_FREQ / p_sys->fmt.i_rate;
354 #define FRAMES_TO_US(x) frames_to_us( p_sys, (x) )
356 static struct thread_cmd *
357 ThreadCmd_New( int id )
359 struct thread_cmd *p_cmd = calloc( 1, sizeof(struct thread_cmd) );
368 ThreadCmd_InsertHead( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
370 TAILQ_INSERT_HEAD( &p_sys->thread_cmd_queue, p_cmd, next);
371 vlc_cond_signal( &p_sys->cond );
375 ThreadCmd_InsertTail( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
377 TAILQ_INSERT_TAIL( &p_sys->thread_cmd_queue, p_cmd, next);
378 vlc_cond_signal( &p_sys->cond );
382 ThreadCmd_Wait( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
384 while( p_cmd->id != CMD_DONE )
385 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
387 return p_cmd->id == CMD_DONE;
391 ThreadCmd_FlushQueue( aout_sys_t *p_sys )
393 struct thread_cmd *p_cmd, *p_cmd_next;
395 for ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue );
396 p_cmd != NULL; p_cmd = p_cmd_next )
398 p_cmd_next = TAILQ_NEXT( p_cmd, next );
399 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
400 if( p_cmd->pf_destroy )
401 p_cmd->pf_destroy( p_cmd );
406 JNIThread_InitDelay( JNIEnv *env, audio_output_t *p_aout, uint32_t *p_delay )
408 aout_sys_t *p_sys = p_aout->sys;
410 if( p_sys->p_audiotrack )
411 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
413 p_sys->i_pos_initial = 0;
415 /* HACK: On some broken devices, head position is still moving after a
416 * flush or a stop. So, wait for the head position to be stabilized. */
417 if( unlikely( p_sys->i_pos_initial != 0 ) )
421 i_last_pos = p_sys->i_pos_initial;
423 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
424 } while( p_sys->i_pos_initial != i_last_pos );
426 p_sys->i_samples_written = 0;
431 JNIThread_SetDelay( JNIEnv *env, audio_output_t *p_aout, uint32_t *p_delay )
433 aout_sys_t *p_sys = p_aout->sys;
434 bool b_frame_delay_set = false;
436 mtime_t i_current_time = mdate();
438 if( p_sys->p_audioTimestamp )
441 * getTimestamp: Poll for a timestamp on demand.
443 * If you need to track timestamps during initial warmup or after a
444 * routing or mode change, you should request a new timestamp once per
445 * second until the reported timestamps show that the audio clock is
446 * stable. Thereafter, query for a new timestamp approximately once
447 * every 10 seconds to once per minute. Calling this method more often
448 * is inefficient. It is also counter-productive to call this method
449 * more often than recommended, because the short-term differences
450 * between successive timestamp reports are not meaningful. If you need
451 * a high-resolution mapping between frame position and presentation
452 * time, consider implementing that at application level, based on
453 * low-resolution timestamps.
456 if( JNI_AT_CALL_BOOL( getTimestamp, p_sys->p_audioTimestamp ) )
458 jlong i_frame_time = JNI_AUDIOTIMESTAMP_GET_LONG( nanoTime ) / 1000;
459 /* frame time should be after last play time
460 * frame time shouldn't be in the future
461 * frame time should be less than 10 seconds old */
462 if( i_frame_time > p_sys->i_play_time
463 && i_current_time > i_frame_time
464 && ( i_current_time - i_frame_time ) <= INT64_C(10000000) )
466 jlong i_time_diff = i_current_time - i_frame_time;
467 jlong i_frames_diff = i_time_diff * p_sys->fmt.i_rate
469 i_frame_pos = JNI_AUDIOTIMESTAMP_GET_LONG( framePosition )
471 b_frame_delay_set = true;
475 if( !b_frame_delay_set )
478 * getPlaybackHeadPosition: Returns the playback head position
479 * expressed in frames. Though the "int" type is signed 32-bits, the
480 * value should be reinterpreted as if it is unsigned 32-bits. That is,
481 * the next position after 0x7FFFFFFF is (int) 0x80000000. This is a
482 * continuously advancing counter. It will wrap (overflow)
483 * periodically, for example approximately once every 27:03:11
484 * hours:minutes:seconds at 44.1 kHz. It is reset to zero by flush(),
485 * reload(), and stop().
488 uint32_t i_head_pos = JNI_AT_CALL_INT( getPlaybackHeadPosition );
489 i_frame_pos = i_head_pos - p_sys->i_pos_initial;
490 b_frame_delay_set = true;
493 if( b_frame_delay_set && p_sys->i_samples_written > i_frame_pos )
494 *p_delay = p_sys->i_samples_written - i_frame_pos;
498 JNIThread_Start( JNIEnv *env, audio_output_t *p_aout )
500 struct aout_sys_t *p_sys = p_aout->sys;
501 int i_size, i_min_buffer_size, i_channel_config, i_rate, i_format,
502 i_format_size, i_nb_channels;
503 jobject p_audiotrack;
505 /* 4000 <= frequency <= 48000 */
506 i_rate = p_sys->fmt.i_rate;
512 /* We can only accept U8, S16N, and FL32 (depending on Android version) */
513 if( p_sys->fmt.i_format != VLC_CODEC_U8
514 && p_sys->fmt.i_format != VLC_CODEC_S16N
515 && p_sys->fmt.i_format != VLC_CODEC_FL32 )
516 p_sys->fmt.i_format = VLC_CODEC_S16N;
518 if( p_sys->fmt.i_format == VLC_CODEC_FL32
519 && !jfields.AudioFormat.has_ENCODING_PCM_FLOAT )
520 p_sys->fmt.i_format = VLC_CODEC_S16N;
522 if( p_sys->fmt.i_format == VLC_CODEC_S16N )
524 i_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
526 } else if( p_sys->fmt.i_format == VLC_CODEC_FL32 )
528 i_format = jfields.AudioFormat.ENCODING_PCM_FLOAT;
532 i_format = jfields.AudioFormat.ENCODING_PCM_8BIT;
535 p_sys->fmt.i_original_channels = p_sys->fmt.i_physical_channels;
537 i_nb_channels = aout_FormatNbChannels( &p_sys->fmt );
538 switch( i_nb_channels )
541 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_MONO;
542 p_sys->fmt.i_physical_channels = AOUT_CHAN_CENTER;
545 i_nb_channels = 2; // XXX: AudioTrack handle only stereo for now
547 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_STEREO;
548 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
552 i_min_buffer_size = JNI_AT_CALL_STATIC_INT( getMinBufferSize, i_rate,
553 i_channel_config, i_format );
554 if( i_min_buffer_size <= 0 )
556 msg_Warn( p_aout, "getMinBufferSize returned an invalid size" ) ;
557 /* use a defaut min buffer size (shouldn't happen) */
558 i_min_buffer_size = i_nb_channels * i_format_size * 2024;
561 i_size = i_min_buffer_size * 2; // double buffering
563 /* create AudioTrack object */
564 p_audiotrack = JNI_AT_NEW( jfields.AudioManager.STREAM_MUSIC, i_rate,
565 i_channel_config, i_format, i_size,
566 jfields.AudioTrack.MODE_STREAM );
567 if( CHECK_AT_EXCEPTION( "AudioTrack<init>" ) || !p_audiotrack )
569 p_sys->p_audiotrack = (*env)->NewGlobalRef( env, p_audiotrack );
570 (*env)->DeleteLocalRef( env, p_audiotrack );
571 if( !p_sys->p_audiotrack )
574 if( jfields.AudioTimestamp.clazz )
576 /* create AudioTimestamp object */
577 jobject p_audioTimestamp = JNI_CALL( NewObject,
578 jfields.AudioTimestamp.clazz,
579 jfields.AudioTimestamp.ctor );
580 if( !p_audioTimestamp )
582 p_sys->p_audioTimestamp = (*env)->NewGlobalRef( env, p_audioTimestamp );
583 (*env)->DeleteLocalRef( env, p_audioTimestamp );
584 if( !p_sys->p_audioTimestamp )
588 p_sys->fmt.i_rate = i_rate;
590 JNI_AT_CALL_VOID( play );
591 CHECK_AT_EXCEPTION( "play" );
592 p_sys->i_play_time = mdate();
596 if( p_sys->p_audiotrack )
598 JNI_AT_CALL_VOID( release );
599 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
600 p_sys->p_audiotrack = NULL;
606 JNIThread_Stop( JNIEnv *env, audio_output_t *p_aout )
608 aout_sys_t *p_sys = p_aout->sys;
610 if( !p_sys->b_audiotrack_exception )
612 JNI_AT_CALL_VOID( stop );
613 if( !CHECK_AT_EXCEPTION( "stop" ) )
614 JNI_AT_CALL_VOID( release );
616 p_sys->b_audiotrack_exception = false;
617 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
618 p_sys->p_audiotrack = NULL;
620 if( p_sys->p_audioTimestamp )
622 (*env)->DeleteGlobalRef( env, p_sys->p_audioTimestamp );
623 p_sys->p_audioTimestamp = NULL;
628 JNIThread_Play( JNIEnv *env, audio_output_t *p_aout,
631 aout_sys_t *p_sys = p_aout->sys;
634 /* check if we need to realloc a ByteArray */
635 if( p_buffer->i_buffer > p_sys->i_bytearray_size )
637 jbyteArray p_bytearray;
639 if( p_sys->p_bytearray )
641 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
642 p_sys->p_bytearray = NULL;
645 p_bytearray = (*env)->NewByteArray( env, p_buffer->i_buffer );
648 p_sys->p_bytearray = (*env)->NewGlobalRef( env, p_bytearray );
649 (*env)->DeleteLocalRef( env, p_bytearray );
651 p_sys->i_bytearray_size = p_buffer->i_buffer;
653 if( !p_sys->p_bytearray )
656 /* copy p_buffer in to ByteArray */
657 (*env)->SetByteArrayRegion( env, p_sys->p_bytearray, 0,
659 (jbyte *)p_buffer->p_buffer);
661 while ( p_buffer->i_buffer > (unsigned int) i_offset )
665 /* write ByteArray */
666 i_ret = JNI_AT_CALL_INT( write, p_sys->p_bytearray, i_offset,
667 p_buffer->i_buffer - i_offset);
669 if( jfields.AudioManager.has_ERROR_DEAD_OBJECT
670 && i_ret == jfields.AudioManager.ERROR_DEAD_OBJECT )
672 msg_Warn( p_aout, "ERROR_DEAD_OBJECT: "
673 "try recreating AudioTrack" );
674 JNIThread_Stop( env, p_aout );
675 i_ret = JNIThread_Start( env, p_aout );
676 if( i_ret == VLC_SUCCESS )
681 if( i_ret == jfields.AudioTrack.ERROR_INVALID_OPERATION )
682 str = "ERROR_INVALID_OPERATION";
683 else if( i_ret == jfields.AudioTrack.ERROR_BAD_VALUE )
684 str = "ERROR_BAD_VALUE";
687 msg_Err( p_aout, "Write failed: %s", str );
694 p_sys->i_samples_written += p_buffer->i_nb_samples;
699 JNIThread_Pause( JNIEnv *env, audio_output_t *p_aout,
700 bool b_pause, mtime_t i_date )
702 VLC_UNUSED( i_date );
704 aout_sys_t *p_sys = p_aout->sys;
708 JNI_AT_CALL_VOID( pause );
709 CHECK_AT_EXCEPTION( "pause" );
712 JNI_AT_CALL_VOID( play );
713 CHECK_AT_EXCEPTION( "play" );
714 p_sys->i_play_time = mdate();
719 JNIThread_Flush( JNIEnv *env, audio_output_t *p_aout,
722 aout_sys_t *p_sys = p_aout->sys;
725 * stop(): Stops playing the audio data. When used on an instance created
726 * in MODE_STREAM mode, audio will stop playing after the last buffer that
727 * was written has been played. For an immediate stop, use pause(),
728 * followed by flush() to discard audio data that hasn't been played back
731 * flush(): Flushes the audio data currently queued for playback. Any data
732 * that has not been played back will be discarded. No-op if not stopped
733 * or paused, or if the track's creation mode is not MODE_STREAM.
737 JNI_AT_CALL_VOID( stop );
738 if( CHECK_AT_EXCEPTION( "stop" ) )
742 JNI_AT_CALL_VOID( pause );
743 if( CHECK_AT_EXCEPTION( "pause" ) )
745 JNI_AT_CALL_VOID( flush );
747 JNI_AT_CALL_VOID( play );
748 CHECK_AT_EXCEPTION( "play" );
749 p_sys->i_play_time = mdate();
753 JNIThread( void *data )
755 audio_output_t *p_aout = data;
756 aout_sys_t *p_sys = p_aout->sys;
757 bool b_error = false;
758 bool b_paused = false;
759 uint32_t i_audiotrack_delay = 0;
762 jni_attach_thread( &env, THREAD_NAME );
764 vlc_mutex_lock( &p_sys->mutex );
768 while( p_sys->b_thread_run )
770 struct thread_cmd *p_cmd;
772 /* wait to process a command */
773 while( ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue ) ) == NULL
774 && p_sys->b_thread_run )
775 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
777 if( !p_sys->b_thread_run || p_cmd == NULL )
780 if( b_paused && p_cmd->id == CMD_PLAY )
782 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
786 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
788 if( p_cmd->id == CMD_PLAY )
790 p_sys->i_samples_queued -= p_cmd->in.play.p_buffer->i_nb_samples;
791 vlc_cond_signal( &p_sys->cond );
794 vlc_mutex_unlock( &p_sys->mutex );
795 /* process a command */
799 assert( !p_sys->p_audiotrack );
801 p_cmd->out.start.i_ret = -1;
804 p_sys->fmt = *p_cmd->in.start.p_fmt;
805 p_cmd->out.start.i_ret =
806 JNIThread_Start( env, p_aout );
807 JNIThread_InitDelay( env, p_aout, &i_audiotrack_delay );
808 p_cmd->out.start.p_fmt = &p_sys->fmt;
812 assert( p_sys->p_audiotrack );
813 JNIThread_Stop( env, p_aout );
814 JNIThread_InitDelay( env, p_aout, &i_audiotrack_delay );
819 assert( p_sys->p_audiotrack );
822 b_error = JNIThread_Play( env, p_aout,
823 p_cmd->in.play.p_buffer ) != VLC_SUCCESS;
824 JNIThread_SetDelay( env, p_aout, &i_audiotrack_delay );
827 assert( p_sys->p_audiotrack );
830 JNIThread_Pause( env, p_aout,
831 p_cmd->in.pause.b_pause,
832 p_cmd->in.pause.i_date );
833 b_paused = p_cmd->in.pause.b_pause;
836 assert( p_sys->p_audiotrack );
839 JNIThread_Flush( env, p_aout,
840 p_cmd->in.flush.b_wait );
841 JNIThread_InitDelay( env, p_aout, &i_audiotrack_delay );
844 vlc_assert_unreachable();
846 if( p_sys->b_audiotrack_exception )
849 vlc_mutex_lock( &p_sys->mutex );
851 p_sys->i_audiotrack_delay = i_audiotrack_delay;
853 p_cmd->id = CMD_DONE;
854 if( p_cmd->pf_destroy )
855 p_cmd->pf_destroy( p_cmd );
857 /* signal that command is processed */
858 vlc_cond_signal( &p_sys->cond );
863 if( p_sys->p_bytearray )
864 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
867 vlc_mutex_unlock( &p_sys->mutex );
872 Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
874 int i_ret = VLC_EGENERIC;
875 struct thread_cmd *p_cmd;
876 aout_sys_t *p_sys = p_aout->sys;
878 vlc_mutex_lock( &p_sys->mutex );
880 p_cmd = ThreadCmd_New( CMD_START );
883 /* ask the thread to process the Start command */
884 p_cmd->in.start.p_fmt = p_fmt;
886 ThreadCmd_InsertHead( p_sys, p_cmd );
887 if( ThreadCmd_Wait( p_sys, p_cmd ) )
889 i_ret = p_cmd->out.start.i_ret;
890 if( i_ret == VLC_SUCCESS )
891 *p_fmt = *p_cmd->out.start.p_fmt;
896 vlc_mutex_unlock( &p_sys->mutex );
898 if( i_ret == VLC_SUCCESS )
899 aout_SoftVolumeStart( p_aout );
905 Stop( audio_output_t *p_aout )
907 aout_sys_t *p_sys = p_aout->sys;
908 struct thread_cmd *p_cmd;
910 vlc_mutex_lock( &p_sys->mutex );
912 p_sys->i_samples_queued = 0;
913 ThreadCmd_FlushQueue( p_sys );
915 p_cmd = ThreadCmd_New( CMD_STOP );
918 /* ask the thread to process the Stop command */
919 ThreadCmd_InsertHead( p_sys, p_cmd );
920 ThreadCmd_Wait( p_sys, p_cmd );
925 vlc_mutex_unlock( &p_sys->mutex );
929 PlayCmd_Destroy( struct thread_cmd *p_cmd )
931 block_Release( p_cmd->in.play.p_buffer );
936 Play( audio_output_t *p_aout, block_t *p_buffer )
938 aout_sys_t *p_sys = p_aout->sys;
939 struct thread_cmd *p_cmd;
941 vlc_mutex_lock( &p_sys->mutex );
943 while( p_sys->i_samples_queued != 0
944 && FRAMES_TO_US( p_sys->i_samples_queued +
945 p_buffer->i_nb_samples ) >= MAX_QUEUE_US )
946 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
948 p_cmd = ThreadCmd_New( CMD_PLAY );
951 /* ask the thread to process the Play command */
952 p_cmd->in.play.p_buffer = p_buffer;
953 p_cmd->pf_destroy = PlayCmd_Destroy;
955 ThreadCmd_InsertTail( p_sys, p_cmd );
957 p_sys->i_samples_queued += p_buffer->i_nb_samples;
959 block_Release( p_cmd->in.play.p_buffer );
961 vlc_mutex_unlock( &p_sys->mutex );
965 Pause( audio_output_t *p_aout, bool b_pause, mtime_t i_date )
967 aout_sys_t *p_sys = p_aout->sys;
968 struct thread_cmd *p_cmd;
970 vlc_mutex_lock( &p_sys->mutex );
972 p_cmd = ThreadCmd_New( CMD_PAUSE );
975 /* ask the thread to process the Pause command */
976 p_cmd->in.pause.b_pause = b_pause;
977 p_cmd->in.pause.i_date = i_date;
979 ThreadCmd_InsertHead( p_sys, p_cmd );
980 ThreadCmd_Wait( p_sys, p_cmd );
985 vlc_mutex_unlock( &p_sys->mutex );
989 Flush( audio_output_t *p_aout, bool b_wait )
991 aout_sys_t *p_sys = p_aout->sys;
992 struct thread_cmd *p_cmd;
994 vlc_mutex_lock( &p_sys->mutex );
996 p_sys->i_samples_queued = 0;
997 ThreadCmd_FlushQueue( p_sys );
999 p_cmd = ThreadCmd_New( CMD_FLUSH );
1002 /* ask the thread to process the Flush command */
1003 p_cmd->in.flush.b_wait = b_wait;
1005 ThreadCmd_InsertHead( p_sys, p_cmd );
1006 ThreadCmd_Wait( p_sys, p_cmd );
1011 vlc_mutex_unlock( &p_sys->mutex );
1015 TimeGet( audio_output_t *p_aout, mtime_t *restrict p_delay )
1017 aout_sys_t *p_sys = p_aout->sys;
1020 vlc_mutex_lock( &p_sys->mutex );
1021 if( p_sys->i_samples_queued != 0 )
1023 *p_delay = FRAMES_TO_US( p_sys->i_samples_queued +
1024 p_sys->i_audiotrack_delay );
1028 vlc_mutex_unlock( &p_sys->mutex );
1035 Open( vlc_object_t *obj )
1037 audio_output_t *p_aout = (audio_output_t *) obj;
1040 if( !InitJNIFields( p_aout ) )
1041 return VLC_EGENERIC;
1043 p_sys = calloc( 1, sizeof (aout_sys_t) );
1045 if( unlikely( p_sys == NULL ) )
1048 vlc_mutex_init( &p_sys->mutex );
1049 vlc_cond_init( &p_sys->cond );
1050 TAILQ_INIT( &p_sys->thread_cmd_queue );
1052 /* create JNIThread */
1053 p_sys->b_thread_run = true;
1054 if( vlc_clone( &p_sys->thread,
1055 JNIThread, p_aout, VLC_THREAD_PRIORITY_AUDIO ) )
1057 msg_Err( p_aout, "JNIThread creation failed" );
1058 p_sys->b_thread_run = false;
1060 return VLC_EGENERIC;
1063 p_aout->sys = p_sys;
1064 p_aout->start = Start;
1065 p_aout->stop = Stop;
1066 p_aout->play = Play;
1067 p_aout->pause = Pause;
1068 p_aout->flush = Flush;
1069 p_aout->time_get = TimeGet;
1071 aout_SoftVolumeInit( p_aout );
1077 Close( vlc_object_t *obj )
1079 audio_output_t *p_aout = (audio_output_t *) obj;
1080 aout_sys_t *p_sys = p_aout->sys;
1082 /* kill the thread */
1083 vlc_mutex_lock( &p_sys->mutex );
1084 if( p_sys->b_thread_run )
1086 p_sys->b_thread_run = false;
1087 vlc_cond_signal( &p_sys->cond );
1088 vlc_mutex_unlock( &p_sys->mutex );
1089 vlc_join( p_sys->thread, NULL );
1091 vlc_mutex_unlock( &p_sys->mutex );
1093 vlc_mutex_destroy( &p_sys->mutex );
1094 vlc_cond_destroy( &p_sys->cond );