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 *****************************************************************************/
35 #include <portaudio.h>
37 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
40 # define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
43 /*****************************************************************************
44 * aout_sys_t: portaudio audio output method descriptor
45 *****************************************************************************/
46 typedef struct pa_thread_t
49 aout_instance_t *p_aout;
52 vlc_mutex_t lock_wait;
55 vlc_mutex_t lock_signal;
62 aout_instance_t *p_aout;
65 PaDeviceIndex i_devices;
67 PaDeviceIndex i_device_id;
68 const PaDeviceInfo *deviceInfo;
70 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
71 int pi_chan_table[AOUT_CHAN_MAX];
72 uint32_t i_channel_mask;
73 uint32_t i_bits_per_sample;
77 static const uint32_t pi_channels_in[] =
78 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
79 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
80 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
81 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
82 static const uint32_t pi_channels_out[] =
83 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
84 AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
85 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
86 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
88 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
89 static vlc_bool_t b_init = 0;
90 static pa_thread_t *pa_thread;
91 static void PORTAUDIOThread( pa_thread_t * );
94 /*****************************************************************************
96 *****************************************************************************/
97 static int Open ( vlc_object_t * );
98 static void Close ( vlc_object_t * );
99 static void Play ( aout_instance_t * );
101 static int PAOpenDevice( aout_instance_t * );
102 static int PAOpenStream( aout_instance_t * );
104 /*****************************************************************************
106 *****************************************************************************/
107 #define DEVICE_TEXT N_("Output device")
108 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
111 set_shortname( "PortAudio" );
112 set_description( _("PORTAUDIO audio output") );
113 set_category( CAT_AUDIO );
114 set_subcategory( SUBCAT_AUDIO_AOUT );
115 add_integer( "portaudio-device", 0, NULL,
116 DEVICE_TEXT, DEVICE_LONGTEXT, VLC_FALSE );
117 set_capability( "audio output", 0 );
118 set_callbacks( Open, Close );
121 /* This routine will be called by the PortAudio engine when audio is needed.
122 * It may called at interrupt level on some machines so don't do anything
123 * that could mess up the system like calling malloc() or free().
125 static int paCallback( const void *inputBuffer, void *outputBuffer,
126 unsigned long framesPerBuffer,
127 const PaStreamCallbackTimeInfo *paDate,
128 PaStreamCallbackFlags statusFlags, void *p_cookie )
130 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
131 aout_instance_t *p_aout = p_sys->p_aout;
132 aout_buffer_t *p_buffer;
135 out_date = mdate() + (mtime_t) ( 1000000 *
136 ( paDate->outputBufferDacTime - paDate->currentTime ) );
137 p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
139 if ( p_buffer != NULL )
141 if( p_sys->b_chan_reorder )
143 /* Do the channel reordering here */
144 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
145 p_sys->i_channels, p_sys->pi_chan_table,
146 p_sys->i_bits_per_sample );
148 p_aout->p_libvlc->pf_memcpy( outputBuffer, p_buffer->p_buffer,
149 framesPerBuffer * p_sys->i_sample_size );
150 /* aout_BufferFree may be dangereous here, but then so is
151 * aout_OutputNextBuffer (calls aout_BufferFree internally).
152 * one solution would be to link the no longer useful buffers
153 * in a second fifo (in aout_OutputNextBuffer too) and to
154 * wait until we are in Play to do the actual free.
156 aout_BufferFree( p_buffer );
159 /* Audio output buffer shortage -> stop the fill process and wait */
161 p_aout->p_libvlc->pf_memset( outputBuffer, 0,
162 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;
177 msg_Dbg( p_aout, "entering Open()");
179 /* Allocate p_sys structure */
180 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
183 msg_Err( p_aout, "out of memory" );
186 p_sys->p_aout = p_aout;
188 p_aout->output.p_sys = p_sys;
189 p_aout->output.pf_play = Play;
191 /* Retrieve output device id from config */
192 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
193 var_Get( p_aout, "portaudio-device", &val );
194 p_sys->i_device_id = val.i_int;
196 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
200 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
202 msg_Err( p_aout, "cannot open portaudio device" );
207 /* Close device for now. We'll re-open it later on */
208 if( ( i_err = Pa_Terminate() ) != paNoError )
210 msg_Err( p_aout, "closing the device returned %d", i_err );
215 /* Now we need to setup our DirectSound play notification structure */
216 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
217 pa_thread->p_aout = p_aout;
218 pa_thread->b_error = VLC_FALSE;
219 vlc_mutex_init( p_aout, &pa_thread->lock_wait );
220 vlc_cond_init( p_aout, &pa_thread->wait );
221 pa_thread->b_wait = VLC_FALSE;
222 vlc_mutex_init( p_aout, &pa_thread->lock_signal );
223 vlc_cond_init( p_aout, &pa_thread->signal );
224 pa_thread->b_signal = VLC_FALSE;
226 /* Create PORTAUDIOThread */
227 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
228 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
230 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
236 pa_thread->p_aout = p_aout;
237 pa_thread->b_wait = VLC_FALSE;
238 pa_thread->b_signal = VLC_FALSE;
239 pa_thread->b_error = VLC_FALSE;
242 /* Signal start of stream */
243 vlc_mutex_lock( &pa_thread->lock_signal );
244 pa_thread->b_signal = VLC_TRUE;
245 vlc_cond_signal( &pa_thread->signal );
246 vlc_mutex_unlock( &pa_thread->lock_signal );
248 /* Wait until thread is ready */
249 vlc_mutex_lock( &pa_thread->lock_wait );
250 if( !pa_thread->b_wait )
251 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
252 vlc_mutex_unlock( &pa_thread->lock_wait );
253 pa_thread->b_wait = VLC_FALSE;
255 if( pa_thread->b_error )
257 msg_Err( p_aout, "PORTAUDIO thread failed" );
266 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
268 msg_Err( p_aout, "cannot open portaudio device" );
273 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
275 msg_Err( p_aout, "cannot open portaudio device" );
283 /*****************************************************************************
284 * Close: close the audio device
285 *****************************************************************************/
286 static void Close ( vlc_object_t *p_this )
288 aout_instance_t *p_aout = (aout_instance_t *)p_this;
289 aout_sys_t *p_sys = p_aout->output.p_sys;
292 msg_Dbg( p_aout, "closing portaudio");
294 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
296 /* Signal end of stream */
297 vlc_mutex_lock( &pa_thread->lock_signal );
298 pa_thread->b_signal = VLC_TRUE;
299 vlc_cond_signal( &pa_thread->signal );
300 vlc_mutex_unlock( &pa_thread->lock_signal );
302 /* Wait until thread is ready */
303 vlc_mutex_lock( &pa_thread->lock_wait );
304 if( !pa_thread->b_wait )
305 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
306 vlc_mutex_unlock( &pa_thread->lock_wait );
307 pa_thread->b_wait = VLC_FALSE;
311 i_err = Pa_StopStream( p_sys->p_stream );
312 if( i_err != paNoError )
314 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
315 Pa_GetErrorText( i_err ) );
317 i_err = Pa_CloseStream( p_sys->p_stream );
318 if( i_err != paNoError )
320 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
321 Pa_GetErrorText( i_err ) );
324 i_err = Pa_Terminate();
325 if( i_err != paNoError )
327 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
328 Pa_GetErrorText( i_err ) );
333 msg_Dbg( p_aout, "portaudio closed");
337 static int PAOpenDevice( aout_instance_t *p_aout )
339 aout_sys_t *p_sys = p_aout->output.p_sys;
340 const PaDeviceInfo *p_pdi;
342 vlc_value_t val, text;
345 /* Initialize portaudio */
346 i_err = Pa_Initialize();
347 if( i_err != paNoError )
349 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
350 i_err, Pa_GetErrorText( i_err ) );
355 p_sys->i_devices = Pa_GetDeviceCount();
356 if( p_sys->i_devices < 0 )
358 i_err = p_sys->i_devices;
359 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
360 Pa_GetErrorText( i_err ) );
365 /* Display all devices info */
366 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
367 for( i = 0; i < p_sys->i_devices; i++ )
369 p_pdi = Pa_GetDeviceInfo( i );
370 msg_Dbg( p_aout, "------------------------------------- #%d", i );
371 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
372 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
373 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
375 msg_Dbg( p_aout, "-------------------------------------" );
377 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
378 if( p_sys->i_device_id >= p_sys->i_devices )
380 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
383 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
385 if( p_sys->deviceInfo->maxOutputChannels < 1 )
387 msg_Err( p_aout, "no channel available" );
391 if( var_Type( p_aout, "audio-device" ) == 0 )
393 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
394 text.psz_string = _("Audio Device");
395 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
397 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
399 val.i_int = AOUT_VAR_MONO;
400 text.psz_string = N_("Mono");
401 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
403 msg_Dbg( p_aout, "device supports 1 channel" );
405 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
407 val.i_int = AOUT_VAR_STEREO;
408 text.psz_string = N_("Stereo");
409 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
411 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
413 var_Set( p_aout, "audio-device", val );
414 msg_Dbg( p_aout, "device supports 2 channels" );
416 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
418 val.i_int = AOUT_VAR_2F2R;
419 text.psz_string = N_("2 Front 2 Rear");
420 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
422 msg_Dbg( p_aout, "device supports 4 channels" );
424 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
426 val.i_int = AOUT_VAR_3F2R;
427 text.psz_string = N_("3 Front 2 Rear");
428 var_Change( p_aout, "audio-device",
429 VLC_VAR_ADDCHOICE, &val, &text );
430 msg_Dbg( p_aout, "device supports 5 channels" );
432 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
434 val.i_int = AOUT_VAR_5_1;
435 text.psz_string = "5.1";
436 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
438 msg_Dbg( p_aout, "device supports 5.1 channels" );
441 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
443 val.b_bool = VLC_TRUE;
444 var_Set( p_aout, "intf-change", val );
447 /* Audio format is paFloat32 (always supported by portaudio v19) */
448 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
453 if( ( i_err = Pa_Terminate() ) != paNoError )
455 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
460 static int PAOpenStream( aout_instance_t *p_aout )
462 aout_sys_t *p_sys = p_aout->output.p_sys;
463 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
464 PaStreamParameters paStreamParameters;
466 int i_channels, i_err;
467 uint32_t i_channel_mask;
469 if( var_Get( p_aout, "audio-device", &val ) < 0 )
474 if( val.i_int == AOUT_VAR_5_1 )
476 p_aout->output.output.i_physical_channels
477 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
478 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
481 else if( val.i_int == AOUT_VAR_3F2R )
483 p_aout->output.output.i_physical_channels
484 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
485 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
487 else if( val.i_int == AOUT_VAR_2F2R )
489 p_aout->output.output.i_physical_channels
490 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
491 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
493 else if( val.i_int == AOUT_VAR_MONO )
495 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
499 p_aout->output.output.i_physical_channels
500 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
503 i_channels = aout_FormatNbChannels( &p_aout->output.output );
504 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
505 i_channel_mask = p_aout->output.output.i_physical_channels;
507 /* Calculate the frame size in bytes */
508 p_sys->i_sample_size = 4 * i_channels;
509 p_aout->output.i_nb_samples = FRAME_SIZE;
510 aout_FormatPrepare( &p_aout->output.output );
511 aout_VolumeSoftInit( p_aout );
513 /* Check for channel reordering */
514 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
515 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
516 p_aout->output.p_sys->i_channels = i_channels;
518 p_aout->output.p_sys->b_chan_reorder =
519 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
520 i_channel_mask, i_channels,
521 p_aout->output.p_sys->pi_chan_table );
523 if( p_aout->output.p_sys->b_chan_reorder )
525 msg_Dbg( p_aout, "channel reordering needed" );
528 paStreamParameters.device = p_sys->i_device_id;
529 paStreamParameters.channelCount = i_channels;
530 paStreamParameters.sampleFormat = paFloat32;
531 paStreamParameters.suggestedLatency =
532 p_sys->deviceInfo->defaultLowOutputLatency;
533 paStreamParameters.hostApiSpecificStreamInfo = NULL;
535 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
536 &paStreamParameters, (double)p_aout->output.output.i_rate,
537 FRAME_SIZE, paClipOff, paCallback, p_sys );
538 if( i_err != paNoError )
540 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
541 Pa_GetErrorText( i_err ) );
542 if( i_err == paUnanticipatedHostError )
544 msg_Err( p_aout, "type %d code %ld : %s",
545 paLastHostErrorInfo->hostApiType,
546 paLastHostErrorInfo->errorCode,
547 paLastHostErrorInfo->errorText );
553 i_err = Pa_StartStream( p_sys->p_stream );
554 if( i_err != paNoError )
556 msg_Err( p_aout, "Pa_StartStream() failed" );
557 Pa_CloseStream( p_sys->p_stream );
564 /*****************************************************************************
566 *****************************************************************************/
567 static void Play( aout_instance_t * p_aout )
571 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
572 /*****************************************************************************
573 * PORTAUDIOThread: all interactions with libportaudio.a are handled
574 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
575 *****************************************************************************/
576 static void PORTAUDIOThread( pa_thread_t *pa_thread )
578 aout_instance_t *p_aout;
582 while( !pa_thread->b_die )
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 = VLC_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 = VLC_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 = VLC_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 = VLC_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 = VLC_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 = VLC_TRUE;
650 vlc_cond_signal( &pa_thread->wait );
651 vlc_mutex_unlock( &pa_thread->lock_wait );