]> git.sesse.net Git - vlc/blob - modules/audio_output/portaudio.c
vlc_mutex_init: remove unused paramter
[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         vlc_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         vlc_memset( outputBuffer, 0, framesPerBuffer * p_sys->i_sample_size );
164     }
165     return 0;
166 }
167
168 /*****************************************************************************
169  * Open: open the audio device
170  *****************************************************************************/
171 static int Open( vlc_object_t * p_this )
172 {
173     aout_instance_t *p_aout = (aout_instance_t *)p_this;
174     struct aout_sys_t * p_sys;
175     vlc_value_t val;
176     int i_err;
177
178     msg_Dbg( p_aout, "entering Open()");
179
180     /* Allocate p_sys structure */
181     p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
182     if( p_sys == NULL )
183     {
184         msg_Err( p_aout, "out of memory" );
185         return VLC_ENOMEM;
186     }
187     p_sys->p_aout = p_aout;
188     p_sys->p_stream = 0;
189     p_aout->output.p_sys = p_sys;
190     p_aout->output.pf_play = Play;
191
192     /* Retrieve output device id from config */
193     var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
194     var_Get( p_aout, "portaudio-device", &val );
195     p_sys->i_device_id = val.i_int;
196
197 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
198     if( !b_init )
199     {
200         /* Test device */
201         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
202         {
203             msg_Err( p_aout, "cannot open portaudio device" );
204             free( p_sys );
205             return VLC_EGENERIC;
206         }
207
208         /* Close device for now. We'll re-open it later on */
209         if( ( i_err = Pa_Terminate() ) != paNoError )
210         {
211             msg_Err( p_aout, "closing the device returned %d", i_err );
212         }
213
214         b_init = true;
215
216         /* Now we need to setup our DirectSound play notification structure */
217         pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
218         pa_thread->p_aout = p_aout;
219         pa_thread->b_error = false;
220         vlc_mutex_init( &pa_thread->lock_wait );
221         vlc_cond_init( p_aout, &pa_thread->wait );
222         pa_thread->b_wait = false;
223         vlc_mutex_init( &pa_thread->lock_signal );
224         vlc_cond_init( p_aout, &pa_thread->signal );
225         pa_thread->b_signal = false;
226
227         /* Create PORTAUDIOThread */
228         if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
229                                VLC_THREAD_PRIORITY_OUTPUT, false ) )
230         {
231             msg_Err( p_aout, "cannot create PORTAUDIO thread" );
232             return VLC_EGENERIC;
233         }
234     }
235     else
236     {
237         pa_thread->p_aout = p_aout;
238         pa_thread->b_wait = false;
239         pa_thread->b_signal = false;
240         pa_thread->b_error = false;
241     }
242
243     /* Signal start of stream */
244     vlc_mutex_lock( &pa_thread->lock_signal );
245     pa_thread->b_signal = true;
246     vlc_cond_signal( &pa_thread->signal );
247     vlc_mutex_unlock( &pa_thread->lock_signal );
248
249     /* Wait until thread is ready */
250     vlc_mutex_lock( &pa_thread->lock_wait );
251     if( !pa_thread->b_wait )
252         vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
253     vlc_mutex_unlock( &pa_thread->lock_wait );
254     pa_thread->b_wait = false;
255
256     if( pa_thread->b_error )
257     {
258         msg_Err( p_aout, "PORTAUDIO thread failed" );
259         Close( p_this );
260         return VLC_EGENERIC;
261     }
262
263     return VLC_SUCCESS;
264
265 #else
266
267     if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
268     {
269         msg_Err( p_aout, "cannot open portaudio device" );
270         free( p_sys );
271         return VLC_EGENERIC;
272     }
273
274     if( PAOpenStream( p_aout ) != VLC_SUCCESS )
275     {
276         msg_Err( p_aout, "cannot open portaudio device" );
277     }
278
279     return VLC_SUCCESS;
280
281 #endif
282 }
283
284 /*****************************************************************************
285  * Close: close the audio device
286  *****************************************************************************/
287 static void Close ( vlc_object_t *p_this )
288 {
289     aout_instance_t *p_aout = (aout_instance_t *)p_this;
290     aout_sys_t *p_sys = p_aout->output.p_sys;
291     int i_err;
292
293     msg_Dbg( p_aout, "closing portaudio");
294
295 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
296
297     /* Signal end of stream */
298     vlc_mutex_lock( &pa_thread->lock_signal );
299     pa_thread->b_signal = true;
300     vlc_cond_signal( &pa_thread->signal );
301     vlc_mutex_unlock( &pa_thread->lock_signal );
302
303     /* Wait until thread is ready */
304     vlc_mutex_lock( &pa_thread->lock_wait );
305     if( !pa_thread->b_wait )
306         vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
307     vlc_mutex_unlock( &pa_thread->lock_wait );
308     pa_thread->b_wait = false;
309
310 #else
311
312     i_err = Pa_StopStream( p_sys->p_stream );
313     if( i_err != paNoError )
314     {
315         msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
316                  Pa_GetErrorText( i_err ) );
317     }
318     i_err = Pa_CloseStream( p_sys->p_stream );
319     if( i_err != paNoError )
320     {
321         msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
322                  Pa_GetErrorText( i_err ) );
323     }
324
325     i_err = Pa_Terminate();
326     if( i_err != paNoError )
327     {
328         msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
329                  Pa_GetErrorText( i_err ) );
330     }
331
332 #endif
333
334     msg_Dbg( p_aout, "portaudio closed");
335     free( p_sys );
336 }
337
338 static int PAOpenDevice( aout_instance_t *p_aout )
339 {
340     aout_sys_t *p_sys = p_aout->output.p_sys;
341     const PaDeviceInfo *p_pdi;
342     PaError i_err;
343     vlc_value_t val, text;
344     int i;
345
346     /* Initialize portaudio */
347     i_err = Pa_Initialize();
348     if( i_err != paNoError )
349     {
350         msg_Err( p_aout, "Pa_Initialize returned %d : %s",
351                  i_err, Pa_GetErrorText( i_err ) );
352
353         return VLC_EGENERIC;
354     }
355
356     p_sys->i_devices = Pa_GetDeviceCount();
357     if( p_sys->i_devices < 0 )
358     {
359         i_err = p_sys->i_devices;
360         msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
361                  Pa_GetErrorText( i_err ) );
362
363         goto error;
364     }
365
366     /* Display all devices info */
367     msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
368     for( i = 0; i < p_sys->i_devices; i++ )
369     {
370         p_pdi = Pa_GetDeviceInfo( i );
371         msg_Dbg( p_aout, "------------------------------------- #%d", i );
372         msg_Dbg( p_aout, "Name         = %s", p_pdi->name );
373         msg_Dbg( p_aout, "Max Inputs   = %d, Max Outputs = %d",
374                   p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
375     }
376     msg_Dbg( p_aout, "-------------------------------------" );
377
378     msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
379     if( p_sys->i_device_id >= p_sys->i_devices )
380     {
381         msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
382         goto error;
383     }
384     p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
385
386     if( p_sys->deviceInfo->maxOutputChannels < 1 )
387     {
388         msg_Err( p_aout, "no channel available" );
389         goto error;
390     }
391
392     if( var_Type( p_aout, "audio-device" ) == 0 )
393     {
394         var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
395         text.psz_string = _("Audio Device");
396         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
397
398         if( p_sys->deviceInfo->maxOutputChannels >= 1 )
399         {
400             val.i_int = AOUT_VAR_MONO;
401             text.psz_string = N_("Mono");
402             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
403                         &val, &text );
404             msg_Dbg( p_aout, "device supports 1 channel" );
405         }
406         if( p_sys->deviceInfo->maxOutputChannels >= 2 )
407         {
408             val.i_int = AOUT_VAR_STEREO;
409             text.psz_string = N_("Stereo");
410             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
411                         &val, &text );
412             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
413                         &val, NULL );
414             var_Set( p_aout, "audio-device", val );
415             msg_Dbg( p_aout, "device supports 2 channels" );
416         }
417         if( p_sys->deviceInfo->maxOutputChannels >= 4 )
418         {
419             val.i_int = AOUT_VAR_2F2R;
420             text.psz_string = N_("2 Front 2 Rear");
421             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
422                         &val, &text );
423             msg_Dbg( p_aout, "device supports 4 channels" );
424         }
425         if( p_sys->deviceInfo->maxOutputChannels >= 5 )
426         {
427             val.i_int = AOUT_VAR_3F2R;
428             text.psz_string = N_("3 Front 2 Rear");
429             var_Change( p_aout, "audio-device",
430                         VLC_VAR_ADDCHOICE, &val, &text );
431             msg_Dbg( p_aout, "device supports 5 channels" );
432         }
433         if( p_sys->deviceInfo->maxOutputChannels >= 6 )
434         {
435             val.i_int = AOUT_VAR_5_1;
436             text.psz_string = "5.1";
437             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
438                         &val, &text );
439             msg_Dbg( p_aout, "device supports 5.1 channels" );
440         }
441
442         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
443
444         val.b_bool = true;
445         var_Set( p_aout, "intf-change", val );
446     }
447
448     /* Audio format is paFloat32 (always supported by portaudio v19) */
449     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
450
451     return VLC_SUCCESS;
452
453  error:
454     if( ( i_err = Pa_Terminate() ) != paNoError )
455     {
456         msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
457     }
458     return VLC_EGENERIC;
459 }
460
461 static int PAOpenStream( aout_instance_t *p_aout )
462 {
463     aout_sys_t *p_sys = p_aout->output.p_sys;
464     const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
465     PaStreamParameters paStreamParameters;
466     vlc_value_t val;
467     int i_channels, i_err;
468     uint32_t i_channel_mask;
469
470     if( var_Get( p_aout, "audio-device", &val ) < 0 )
471     {
472         return VLC_EGENERIC;
473     }
474
475     if( val.i_int == AOUT_VAR_5_1 )
476     {
477         p_aout->output.output.i_physical_channels
478             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
479               | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
480               | AOUT_CHAN_LFE;
481     }
482     else if( val.i_int == AOUT_VAR_3F2R )
483     {
484         p_aout->output.output.i_physical_channels
485             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
486             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
487     }
488     else if( val.i_int == AOUT_VAR_2F2R )
489     {
490         p_aout->output.output.i_physical_channels
491             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
492             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
493     }
494     else if( val.i_int == AOUT_VAR_MONO )
495     {
496         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
497     }
498     else
499     {
500         p_aout->output.output.i_physical_channels
501             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
502     }
503
504     i_channels = aout_FormatNbChannels( &p_aout->output.output );
505     msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
506     i_channel_mask = p_aout->output.output.i_physical_channels;
507
508     /* Calculate the frame size in bytes */
509     p_sys->i_sample_size = 4 * i_channels;
510     p_aout->output.i_nb_samples = FRAME_SIZE;
511     aout_FormatPrepare( &p_aout->output.output );
512     aout_VolumeSoftInit( p_aout );
513
514     /* Check for channel reordering */
515     p_aout->output.p_sys->i_channel_mask = i_channel_mask;
516     p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
517     p_aout->output.p_sys->i_channels = i_channels;
518
519     p_aout->output.p_sys->b_chan_reorder =
520         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
521                                   i_channel_mask, i_channels,
522                                   p_aout->output.p_sys->pi_chan_table );
523
524     if( p_aout->output.p_sys->b_chan_reorder )
525     {
526         msg_Dbg( p_aout, "channel reordering needed" );
527     }
528
529     paStreamParameters.device = p_sys->i_device_id;
530     paStreamParameters.channelCount = i_channels;
531     paStreamParameters.sampleFormat = paFloat32;
532     paStreamParameters.suggestedLatency =
533         p_sys->deviceInfo->defaultLowOutputLatency;
534     paStreamParameters.hostApiSpecificStreamInfo = NULL;
535
536     i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
537                 &paStreamParameters, (double)p_aout->output.output.i_rate,
538                 FRAME_SIZE, paClipOff, paCallback, p_sys );
539     if( i_err != paNoError )
540     {
541         msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
542                  Pa_GetErrorText( i_err ) );
543         if( i_err == paUnanticipatedHostError )
544         {
545             msg_Err( p_aout, "type %d code %ld : %s",
546                      paLastHostErrorInfo->hostApiType,
547                      paLastHostErrorInfo->errorCode,
548                      paLastHostErrorInfo->errorText );
549         }
550         p_sys->p_stream = 0;
551         return VLC_EGENERIC;
552     }
553
554     i_err = Pa_StartStream( p_sys->p_stream );
555     if( i_err != paNoError )
556     {
557         msg_Err( p_aout, "Pa_StartStream() failed" );
558         Pa_CloseStream( p_sys->p_stream );
559         return VLC_EGENERIC;
560     }
561
562     return VLC_SUCCESS;
563 }
564
565 /*****************************************************************************
566  * Play: play sound
567  *****************************************************************************/
568 static void Play( aout_instance_t * p_aout )
569 {
570 }
571
572 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
573 /*****************************************************************************
574  * PORTAUDIOThread: all interactions with libportaudio.a are handled
575  * in this single thread.  Otherwise libportaudio.a is _not_ happy :-(
576  *****************************************************************************/
577 static void PORTAUDIOThread( pa_thread_t *pa_thread )
578 {
579     aout_instance_t *p_aout;
580     aout_sys_t *p_sys;
581     int i_err;
582
583     while( !pa_thread->b_die )
584     {
585         /* Wait for start of stream */
586         vlc_mutex_lock( &pa_thread->lock_signal );
587         if( !pa_thread->b_signal )
588             vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
589         vlc_mutex_unlock( &pa_thread->lock_signal );
590         pa_thread->b_signal = false;
591
592         p_aout = pa_thread->p_aout;
593         p_sys = p_aout->output.p_sys;
594
595         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
596         {
597             msg_Err( p_aout, "cannot open portaudio device" );
598             pa_thread->b_error = true;
599         }
600
601         if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
602         {
603             msg_Err( p_aout, "cannot open portaudio device" );
604             pa_thread->b_error = true;
605
606             i_err = Pa_Terminate();
607             if( i_err != paNoError )
608             {
609                 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
610                          Pa_GetErrorText( i_err ) );
611             }
612         }
613
614         /* Tell the main thread that we are ready */
615         vlc_mutex_lock( &pa_thread->lock_wait );
616         pa_thread->b_wait = true;
617         vlc_cond_signal( &pa_thread->wait );
618         vlc_mutex_unlock( &pa_thread->lock_wait );
619
620         /* Wait for end of stream */
621         vlc_mutex_lock( &pa_thread->lock_signal );
622         if( !pa_thread->b_signal )
623             vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
624         vlc_mutex_unlock( &pa_thread->lock_signal );
625         pa_thread->b_signal = false;
626
627         if( pa_thread->b_error ) continue;
628
629         i_err = Pa_StopStream( p_sys->p_stream );
630         if( i_err != paNoError )
631         {
632             msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
633                      Pa_GetErrorText( i_err ) );
634         }
635         i_err = Pa_CloseStream( p_sys->p_stream );
636         if( i_err != paNoError )
637         {
638             msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
639                      Pa_GetErrorText( i_err ) );
640         }
641         i_err = Pa_Terminate();
642         if( i_err != paNoError )
643         {
644             msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
645                      Pa_GetErrorText( i_err ) );
646         }
647
648         /* Tell the main thread that we are ready */
649         vlc_mutex_lock( &pa_thread->lock_wait );
650         pa_thread->b_wait = true;
651         vlc_cond_signal( &pa_thread->wait );
652         vlc_mutex_unlock( &pa_thread->lock_wait );
653     }
654 }
655 #endif