1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
6 * Authors: Michel Kaempf <maxx@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
27 * - Faire les optimisations dans les fonctions threads :
28 * = Stocker les "petits calculs" dans des variables au lieu de les refaire
30 * = Utiliser des tables pour les gros calculs
31 * - Faire une structure différente pour intf/adec fifo
35 /*****************************************************************************
37 *****************************************************************************/
40 #include <unistd.h> /* getpid() */
42 #include <stdio.h> /* "intf_msg.h" */
43 #include <stdlib.h> /* calloc(), malloc(), free() */
48 #include "mtime.h" /* mtime_t, mdate(), msleep() */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
53 #include "audio_output.h"
54 #include "aout_common.h"
58 /*****************************************************************************
60 *****************************************************************************/
61 static int aout_SpawnThread( aout_thread_t * p_aout );
63 /*****************************************************************************
64 * aout_CreateThread: initialize audio thread
65 *****************************************************************************/
66 aout_thread_t *aout_CreateThread( int *pi_status )
68 aout_thread_t * p_aout; /* thread descriptor */
70 int i_status; /* thread status */
73 /* Allocate descriptor */
74 p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
80 /* Choose the best module */
81 p_aout->p_module = module_Need( p_main->p_bank,
82 MODULE_CAPABILITY_AOUT, NULL );
84 if( p_aout->p_module == NULL )
86 intf_ErrMsg( "aout error: no suitable aout module" );
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;
100 * Initialize audio device
102 if ( p_aout->pf_open( p_aout ) )
104 module_Unneed( p_main->p_bank, p_aout->p_module );
109 if( p_aout->l_rate == 0 )
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 );
118 p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0; /* FIXME: only works
119 for i_channels == 1 or 2 ??*/
121 if ( p_aout->pf_setformat( p_aout ) )
123 p_aout->pf_close( p_aout );
124 module_Unneed( p_main->p_bank, p_aout->p_module );
129 /* Initialize the volume level */
130 p_aout->vol = VOLUME_DEFAULT;
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 ) )
137 p_aout->pf_close( p_aout );
138 module_Unneed( p_main->p_bank, p_aout->p_module );
146 /*****************************************************************************
148 *****************************************************************************/
149 static int aout_SpawnThread( aout_thread_t * p_aout )
153 void * aout_thread = NULL;
155 /* We want the audio output thread to live */
157 p_aout->b_active = 1;
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++ )
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 );
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 );
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 )
179 /* Audio output is mono */
181 switch ( p_aout->i_format )
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;
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;
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;
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;
210 intf_ErrMsg( "aout error: unknown audio output format (%i)",
216 /* Audio output is stereo */
218 switch ( p_aout->i_format )
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;
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;
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;
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;
247 intf_ErrMsg( "aout error: unknown audio output format %i",
254 intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
255 p_aout->i_channels );
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 )
264 intf_ErrMsg( "aout error: cannot create output buffer" );
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 )
272 intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
273 free( p_aout->buffer );
277 /* Rough estimate of the playing date */
278 p_aout->date = mdate();
280 /* Launch the thread */
281 if ( vlc_thread_create( &p_aout->thread_id, "audio output",
282 (vlc_thread_func_t)aout_thread, p_aout ) )
284 intf_ErrMsg( "aout error: cannot spawn audio output thread" );
285 free( p_aout->buffer );
286 free( p_aout->s32_buffer );
290 intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
294 /*****************************************************************************
296 *****************************************************************************/
297 void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
302 /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
304 /* Ask thread to kill itself and wait until it's done */
306 vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
308 /* Free the allocated memory */
309 free( p_aout->buffer );
310 free( p_aout->s32_buffer );
312 /* Destroy the condition and mutex locks */
313 for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
315 vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
316 vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
318 vlc_mutex_destroy( &p_aout->fifos_lock );
320 /* Free the plugin */
321 p_aout->pf_close( p_aout );
323 /* Release the aout module */
324 module_Unneed( p_main->p_bank, p_aout->p_module );