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_out[] =
81 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
82 AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
83 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT, AOUT_CHAN_REARCENTER,
84 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
86 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
87 static bool b_init = 0;
88 static pa_thread_t *pa_thread;
89 static void* PORTAUDIOThread( vlc_object_t * );
92 /*****************************************************************************
94 *****************************************************************************/
95 static int Open ( vlc_object_t * );
96 static void Close ( vlc_object_t * );
97 static void Play ( aout_instance_t * );
99 static int PAOpenDevice( aout_instance_t * );
100 static int PAOpenStream( aout_instance_t * );
102 /*****************************************************************************
104 *****************************************************************************/
105 #define DEVICE_TEXT N_("Output device")
106 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
109 set_shortname( "PortAudio" )
110 set_description( N_("PORTAUDIO audio output") )
111 set_category( CAT_AUDIO )
112 set_subcategory( SUBCAT_AUDIO_AOUT )
113 add_integer( "portaudio-audio-device", 0, NULL,
114 DEVICE_TEXT, DEVICE_LONGTEXT, false )
115 add_deprecated_alias( "portaudio-device" ) /* deprecated since 0.9.3 */
116 set_capability( "audio output", 0 )
117 set_callbacks( Open, Close )
120 /* This routine will be called by the PortAudio engine when audio is needed.
121 * It may called at interrupt level on some machines so don't do anything
122 * that could mess up the system like calling malloc() or free().
124 static int paCallback( const void *inputBuffer, void *outputBuffer,
125 unsigned long framesPerBuffer,
126 const PaStreamCallbackTimeInfo *paDate,
127 PaStreamCallbackFlags statusFlags, void *p_cookie )
129 VLC_UNUSED( inputBuffer ); VLC_UNUSED( statusFlags );
131 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
132 aout_instance_t *p_aout = p_sys->p_aout;
133 aout_buffer_t *p_buffer;
136 out_date = mdate() + (mtime_t) ( 1000000 *
137 ( paDate->outputBufferDacTime - paDate->currentTime ) );
138 p_buffer = aout_OutputNextBuffer( p_aout, out_date, true );
140 if ( p_buffer != NULL )
142 if( p_sys->b_chan_reorder )
144 /* Do the channel reordering here */
145 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_buffer,
146 p_sys->i_channels, p_sys->pi_chan_table,
147 p_sys->i_bits_per_sample );
149 vlc_memcpy( outputBuffer, p_buffer->p_buffer,
150 framesPerBuffer * p_sys->i_sample_size );
151 /* aout_BufferFree may be dangereous here, but then so is
152 * aout_OutputNextBuffer (calls aout_BufferFree internally).
153 * one solution would be to link the no longer useful buffers
154 * in a second fifo (in aout_OutputNextBuffer too) and to
155 * wait until we are in Play to do the actual free.
157 aout_BufferFree( p_buffer );
160 /* Audio output buffer shortage -> stop the fill process and wait */
162 vlc_memset( outputBuffer, 0, framesPerBuffer * p_sys->i_sample_size );
167 /*****************************************************************************
168 * Open: open the audio device
169 *****************************************************************************/
170 static int Open( vlc_object_t * p_this )
172 aout_instance_t *p_aout = (aout_instance_t *)p_this;
173 struct aout_sys_t * p_sys;
175 msg_Dbg( p_aout, "entering Open()");
177 /* Allocate p_sys structure */
178 p_sys = malloc( sizeof(aout_sys_t) );
181 p_sys->p_aout = p_aout;
183 p_aout->output.p_sys = p_sys;
184 p_aout->output.pf_play = Play;
186 /* Retrieve output device id from config */
187 p_sys->i_device_id = var_CreateGetInteger( p_aout, "portaudio-audio-device" );
189 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
195 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
197 msg_Err( p_aout, "cannot open portaudio device" );
202 /* Close device for now. We'll re-open it later on */
203 if( ( i_err = Pa_Terminate() ) != paNoError )
205 msg_Err( p_aout, "closing the device returned %d", i_err );
210 /* Now we need to setup our DirectSound play notification structure */
211 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
212 pa_thread->p_aout = p_aout;
213 pa_thread->b_error = false;
214 vlc_mutex_init( &pa_thread->lock_wait );
215 vlc_cond_init( &pa_thread->wait );
216 pa_thread->b_wait = false;
217 vlc_mutex_init( &pa_thread->lock_signal );
218 vlc_cond_init( &pa_thread->signal );
219 pa_thread->b_signal = false;
221 /* Create PORTAUDIOThread */
222 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
223 VLC_THREAD_PRIORITY_OUTPUT ) )
225 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
231 pa_thread->p_aout = p_aout;
232 pa_thread->b_wait = false;
233 pa_thread->b_signal = false;
234 pa_thread->b_error = false;
237 /* Signal start of stream */
238 vlc_mutex_lock( &pa_thread->lock_signal );
239 pa_thread->b_signal = true;
240 vlc_cond_signal( &pa_thread->signal );
241 vlc_mutex_unlock( &pa_thread->lock_signal );
243 /* Wait until thread is ready */
244 vlc_mutex_lock( &pa_thread->lock_wait );
245 if( !pa_thread->b_wait )
246 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
247 vlc_mutex_unlock( &pa_thread->lock_wait );
248 pa_thread->b_wait = false;
250 if( pa_thread->b_error )
252 msg_Err( p_aout, "PORTAUDIO thread failed" );
261 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
263 msg_Err( p_aout, "cannot open portaudio device" );
268 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
270 msg_Err( p_aout, "cannot open portaudio device" );
278 /*****************************************************************************
279 * Close: close the audio device
280 *****************************************************************************/
281 static void Close ( vlc_object_t *p_this )
283 aout_instance_t *p_aout = (aout_instance_t *)p_this;
284 aout_sys_t *p_sys = p_aout->output.p_sys;
286 msg_Dbg( p_aout, "closing portaudio");
288 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
290 /* Signal end of stream */
291 vlc_mutex_lock( &pa_thread->lock_signal );
292 pa_thread->b_signal = true;
293 vlc_cond_signal( &pa_thread->signal );
294 vlc_mutex_unlock( &pa_thread->lock_signal );
296 /* Wait until thread is ready */
297 vlc_mutex_lock( &pa_thread->lock_wait );
298 if( !pa_thread->b_wait )
299 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
300 vlc_mutex_unlock( &pa_thread->lock_wait );
301 pa_thread->b_wait = false;
305 int i_err = Pa_StopStream( p_sys->p_stream );
306 if( i_err != paNoError )
308 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
309 Pa_GetErrorText( i_err ) );
311 i_err = Pa_CloseStream( p_sys->p_stream );
312 if( i_err != paNoError )
314 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
315 Pa_GetErrorText( i_err ) );
318 i_err = Pa_Terminate();
319 if( i_err != paNoError )
321 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
322 Pa_GetErrorText( i_err ) );
327 msg_Dbg( p_aout, "portaudio closed");
331 static int PAOpenDevice( aout_instance_t *p_aout )
333 aout_sys_t *p_sys = p_aout->output.p_sys;
334 const PaDeviceInfo *p_pdi;
336 vlc_value_t val, text;
339 /* Initialize portaudio */
340 i_err = Pa_Initialize();
341 if( i_err != paNoError )
343 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
344 i_err, Pa_GetErrorText( i_err ) );
349 p_sys->i_devices = Pa_GetDeviceCount();
350 if( p_sys->i_devices < 0 )
352 i_err = p_sys->i_devices;
353 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
354 Pa_GetErrorText( i_err ) );
359 /* Display all devices info */
360 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
361 for( i = 0; i < p_sys->i_devices; i++ )
363 p_pdi = Pa_GetDeviceInfo( i );
364 msg_Dbg( p_aout, "------------------------------------- #%d", i );
365 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
366 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
367 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
369 msg_Dbg( p_aout, "-------------------------------------" );
371 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
372 if( p_sys->i_device_id >= p_sys->i_devices )
374 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
377 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
379 if( p_sys->deviceInfo->maxOutputChannels < 1 )
381 msg_Err( p_aout, "no channel available" );
385 if( var_Type( p_aout, "audio-device" ) == 0 )
387 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
388 text.psz_string = _("Audio Device");
389 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
391 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
393 val.i_int = AOUT_VAR_MONO;
394 text.psz_string = _("Mono");
395 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
397 msg_Dbg( p_aout, "device supports 1 channel" );
399 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
401 val.i_int = AOUT_VAR_STEREO;
402 text.psz_string = _("Stereo");
403 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
405 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
407 var_Set( p_aout, "audio-device", val );
408 msg_Dbg( p_aout, "device supports 2 channels" );
410 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
412 val.i_int = AOUT_VAR_2F2R;
413 text.psz_string = _("2 Front 2 Rear");
414 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
416 msg_Dbg( p_aout, "device supports 4 channels" );
418 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
420 val.i_int = AOUT_VAR_3F2R;
421 text.psz_string = _("3 Front 2 Rear");
422 var_Change( p_aout, "audio-device",
423 VLC_VAR_ADDCHOICE, &val, &text );
424 msg_Dbg( p_aout, "device supports 5 channels" );
426 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
428 val.i_int = AOUT_VAR_5_1;
429 text.psz_string = _("5.1");
430 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
432 msg_Dbg( p_aout, "device supports 5.1 channels" );
435 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
436 var_SetBool( p_aout, "intf-change", true );
439 /* Audio format is paFloat32 (always supported by portaudio v19) */
440 p_aout->output.output.i_format = VLC_CODEC_FL32;
445 if( ( i_err = Pa_Terminate() ) != paNoError )
447 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
452 static int PAOpenStream( aout_instance_t *p_aout )
454 aout_sys_t *p_sys = p_aout->output.p_sys;
455 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
456 PaStreamParameters paStreamParameters;
458 int i_channels, i_err;
459 uint32_t i_channel_mask;
461 if( var_Get( p_aout, "audio-device", &val ) < 0 )
466 if( val.i_int == AOUT_VAR_5_1 )
468 p_aout->output.output.i_physical_channels
469 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
470 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
473 else if( val.i_int == AOUT_VAR_3F2R )
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;
479 else if( val.i_int == AOUT_VAR_2F2R )
481 p_aout->output.output.i_physical_channels
482 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
483 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
485 else if( val.i_int == AOUT_VAR_MONO )
487 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
491 p_aout->output.output.i_physical_channels
492 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
495 i_channels = aout_FormatNbChannels( &p_aout->output.output );
496 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
497 i_channel_mask = p_aout->output.output.i_physical_channels;
499 /* Calculate the frame size in bytes */
500 p_sys->i_sample_size = 4 * i_channels;
501 p_aout->output.i_nb_samples = FRAME_SIZE;
502 aout_FormatPrepare( &p_aout->output.output );
503 aout_VolumeSoftInit( p_aout );
505 /* Check for channel reordering */
506 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
507 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
508 p_aout->output.p_sys->i_channels = i_channels;
510 p_aout->output.p_sys->b_chan_reorder =
511 aout_CheckChannelReorder( NULL, pi_channels_out,
512 i_channel_mask, i_channels,
513 p_aout->output.p_sys->pi_chan_table );
515 if( p_aout->output.p_sys->b_chan_reorder )
517 msg_Dbg( p_aout, "channel reordering needed" );
520 paStreamParameters.device = p_sys->i_device_id;
521 paStreamParameters.channelCount = i_channels;
522 paStreamParameters.sampleFormat = paFloat32;
523 paStreamParameters.suggestedLatency =
524 p_sys->deviceInfo->defaultLowOutputLatency;
525 paStreamParameters.hostApiSpecificStreamInfo = NULL;
527 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
528 &paStreamParameters, (double)p_aout->output.output.i_rate,
529 FRAME_SIZE, paClipOff, paCallback, p_sys );
530 if( i_err != paNoError )
532 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
533 Pa_GetErrorText( i_err ) );
534 if( i_err == paUnanticipatedHostError )
536 msg_Err( p_aout, "type %d code %ld : %s",
537 paLastHostErrorInfo->hostApiType,
538 paLastHostErrorInfo->errorCode,
539 paLastHostErrorInfo->errorText );
545 i_err = Pa_StartStream( p_sys->p_stream );
546 if( i_err != paNoError )
548 msg_Err( p_aout, "Pa_StartStream() failed" );
549 Pa_CloseStream( p_sys->p_stream );
556 /*****************************************************************************
558 *****************************************************************************/
559 static void Play( aout_instance_t * p_aout )
561 VLC_UNUSED( p_aout );
564 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
565 /*****************************************************************************
566 * PORTAUDIOThread: all interactions with libportaudio.a are handled
567 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
568 *****************************************************************************/
569 static void* PORTAUDIOThread( vlc_object_t *p_this )
571 pa_thread_t *pa_thread = (pa_thread_t*)p_this;
572 aout_instance_t *p_aout;
575 int canc = vlc_savecancel ();
577 while( vlc_object_alive (pa_thread) )
579 /* Wait for start of stream */
580 vlc_mutex_lock( &pa_thread->lock_signal );
581 if( !pa_thread->b_signal )
582 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
583 vlc_mutex_unlock( &pa_thread->lock_signal );
584 pa_thread->b_signal = false;
586 p_aout = pa_thread->p_aout;
587 p_sys = p_aout->output.p_sys;
589 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
591 msg_Err( p_aout, "cannot open portaudio device" );
592 pa_thread->b_error = true;
595 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
597 msg_Err( p_aout, "cannot open portaudio device" );
598 pa_thread->b_error = true;
600 i_err = Pa_Terminate();
601 if( i_err != paNoError )
603 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
604 Pa_GetErrorText( i_err ) );
608 /* Tell the main thread that we are ready */
609 vlc_mutex_lock( &pa_thread->lock_wait );
610 pa_thread->b_wait = true;
611 vlc_cond_signal( &pa_thread->wait );
612 vlc_mutex_unlock( &pa_thread->lock_wait );
614 /* Wait for end of stream */
615 vlc_mutex_lock( &pa_thread->lock_signal );
616 if( !pa_thread->b_signal )
617 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
618 vlc_mutex_unlock( &pa_thread->lock_signal );
619 pa_thread->b_signal = false;
621 if( pa_thread->b_error ) continue;
623 i_err = Pa_StopStream( p_sys->p_stream );
624 if( i_err != paNoError )
626 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
627 Pa_GetErrorText( i_err ) );
629 i_err = Pa_CloseStream( p_sys->p_stream );
630 if( i_err != paNoError )
632 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
633 Pa_GetErrorText( i_err ) );
635 i_err = Pa_Terminate();
636 if( i_err != paNoError )
638 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
639 Pa_GetErrorText( i_err ) );
642 /* Tell the main thread that we are ready */
643 vlc_mutex_lock( &pa_thread->lock_wait );
644 pa_thread->b_wait = true;
645 vlc_cond_signal( &pa_thread->wait );
646 vlc_mutex_unlock( &pa_thread->lock_wait );
648 vlc_restorecancel (canc);