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 *****************************************************************************/
25 * - use the libesd function to get latency when it's not buggy anymore
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <fcntl.h> /* open(), O_WRONLY */
36 #include <string.h> /* strerror() */
37 #include <unistd.h> /* write(), close() */
38 #include <stdio.h> /* "intf_msg.h" */
39 #include <stdlib.h> /* calloc(), malloc(), free() */
44 #include "common.h" /* boolean_t, byte_t */
49 #include "audio_output.h" /* aout_thread_t */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
56 /*****************************************************************************
57 * aout_sys_t: esd audio output method descriptor
58 *****************************************************************************
59 * This structure is part of the audio output thread descriptor.
60 * It describes some esd specific variables.
61 *****************************************************************************/
62 typedef struct aout_sys_s
64 esd_format_t esd_format;
68 /*****************************************************************************
70 *****************************************************************************/
71 static int aout_Probe ( probedata_t *p_data );
72 static int aout_Open ( aout_thread_t *p_aout );
73 static int aout_SetFormat ( aout_thread_t *p_aout );
74 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
75 static void aout_Play ( aout_thread_t *p_aout,
76 byte_t *buffer, int i_size );
77 static void aout_Close ( aout_thread_t *p_aout );
79 /*****************************************************************************
80 * Functions exported as capabilities. They are declared as static so that
81 * we don't pollute the namespace too much.
82 *****************************************************************************/
83 void aout_getfunctions( function_list_t * p_function_list )
85 p_function_list->pf_probe = aout_Probe;
86 p_function_list->functions.aout.pf_open = aout_Open;
87 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
88 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
89 p_function_list->functions.aout.pf_play = aout_Play;
90 p_function_list->functions.aout.pf_close = aout_Close;
93 /*****************************************************************************
94 * aout_Probe: probes the audio device and return a score
95 *****************************************************************************
96 * This function tries to open the dps and returns a score to the plugin
97 * manager so that it can
98 *****************************************************************************/
99 static int aout_Probe( probedata_t *p_data )
101 if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
106 /* We don't have to test anything -- if we managed to open this plugin,
107 * it means we have the appropriate libs. */
111 /*****************************************************************************
112 * aout_Open: open an esd socket
113 *****************************************************************************/
114 static int aout_Open( aout_thread_t *p_aout )
116 /* mpg123 does it this way */
117 int i_bits = ESD_BITS16;
118 int i_mode = ESD_STREAM;
119 int i_func = ESD_PLAY;
121 /* Allocate structure */
122 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
123 if( p_aout->p_sys == NULL )
125 intf_ErrMsg("error: %s", strerror(ENOMEM) );
129 /* Initialize some variables */
130 p_aout->i_format = AOUT_FORMAT_DEFAULT;
131 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
132 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
137 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
139 if( p_aout->i_channels == 1 )
140 p_aout->p_sys->esd_format |= ESD_MONO;
142 p_aout->p_sys->esd_format |= ESD_STEREO;
144 /* open a socket for playing a stream
145 * and try to open /dev/dsp if there's no EsounD */
147 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
148 p_aout->l_rate, NULL, "vlc")) < 0 )
150 intf_ErrMsg( "aout error: can't open esound socket"
151 " (format 0x%08x at %ld Hz)",
152 p_aout->p_sys->esd_format, p_aout->l_rate );
159 /*****************************************************************************
160 * aout_SetFormat: set the output format
161 *****************************************************************************/
162 static int aout_SetFormat( aout_thread_t *p_aout )
167 /*****************************************************************************
168 * aout_GetBufInfo: buffer status query
169 *****************************************************************************/
170 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
172 /* arbitrary value that should be changed */
173 return( l_buffer_limit );
176 /*****************************************************************************
177 * aout_Play: play a sound samples buffer
178 *****************************************************************************
179 * This function writes a buffer of i_length bytes in the socket
180 *****************************************************************************/
181 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
185 if (p_aout->p_sys->esd_format & ESD_STEREO)
187 if (p_aout->p_sys->esd_format & ESD_BITS16)
188 amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
190 amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
194 if (p_aout->p_sys->esd_format & ESD_BITS16)
195 amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
197 amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
200 intf_DbgMsg( "aout: latency is %i", amount );
202 write( p_aout->i_fd, buffer, i_size );
205 /*****************************************************************************
206 * aout_Close: close the Esound socket
207 *****************************************************************************/
208 static void aout_Close( aout_thread_t *p_aout )
210 close( p_aout->i_fd );