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