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