]> git.sesse.net Git - vlc/blob - modules/audio_output/portaudio.c
Improvements to preferences
[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 #ifdef WIN32
40 #   define PORTAUDIO_IS_SERIOUSLY_BROKEN 1
41 #endif
42
43 /*****************************************************************************
44  * aout_sys_t: portaudio audio output method descriptor
45  *****************************************************************************/
46 typedef struct pa_thread_t
47 {
48     VLC_COMMON_MEMBERS
49     aout_instance_t *p_aout;
50
51     vlc_cond_t  wait;
52     vlc_mutex_t lock_wait;
53     vlc_bool_t  b_wait;
54     vlc_cond_t  signal;
55     vlc_mutex_t lock_signal;
56     vlc_bool_t  b_signal;
57
58 } pa_thread_t;
59
60 struct aout_sys_t
61 {
62     aout_instance_t *p_aout;
63     PaStream *p_stream;
64
65     PaDeviceIndex i_devices;
66     int i_sample_size;
67     PaDeviceIndex i_device_id;
68     const PaDeviceInfo *deviceInfo;
69
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;
74     uint32_t i_channels;
75 };
76
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 };
87
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 * );
92 #endif
93
94 /*****************************************************************************
95  * Local prototypes.
96  *****************************************************************************/
97 static int  Open        ( vlc_object_t * );
98 static void Close       ( vlc_object_t * );
99 static void Play        ( aout_instance_t * );
100
101 static int PAOpenDevice( aout_instance_t * );
102 static int PAOpenStream( aout_instance_t * );
103
104 /*****************************************************************************
105  * Module descriptor
106  *****************************************************************************/
107 #define DEVICE_TEXT N_("Output device")
108 #define DEVICE_LONGTEXT N_("Portaudio identifier for the output device")
109
110 vlc_module_begin();
111     set_description( N_("PORTAUDIO audio output") );
112     set_category( CAT_AUDIO );
113     set_subcategory( SUBCAT_AUDIO_AOUT );
114     add_integer( "portaudio-device", 0, NULL,
115                  DEVICE_TEXT, DEVICE_LONGTEXT, VLC_FALSE );
116     set_capability( "audio output", 0 );
117     set_callbacks( Open, Close );
118 vlc_module_end();
119
120 /* This routine will be called by the PortAudio engine when audio is needed.
121  * It may called at interrupt level on some machines so don't do anything
122  * that could mess up the system like calling malloc() or free().
123  */
124 static int paCallback( const void *inputBuffer, void *outputBuffer,
125                        unsigned long framesPerBuffer,
126                        const PaStreamCallbackTimeInfo *paDate,
127                        PaStreamCallbackFlags statusFlags, void *p_cookie )
128 {
129     struct aout_sys_t *p_sys = (struct aout_sys_t*) p_cookie;
130     aout_instance_t   *p_aout = p_sys->p_aout;
131     aout_buffer_t     *p_buffer;
132     mtime_t out_date;
133
134     out_date = mdate() + (mtime_t) ( 1000000 *
135         ( paDate->outputBufferDacTime - paDate->currentTime ) );
136     p_buffer = aout_OutputNextBuffer( p_aout, out_date, VLC_TRUE );
137
138     if ( p_buffer != NULL )
139     {
140         if( p_sys->b_chan_reorder )
141         {
142             /* Do the channel reordering here */
143             aout_ChannelReorder( p_buffer->p_buffer, p_buffer->i_nb_bytes,
144                                  p_sys->i_channels, p_sys->pi_chan_table,
145                                  p_sys->i_bits_per_sample );
146         }
147         p_aout->p_vlc->pf_memcpy( outputBuffer, p_buffer->p_buffer,
148                                   framesPerBuffer * p_sys->i_sample_size );
149         /* aout_BufferFree may be dangereous here, but then so is
150          * aout_OutputNextBuffer (calls aout_BufferFree internally).
151          * one solution would be to link the no longer useful buffers
152          * in a second fifo (in aout_OutputNextBuffer too) and to
153          * wait until we are in Play to do the actual free.
154          */
155         aout_BufferFree( p_buffer );
156     }
157     else
158         /* Audio output buffer shortage -> stop the fill process and wait */
159     {
160         p_aout->p_vlc->pf_memset( outputBuffer, 0,
161                                   framesPerBuffer * p_sys->i_sample_size );
162     }
163     return 0;
164 }
165
166 /*****************************************************************************
167  * Open: open the audio device
168  *****************************************************************************/
169 static int Open( vlc_object_t * p_this )
170 {
171     aout_instance_t *p_aout = (aout_instance_t *)p_this;
172     struct aout_sys_t * p_sys;
173     vlc_value_t val;
174     int i_err;
175
176     msg_Dbg( p_aout, "Entering Open()");
177
178     /* Allocate p_sys structure */
179     p_sys = (aout_sys_t *)malloc( sizeof(aout_sys_t) );
180     if( p_sys == NULL )
181     {
182         msg_Err( p_aout, "out of memory" );
183         return VLC_ENOMEM;
184     }
185     p_sys->p_aout = p_aout;
186     p_sys->p_stream = 0;
187     p_aout->output.p_sys = p_sys;
188     p_aout->output.pf_play = Play;
189
190     /* Retrieve output device id from config */
191     var_Create( p_aout, "portaudio-device", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
192     var_Get( p_aout, "portaudio-device", &val );
193     p_sys->i_device_id = val.i_int;
194
195 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
196     if( !b_init )
197     {
198         /* Test device */
199         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
200         {
201             msg_Err( p_aout, "cannot open portaudio device" );
202             free( p_sys );
203             return VLC_EGENERIC;
204         }
205
206         /* Close device for now. We'll re-open it later on */
207         if( ( i_err = Pa_Terminate() ) != paNoError )
208         {
209             msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
210         }
211
212         b_init = VLC_TRUE;
213
214         /* Now we need to setup our DirectSound play notification structure */
215         pa_thread = vlc_object_create( p_aout, sizeof(pa_thread_t) );
216         pa_thread->p_aout = p_aout;
217         pa_thread->b_error = VLC_FALSE;
218         vlc_mutex_init( p_aout, &pa_thread->lock_wait );
219         vlc_cond_init( p_aout, &pa_thread->wait );
220         pa_thread->b_wait = VLC_FALSE;
221         vlc_mutex_init( p_aout, &pa_thread->lock_signal );
222         vlc_cond_init( p_aout, &pa_thread->signal );
223         pa_thread->b_signal = VLC_FALSE;
224
225         /* Create PORTAUDIOThread */
226         if( vlc_thread_create( pa_thread, "aout", PORTAUDIOThread,
227                                VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
228         {
229             msg_Err( p_aout, "cannot create PORTAUDIO thread" );
230             return VLC_EGENERIC;
231         }
232     }
233     else
234     {
235         pa_thread->p_aout = p_aout;
236         pa_thread->b_wait = VLC_FALSE;
237         pa_thread->b_signal = VLC_FALSE;
238         pa_thread->b_error = VLC_FALSE;
239     }
240
241     /* Signal start of stream */
242     vlc_mutex_lock( &pa_thread->lock_signal );
243     pa_thread->b_signal = VLC_TRUE;
244     vlc_cond_signal( &pa_thread->signal );
245     vlc_mutex_unlock( &pa_thread->lock_signal );
246
247     /* Wait until thread is ready */
248     vlc_mutex_lock( &pa_thread->lock_wait );
249     if( !pa_thread->b_wait )
250         vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
251     vlc_mutex_unlock( &pa_thread->lock_wait );
252     pa_thread->b_wait = VLC_FALSE;
253
254     if( pa_thread->b_error )
255     {
256         msg_Err( p_aout, "PORTAUDIO thread failed" );
257         Close( p_this );
258         return VLC_EGENERIC;
259     }
260
261     return VLC_SUCCESS;
262
263 #else
264
265     if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
266     {
267         msg_Err( p_aout, "cannot open portaudio device" );
268         free( p_sys );
269         return VLC_EGENERIC;
270     }
271
272     if( PAOpenStream( p_aout ) != VLC_SUCCESS )
273     {
274         msg_Err( p_aout, "cannot open portaudio device" );
275     }
276
277     return VLC_SUCCESS;
278
279 #endif
280 }
281
282 /*****************************************************************************
283  * Close: close the audio device
284  *****************************************************************************/
285 static void Close ( vlc_object_t *p_this )
286 {
287     aout_instance_t *p_aout = (aout_instance_t *)p_this;
288     aout_sys_t *p_sys = p_aout->output.p_sys;
289     int i_err;
290
291     msg_Dbg( p_aout, "closing portaudio");
292
293 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
294
295     /* Signal end of stream */
296     vlc_mutex_lock( &pa_thread->lock_signal );
297     pa_thread->b_signal = VLC_TRUE;
298     vlc_cond_signal( &pa_thread->signal );
299     vlc_mutex_unlock( &pa_thread->lock_signal );
300
301     /* Wait until thread is ready */
302     vlc_mutex_lock( &pa_thread->lock_wait );
303     if( !pa_thread->b_wait )
304         vlc_cond_wait( &pa_thread->wait, &pa_thread->lock_wait );
305     vlc_mutex_unlock( &pa_thread->lock_wait );
306     pa_thread->b_wait = VLC_FALSE;
307
308 #else
309
310     i_err = Pa_StopStream( p_sys->p_stream );
311     if( i_err != paNoError )
312     {
313         msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
314                  Pa_GetErrorText( i_err ) );
315     }
316     i_err = Pa_CloseStream( p_sys->p_stream );
317     if( i_err != paNoError )
318     {
319         msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
320                  Pa_GetErrorText( i_err ) );
321     }
322
323     i_err = Pa_Terminate();
324     if( i_err != paNoError )
325     {
326         msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
327                  Pa_GetErrorText( i_err ) );
328     }
329
330 #endif
331
332     msg_Dbg( p_aout, "portaudio closed");
333     free( p_sys );
334 }
335
336 static int PAOpenDevice( aout_instance_t *p_aout )
337 {
338     aout_sys_t *p_sys = p_aout->output.p_sys;
339     const PaDeviceInfo *p_pdi;
340     PaError i_err;
341     vlc_value_t val, text;
342     int i;
343
344     /* Initialize portaudio */
345     i_err = Pa_Initialize();
346     if( i_err != paNoError )
347     {
348         msg_Err( p_aout, "Pa_Initialize returned %d : %s",
349                  i_err, Pa_GetErrorText( i_err ) );
350
351         return VLC_EGENERIC;
352     }
353
354     p_sys->i_devices = Pa_GetDeviceCount();
355     if( p_sys->i_devices < 0 )
356     {
357         i_err = p_sys->i_devices;
358         msg_Err( p_aout, "Pa_GetDeviceCount returned %d : %s", i_err,
359                  Pa_GetErrorText( i_err ) );
360
361         goto error;
362     }
363
364     /* Display all devices info */
365     msg_Dbg( p_aout, "number of devices = %d", p_sys->i_devices );
366     for( i = 0; i < p_sys->i_devices; i++ )
367     {
368         p_pdi = Pa_GetDeviceInfo( i );
369         msg_Dbg( p_aout, "------------------------------------- #%d", i );
370         msg_Dbg( p_aout, "Name         = %s", p_pdi->name );
371         msg_Dbg( p_aout, "Max Inputs   = %d, Max Outputs = %d",
372                   p_pdi->maxInputChannels, p_pdi->maxOutputChannels );
373     }
374     msg_Dbg( p_aout, "-------------------------------------" );
375
376     msg_Dbg( p_aout, "requested device is #%d", p_sys->i_device_id );
377     if( p_sys->i_device_id >= p_sys->i_devices )
378     {
379         msg_Err( p_aout, "device %d does not exist", p_sys->i_device_id );
380         goto error;
381     }
382     p_sys->deviceInfo = Pa_GetDeviceInfo( p_sys->i_device_id );
383
384     if( p_sys->deviceInfo->maxOutputChannels < 1 )
385     {
386         msg_Err( p_aout, "no channel available" );
387         goto error;
388     }
389
390     if( var_Type( p_aout, "audio-device" ) == 0 )
391     {
392         var_Create( p_aout, "audio-device", VLC_VAR_INTEGER|VLC_VAR_HASCHOICE);
393         text.psz_string = _("Audio Device");
394         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
395
396         if( p_sys->deviceInfo->maxOutputChannels >= 1 )
397         {
398             val.i_int = AOUT_VAR_MONO;
399             text.psz_string = N_("Mono");
400             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
401                         &val, &text );
402             msg_Dbg( p_aout, "device supports 1 channel" );
403         }
404         if( p_sys->deviceInfo->maxOutputChannels >= 2 )
405         {
406             val.i_int = AOUT_VAR_STEREO;
407             text.psz_string = N_("Stereo");
408             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
409                         &val, &text );
410             var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT,
411                         &val, NULL );
412             var_Set( p_aout, "audio-device", val );
413             msg_Dbg( p_aout, "device supports 2 channels" );
414         }
415         if( p_sys->deviceInfo->maxOutputChannels >= 4 )
416         {
417             val.i_int = AOUT_VAR_2F2R;
418             text.psz_string = N_("2 Front 2 Rear");
419             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
420                         &val, &text );
421             msg_Dbg( p_aout, "device supports 4 channels" );
422         }
423         if( p_sys->deviceInfo->maxOutputChannels >= 5 )
424         {
425             val.i_int = AOUT_VAR_3F2R;
426             text.psz_string = N_("3 Front 2 Rear");
427             var_Change( p_aout, "audio-device",
428                         VLC_VAR_ADDCHOICE, &val, &text );
429             msg_Dbg( p_aout, "device supports 5 channels" );
430         }
431         if( p_sys->deviceInfo->maxOutputChannels >= 6 )
432         {
433             val.i_int = AOUT_VAR_5_1;
434             text.psz_string = N_("5.1");
435             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE,
436                         &val, &text );
437             msg_Dbg( p_aout, "device supports 5.1 channels" );
438         }
439
440         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
441
442         val.b_bool = VLC_TRUE;
443         var_Set( p_aout, "intf-change", val );
444     }
445
446     /* Audio format is paFloat32 (always supported by portaudio v19) */
447     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
448
449     return VLC_SUCCESS;
450
451  error:
452     if( ( i_err = Pa_Terminate() ) != paNoError )
453     {
454         msg_Err( p_aout, "Pa_Terminate returned %d", i_err );
455     }
456     return VLC_EGENERIC;
457 }
458
459 static int PAOpenStream( aout_instance_t *p_aout )
460 {
461     aout_sys_t *p_sys = p_aout->output.p_sys;
462     const PaHostErrorInfo* paLastHostErrorInfo = Pa_GetLastHostErrorInfo();
463     PaStreamParameters paStreamParameters;
464     vlc_value_t val;
465     int i_channels, i_err;
466     uint32_t i_channel_mask;
467
468     if( var_Get( p_aout, "audio-device", &val ) < 0 )
469     {
470         return VLC_EGENERIC;
471     }
472
473     if( val.i_int == AOUT_VAR_5_1 )
474     {
475         p_aout->output.output.i_physical_channels
476             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
477               | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
478               | AOUT_CHAN_LFE;
479     }
480     else if( val.i_int == AOUT_VAR_3F2R )
481     {
482         p_aout->output.output.i_physical_channels
483             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
484             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
485     }
486     else if( val.i_int == AOUT_VAR_2F2R )
487     {
488         p_aout->output.output.i_physical_channels
489             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
490             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
491     }
492     else if( val.i_int == AOUT_VAR_MONO )
493     {
494         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
495     }
496     else
497     {
498         p_aout->output.output.i_physical_channels
499             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
500     }
501
502     i_channels = aout_FormatNbChannels( &p_aout->output.output );
503     msg_Dbg( p_aout, "nb_channels requested = %d", i_channels );
504     i_channel_mask = p_aout->output.output.i_physical_channels;
505
506     /* Calculate the frame size in bytes */
507     p_sys->i_sample_size = 4 * i_channels;
508     p_aout->output.i_nb_samples = FRAME_SIZE;
509     aout_FormatPrepare( &p_aout->output.output );
510     aout_VolumeSoftInit( p_aout );
511
512     /* Check for channel reordering */
513     p_aout->output.p_sys->i_channel_mask = i_channel_mask;
514     p_aout->output.p_sys->i_bits_per_sample = 32; /* forced to paFloat32 */
515     p_aout->output.p_sys->i_channels = i_channels;
516
517     p_aout->output.p_sys->b_chan_reorder =
518         aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
519                                   i_channel_mask, i_channels,
520                                   p_aout->output.p_sys->pi_chan_table );
521
522     if( p_aout->output.p_sys->b_chan_reorder )
523     {
524         msg_Dbg( p_aout, "channel reordering needed" );
525     }
526
527     paStreamParameters.device = p_sys->i_device_id;
528     paStreamParameters.channelCount = i_channels;
529     paStreamParameters.sampleFormat = paFloat32;
530     paStreamParameters.suggestedLatency =
531         p_sys->deviceInfo->defaultLowOutputLatency;
532     paStreamParameters.hostApiSpecificStreamInfo = NULL;
533
534     i_err = Pa_OpenStream( &p_sys->p_stream, NULL /* no input */,
535                 &paStreamParameters, (double)p_aout->output.output.i_rate,
536                 FRAME_SIZE, paClipOff, paCallback, p_sys );
537     if( i_err != paNoError )
538     {
539         msg_Err( p_aout, "Pa_OpenStream returns %d : %s", i_err,
540                  Pa_GetErrorText( i_err ) );
541         if( i_err == paUnanticipatedHostError )
542         {
543             msg_Err( p_aout, "type %d code %ld : %s",
544                      paLastHostErrorInfo->hostApiType,
545                      paLastHostErrorInfo->errorCode,
546                      paLastHostErrorInfo->errorText );
547         }
548         p_sys->p_stream = 0;
549         return VLC_EGENERIC;
550     }
551
552     i_err = Pa_StartStream( p_sys->p_stream );
553     if( i_err != paNoError )
554     {
555         msg_Err( p_aout, "Pa_StartStream() failed" );
556         Pa_CloseStream( p_sys->p_stream );
557         return VLC_EGENERIC;
558     }
559
560     return VLC_SUCCESS;
561 }
562
563 /*****************************************************************************
564  * Play: play sound
565  *****************************************************************************/
566 static void Play( aout_instance_t * p_aout )
567 {
568 }
569
570 #ifdef PORTAUDIO_IS_SERIOUSLY_BROKEN
571 /*****************************************************************************
572  * PORTAUDIOThread: all interactions with libportaudio.a are handled
573  * in this single thread.  Otherwise libportaudio.a is _not_ happy :-(
574  *****************************************************************************/
575 static void PORTAUDIOThread( pa_thread_t *pa_thread )
576 {
577     aout_instance_t *p_aout;
578     aout_sys_t *p_sys;
579     int i_err;
580
581     while( !pa_thread->b_die )
582     {
583         /* Wait for start of stream */
584         vlc_mutex_lock( &pa_thread->lock_signal );
585         if( !pa_thread->b_signal )
586             vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
587         vlc_mutex_unlock( &pa_thread->lock_signal );
588         pa_thread->b_signal = VLC_FALSE;
589
590         p_aout = pa_thread->p_aout;
591         p_sys = p_aout->output.p_sys;
592
593         if( PAOpenDevice( p_aout ) != VLC_SUCCESS )
594         {
595             msg_Err( p_aout, "cannot open portaudio device" );
596             pa_thread->b_error = VLC_TRUE;
597         }
598
599         if( !pa_thread->b_error && PAOpenStream( p_aout ) != VLC_SUCCESS )
600         {
601             msg_Err( p_aout, "cannot open portaudio device" );
602             pa_thread->b_error = VLC_TRUE;
603
604             i_err = Pa_Terminate();
605             if( i_err != paNoError )
606             {
607                 msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
608                          Pa_GetErrorText( i_err ) );
609             }
610         }
611
612         /* Tell the main thread that we are ready */
613         vlc_mutex_lock( &pa_thread->lock_wait );
614         pa_thread->b_wait = VLC_TRUE;
615         vlc_cond_signal( &pa_thread->wait );
616         vlc_mutex_unlock( &pa_thread->lock_wait );
617
618         /* Wait for end of stream */
619         vlc_mutex_lock( &pa_thread->lock_signal );
620         if( !pa_thread->b_signal )
621             vlc_cond_wait( &pa_thread->signal, &pa_thread->lock_signal );
622         vlc_mutex_unlock( &pa_thread->lock_signal );
623         pa_thread->b_signal = VLC_FALSE;
624
625         if( pa_thread->b_error ) continue;
626
627         i_err = Pa_StopStream( p_sys->p_stream );
628         if( i_err != paNoError )
629         {
630             msg_Err( p_aout, "Pa_StopStream: %d (%s)", i_err,
631                      Pa_GetErrorText( i_err ) );
632         }
633         i_err = Pa_CloseStream( p_sys->p_stream );
634         if( i_err != paNoError )
635         {
636             msg_Err( p_aout, "Pa_CloseStream: %d (%s)", i_err,
637                      Pa_GetErrorText( i_err ) );
638         }
639         i_err = Pa_Terminate();
640         if( i_err != paNoError )
641         {
642             msg_Err( p_aout, "Pa_Terminate: %d (%s)", i_err,
643                      Pa_GetErrorText( i_err ) );
644         }
645
646         /* Tell the main thread that we are ready */
647         vlc_mutex_lock( &pa_thread->lock_wait );
648         pa_thread->b_wait = VLC_TRUE;
649         vlc_cond_signal( &pa_thread->wait );
650         vlc_mutex_unlock( &pa_thread->lock_wait );
651     }
652 }
653 #endif