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