]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
* Split audio output into several separate files to make it easier
[vlc] / src / audio_output / audio_output.c
1 /*****************************************************************************
2  * audio_output.c : audio output thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  *
6  * Authors: Michel Kaempf <maxx@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /* TODO:
24  *
25  * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
26  *   inline
27  * - Faire les optimisations dans les fonctions threads :
28  *   = Stocker les "petits calculs" dans des variables au lieu de les refaire
29  *     à chaque boucle
30  *   = Utiliser des tables pour les gros calculs
31  * - Faire une structure différente pour intf/adec fifo
32  *
33  */
34
35 /*****************************************************************************
36  * Preamble
37  *****************************************************************************/
38 #include "defs.h"
39
40 #include <unistd.h>                                              /* getpid() */
41
42 #include <stdio.h>                                           /* "intf_msg.h" */
43 #include <stdlib.h>                            /* calloc(), malloc(), free() */
44
45 #include "config.h"
46 #include "common.h"
47 #include "threads.h"
48 #include "mtime.h"                             /* mtime_t, mdate(), msleep() */
49 #include "modules.h"
50
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52
53 #include "audio_output.h"
54 #include "aout_common.h"
55
56 #include "main.h"
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int aout_SpawnThread( aout_thread_t * p_aout );
62
63 /*****************************************************************************
64  * aout_CreateThread: initialize audio thread
65  *****************************************************************************/
66 aout_thread_t *aout_CreateThread( int *pi_status )
67 {
68     aout_thread_t * p_aout;                             /* thread descriptor */
69 #if 0
70     int             i_status;                               /* thread status */
71 #endif
72
73     /* Allocate descriptor */
74     p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
75     if( p_aout == NULL )
76     {
77         return( NULL );
78     }
79
80     /* Choose the best module */
81     p_aout->p_module = module_Need( p_main->p_bank,
82                                     MODULE_CAPABILITY_AOUT, NULL );
83
84     if( p_aout->p_module == NULL )
85     {
86         intf_ErrMsg( "aout error: no suitable aout module" );
87         free( p_aout );
88         return( NULL );
89     }
90
91 #define aout_functions p_aout->p_module->p_functions->aout.functions.aout
92     p_aout->pf_open       = aout_functions.pf_open;
93     p_aout->pf_setformat  = aout_functions.pf_setformat;
94     p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
95     p_aout->pf_play       = aout_functions.pf_play;
96     p_aout->pf_close      = aout_functions.pf_close;
97 #undef aout_functions
98
99     /*
100      * Initialize audio device
101      */
102     if ( p_aout->pf_open( p_aout ) )
103     {
104         module_Unneed( p_main->p_bank, p_aout->p_module );
105         free( p_aout );
106         return( NULL );
107     }
108
109     if( p_aout->l_rate == 0 )
110     {
111         intf_ErrMsg( "aout error: null sample rate" );
112         p_aout->pf_close( p_aout );
113         module_Unneed( p_main->p_bank, p_aout->p_module );
114         free( p_aout );
115         return( NULL );
116     }
117
118     p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0; /* FIXME: only works
119                                                    for i_channels == 1 or 2 ??*/
120
121     if ( p_aout->pf_setformat( p_aout ) )
122     {
123         p_aout->pf_close( p_aout );
124         module_Unneed( p_main->p_bank, p_aout->p_module );
125         free( p_aout );
126         return( NULL );
127     }
128
129     /* Initialize the volume level */
130     p_aout->vol = VOLUME_DEFAULT;
131     
132     /* FIXME: maybe it would be cleaner to change SpawnThread prototype
133      * see vout to handle status correctly ?? however, it is not critical since
134      * this thread is only called in main and all calls are blocking */
135     if( aout_SpawnThread( p_aout ) )
136     {
137         p_aout->pf_close( p_aout );
138         module_Unneed( p_main->p_bank, p_aout->p_module );
139         free( p_aout );
140         return( NULL );
141     }
142
143     return( p_aout );
144 }
145
146 /*****************************************************************************
147  * aout_SpawnThread
148  *****************************************************************************/
149 static int aout_SpawnThread( aout_thread_t * p_aout )
150 {
151     int             i_fifo;
152     long            l_bytes;
153     void *          aout_thread = NULL;
154
155     /* We want the audio output thread to live */
156     p_aout->b_die = 0;
157     p_aout->b_active = 1;
158
159     /* Initialize the fifos lock */
160     vlc_mutex_init( &p_aout->fifos_lock );
161     /* Initialize audio fifos : set all fifos as empty and initialize locks */
162     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
163     {
164         p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
165         vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
166         vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
167     }
168
169     /* Compute the size (in audio units) of the audio output buffer. Although
170      * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
171      * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
172     p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
173     p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
174
175     /* Make aout_thread point to the right thread function, and compute the
176      * byte size of the audio output buffer */
177     switch ( p_aout->i_channels )
178     {
179     /* Audio output is mono */
180     case 1:
181         switch ( p_aout->i_format )
182         {
183         case AOUT_FMT_U8:
184             intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
185             l_bytes = 1 * sizeof(u8) * p_aout->l_units;
186             aout_thread = (void *)aout_U8MonoThread;
187             break;
188
189         case AOUT_FMT_S8:
190             intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
191             l_bytes = 1 * sizeof(s8) * p_aout->l_units;
192             aout_thread = (void *)aout_S8MonoThread;
193             break;
194
195         case AOUT_FMT_U16_LE:
196         case AOUT_FMT_U16_BE:
197             intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
198             l_bytes = 1 * sizeof(u16) * p_aout->l_units;
199             aout_thread = (void *)aout_U16MonoThread;
200             break;
201
202         case AOUT_FMT_S16_LE:
203         case AOUT_FMT_S16_BE:
204             intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
205             l_bytes = 1 * sizeof(s16) * p_aout->l_units;
206             aout_thread = (void *)aout_S16MonoThread;
207             break;
208
209                 default:
210             intf_ErrMsg( "aout error: unknown audio output format (%i)",
211                          p_aout->i_format );
212             return( -1 );
213         }
214         break;
215
216     /* Audio output is stereo */
217     case 2:
218         switch ( p_aout->i_format )
219         {
220         case AOUT_FMT_U8:
221             intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
222             l_bytes = 2 * sizeof(u8) * p_aout->l_units;
223             aout_thread = (void *)aout_U8StereoThread;
224             break;
225
226         case AOUT_FMT_S8:
227             intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
228             l_bytes = 2 * sizeof(s8) * p_aout->l_units;
229             aout_thread = (void *)aout_S8StereoThread;
230             break;
231
232         case AOUT_FMT_U16_LE:
233         case AOUT_FMT_U16_BE:
234             intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
235             l_bytes = 2 * sizeof(u16) * p_aout->l_units;
236             aout_thread = (void *)aout_U16StereoThread;
237             break;
238
239         case AOUT_FMT_S16_LE:
240         case AOUT_FMT_S16_BE:
241             intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
242             l_bytes = 2 * sizeof(s16) * p_aout->l_units;
243             aout_thread = (void *)aout_S16StereoThread;
244             break;
245
246         default:
247             intf_ErrMsg( "aout error: unknown audio output format %i",
248                          p_aout->i_format );
249             return( -1 );
250         }
251         break;
252
253     default:
254         intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
255                      p_aout->i_channels );
256         return( -1 );
257     }
258
259     /* Allocate the memory needed by the audio output buffers, and set to zero
260      * the s32 buffer's memory */
261     p_aout->buffer = malloc( l_bytes );
262     if ( p_aout->buffer == NULL )
263     {
264         intf_ErrMsg( "aout error: cannot create output buffer" );
265         return( -1 );
266     }
267
268     p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
269                                         sizeof(s32) << ( p_aout->b_stereo ) );
270     if ( p_aout->s32_buffer == NULL )
271     {
272         intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
273         free( p_aout->buffer );
274         return( -1 );
275     }
276
277     /* Rough estimate of the playing date */
278     p_aout->date = mdate();
279
280     /* Launch the thread */
281     if ( vlc_thread_create( &p_aout->thread_id, "audio output",
282                             (vlc_thread_func_t)aout_thread, p_aout ) )
283     {
284         intf_ErrMsg( "aout error: cannot spawn audio output thread" );
285         free( p_aout->buffer );
286         free( p_aout->s32_buffer );
287         return( -1 );
288     }
289
290     intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
291     return( 0 );
292 }
293
294 /*****************************************************************************
295  * aout_DestroyThread
296  *****************************************************************************/
297 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
298 {
299
300     int i_fifo;
301     
302     /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
303
304     /* Ask thread to kill itself and wait until it's done */
305     p_aout->b_die = 1;
306     vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
307
308     /* Free the allocated memory */
309     free( p_aout->buffer );
310     free( p_aout->s32_buffer );
311
312     /* Destroy the condition and mutex locks */
313     for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
314     {
315         vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
316         vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
317     }
318     vlc_mutex_destroy( &p_aout->fifos_lock );
319     
320     /* Free the plugin */
321     p_aout->pf_close( p_aout );
322
323     /* Release the aout module */
324     module_Unneed( p_main->p_bank, p_aout->p_module );
325
326     /* Free structure */
327     free( p_aout );
328 }
329