1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: audio_output.c,v 1.62 2001/05/30 05:19:03 stef Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
28 * - Faire les optimisations dans les fonctions threads :
29 * = Stocker les "petits calculs" dans des variables au lieu de les refaire
31 * = Utiliser des tables pour les gros calculs
32 * - Faire une structure différente pour intf/adec fifo
36 /*****************************************************************************
38 *****************************************************************************/
41 #include <unistd.h> /* getpid() */
42 #ifdef WIN32 /* getpid() for win32 is located in process.h */
46 #include <stdio.h> /* "intf_msg.h" */
47 #include <stdlib.h> /* calloc(), malloc(), free() */
52 #include "mtime.h" /* mtime_t, mdate(), msleep() */
55 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
57 #include "audio_output.h"
58 #include "aout_common.h"
62 /*****************************************************************************
64 *****************************************************************************/
65 static int aout_SpawnThread( aout_thread_t * p_aout );
67 /*****************************************************************************
68 * aout_InitBank: initialize the audio output bank.
69 *****************************************************************************/
70 void aout_InitBank ( void )
72 p_aout_bank->i_count = 0;
74 vlc_mutex_init( &p_aout_bank->lock );
77 /*****************************************************************************
78 * aout_EndBank: empty the audio output bank.
79 *****************************************************************************
80 * This function ends all unused audio outputs and empties the bank in
82 *****************************************************************************/
83 void aout_EndBank ( void )
85 /* Ask all remaining audio outputs to die */
86 while( p_aout_bank->i_count )
89 p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
92 vlc_mutex_destroy( &p_aout_bank->lock );
95 /*****************************************************************************
96 * aout_CreateThread: initialize audio thread
97 *****************************************************************************/
98 aout_thread_t *aout_CreateThread( int *pi_status )
100 aout_thread_t * p_aout; /* thread descriptor */
102 int i_status; /* thread status */
105 /* Allocate descriptor */
106 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
112 /* Choose the best module */
113 p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT, NULL );
115 if( p_aout->p_module == NULL )
117 intf_ErrMsg( "aout error: no suitable aout module" );
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
131 * Initialize audio device
133 if ( p_aout->pf_open( p_aout ) )
135 module_Unneed( p_aout->p_module );
140 if( p_aout->l_rate == 0 )
142 intf_ErrMsg( "aout error: null sample rate" );
143 p_aout->pf_close( p_aout );
144 module_Unneed( p_aout->p_module );
149 /* special setting for ac3 pass-through mode */
150 if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) )
152 p_aout->i_format = AOUT_FMT_AC3;
153 p_aout->l_rate = 48000;
156 /* FIXME: only works for i_channels == 1 or 2 ?? */
157 p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
159 if ( p_aout->pf_setformat( p_aout ) )
161 p_aout->pf_close( p_aout );
162 module_Unneed( p_aout->p_module );
167 /* Initialize the volume level */
168 p_aout->i_volume = VOLUME_DEFAULT;
169 p_aout->i_savedvolume = 0;
171 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
172 * see vout to handle status correctly ?? however, it is not critical since
173 * this thread is only called in main and all calls are blocking */
174 if( aout_SpawnThread( p_aout ) )
176 p_aout->pf_close( p_aout );
177 module_Unneed( p_aout->p_module );
185 /*****************************************************************************
187 *****************************************************************************/
188 static int aout_SpawnThread( aout_thread_t * p_aout )
192 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
194 /* We want the audio output thread to live */
196 p_aout->b_active = 1;
198 /* Initialize the fifos lock */
199 vlc_mutex_init( &p_aout->fifos_lock );
200 /* Initialize audio fifos : set all fifos as empty and initialize locks */
201 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
203 p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
204 vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
205 vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
208 /* Compute the size (in audio units) of the audio output buffer. Although
209 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
210 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
211 p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
212 p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
214 /* Make pf_aout_thread point to the right thread function, and compute the
215 * byte size of the audio output buffer */
216 switch ( p_aout->i_channels )
218 /* Audio output is mono */
220 switch ( p_aout->i_format )
223 intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
224 l_bytes = 1 * sizeof(u8) * p_aout->l_units;
225 pf_aout_thread = aout_U8MonoThread;
229 intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
230 l_bytes = 1 * sizeof(s8) * p_aout->l_units;
231 pf_aout_thread = aout_S8MonoThread;
234 case AOUT_FMT_U16_LE:
235 case AOUT_FMT_U16_BE:
236 intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
237 l_bytes = 1 * sizeof(u16) * p_aout->l_units;
238 pf_aout_thread = aout_U16MonoThread;
241 case AOUT_FMT_S16_LE:
242 case AOUT_FMT_S16_BE:
243 intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
244 l_bytes = 1 * sizeof(s16) * p_aout->l_units;
245 pf_aout_thread = aout_S16MonoThread;
249 intf_ErrMsg( "aout error: unknown audio output format (%i)",
255 /* Audio output is stereo */
257 switch ( p_aout->i_format )
260 intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
261 l_bytes = 2 * sizeof(u8) * p_aout->l_units;
262 pf_aout_thread = aout_U8StereoThread;
266 intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
267 l_bytes = 2 * sizeof(s8) * p_aout->l_units;
268 pf_aout_thread = aout_S8StereoThread;
271 case AOUT_FMT_U16_LE:
272 case AOUT_FMT_U16_BE:
273 intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
274 l_bytes = 2 * sizeof(u16) * p_aout->l_units;
275 pf_aout_thread = aout_U16StereoThread;
278 case AOUT_FMT_S16_LE:
279 case AOUT_FMT_S16_BE:
280 intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
281 l_bytes = 2 * sizeof(s16) * p_aout->l_units;
282 pf_aout_thread = aout_S16StereoThread;
286 intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
287 l_bytes = SPDIF_FRAME_SIZE;
288 pf_aout_thread = aout_SpdifThread;
292 intf_ErrMsg( "aout error: unknown audio output format %i",
299 intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
300 p_aout->i_channels );
304 /* Allocate the memory needed by the audio output buffers, and set to zero
305 * the s32 buffer's memory */
306 p_aout->buffer = malloc( l_bytes );
307 if ( p_aout->buffer == NULL )
309 intf_ErrMsg( "aout error: cannot create output buffer" );
313 p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
314 sizeof(s32) << ( p_aout->b_stereo ) );
315 if ( p_aout->s32_buffer == NULL )
317 intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
318 free( p_aout->buffer );
322 /* Rough estimate of the playing date */
323 p_aout->date = mdate();
325 /* Launch the thread */
326 if ( vlc_thread_create( &p_aout->thread_id, "audio output",
327 (vlc_thread_func_t)pf_aout_thread, p_aout ) )
329 intf_ErrMsg( "aout error: cannot spawn audio output thread" );
330 free( p_aout->buffer );
331 free( p_aout->s32_buffer );
335 intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
339 /*****************************************************************************
341 *****************************************************************************/
342 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
346 /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
348 /* Ask thread to kill itself and wait until it's done */
350 vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
352 /* Free the allocated memory */
353 free( p_aout->buffer );
354 free( p_aout->s32_buffer );
356 /* Destroy the condition and mutex locks */
357 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
359 vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
360 vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
362 vlc_mutex_destroy( &p_aout->fifos_lock );
364 /* Free the plugin */
365 p_aout->pf_close( p_aout );
367 /* Release the aout module */
368 module_Unneed( p_aout->p_module );