1 /*****************************************************************************
2 * audio_output.c : audio output thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: audio_output.c,v 1.89 2002/08/04 20:04:11 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 */
60 /* Allocate descriptor */
61 p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
67 p_aout->i_latency = 0;
68 p_aout->i_rate = config_GetInt( p_aout, "rate" );
69 p_aout->i_channels = config_GetInt( p_aout, "mono" ) ? 1 : 2;
71 i_format = config_GetInt( p_aout, "audio-format" );
72 if( ( !i_format ) || ( i_format > 8 ) )
74 p_aout->i_format = AOUT_FMT_S16_NE;
78 p_aout->i_format = 1 << ( i_format + 2 );
81 if( p_aout->i_rate == 0 )
83 msg_Err( p_aout, "null sample rate" );
84 vlc_object_destroy( p_aout );
88 /* Choose the best module */
89 p_aout->p_module = module_Need( p_aout, "audio output", "$aout" );
90 if( p_aout->p_module == NULL )
92 msg_Err( p_aout, "no suitable aout module" );
93 vlc_object_destroy( p_aout );
98 * Initialize audio device
100 if ( p_aout->pf_setformat( p_aout ) )
102 module_Unneed( p_aout, p_aout->p_module );
103 vlc_object_destroy( p_aout );
107 /* Initialize the volume level */
108 p_aout->i_volume = config_GetInt( p_aout, "volume" );
109 p_aout->i_savedvolume = 0;
111 /* FIXME: maybe it would be cleaner to change SpawnThread prototype
112 * see vout to handle status correctly ?? however, it is not critical since
113 * this thread is only called in main and all calls are blocking */
114 if( aout_SpawnThread( p_aout ) )
116 module_Unneed( p_aout, p_aout->p_module );
117 vlc_object_destroy( p_aout );
121 vlc_object_attach( p_aout, p_parent->p_vlc );
126 /*****************************************************************************
128 *****************************************************************************/
129 static int aout_SpawnThread( aout_thread_t * p_aout )
131 int i_index, i_bytes;
132 void (* pf_aout_thread)( aout_thread_t * ) = NULL;
135 /* Initialize the fifos lock */
136 vlc_mutex_init( p_aout, &p_aout->fifos_lock );
138 /* Initialize audio fifos : set all fifos as empty and initialize locks */
139 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
141 p_aout->fifo[i_index].i_format = AOUT_FIFO_NONE;
142 vlc_mutex_init( p_aout, &p_aout->fifo[i_index].data_lock );
143 vlc_cond_init( p_aout, &p_aout->fifo[i_index].data_wait );
146 /* Compute the size (in audio units) of the audio output buffer. Although
147 * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
148 * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
149 p_aout->i_units = (s64)p_aout->i_rate * AOUT_BUFFER_DURATION / 1000000;
151 /* Make pf_aout_thread point to the right thread function, and compute the
152 * byte size of the audio output buffer */
153 switch ( p_aout->i_format )
156 pf_aout_thread = aout_PCMThread;
157 psz_format = "unsigned 8 bits";
158 i_bytes = p_aout->i_units * p_aout->i_channels;
162 pf_aout_thread = aout_PCMThread;
163 psz_format = "signed 8 bits";
164 i_bytes = p_aout->i_units * p_aout->i_channels;
167 case AOUT_FMT_U16_LE:
168 case AOUT_FMT_U16_BE:
169 pf_aout_thread = aout_PCMThread;
170 psz_format = "unsigned 16 bits";
171 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
174 case AOUT_FMT_S16_LE:
175 case AOUT_FMT_S16_BE:
176 pf_aout_thread = aout_PCMThread;
177 psz_format = "signed 16 bits";
178 i_bytes = 2 * p_aout->i_units * p_aout->i_channels;
182 pf_aout_thread = aout_SpdifThread;
183 psz_format = "A52 pass-through";
184 i_bytes = SPDIF_FRAME_SIZE;
188 msg_Err( p_aout, "unknown audio output format %i",
193 /* Allocate the memory needed by the audio output buffers, and set to zero
194 * the s32 buffer's memory */
195 p_aout->buffer = malloc( i_bytes );
196 if ( p_aout->buffer == NULL )
198 msg_Err( p_aout, "out of memory" );
202 p_aout->s32_buffer = (s32 *)calloc( p_aout->i_units,
203 sizeof(s32) * p_aout->i_channels );
204 if ( p_aout->s32_buffer == NULL )
206 msg_Err( p_aout, "out of memory" );
207 free( p_aout->buffer );
211 /* Rough estimate of the playing date */
212 p_aout->date = mdate() + p_aout->p_vlc->i_desync;
214 /* Launch the thread */
215 if ( vlc_thread_create( p_aout, "audio output", pf_aout_thread, 0 ) )
217 msg_Err( p_aout, "cannot spawn audio output thread" );
218 free( p_aout->buffer );
219 free( p_aout->s32_buffer );
223 msg_Dbg( p_aout, "%s thread spawned, %i channels, rate %i",
224 psz_format, p_aout->i_channels, p_aout->i_rate );
228 /*****************************************************************************
230 *****************************************************************************/
231 void aout_DestroyThread( aout_thread_t * p_aout )
235 /* Ask thread to kill itself and wait until it's done */
238 vlc_thread_join( p_aout );
240 /* Free the allocated memory */
241 free( p_aout->buffer );
242 free( p_aout->s32_buffer );
244 /* Destroy the condition and mutex locks */
245 for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
247 vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
248 vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
250 vlc_mutex_destroy( &p_aout->fifos_lock );
252 /* Release the aout module */
253 module_Unneed( p_aout, p_aout->p_module );
256 vlc_object_destroy( p_aout );