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 */
65 /* JNIThread control */
70 /* Shared between two threads, must be locked */
71 bool b_thread_run; /* is thread alive */
72 THREAD_CMD_QUEUE thread_cmd_queue; /* thread cmd queue */
73 uint32_t i_samples_queued; /* number of samples queued */
74 uint32_t i_audiotrack_delay; /* audiotrack delay in samples */
77 /* Soft volume helper */
78 #include "audio_output/volume.h"
80 //#define AUDIOTRACK_USE_FLOAT
83 set_shortname( "AudioTrack" )
84 set_description( N_( "Android AudioTrack audio output" ) )
85 set_capability( "audio output", 180 )
86 set_category( CAT_AUDIO )
87 set_subcategory( SUBCAT_AUDIO_AOUT )
89 add_shortcut( "audiotrack" )
90 set_callbacks( Open, Close )
95 TAILQ_ENTRY(thread_cmd) next;
106 audio_sample_format_t *p_fmt;
122 audio_sample_format_t *p_fmt;
125 void ( *pf_destroy )( struct thread_cmd * );
128 #define THREAD_NAME "android_audiotrack"
130 extern int jni_attach_thread(JNIEnv **env, const char *thread_name);
131 extern void jni_detach_thread();
132 extern int jni_get_env(JNIEnv **env);
145 jmethodID getPlaybackHeadPosition;
146 jmethodID getTimestamp;
147 jmethodID getMinBufferSize;
150 jint ERROR_BAD_VALUE;
151 jint ERROR_INVALID_OPERATION;
154 jint ENCODING_PCM_8BIT;
155 jint ENCODING_PCM_16BIT;
156 jint ENCODING_PCM_FLOAT;
157 bool has_ENCODING_PCM_FLOAT;
158 jint CHANNEL_OUT_MONO;
159 jint CHANNEL_OUT_STEREO;
162 jint ERROR_DEAD_OBJECT;
163 bool has_ERROR_DEAD_OBJECT;
169 jfieldID framePosition;
174 /* init all jni fields.
175 * Done only one time during the first initialisation */
177 InitJNIFields( audio_output_t *p_aout )
179 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
180 static int i_init_state = -1;
181 bool ret, b_attached = false;
186 vlc_mutex_lock( &lock );
188 if( i_init_state != -1 )
191 if( jni_get_env(&env) < 0 )
193 jni_attach_thread( &env, THREAD_NAME );
202 #define CHECK_EXCEPTION( what, critical ) do { \
203 if( (*env)->ExceptionOccurred( env ) ) \
205 msg_Err( p_aout, "%s failed", what ); \
206 (*env)->ExceptionClear( env ); \
214 #define GET_CLASS( str, critical ) do { \
215 clazz = (*env)->FindClass( env, (str) ); \
216 CHECK_EXCEPTION( str, critical ); \
218 #define GET_ID( get, id, str, args, critical ) do { \
219 jfields.id = (*env)->get( env, clazz, (str), (args) ); \
220 CHECK_EXCEPTION( #get, critical ); \
222 #define GET_CONST_INT( id, str, critical ) do { \
224 field = (*env)->GetStaticFieldID( env, clazz, (str), "I" ); \
225 CHECK_EXCEPTION( #id, critical ); \
228 jfields.id = (*env)->GetStaticIntField( env, clazz, field ); \
229 CHECK_EXCEPTION( #id, critical ); \
233 /* AudioTrack class init */
234 GET_CLASS( "android/media/AudioTrack", true );
235 jfields.AudioTrack.clazz = (jclass) (*env)->NewGlobalRef( env, clazz );
236 CHECK_EXCEPTION( "NewGlobalRef", true );
238 GET_ID( GetMethodID, AudioTrack.ctor, "<init>", "(IIIIII)V", true );
239 GET_ID( GetMethodID, AudioTrack.release, "release", "()V", true );
240 GET_ID( GetMethodID, AudioTrack.play, "play", "()V", true );
241 GET_ID( GetMethodID, AudioTrack.stop, "stop", "()V", true );
242 GET_ID( GetMethodID, AudioTrack.flush, "flush", "()V", true );
243 GET_ID( GetMethodID, AudioTrack.pause, "pause", "()V", true );
244 GET_ID( GetMethodID, AudioTrack.write, "write", "([BII)I", true );
246 GET_ID( GetMethodID, AudioTrack.getTimestamp,
247 "getTimestamp", "(Landroid/media/AudioTimestamp;)Z", false );
248 GET_ID( GetMethodID, AudioTrack.getPlaybackHeadPosition,
249 "getPlaybackHeadPosition", "()I", true );
251 GET_ID( GetStaticMethodID, AudioTrack.getMinBufferSize, "getMinBufferSize",
253 GET_CONST_INT( AudioTrack.MODE_STREAM, "MODE_STREAM", true );
254 GET_CONST_INT( AudioTrack.ERROR, "ERROR", true );
255 GET_CONST_INT( AudioTrack.ERROR_BAD_VALUE , "ERROR_BAD_VALUE", true );
256 GET_CONST_INT( AudioTrack.ERROR_INVALID_OPERATION ,
257 "ERROR_INVALID_OPERATION", true );
259 /* AudioTimestamp class init (if any) */
260 if( jfields.AudioTrack.getTimestamp )
262 GET_CLASS( "android/media/AudioTimestamp", true );
263 jfields.AudioTimestamp.clazz = (jclass) (*env)->NewGlobalRef( env,
265 CHECK_EXCEPTION( "NewGlobalRef", true );
267 GET_ID( GetMethodID, AudioTimestamp.ctor, "<init>", "()V", true );
268 GET_ID( GetFieldID, AudioTimestamp.framePosition,
269 "framePosition", "J", true );
270 GET_ID( GetFieldID, AudioTimestamp.nanoTime,
271 "nanoTime", "J", true );
274 jfields.AudioTimestamp.clazz = NULL;
275 jfields.AudioTimestamp.ctor = NULL;
276 jfields.AudioTimestamp.framePosition = NULL;
277 jfields.AudioTimestamp.nanoTime = NULL;
280 /* AudioFormat class init */
281 GET_CLASS( "android/media/AudioFormat", true );
282 GET_CONST_INT( AudioFormat.ENCODING_PCM_8BIT, "ENCODING_PCM_8BIT", true );
283 GET_CONST_INT( AudioFormat.ENCODING_PCM_16BIT, "ENCODING_PCM_16BIT", true );
284 #ifdef AUDIOTRACK_USE_FLOAT
285 GET_CONST_INT( AudioFormat.ENCODING_PCM_FLOAT, "ENCODING_PCM_FLOAT",
287 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = field != NULL;
289 jfields.AudioFormat.has_ENCODING_PCM_FLOAT = false;
291 GET_CONST_INT( AudioFormat.CHANNEL_OUT_MONO, "CHANNEL_OUT_MONO", true );
292 GET_CONST_INT( AudioFormat.CHANNEL_OUT_STEREO, "CHANNEL_OUT_STEREO", true );
294 /* AudioManager class init */
295 GET_CLASS( "android/media/AudioManager", true );
296 GET_CONST_INT( AudioManager.ERROR_DEAD_OBJECT, "ERROR_DEAD_OBJECT", false );
297 jfields.AudioManager.has_ERROR_DEAD_OBJECT = field != NULL;
298 GET_CONST_INT( AudioManager.STREAM_MUSIC, "STREAM_MUSIC", true );
300 #undef CHECK_EXCEPTION
307 ret = i_init_state == 1;
309 msg_Err( p_aout, "AudioTrack jni init failed" );
312 vlc_mutex_unlock( &lock );
317 check_exception( JNIEnv *env, bool *p_error, audio_output_t *p_aout,
320 if( (*env)->ExceptionOccurred( env ) )
322 (*env)->ExceptionClear( env );
324 msg_Err( p_aout, "AudioTrack.%s triggered an exception !", method );
329 #define CHECK_EXCEPTION( method ) check_exception( env, p_error, p_aout, method )
331 #define JNI_CALL( what, obj, method, ... ) (*env)->what( env, obj, method, ##__VA_ARGS__ )
333 #define JNI_CALL_INT( obj, method, ... ) JNI_CALL( CallIntMethod, obj, method, ##__VA_ARGS__ )
334 #define JNI_CALL_BOOL( obj, method, ... ) JNI_CALL( CallBooleanMethod, obj, method, ##__VA_ARGS__ )
335 #define JNI_CALL_VOID( obj, method, ... ) JNI_CALL( CallVoidMethod, obj, method, ##__VA_ARGS__ )
336 #define JNI_CALL_STATIC_INT( clazz, method, ... ) JNI_CALL( CallStaticIntMethod, clazz, method, ##__VA_ARGS__ )
338 #define JNI_AT_NEW( ... ) JNI_CALL( NewObject, jfields.AudioTrack.clazz, jfields.AudioTrack.ctor, ##__VA_ARGS__ )
339 #define JNI_AT_CALL_INT( method, ... ) JNI_CALL_INT( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
340 #define JNI_AT_CALL_BOOL( method, ... ) JNI_CALL_BOOL( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
341 #define JNI_AT_CALL_VOID( method, ... ) JNI_CALL_VOID( p_sys->p_audiotrack, jfields.AudioTrack.method, ##__VA_ARGS__ )
342 #define JNI_AT_CALL_STATIC_INT( method, ... ) JNI_CALL( CallStaticIntMethod, jfields.AudioTrack.clazz, jfields.AudioTrack.method, ##__VA_ARGS__ )
344 #define JNI_AUDIOTIMESTAMP_GET_LONG( field ) JNI_CALL( GetLongField, p_sys->p_audioTimestamp, jfields.AudioTimestamp.field )
346 static inline mtime_t
347 frames_to_us( aout_sys_t *p_sys, uint32_t i_nb_frames )
349 return i_nb_frames * CLOCK_FREQ / p_sys->fmt.i_rate;
351 #define FRAMES_TO_US(x) frames_to_us( p_sys, (x) )
353 static struct thread_cmd *
354 ThreadCmd_New( int id )
356 struct thread_cmd *p_cmd = calloc( 1, sizeof(struct thread_cmd) );
365 ThreadCmd_InsertHead( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
367 TAILQ_INSERT_HEAD( &p_sys->thread_cmd_queue, p_cmd, next);
368 vlc_cond_signal( &p_sys->cond );
372 ThreadCmd_InsertTail( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
374 TAILQ_INSERT_TAIL( &p_sys->thread_cmd_queue, p_cmd, next);
375 vlc_cond_signal( &p_sys->cond );
379 ThreadCmd_Wait( aout_sys_t *p_sys, struct thread_cmd *p_cmd )
381 while( p_cmd->id != CMD_DONE && p_sys->b_thread_run )
382 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
384 return p_cmd->id == CMD_DONE;
388 ThreadCmd_FlushQueue( aout_sys_t *p_sys )
390 struct thread_cmd *p_cmd, *p_cmd_next;
392 for ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue );
393 p_cmd != NULL; p_cmd = p_cmd_next )
395 p_cmd_next = TAILQ_NEXT( p_cmd, next );
396 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
397 if( p_cmd->pf_destroy )
398 p_cmd->pf_destroy( p_cmd );
403 JNIThread_InitDelay( JNIEnv *env, audio_output_t *p_aout, uint32_t *p_delay )
405 aout_sys_t *p_sys = p_aout->sys;
407 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
409 /* HACK: On some broken devices, head position is still moving after a
410 * flush or a stop. So, wait for the head position to be stabilized. */
411 if( unlikely( p_sys->i_pos_initial != 0 ) )
415 i_last_pos = p_sys->i_pos_initial;
417 p_sys->i_pos_initial = JNI_AT_CALL_INT( getPlaybackHeadPosition );
418 } while( p_sys->i_pos_initial != i_last_pos );
420 p_sys->i_samples_written = 0;
425 JNIThread_SetDelay( JNIEnv *env, audio_output_t *p_aout, uint32_t *p_delay )
427 aout_sys_t *p_sys = p_aout->sys;
428 bool b_frame_delay_set = false;
430 mtime_t i_current_time = mdate();
432 if( p_sys->p_audioTimestamp )
435 * getTimestamp: Poll for a timestamp on demand.
437 * If you need to track timestamps during initial warmup or after a
438 * routing or mode change, you should request a new timestamp once per
439 * second until the reported timestamps show that the audio clock is
440 * stable. Thereafter, query for a new timestamp approximately once
441 * every 10 seconds to once per minute. Calling this method more often
442 * is inefficient. It is also counter-productive to call this method
443 * more often than recommended, because the short-term differences
444 * between successive timestamp reports are not meaningful. If you need
445 * a high-resolution mapping between frame position and presentation
446 * time, consider implementing that at application level, based on
447 * low-resolution timestamps.
450 if( JNI_AT_CALL_BOOL( getTimestamp, p_sys->p_audioTimestamp ) )
452 jlong i_frame_time = JNI_AUDIOTIMESTAMP_GET_LONG( nanoTime ) / 1000;
453 /* frame time should be after last play time
454 * frame time shouldn't be in the future
455 * frame time should be less than 10 seconds old */
456 if( i_frame_time > p_sys->i_play_time
457 && i_current_time > i_frame_time
458 && ( i_current_time - i_frame_time ) <= INT64_C(10000000) )
460 jlong i_time_diff = i_current_time - i_frame_time;
461 jlong i_frames_diff = i_time_diff * p_sys->fmt.i_rate
463 i_frame_pos = JNI_AUDIOTIMESTAMP_GET_LONG( framePosition )
465 b_frame_delay_set = true;
469 if( !b_frame_delay_set )
472 * getPlaybackHeadPosition: Returns the playback head position
473 * expressed in frames. Though the "int" type is signed 32-bits, the
474 * value should be reinterpreted as if it is unsigned 32-bits. That is,
475 * the next position after 0x7FFFFFFF is (int) 0x80000000. This is a
476 * continuously advancing counter. It will wrap (overflow)
477 * periodically, for example approximately once every 27:03:11
478 * hours:minutes:seconds at 44.1 kHz. It is reset to zero by flush(),
479 * reload(), and stop().
482 uint32_t i_head_pos = JNI_AT_CALL_INT( getPlaybackHeadPosition );
483 i_frame_pos = i_head_pos - p_sys->i_pos_initial;
484 b_frame_delay_set = true;
487 if( b_frame_delay_set && p_sys->i_samples_written > i_frame_pos )
488 *p_delay = p_sys->i_samples_written - i_frame_pos;
492 JNIThread_Start( JNIEnv *env, bool *p_error, audio_output_t *p_aout )
494 struct aout_sys_t *p_sys = p_aout->sys;
495 int i_size, i_min_buffer_size, i_channel_config, i_rate, i_format,
496 i_format_size, i_nb_channels;
497 jobject p_audiotrack;
499 /* 4000 <= frequency <= 48000 */
500 i_rate = p_sys->fmt.i_rate;
506 /* We can only accept U8, S16N, and FL32 (depending on Android version) */
507 if( p_sys->fmt.i_format != VLC_CODEC_U8
508 && p_sys->fmt.i_format != VLC_CODEC_S16N
509 && p_sys->fmt.i_format != VLC_CODEC_FL32 )
510 p_sys->fmt.i_format = VLC_CODEC_S16N;
512 if( p_sys->fmt.i_format == VLC_CODEC_FL32
513 && !jfields.AudioFormat.has_ENCODING_PCM_FLOAT )
514 p_sys->fmt.i_format = VLC_CODEC_S16N;
516 if( p_sys->fmt.i_format == VLC_CODEC_S16N )
518 i_format = jfields.AudioFormat.ENCODING_PCM_16BIT;
520 } else if( p_sys->fmt.i_format == VLC_CODEC_FL32 )
522 i_format = jfields.AudioFormat.ENCODING_PCM_FLOAT;
526 i_format = jfields.AudioFormat.ENCODING_PCM_8BIT;
529 p_sys->fmt.i_original_channels = p_sys->fmt.i_physical_channels;
531 i_nb_channels = aout_FormatNbChannels( &p_sys->fmt );
532 switch( i_nb_channels )
535 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_MONO;
536 p_sys->fmt.i_physical_channels = AOUT_CHAN_CENTER;
539 i_nb_channels = 2; // XXX: AudioTrack handle only stereo for now
541 i_channel_config = jfields.AudioFormat.CHANNEL_OUT_STEREO;
542 p_sys->fmt.i_physical_channels = AOUT_CHANS_STEREO;
546 i_min_buffer_size = JNI_AT_CALL_STATIC_INT( getMinBufferSize, i_rate,
547 i_channel_config, i_format );
548 if( i_min_buffer_size <= 0 )
550 msg_Warn( p_aout, "getMinBufferSize returned an invalid size" ) ;
551 /* use a defaut min buffer size (shouldn't happen) */
552 i_min_buffer_size = i_nb_channels * i_format_size * 2024;
555 i_size = i_min_buffer_size * 2; // double buffering
557 /* create AudioTrack object */
558 p_audiotrack = JNI_AT_NEW( jfields.AudioManager.STREAM_MUSIC, i_rate,
559 i_channel_config, i_format, i_size,
560 jfields.AudioTrack.MODE_STREAM );
561 if( CHECK_EXCEPTION( "AudioTrack<init>" ) || !p_audiotrack )
563 p_sys->p_audiotrack = (*env)->NewGlobalRef( env, p_audiotrack );
564 (*env)->DeleteLocalRef( env, p_audiotrack );
565 if( !p_sys->p_audiotrack )
568 if( jfields.AudioTimestamp.clazz )
570 /* create AudioTimestamp object */
571 jobject p_audioTimestamp = JNI_CALL( NewObject,
572 jfields.AudioTimestamp.clazz,
573 jfields.AudioTimestamp.ctor );
574 if( CHECK_EXCEPTION( "AudioTimestamp<init>" ) || !p_audioTimestamp )
576 p_sys->p_audioTimestamp = (*env)->NewGlobalRef( env, p_audioTimestamp );
577 (*env)->DeleteLocalRef( env, p_audioTimestamp );
578 if( !p_sys->p_audioTimestamp )
582 p_sys->fmt.i_rate = i_rate;
584 JNI_AT_CALL_VOID( play );
585 CHECK_EXCEPTION( "play" );
586 p_sys->i_play_time = mdate();
590 if( p_sys->p_audiotrack )
592 JNI_AT_CALL_VOID( release );
593 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
594 p_sys->p_audiotrack = NULL;
600 JNIThread_Stop( JNIEnv *env, bool *p_error, audio_output_t *p_aout )
602 aout_sys_t *p_sys = p_aout->sys;
604 JNI_AT_CALL_VOID( stop );
605 CHECK_EXCEPTION( "stop" );
607 JNI_AT_CALL_VOID( release );
608 (*env)->DeleteGlobalRef( env, p_sys->p_audiotrack );
609 p_sys->p_audiotrack = NULL;
613 JNIThread_Play( JNIEnv *env, bool *p_error, audio_output_t *p_aout,
616 aout_sys_t *p_sys = p_aout->sys;
619 /* check if we need to realloc a ByteArray */
620 if( p_buffer->i_buffer > p_sys->i_bytearray_size )
622 jbyteArray p_bytearray;
624 if( p_sys->p_bytearray )
626 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
627 p_sys->p_bytearray = NULL;
630 p_bytearray = (*env)->NewByteArray( env, p_buffer->i_buffer );
633 p_sys->p_bytearray = (*env)->NewGlobalRef( env, p_bytearray );
634 (*env)->DeleteLocalRef( env, p_bytearray );
636 p_sys->i_bytearray_size = p_buffer->i_buffer;
638 if( !p_sys->p_bytearray )
644 /* copy p_buffer in to ByteArray */
645 (*env)->SetByteArrayRegion( env, p_sys->p_bytearray, 0,
647 (jbyte *)p_buffer->p_buffer);
649 while ( p_buffer->i_buffer > (unsigned int) i_offset )
653 /* write ByteArray */
654 i_ret = JNI_AT_CALL_INT( write, p_sys->p_bytearray, i_offset,
655 p_buffer->i_buffer - i_offset);
657 if( jfields.AudioManager.has_ERROR_DEAD_OBJECT
658 && i_ret == jfields.AudioManager.ERROR_DEAD_OBJECT )
660 msg_Warn( p_aout, "ERROR_DEAD_OBJECT: "
661 "try recreating AudioTrack" );
662 JNIThread_Stop( env, p_error, p_aout );
663 i_ret = JNIThread_Start( env, p_error, p_aout );
664 if( i_ret == VLC_SUCCESS )
669 if( i_ret == jfields.AudioTrack.ERROR_INVALID_OPERATION )
670 str = "ERROR_INVALID_OPERATION";
671 else if( i_ret == jfields.AudioTrack.ERROR_BAD_VALUE )
672 str = "ERROR_BAD_VALUE";
675 msg_Err( p_aout, "Write failed: %s", str );
683 p_sys->i_samples_written += p_buffer->i_nb_samples;
687 JNIThread_Pause( JNIEnv *env, bool *p_error, audio_output_t *p_aout,
688 bool b_pause, mtime_t i_date )
690 VLC_UNUSED( i_date );
692 aout_sys_t *p_sys = p_aout->sys;
696 JNI_AT_CALL_VOID( pause );
697 CHECK_EXCEPTION( "pause" );
700 JNI_AT_CALL_VOID( play );
701 CHECK_EXCEPTION( "play" );
702 p_sys->i_play_time = mdate();
707 JNIThread_Flush( JNIEnv *env, bool *p_error, audio_output_t *p_aout,
710 aout_sys_t *p_sys = p_aout->sys;
713 * stop(): Stops playing the audio data. When used on an instance created
714 * in MODE_STREAM mode, audio will stop playing after the last buffer that
715 * was written has been played. For an immediate stop, use pause(),
716 * followed by flush() to discard audio data that hasn't been played back
719 * flush(): Flushes the audio data currently queued for playback. Any data
720 * that has not been played back will be discarded. No-op if not stopped
721 * or paused, or if the track's creation mode is not MODE_STREAM.
725 JNI_AT_CALL_VOID( stop );
726 if( CHECK_EXCEPTION( "stop" ) )
730 JNI_AT_CALL_VOID( pause );
731 if( CHECK_EXCEPTION( "pause" ) )
733 JNI_AT_CALL_VOID( flush );
735 JNI_AT_CALL_VOID( play );
736 CHECK_EXCEPTION( "play" );
737 p_sys->i_play_time = mdate();
741 JNIThread( void *data )
743 audio_output_t *p_aout = data;
744 aout_sys_t *p_sys = p_aout->sys;
745 bool b_error = false;
746 bool b_paused = false;
747 uint32_t i_audiotrack_delay = 0;
750 jni_attach_thread( &env, THREAD_NAME );
752 vlc_mutex_lock( &p_sys->mutex );
756 while( p_sys->b_thread_run )
758 struct thread_cmd *p_cmd;
760 /* wait to process a command */
761 while( ( p_cmd = TAILQ_FIRST( &p_sys->thread_cmd_queue ) ) == NULL
762 && p_sys->b_thread_run )
763 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
765 if( !p_sys->b_thread_run || p_cmd == NULL )
768 if( b_paused && p_cmd->id == CMD_PLAY )
770 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
774 TAILQ_REMOVE( &p_sys->thread_cmd_queue, p_cmd, next );
776 if( p_cmd->id == CMD_PLAY )
778 p_sys->i_samples_queued -= p_cmd->in.play.p_buffer->i_nb_samples;
779 vlc_cond_signal( &p_sys->cond );
782 vlc_mutex_unlock( &p_sys->mutex );
783 /* process a command */
787 p_sys->fmt = *p_cmd->in.start.p_fmt;
788 p_cmd->out.start.i_ret =
789 JNIThread_Start( env, &b_error, p_aout );
790 JNIThread_InitDelay( env, p_aout, &i_audiotrack_delay );
791 p_cmd->out.start.p_fmt = &p_sys->fmt;
795 JNIThread_Stop( env, &b_error, p_aout );
799 JNIThread_Play( env, &b_error, p_aout,
800 p_cmd->in.play.p_buffer );
801 JNIThread_SetDelay( env, p_aout, &i_audiotrack_delay );
804 JNIThread_Pause( env, &b_error, p_aout,
805 p_cmd->in.pause.b_pause,
806 p_cmd->in.pause.i_date );
807 b_paused = p_cmd->in.pause.b_pause;
810 JNIThread_Flush( env, &b_error, p_aout,
811 p_cmd->in.flush.b_wait );
812 JNIThread_InitDelay( env, p_aout, &i_audiotrack_delay );
815 vlc_assert_unreachable();
818 vlc_mutex_lock( &p_sys->mutex );
820 p_sys->i_audiotrack_delay = i_audiotrack_delay;
822 p_cmd->id = CMD_DONE;
823 if( p_cmd->pf_destroy )
824 p_cmd->pf_destroy( p_cmd );
827 p_sys->b_thread_run = false;
829 /* signal that command is processed */
830 vlc_cond_signal( &p_sys->cond );
835 if( p_sys->p_bytearray )
836 (*env)->DeleteGlobalRef( env, p_sys->p_bytearray );
839 p_sys->b_thread_run = false;
840 vlc_cond_signal( &p_sys->cond );
841 vlc_mutex_unlock( &p_sys->mutex );
846 Start( audio_output_t *p_aout, audio_sample_format_t *restrict p_fmt )
848 int i_ret = VLC_EGENERIC;
849 struct thread_cmd *p_cmd;
850 aout_sys_t *p_sys = p_aout->sys;
852 vlc_mutex_lock( &p_sys->mutex );
854 assert( !p_sys->b_thread_run );
856 /* create JNIThread */
857 p_sys->b_thread_run = true;
858 if( vlc_clone( &p_sys->thread,
859 JNIThread, p_aout, VLC_THREAD_PRIORITY_AUDIO ) )
861 msg_Err( p_aout, "JNIThread creation failed" );
862 vlc_mutex_unlock( &p_sys->mutex );
866 p_cmd = ThreadCmd_New( CMD_START );
869 /* ask the thread to process the Start command */
870 p_cmd->in.start.p_fmt = p_fmt;
872 ThreadCmd_InsertHead( p_sys, p_cmd );
873 if( ThreadCmd_Wait( p_sys, p_cmd ) )
875 i_ret = p_cmd->out.start.i_ret;
876 if( i_ret == VLC_SUCCESS )
877 *p_fmt = *p_cmd->out.start.p_fmt;
881 vlc_mutex_unlock( &p_sys->mutex );
883 if( i_ret == VLC_SUCCESS )
884 aout_SoftVolumeStart( p_aout );
890 Stop( audio_output_t *p_aout )
892 aout_sys_t *p_sys = p_aout->sys;
894 vlc_mutex_lock( &p_sys->mutex );
896 if( p_sys->b_thread_run )
898 struct thread_cmd *p_cmd;
900 p_sys->i_samples_queued = 0;
901 ThreadCmd_FlushQueue( p_sys );
903 p_cmd = ThreadCmd_New( CMD_STOP );
906 /* ask the thread to process the Stop command */
907 ThreadCmd_InsertHead( p_sys, p_cmd );
908 ThreadCmd_Wait( p_sys, p_cmd );
912 /* kill the thread */
913 p_sys->b_thread_run = false;
914 vlc_cond_signal( &p_sys->cond );
916 vlc_mutex_unlock( &p_sys->mutex );
918 vlc_join( p_sys->thread, NULL );
922 PlayCmd_Destroy( struct thread_cmd *p_cmd )
924 block_Release( p_cmd->in.play.p_buffer );
929 Play( audio_output_t *p_aout, block_t *p_buffer )
931 aout_sys_t *p_sys = p_aout->sys;
933 vlc_mutex_lock( &p_sys->mutex );
935 if( p_sys->b_thread_run )
937 struct thread_cmd *p_cmd;
939 while( p_sys->i_samples_queued != 0 && p_sys->b_thread_run
940 && FRAMES_TO_US( p_sys->i_samples_queued +
941 p_buffer->i_nb_samples ) >= MAX_QUEUE_US )
942 vlc_cond_wait( &p_sys->cond, &p_sys->mutex );
944 p_cmd = ThreadCmd_New( CMD_PLAY );
948 /* ask the thread to process the Play command */
949 p_cmd->in.play.p_buffer = p_buffer;
950 p_cmd->pf_destroy = PlayCmd_Destroy;
952 ThreadCmd_InsertTail( p_sys, p_cmd );
954 p_sys->i_samples_queued += p_buffer->i_nb_samples;
956 block_Release( p_cmd->in.play.p_buffer );
958 vlc_mutex_unlock( &p_sys->mutex );
962 Pause( audio_output_t *p_aout, bool b_pause, mtime_t i_date )
964 aout_sys_t *p_sys = p_aout->sys;
966 vlc_mutex_lock( &p_sys->mutex );
968 if( p_sys->b_thread_run )
970 struct thread_cmd *p_cmd = ThreadCmd_New( CMD_PAUSE );
974 /* ask the thread to process the Pause command */
975 p_cmd->in.pause.b_pause = b_pause;
976 p_cmd->in.pause.i_date = i_date;
978 ThreadCmd_InsertHead( p_sys, p_cmd );
979 ThreadCmd_Wait( p_sys, p_cmd );
984 vlc_mutex_unlock( &p_sys->mutex );
988 Flush( audio_output_t *p_aout, bool b_wait )
990 aout_sys_t *p_sys = p_aout->sys;
992 vlc_mutex_lock( &p_sys->mutex );
994 if( p_sys->b_thread_run )
996 struct thread_cmd *p_cmd;
998 p_sys->i_samples_queued = 0;
999 ThreadCmd_FlushQueue( p_sys );
1001 p_cmd = ThreadCmd_New( CMD_FLUSH );
1004 /* ask the thread to process the Flush command */
1005 p_cmd->in.flush.b_wait = b_wait;
1007 ThreadCmd_InsertHead( p_sys, p_cmd );
1008 ThreadCmd_Wait( p_sys, p_cmd );
1013 vlc_mutex_unlock( &p_sys->mutex );
1017 TimeGet( audio_output_t *p_aout, mtime_t *restrict p_delay )
1019 aout_sys_t *p_sys = p_aout->sys;
1022 vlc_mutex_lock( &p_sys->mutex );
1023 if( p_sys->i_samples_queued != 0 )
1025 *p_delay = FRAMES_TO_US( p_sys->i_samples_queued +
1026 p_sys->i_audiotrack_delay );
1030 vlc_mutex_unlock( &p_sys->mutex );
1037 Open( vlc_object_t *obj )
1039 audio_output_t *p_aout = (audio_output_t *) obj;
1042 if( !InitJNIFields( p_aout ) )
1043 return VLC_EGENERIC;
1045 p_sys = calloc( 1, sizeof (aout_sys_t) );
1047 if( unlikely( p_sys == NULL ) )
1050 vlc_mutex_init( &p_sys->mutex );
1051 vlc_cond_init( &p_sys->cond );
1052 TAILQ_INIT( &p_sys->thread_cmd_queue );
1054 p_aout->sys = p_sys;
1055 p_aout->start = Start;
1056 p_aout->stop = Stop;
1057 p_aout->play = Play;
1058 p_aout->pause = Pause;
1059 p_aout->flush = Flush;
1060 p_aout->time_get = TimeGet;
1062 aout_SoftVolumeInit( p_aout );
1068 Close( vlc_object_t *obj )
1070 audio_output_t *p_aout = (audio_output_t *) obj;
1071 aout_sys_t *p_sys = p_aout->sys;
1073 vlc_mutex_destroy( &p_sys->mutex );
1074 vlc_cond_destroy( &p_sys->cond );