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 */
39 /*****************************************************************************
40 * aout_sys_t: portaudio audio output method descriptor
41 *****************************************************************************/
42 typedef struct pa_thread_t
45 aout_instance_t * p_aout;
51 aout_instance_t *p_aout;
54 PaDeviceIndex i_devices;
56 PaDeviceIndex i_device_id;
57 const PaDeviceInfo *deviceInfo;
62 pa_thread_t *pa_thread;
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
70 static void Play ( aout_instance_t * );
71 static void PORTAUDIOThread( pa_thread_t * );
73 static int PAOpenDevice( aout_instance_t * );
74 static int PAOpenStream( aout_instance_t * );
76 /*****************************************************************************
78 *****************************************************************************/
79 #define DEVICE_TEXT N_("Output device")
80 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
83 set_description( N_("PORTAUDIO audio output") );
84 add_integer( "portaudio-device", 0, NULL,
85 DEVICE_TEXT, DEVICE_LONGTEXT, VLC_FALSE );
86 set_capability( "audio output", 0 );
87 set_callbacks( Open, Close );
90 /* This routine will be called by the PortAudio engine when audio is needed.
91 * It may called at interrupt level on some machines so don't do anything
92 * that could mess up the system like calling malloc() or free().
94 static int paCallback( const void *inputBuffer, void *outputBuffer,
95 unsigned long framesPerBuffer,
96 const PaStreamCallbackTimeInfo *paDate,
97 PaStreamCallbackFlags statusFlags,
100 struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
101 aout_instance_t *p_aout = p_sys->p_aout;
102 aout_buffer_t *p_buffer;
105 out_date = mdate() + (mtime_t) ( 1000000 *
106 ( paDate->outputBufferDacTime - paDate->currentTime ) );
107 p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
109 if ( p_buffer != NULL )
111 p_aout->p_vlc->pf_memcpy( outputBuffer, p_buffer->p_buffer,
112 framesPerBuffer * p_sys->i_sample_size );
113 /* aout_BufferFree may be dangereous here, but then so is
114 * aout_OutputNextBuffer (calls aout_BufferFree internally).
115 * one solution would be to link the no longer useful buffers
116 * in a second fifo (in aout_OutputNextBuffer too) and to
117 * wait until we are in Play to do the actual free.
119 aout_BufferFree( p_buffer );
122 /* Audio output buffer shortage -> stop the fill process and wait */
124 p_aout->p_vlc->pf_memset( outputBuffer, 0,
125 framesPerBuffer * p_sys->i_sample_size );
130 /*****************************************************************************
131 * Open: open the audio device
132 *****************************************************************************/
133 static int Open( vlc_object_t * p_this )
135 aout_instance_t *p_aout = (aout_instance_t *)p_this;
136 struct aout_sys_t * p_sys;
140 msg_Dbg( p_aout, "Entering Open()");
142 /* Allocate p_sys structure */
143 p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
146 msg_Err( p_aout, "out of memory" );
149 p_sys->p_aout = p_aout;
151 p_aout->output.p_sys = p_sys;
152 p_aout->output.pf_play = Play;
154 /* Retrieve output device id from config */
155 var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
156 var_Get( p_aout, "portaudio-device", &val );
157 p_sys->i_device_id = val.i_int;
159 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
161 msg_Err( p_aout, "cannot open portaudio device" );
166 /* Close device for now. We'll re-open it later on */
167 if( ( i_err = Pa_Terminate() ) != paNoError )
169 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
172 /* Now we need to setup our DirectSound play notification structure */
173 p_sys->pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
174 p_sys->pa_thread->p_aout = p_aout;
176 vlc_mutex_init( p_aout, &p_sys->lock );
177 vlc_cond_init( p_aout, &p_sys->wait );
179 /* Create PORTAUDIOThread */
180 if( vlc_thread_create( p_sys->pa_thread, "aout", PORTAUDIOThread,
181 VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
183 msg_Err( p_aout, "cannot create PORTAUDIO thread" );
187 if( p_sys->pa_thread->b_error )
189 msg_Err( p_aout, "PORTAUDIO thread failed" );
197 /*****************************************************************************
198 * Close: close the audio device
199 *****************************************************************************/
200 static void Close ( vlc_object_t *p_this )
202 aout_instance_t *p_aout = (aout_instance_t *)p_this;
203 aout_sys_t *p_sys = p_aout->output.p_sys;
205 msg_Dbg( p_aout, "closing portaudio");
207 vlc_mutex_lock( &p_sys->lock );
208 p_sys->pa_thread->b_die = VLC_TRUE;
209 vlc_cond_signal( &p_sys->wait );
210 vlc_mutex_unlock( &p_sys->lock );
212 vlc_thread_join( p_sys->pa_thread );
213 vlc_cond_destroy( &p_sys->wait );
214 vlc_mutex_destroy( &p_sys->lock );
216 msg_Dbg( p_aout, "portaudio closed");
220 /*****************************************************************************
222 *****************************************************************************/
223 static void Play( aout_instance_t * p_aout )
227 /*****************************************************************************
228 * PORTAUDIOThread: all interactions with libportaudio.a are handled
229 * in this single thread. Otherwise libportaudio.a is _not_ happy :-(
230 *****************************************************************************/
231 static void PORTAUDIOThread( pa_thread_t *pa_thread )
233 aout_instance_t *p_aout = pa_thread->p_aout;
234 aout_sys_t *p_sys = p_aout->output.p_sys;
237 if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
239 msg_Err( p_aout, "cannot open portaudio device" );
240 pa_thread->b_error = VLC_TRUE;
241 vlc_thread_ready( pa_thread );
245 if( PAOpenStream( p_aout ) != VLC_SUCCESS )
247 msg_Err( p_aout, "cannot open portaudio device" );
248 pa_thread->b_error = VLC_TRUE;
249 vlc_thread_ready( pa_thread );
253 /* Tell the main thread that we are ready */
254 vlc_thread_ready( pa_thread );
256 vlc_mutex_lock( &p_sys->lock );
257 if( !pa_thread->b_die ) vlc_cond_wait( &p_sys->wait, &p_sys->lock );
258 vlc_mutex_unlock( &p_sys->lock );
260 i_err = Pa_StopStream( p_sys->p_stream );
261 if( i_err != paNoError )
263 msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
264 Pa_GetErrorText( i_err ) );
266 i_err = Pa_CloseStream( p_sys->p_stream );
267 if( i_err != paNoError )
269 msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
270 Pa_GetErrorText( i_err ) );
274 i_err = Pa_Terminate();
275 if( i_err != paNoError )
277 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
278 Pa_GetErrorText( i_err ) );
282 static int PAOpenDevice( aout_instance_t *p_aout )
284 aout_sys_t *p_sys = p_aout->output.p_sys;
285 const PaDeviceInfo *p_pdi;
287 vlc_value_t val, text;
290 /* Initialize portaudio */
291 i_err = Pa_Initialize();
292 if( i_err != paNoError )
294 msg_Err( p_aout, "Pa_Initialize returned %d : %s",
295 i_err, Pa_GetErrorText( i_err ) );
300 p_sys->i_devices = Pa_GetDeviceCount();
301 if( p_sys->i_devices < 0 )
303 i_err = p_sys->i_devices;
304 msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
305 Pa_GetErrorText( i_err ) );
310 /* Display all devices info */
311 msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
312 for( i = 0; i < p_sys->i_devices; i++ )
314 p_pdi = Pa_GetDeviceInfo( i );
315 msg_Dbg( p_aout, "------------------------------------- #%d", i );
316 msg_Dbg( p_aout, "Name = %s", p_pdi->name );
317 msg_Dbg( p_aout, "Max Inputs = %d, Max Outputs = %d",
318 p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
320 msg_Dbg( p_aout, "-------------------------------------" );
322 msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
323 if( p_sys->i_device_id >= p_sys->i_devices )
325 msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
328 p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
330 if( p_sys->deviceInfo->maxOutputChannels < 1 )
332 msg_Err( p_aout, "no channel available" );
336 if( var_Type( p_aout, "audio-device" ) == 0 )
338 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
339 text.psz_string = _("Audio Device");
340 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
342 if( p_sys->deviceInfo->maxOutputChannels >= 1 )
344 val.i_int = AOUT_VAR_MONO;
345 text.psz_string = N_("Mono");
346 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
348 msg_Dbg( p_aout, "device supports 1 channel" );
350 if( p_sys->deviceInfo->maxOutputChannels >= 2 )
352 val.i_int = AOUT_VAR_STEREO;
353 text.psz_string = N_("Stereo");
354 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
356 var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
358 var_Set( p_aout, "audio-device", val );
359 msg_Dbg( p_aout, "device supports 2 channels" );
361 if( p_sys->deviceInfo->maxOutputChannels >= 4 )
363 val.i_int = AOUT_VAR_2F2R;
364 text.psz_string = N_("2 Front 2 Rear");
365 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
367 msg_Dbg( p_aout, "device supports 4 channels" );
369 if( p_sys->deviceInfo->maxOutputChannels >= 5 )
371 val.i_int = AOUT_VAR_3F2R;
372 text.psz_string = N_("3 Front 2 Rear");
373 var_Change( p_aout, "audio-device",
374 VLC_VAR_ADDCHOICE, &val, &text );
375 msg_Dbg( p_aout, "device supports 5 channels" );
377 if( p_sys->deviceInfo->maxOutputChannels >= 6 )
379 val.i_int = AOUT_VAR_5_1;
380 text.psz_string = N_("5.1");
381 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
383 msg_Dbg( p_aout, "device supports 5.1 channels" );
386 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
388 val.b_bool = VLC_TRUE;
389 var_Set( p_aout, "intf-change", val );
392 /* Audio format is paFloat32 (always supported by portaudio v19) */
393 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
398 if( ( i_err = Pa_Terminate() ) != paNoError )
400 msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
405 static int PAOpenStream( aout_instance_t *p_aout )
407 aout_sys_t *p_sys = p_aout->output.p_sys;
408 const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
409 PaStreamParameters paStreamParameters;
411 int i_channels, i_err;
413 if( var_Get( p_aout, "audio-device", &val ) < 0 )
418 if( val.i_int == AOUT_VAR_5_1 )
420 p_aout->output.output.i_physical_channels
421 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
422 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
425 else if( val.i_int == AOUT_VAR_3F2R )
427 p_aout->output.output.i_physical_channels
428 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
429 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
431 else if( val.i_int == AOUT_VAR_2F2R )
433 p_aout->output.output.i_physical_channels
434 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
435 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
437 else if( val.i_int == AOUT_VAR_MONO )
439 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
443 p_aout->output.output.i_physical_channels
444 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
447 i_channels = aout_FormatNbChannels( &p_aout->output.output );
448 msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
450 /* Calculate the frame size in bytes */
451 p_sys->i_sample_size = 4 * i_channels;
452 p_aout->output.i_nb_samples = FRAME_SIZE;
453 aout_FormatPrepare( &p_aout->output.output );
454 aout_VolumeSoftInit( p_aout );
456 paStreamParameters.device = p_sys->i_device_id;
457 paStreamParameters.channelCount = i_channels;
458 paStreamParameters.sampleFormat = paFloat32;
459 paStreamParameters.suggestedLatency =
460 p_sys->deviceInfo->defaultLowOutputLatency;
461 paStreamParameters.hostApiSpecificStreamInfo = NULL;
463 i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
464 &paStreamParameters, (double)p_aout->output.output.i_rate,
465 FRAME_SIZE, paClipOff, paCallback, p_sys );
466 if( i_err != paNoError )
468 msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
469 Pa_GetErrorText( i_err ) );
470 if( i_err == paUnanticipatedHostError )
472 msg_Err( p_aout, "type %d code %ld : %s",
473 paLastHostErrorInfo->hostApiType,
474 paLastHostErrorInfo->errorCode,
475 paLastHostErrorInfo->errorText );
481 i_err = Pa_StartStream( p_sys->p_stream );
482 if( i_err != paNoError )
484 msg_Err( p_aout, "Pa_StartStream() failed" );
485 Pa_CloseStream( p_sys->p_stream );