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