1 /*****************************************************************************
2 * aout_esd.c : Esound functions library
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: aout_esd.c,v 1.14 2001/07/12 20:31:33 reno Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #define MODULE_NAME esd
25 #include "modules_inner.h"
29 * - use the libesd function to get latency when it's not buggy anymore
33 /*****************************************************************************
35 *****************************************************************************/
38 #include <errno.h> /* ENOMEM */
39 #include <fcntl.h> /* open(), O_WRONLY */
40 #include <string.h> /* strerror() */
41 #include <unistd.h> /* write(), close() */
42 #include <stdio.h> /* "intf_msg.h" */
43 #include <stdlib.h> /* calloc(), malloc(), free() */
48 #include "common.h" /* boolean_t, byte_t */
53 #include "audio_output.h" /* aout_thread_t */
55 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
59 #include "modules_export.h"
61 /*****************************************************************************
62 * aout_sys_t: esd audio output method descriptor
63 *****************************************************************************
64 * This structure is part of the audio output thread descriptor.
65 * It describes some esd specific variables.
66 *****************************************************************************/
67 typedef struct aout_sys_s
69 esd_format_t esd_format;
73 /*****************************************************************************
75 *****************************************************************************/
76 static int aout_Probe ( probedata_t *p_data );
77 static int aout_Open ( aout_thread_t *p_aout );
78 static int aout_SetFormat ( aout_thread_t *p_aout );
79 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
80 static void aout_Play ( aout_thread_t *p_aout,
81 byte_t *buffer, int i_size );
82 static void aout_Close ( aout_thread_t *p_aout );
84 /*****************************************************************************
85 * Functions exported as capabilities. They are declared as static so that
86 * we don't pollute the namespace too much.
87 *****************************************************************************/
88 void _M( aout_getfunctions )( function_list_t * p_function_list )
90 p_function_list->pf_probe = aout_Probe;
91 p_function_list->functions.aout.pf_open = aout_Open;
92 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
93 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
94 p_function_list->functions.aout.pf_play = aout_Play;
95 p_function_list->functions.aout.pf_close = aout_Close;
98 /*****************************************************************************
99 * aout_Probe: probes the audio device and return a score
100 *****************************************************************************
101 * This function tries to open the dps and returns a score to the plugin
102 * manager so that it can
103 *****************************************************************************/
104 static int aout_Probe( probedata_t *p_data )
106 if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
111 /* We don't have to test anything -- if we managed to open this plugin,
112 * it means we have the appropriate libs. */
116 /*****************************************************************************
117 * aout_Open: open an esd socket
118 *****************************************************************************/
119 static int aout_Open( aout_thread_t *p_aout )
121 /* mpg123 does it this way */
122 int i_bits = ESD_BITS16;
123 int i_mode = ESD_STREAM;
124 int i_func = ESD_PLAY;
126 /* Allocate structure */
127 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
128 if( p_aout->p_sys == NULL )
130 intf_ErrMsg("error: %s", strerror(ENOMEM) );
134 /* Initialize some variables */
135 p_aout->i_format = AOUT_FORMAT_DEFAULT;
136 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
137 p_aout->l_rate = esd_audio_rate; /* We use actual esd rate value, not AOUT_RATE_DEFAULT */
142 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
144 if( p_aout->i_channels == 1 )
146 p_aout->p_sys->esd_format |= ESD_MONO;
150 p_aout->p_sys->esd_format |= ESD_STEREO;
153 /* open a socket for playing a stream
154 * and try to open /dev/dsp if there's no EsounD */
156 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
157 p_aout->l_rate, NULL, "vlc")) < 0 )
159 intf_ErrMsg( "aout error: can't open esound socket"
160 " (format 0x%08x at %ld Hz)",
161 p_aout->p_sys->esd_format, p_aout->l_rate );
168 /*****************************************************************************
169 * aout_SetFormat: set the output format
170 *****************************************************************************/
171 static int aout_SetFormat( aout_thread_t *p_aout )
175 i_fd = esd_open_sound(NULL);
176 p_aout->i_latency = esd_get_latency(i_fd);
178 intf_WarnMsg(2, "aout_esd_latency: %d",p_aout->i_latency);
183 /*****************************************************************************
184 * aout_GetBufInfo: buffer status query
185 *****************************************************************************/
186 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
188 /* arbitrary value that should be changed */
189 return( l_buffer_limit );
192 /*****************************************************************************
193 * aout_Play: play a sound samples buffer
194 *****************************************************************************
195 * This function writes a buffer of i_length bytes in the socket
196 *****************************************************************************/
197 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
201 if (p_aout->p_sys->esd_format & ESD_STEREO)
203 if (p_aout->p_sys->esd_format & ESD_BITS16)
205 i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
209 i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
214 if (p_aout->p_sys->esd_format & ESD_BITS16)
216 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
220 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
224 write( p_aout->i_fd, buffer, i_size );
227 /*****************************************************************************
228 * aout_Close: close the Esound socket
229 *****************************************************************************/
230 static void aout_Close( aout_thread_t *p_aout )
232 close( p_aout->i_fd );