1 /*****************************************************************************
2 * portaudio.c : portaudio (v19) audio output plugin
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <portaudio.h>
35 #include "aout_internal.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_description( N_("PORTAUDIO audio output") );
112 add_integer( "portaudio-device", 0, NULL,
113 DEVICE_TEXT, DEVICE_LONGTEXT, VLC_FALSE );
114 set_capability( "audio output", 0 );
115 set_callbacks( Open, Close );
118 /* This routine will be called by the PortAudio engine when audio is needed.
119 * It may called at interrupt level on some machines so don't do anything
120 * that could mess up the system like calling malloc() or free().
122 static int paCallback( const void *inputBuffer, void *outputBuffer,
123 unsigned long framesPerBuffer,
124 const PaStreamCallbackTimeInfo *paDate,
125 PaStreamCallbackFlags statusFlags, void *p_cookie )
127 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
128 aout_instance_t *p_aout = p_sys->p_aout;
129 aout_buffer_t *p_buffer;
132 out_date = mdate() + (mtime_t) ( 1000000 *
133 ( paDate->outputBufferDacTime - paDate->currentTime ) );
134 p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
136 if ( p_buffer != NULL )
138 if( p_sys->b_chan_reorder )
140 /* Do the channel reordering here */
141 aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
142 p_sys->i_channels, p_sys->pi_chan_table,
143 p_sys->i_bits_per_sample );
145 p_aout->p_vlc->pf_memcpy( outputBuffer, p_buffer->p_buffer,
146 framesPerBuffer * p_sys->i_sample_size );
147 /* aout_BufferFree may be dangereous here, but then so is
148 * aout_OutputNextBuffer (calls aout_BufferFree internally).
149 * one solution would be to link the no longer useful buffers
150 * in a second fifo (in aout_OutputNextBuffer too) and to
151 * wait until we are in Play to do the actual free.
153 aout_BufferFree( p_buffer );
156 /* Audio output buffer shortage -> stop the fill process and wait */
158 p_aout->p_vlc->pf_memset( outputBuffer, 0,
159 framesPerBuffer * p_sys->i_sample_size );
164 /*****************************************************************************
165 * Open: open the audio device
166 *****************************************************************************/
167 static int Open( vlc_object_t * p_this )
169 aout_instance_t *p_aout = (aout_instance_t *)p_this;
170 struct aout_sys_t * p_sys;
174 msg_Dbg( p_aout, "Entering Open()");
176 /* Allocate p_sys structure */
177 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
180 msg_Err( p_aout, "out of memory" );
183 p_sys->p_aout = p_aout;
185 p_aout->output.p_sys = p_sys;
186 p_aout->output.pf_play = Play;
188 /* Retrieve output device id from config */
189 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
190 var_Get( p_aout, "portaudio-device", &val );
191 p_sys->i_device_id = val.i_int;
193 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
197 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
199 msg_Err( p_aout, "cannot open portaudio device" );
204 /* Close device for now. We'll re-open it later on */
205 if( ( i_err = Pa_Terminate() ) != paNoError )
207 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
212 /* Now we need to setup our DirectSound play notification structure */
213 pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
214 pa_thread->p_aout = p_aout;
215 pa_thread->b_error = VLC_FALSE;
216 vlc_mutex_init( p_aout, &pa_thread->lock_wait );
217 vlc_cond_init( p_aout, &pa_thread->wait );
218 pa_thread->b_wait = VLC_FALSE;
219 vlc_mutex_init( p_aout, &pa_thread->lock_signal );
220 vlc_cond_init( p_aout, &pa_thread->signal );
221 pa_thread->b_signal = VLC_FALSE;
223 /* Create PORTAUDIOThread */
224 if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
225 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
227 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
233 pa_thread->p_aout = p_aout;
234 pa_thread->b_wait = VLC_FALSE;
235 pa_thread->b_signal = VLC_FALSE;
236 pa_thread->b_error = VLC_FALSE;
239 /* Signal start of stream */
240 vlc_mutex_lock( &pa_thread->lock_signal );
241 pa_thread->b_signal = VLC_TRUE;
242 vlc_cond_signal( &pa_thread->signal );
243 vlc_mutex_unlock( &pa_thread->lock_signal );
245 /* Wait until thread is ready */
246 vlc_mutex_lock( &pa_thread->lock_wait );
247 if( !pa_thread->b_wait )
248 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
249 vlc_mutex_unlock( &pa_thread->lock_wait );
250 pa_thread->b_wait = VLC_FALSE;
252 if( pa_thread->b_error )
254 msg_Err( p_aout, "PORTAUDIO thread failed" );
263 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
265 msg_Err( p_aout, "cannot open portaudio device" );
270 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
272 msg_Err( p_aout, "cannot open portaudio device" );
280 /*****************************************************************************
281 * Close: close the audio device
282 *****************************************************************************/
283 static void Close ( vlc_object_t *p_this )
285 aout_instance_t *p_aout = (aout_instance_t *)p_this;
286 aout_sys_t *p_sys = p_aout->output.p_sys;
289 msg_Dbg( p_aout, "closing portaudio");
291 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
293 /* Signal end of stream */
294 vlc_mutex_lock( &pa_thread->lock_signal );
295 pa_thread->b_signal = VLC_TRUE;
296 vlc_cond_signal( &pa_thread->signal );
297 vlc_mutex_unlock( &pa_thread->lock_signal );
299 /* Wait until thread is ready */
300 vlc_mutex_lock( &pa_thread->lock_wait );
301 if( !pa_thread->b_wait )
302 vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
303 vlc_mutex_unlock( &pa_thread->lock_wait );
304 pa_thread->b_wait = VLC_FALSE;
308 i_err = Pa_StopStream( p_sys->p_stream );
309 if( i_err != paNoError )
311 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
312 Pa_GetErrorText( i_err ) );
314 i_err = Pa_CloseStream( p_sys->p_stream );
315 if( i_err != paNoError )
317 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
318 Pa_GetErrorText( i_err ) );
321 i_err = Pa_Terminate();
322 if( i_err != paNoError )
324 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
325 Pa_GetErrorText( i_err ) );
330 msg_Dbg( p_aout, "portaudio closed");
334 static int PAOpenDevice( aout_instance_t *p_aout )
336 aout_sys_t *p_sys = p_aout->output.p_sys;
337 const PaDeviceInfo *p_pdi;
339 vlc_value_t val, text;
342 /* Initialize portaudio */
343 i_err = Pa_Initialize();
344 if( i_err != paNoError )
346 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
347 i_err, Pa_GetErrorText( i_err ) );
352 p_sys->i_devices = Pa_GetDeviceCount();
353 if( p_sys->i_devices < 0 )
355 i_err = p_sys->i_devices;
356 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
357 Pa_GetErrorText( i_err ) );
362 /* Display all devices info */
363 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
364 for( i = 0; i < p_sys->i_devices; i++ )
366 p_pdi = Pa_GetDeviceInfo( i );
367 msg_Dbg( p_aout, "------------------------------------- #%d", i );
368 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
369 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
370 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
372 msg_Dbg( p_aout, "-------------------------------------" );
374 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
375 if( p_sys->i_device_id >= p_sys->i_devices )
377 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
380 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
382 if( p_sys->deviceInfo->maxOutputChannels < 1 )
384 msg_Err( p_aout, "no channel available" );
388 if( var_Type( p_aout, "audio-device" ) == 0 )
390 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
391 text.psz_string = _("Audio Device");
392 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
394 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
396 val.i_int = AOUT_VAR_MONO;
397 text.psz_string = N_("Mono");
398 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
400 msg_Dbg( p_aout, "device supports 1 channel" );
402 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
404 val.i_int = AOUT_VAR_STEREO;
405 text.psz_string = N_("Stereo");
406 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
408 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
410 var_Set( p_aout, "audio-device", val );
411 msg_Dbg( p_aout, "device supports 2 channels" );
413 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
415 val.i_int = AOUT_VAR_2F2R;
416 text.psz_string = N_("2 Front 2 Rear");
417 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
419 msg_Dbg( p_aout, "device supports 4 channels" );
421 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
423 val.i_int = AOUT_VAR_3F2R;
424 text.psz_string = N_("3 Front 2 Rear");
425 var_Change( p_aout, "audio-device",
426 VLC_VAR_ADDCHOICE, &val, &text );
427 msg_Dbg( p_aout, "device supports 5 channels" );
429 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
431 val.i_int = AOUT_VAR_5_1;
432 text.psz_string = N_("5.1");
433 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
435 msg_Dbg( p_aout, "device supports 5.1 channels" );
438 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
440 val.b_bool = VLC_TRUE;
441 var_Set( p_aout, "intf-change", val );
444 /* Audio format is paFloat32 (always supported by portaudio v19) */
445 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
450 if( ( i_err = Pa_Terminate() ) != paNoError )
452 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
457 static int PAOpenStream( aout_instance_t *p_aout )
459 aout_sys_t *p_sys = p_aout->output.p_sys;
460 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
461 PaStreamParameters paStreamParameters;
463 int i_channels, i_err;
464 uint32_t i_channel_mask;
466 if( var_Get( p_aout, "audio-device", &val ) < 0 )
471 if( val.i_int == AOUT_VAR_5_1 )
473 p_aout->output.output.i_physical_channels
474 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
475 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
478 else if( val.i_int == AOUT_VAR_3F2R )
480 p_aout->output.output.i_physical_channels
481 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
482 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
484 else if( val.i_int == AOUT_VAR_2F2R )
486 p_aout->output.output.i_physical_channels
487 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
488 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
490 else if( val.i_int == AOUT_VAR_MONO )
492 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
496 p_aout->output.output.i_physical_channels
497 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
500 i_channels = aout_FormatNbChannels( &p_aout->output.output );
501 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
502 i_channel_mask = p_aout->output.output.i_physical_channels;
504 /* Calculate the frame size in bytes */
505 p_sys->i_sample_size = 4 * i_channels;
506 p_aout->output.i_nb_samples = FRAME_SIZE;
507 aout_FormatPrepare( &p_aout->output.output );
508 aout_VolumeSoftInit( p_aout );
510 /* Check for channel reordering */
511 p_aout->output.p_sys->i_channel_mask = i_channel_mask;
512 p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
513 p_aout->output.p_sys->i_channels = i_channels;
515 p_aout->output.p_sys->b_chan_reorder =
516 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
517 i_channel_mask, i_channels,
518 p_aout->output.p_sys->pi_chan_table );
520 if( p_aout->output.p_sys->b_chan_reorder )
522 msg_Dbg( p_aout, "channel reordering needed" );
525 paStreamParameters.device = p_sys->i_device_id;
526 paStreamParameters.channelCount = i_channels;
527 paStreamParameters.sampleFormat = paFloat32;
528 paStreamParameters.suggestedLatency =
529 p_sys->deviceInfo->defaultLowOutputLatency;
530 paStreamParameters.hostApiSpecificStreamInfo = NULL;
532 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
533 &paStreamParameters, (double)p_aout->output.output.i_rate,
534 FRAME_SIZE, paClipOff, paCallback, p_sys );
535 if( i_err != paNoError )
537 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
538 Pa_GetErrorText( i_err ) );
539 if( i_err == paUnanticipatedHostError )
541 msg_Err( p_aout, "type %d code %ld : %s",
542 paLastHostErrorInfo->hostApiType,
543 paLastHostErrorInfo->errorCode,
544 paLastHostErrorInfo->errorText );
550 i_err = Pa_StartStream( p_sys->p_stream );
551 if( i_err != paNoError )
553 msg_Err( p_aout, "Pa_StartStream() failed" );
554 Pa_CloseStream( p_sys->p_stream );
561 /*****************************************************************************
563 *****************************************************************************/
564 static void Play( aout_instance_t * p_aout )
568 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
569 /*****************************************************************************
570 * PORTAUDIOThread: all interactions with libportaudio.a are handled
571 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
572 *****************************************************************************/
573 static void PORTAUDIOThread( pa_thread_t *pa_thread )
575 aout_instance_t *p_aout;
579 while( !pa_thread->b_die )
581 /* Wait for start of stream */
582 vlc_mutex_lock( &pa_thread->lock_signal );
583 if( !pa_thread->b_signal )
584 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
585 vlc_mutex_unlock( &pa_thread->lock_signal );
586 pa_thread->b_signal = VLC_FALSE;
588 p_aout = pa_thread->p_aout;
589 p_sys = p_aout->output.p_sys;
591 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
593 msg_Err( p_aout, "cannot open portaudio device" );
594 pa_thread->b_error = VLC_TRUE;
597 if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
599 msg_Err( p_aout, "cannot open portaudio device" );
600 pa_thread->b_error = VLC_TRUE;
602 i_err = Pa_Terminate();
603 if( i_err != paNoError )
605 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
606 Pa_GetErrorText( i_err ) );
610 /* Tell the main thread that we are ready */
611 vlc_mutex_lock( &pa_thread->lock_wait );
612 pa_thread->b_wait = VLC_TRUE;
613 vlc_cond_signal( &pa_thread->wait );
614 vlc_mutex_unlock( &pa_thread->lock_wait );
616 /* Wait for end of stream */
617 vlc_mutex_lock( &pa_thread->lock_signal );
618 if( !pa_thread->b_signal )
619 vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
620 vlc_mutex_unlock( &pa_thread->lock_signal );
621 pa_thread->b_signal = VLC_FALSE;
623 if( pa_thread->b_error ) continue;
625 i_err = Pa_StopStream( p_sys->p_stream );
626 if( i_err != paNoError )
628 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
629 Pa_GetErrorText( i_err ) );
631 i_err = Pa_CloseStream( p_sys->p_stream );
632 if( i_err != paNoError )
634 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
635 Pa_GetErrorText( i_err ) );
637 i_err = Pa_Terminate();
638 if( i_err != paNoError )
640 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
641 Pa_GetErrorText( i_err ) );
644 /* Tell the main thread that we are ready */
645 vlc_mutex_lock( &pa_thread->lock_wait );
646 pa_thread->b_wait = VLC_TRUE;
647 vlc_cond_signal( &pa_thread->wait );
648 vlc_mutex_unlock( &pa_thread->lock_wait );