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