1 /*****************************************************************************
2 * aout_esd.c : Esound functions library
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: aout_esd.c,v 1.12 2001/05/25 04:23:37 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 *****************************************************************************/
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() */
60 /*****************************************************************************
61 * aout_sys_t: esd audio output method descriptor
62 *****************************************************************************
63 * This structure is part of the audio output thread descriptor.
64 * It describes some esd specific variables.
65 *****************************************************************************/
66 typedef struct aout_sys_s
68 esd_format_t esd_format
;
72 /*****************************************************************************
74 *****************************************************************************/
75 static int aout_Probe ( probedata_t
*p_data
);
76 static int aout_Open ( aout_thread_t
*p_aout
);
77 static int aout_SetFormat ( aout_thread_t
*p_aout
);
78 static long aout_GetBufInfo ( aout_thread_t
*p_aout
, long l_buffer_info
);
79 static void aout_Play ( aout_thread_t
*p_aout
,
80 byte_t
*buffer
, int i_size
);
81 static void aout_Close ( aout_thread_t
*p_aout
);
83 /*****************************************************************************
84 * Functions exported as capabilities. They are declared as static so that
85 * we don't pollute the namespace too much.
86 *****************************************************************************/
87 void _M( aout_getfunctions
)( function_list_t
* p_function_list
)
89 p_function_list
->pf_probe
= aout_Probe
;
90 p_function_list
->functions
.aout
.pf_open
= aout_Open
;
91 p_function_list
->functions
.aout
.pf_setformat
= aout_SetFormat
;
92 p_function_list
->functions
.aout
.pf_getbufinfo
= aout_GetBufInfo
;
93 p_function_list
->functions
.aout
.pf_play
= aout_Play
;
94 p_function_list
->functions
.aout
.pf_close
= aout_Close
;
97 /*****************************************************************************
98 * aout_Probe: probes the audio device and return a score
99 *****************************************************************************
100 * This function tries to open the dps and returns a score to the plugin
101 * manager so that it can
102 *****************************************************************************/
103 static int aout_Probe( probedata_t
*p_data
)
105 if( TestMethod( AOUT_METHOD_VAR
, "esd" ) )
110 /* We don't have to test anything -- if we managed to open this plugin,
111 * it means we have the appropriate libs. */
115 /*****************************************************************************
116 * aout_Open: open an esd socket
117 *****************************************************************************/
118 static int aout_Open( aout_thread_t
*p_aout
)
120 /* mpg123 does it this way */
121 int i_bits
= ESD_BITS16
;
122 int i_mode
= ESD_STREAM
;
123 int i_func
= ESD_PLAY
;
125 /* Allocate structure */
126 p_aout
->p_sys
= malloc( sizeof( aout_sys_t
) );
127 if( p_aout
->p_sys
== NULL
)
129 intf_ErrMsg("error: %s", strerror(ENOMEM
) );
133 /* Initialize some variables */
134 p_aout
->i_format
= AOUT_FORMAT_DEFAULT
;
135 p_aout
->i_channels
= 1 + main_GetIntVariable( AOUT_STEREO_VAR
, AOUT_STEREO_DEFAULT
);
136 p_aout
->l_rate
= main_GetIntVariable( AOUT_RATE_VAR
, AOUT_RATE_DEFAULT
);
141 p_aout
->p_sys
->esd_format
= (i_bits
| i_mode
| i_func
) & (~ESD_MASK_CHAN
);
143 if( p_aout
->i_channels
== 1 )
145 p_aout
->p_sys
->esd_format
|= ESD_MONO
;
149 p_aout
->p_sys
->esd_format
|= ESD_STEREO
;
152 /* open a socket for playing a stream
153 * and try to open /dev/dsp if there's no EsounD */
155 = esd_play_stream_fallback(p_aout
->p_sys
->esd_format
,
156 p_aout
->l_rate
, NULL
, "vlc")) < 0 )
158 intf_ErrMsg( "aout error: can't open esound socket"
159 " (format 0x%08x at %ld Hz)",
160 p_aout
->p_sys
->esd_format
, p_aout
->l_rate
);
164 intf_ErrMsg( "aout error: you are using the Esound plugin. There is no way yet to get the\n"
165 " driver latency because esd_get_latency() hangs, so expect a one\n"
166 " second delay with sound. Type `esdctl off' to disable esd." );
171 /*****************************************************************************
172 * aout_SetFormat: set the output format
173 *****************************************************************************/
174 static int aout_SetFormat( aout_thread_t
*p_aout
)
179 /*****************************************************************************
180 * aout_GetBufInfo: buffer status query
181 *****************************************************************************/
182 static long aout_GetBufInfo( aout_thread_t
*p_aout
, long l_buffer_limit
)
184 /* arbitrary value that should be changed */
185 return( l_buffer_limit
);
188 /*****************************************************************************
189 * aout_Play: play a sound samples buffer
190 *****************************************************************************
191 * This function writes a buffer of i_length bytes in the socket
192 *****************************************************************************/
193 static void aout_Play( aout_thread_t
*p_aout
, byte_t
*buffer
, int i_size
)
197 if (p_aout
->p_sys
->esd_format
& ESD_STEREO
)
199 if (p_aout
->p_sys
->esd_format
& ESD_BITS16
)
201 i_amount
= (44100 * (ESD_BUF_SIZE
+ 64)) / p_aout
->l_rate
;
205 i_amount
= (44100 * (ESD_BUF_SIZE
+ 128)) / p_aout
->l_rate
;
210 if (p_aout
->p_sys
->esd_format
& ESD_BITS16
)
212 i_amount
= (2 * 44100 * (ESD_BUF_SIZE
+ 128)) / p_aout
->l_rate
;
216 i_amount
= (2 * 44100 * (ESD_BUF_SIZE
+ 256)) / p_aout
->l_rate
;
220 write( p_aout
->i_fd
, buffer
, i_size
);
223 /*****************************************************************************
224 * aout_Close: close the Esound socket
225 *****************************************************************************/
226 static void aout_Close( aout_thread_t
*p_aout
)
228 close( p_aout
->i_fd
);