1 /*****************************************************************************
2 * esd.c : EsounD module
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: esd.c,v 1.18 2003/03/30 18:14:36 gbazin 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 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
30 #include <unistd.h> /* write(), close() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
35 #include "aout_internal.h"
39 /*****************************************************************************
40 * aout_sys_t: esd audio output method descriptor
41 *****************************************************************************
42 * This structure is part of the audio output thread descriptor.
43 * It describes some esd specific variables.
44 *****************************************************************************/
47 esd_format_t esd_format;
50 mtime_t latency; /* unused, but we might do something with it */
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
58 static void Play ( aout_instance_t * );
60 /*****************************************************************************
62 *****************************************************************************/
64 set_description( _("EsounD audio output") );
65 set_capability( "audio output", 50 );
66 set_callbacks( Open, Close );
67 add_shortcut( "esound" );
70 /*****************************************************************************
71 * Open: open an esd socket
72 *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
75 aout_instance_t *p_aout = (aout_instance_t *)p_this;
76 struct aout_sys_t * p_sys;
79 /* Allocate structure */
80 p_sys = malloc( sizeof( aout_sys_t ) );
83 msg_Err( p_aout, "out of memory" );
87 p_aout->output.p_sys = p_sys;
89 p_aout->output.pf_play = Play;
90 aout_VolumeSoftInit( p_aout );
92 /* Initialize some variables */
93 p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
94 p_sys->esd_format &= ~ESD_MASK_CHAN;
96 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
97 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
98 if ( i_nb_channels > 2 )
100 /* EsounD doesn't support more than two channels. */
102 p_aout->output.output.i_physical_channels =
103 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
106 switch( i_nb_channels )
109 p_sys->esd_format |= ESD_MONO;
112 p_sys->esd_format |= ESD_STEREO;
116 /* open a socket for playing a stream
117 * and try to open /dev/dsp if there's no EsounD */
118 p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
119 p_aout->output.output.i_rate, NULL, "vlc" );
120 if( p_sys->i_fd < 0 )
122 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
123 p_sys->esd_format, p_aout->output.output.i_rate );
128 p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
130 /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
131 * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
133 (mtime_t)( esd_get_latency( esd_open_sound(NULL) ) + ESD_BUF_SIZE/2
134 * p_aout->output.output.i_bytes_per_frame
135 * p_aout->output.output.i_rate
138 / p_aout->output.output.i_bytes_per_frame
139 / p_aout->output.output.i_rate;
144 /*****************************************************************************
145 * Play: nothing to do
146 *****************************************************************************/
147 static void Play( aout_instance_t *p_aout )
149 struct aout_sys_t * p_sys = p_aout->output.p_sys;
150 aout_buffer_t * p_buffer;
153 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
155 if ( p_buffer != NULL )
157 i_tmp = write( p_sys->i_fd, p_buffer->p_buffer, p_buffer->i_nb_bytes );
161 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
164 aout_BufferFree( p_buffer );
168 /*****************************************************************************
169 * Close: close the Esound socket
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 aout_instance_t *p_aout = (aout_instance_t *)p_this;
174 struct aout_sys_t * p_sys = p_aout->output.p_sys;
176 close( p_sys->i_fd );
181 /*****************************************************************************
182 * Play: play a sound samples buffer
183 *****************************************************************************
184 * This function writes a buffer of i_length bytes in the socket
185 *****************************************************************************/
186 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
190 int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
191 int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
193 i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
195 write( p_aout->output.p_sys->i_fd, buffer, i_size );