]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
0022da6d7fb2ac1e5002d6d7e0caff15dff9320d
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
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() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <sys/socket.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_capability( "audio output", 50 );
68     set_callbacks( Open, Close );
69     add_shortcut( "esound" );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * Open: open an esd socket
74  *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
76 {
77     aout_instance_t *p_aout = (aout_instance_t *)p_this;
78     struct aout_sys_t * p_sys;
79     int i_nb_channels;
80     int i_newfd = -1;
81     int fl;
82
83     /* Allocate structure */
84     p_sys = malloc( sizeof( aout_sys_t ) );
85     if( p_sys == NULL )
86     {
87         msg_Err( p_aout, "out of memory" );
88         return VLC_ENOMEM;
89     }
90
91     p_aout->output.p_sys = p_sys;
92
93     p_aout->output.pf_play = Play;
94     aout_VolumeSoftInit( p_aout );
95
96     /* Initialize some variables */
97     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
98     p_sys->esd_format &= ~ESD_MASK_CHAN;
99
100     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
101     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
102     if ( i_nb_channels > 2 )
103     {
104         /* EsounD doesn't support more than two channels. */
105         i_nb_channels = 2;
106         p_aout->output.output.i_physical_channels =
107             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
108     }
109
110     switch( i_nb_channels )
111     {
112     case 1:
113         p_sys->esd_format |= ESD_MONO;
114         break;
115     case 2:
116         p_sys->esd_format |= ESD_STEREO;
117         break;
118     }
119
120     /* Force the rate, otherwise the sound is very noisy */
121     p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
122
123     /* open a socket for playing a stream
124      * and try to open /dev/dsp if there's no EsounD */
125     p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
126                               p_aout->output.output.i_rate, NULL, "vlc" );
127     if( p_sys->i_fd < 0 )
128     {
129         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
130                          p_sys->esd_format, p_aout->output.output.i_rate );
131         free( p_sys );
132         return VLC_EGENERIC;
133     }
134
135     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
136
137     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
138      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
139     p_sys->latency =
140         (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) ) +
141                       ESD_BUF_SIZE/2
142                     * p_aout->output.output.i_bytes_per_frame
143                     * p_aout->output.output.i_rate
144                     / ESD_DEFAULT_RATE )
145       * (mtime_t)1000000
146       / p_aout->output.output.i_bytes_per_frame
147       / p_aout->output.output.i_rate;
148
149     close( i_newfd );
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * Play: nothing to do
155  *****************************************************************************/
156 static void Play( aout_instance_t *p_aout )
157 {
158     struct aout_sys_t * p_sys = p_aout->output.p_sys;
159     aout_buffer_t * p_buffer;
160     int i_tmp;
161
162     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
163
164     if ( p_buffer != NULL )
165     {
166         int pos;
167         char *data = p_buffer->p_buffer;
168
169         for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes; 
170              pos += ESD_BUF_SIZE )
171         {
172             i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
173             if( i_tmp < 0 )
174             {
175                 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
176             }
177         }
178         aout_BufferFree( p_buffer );
179     }
180 }
181
182 /*****************************************************************************
183  * Close: close the Esound socket
184  *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
186 {
187     aout_instance_t *p_aout = (aout_instance_t *)p_this;
188     struct aout_sys_t * p_sys = p_aout->output.p_sys;
189
190     close( p_sys->i_fd );
191     free( p_sys );
192 }
193
194 #if 0
195 /*****************************************************************************
196  * Play: play a sound samples buffer
197  *****************************************************************************
198  * This function writes a buffer of i_length bytes in the socket
199  *****************************************************************************/
200 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
201 {
202     int i_amount;
203
204     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
205     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
206
207     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
208
209     write( p_aout->output.p_sys->i_fd, buffer, i_size );
210 }
211 #endif
212