1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.87 2002/07/31 20:56:52 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Cyril Deguet <asmax@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* calloc(), malloc(), free() */
34 # include <unistd.h> /* getpid() */
37 #ifdef WIN32 /* getpid() for win32 is located in process.h */
41 #include "audio_output.h"
44 #include "aout_spdif.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int aout_SpawnThread ( aout_thread_t * p_aout );
51 /*****************************************************************************
52 * aout_CreateThread: initialize audio thread
53 *****************************************************************************/
54 aout_thread_t *aout_CreateThread( vlc_object_t *p_parent,
55 int i_channels, int i_rate )
57 aout_thread_t * p_aout; /* thread descriptor */
61 /* Allocate descriptor */
62 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
68 p_aout->i_latency = 0;
69 p_aout->i_rate = config_GetInt( p_aout, "rate" );
70 p_aout->i_channels = config_GetInt( p_aout, "mono" ) ? 1 : 2;
72 i_format = config_GetInt( p_aout, "audio-format" );
73 if( ( !i_format ) || ( i_format > 8 ) )
75 p_aout->i_format = AOUT_FMT_S16_NE;
79 p_aout->i_format = 1 << ( i_format + 2 );
82 if( p_aout->i_rate == 0 )
84 msg_Err( p_aout, "null sample rate" );
85 vlc_object_destroy( p_aout );
89 /* Choose the best module */
90 psz_name = config_GetPsz( p_aout, "aout" );
91 p_aout->p_module = module_Need( p_aout, "audio output", psz_name );
92 if( psz_name ) free( psz_name );
93 if( p_aout->p_module == NULL )
95 msg_Err( p_aout, "no suitable aout module" );
96 vlc_object_destroy( p_aout );
101 * Initialize audio device
103 if ( p_aout->pf_setformat( p_aout ) )
105 module_Unneed( p_aout, p_aout->p_module );
106 vlc_object_destroy( p_aout );
110 /* Initialize the volume level */
111 p_aout->i_volume = config_GetInt( p_aout, "volume" );
112 p_aout->i_savedvolume = 0;
114 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
115 * see vout to handle status correctly ?? however, it is not critical since
116 * this thread is only called in main and all calls are blocking */
117 if( aout_SpawnThread( p_aout ) )
119 module_Unneed( p_aout, p_aout->p_module );
120 vlc_object_destroy( p_aout );
124 vlc_object_attach( p_aout, p_parent->p_vlc );
129 /*****************************************************************************
131 *****************************************************************************/
132 static int aout_SpawnThread( aout_thread_t * p_aout )
134 int i_index, i_bytes;
135 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
138 /* Initialize the fifos lock */
139 vlc_mutex_init( p_aout, &p_aout->fifos_lock );
141 /* Initialize audio fifos : set all fifos as empty and initialize locks */
142 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
144 p_aout->fifo[i_index].i_format = AOUT_FIFO_NONE;
145 vlc_mutex_init( p_aout, &p_aout->fifo[i_index].data_lock );
146 vlc_cond_init( p_aout, &p_aout->fifo[i_index].data_wait );
149 /* Compute the size (in audio units) of the audio output buffer. Although
150 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
151 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
152 p_aout->i_units = (s64)p_aout->i_rate * AOUT_BUFFER_DURATION / 1000000;
154 /* Make pf_aout_thread point to the right thread function, and compute the
155 * byte size of the audio output buffer */
156 switch ( p_aout->i_format )
159 pf_aout_thread = aout_PCMThread;
160 psz_format = "unsigned 8 bits";
161 i_bytes = p_aout->i_units * p_aout->i_channels;
165 pf_aout_thread = aout_PCMThread;
166 psz_format = "signed 8 bits";
167 i_bytes = p_aout->i_units * p_aout->i_channels;
170 case AOUT_FMT_U16_LE:
171 case AOUT_FMT_U16_BE:
172 pf_aout_thread = aout_PCMThread;
173 psz_format = "unsigned 16 bits";
174 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
177 case AOUT_FMT_S16_LE:
178 case AOUT_FMT_S16_BE:
179 pf_aout_thread = aout_PCMThread;
180 psz_format = "signed 16 bits";
181 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
185 pf_aout_thread = aout_SpdifThread;
186 psz_format = "ac3 pass-through";
187 i_bytes = SPDIF_FRAME_SIZE;
191 msg_Err( p_aout, "unknown audio output format %i",
196 /* Allocate the memory needed by the audio output buffers, and set to zero
197 * the s32 buffer's memory */
198 p_aout->buffer = malloc( i_bytes );
199 if ( p_aout->buffer == NULL )
201 msg_Err( p_aout, "out of memory" );
205 p_aout->s32_buffer = (s32 *)calloc( p_aout->i_units,
206 sizeof(s32) * p_aout->i_channels );
207 if ( p_aout->s32_buffer == NULL )
209 msg_Err( p_aout, "out of memory" );
210 free( p_aout->buffer );
214 /* Rough estimate of the playing date */
215 p_aout->date = mdate() + p_aout->p_vlc->i_desync;
217 /* Launch the thread */
218 if ( vlc_thread_create( p_aout, "audio output", pf_aout_thread, 0 ) )
220 msg_Err( p_aout, "cannot spawn audio output thread" );
221 free( p_aout->buffer );
222 free( p_aout->s32_buffer );
226 msg_Dbg( p_aout, "%s thread spawned, %i channels, rate %i",
227 psz_format, p_aout->i_channels, p_aout->i_rate );
231 /*****************************************************************************
233 *****************************************************************************/
234 void aout_DestroyThread( aout_thread_t * p_aout )
238 /* Ask thread to kill itself and wait until it's done */
241 vlc_thread_join( p_aout );
243 /* Free the allocated memory */
244 free( p_aout->buffer );
245 free( p_aout->s32_buffer );
247 /* Destroy the condition and mutex locks */
248 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
250 vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
251 vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
253 vlc_mutex_destroy( &p_aout->fifos_lock );
255 /* Release the aout module */
256 module_Unneed( p_aout, p_aout->p_module );
259 vlc_object_destroy( p_aout );