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