]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[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.56 2001/03/21 13:42:34 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
43 #include <stdio.h>                                           /* "intf_msg.h" */
44 #include <stdlib.h>                            /* calloc(), malloc(), free() */
45
46 #include "config.h"
47 #include "common.h"
48 #include "threads.h"
49 #include "mtime.h"                             /* mtime_t, mdate(), msleep() */
50 #include "modules.h"
51
52 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
53
54 #include "audio_output.h"
55 #include "aout_common.h"
56
57 #include "main.h"
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int aout_SpawnThread( aout_thread_t * p_aout );
63
64 /*****************************************************************************
65  * aout_CreateThread: initialize audio thread
66  *****************************************************************************/
67 aout_thread_t *aout_CreateThread( int *pi_status )
68 {
69     aout_thread_t * p_aout;                             /* thread descriptor */
70 #if 0
71     int             i_status;                               /* thread status */
72 #endif
73
74     /* Allocate descriptor */
75     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
76     if( p_aout == NULL )
77     {
78         return( NULL );
79     }
80
81     /* Choose the best module */
82     p_aout->p_module = module_Need( p_main->p_bank,
83                                     MODULE_CAPABILITY_AOUT, NULL );
84
85     if( p_aout->p_module == NULL )
86     {
87         intf_ErrMsg( "aout error: no suitable aout module" );
88         free( p_aout );
89         return( NULL );
90     }
91
92 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
93     p_aout->pf_open       = aout_functions.pf_open;
94     p_aout->pf_setformat  = aout_functions.pf_setformat;
95     p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
96     p_aout->pf_play       = aout_functions.pf_play;
97     p_aout->pf_close      = aout_functions.pf_close;
98 #undef aout_functions
99
100     /*
101      * Initialize audio device
102      */
103     if ( p_aout->pf_open( p_aout ) )
104     {
105         module_Unneed( p_main->p_bank, p_aout->p_module );
106         free( p_aout );
107         return( NULL );
108     }
109
110     if( p_aout->l_rate == 0 )
111     {
112         intf_ErrMsg( "aout error: null sample rate" );
113         p_aout->pf_close( p_aout );
114         module_Unneed( p_main->p_bank, p_aout->p_module );
115         free( p_aout );
116         return( NULL );
117     }
118
119     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0; /* FIXME: only works
120                                                    for i_channels == 1 or 2 ??*/
121
122     if ( p_aout->pf_setformat( p_aout ) )
123     {
124         p_aout->pf_close( p_aout );
125         module_Unneed( p_main->p_bank, p_aout->p_module );
126         free( p_aout );
127         return( NULL );
128     }
129
130     /* Initialize the volume level */
131     p_aout->vol = VOLUME_DEFAULT;
132     
133     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
134      * see vout to handle status correctly ?? however, it is not critical since
135      * this thread is only called in main and all calls are blocking */
136     if( aout_SpawnThread( p_aout ) )
137     {
138         p_aout->pf_close( p_aout );
139         module_Unneed( p_main->p_bank, p_aout->p_module );
140         free( p_aout );
141         return( NULL );
142     }
143
144     return( p_aout );
145 }
146
147 /*****************************************************************************
148  * aout_SpawnThread
149  *****************************************************************************/
150 static int aout_SpawnThread( aout_thread_t * p_aout )
151 {
152     int             i_fifo;
153     long            l_bytes;
154     void *          aout_thread = NULL;
155
156     /* We want the audio output thread to live */
157     p_aout->b_die = 0;
158     p_aout->b_active = 1;
159
160     /* Initialize the fifos lock */
161     vlc_mutex_init( &p_aout->fifos_lock );
162     /* Initialize audio fifos : set all fifos as empty and initialize locks */
163     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
164     {
165         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
166         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
167         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
168     }
169
170     /* Compute the size (in audio units) of the audio output buffer. Although
171      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
172      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
173     p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
174     p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
175
176     /* Make aout_thread point to the right thread function, and compute the
177      * byte size of the audio output buffer */
178     switch ( p_aout->i_channels )
179     {
180     /* Audio output is mono */
181     case 1:
182         switch ( p_aout->i_format )
183         {
184         case AOUT_FMT_U8:
185             intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
186             l_bytes = 1 * sizeof(u8) * p_aout->l_units;
187             aout_thread = (void *)aout_U8MonoThread;
188             break;
189
190         case AOUT_FMT_S8:
191             intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
192             l_bytes = 1 * sizeof(s8) * p_aout->l_units;
193             aout_thread = (void *)aout_S8MonoThread;
194             break;
195
196         case AOUT_FMT_U16_LE:
197         case AOUT_FMT_U16_BE:
198             intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
199             l_bytes = 1 * sizeof(u16) * p_aout->l_units;
200             aout_thread = (void *)aout_U16MonoThread;
201             break;
202
203         case AOUT_FMT_S16_LE:
204         case AOUT_FMT_S16_BE:
205             intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
206             l_bytes = 1 * sizeof(s16) * p_aout->l_units;
207             aout_thread = (void *)aout_S16MonoThread;
208             break;
209
210                 default:
211             intf_ErrMsg( "aout error: unknown audio output format (%i)",
212                          p_aout->i_format );
213             return( -1 );
214         }
215         break;
216
217     /* Audio output is stereo */
218     case 2:
219         switch ( p_aout->i_format )
220         {
221         case AOUT_FMT_U8:
222             intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
223             l_bytes = 2 * sizeof(u8) * p_aout->l_units;
224             aout_thread = (void *)aout_U8StereoThread;
225             break;
226
227         case AOUT_FMT_S8:
228             intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
229             l_bytes = 2 * sizeof(s8) * p_aout->l_units;
230             aout_thread = (void *)aout_S8StereoThread;
231             break;
232
233         case AOUT_FMT_U16_LE:
234         case AOUT_FMT_U16_BE:
235             intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
236             l_bytes = 2 * sizeof(u16) * p_aout->l_units;
237             aout_thread = (void *)aout_U16StereoThread;
238             break;
239
240         case AOUT_FMT_S16_LE:
241         case AOUT_FMT_S16_BE:
242             intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
243             l_bytes = 2 * sizeof(s16) * p_aout->l_units;
244             aout_thread = (void *)aout_S16StereoThread;
245             break;
246
247         default:
248             intf_ErrMsg( "aout error: unknown audio output format %i",
249                          p_aout->i_format );
250             return( -1 );
251         }
252         break;
253
254     default:
255         intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
256                      p_aout->i_channels );
257         return( -1 );
258     }
259
260     /* Allocate the memory needed by the audio output buffers, and set to zero
261      * the s32 buffer's memory */
262     p_aout->buffer = malloc( l_bytes );
263     if ( p_aout->buffer == NULL )
264     {
265         intf_ErrMsg( "aout error: cannot create output buffer" );
266         return( -1 );
267     }
268
269     p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
270                                         sizeof(s32) << ( p_aout->b_stereo ) );
271     if ( p_aout->s32_buffer == NULL )
272     {
273         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
274         free( p_aout->buffer );
275         return( -1 );
276     }
277
278     /* Rough estimate of the playing date */
279     p_aout->date = mdate();
280
281     /* Launch the thread */
282     if ( vlc_thread_create( &p_aout->thread_id, "audio output",
283                             (vlc_thread_func_t)aout_thread, p_aout ) )
284     {
285         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
286         free( p_aout->buffer );
287         free( p_aout->s32_buffer );
288         return( -1 );
289     }
290
291     intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
292     return( 0 );
293 }
294
295 /*****************************************************************************
296  * aout_DestroyThread
297  *****************************************************************************/
298 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
299 {
300
301     int i_fifo;
302     
303     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
304
305     /* Ask thread to kill itself and wait until it's done */
306     p_aout->b_die = 1;
307     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
308
309     /* Free the allocated memory */
310     free( p_aout->buffer );
311     free( p_aout->s32_buffer );
312
313     /* Destroy the condition and mutex locks */
314     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
315     {
316         vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
317         vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
318     }
319     vlc_mutex_destroy( &p_aout->fifos_lock );
320     
321     /* Free the plugin */
322     p_aout->pf_close( p_aout );
323
324     /* Release the aout module */
325     module_Unneed( p_main->p_bank, p_aout->p_module );
326
327     /* Free structure */
328     free( p_aout );
329 }
330