]> git.sesse.net Git - vlc/blob - modules/audio_output/portaudio.c
* modules/audio_output/portaudio.c: ported to portaudio v19 by Frederic Ruget + ...
[vlc] / modules / audio_output / portaudio.c
1 /*****************************************************************************
2  * portaudio.c : portaudio (v19) audio output plugin
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Frederic Ruget <frederic.ruget@free.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33 #include <portaudio.h>
34
35 #include "aout_internal.h"
36
37 #define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
38
39 /*****************************************************************************
40  * aout_sys_t: portaudio audio output method descriptor
41  *****************************************************************************/
42 typedef struct pa_thread_t
43 {
44     VLC_COMMON_MEMBERS
45     aout_instance_t * p_aout;
46
47 } pa_thread_t;
48
49 struct aout_sys_t
50 {
51     aout_instance_t *p_aout;
52     PaStream *p_stream;
53
54     PaDeviceIndex i_devices;
55     int i_sample_size;
56     PaDeviceIndex i_device_id;
57     const PaDeviceInfo *deviceInfo;
58
59     vlc_mutex_t lock;
60     vlc_cond_t wait;
61
62     pa_thread_t *pa_thread;
63 };
64
65 /*****************************************************************************
66  * Local prototypes.
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 * );
72
73 static int PAOpenDevice( aout_instance_t * );
74 static int PAOpenStream( aout_instance_t * );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 #define DEVICE_TEXT N_("Output device")
80 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
81
82 vlc_module_begin();
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 );
88 vlc_module_end();
89
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().
93  */
94 static int paCallback( const void *inputBuffer, void *outputBuffer,
95                             unsigned long framesPerBuffer,
96                             const PaStreamCallbackTimeInfo *paDate,
97                             PaStreamCallbackFlags statusFlags,
98                             void *p_cookie )
99 {
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;
103     mtime_t out_date;
104
105     out_date = mdate() + (mtime_t) ( 1000000 *
106         ( paDate->outputBufferDacTime - paDate->currentTime ) );
107     p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
108
109     if ( p_buffer != NULL )
110     {
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.
118          */
119         aout_BufferFree( p_buffer );
120     }
121     else
122         /* Audio output buffer shortage -> stop the fill process and wait */
123     {
124         p_aout->p_vlc->pf_memset( outputBuffer, 0,
125                                   framesPerBuffer * p_sys->i_sample_size );
126     }
127     return 0;
128 }
129
130 /*****************************************************************************
131  * Open: open the audio device
132  *****************************************************************************/
133 static int Open( vlc_object_t * p_this )
134 {
135     aout_instance_t *p_aout = (aout_instance_t *)p_this;
136     struct aout_sys_t * p_sys;
137     vlc_value_t val;
138     int i_err;
139
140     msg_Dbg( p_aout, "Entering Open()");
141
142     /* Allocate p_sys structure */
143     p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
144     if( p_sys == NULL )
145     {
146         msg_Err( p_aout, "out of memory" );
147         return VLC_ENOMEM;
148     }
149     p_sys->p_aout = p_aout;
150     p_sys->p_stream = 0;
151     p_aout->output.p_sys = p_sys;
152     p_aout->output.pf_play = Play;
153
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;
158
159     if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
160     {
161         msg_Err( p_aout, "cannot open portaudio device" );
162         free( p_sys );
163         return VLC_EGENERIC;
164     }
165
166     /* Close device for now. We'll re-open it later on */
167     if( ( i_err = Pa_Terminate() ) != paNoError )
168     {
169         msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
170     }
171
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;
175
176     vlc_mutex_init( p_aout, &p_sys->lock );
177     vlc_cond_init( p_aout, &p_sys->wait );
178
179     /* Create PORTAUDIOThread */
180     if( vlc_thread_create( p_sys->pa_thread, "aout", PORTAUDIOThread,
181                            VLC_THREAD_PRIORITY_OUTPUT, VLC_TRUE ) )
182     {
183         msg_Err( p_aout, "cannot create PORTAUDIO thread" );
184         return VLC_EGENERIC;
185     }
186
187     if( p_sys->pa_thread->b_error )
188     {
189         msg_Err( p_aout, "PORTAUDIO thread failed" );
190         Close( p_this );
191         return VLC_EGENERIC;
192     }
193
194     return VLC_SUCCESS;
195 }
196
197 /*****************************************************************************
198  * Close: close the audio device
199  *****************************************************************************/
200 static void Close ( vlc_object_t *p_this )
201 {
202     aout_instance_t *p_aout = (aout_instance_t *)p_this;
203     aout_sys_t *p_sys = p_aout->output.p_sys;
204
205     msg_Dbg( p_aout, "closing portaudio");
206
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 );
211
212     vlc_thread_join( p_sys->pa_thread );
213     vlc_cond_destroy( &p_sys->wait );
214     vlc_mutex_destroy( &p_sys->lock );
215
216     msg_Dbg( p_aout, "portaudio closed");
217     free( p_sys );
218 }
219
220 /*****************************************************************************
221  * Play: play sound
222  *****************************************************************************/
223 static void Play( aout_instance_t * p_aout )
224 {
225 }
226
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 )
232 {
233     aout_instance_t *p_aout = pa_thread->p_aout;
234     aout_sys_t *p_sys = p_aout->output.p_sys;
235     int i_err;
236
237     if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
238     {
239         msg_Err( p_aout, "cannot open portaudio device" );
240         pa_thread->b_error = VLC_TRUE;
241         vlc_thread_ready( pa_thread );
242         return;
243     }
244
245     if( PAOpenStream( p_aout ) != VLC_SUCCESS )
246     {
247         msg_Err( p_aout, "cannot open portaudio device" );
248         pa_thread->b_error = VLC_TRUE;
249         vlc_thread_ready( pa_thread );
250         goto end;
251     }
252
253     /* Tell the main thread that we are ready */
254     vlc_thread_ready( pa_thread );
255
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 );
259
260     i_err = Pa_StopStream( p_sys->p_stream );
261     if( i_err != paNoError )
262     {
263         msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
264                  Pa_GetErrorText( i_err ) );
265     }
266     i_err = Pa_CloseStream( p_sys->p_stream );
267     if( i_err != paNoError )
268     {
269         msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
270                  Pa_GetErrorText( i_err ) );
271     }
272
273  end:
274     i_err = Pa_Terminate();
275     if( i_err != paNoError )
276     {
277         msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
278                  Pa_GetErrorText( i_err ) );
279     }
280 }
281
282 static int PAOpenDevice( aout_instance_t *p_aout )
283 {
284     aout_sys_t *p_sys = p_aout->output.p_sys;
285     const PaDeviceInfo *p_pdi;
286     PaError i_err;
287     vlc_value_t val, text;
288     int i;
289
290     /* Initialize portaudio */
291     i_err = Pa_Initialize();
292     if( i_err != paNoError )
293     {
294         msg_Err( p_aout, "Pa_Initialize returned %d : %s",
295                  i_err, Pa_GetErrorText( i_err ) );
296
297         return VLC_EGENERIC;
298     }
299
300     p_sys->i_devices = Pa_GetDeviceCount();
301     if( p_sys->i_devices < 0 )
302     {
303         i_err = p_sys->i_devices;
304         msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
305                  Pa_GetErrorText( i_err ) );
306
307         goto error;
308     }
309
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++ )
313     {
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 );
319     }
320     msg_Dbg( p_aout, "-------------------------------------" );
321
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 )
324     {
325         msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
326         goto error;
327     }
328     p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
329
330     if( p_sys->deviceInfo->maxOutputChannels < 1 )
331     {
332         msg_Err( p_aout, "no channel available" );
333         goto error;
334     }
335
336     if( var_Type( p_aout, "audio-device" ) == 0 )
337     {
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 );
341
342         if( p_sys->deviceInfo->maxOutputChannels >= 1 )
343         {
344             val.i_int = AOUT_VAR_MONO;
345             text.psz_string = N_("Mono");
346             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
347                         &val, &text );
348             msg_Dbg( p_aout, "device supports 1 channel" );
349         }
350         if( p_sys->deviceInfo->maxOutputChannels >= 2 )
351         {
352             val.i_int = AOUT_VAR_STEREO;
353             text.psz_string = N_("Stereo");
354             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
355                         &val, &text );
356             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
357                         &val, NULL );
358             msg_Dbg( p_aout, "device supports 2 channels" );
359         }
360         if( p_sys->deviceInfo->maxOutputChannels >= 4 )
361         {
362             val.i_int = AOUT_VAR_2F2R;
363             text.psz_string = N_("2 Front 2 Rear");
364             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
365                         &val, &text );
366             msg_Dbg( p_aout, "device supports 4 channels" );
367         }
368         if( p_sys->deviceInfo->maxOutputChannels >= 5 )
369         {
370             val.i_int = AOUT_VAR_3F2R;
371             text.psz_string = N_("3 Front 2 Rear");
372             var_Change( p_aout, "audio-device",
373                         VLC_VAR_ADDCHOICE, &val, &text );
374             msg_Dbg( p_aout, "device supports 5 channels" );
375         }
376         if( p_sys->deviceInfo->maxOutputChannels >= 6 )
377         {
378             val.i_int = AOUT_VAR_5_1;
379             text.psz_string = N_("5.1");
380             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
381                         &val, &text );
382             msg_Dbg( p_aout, "device supports 5.1 channels" );
383         }
384
385         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
386
387         val.b_bool = VLC_TRUE;
388         var_Set( p_aout, "intf-change", val );
389     }
390
391     /* Audio format is paFloat32 (always supported by portaudio v19) */
392     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
393
394     return VLC_SUCCESS;
395
396  error:
397     if( ( i_err = Pa_Terminate() ) != paNoError )
398     {
399         msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
400     }
401     return VLC_EGENERIC;
402 }
403
404 static int PAOpenStream( aout_instance_t *p_aout )
405 {
406     aout_sys_t *p_sys = p_aout->output.p_sys;
407     const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
408     PaStreamParameters paStreamParameters;
409     vlc_value_t val;
410     int i_channels, i_err;
411
412     if( var_Get( p_aout, "audio-device", &val ) < 0 )
413     {
414         return VLC_EGENERIC;
415     }
416
417     if( val.i_int == AOUT_VAR_5_1 )
418     {
419         p_aout->output.output.i_physical_channels
420             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
421               | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
422               | AOUT_CHAN_LFE;
423     }
424     else if( val.i_int == AOUT_VAR_3F2R )
425     {
426         p_aout->output.output.i_physical_channels
427             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
428             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
429     }
430     else if( val.i_int == AOUT_VAR_2F2R )
431     {
432         p_aout->output.output.i_physical_channels
433             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
434             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
435     }
436     else if( val.i_int == AOUT_VAR_MONO )
437     {
438         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
439     }
440     else
441     {
442         p_aout->output.output.i_physical_channels
443             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
444     }
445
446     i_channels = aout_FormatNbChannels( &p_aout->output.output );
447     msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
448
449     /* Calculate the frame size in bytes */
450     p_sys->i_sample_size = 4 * i_channels;
451     p_aout->output.i_nb_samples = FRAME_SIZE;
452     aout_FormatPrepare( &p_aout->output.output );
453     aout_VolumeSoftInit( p_aout );
454
455     paStreamParameters.device = p_sys->i_device_id;
456     paStreamParameters.channelCount = i_channels;
457     paStreamParameters.sampleFormat = paFloat32;
458     paStreamParameters.suggestedLatency =
459         p_sys->deviceInfo->defaultLowOutputLatency;
460     paStreamParameters.hostApiSpecificStreamInfo = NULL;
461
462     i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
463                 &paStreamParameters, (double)p_aout->output.output.i_rate,
464                 FRAME_SIZE, paClipOff, paCallback, p_sys );
465     if( i_err != paNoError )
466     {
467         msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
468                  Pa_GetErrorText( i_err ) );
469         if( i_err == paUnanticipatedHostError )
470         {
471             msg_Err( p_aout, "type %d code %ld : %s",
472                      paLastHostErrorInfo->hostApiType,
473                      paLastHostErrorInfo->errorCode,
474                      paLastHostErrorInfo->errorText );
475         }
476         p_sys->p_stream = 0;
477         return VLC_EGENERIC;
478     }
479
480     i_err = Pa_StartStream( p_sys->p_stream );
481     if( i_err != paNoError )
482     {
483         msg_Err( p_aout, "Pa_StartStream() failed" );
484         Pa_CloseStream( p_sys->p_stream );
485         return VLC_EGENERIC;
486     }
487
488     return VLC_SUCCESS;
489 }