]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* Added error checking in pthread wrapper ; as a result, intf_msg.h must
[vlc] / src / audio_output / audio_output.c
1 /*****************************************************************************
2  * audio_output.c : audio output thread
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: audio_output.c,v 1.66 2001/11/28 15:08:06 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>                                              /* getpid() */
31 #endif
32
33 #ifdef WIN32                   /* getpid() for win32 is located in process.h */
34 #include <process.h>
35 #endif
36
37 #include <stdio.h>                                           /* "intf_msg.h" */
38 #include <stdlib.h>                            /* calloc(), malloc(), free() */
39 #include <string.h>
40
41 #include "config.h"
42 #include "common.h"
43 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
44 #include "threads.h"
45 #include "mtime.h"                             /* mtime_t, mdate(), msleep() */
46 #include "modules.h"
47
48 #include "audio_output.h"
49 #include "aout_common.h"
50
51 #include "main.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int aout_SpawnThread( aout_thread_t * p_aout );
57
58 /*****************************************************************************
59  * aout_InitBank: initialize the audio output bank.
60  *****************************************************************************/
61 void aout_InitBank ( void )
62 {
63     p_aout_bank->i_count = 0;
64
65     vlc_mutex_init( &p_aout_bank->lock );
66 }
67
68 /*****************************************************************************
69  * aout_EndBank: empty the audio output bank.
70  *****************************************************************************
71  * This function ends all unused audio outputs and empties the bank in
72  * case of success.
73  *****************************************************************************/
74 void aout_EndBank ( void )
75 {
76     /* Ask all remaining audio outputs to die */
77     while( p_aout_bank->i_count )
78     {
79         aout_DestroyThread(
80                 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
81     }
82
83     vlc_mutex_destroy( &p_aout_bank->lock );
84 }
85
86 /*****************************************************************************
87  * aout_CreateThread: initialize audio thread
88  *****************************************************************************/
89 aout_thread_t *aout_CreateThread( int *pi_status )
90 {
91     aout_thread_t * p_aout;                             /* thread descriptor */
92 #if 0
93     int             i_status;                               /* thread status */
94 #endif
95
96     /* Allocate descriptor */
97     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
98     if( p_aout == NULL )
99     {
100         return( NULL );
101     }
102
103     /* Choose the best module */
104     p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT, NULL );
105
106     if( p_aout->p_module == NULL )
107     {
108         intf_ErrMsg( "aout error: no suitable aout module" );
109         free( p_aout );
110         return( NULL );
111     }
112
113 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
114     p_aout->pf_open       = aout_functions.pf_open;
115     p_aout->pf_setformat  = aout_functions.pf_setformat;
116     p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
117     p_aout->pf_play       = aout_functions.pf_play;
118     p_aout->pf_close      = aout_functions.pf_close;
119 #undef aout_functions
120
121     /*
122      * Initialize audio device
123      */
124     if ( p_aout->pf_open( p_aout ) )
125     {
126         module_Unneed( p_aout->p_module );
127         free( p_aout );
128         return( NULL );
129     }
130
131     if( p_aout->l_rate == 0 )
132     {
133         intf_ErrMsg( "aout error: null sample rate" );
134         p_aout->pf_close( p_aout );
135         module_Unneed( p_aout->p_module );
136         free( p_aout );
137         return( NULL );
138     }
139
140     /* special setting for ac3 pass-through mode */
141     if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
142     {
143         intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
144         p_aout->i_format = AOUT_FMT_AC3;
145         p_aout->l_rate = 48000;
146     }
147
148     /* FIXME: only works for i_channels == 1 or 2 ?? */
149     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
150
151     if ( p_aout->pf_setformat( p_aout ) )
152     {
153         p_aout->pf_close( p_aout );
154         module_Unneed( p_aout->p_module );
155         free( p_aout );
156         return( NULL );
157     }
158
159     /* Initialize the volume level */
160     p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
161     p_aout->i_savedvolume = 0;
162     
163     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
164      * see vout to handle status correctly ?? however, it is not critical since
165      * this thread is only called in main and all calls are blocking */
166     if( aout_SpawnThread( p_aout ) )
167     {
168         p_aout->pf_close( p_aout );
169         module_Unneed( p_aout->p_module );
170         free( p_aout );
171         return( NULL );
172     }
173
174     return( p_aout );
175 }
176
177 /*****************************************************************************
178  * aout_SpawnThread
179  *****************************************************************************/
180 static int aout_SpawnThread( aout_thread_t * p_aout )
181 {
182     int     i_fifo;
183     long    l_bytes;
184     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
185
186     /* We want the audio output thread to live */
187     p_aout->b_die = 0;
188     p_aout->b_active = 1;
189
190     /* Initialize the fifos lock */
191     vlc_mutex_init( &p_aout->fifos_lock );
192     /* Initialize audio fifos : set all fifos as empty and initialize locks */
193     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
194     {
195         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
196         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
197         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
198     }
199
200     /* Compute the size (in audio units) of the audio output buffer. Although
201      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
202      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
203     p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
204     p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
205
206     /* Make pf_aout_thread point to the right thread function, and compute the
207      * byte size of the audio output buffer */
208     switch ( p_aout->i_channels )
209     {
210     /* Audio output is mono */
211     case 1:
212         switch ( p_aout->i_format )
213         {
214         case AOUT_FMT_U8:
215             intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
216             l_bytes = 1 * sizeof(u8) * p_aout->l_units;
217             pf_aout_thread = aout_U8MonoThread;
218             break;
219
220         case AOUT_FMT_S8:
221             intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
222             l_bytes = 1 * sizeof(s8) * p_aout->l_units;
223             pf_aout_thread = aout_S8MonoThread;
224             break;
225
226         case AOUT_FMT_U16_LE:
227         case AOUT_FMT_U16_BE:
228             intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
229             l_bytes = 1 * sizeof(u16) * p_aout->l_units;
230             pf_aout_thread = aout_U16MonoThread;
231             break;
232
233         case AOUT_FMT_S16_LE:
234         case AOUT_FMT_S16_BE:
235             intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
236             l_bytes = 1 * sizeof(s16) * p_aout->l_units;
237             pf_aout_thread = aout_S16MonoThread;
238             break;
239
240         default:
241             intf_ErrMsg( "aout error: unknown audio output format (%i)",
242                          p_aout->i_format );
243             return( -1 );
244         }
245         break;
246
247     /* Audio output is stereo */
248     case 2:
249         switch ( p_aout->i_format )
250         {
251         case AOUT_FMT_U8:
252             intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
253             l_bytes = 2 * sizeof(u8) * p_aout->l_units;
254             pf_aout_thread = aout_U8StereoThread;
255             break;
256
257         case AOUT_FMT_S8:
258             intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
259             l_bytes = 2 * sizeof(s8) * p_aout->l_units;
260             pf_aout_thread = aout_S8StereoThread;
261             break;
262
263         case AOUT_FMT_U16_LE:
264         case AOUT_FMT_U16_BE:
265             intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
266             l_bytes = 2 * sizeof(u16) * p_aout->l_units;
267             pf_aout_thread = aout_U16StereoThread;
268             break;
269
270         case AOUT_FMT_S16_LE:
271         case AOUT_FMT_S16_BE:
272             intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
273             l_bytes = 2 * sizeof(s16) * p_aout->l_units;
274             pf_aout_thread = aout_S16StereoThread;
275             break;
276
277         case AOUT_FMT_AC3:
278             intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
279             l_bytes = SPDIF_FRAME_SIZE;
280             pf_aout_thread = aout_SpdifThread;
281             break;
282
283         default:
284             intf_ErrMsg( "aout error: unknown audio output format %i",
285                          p_aout->i_format );
286             return( -1 );
287         }
288         break;
289
290     default:
291         intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
292                      p_aout->i_channels );
293         return( -1 );
294     }
295
296     /* Allocate the memory needed by the audio output buffers, and set to zero
297      * the s32 buffer's memory */
298     p_aout->buffer = malloc( l_bytes );
299     if ( p_aout->buffer == NULL )
300     {
301         intf_ErrMsg( "aout error: cannot create output buffer" );
302         return( -1 );
303     }
304
305     p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
306                                         sizeof(s32) << ( p_aout->b_stereo ) );
307     if ( p_aout->s32_buffer == NULL )
308     {
309         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
310         free( p_aout->buffer );
311         return( -1 );
312     }
313
314     /* Rough estimate of the playing date */
315     p_aout->date = mdate();
316
317     /* Launch the thread */
318     if ( vlc_thread_create( &p_aout->thread_id, "audio output",
319                             (vlc_thread_func_t)pf_aout_thread, p_aout ) )
320     {
321         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
322         free( p_aout->buffer );
323         free( p_aout->s32_buffer );
324         return( -1 );
325     }
326
327     intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
328     return( 0 );
329 }
330
331 /*****************************************************************************
332  * aout_DestroyThread
333  *****************************************************************************/
334 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
335 {
336     int i_fifo;
337     
338     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
339
340     /* Ask thread to kill itself and wait until it's done */
341     p_aout->b_die = 1;
342     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
343
344     /* Free the allocated memory */
345     free( p_aout->buffer );
346     free( p_aout->s32_buffer );
347
348     /* Destroy the condition and mutex locks */
349     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
350     {
351         vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
352         vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
353     }
354     vlc_mutex_destroy( &p_aout->fifos_lock );
355     
356     /* Free the plugin */
357     p_aout->pf_close( p_aout );
358
359     /* Release the aout module */
360     module_Unneed( p_aout->p_module );
361
362     /* Free structure */
363     free( p_aout );
364 }
365