]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
Remove stdlib.h
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <string.h>                                            /* strerror() */
29 #include <unistd.h>                                      /* write(), close() */
30
31 #include <vlc/vlc.h>
32 #include <vlc_aout.h>
33
34 #include <sys/socket.h>
35
36 #include <sys/time.h>
37 #include <time.h>
38
39 #include <esd.h>
40
41 /*****************************************************************************
42  * aout_sys_t: esd audio output method descriptor
43  *****************************************************************************
44  * This structure is part of the audio output thread descriptor.
45  * It describes some esd specific variables.
46  *****************************************************************************/
47 struct aout_sys_t
48 {
49     esd_format_t esd_format;
50     int          i_fd;
51
52     mtime_t      latency;       /* unused, but we might do something with it */
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open         ( vlc_object_t * );
59 static void Close        ( vlc_object_t * );
60 static void Play         ( aout_instance_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("EsounD audio output") );
67     set_shortname( "EsounD" );
68     set_capability( "audio output", 50 );
69     add_string( "esdserver", "", NULL, N_("Esound server"), NULL, VLC_FALSE );
70     set_category( CAT_AUDIO );
71     set_subcategory( SUBCAT_AUDIO_AOUT );
72     set_callbacks( Open, Close );
73     add_shortcut( "esound" );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: open an esd socket
78  *****************************************************************************/
79 static int Open( vlc_object_t *p_this )
80 {
81     aout_instance_t *p_aout = (aout_instance_t *)p_this;
82     struct aout_sys_t * p_sys;
83     char * psz_server;
84     int i_nb_channels;
85     int i_newfd;
86
87     /* Allocate structure */
88     p_sys = malloc( sizeof( aout_sys_t ) );
89     if( p_sys == NULL )
90     {
91         msg_Err( p_aout, "out of memory" );
92         return VLC_ENOMEM;
93     }
94
95     p_aout->output.p_sys = p_sys;
96
97     p_aout->output.pf_play = Play;
98     aout_VolumeSoftInit( p_aout );
99
100     /* Initialize some variables */
101     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
102     p_sys->esd_format &= ~ESD_MASK_CHAN;
103
104     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
105     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
106     if ( i_nb_channels > 2 )
107     {
108         /* EsounD doesn't support more than two channels. */
109         i_nb_channels = 2;
110         p_aout->output.output.i_physical_channels =
111             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
112     }
113
114     switch( i_nb_channels )
115     {
116     case 1:
117         p_sys->esd_format |= ESD_MONO;
118         break;
119     case 2:
120         p_sys->esd_format |= ESD_STEREO;
121         break;
122     }
123
124     /* Force the rate, otherwise the sound is very noisy */
125     p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
126     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
127
128     /* Open a socket for playing a stream
129      * and try to open /dev/dsp if there's no EsounD */
130     psz_server = config_GetPsz( p_aout, "esdserver" );
131     if( psz_server && *psz_server )
132     {
133         p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
134                                                 p_aout->output.output.i_rate,
135                                                 psz_server, "vlc" );
136     }
137     else
138     {
139         p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
140                                                 p_aout->output.output.i_rate,
141                                                 NULL, "vlc" );
142     }
143
144     if( p_sys->i_fd < 0 )
145     {
146         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
147                          p_sys->esd_format, p_aout->output.output.i_rate );
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152     if( psz_server && *psz_server )
153     {
154         struct timeval start, stop;
155         esd_server_info_t * p_info;
156
157         gettimeofday( &start, NULL );
158         p_info = esd_get_server_info( p_sys->i_fd );
159         gettimeofday( &stop, NULL );
160
161         p_sys->latency = (mtime_t)( stop.tv_sec - start.tv_sec )
162                            * (mtime_t)1000000;
163         p_sys->latency += stop.tv_usec - start.tv_usec;
164     }
165     else
166     {
167         p_sys->latency = 0;
168     }
169
170     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
171      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
172     p_sys->latency +=
173         (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) )
174                     + ESD_BUF_SIZE / 2
175                       * p_aout->output.output.i_bytes_per_frame
176                       * p_aout->output.output.i_rate
177                       / ESD_DEFAULT_RATE )
178       * (mtime_t)1000000
179       / p_aout->output.output.i_bytes_per_frame
180       / p_aout->output.output.i_rate;
181
182     close( i_newfd );
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * Play: nothing to do
188  *****************************************************************************/
189 static void Play( aout_instance_t *p_aout )
190 {
191     struct aout_sys_t * p_sys = p_aout->output.p_sys;
192     aout_buffer_t * p_buffer;
193     int i_tmp;
194
195     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
196
197     if ( p_buffer != NULL )
198     {
199         unsigned int pos;
200         unsigned char *data = p_buffer->p_buffer;
201
202         for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
203              pos += ESD_BUF_SIZE )
204         {
205             i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
206             if( i_tmp < 0 )
207             {
208                 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
209             }
210         }
211         aout_BufferFree( p_buffer );
212     }
213 }
214
215 /*****************************************************************************
216  * Close: close the Esound socket
217  *****************************************************************************/
218 static void Close( vlc_object_t *p_this )
219 {
220     aout_instance_t *p_aout = (aout_instance_t *)p_this;
221     struct aout_sys_t * p_sys = p_aout->output.p_sys;
222
223     close( p_sys->i_fd );
224     free( p_sys );
225 }
226
227 #if 0
228 /*****************************************************************************
229  * Play: play a sound samples buffer
230  *****************************************************************************
231  * This function writes a buffer of i_length bytes in the socket
232  *****************************************************************************/
233 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
234 {
235     int i_amount;
236
237     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
238     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
239
240     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
241
242     write( p_aout->output.p_sys->i_fd, buffer, i_size );
243 }
244 #endif
245