]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
* ./modules/audio_output/esd.c: fixed a stack space leak (Closes: #113).
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: esd.c,v 1.16 2003/01/28 03:46:22 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <fcntl.h>                                       /* open(), O_WRONLY */
29 #include <string.h>                                            /* strerror() */
30 #include <unistd.h>                                      /* write(), close() */
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <esd.h>
38
39 /*****************************************************************************
40  * aout_sys_t: esd audio output method descriptor
41  *****************************************************************************
42  * This structure is part of the audio output thread descriptor.
43  * It describes some esd specific variables.
44  *****************************************************************************/
45 struct aout_sys_t
46 {
47     esd_format_t esd_format;
48     int          i_fd;
49
50     mtime_t      latency;
51 };
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Open         ( vlc_object_t * );
57 static void Close        ( vlc_object_t * );
58 static void Play         ( aout_instance_t * );
59 static int  ESDThread    ( aout_instance_t * );
60 static void ESDLoop      ( aout_instance_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("EsounD audio module") );
67     set_capability( "audio output", 50 );
68     set_callbacks( Open, Close );
69     add_shortcut( "esound" );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Open: open an esd socket
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     aout_instance_t *p_aout = (aout_instance_t *)p_this;
78     struct aout_sys_t * p_sys;
79     int i_nb_channels;
80
81     /* Allocate structure */
82     p_sys = malloc( sizeof( aout_sys_t ) );
83     if( p_sys == NULL )
84     {
85         msg_Err( p_aout, "out of memory" );
86         return -1;
87     }
88
89     p_aout->output.p_sys = p_sys;
90
91     p_aout->output.pf_play = Play;
92     aout_VolumeSoftInit( p_aout );
93
94     /* Initialize some variables */
95     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
96     p_sys->esd_format &= ~ESD_MASK_CHAN;
97
98     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
99     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
100     if ( i_nb_channels > 2 )
101     {
102         /* EsounD doesn't support more than two channels. */
103         i_nb_channels = 2;
104         p_aout->output.output.i_physical_channels =
105             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
106     }
107
108     switch( i_nb_channels )
109     {
110     case 1:
111         p_sys->esd_format |= ESD_MONO;
112         break;
113     case 2:
114         p_sys->esd_format |= ESD_STEREO;
115         break;
116     }
117
118     /* open a socket for playing a stream
119      * and try to open /dev/dsp if there's no EsounD */
120     p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
121                               p_aout->output.output.i_rate, NULL, "vlc" );
122     if( p_sys->i_fd < 0 )
123     {
124         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
125                          p_sys->esd_format, p_aout->output.output.i_rate );
126         free( p_sys );
127         return -1;
128     }
129
130     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
131
132     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
133      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
134     p_sys->latency =
135         (mtime_t)( esd_get_latency( esd_open_sound(NULL) ) + ESD_BUF_SIZE/2
136                     * p_aout->output.output.i_bytes_per_frame
137                     * p_aout->output.output.i_rate
138                     / ESD_DEFAULT_RATE )
139       * (mtime_t)1000000
140       / p_aout->output.output.i_bytes_per_frame
141       / p_aout->output.output.i_rate;
142
143     /* Create ESD thread and wait for its readiness. */
144     if( vlc_thread_create( p_aout, "aout", ESDThread,
145                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
146     {
147         msg_Err( p_aout, "cannot create ESD thread (%s)", strerror(errno) );
148         close( p_sys->i_fd );
149         free( p_sys );
150         return -1;
151     }
152
153     return 0;
154 }
155
156 /*****************************************************************************
157  * Play: nothing to do
158  *****************************************************************************/
159 static void Play( aout_instance_t *p_aout )
160 {
161 }
162
163 /*****************************************************************************
164  * Close: close the Esound socket
165  *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
167 {
168     aout_instance_t *p_aout = (aout_instance_t *)p_this;
169     struct aout_sys_t * p_sys = p_aout->output.p_sys;
170
171     p_aout->b_die = 1;
172     vlc_thread_join( p_aout );
173
174     close( p_sys->i_fd );
175     free( p_sys );
176 }
177
178 /*****************************************************************************
179  * ESDThread: asynchronous thread used to DMA the data to the device
180  *****************************************************************************/
181 static int ESDThread( aout_instance_t * p_aout )
182 {
183     while ( !p_aout->b_die )
184     {
185         ESDLoop( p_aout );
186     }
187
188     return 0;
189 }
190
191 /*****************************************************************************
192  * ESDLoop: ESDThread's inner loop
193  *****************************************************************************
194  * This is a separate function because it makes use of alloca() which makes
195  * use of the caller's stack frame, which means we need to return after each
196  * iteration.
197  *****************************************************************************/
198 static void ESDLoop( aout_instance_t * p_aout )
199 {
200     struct aout_sys_t * p_sys = p_aout->output.p_sys;
201     aout_buffer_t * p_buffer;
202     int i_tmp, i_size;
203     byte_t * p_bytes = NULL;
204
205     /* Get the presentation date of the next write() operation. It
206      * is equal to the current date + buffered samples + esd latency */
207     p_buffer = aout_OutputNextBuffer( p_aout, mdate() + p_sys->latency,
208                                               VLC_FALSE );
209
210     if ( p_buffer != NULL )
211     {
212         p_bytes = p_buffer->p_buffer;
213         i_size = p_buffer->i_nb_bytes;
214     }
215     else
216     {
217         i_size = ESD_BUF_SIZE * 2
218                   / p_aout->output.output.i_frame_length
219                   * p_aout->output.output.i_bytes_per_frame;
220         p_bytes = alloca( i_size );
221         memset( p_bytes, 0, i_size );
222     }
223
224     i_tmp = write( p_sys->i_fd, p_bytes, i_size );
225
226     if( i_tmp < 0 )
227     {
228         msg_Err( p_aout, "write failed (%s)", strerror(errno) );
229     }
230
231     if ( p_buffer != NULL )
232     {
233         aout_BufferFree( p_buffer );
234     }
235 }
236
237 #if 0
238 /*****************************************************************************
239  * Play: play a sound samples buffer
240  *****************************************************************************
241  * This function writes a buffer of i_length bytes in the socket
242  *****************************************************************************/
243 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
244 {
245     int i_amount;
246
247     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
248     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
249
250     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
251
252     write( p_aout->output.p_sys->i_fd, buffer, i_size );
253 }
254 #endif
255