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