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