1 /*****************************************************************************
2 * aout_esd.c : Esound functions library
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: aout_esd.c,v 1.21 2002/02/24 22:06:50 sam 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 *****************************************************************************/
26 * - use the libesd function to get latency when it's not buggy anymore
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <fcntl.h> /* open(), O_WRONLY */
35 #include <string.h> /* strerror() */
36 #include <unistd.h> /* write(), close() */
37 #include <stdio.h> /* "intf_msg.h" */
38 #include <stdlib.h> /* calloc(), malloc(), free() */
42 #include <videolan/vlc.h>
44 #include "audio_output.h" /* aout_thread_t */
46 /*****************************************************************************
47 * aout_sys_t: esd audio output method descriptor
48 *****************************************************************************
49 * This structure is part of the audio output thread descriptor.
50 * It describes some esd specific variables.
51 *****************************************************************************/
52 typedef struct aout_sys_s
54 esd_format_t esd_format;
59 /*****************************************************************************
61 *****************************************************************************/
62 static int aout_Open ( aout_thread_t *p_aout );
63 static int aout_SetFormat ( aout_thread_t *p_aout );
64 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
65 static void aout_Play ( aout_thread_t *p_aout,
66 byte_t *buffer, int i_size );
67 static void aout_Close ( aout_thread_t *p_aout );
69 /*****************************************************************************
70 * Functions exported as capabilities. They are declared as static so that
71 * we don't pollute the namespace too much.
72 *****************************************************************************/
73 void _M( aout_getfunctions )( function_list_t * p_function_list )
75 p_function_list->functions.aout.pf_open = aout_Open;
76 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
77 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
78 p_function_list->functions.aout.pf_play = aout_Play;
79 p_function_list->functions.aout.pf_close = aout_Close;
82 /*****************************************************************************
83 * aout_Open: open an esd socket
84 *****************************************************************************/
85 static int aout_Open( aout_thread_t *p_aout )
87 /* mpg123 does it this way */
88 int i_bits = ESD_BITS16;
89 int i_mode = ESD_STREAM;
90 int i_func = ESD_PLAY;
92 /* Allocate structure */
93 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
94 if( p_aout->p_sys == NULL )
96 intf_ErrMsg("error: %s", strerror(ENOMEM) );
100 /* Initialize some variables */
101 p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
107 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
109 if( p_aout->i_channels == 1 )
111 p_aout->p_sys->esd_format |= ESD_MONO;
115 p_aout->p_sys->esd_format |= ESD_STEREO;
118 /* open a socket for playing a stream
119 * and try to open /dev/dsp if there's no EsounD */
120 if ( (p_aout->p_sys->i_fd
121 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
122 p_aout->i_rate, NULL, "vlc")) < 0 )
124 intf_ErrMsg( "aout error: can't open esound socket"
125 " (format 0x%08x at %ld Hz)",
126 p_aout->p_sys->esd_format, p_aout->i_rate );
133 /*****************************************************************************
134 * aout_SetFormat: set the output format
135 *****************************************************************************/
136 static int aout_SetFormat( aout_thread_t *p_aout )
140 i_fd = esd_open_sound(NULL);
141 p_aout->i_latency = esd_get_latency(i_fd);
143 intf_WarnMsg(2, "aout_esd_latency: %d",p_aout->i_latency);
148 /*****************************************************************************
149 * aout_GetBufInfo: buffer status query
150 *****************************************************************************/
151 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
153 /* arbitrary value that should be changed */
154 return( i_buffer_limit );
157 /*****************************************************************************
158 * aout_Play: play a sound samples buffer
159 *****************************************************************************
160 * This function writes a buffer of i_length bytes in the socket
161 *****************************************************************************/
162 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
166 if (p_aout->p_sys->esd_format & ESD_STEREO)
168 if (p_aout->p_sys->esd_format & ESD_BITS16)
170 i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
174 i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
179 if (p_aout->p_sys->esd_format & ESD_BITS16)
181 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
185 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
189 write( p_aout->p_sys->i_fd, buffer, i_size );
192 /*****************************************************************************
193 * aout_Close: close the Esound socket
194 *****************************************************************************/
195 static void aout_Close( aout_thread_t *p_aout )
197 close( p_aout->p_sys->i_fd );