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