]> git.sesse.net Git - vlc/blob - modules/audio_output/portaudio.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / audio_output / portaudio.c
1 /*****************************************************************************
2  * portaudio.c : portaudio (v19) audio output plugin
3  *****************************************************************************
4  * Copyright (C) 2002, 2006 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_aout.h>
35
36
37 #include <portaudio.h>
38
39 #define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
40
41 #ifdef WIN32
42 #   define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
43 #endif
44
45 /*****************************************************************************
46  * aout_sys_t: portaudio audio output method descriptor
47  *****************************************************************************/
48 typedef struct pa_thread_t
49 {
50     VLC_COMMON_MEMBERS
51     aout_instance_t *p_aout;
52
53     vlc_cond_t  wait;
54     vlc_mutex_t lock_wait;
55     bool  b_wait;
56     vlc_cond_t  signal;
57     vlc_mutex_t lock_signal;
58     bool  b_signal;
59
60 } pa_thread_t;
61
62 struct aout_sys_t
63 {
64     aout_instance_t *p_aout;
65     PaStream *p_stream;
66
67     PaDeviceIndex i_devices;
68     int i_sample_size;
69     PaDeviceIndex i_device_id;
70     const PaDeviceInfo *deviceInfo;
71
72     bool 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;
76     uint32_t i_channels;
77 };
78
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 };
89
90 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
91 static bool b_init = 0;
92 static pa_thread_t *pa_thread;
93 static void PORTAUDIOThread( pa_thread_t * );
94 #endif
95
96 /*****************************************************************************
97  * Local prototypes.
98  *****************************************************************************/
99 static int  Open        ( vlc_object_t * );
100 static void Close       ( vlc_object_t * );
101 static void Play        ( aout_instance_t * );
102
103 static int PAOpenDevice( aout_instance_t * );
104 static int PAOpenStream( aout_instance_t * );
105
106 /*****************************************************************************
107  * Module descriptor
108  *****************************************************************************/
109 #define DEVICE_TEXT N_("Output device")
110 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
111
112 vlc_module_begin();
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, false );
119     set_capability( "audio output", 0 );
120     set_callbacks( Open, Close );
121 vlc_module_end();
122
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().
126  */
127 static int paCallback( const void *inputBuffer, void *outputBuffer,
128                        unsigned long framesPerBuffer,
129                        const PaStreamCallbackTimeInfo *paDate,
130                        PaStreamCallbackFlags statusFlags, void *p_cookie )
131 {
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;
135     mtime_t out_date;
136
137     out_date = mdate() + (mtime_t) ( 1000000 *
138         ( paDate->outputBufferDacTime - paDate->currentTime ) );
139     p_buffer = aout_OutputNextBuffer( p_aout, out_date, true );
140
141     if ( p_buffer != NULL )
142     {
143         if( p_sys->b_chan_reorder )
144         {
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 );
149         }
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.
157          */
158         aout_BufferFree( p_buffer );
159     }
160     else
161         /* Audio output buffer shortage -> stop the fill process and wait */
162     {
163         p_aout->p_libvlc->pf_memset( outputBuffer, 0,
164                                   framesPerBuffer * p_sys->i_sample_size );
165     }
166     return 0;
167 }
168
169 /*****************************************************************************
170  * Open: open the audio device
171  *****************************************************************************/
172 static int Open( vlc_object_t * p_this )
173 {
174     aout_instance_t *p_aout = (aout_instance_t *)p_this;
175     struct aout_sys_t * p_sys;
176     vlc_value_t val;
177     int i_err;
178
179     msg_Dbg( p_aout, "entering Open()");
180
181     /* Allocate p_sys structure */
182     p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
183     if( p_sys == NULL )
184     {
185         msg_Err( p_aout, "out of memory" );
186         return VLC_ENOMEM;
187     }
188     p_sys->p_aout = p_aout;
189     p_sys->p_stream = 0;
190     p_aout->output.p_sys = p_sys;
191     p_aout->output.pf_play = Play;
192
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;
197
198 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
199     if( !b_init )
200     {
201         /* Test device */
202         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
203         {
204             msg_Err( p_aout, "cannot open portaudio device" );
205             free( p_sys );
206             return VLC_EGENERIC;
207         }
208
209         /* Close device for now. We'll re-open it later on */
210         if( ( i_err = Pa_Terminate() ) != paNoError )
211         {
212             msg_Err( p_aout, "closing the device returned %d", i_err );
213         }
214
215         b_init = true;
216
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 = 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 = 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 = false;
227
228         /* Create PORTAUDIOThread */
229         if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
230                                VLC_THREAD_PRIORITY_OUTPUT, false ) )
231         {
232             msg_Err( p_aout, "cannot create PORTAUDIO thread" );
233             return VLC_EGENERIC;
234         }
235     }
236     else
237     {
238         pa_thread->p_aout = p_aout;
239         pa_thread->b_wait = false;
240         pa_thread->b_signal = false;
241         pa_thread->b_error = false;
242     }
243
244     /* Signal start of stream */
245     vlc_mutex_lock( &pa_thread->lock_signal );
246     pa_thread->b_signal = true;
247     vlc_cond_signal( &pa_thread->signal );
248     vlc_mutex_unlock( &pa_thread->lock_signal );
249
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 = false;
256
257     if( pa_thread->b_error )
258     {
259         msg_Err( p_aout, "PORTAUDIO thread failed" );
260         Close( p_this );
261         return VLC_EGENERIC;
262     }
263
264     return VLC_SUCCESS;
265
266 #else
267
268     if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
269     {
270         msg_Err( p_aout, "cannot open portaudio device" );
271         free( p_sys );
272         return VLC_EGENERIC;
273     }
274
275     if( PAOpenStream( p_aout ) != VLC_SUCCESS )
276     {
277         msg_Err( p_aout, "cannot open portaudio device" );
278     }
279
280     return VLC_SUCCESS;
281
282 #endif
283 }
284
285 /*****************************************************************************
286  * Close: close the audio device
287  *****************************************************************************/
288 static void Close ( vlc_object_t *p_this )
289 {
290     aout_instance_t *p_aout = (aout_instance_t *)p_this;
291     aout_sys_t *p_sys = p_aout->output.p_sys;
292     int i_err;
293
294     msg_Dbg( p_aout, "closing portaudio");
295
296 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
297
298     /* Signal end of stream */
299     vlc_mutex_lock( &pa_thread->lock_signal );
300     pa_thread->b_signal = true;
301     vlc_cond_signal( &pa_thread->signal );
302     vlc_mutex_unlock( &pa_thread->lock_signal );
303
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 = false;
310
311 #else
312
313     i_err = Pa_StopStream( p_sys->p_stream );
314     if( i_err != paNoError )
315     {
316         msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
317                  Pa_GetErrorText( i_err ) );
318     }
319     i_err = Pa_CloseStream( p_sys->p_stream );
320     if( i_err != paNoError )
321     {
322         msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
323                  Pa_GetErrorText( i_err ) );
324     }
325
326     i_err = Pa_Terminate();
327     if( i_err != paNoError )
328     {
329         msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
330                  Pa_GetErrorText( i_err ) );
331     }
332
333 #endif
334
335     msg_Dbg( p_aout, "portaudio closed");
336     free( p_sys );
337 }
338
339 static int PAOpenDevice( aout_instance_t *p_aout )
340 {
341     aout_sys_t *p_sys = p_aout->output.p_sys;
342     const PaDeviceInfo *p_pdi;
343     PaError i_err;
344     vlc_value_t val, text;
345     int i;
346
347     /* Initialize portaudio */
348     i_err = Pa_Initialize();
349     if( i_err != paNoError )
350     {
351         msg_Err( p_aout, "Pa_Initialize returned %d : %s",
352                  i_err, Pa_GetErrorText( i_err ) );
353
354         return VLC_EGENERIC;
355     }
356
357     p_sys->i_devices = Pa_GetDeviceCount();
358     if( p_sys->i_devices < 0 )
359     {
360         i_err = p_sys->i_devices;
361         msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
362                  Pa_GetErrorText( i_err ) );
363
364         goto error;
365     }
366
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++ )
370     {
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 );
376     }
377     msg_Dbg( p_aout, "-------------------------------------" );
378
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 )
381     {
382         msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
383         goto error;
384     }
385     p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
386
387     if( p_sys->deviceInfo->maxOutputChannels < 1 )
388     {
389         msg_Err( p_aout, "no channel available" );
390         goto error;
391     }
392
393     if( var_Type( p_aout, "audio-device" ) == 0 )
394     {
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 );
398
399         if( p_sys->deviceInfo->maxOutputChannels >= 1 )
400         {
401             val.i_int = AOUT_VAR_MONO;
402             text.psz_string = N_("Mono");
403             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
404                         &val, &text );
405             msg_Dbg( p_aout, "device supports 1 channel" );
406         }
407         if( p_sys->deviceInfo->maxOutputChannels >= 2 )
408         {
409             val.i_int = AOUT_VAR_STEREO;
410             text.psz_string = N_("Stereo");
411             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
412                         &val, &text );
413             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
414                         &val, NULL );
415             var_Set( p_aout, "audio-device", val );
416             msg_Dbg( p_aout, "device supports 2 channels" );
417         }
418         if( p_sys->deviceInfo->maxOutputChannels >= 4 )
419         {
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,
423                         &val, &text );
424             msg_Dbg( p_aout, "device supports 4 channels" );
425         }
426         if( p_sys->deviceInfo->maxOutputChannels >= 5 )
427         {
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" );
433         }
434         if( p_sys->deviceInfo->maxOutputChannels >= 6 )
435         {
436             val.i_int = AOUT_VAR_5_1;
437             text.psz_string = "5.1";
438             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
439                         &val, &text );
440             msg_Dbg( p_aout, "device supports 5.1 channels" );
441         }
442
443         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
444
445         val.b_bool = true;
446         var_Set( p_aout, "intf-change", val );
447     }
448
449     /* Audio format is paFloat32 (always supported by portaudio v19) */
450     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
451
452     return VLC_SUCCESS;
453
454  error:
455     if( ( i_err = Pa_Terminate() ) != paNoError )
456     {
457         msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
458     }
459     return VLC_EGENERIC;
460 }
461
462 static int PAOpenStream( aout_instance_t *p_aout )
463 {
464     aout_sys_t *p_sys = p_aout->output.p_sys;
465     const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
466     PaStreamParameters paStreamParameters;
467     vlc_value_t val;
468     int i_channels, i_err;
469     uint32_t i_channel_mask;
470
471     if( var_Get( p_aout, "audio-device", &val ) < 0 )
472     {
473         return VLC_EGENERIC;
474     }
475
476     if( val.i_int == AOUT_VAR_5_1 )
477     {
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
481               | AOUT_CHAN_LFE;
482     }
483     else if( val.i_int == AOUT_VAR_3F2R )
484     {
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;
488     }
489     else if( val.i_int == AOUT_VAR_2F2R )
490     {
491         p_aout->output.output.i_physical_channels
492             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
493             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
494     }
495     else if( val.i_int == AOUT_VAR_MONO )
496     {
497         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
498     }
499     else
500     {
501         p_aout->output.output.i_physical_channels
502             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
503     }
504
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;
508
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 );
514
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;
519
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 );
524
525     if( p_aout->output.p_sys->b_chan_reorder )
526     {
527         msg_Dbg( p_aout, "channel reordering needed" );
528     }
529
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;
536
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 )
541     {
542         msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
543                  Pa_GetErrorText( i_err ) );
544         if( i_err == paUnanticipatedHostError )
545         {
546             msg_Err( p_aout, "type %d code %ld : %s",
547                      paLastHostErrorInfo->hostApiType,
548                      paLastHostErrorInfo->errorCode,
549                      paLastHostErrorInfo->errorText );
550         }
551         p_sys->p_stream = 0;
552         return VLC_EGENERIC;
553     }
554
555     i_err = Pa_StartStream( p_sys->p_stream );
556     if( i_err != paNoError )
557     {
558         msg_Err( p_aout, "Pa_StartStream() failed" );
559         Pa_CloseStream( p_sys->p_stream );
560         return VLC_EGENERIC;
561     }
562
563     return VLC_SUCCESS;
564 }
565
566 /*****************************************************************************
567  * Play: play sound
568  *****************************************************************************/
569 static void Play( aout_instance_t * p_aout )
570 {
571 }
572
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 )
579 {
580     aout_instance_t *p_aout;
581     aout_sys_t *p_sys;
582     int i_err;
583
584     while( !pa_thread->b_die )
585     {
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 = false;
592
593         p_aout = pa_thread->p_aout;
594         p_sys = p_aout->output.p_sys;
595
596         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
597         {
598             msg_Err( p_aout, "cannot open portaudio device" );
599             pa_thread->b_error = true;
600         }
601
602         if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
603         {
604             msg_Err( p_aout, "cannot open portaudio device" );
605             pa_thread->b_error = true;
606
607             i_err = Pa_Terminate();
608             if( i_err != paNoError )
609             {
610                 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
611                          Pa_GetErrorText( i_err ) );
612             }
613         }
614
615         /* Tell the main thread that we are ready */
616         vlc_mutex_lock( &pa_thread->lock_wait );
617         pa_thread->b_wait = true;
618         vlc_cond_signal( &pa_thread->wait );
619         vlc_mutex_unlock( &pa_thread->lock_wait );
620
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 = false;
627
628         if( pa_thread->b_error ) continue;
629
630         i_err = Pa_StopStream( p_sys->p_stream );
631         if( i_err != paNoError )
632         {
633             msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
634                      Pa_GetErrorText( i_err ) );
635         }
636         i_err = Pa_CloseStream( p_sys->p_stream );
637         if( i_err != paNoError )
638         {
639             msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
640                      Pa_GetErrorText( i_err ) );
641         }
642         i_err = Pa_Terminate();
643         if( i_err != paNoError )
644         {
645             msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
646                      Pa_GetErrorText( i_err ) );
647         }
648
649         /* Tell the main thread that we are ready */
650         vlc_mutex_lock( &pa_thread->lock_wait );
651         pa_thread->b_wait = true;
652         vlc_cond_signal( &pa_thread->wait );
653         vlc_mutex_unlock( &pa_thread->lock_wait );
654     }
655 }
656 #endif