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 <portaudio.h>
35 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
38 # define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
41 /*****************************************************************************
42 * aout_sys_t: portaudio audio output method descriptor
43 *****************************************************************************/
44 typedef struct pa_thread_t
47 aout_instance_t *p_aout;
50 vlc_mutex_t lock_wait;
53 vlc_mutex_t lock_signal;
60 aout_instance_t *p_aout;
63 PaDeviceIndex i_devices;
65 PaDeviceIndex i_device_id;
66 const PaDeviceInfo *deviceInfo;
68 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
69 int pi_chan_table[AOUT_CHAN_MAX];
70 uint32_t i_channel_mask;
71 uint32_t i_bits_per_sample;
75 static const uint32_t pi_channels_in[] =
76 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
77 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
78 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
79 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
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,
84 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT, 0 };
86 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
87 static vlc_bool_t b_init = 0;
88 static pa_thread_t *pa_thread;
89 static void PORTAUDIOThread( pa_thread_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( _("PORTAUDIO audio output") );
111 set_category( CAT_AUDIO );
112 set_subcategory( SUBCAT_AUDIO_AOUT );
113 add_integer( "portaudio-device", 0, NULL,
114 DEVICE_TEXT, DEVICE_LONGTEXT, VLC_FALSE );
115 set_capability( "audio output", 0 );
116 set_callbacks( Open, Close );
119 /* This routine will be called by the PortAudio engine when audio is needed.
120 * It may called at interrupt level on some machines so don't do anything
121 * that could mess up the system like calling malloc() or free().
123 static int paCallback( const void *inputBuffer, void *outputBuffer,
124 unsigned long framesPerBuffer,
125 const PaStreamCallbackTimeInfo *paDate,
126 PaStreamCallbackFlags statusFlags, void *p_cookie )
128 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
129 aout_instance_t *p_aout = p_sys->p_aout;
130 aout_buffer_t *p_buffer;
133 out_date = mdate() + (mtime_t) ( 1000000 *
134 ( paDate->outputBufferDacTime - paDate->currentTime ) );
135 p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
137 if ( p_buffer != NULL )
139 if( p_sys->b_chan_reorder )
141 /* Do the channel reordering here */
142 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
143 p_sys->i_channels, p_sys->pi_chan_table,
144 p_sys->i_bits_per_sample );
146 p_aout->p_libvlc->pf_memcpy( outputBuffer, p_buffer->p_buffer,
147 framesPerBuffer * p_sys->i_sample_size );
148 /* aout_BufferFree may be dangereous here, but then so is
149 * aout_OutputNextBuffer (calls aout_BufferFree internally).
150 * one solution would be to link the no longer useful buffers
151 * in a second fifo (in aout_OutputNextBuffer too) and to
152 * wait until we are in Play to do the actual free.
154 aout_BufferFree( p_buffer );
157 /* Audio output buffer shortage -> stop the fill process and wait */
159 p_aout->p_libvlc->pf_memset( outputBuffer, 0,
160 framesPerBuffer * p_sys->i_sample_size );
165 /*****************************************************************************
166 * Open: open the audio device
167 *****************************************************************************/
168 static int Open( vlc_object_t * p_this )
170 aout_instance_t *p_aout = (aout_instance_t *)p_this;
171 struct aout_sys_t * p_sys;
175 msg_Dbg( p_aout, "entering Open()");
177 /* Allocate p_sys structure */
178 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
181 msg_Err( p_aout, "out of memory" );
184 p_sys->p_aout = p_aout;
186 p_aout->output.p_sys = p_sys;
187 p_aout->output.pf_play = Play;
189 /* Retrieve output device id from config */
190 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
191 var_Get( p_aout, "portaudio-device", &val );
192 p_sys->i_device_id = val.i_int;
194 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
198 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
200 msg_Err( p_aout, "cannot open portaudio device" );
205 /* Close device for now. We'll re-open it later on */
206 if( ( i_err = Pa_Terminate() ) != paNoError )
208 msg_Err( p_aout, "closing the device returned %d", i_err );
213 /* Now we need to setup our DirectSound play notification structure */
214 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
215 pa_thread->p_aout = p_aout;
216 pa_thread->b_error = VLC_FALSE;
217 vlc_mutex_init( p_aout, &pa_thread->lock_wait );
218 vlc_cond_init( p_aout, &pa_thread->wait );
219 pa_thread->b_wait = VLC_FALSE;
220 vlc_mutex_init( p_aout, &pa_thread->lock_signal );
221 vlc_cond_init( p_aout, &pa_thread->signal );
222 pa_thread->b_signal = VLC_FALSE;
224 /* Create PORTAUDIOThread */
225 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
226 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
228 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
234 pa_thread->p_aout = p_aout;
235 pa_thread->b_wait = VLC_FALSE;
236 pa_thread->b_signal = VLC_FALSE;
237 pa_thread->b_error = VLC_FALSE;
240 /* Signal start of stream */
241 vlc_mutex_lock( &pa_thread->lock_signal );
242 pa_thread->b_signal = VLC_TRUE;
243 vlc_cond_signal( &pa_thread->signal );
244 vlc_mutex_unlock( &pa_thread->lock_signal );
246 /* Wait until thread is ready */
247 vlc_mutex_lock( &pa_thread->lock_wait );
248 if( !pa_thread->b_wait )
249 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
250 vlc_mutex_unlock( &pa_thread->lock_wait );
251 pa_thread->b_wait = VLC_FALSE;
253 if( pa_thread->b_error )
255 msg_Err( p_aout, "PORTAUDIO thread failed" );
264 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
266 msg_Err( p_aout, "cannot open portaudio device" );
271 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
273 msg_Err( p_aout, "cannot open portaudio device" );
281 /*****************************************************************************
282 * Close: close the audio device
283 *****************************************************************************/
284 static void Close ( vlc_object_t *p_this )
286 aout_instance_t *p_aout = (aout_instance_t *)p_this;
287 aout_sys_t *p_sys = p_aout->output.p_sys;
290 msg_Dbg( p_aout, "closing portaudio");
292 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
294 /* Signal end of stream */
295 vlc_mutex_lock( &pa_thread->lock_signal );
296 pa_thread->b_signal = VLC_TRUE;
297 vlc_cond_signal( &pa_thread->signal );
298 vlc_mutex_unlock( &pa_thread->lock_signal );
300 /* Wait until thread is ready */
301 vlc_mutex_lock( &pa_thread->lock_wait );
302 if( !pa_thread->b_wait )
303 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
304 vlc_mutex_unlock( &pa_thread->lock_wait );
305 pa_thread->b_wait = VLC_FALSE;
309 i_err = Pa_StopStream( p_sys->p_stream );
310 if( i_err != paNoError )
312 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
313 Pa_GetErrorText( i_err ) );
315 i_err = Pa_CloseStream( p_sys->p_stream );
316 if( i_err != paNoError )
318 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
319 Pa_GetErrorText( i_err ) );
322 i_err = Pa_Terminate();
323 if( i_err != paNoError )
325 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
326 Pa_GetErrorText( i_err ) );
331 msg_Dbg( p_aout, "portaudio closed");
335 static int PAOpenDevice( aout_instance_t *p_aout )
337 aout_sys_t *p_sys = p_aout->output.p_sys;
338 const PaDeviceInfo *p_pdi;
340 vlc_value_t val, text;
343 /* Initialize portaudio */
344 i_err = Pa_Initialize();
345 if( i_err != paNoError )
347 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
348 i_err, Pa_GetErrorText( i_err ) );
353 p_sys->i_devices = Pa_GetDeviceCount();
354 if( p_sys->i_devices < 0 )
356 i_err = p_sys->i_devices;
357 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
358 Pa_GetErrorText( i_err ) );
363 /* Display all devices info */
364 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
365 for( i = 0; i < p_sys->i_devices; i++ )
367 p_pdi = Pa_GetDeviceInfo( i );
368 msg_Dbg( p_aout, "------------------------------------- #%d", i );
369 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
370 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
371 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
373 msg_Dbg( p_aout, "-------------------------------------" );
375 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
376 if( p_sys->i_device_id >= p_sys->i_devices )
378 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
381 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
383 if( p_sys->deviceInfo->maxOutputChannels < 1 )
385 msg_Err( p_aout, "no channel available" );
389 if( var_Type( p_aout, "audio-device" ) == 0 )
391 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
392 text.psz_string = _("Audio Device");
393 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
395 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
397 val.i_int = AOUT_VAR_MONO;
398 text.psz_string = N_("Mono");
399 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
401 msg_Dbg( p_aout, "device supports 1 channel" );
403 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
405 val.i_int = AOUT_VAR_STEREO;
406 text.psz_string = N_("Stereo");
407 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
409 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
411 var_Set( p_aout, "audio-device", val );
412 msg_Dbg( p_aout, "device supports 2 channels" );
414 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
416 val.i_int = AOUT_VAR_2F2R;
417 text.psz_string = N_("2 Front 2 Rear");
418 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
420 msg_Dbg( p_aout, "device supports 4 channels" );
422 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
424 val.i_int = AOUT_VAR_3F2R;
425 text.psz_string = N_("3 Front 2 Rear");
426 var_Change( p_aout, "audio-device",
427 VLC_VAR_ADDCHOICE, &val, &text );
428 msg_Dbg( p_aout, "device supports 5 channels" );
430 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
432 val.i_int = AOUT_VAR_5_1;
433 text.psz_string = "5.1";
434 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
436 msg_Dbg( p_aout, "device supports 5.1 channels" );
439 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
441 val.b_bool = VLC_TRUE;
442 var_Set( p_aout, "intf-change", val );
445 /* Audio format is paFloat32 (always supported by portaudio v19) */
446 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
451 if( ( i_err = Pa_Terminate() ) != paNoError )
453 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
458 static int PAOpenStream( aout_instance_t *p_aout )
460 aout_sys_t *p_sys = p_aout->output.p_sys;
461 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
462 PaStreamParameters paStreamParameters;
464 int i_channels, i_err;
465 uint32_t i_channel_mask;
467 if( var_Get( p_aout, "audio-device", &val ) < 0 )
472 if( val.i_int == AOUT_VAR_5_1 )
474 p_aout->output.output.i_physical_channels
475 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
476 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
479 else if( val.i_int == AOUT_VAR_3F2R )
481 p_aout->output.output.i_physical_channels
482 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
483 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
485 else if( val.i_int == AOUT_VAR_2F2R )
487 p_aout->output.output.i_physical_channels
488 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
489 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
491 else if( val.i_int == AOUT_VAR_MONO )
493 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
497 p_aout->output.output.i_physical_channels
498 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
501 i_channels = aout_FormatNbChannels( &p_aout->output.output );
502 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
503 i_channel_mask = p_aout->output.output.i_physical_channels;
505 /* Calculate the frame size in bytes */
506 p_sys->i_sample_size = 4 * i_channels;
507 p_aout->output.i_nb_samples = FRAME_SIZE;
508 aout_FormatPrepare( &p_aout->output.output );
509 aout_VolumeSoftInit( p_aout );
511 /* Check for channel reordering */
512 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
513 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
514 p_aout->output.p_sys->i_channels = i_channels;
516 p_aout->output.p_sys->b_chan_reorder =
517 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
518 i_channel_mask, i_channels,
519 p_aout->output.p_sys->pi_chan_table );
521 if( p_aout->output.p_sys->b_chan_reorder )
523 msg_Dbg( p_aout, "channel reordering needed" );
526 paStreamParameters.device = p_sys->i_device_id;
527 paStreamParameters.channelCount = i_channels;
528 paStreamParameters.sampleFormat = paFloat32;
529 paStreamParameters.suggestedLatency =
530 p_sys->deviceInfo->defaultLowOutputLatency;
531 paStreamParameters.hostApiSpecificStreamInfo = NULL;
533 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
534 &paStreamParameters, (double)p_aout->output.output.i_rate,
535 FRAME_SIZE, paClipOff, paCallback, p_sys );
536 if( i_err != paNoError )
538 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
539 Pa_GetErrorText( i_err ) );
540 if( i_err == paUnanticipatedHostError )
542 msg_Err( p_aout, "type %d code %ld : %s",
543 paLastHostErrorInfo->hostApiType,
544 paLastHostErrorInfo->errorCode,
545 paLastHostErrorInfo->errorText );
551 i_err = Pa_StartStream( p_sys->p_stream );
552 if( i_err != paNoError )
554 msg_Err( p_aout, "Pa_StartStream() failed" );
555 Pa_CloseStream( p_sys->p_stream );
562 /*****************************************************************************
564 *****************************************************************************/
565 static void Play( aout_instance_t * p_aout )
569 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
570 /*****************************************************************************
571 * PORTAUDIOThread: all interactions with libportaudio.a are handled
572 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
573 *****************************************************************************/
574 static void PORTAUDIOThread( pa_thread_t *pa_thread )
576 aout_instance_t *p_aout;
580 while( !pa_thread->b_die )
582 /* Wait for start of stream */
583 vlc_mutex_lock( &pa_thread->lock_signal );
584 if( !pa_thread->b_signal )
585 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
586 vlc_mutex_unlock( &pa_thread->lock_signal );
587 pa_thread->b_signal = VLC_FALSE;
589 p_aout = pa_thread->p_aout;
590 p_sys = p_aout->output.p_sys;
592 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
594 msg_Err( p_aout, "cannot open portaudio device" );
595 pa_thread->b_error = VLC_TRUE;
598 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
600 msg_Err( p_aout, "cannot open portaudio device" );
601 pa_thread->b_error = VLC_TRUE;
603 i_err = Pa_Terminate();
604 if( i_err != paNoError )
606 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
607 Pa_GetErrorText( i_err ) );
611 /* Tell the main thread that we are ready */
612 vlc_mutex_lock( &pa_thread->lock_wait );
613 pa_thread->b_wait = VLC_TRUE;
614 vlc_cond_signal( &pa_thread->wait );
615 vlc_mutex_unlock( &pa_thread->lock_wait );
617 /* Wait for end of stream */
618 vlc_mutex_lock( &pa_thread->lock_signal );
619 if( !pa_thread->b_signal )
620 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
621 vlc_mutex_unlock( &pa_thread->lock_signal );
622 pa_thread->b_signal = VLC_FALSE;
624 if( pa_thread->b_error ) continue;
626 i_err = Pa_StopStream( p_sys->p_stream );
627 if( i_err != paNoError )
629 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
630 Pa_GetErrorText( i_err ) );
632 i_err = Pa_CloseStream( p_sys->p_stream );
633 if( i_err != paNoError )
635 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
636 Pa_GetErrorText( i_err ) );
638 i_err = Pa_Terminate();
639 if( i_err != paNoError )
641 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
642 Pa_GetErrorText( i_err ) );
645 /* Tell the main thread that we are ready */
646 vlc_mutex_lock( &pa_thread->lock_wait );
647 pa_thread->b_wait = VLC_TRUE;
648 vlc_cond_signal( &pa_thread->wait );
649 vlc_mutex_unlock( &pa_thread->lock_wait );