]> git.sesse.net Git - vlc/blob - src/audio_output/audio_output.c
f7ddbabd5e8eec930c533e82cce4bd1978f5a75b
[vlc] / src / audio_output / audio_output.c
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 $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Cyril Deguet <asmax@via.ecp.fr>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                            /* calloc(), malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32
33 #ifdef HAVE_UNISTD_H
34 #   include <unistd.h>                                           /* getpid() */
35 #endif
36
37 #ifdef WIN32                   /* getpid() for win32 is located in process.h */
38 #   include <process.h>
39 #endif
40
41 #include "audio_output.h"
42
43 #include "aout_pcm.h"
44 #include "aout_spdif.h"
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  aout_SpawnThread ( aout_thread_t * p_aout );
50
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 )
56 {
57     aout_thread_t * p_aout;                             /* thread descriptor */
58     char *          psz_name;
59     int             i_format;
60
61     /* Allocate descriptor */
62     p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
63     if( p_aout == NULL )
64     {
65         return NULL;
66     }
67
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;
71
72     i_format = config_GetInt( p_aout, "audio-format" );
73     if( ( !i_format ) || ( i_format > 8 ) )
74     {
75         p_aout->i_format = AOUT_FMT_S16_NE;
76     }
77     else
78     {
79         p_aout->i_format = 1 << ( i_format + 2 );
80     }
81
82     if( p_aout->i_rate == 0 )
83     {
84         msg_Err( p_aout, "null sample rate" );
85         vlc_object_destroy( p_aout );
86         return NULL;
87     }
88
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 )
94     {
95         msg_Err( p_aout, "no suitable aout module" );
96         vlc_object_destroy( p_aout );
97         return NULL;
98     }
99
100     /*
101      * Initialize audio device
102      */
103     if ( p_aout->pf_setformat( p_aout ) )
104     {
105         module_Unneed( p_aout, p_aout->p_module );
106         vlc_object_destroy( p_aout );
107         return NULL;
108     }
109
110     /* Initialize the volume level */
111     p_aout->i_volume = config_GetInt( p_aout, "volume" );
112     p_aout->i_savedvolume = 0;
113     
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 ) )
118     {
119         module_Unneed( p_aout, p_aout->p_module );
120         vlc_object_destroy( p_aout );
121         return NULL;
122     }
123
124     vlc_object_attach( p_aout, p_parent->p_vlc );
125
126     return p_aout;
127 }
128
129 /*****************************************************************************
130  * aout_SpawnThread
131  *****************************************************************************/
132 static int aout_SpawnThread( aout_thread_t * p_aout )
133 {
134     int     i_index, i_bytes;
135     void (* pf_aout_thread)( aout_thread_t * ) = NULL;
136     char   *psz_format;
137
138     /* Initialize the fifos lock */
139     vlc_mutex_init( p_aout, &p_aout->fifos_lock );
140
141     /* Initialize audio fifos : set all fifos as empty and initialize locks */
142     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
143     {
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 );
147     }
148
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;
153
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 )
157     {
158         case AOUT_FMT_U8:
159             pf_aout_thread = aout_PCMThread;
160             psz_format = "unsigned 8 bits";
161             i_bytes = p_aout->i_units * p_aout->i_channels;
162             break;
163
164         case AOUT_FMT_S8:
165             pf_aout_thread = aout_PCMThread;
166             psz_format = "signed 8 bits";
167             i_bytes = p_aout->i_units * p_aout->i_channels;
168             break;
169
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;
175             break;
176
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;
182             break;
183
184         case AOUT_FMT_AC3:
185             pf_aout_thread = aout_SpdifThread;
186             psz_format = "ac3 pass-through";
187             i_bytes = SPDIF_FRAME_SIZE;
188             break;
189
190         default:
191             msg_Err( p_aout, "unknown audio output format %i",
192                              p_aout->i_format );
193             return( -1 );
194     }
195
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 )
200     {
201         msg_Err( p_aout, "out of memory" );
202         return( -1 );
203     }
204
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 )
208     {
209         msg_Err( p_aout, "out of memory" );
210         free( p_aout->buffer );
211         return( -1 );
212     }
213
214     /* Rough estimate of the playing date */
215     p_aout->date = mdate() + p_aout->p_vlc->i_desync;
216
217     /* Launch the thread */
218     if ( vlc_thread_create( p_aout, "audio output", pf_aout_thread, 0 ) )
219     {
220         msg_Err( p_aout, "cannot spawn audio output thread" );
221         free( p_aout->buffer );
222         free( p_aout->s32_buffer );
223         return( -1 );
224     }
225
226     msg_Dbg( p_aout, "%s thread spawned, %i channels, rate %i",
227                      psz_format, p_aout->i_channels, p_aout->i_rate );
228     return( 0 );
229 }
230
231 /*****************************************************************************
232  * aout_DestroyThread
233  *****************************************************************************/
234 void aout_DestroyThread( aout_thread_t * p_aout )
235 {
236     int i_index;
237     
238     /* Ask thread to kill itself and wait until it's done */
239     p_aout->b_die = 1;
240
241     vlc_thread_join( p_aout );
242
243     /* Free the allocated memory */
244     free( p_aout->buffer );
245     free( p_aout->s32_buffer );
246
247     /* Destroy the condition and mutex locks */
248     for ( i_index = 0; i_index < AOUT_MAX_FIFOS; i_index++ )
249     {
250         vlc_mutex_destroy( &p_aout->fifo[i_index].data_lock );
251         vlc_cond_destroy( &p_aout->fifo[i_index].data_wait );
252     }
253     vlc_mutex_destroy( &p_aout->fifos_lock );
254     
255     /* Release the aout module */
256     module_Unneed( p_aout, p_aout->p_module );
257
258     /* Free structure */
259     vlc_object_destroy( p_aout );
260 }
261