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