1 /*****************************************************************************
2 * portaudio.c : portaudio (v19) audio output plugin
3 *****************************************************************************
4 * Copyright (C) 2002, 2006 the VideoLAN team
7 * Authors: Frederic Ruget <frederic.ruget@free.fr>
8 * Gildas Bazin <gbazin@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <portaudio.h>
40 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
43 # define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
46 /*****************************************************************************
47 * aout_sys_t: portaudio audio output method descriptor
48 *****************************************************************************/
49 typedef struct pa_thread_t
52 aout_instance_t *p_aout;
55 vlc_mutex_t lock_wait;
58 vlc_mutex_t lock_signal;
65 aout_instance_t *p_aout;
68 PaDeviceIndex i_devices;
70 PaDeviceIndex i_device_id;
71 const PaDeviceInfo *deviceInfo;
73 bool b_chan_reorder; /* do we need channel reordering */
74 int pi_chan_table[AOUT_CHAN_MAX];
75 uint32_t i_channel_mask;
76 uint32_t i_bits_per_sample;
80 static const uint32_t pi_channels_in[] =
81 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
82 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
83 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
84 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
85 static const uint32_t pi_channels_out[] =
86 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
87 AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
88 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
89 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
91 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
92 static bool b_init = 0;
93 static pa_thread_t *pa_thread;
94 static void* PORTAUDIOThread( vlc_object_t * );
97 /*****************************************************************************
99 *****************************************************************************/
100 static int Open ( vlc_object_t * );
101 static void Close ( vlc_object_t * );
102 static void Play ( aout_instance_t * );
104 static int PAOpenDevice( aout_instance_t * );
105 static int PAOpenStream( aout_instance_t * );
107 /*****************************************************************************
109 *****************************************************************************/
110 #define DEVICE_TEXT N_("Output device")
111 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
114 set_shortname( "PortAudio" );
115 set_description( N_("PORTAUDIO audio output") );
116 set_category( CAT_AUDIO );
117 set_subcategory( SUBCAT_AUDIO_AOUT );
118 add_integer( "portaudio-device", 0, NULL,
119 DEVICE_TEXT, DEVICE_LONGTEXT, false );
120 set_capability( "audio output", 0 );
121 set_callbacks( Open, Close );
124 /* This routine will be called by the PortAudio engine when audio is needed.
125 * It may called at interrupt level on some machines so don't do anything
126 * that could mess up the system like calling malloc() or free().
128 static int paCallback( const void *inputBuffer, void *outputBuffer,
129 unsigned long framesPerBuffer,
130 const PaStreamCallbackTimeInfo *paDate,
131 PaStreamCallbackFlags statusFlags, void *p_cookie )
133 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
134 aout_instance_t *p_aout = p_sys->p_aout;
135 aout_buffer_t *p_buffer;
138 out_date = mdate() + (mtime_t) ( 1000000 *
139 ( paDate->outputBufferDacTime - paDate->currentTime ) );
140 p_buffer = aout_OutputNextBuffer( p_aout, out_date, true );
142 if ( p_buffer != NULL )
144 if( p_sys->b_chan_reorder )
146 /* Do the channel reordering here */
147 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
148 p_sys->i_channels, p_sys->pi_chan_table,
149 p_sys->i_bits_per_sample );
151 vlc_memcpy( outputBuffer, p_buffer->p_buffer,
152 framesPerBuffer * p_sys->i_sample_size );
153 /* aout_BufferFree may be dangereous here, but then so is
154 * aout_OutputNextBuffer (calls aout_BufferFree internally).
155 * one solution would be to link the no longer useful buffers
156 * in a second fifo (in aout_OutputNextBuffer too) and to
157 * wait until we are in Play to do the actual free.
159 aout_BufferFree( p_buffer );
162 /* Audio output buffer shortage -> stop the fill process and wait */
164 vlc_memset( outputBuffer, 0, framesPerBuffer * p_sys->i_sample_size );
169 /*****************************************************************************
170 * Open: open the audio device
171 *****************************************************************************/
172 static int Open( vlc_object_t * p_this )
174 aout_instance_t *p_aout = (aout_instance_t *)p_this;
175 struct aout_sys_t * p_sys;
179 msg_Dbg( p_aout, "entering Open()");
181 /* Allocate p_sys structure */
182 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
185 p_sys->p_aout = p_aout;
187 p_aout->output.p_sys = p_sys;
188 p_aout->output.pf_play = Play;
190 /* Retrieve output device id from config */
191 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
192 var_Get( p_aout, "portaudio-device", &val );
193 p_sys->i_device_id = val.i_int;
195 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
199 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
201 msg_Err( p_aout, "cannot open portaudio device" );
206 /* Close device for now. We'll re-open it later on */
207 if( ( i_err = Pa_Terminate() ) != paNoError )
209 msg_Err( p_aout, "closing the device returned %d", i_err );
214 /* Now we need to setup our DirectSound play notification structure */
215 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
216 pa_thread->p_aout = p_aout;
217 pa_thread->b_error = false;
218 vlc_mutex_init( &pa_thread->lock_wait );
219 vlc_cond_init( p_aout, &pa_thread->wait );
220 pa_thread->b_wait = false;
221 vlc_mutex_init( &pa_thread->lock_signal );
222 vlc_cond_init( p_aout, &pa_thread->signal );
223 pa_thread->b_signal = false;
225 /* Create PORTAUDIOThread */
226 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
227 VLC_THREAD_PRIORITY_OUTPUT, false ) )
229 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
235 pa_thread->p_aout = p_aout;
236 pa_thread->b_wait = false;
237 pa_thread->b_signal = false;
238 pa_thread->b_error = false;
241 /* Signal start of stream */
242 vlc_mutex_lock( &pa_thread->lock_signal );
243 pa_thread->b_signal = true;
244 vlc_cond_signal( &pa_thread->signal );
245 vlc_mutex_unlock( &pa_thread->lock_signal );
247 /* Wait until thread is ready */
248 vlc_mutex_lock( &pa_thread->lock_wait );
249 if( !pa_thread->b_wait )
250 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
251 vlc_mutex_unlock( &pa_thread->lock_wait );
252 pa_thread->b_wait = false;
254 if( pa_thread->b_error )
256 msg_Err( p_aout, "PORTAUDIO thread failed" );
265 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
267 msg_Err( p_aout, "cannot open portaudio device" );
272 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
274 msg_Err( p_aout, "cannot open portaudio device" );
282 /*****************************************************************************
283 * Close: close the audio device
284 *****************************************************************************/
285 static void Close ( vlc_object_t *p_this )
287 aout_instance_t *p_aout = (aout_instance_t *)p_this;
288 aout_sys_t *p_sys = p_aout->output.p_sys;
291 msg_Dbg( p_aout, "closing portaudio");
293 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
295 /* Signal end of stream */
296 vlc_mutex_lock( &pa_thread->lock_signal );
297 pa_thread->b_signal = true;
298 vlc_cond_signal( &pa_thread->signal );
299 vlc_mutex_unlock( &pa_thread->lock_signal );
301 /* Wait until thread is ready */
302 vlc_mutex_lock( &pa_thread->lock_wait );
303 if( !pa_thread->b_wait )
304 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
305 vlc_mutex_unlock( &pa_thread->lock_wait );
306 pa_thread->b_wait = false;
310 i_err = Pa_StopStream( p_sys->p_stream );
311 if( i_err != paNoError )
313 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
314 Pa_GetErrorText( i_err ) );
316 i_err = Pa_CloseStream( p_sys->p_stream );
317 if( i_err != paNoError )
319 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
320 Pa_GetErrorText( i_err ) );
323 i_err = Pa_Terminate();
324 if( i_err != paNoError )
326 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
327 Pa_GetErrorText( i_err ) );
332 msg_Dbg( p_aout, "portaudio closed");
336 static int PAOpenDevice( aout_instance_t *p_aout )
338 aout_sys_t *p_sys = p_aout->output.p_sys;
339 const PaDeviceInfo *p_pdi;
341 vlc_value_t val, text;
344 /* Initialize portaudio */
345 i_err = Pa_Initialize();
346 if( i_err != paNoError )
348 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
349 i_err, Pa_GetErrorText( i_err ) );
354 p_sys->i_devices = Pa_GetDeviceCount();
355 if( p_sys->i_devices < 0 )
357 i_err = p_sys->i_devices;
358 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
359 Pa_GetErrorText( i_err ) );
364 /* Display all devices info */
365 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
366 for( i = 0; i < p_sys->i_devices; i++ )
368 p_pdi = Pa_GetDeviceInfo( i );
369 msg_Dbg( p_aout, "------------------------------------- #%d", i );
370 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
371 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
372 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
374 msg_Dbg( p_aout, "-------------------------------------" );
376 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
377 if( p_sys->i_device_id >= p_sys->i_devices )
379 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
382 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
384 if( p_sys->deviceInfo->maxOutputChannels < 1 )
386 msg_Err( p_aout, "no channel available" );
390 if( var_Type( p_aout, "audio-device" ) == 0 )
392 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
393 text.psz_string = _("Audio Device");
394 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
396 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
398 val.i_int = AOUT_VAR_MONO;
399 text.psz_string = N_("Mono");
400 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
402 msg_Dbg( p_aout, "device supports 1 channel" );
404 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
406 val.i_int = AOUT_VAR_STEREO;
407 text.psz_string = N_("Stereo");
408 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
410 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
412 var_Set( p_aout, "audio-device", val );
413 msg_Dbg( p_aout, "device supports 2 channels" );
415 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
417 val.i_int = AOUT_VAR_2F2R;
418 text.psz_string = N_("2 Front 2 Rear");
419 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
421 msg_Dbg( p_aout, "device supports 4 channels" );
423 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
425 val.i_int = AOUT_VAR_3F2R;
426 text.psz_string = N_("3 Front 2 Rear");
427 var_Change( p_aout, "audio-device",
428 VLC_VAR_ADDCHOICE, &val, &text );
429 msg_Dbg( p_aout, "device supports 5 channels" );
431 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
433 val.i_int = AOUT_VAR_5_1;
434 text.psz_string = "5.1";
435 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
437 msg_Dbg( p_aout, "device supports 5.1 channels" );
440 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
443 var_Set( p_aout, "intf-change", val );
446 /* Audio format is paFloat32 (always supported by portaudio v19) */
447 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
452 if( ( i_err = Pa_Terminate() ) != paNoError )
454 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
459 static int PAOpenStream( aout_instance_t *p_aout )
461 aout_sys_t *p_sys = p_aout->output.p_sys;
462 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
463 PaStreamParameters paStreamParameters;
465 int i_channels, i_err;
466 uint32_t i_channel_mask;
468 if( var_Get( p_aout, "audio-device", &val ) < 0 )
473 if( val.i_int == AOUT_VAR_5_1 )
475 p_aout->output.output.i_physical_channels
476 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
477 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
480 else if( val.i_int == AOUT_VAR_3F2R )
482 p_aout->output.output.i_physical_channels
483 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
484 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
486 else if( val.i_int == AOUT_VAR_2F2R )
488 p_aout->output.output.i_physical_channels
489 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
490 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
492 else if( val.i_int == AOUT_VAR_MONO )
494 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
498 p_aout->output.output.i_physical_channels
499 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
502 i_channels = aout_FormatNbChannels( &p_aout->output.output );
503 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
504 i_channel_mask = p_aout->output.output.i_physical_channels;
506 /* Calculate the frame size in bytes */
507 p_sys->i_sample_size = 4 * i_channels;
508 p_aout->output.i_nb_samples = FRAME_SIZE;
509 aout_FormatPrepare( &p_aout->output.output );
510 aout_VolumeSoftInit( p_aout );
512 /* Check for channel reordering */
513 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
514 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
515 p_aout->output.p_sys->i_channels = i_channels;
517 p_aout->output.p_sys->b_chan_reorder =
518 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
519 i_channel_mask, i_channels,
520 p_aout->output.p_sys->pi_chan_table );
522 if( p_aout->output.p_sys->b_chan_reorder )
524 msg_Dbg( p_aout, "channel reordering needed" );
527 paStreamParameters.device = p_sys->i_device_id;
528 paStreamParameters.channelCount = i_channels;
529 paStreamParameters.sampleFormat = paFloat32;
530 paStreamParameters.suggestedLatency =
531 p_sys->deviceInfo->defaultLowOutputLatency;
532 paStreamParameters.hostApiSpecificStreamInfo = NULL;
534 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
535 &paStreamParameters, (double)p_aout->output.output.i_rate,
536 FRAME_SIZE, paClipOff, paCallback, p_sys );
537 if( i_err != paNoError )
539 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
540 Pa_GetErrorText( i_err ) );
541 if( i_err == paUnanticipatedHostError )
543 msg_Err( p_aout, "type %d code %ld : %s",
544 paLastHostErrorInfo->hostApiType,
545 paLastHostErrorInfo->errorCode,
546 paLastHostErrorInfo->errorText );
552 i_err = Pa_StartStream( p_sys->p_stream );
553 if( i_err != paNoError )
555 msg_Err( p_aout, "Pa_StartStream() failed" );
556 Pa_CloseStream( p_sys->p_stream );
563 /*****************************************************************************
565 *****************************************************************************/
566 static void Play( aout_instance_t * p_aout )
570 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
571 /*****************************************************************************
572 * PORTAUDIOThread: all interactions with libportaudio.a are handled
573 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
574 *****************************************************************************/
575 static void* PORTAUDIOThread( vlc_object_t *p_this )
577 pa_thread_t *pa_thread = (pa_thread_t*)p_this;
578 aout_instance_t *p_aout;
582 while( vlc_object_alive (pa_thread) )
584 /* Wait for start of stream */
585 vlc_mutex_lock( &pa_thread->lock_signal );
586 if( !pa_thread->b_signal )
587 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
588 vlc_mutex_unlock( &pa_thread->lock_signal );
589 pa_thread->b_signal = false;
591 p_aout = pa_thread->p_aout;
592 p_sys = p_aout->output.p_sys;
594 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
596 msg_Err( p_aout, "cannot open portaudio device" );
597 pa_thread->b_error = true;
600 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
602 msg_Err( p_aout, "cannot open portaudio device" );
603 pa_thread->b_error = true;
605 i_err = Pa_Terminate();
606 if( i_err != paNoError )
608 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
609 Pa_GetErrorText( i_err ) );
613 /* Tell the main thread that we are ready */
614 vlc_mutex_lock( &pa_thread->lock_wait );
615 pa_thread->b_wait = true;
616 vlc_cond_signal( &pa_thread->wait );
617 vlc_mutex_unlock( &pa_thread->lock_wait );
619 /* Wait for end of stream */
620 vlc_mutex_lock( &pa_thread->lock_signal );
621 if( !pa_thread->b_signal )
622 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
623 vlc_mutex_unlock( &pa_thread->lock_signal );
624 pa_thread->b_signal = false;
626 if( pa_thread->b_error ) continue;
628 i_err = Pa_StopStream( p_sys->p_stream );
629 if( i_err != paNoError )
631 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
632 Pa_GetErrorText( i_err ) );
634 i_err = Pa_CloseStream( p_sys->p_stream );
635 if( i_err != paNoError )
637 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
638 Pa_GetErrorText( i_err ) );
640 i_err = Pa_Terminate();
641 if( i_err != paNoError )
643 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
644 Pa_GetErrorText( i_err ) );
647 /* Tell the main thread that we are ready */
648 vlc_mutex_lock( &pa_thread->lock_wait );
649 pa_thread->b_wait = true;
650 vlc_cond_signal( &pa_thread->wait );
651 vlc_mutex_unlock( &pa_thread->lock_wait );