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