1 /*****************************************************************************
2 * aout_esd.c : Esound functions library
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 #define MODULE_NAME esd
24 #include "modules_inner.h"
28 * - use the libesd function to get latency when it's not buggy anymore
32 /*****************************************************************************
34 *****************************************************************************/
37 #include <errno.h> /* ENOMEM */
38 #include <fcntl.h> /* open(), O_WRONLY */
39 #include <string.h> /* strerror() */
40 #include <unistd.h> /* write(), close() */
41 #include <stdio.h> /* "intf_msg.h" */
42 #include <stdlib.h> /* calloc(), malloc(), free() */
47 #include "common.h" /* boolean_t, byte_t */
52 #include "audio_output.h" /* aout_thread_t */
54 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
59 /*****************************************************************************
60 * aout_sys_t: esd audio output method descriptor
61 *****************************************************************************
62 * This structure is part of the audio output thread descriptor.
63 * It describes some esd specific variables.
64 *****************************************************************************/
65 typedef struct aout_sys_s
67 esd_format_t esd_format;
71 /*****************************************************************************
73 *****************************************************************************/
74 static int aout_Probe ( probedata_t *p_data );
75 static int aout_Open ( aout_thread_t *p_aout );
76 static int aout_SetFormat ( aout_thread_t *p_aout );
77 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
78 static void aout_Play ( aout_thread_t *p_aout,
79 byte_t *buffer, int i_size );
80 static void aout_Close ( aout_thread_t *p_aout );
82 /*****************************************************************************
83 * Functions exported as capabilities. They are declared as static so that
84 * we don't pollute the namespace too much.
85 *****************************************************************************/
86 void _M( aout_getfunctions )( function_list_t * p_function_list )
88 p_function_list->pf_probe = aout_Probe;
89 p_function_list->functions.aout.pf_open = aout_Open;
90 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
91 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
92 p_function_list->functions.aout.pf_play = aout_Play;
93 p_function_list->functions.aout.pf_close = aout_Close;
96 /*****************************************************************************
97 * aout_Probe: probes the audio device and return a score
98 *****************************************************************************
99 * This function tries to open the dps and returns a score to the plugin
100 * manager so that it can
101 *****************************************************************************/
102 static int aout_Probe( probedata_t *p_data )
104 if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
109 /* We don't have to test anything -- if we managed to open this plugin,
110 * it means we have the appropriate libs. */
114 /*****************************************************************************
115 * aout_Open: open an esd socket
116 *****************************************************************************/
117 static int aout_Open( aout_thread_t *p_aout )
119 /* mpg123 does it this way */
120 int i_bits = ESD_BITS16;
121 int i_mode = ESD_STREAM;
122 int i_func = ESD_PLAY;
124 /* Allocate structure */
125 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
126 if( p_aout->p_sys == NULL )
128 intf_ErrMsg("error: %s", strerror(ENOMEM) );
132 /* Initialize some variables */
133 p_aout->i_format = AOUT_FORMAT_DEFAULT;
134 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
135 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
140 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
142 if( p_aout->i_channels == 1 )
143 p_aout->p_sys->esd_format |= ESD_MONO;
145 p_aout->p_sys->esd_format |= ESD_STEREO;
147 /* open a socket for playing a stream
148 * and try to open /dev/dsp if there's no EsounD */
150 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
151 p_aout->l_rate, NULL, "vlc")) < 0 )
153 intf_ErrMsg( "aout error: can't open esound socket"
154 " (format 0x%08x at %ld Hz)",
155 p_aout->p_sys->esd_format, p_aout->l_rate );
162 /*****************************************************************************
163 * aout_SetFormat: set the output format
164 *****************************************************************************/
165 static int aout_SetFormat( aout_thread_t *p_aout )
170 /*****************************************************************************
171 * aout_GetBufInfo: buffer status query
172 *****************************************************************************/
173 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
175 /* arbitrary value that should be changed */
176 return( l_buffer_limit );
179 /*****************************************************************************
180 * aout_Play: play a sound samples buffer
181 *****************************************************************************
182 * This function writes a buffer of i_length bytes in the socket
183 *****************************************************************************/
184 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
188 if (p_aout->p_sys->esd_format & ESD_STEREO)
190 if (p_aout->p_sys->esd_format & ESD_BITS16)
191 amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
193 amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
197 if (p_aout->p_sys->esd_format & ESD_BITS16)
198 amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
200 amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
203 intf_DbgMsg( "aout: latency is %i", amount );
205 write( p_aout->i_fd, buffer, i_size );
208 /*****************************************************************************
209 * aout_Close: close the Esound socket
210 *****************************************************************************/
211 static void aout_Close( aout_thread_t *p_aout )
213 close( p_aout->i_fd );