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