1 /*****************************************************************************
2 * esd.c : EsounD module
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <unistd.h> /* write(), close() */
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
38 #include <sys/socket.h>
45 /*****************************************************************************
46 * aout_sys_t: esd audio output method descriptor
47 *****************************************************************************
48 * This structure is part of the audio output thread descriptor.
49 * It describes some esd specific variables.
50 *****************************************************************************/
53 esd_format_t esd_format;
56 mtime_t latency; /* unused, but we might do something with it */
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
64 static void Play ( aout_instance_t * );
66 /*****************************************************************************
68 *****************************************************************************/
70 set_description( N_("EsounD audio output") );
71 set_shortname( "EsounD" );
72 set_capability( "audio output", 50 );
73 add_string( "esdserver", "", NULL, N_("Esound server"), NULL, false );
74 set_category( CAT_AUDIO );
75 set_subcategory( SUBCAT_AUDIO_AOUT );
76 set_callbacks( Open, Close );
77 add_shortcut( "esound" );
80 /*****************************************************************************
81 * Open: open an esd socket
82 *****************************************************************************/
83 static int Open( vlc_object_t *p_this )
85 aout_instance_t *p_aout = (aout_instance_t *)p_this;
86 struct aout_sys_t * p_sys;
91 /* Allocate structure */
92 p_sys = malloc( sizeof( aout_sys_t ) );
96 p_aout->output.p_sys = p_sys;
98 p_aout->output.pf_play = Play;
99 aout_VolumeSoftInit( p_aout );
101 /* Initialize some variables */
102 p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
103 p_sys->esd_format &= ~ESD_MASK_CHAN;
105 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
106 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
107 if ( i_nb_channels > 2 )
109 /* EsounD doesn't support more than two channels. */
111 p_aout->output.output.i_physical_channels =
112 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
115 switch( i_nb_channels )
118 p_sys->esd_format |= ESD_MONO;
121 p_sys->esd_format |= ESD_STEREO;
125 /* Force the rate, otherwise the sound is very noisy */
126 p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
127 p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
129 /* Open a socket for playing a stream
130 * and try to open /dev/dsp if there's no EsounD */
131 psz_server = config_GetPsz( p_aout, "esdserver" );
132 if( psz_server && *psz_server )
134 p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
135 p_aout->output.output.i_rate,
140 p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
141 p_aout->output.output.i_rate,
145 if( p_sys->i_fd < 0 )
147 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
148 p_sys->esd_format, p_aout->output.output.i_rate );
154 if( psz_server && *psz_server )
156 struct timeval start, stop;
157 esd_server_info_t * p_info;
159 gettimeofday( &start, NULL );
160 p_info = esd_get_server_info( p_sys->i_fd );
161 gettimeofday( &stop, NULL );
163 p_sys->latency = (mtime_t)( stop.tv_sec - start.tv_sec )
165 p_sys->latency += stop.tv_usec - start.tv_usec;
172 /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
173 * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
175 (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) )
177 * p_aout->output.output.i_bytes_per_frame
178 * p_aout->output.output.i_rate
181 / p_aout->output.output.i_bytes_per_frame
182 / p_aout->output.output.i_rate;
189 /*****************************************************************************
190 * Play: nothing to do
191 *****************************************************************************/
192 static void Play( aout_instance_t *p_aout )
194 struct aout_sys_t * p_sys = p_aout->output.p_sys;
195 aout_buffer_t * p_buffer;
198 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
200 if ( p_buffer != NULL )
203 unsigned char *data = p_buffer->p_buffer;
205 for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
206 pos += ESD_BUF_SIZE )
208 i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
211 msg_Err( p_aout, "write failed (%m)" );
214 aout_BufferFree( p_buffer );
218 /*****************************************************************************
219 * Close: close the Esound socket
220 *****************************************************************************/
221 static void Close( vlc_object_t *p_this )
223 aout_instance_t *p_aout = (aout_instance_t *)p_this;
224 struct aout_sys_t * p_sys = p_aout->output.p_sys;
226 close( p_sys->i_fd );
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, uint8_t *buffer, int i_size )
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;
243 i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
245 write( p_aout->output.p_sys->i_fd, buffer, i_size );