]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* We now make sure the aout plugin buffers always contain between
[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.81 2002/03/04 22:18:25 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
44 #include "aout_pcm.h"
45 #include "aout_spdif.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  aout_SpawnThread ( aout_thread_t * p_aout );
51
52 /*****************************************************************************
53  * aout_InitBank: initialize the audio output bank.
54  *****************************************************************************/
55 void aout_InitBank ( void )
56 {
57     p_aout_bank->i_count = 0;
58
59     vlc_mutex_init( &p_aout_bank->lock );
60 }
61
62 /*****************************************************************************
63  * aout_EndBank: empty the audio output bank.
64  *****************************************************************************
65  * This function ends all unused audio outputs and empties the bank in
66  * case of success.
67  *****************************************************************************/
68 void aout_EndBank ( void )
69 {
70     /* Ask all remaining audio outputs to die */
71     while( p_aout_bank->i_count )
72     {
73         aout_DestroyThread(
74                 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
75     }
76
77     vlc_mutex_destroy( &p_aout_bank->lock );
78 }
79
80 /*****************************************************************************
81  * aout_CreateThread: initialize audio thread
82  *****************************************************************************/
83 aout_thread_t *aout_CreateThread( int *pi_status, int i_channels, int i_rate )
84 {
85     aout_thread_t * p_aout;                             /* thread descriptor */
86 #if 0
87     int             i_status;                               /* thread status */
88 #endif
89     char *psz_name;
90
91     /* Allocate descriptor */
92     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
93     if( p_aout == NULL )
94     {
95         return( NULL );
96     }
97
98     p_aout->i_latency = 0;
99     p_aout->i_rate = config_GetIntVariable( AOUT_RATE_VAR );
100     p_aout->i_channels = config_GetIntVariable( AOUT_MONO_VAR ) ? 1 : 2;
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->i_rate == 0 )
114     {
115         intf_ErrMsg( "aout error: null sample rate" );
116         free( p_aout );
117         return( NULL );
118     }
119
120     /* Choose the best module */
121     psz_name = config_GetPszVariable( AOUT_METHOD_VAR );
122     p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT, psz_name,
123                                     (void *)p_aout );
124     if( psz_name ) free( psz_name );
125     if( p_aout->p_module == NULL )
126     {
127         intf_ErrMsg( "aout error: no suitable aout module" );
128         free( p_aout );
129         return( NULL );
130     }
131
132 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
133     p_aout->pf_open       = aout_functions.pf_open;
134     p_aout->pf_setformat  = aout_functions.pf_setformat;
135     p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
136     p_aout->pf_play       = aout_functions.pf_play;
137     p_aout->pf_close      = aout_functions.pf_close;
138 #undef aout_functions
139
140     /*
141      * Initialize audio device
142      */
143     if ( p_aout->pf_setformat( p_aout ) )
144     {
145         p_aout->pf_close( p_aout );
146         module_Unneed( p_aout->p_module );
147         free( p_aout );
148         return( NULL );
149     }
150
151     /* Initialize the volume level */
152     p_aout->i_volume = config_GetIntVariable( AOUT_VOLUME_VAR );
153     p_aout->i_savedvolume = 0;
154     
155     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
156      * see vout to handle status correctly ?? however, it is not critical since
157      * this thread is only called in main and all calls are blocking */
158     if( aout_SpawnThread( p_aout ) )
159     {
160         p_aout->pf_close( p_aout );
161         module_Unneed( p_aout->p_module );
162         free( p_aout );
163         return( NULL );
164     }
165
166     return( p_aout );
167 }
168
169 /*****************************************************************************
170  * aout_SpawnThread
171  *****************************************************************************/
172 static int aout_SpawnThread( aout_thread_t * p_aout )
173 {
174     int     i_index, i_bytes;
175     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
176     char   *psz_format;
177
178     /* We want the audio output thread to live */
179     p_aout->b_die = 0;
180     p_aout->b_active = 1;
181
182     /* Initialize the fifos lock */
183     vlc_mutex_init( &p_aout->fifos_lock );
184
185     /* Initialize audio fifos : set all fifos as empty and initialize locks */
186     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
187     {
188         p_aout->fifo[i_index].i_format = AOUT_FIFO_NONE;
189         vlc_mutex_init( &p_aout->fifo[i_index].data_lock );
190         vlc_cond_init( &p_aout->fifo[i_index].data_wait );
191     }
192
193     /* Compute the size (in audio units) of the audio output buffer. Although
194      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
195      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
196     p_aout->i_units = (s64)p_aout->i_rate * AOUT_BUFFER_DURATION / 1000000;
197
198     /* Make pf_aout_thread point to the right thread function, and compute the
199      * byte size of the audio output buffer */
200     switch ( p_aout->i_format )
201     {
202         case AOUT_FMT_U8:
203             pf_aout_thread = aout_PCMThread;
204             psz_format = "unsigned 8 bits";
205             i_bytes = p_aout->i_units * p_aout->i_channels;
206             break;
207
208         case AOUT_FMT_S8:
209             pf_aout_thread = aout_PCMThread;
210             psz_format = "signed 8 bits";
211             i_bytes = p_aout->i_units * p_aout->i_channels;
212             break;
213
214         case AOUT_FMT_U16_LE:
215         case AOUT_FMT_U16_BE:
216             pf_aout_thread = aout_PCMThread;
217             psz_format = "unsigned 16 bits";
218             i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
219             break;
220
221         case AOUT_FMT_S16_LE:
222         case AOUT_FMT_S16_BE:
223             pf_aout_thread = aout_PCMThread;
224             psz_format = "signed 16 bits";
225             i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
226             break;
227
228         case AOUT_FMT_AC3:
229             pf_aout_thread = aout_SpdifThread;
230             psz_format = "ac3 pass-through";
231             i_bytes = SPDIF_FRAME_SIZE;
232             break;
233
234         default:
235             intf_ErrMsg( "aout error: unknown audio output format %i",
236                          p_aout->i_format );
237             return( -1 );
238     }
239
240     /* Allocate the memory needed by the audio output buffers, and set to zero
241      * the s32 buffer's memory */
242     p_aout->buffer = malloc( i_bytes );
243     if ( p_aout->buffer == NULL )
244     {
245         intf_ErrMsg( "aout error: cannot create output buffer" );
246         return( -1 );
247     }
248
249     p_aout->s32_buffer = (s32 *)calloc( p_aout->i_units,
250                                         sizeof(s32) * p_aout->i_channels );
251     if ( p_aout->s32_buffer == NULL )
252     {
253         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
254         free( p_aout->buffer );
255         return( -1 );
256     }
257
258     /* Rough estimate of the playing date */
259     p_aout->date = mdate() + p_main->i_desync;
260
261     /* Launch the thread */
262     if ( vlc_thread_create( &p_aout->thread_id, "audio output",
263                             (vlc_thread_func_t)pf_aout_thread, p_aout ) )
264     {
265         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
266         free( p_aout->buffer );
267         free( p_aout->s32_buffer );
268         return( -1 );
269     }
270
271     intf_WarnMsg( 2, "aout info: %s thread spawned, %i channels, rate %i",
272                      psz_format, p_aout->i_channels, p_aout->i_rate );
273     return( 0 );
274 }
275
276 /*****************************************************************************
277  * aout_DestroyThread
278  *****************************************************************************/
279 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
280 {
281     int i_index;
282     
283     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
284
285     /* Ask thread to kill itself and wait until it's done */
286     p_aout->b_die = 1;
287     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
288
289     /* Free the allocated memory */
290     free( p_aout->buffer );
291     free( p_aout->s32_buffer );
292
293     /* Destroy the condition and mutex locks */
294     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
295     {
296         vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
297         vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
298     }
299     vlc_mutex_destroy( &p_aout->fifos_lock );
300     
301     /* Free the plugin */
302     p_aout->pf_close( p_aout );
303
304     /* Release the aout module */
305     module_Unneed( p_aout->p_module );
306
307     /* Free structure */
308     free( p_aout );
309 }
310