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 *****************************************************************************/
37 #include <portaudio.h>
39 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
42 # define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
45 /*****************************************************************************
46 * aout_sys_t: portaudio audio output method descriptor
47 *****************************************************************************/
48 typedef struct pa_thread_t
51 aout_instance_t *p_aout;
54 vlc_mutex_t lock_wait;
57 vlc_mutex_t lock_signal;
64 aout_instance_t *p_aout;
67 PaDeviceIndex i_devices;
69 PaDeviceIndex i_device_id;
70 const PaDeviceInfo *deviceInfo;
72 bool b_chan_reorder; /* do we need channel reordering */
73 int pi_chan_table[AOUT_CHAN_MAX];
74 uint32_t i_channel_mask;
75 uint32_t i_bits_per_sample;
79 static const uint32_t pi_channels_in[] =
80 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
81 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
82 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
83 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
84 static const uint32_t pi_channels_out[] =
85 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
86 AOUT_CHAN_CENTER, AOUT_CHAN_LFE,
87 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
88 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
90 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
91 static bool b_init = 0;
92 static pa_thread_t *pa_thread;
93 static void PORTAUDIOThread( pa_thread_t * );
96 /*****************************************************************************
98 *****************************************************************************/
99 static int Open ( vlc_object_t * );
100 static void Close ( vlc_object_t * );
101 static void Play ( aout_instance_t * );
103 static int PAOpenDevice( aout_instance_t * );
104 static int PAOpenStream( aout_instance_t * );
106 /*****************************************************************************
108 *****************************************************************************/
109 #define DEVICE_TEXT N_("Output device")
110 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
113 set_shortname( "PortAudio" );
114 set_description( _("PORTAUDIO audio output") );
115 set_category( CAT_AUDIO );
116 set_subcategory( SUBCAT_AUDIO_AOUT );
117 add_integer( "portaudio-device", 0, NULL,
118 DEVICE_TEXT, DEVICE_LONGTEXT, false );
119 set_capability( "audio output", 0 );
120 set_callbacks( Open, Close );
123 /* This routine will be called by the PortAudio engine when audio is needed.
124 * It may called at interrupt level on some machines so don't do anything
125 * that could mess up the system like calling malloc() or free().
127 static int paCallback( const void *inputBuffer, void *outputBuffer,
128 unsigned long framesPerBuffer,
129 const PaStreamCallbackTimeInfo *paDate,
130 PaStreamCallbackFlags statusFlags, void *p_cookie )
132 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
133 aout_instance_t *p_aout = p_sys->p_aout;
134 aout_buffer_t *p_buffer;
137 out_date = mdate() + (mtime_t) ( 1000000 *
138 ( paDate->outputBufferDacTime - paDate->currentTime ) );
139 p_buffer = aout_OutputNextBuffer( p_aout, out_date, true );
141 if ( p_buffer != NULL )
143 if( p_sys->b_chan_reorder )
145 /* Do the channel reordering here */
146 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
147 p_sys->i_channels, p_sys->pi_chan_table,
148 p_sys->i_bits_per_sample );
150 vlc_memcpy( outputBuffer, p_buffer->p_buffer,
151 framesPerBuffer * p_sys->i_sample_size );
152 /* aout_BufferFree may be dangereous here, but then so is
153 * aout_OutputNextBuffer (calls aout_BufferFree internally).
154 * one solution would be to link the no longer useful buffers
155 * in a second fifo (in aout_OutputNextBuffer too) and to
156 * wait until we are in Play to do the actual free.
158 aout_BufferFree( p_buffer );
161 /* Audio output buffer shortage -> stop the fill process and wait */
163 vlc_memset( outputBuffer, 0, framesPerBuffer * p_sys->i_sample_size );
168 /*****************************************************************************
169 * Open: open the audio device
170 *****************************************************************************/
171 static int Open( vlc_object_t * p_this )
173 aout_instance_t *p_aout = (aout_instance_t *)p_this;
174 struct aout_sys_t * p_sys;
178 msg_Dbg( p_aout, "entering Open()");
180 /* Allocate p_sys structure */
181 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
184 msg_Err( p_aout, "out of memory" );
187 p_sys->p_aout = p_aout;
189 p_aout->output.p_sys = p_sys;
190 p_aout->output.pf_play = Play;
192 /* Retrieve output device id from config */
193 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
194 var_Get( p_aout, "portaudio-device", &val );
195 p_sys->i_device_id = val.i_int;
197 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
201 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
203 msg_Err( p_aout, "cannot open portaudio device" );
208 /* Close device for now. We'll re-open it later on */
209 if( ( i_err = Pa_Terminate() ) != paNoError )
211 msg_Err( p_aout, "closing the device returned %d", i_err );
216 /* Now we need to setup our DirectSound play notification structure */
217 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
218 pa_thread->p_aout = p_aout;
219 pa_thread->b_error = false;
220 vlc_mutex_init( &pa_thread->lock_wait );
221 vlc_cond_init( p_aout, &pa_thread->wait );
222 pa_thread->b_wait = false;
223 vlc_mutex_init( &pa_thread->lock_signal );
224 vlc_cond_init( p_aout, &pa_thread->signal );
225 pa_thread->b_signal = false;
227 /* Create PORTAUDIOThread */
228 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
229 VLC_THREAD_PRIORITY_OUTPUT, false ) )
231 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
237 pa_thread->p_aout = p_aout;
238 pa_thread->b_wait = false;
239 pa_thread->b_signal = false;
240 pa_thread->b_error = false;
243 /* Signal start of stream */
244 vlc_mutex_lock( &pa_thread->lock_signal );
245 pa_thread->b_signal = true;
246 vlc_cond_signal( &pa_thread->signal );
247 vlc_mutex_unlock( &pa_thread->lock_signal );
249 /* Wait until thread is ready */
250 vlc_mutex_lock( &pa_thread->lock_wait );
251 if( !pa_thread->b_wait )
252 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
253 vlc_mutex_unlock( &pa_thread->lock_wait );
254 pa_thread->b_wait = false;
256 if( pa_thread->b_error )
258 msg_Err( p_aout, "PORTAUDIO thread failed" );
267 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
269 msg_Err( p_aout, "cannot open portaudio device" );
274 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
276 msg_Err( p_aout, "cannot open portaudio device" );
284 /*****************************************************************************
285 * Close: close the audio device
286 *****************************************************************************/
287 static void Close ( vlc_object_t *p_this )
289 aout_instance_t *p_aout = (aout_instance_t *)p_this;
290 aout_sys_t *p_sys = p_aout->output.p_sys;
293 msg_Dbg( p_aout, "closing portaudio");
295 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
297 /* Signal end of stream */
298 vlc_mutex_lock( &pa_thread->lock_signal );
299 pa_thread->b_signal = true;
300 vlc_cond_signal( &pa_thread->signal );
301 vlc_mutex_unlock( &pa_thread->lock_signal );
303 /* Wait until thread is ready */
304 vlc_mutex_lock( &pa_thread->lock_wait );
305 if( !pa_thread->b_wait )
306 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
307 vlc_mutex_unlock( &pa_thread->lock_wait );
308 pa_thread->b_wait = false;
312 i_err = Pa_StopStream( p_sys->p_stream );
313 if( i_err != paNoError )
315 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
316 Pa_GetErrorText( i_err ) );
318 i_err = Pa_CloseStream( p_sys->p_stream );
319 if( i_err != paNoError )
321 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
322 Pa_GetErrorText( i_err ) );
325 i_err = Pa_Terminate();
326 if( i_err != paNoError )
328 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
329 Pa_GetErrorText( i_err ) );
334 msg_Dbg( p_aout, "portaudio closed");
338 static int PAOpenDevice( aout_instance_t *p_aout )
340 aout_sys_t *p_sys = p_aout->output.p_sys;
341 const PaDeviceInfo *p_pdi;
343 vlc_value_t val, text;
346 /* Initialize portaudio */
347 i_err = Pa_Initialize();
348 if( i_err != paNoError )
350 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
351 i_err, Pa_GetErrorText( i_err ) );
356 p_sys->i_devices = Pa_GetDeviceCount();
357 if( p_sys->i_devices < 0 )
359 i_err = p_sys->i_devices;
360 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
361 Pa_GetErrorText( i_err ) );
366 /* Display all devices info */
367 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
368 for( i = 0; i < p_sys->i_devices; i++ )
370 p_pdi = Pa_GetDeviceInfo( i );
371 msg_Dbg( p_aout, "------------------------------------- #%d", i );
372 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
373 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
374 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
376 msg_Dbg( p_aout, "-------------------------------------" );
378 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
379 if( p_sys->i_device_id >= p_sys->i_devices )
381 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
384 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
386 if( p_sys->deviceInfo->maxOutputChannels < 1 )
388 msg_Err( p_aout, "no channel available" );
392 if( var_Type( p_aout, "audio-device" ) == 0 )
394 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
395 text.psz_string = _("Audio Device");
396 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
398 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
400 val.i_int = AOUT_VAR_MONO;
401 text.psz_string = N_("Mono");
402 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
404 msg_Dbg( p_aout, "device supports 1 channel" );
406 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
408 val.i_int = AOUT_VAR_STEREO;
409 text.psz_string = N_("Stereo");
410 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
412 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
414 var_Set( p_aout, "audio-device", val );
415 msg_Dbg( p_aout, "device supports 2 channels" );
417 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
419 val.i_int = AOUT_VAR_2F2R;
420 text.psz_string = N_("2 Front 2 Rear");
421 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
423 msg_Dbg( p_aout, "device supports 4 channels" );
425 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
427 val.i_int = AOUT_VAR_3F2R;
428 text.psz_string = N_("3 Front 2 Rear");
429 var_Change( p_aout, "audio-device",
430 VLC_VAR_ADDCHOICE, &val, &text );
431 msg_Dbg( p_aout, "device supports 5 channels" );
433 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
435 val.i_int = AOUT_VAR_5_1;
436 text.psz_string = "5.1";
437 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
439 msg_Dbg( p_aout, "device supports 5.1 channels" );
442 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
445 var_Set( p_aout, "intf-change", val );
448 /* Audio format is paFloat32 (always supported by portaudio v19) */
449 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
454 if( ( i_err = Pa_Terminate() ) != paNoError )
456 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
461 static int PAOpenStream( aout_instance_t *p_aout )
463 aout_sys_t *p_sys = p_aout->output.p_sys;
464 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
465 PaStreamParameters paStreamParameters;
467 int i_channels, i_err;
468 uint32_t i_channel_mask;
470 if( var_Get( p_aout, "audio-device", &val ) < 0 )
475 if( val.i_int == AOUT_VAR_5_1 )
477 p_aout->output.output.i_physical_channels
478 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
479 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
482 else if( val.i_int == AOUT_VAR_3F2R )
484 p_aout->output.output.i_physical_channels
485 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
486 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
488 else if( val.i_int == AOUT_VAR_2F2R )
490 p_aout->output.output.i_physical_channels
491 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
492 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
494 else if( val.i_int == AOUT_VAR_MONO )
496 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
500 p_aout->output.output.i_physical_channels
501 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
504 i_channels = aout_FormatNbChannels( &p_aout->output.output );
505 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
506 i_channel_mask = p_aout->output.output.i_physical_channels;
508 /* Calculate the frame size in bytes */
509 p_sys->i_sample_size = 4 * i_channels;
510 p_aout->output.i_nb_samples = FRAME_SIZE;
511 aout_FormatPrepare( &p_aout->output.output );
512 aout_VolumeSoftInit( p_aout );
514 /* Check for channel reordering */
515 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
516 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
517 p_aout->output.p_sys->i_channels = i_channels;
519 p_aout->output.p_sys->b_chan_reorder =
520 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
521 i_channel_mask, i_channels,
522 p_aout->output.p_sys->pi_chan_table );
524 if( p_aout->output.p_sys->b_chan_reorder )
526 msg_Dbg( p_aout, "channel reordering needed" );
529 paStreamParameters.device = p_sys->i_device_id;
530 paStreamParameters.channelCount = i_channels;
531 paStreamParameters.sampleFormat = paFloat32;
532 paStreamParameters.suggestedLatency =
533 p_sys->deviceInfo->defaultLowOutputLatency;
534 paStreamParameters.hostApiSpecificStreamInfo = NULL;
536 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
537 &paStreamParameters, (double)p_aout->output.output.i_rate,
538 FRAME_SIZE, paClipOff, paCallback, p_sys );
539 if( i_err != paNoError )
541 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
542 Pa_GetErrorText( i_err ) );
543 if( i_err == paUnanticipatedHostError )
545 msg_Err( p_aout, "type %d code %ld : %s",
546 paLastHostErrorInfo->hostApiType,
547 paLastHostErrorInfo->errorCode,
548 paLastHostErrorInfo->errorText );
554 i_err = Pa_StartStream( p_sys->p_stream );
555 if( i_err != paNoError )
557 msg_Err( p_aout, "Pa_StartStream() failed" );
558 Pa_CloseStream( p_sys->p_stream );
565 /*****************************************************************************
567 *****************************************************************************/
568 static void Play( aout_instance_t * p_aout )
572 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
573 /*****************************************************************************
574 * PORTAUDIOThread: all interactions with libportaudio.a are handled
575 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
576 *****************************************************************************/
577 static void PORTAUDIOThread( pa_thread_t *pa_thread )
579 aout_instance_t *p_aout;
583 while( !pa_thread->b_die )
585 /* Wait for start of stream */
586 vlc_mutex_lock( &pa_thread->lock_signal );
587 if( !pa_thread->b_signal )
588 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
589 vlc_mutex_unlock( &pa_thread->lock_signal );
590 pa_thread->b_signal = false;
592 p_aout = pa_thread->p_aout;
593 p_sys = p_aout->output.p_sys;
595 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
597 msg_Err( p_aout, "cannot open portaudio device" );
598 pa_thread->b_error = true;
601 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
603 msg_Err( p_aout, "cannot open portaudio device" );
604 pa_thread->b_error = true;
606 i_err = Pa_Terminate();
607 if( i_err != paNoError )
609 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
610 Pa_GetErrorText( i_err ) );
614 /* Tell the main thread that we are ready */
615 vlc_mutex_lock( &pa_thread->lock_wait );
616 pa_thread->b_wait = true;
617 vlc_cond_signal( &pa_thread->wait );
618 vlc_mutex_unlock( &pa_thread->lock_wait );
620 /* Wait for end of stream */
621 vlc_mutex_lock( &pa_thread->lock_signal );
622 if( !pa_thread->b_signal )
623 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
624 vlc_mutex_unlock( &pa_thread->lock_signal );
625 pa_thread->b_signal = false;
627 if( pa_thread->b_error ) continue;
629 i_err = Pa_StopStream( p_sys->p_stream );
630 if( i_err != paNoError )
632 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
633 Pa_GetErrorText( i_err ) );
635 i_err = Pa_CloseStream( p_sys->p_stream );
636 if( i_err != paNoError )
638 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
639 Pa_GetErrorText( i_err ) );
641 i_err = Pa_Terminate();
642 if( i_err != paNoError )
644 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
645 Pa_GetErrorText( i_err ) );
648 /* Tell the main thread that we are ready */
649 vlc_mutex_lock( &pa_thread->lock_wait );
650 pa_thread->b_wait = true;
651 vlc_cond_signal( &pa_thread->wait );
652 vlc_mutex_unlock( &pa_thread->lock_wait );