]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
FSF address change.
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 <sys/socket.h>
38
39 #include <sys/time.h>
40 #include <time.h>
41
42 #include <esd.h>
43
44 /*****************************************************************************
45  * aout_sys_t: esd audio output method descriptor
46  *****************************************************************************
47  * This structure is part of the audio output thread descriptor.
48  * It describes some esd specific variables.
49  *****************************************************************************/
50 struct aout_sys_t
51 {
52     esd_format_t esd_format;
53     int          i_fd;
54
55     mtime_t      latency;       /* unused, but we might do something with it */
56 };
57
58 /*****************************************************************************
59  * Local prototypes
60  *****************************************************************************/
61 static int  Open         ( vlc_object_t * );
62 static void Close        ( vlc_object_t * );
63 static void Play         ( aout_instance_t * );
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin();
69     set_description( _("EsounD audio output") );
70     set_shortname( "EsounD" );
71     set_capability( "audio output", 50 );
72     add_string( "esdserver", "", NULL, N_("Esound server"), NULL, VLC_FALSE );
73     set_category( CAT_AUDIO );
74     set_subcategory( SUBCAT_AUDIO_AOUT );
75     set_callbacks( Open, Close );
76     add_shortcut( "esound" );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Open: open an esd socket
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     aout_instance_t *p_aout = (aout_instance_t *)p_this;
85     struct aout_sys_t * p_sys;
86     char * psz_server;
87     int i_nb_channels;
88     int i_newfd;
89
90     /* Allocate structure */
91     p_sys = malloc( sizeof( aout_sys_t ) );
92     if( p_sys == NULL )
93     {
94         msg_Err( p_aout, "out of memory" );
95         return VLC_ENOMEM;
96     }
97
98     p_aout->output.p_sys = p_sys;
99
100     p_aout->output.pf_play = Play;
101     aout_VolumeSoftInit( p_aout );
102
103     /* Initialize some variables */
104     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
105     p_sys->esd_format &= ~ESD_MASK_CHAN;
106
107     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
108     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
109     if ( i_nb_channels > 2 )
110     {
111         /* EsounD doesn't support more than two channels. */
112         i_nb_channels = 2;
113         p_aout->output.output.i_physical_channels =
114             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
115     }
116
117     switch( i_nb_channels )
118     {
119     case 1:
120         p_sys->esd_format |= ESD_MONO;
121         break;
122     case 2:
123         p_sys->esd_format |= ESD_STEREO;
124         break;
125     }
126
127     /* Force the rate, otherwise the sound is very noisy */
128     p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
129     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
130
131     /* Open a socket for playing a stream
132      * and try to open /dev/dsp if there's no EsounD */
133     psz_server = config_GetPsz( p_aout, "esdserver" );
134     if( psz_server && *psz_server )
135     {
136         p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
137                                                 p_aout->output.output.i_rate,
138                                                 psz_server, "vlc" );
139     }
140     else
141     {
142         p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
143                                                 p_aout->output.output.i_rate,
144                                                 NULL, "vlc" );
145     }
146
147     if( p_sys->i_fd < 0 )
148     {
149         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
150                          p_sys->esd_format, p_aout->output.output.i_rate );
151         free( p_sys );
152         return VLC_EGENERIC;
153     }
154
155     if( psz_server && *psz_server )
156     {
157         struct timeval start, stop;
158         esd_server_info_t * p_info;
159
160         gettimeofday( &start, NULL );
161         p_info = esd_get_server_info( p_sys->i_fd );
162         gettimeofday( &stop, NULL );
163
164         p_sys->latency = (mtime_t)( stop.tv_sec - start.tv_sec )
165                            * (mtime_t)1000000;
166         p_sys->latency += stop.tv_usec - start.tv_usec;
167     }
168     else
169     {
170         p_sys->latency = 0;
171     }
172
173     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
174      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
175     p_sys->latency +=
176         (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) )
177                     + ESD_BUF_SIZE / 2
178                       * p_aout->output.output.i_bytes_per_frame
179                       * p_aout->output.output.i_rate
180                       / ESD_DEFAULT_RATE )
181       * (mtime_t)1000000
182       / p_aout->output.output.i_bytes_per_frame
183       / p_aout->output.output.i_rate;
184
185     close( i_newfd );
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * Play: nothing to do
191  *****************************************************************************/
192 static void Play( aout_instance_t *p_aout )
193 {
194     struct aout_sys_t * p_sys = p_aout->output.p_sys;
195     aout_buffer_t * p_buffer;
196     int i_tmp;
197
198     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
199
200     if ( p_buffer != NULL )
201     {
202         unsigned int pos;
203         unsigned char *data = p_buffer->p_buffer;
204
205         for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
206              pos += ESD_BUF_SIZE )
207         {
208             i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
209             if( i_tmp < 0 )
210             {
211                 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
212             }
213         }
214         aout_BufferFree( p_buffer );
215     }
216 }
217
218 /*****************************************************************************
219  * Close: close the Esound socket
220  *****************************************************************************/
221 static void Close( vlc_object_t *p_this )
222 {
223     aout_instance_t *p_aout = (aout_instance_t *)p_this;
224     struct aout_sys_t * p_sys = p_aout->output.p_sys;
225
226     close( p_sys->i_fd );
227     free( p_sys );
228 }
229
230 #if 0
231 /*****************************************************************************
232  * Play: play a sound samples buffer
233  *****************************************************************************
234  * This function writes a buffer of i_length bytes in the socket
235  *****************************************************************************/
236 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
237 {
238     int i_amount;
239
240     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
241     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
242
243     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
244
245     write( p_aout->output.p_sys->i_fd, buffer, i_size );
246 }
247 #endif
248