]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
ca4149bda7645b3eb42c46fb805acf8ca749d5fe
[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.75 2002/01/28 23:08:31 stef 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 = main_GetIntVariable( AOUT_RATE_VAR, l_rate );
97     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
98                                                   i_channels - 1 );
99
100     intf_WarnMsg( 3, "aout_CreateThread: channels == %d, rate == %d",
101                          p_aout->i_channels,
102                          p_aout->l_rate );
103     
104     /* Maybe we should pass this setting in argument */
105     p_aout->i_format = AOUT_FORMAT_DEFAULT;
106
107     /* special setting for ac3 pass-through mode */
108     /* FIXME is it necessary ? (cf ac3_adec.c) */
109     if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
110     {
111         intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
112         p_aout->i_format = AOUT_FMT_AC3;
113     }
114     
115     if( p_aout->l_rate == 0 )
116     {
117         intf_ErrMsg( "aout error: null sample rate" );
118         free( p_aout );
119         return( NULL );
120     }
121
122     /* FIXME: only works for i_channels == 1 or 2 ?? */
123     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
124
125     /* Choose the best module */
126     p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT,
127                            main_GetPszVariable( AOUT_METHOD_VAR, NULL ), 
128                            NULL );
129
130     if( p_aout->p_module == NULL )
131     {
132         intf_ErrMsg( "aout error: no suitable aout module" );
133         free( p_aout );
134         return( NULL );
135     }
136
137 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
138     p_aout->pf_open       = aout_functions.pf_open;
139     p_aout->pf_setformat  = aout_functions.pf_setformat;
140     p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
141     p_aout->pf_play       = aout_functions.pf_play;
142     p_aout->pf_close      = aout_functions.pf_close;
143 #undef aout_functions
144
145     /*
146      * Initialize audio device
147      */
148     if ( p_aout->pf_open( p_aout ) )
149     {
150         module_Unneed( p_aout->p_module );
151         free( p_aout );
152         return( NULL );
153     }
154
155     if ( p_aout->pf_setformat( p_aout ) )
156     {
157         p_aout->pf_close( p_aout );
158         module_Unneed( p_aout->p_module );
159         free( p_aout );
160         return( NULL );
161     }
162
163     /* Initialize the volume level */
164     p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
165     p_aout->i_savedvolume = 0;
166     
167     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
168      * see vout to handle status correctly ?? however, it is not critical since
169      * this thread is only called in main and all calls are blocking */
170     if( aout_SpawnThread( p_aout ) )
171     {
172         p_aout->pf_close( p_aout );
173         module_Unneed( p_aout->p_module );
174         free( p_aout );
175         return( NULL );
176     }
177
178     return( p_aout );
179 }
180
181
182 /*****************************************************************************
183  * Declare the different aout thread functions
184  *****************************************************************************/
185 DECLARE_AOUT_THREAD( S16, s16, ( p_aout->s32_buffer[l_buffer] / 
186         AOUT_MAX_FIFOS ) )
187
188 DECLARE_AOUT_THREAD( U16, u16, (( p_aout->s32_buffer[l_buffer] / 
189         AOUT_MAX_FIFOS) + 128) )
190         
191 DECLARE_AOUT_THREAD( U8, u8, (( p_aout->s32_buffer[l_buffer] / 
192         AOUT_MAX_FIFOS / 256) + 128 ) )
193
194 DECLARE_AOUT_THREAD( S8, s8, ( p_aout->s32_buffer[l_buffer] / 
195         AOUT_MAX_FIFOS / 256) )
196         
197
198 /*****************************************************************************
199  * aout_SpawnThread
200  *****************************************************************************/
201 static int aout_SpawnThread( aout_thread_t * p_aout )
202 {
203     int     i_fifo;
204     long    l_bytes;
205     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
206
207     /* We want the audio output thread to live */
208     p_aout->b_die = 0;
209     p_aout->b_active = 1;
210
211     /* Initialize the fifos lock */
212     vlc_mutex_init( &p_aout->fifos_lock );
213     /* Initialize audio fifos : set all fifos as empty and initialize locks */
214     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
215     {
216         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
217         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
218         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
219     }
220
221     /* Compute the size (in audio units) of the audio output buffer. Although
222      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
223      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
224     p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
225     p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
226
227     /* Make pf_aout_thread point to the right thread function, and compute the
228      * byte size of the audio output buffer */
229     switch ( p_aout->i_channels )
230     {
231     /* Audio output is mono */
232     case 1:
233         switch ( p_aout->i_format )
234         {
235         case AOUT_FMT_U8:
236             intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
237             l_bytes = 1 * sizeof(u8) * p_aout->l_units;
238             pf_aout_thread = aout_U8Thread;
239             break;
240
241         case AOUT_FMT_S8:
242             intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
243             l_bytes = 1 * sizeof(s8) * p_aout->l_units;
244             pf_aout_thread = aout_S8Thread;
245             break;
246
247         case AOUT_FMT_U16_LE:
248         case AOUT_FMT_U16_BE:
249             intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
250             l_bytes = 1 * sizeof(u16) * p_aout->l_units;
251             pf_aout_thread = aout_U16Thread;
252             break;
253
254         case AOUT_FMT_S16_LE:
255         case AOUT_FMT_S16_BE:
256             intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
257             l_bytes = 1 * sizeof(s16) * p_aout->l_units;
258             pf_aout_thread = aout_S16Thread;
259             break;
260
261         default:
262             intf_ErrMsg( "aout error: unknown audio output format (%i)",
263                          p_aout->i_format );
264             return( -1 );
265         }
266         break;
267
268     /* Audio output is stereo */
269     case 2:
270         switch ( p_aout->i_format )
271         {
272         case AOUT_FMT_U8:
273             intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
274             l_bytes = 2 * sizeof(u8) * p_aout->l_units;
275             pf_aout_thread = aout_U8Thread;
276             break;
277
278         case AOUT_FMT_S8:
279             intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
280             l_bytes = 2 * sizeof(s8) * p_aout->l_units;
281             pf_aout_thread = aout_S8Thread;
282             break;
283
284         case AOUT_FMT_U16_LE:
285         case AOUT_FMT_U16_BE:
286             intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
287             l_bytes = 2 * sizeof(u16) * p_aout->l_units;
288             pf_aout_thread = aout_U16Thread;
289             break;
290
291         case AOUT_FMT_S16_LE:
292         case AOUT_FMT_S16_BE:
293             intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
294             l_bytes = 2 * sizeof(s16) * p_aout->l_units;
295             pf_aout_thread = aout_S16Thread;
296             break;
297
298         case AOUT_FMT_AC3:
299             intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
300             l_bytes = SPDIF_FRAME_SIZE;
301             pf_aout_thread = aout_SpdifThread;
302             break;
303
304         default:
305             intf_ErrMsg( "aout error: unknown audio output format %i",
306                          p_aout->i_format );
307             return( -1 );
308         }
309         break;
310
311     default:
312         intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
313                      p_aout->i_channels );
314         return( -1 );
315     }
316
317     /* Allocate the memory needed by the audio output buffers, and set to zero
318      * the s32 buffer's memory */
319     p_aout->buffer = malloc( l_bytes );
320     if ( p_aout->buffer == NULL )
321     {
322         intf_ErrMsg( "aout error: cannot create output buffer" );
323         return( -1 );
324     }
325
326     p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
327                                         sizeof(s32) << ( p_aout->b_stereo ) );
328     if ( p_aout->s32_buffer == NULL )
329     {
330         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
331         free( p_aout->buffer );
332         return( -1 );
333     }
334
335     /* Rough estimate of the playing date */
336     p_aout->date = mdate() + p_main->i_desync;
337
338     /* Launch the thread */
339     if ( vlc_thread_create( &p_aout->thread_id, "audio output",
340                             (vlc_thread_func_t)pf_aout_thread, p_aout ) )
341     {
342         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
343         free( p_aout->buffer );
344         free( p_aout->s32_buffer );
345         return( -1 );
346     }
347
348     intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
349     return( 0 );
350 }
351
352 /*****************************************************************************
353  * aout_DestroyThread
354  *****************************************************************************/
355 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
356 {
357     int i_fifo;
358     
359     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
360
361     /* Ask thread to kill itself and wait until it's done */
362     p_aout->b_die = 1;
363     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
364
365     /* Free the allocated memory */
366     free( p_aout->buffer );
367     free( p_aout->s32_buffer );
368
369     /* Destroy the condition and mutex locks */
370     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
371     {
372         vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
373         vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
374     }
375     vlc_mutex_destroy( &p_aout->fifos_lock );
376     
377     /* Free the plugin */
378     p_aout->pf_close( p_aout );
379
380     /* Release the aout module */
381     module_Unneed( p_aout->p_module );
382
383     /* Free structure */
384     free( p_aout );
385 }
386