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 vlc_bool_t 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 vlc_bool_t 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, VLC_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, VLC_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 p_aout->p_libvlc->pf_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 p_aout->p_libvlc->pf_memset( outputBuffer, 0,
164 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 msg_Err( p_aout, "out of memory" );
188 p_sys->p_aout = p_aout;
190 p_aout->output.p_sys = p_sys;
191 p_aout->output.pf_play = Play;
193 /* Retrieve output device id from config */
194 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
195 var_Get( p_aout, "portaudio-device", &val );
196 p_sys->i_device_id = val.i_int;
198 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
202 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
204 msg_Err( p_aout, "cannot open portaudio device" );
209 /* Close device for now. We'll re-open it later on */
210 if( ( i_err = Pa_Terminate() ) != paNoError )
212 msg_Err( p_aout, "closing the device returned %d", i_err );
217 /* Now we need to setup our DirectSound play notification structure */
218 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
219 pa_thread->p_aout = p_aout;
220 pa_thread->b_error = VLC_FALSE;
221 vlc_mutex_init( p_aout, &pa_thread->lock_wait );
222 vlc_cond_init( p_aout, &pa_thread->wait );
223 pa_thread->b_wait = VLC_FALSE;
224 vlc_mutex_init( p_aout, &pa_thread->lock_signal );
225 vlc_cond_init( p_aout, &pa_thread->signal );
226 pa_thread->b_signal = VLC_FALSE;
228 /* Create PORTAUDIOThread */
229 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
230 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
232 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
238 pa_thread->p_aout = p_aout;
239 pa_thread->b_wait = VLC_FALSE;
240 pa_thread->b_signal = VLC_FALSE;
241 pa_thread->b_error = VLC_FALSE;
244 /* Signal start of stream */
245 vlc_mutex_lock( &pa_thread->lock_signal );
246 pa_thread->b_signal = VLC_TRUE;
247 vlc_cond_signal( &pa_thread->signal );
248 vlc_mutex_unlock( &pa_thread->lock_signal );
250 /* Wait until thread is ready */
251 vlc_mutex_lock( &pa_thread->lock_wait );
252 if( !pa_thread->b_wait )
253 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
254 vlc_mutex_unlock( &pa_thread->lock_wait );
255 pa_thread->b_wait = VLC_FALSE;
257 if( pa_thread->b_error )
259 msg_Err( p_aout, "PORTAUDIO thread failed" );
268 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
270 msg_Err( p_aout, "cannot open portaudio device" );
275 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
277 msg_Err( p_aout, "cannot open portaudio device" );
285 /*****************************************************************************
286 * Close: close the audio device
287 *****************************************************************************/
288 static void Close ( vlc_object_t *p_this )
290 aout_instance_t *p_aout = (aout_instance_t *)p_this;
291 aout_sys_t *p_sys = p_aout->output.p_sys;
294 msg_Dbg( p_aout, "closing portaudio");
296 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
298 /* Signal end of stream */
299 vlc_mutex_lock( &pa_thread->lock_signal );
300 pa_thread->b_signal = VLC_TRUE;
301 vlc_cond_signal( &pa_thread->signal );
302 vlc_mutex_unlock( &pa_thread->lock_signal );
304 /* Wait until thread is ready */
305 vlc_mutex_lock( &pa_thread->lock_wait );
306 if( !pa_thread->b_wait )
307 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
308 vlc_mutex_unlock( &pa_thread->lock_wait );
309 pa_thread->b_wait = VLC_FALSE;
313 i_err = Pa_StopStream( p_sys->p_stream );
314 if( i_err != paNoError )
316 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
317 Pa_GetErrorText( i_err ) );
319 i_err = Pa_CloseStream( p_sys->p_stream );
320 if( i_err != paNoError )
322 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
323 Pa_GetErrorText( i_err ) );
326 i_err = Pa_Terminate();
327 if( i_err != paNoError )
329 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
330 Pa_GetErrorText( i_err ) );
335 msg_Dbg( p_aout, "portaudio closed");
339 static int PAOpenDevice( aout_instance_t *p_aout )
341 aout_sys_t *p_sys = p_aout->output.p_sys;
342 const PaDeviceInfo *p_pdi;
344 vlc_value_t val, text;
347 /* Initialize portaudio */
348 i_err = Pa_Initialize();
349 if( i_err != paNoError )
351 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
352 i_err, Pa_GetErrorText( i_err ) );
357 p_sys->i_devices = Pa_GetDeviceCount();
358 if( p_sys->i_devices < 0 )
360 i_err = p_sys->i_devices;
361 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
362 Pa_GetErrorText( i_err ) );
367 /* Display all devices info */
368 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
369 for( i = 0; i < p_sys->i_devices; i++ )
371 p_pdi = Pa_GetDeviceInfo( i );
372 msg_Dbg( p_aout, "------------------------------------- #%d", i );
373 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
374 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
375 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
377 msg_Dbg( p_aout, "-------------------------------------" );
379 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
380 if( p_sys->i_device_id >= p_sys->i_devices )
382 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
385 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
387 if( p_sys->deviceInfo->maxOutputChannels < 1 )
389 msg_Err( p_aout, "no channel available" );
393 if( var_Type( p_aout, "audio-device" ) == 0 )
395 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
396 text.psz_string = _("Audio Device");
397 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
399 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
401 val.i_int = AOUT_VAR_MONO;
402 text.psz_string = N_("Mono");
403 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
405 msg_Dbg( p_aout, "device supports 1 channel" );
407 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
409 val.i_int = AOUT_VAR_STEREO;
410 text.psz_string = N_("Stereo");
411 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
413 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
415 var_Set( p_aout, "audio-device", val );
416 msg_Dbg( p_aout, "device supports 2 channels" );
418 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
420 val.i_int = AOUT_VAR_2F2R;
421 text.psz_string = N_("2 Front 2 Rear");
422 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
424 msg_Dbg( p_aout, "device supports 4 channels" );
426 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
428 val.i_int = AOUT_VAR_3F2R;
429 text.psz_string = N_("3 Front 2 Rear");
430 var_Change( p_aout, "audio-device",
431 VLC_VAR_ADDCHOICE, &val, &text );
432 msg_Dbg( p_aout, "device supports 5 channels" );
434 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
436 val.i_int = AOUT_VAR_5_1;
437 text.psz_string = "5.1";
438 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
440 msg_Dbg( p_aout, "device supports 5.1 channels" );
443 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
445 val.b_bool = VLC_TRUE;
446 var_Set( p_aout, "intf-change", val );
449 /* Audio format is paFloat32 (always supported by portaudio v19) */
450 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
455 if( ( i_err = Pa_Terminate() ) != paNoError )
457 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
462 static int PAOpenStream( aout_instance_t *p_aout )
464 aout_sys_t *p_sys = p_aout->output.p_sys;
465 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
466 PaStreamParameters paStreamParameters;
468 int i_channels, i_err;
469 uint32_t i_channel_mask;
471 if( var_Get( p_aout, "audio-device", &val ) < 0 )
476 if( val.i_int == AOUT_VAR_5_1 )
478 p_aout->output.output.i_physical_channels
479 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
480 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
483 else if( val.i_int == AOUT_VAR_3F2R )
485 p_aout->output.output.i_physical_channels
486 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
487 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
489 else if( val.i_int == AOUT_VAR_2F2R )
491 p_aout->output.output.i_physical_channels
492 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
493 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
495 else if( val.i_int == AOUT_VAR_MONO )
497 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
501 p_aout->output.output.i_physical_channels
502 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
505 i_channels = aout_FormatNbChannels( &p_aout->output.output );
506 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
507 i_channel_mask = p_aout->output.output.i_physical_channels;
509 /* Calculate the frame size in bytes */
510 p_sys->i_sample_size = 4 * i_channels;
511 p_aout->output.i_nb_samples = FRAME_SIZE;
512 aout_FormatPrepare( &p_aout->output.output );
513 aout_VolumeSoftInit( p_aout );
515 /* Check for channel reordering */
516 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
517 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
518 p_aout->output.p_sys->i_channels = i_channels;
520 p_aout->output.p_sys->b_chan_reorder =
521 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
522 i_channel_mask, i_channels,
523 p_aout->output.p_sys->pi_chan_table );
525 if( p_aout->output.p_sys->b_chan_reorder )
527 msg_Dbg( p_aout, "channel reordering needed" );
530 paStreamParameters.device = p_sys->i_device_id;
531 paStreamParameters.channelCount = i_channels;
532 paStreamParameters.sampleFormat = paFloat32;
533 paStreamParameters.suggestedLatency =
534 p_sys->deviceInfo->defaultLowOutputLatency;
535 paStreamParameters.hostApiSpecificStreamInfo = NULL;
537 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
538 &paStreamParameters, (double)p_aout->output.output.i_rate,
539 FRAME_SIZE, paClipOff, paCallback, p_sys );
540 if( i_err != paNoError )
542 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
543 Pa_GetErrorText( i_err ) );
544 if( i_err == paUnanticipatedHostError )
546 msg_Err( p_aout, "type %d code %ld : %s",
547 paLastHostErrorInfo->hostApiType,
548 paLastHostErrorInfo->errorCode,
549 paLastHostErrorInfo->errorText );
555 i_err = Pa_StartStream( p_sys->p_stream );
556 if( i_err != paNoError )
558 msg_Err( p_aout, "Pa_StartStream() failed" );
559 Pa_CloseStream( p_sys->p_stream );
566 /*****************************************************************************
568 *****************************************************************************/
569 static void Play( aout_instance_t * p_aout )
573 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
574 /*****************************************************************************
575 * PORTAUDIOThread: all interactions with libportaudio.a are handled
576 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
577 *****************************************************************************/
578 static void PORTAUDIOThread( pa_thread_t *pa_thread )
580 aout_instance_t *p_aout;
584 while( !pa_thread->b_die )
586 /* Wait for start of stream */
587 vlc_mutex_lock( &pa_thread->lock_signal );
588 if( !pa_thread->b_signal )
589 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
590 vlc_mutex_unlock( &pa_thread->lock_signal );
591 pa_thread->b_signal = VLC_FALSE;
593 p_aout = pa_thread->p_aout;
594 p_sys = p_aout->output.p_sys;
596 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
598 msg_Err( p_aout, "cannot open portaudio device" );
599 pa_thread->b_error = VLC_TRUE;
602 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
604 msg_Err( p_aout, "cannot open portaudio device" );
605 pa_thread->b_error = VLC_TRUE;
607 i_err = Pa_Terminate();
608 if( i_err != paNoError )
610 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
611 Pa_GetErrorText( i_err ) );
615 /* Tell the main thread that we are ready */
616 vlc_mutex_lock( &pa_thread->lock_wait );
617 pa_thread->b_wait = VLC_TRUE;
618 vlc_cond_signal( &pa_thread->wait );
619 vlc_mutex_unlock( &pa_thread->lock_wait );
621 /* Wait for end of stream */
622 vlc_mutex_lock( &pa_thread->lock_signal );
623 if( !pa_thread->b_signal )
624 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
625 vlc_mutex_unlock( &pa_thread->lock_signal );
626 pa_thread->b_signal = VLC_FALSE;
628 if( pa_thread->b_error ) continue;
630 i_err = Pa_StopStream( p_sys->p_stream );
631 if( i_err != paNoError )
633 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
634 Pa_GetErrorText( i_err ) );
636 i_err = Pa_CloseStream( p_sys->p_stream );
637 if( i_err != paNoError )
639 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
640 Pa_GetErrorText( i_err ) );
642 i_err = Pa_Terminate();
643 if( i_err != paNoError )
645 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
646 Pa_GetErrorText( i_err ) );
649 /* Tell the main thread that we are ready */
650 vlc_mutex_lock( &pa_thread->lock_wait );
651 pa_thread->b_wait = VLC_TRUE;
652 vlc_cond_signal( &pa_thread->wait );
653 vlc_mutex_unlock( &pa_thread->lock_wait );