]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
* ./modules/audio_output/*: ported the ALSA, aRts, esd and OSS modules to
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: esd.c,v 1.8 2002/08/19 23:07:30 sam Exp $
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 <esd.h>
38
39 /*****************************************************************************
40  * aout_sys_t: esd audio output method descriptor
41  *****************************************************************************
42  * This structure is part of the audio output thread descriptor.
43  * It describes some esd specific variables.
44  *****************************************************************************/
45 struct aout_sys_t
46 {
47     esd_format_t esd_format;
48     int          i_fd;
49     vlc_bool_t   b_initialized;
50
51     mtime_t      latency;
52 };
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 static int  Open         ( vlc_object_t * );
58 static void Close        ( vlc_object_t * );
59
60 static int  SetFormat    ( aout_instance_t * );
61 static void Play         ( aout_instance_t * );
62 static int  ESDThread    ( aout_instance_t * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin();
68     set_description( _("EsounD audio module") );
69     set_capability( "audio output", 50 );
70     set_callbacks( Open, Close );
71     add_shortcut( "esound" );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * Open: open an esd socket
76  *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
78 {
79     aout_instance_t *p_aout = (aout_instance_t *)p_this;
80     struct aout_sys_t * p_sys;
81
82     /* Allocate structure */
83     p_sys = malloc( sizeof( aout_sys_t ) );
84     if( p_sys == NULL )
85     {
86         msg_Err( p_aout, "out of memory" );
87         return -1;
88     }
89
90     p_aout->output.p_sys = p_sys;
91
92     /* Create ESD thread and wait for its readiness. */
93     p_sys->b_initialized = VLC_FALSE;
94     if( vlc_thread_create( p_aout, "aout", ESDThread, VLC_FALSE ) )
95     {
96         msg_Err( p_aout, "cannot create ESD thread (%s)", strerror(errno) );
97         free( p_sys );
98         return -1;
99     }
100
101     p_aout->output.pf_setformat = SetFormat;
102     p_aout->output.pf_play = Play;
103
104     return( 0 );
105 }
106
107 /*****************************************************************************
108  * SetFormat: set the output format
109  *****************************************************************************/
110 static int SetFormat( aout_instance_t *p_aout )
111 {
112     struct aout_sys_t * p_sys = p_aout->output.p_sys;
113
114     p_sys->b_initialized = VLC_FALSE;
115
116     /* Initialize some variables */
117     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
118     p_sys->esd_format &= ~ESD_MASK_CHAN;
119
120     switch( p_aout->output.output.i_channels )
121     {
122     case 1:
123         p_sys->esd_format |= ESD_MONO;
124         break;
125     case 2:
126         p_sys->esd_format |= ESD_STEREO;
127         break;
128     default:
129         return -1;
130     }
131
132     /* open a socket for playing a stream
133      * and try to open /dev/dsp if there's no EsounD */
134     p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
135                               p_aout->output.output.i_rate, NULL, "vlc" );
136     if( p_sys->i_fd < 0 )
137     {
138         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %ld Hz)",
139                          p_sys->esd_format, p_aout->output.output.i_rate );
140         return -1;
141     }
142
143     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
144     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
145
146     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
147      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
148     p_sys->latency =
149         (mtime_t)( esd_get_latency( esd_open_sound(NULL) ) + ESD_BUF_SIZE/2
150                     * p_aout->output.output.i_bytes_per_frame
151                     * p_aout->output.output.i_rate
152                     / p_aout->output.output.i_frame_length
153                     / ESD_DEFAULT_RATE )
154       * (mtime_t)1000000
155       / p_aout->output.output.i_bytes_per_frame
156       / p_aout->output.output.i_rate
157       * p_aout->output.output.i_frame_length;
158
159     p_sys->b_initialized = VLC_TRUE;
160
161     return 0;
162 }
163
164 /*****************************************************************************
165  * Play: queue a buffer for playing by ESDThread
166  *****************************************************************************/
167 static void Play( aout_instance_t *p_aout )
168 {
169 }
170
171 /*****************************************************************************
172  * Close: close the Esound socket
173  *****************************************************************************/
174 static void Close( vlc_object_t *p_this )
175 {
176     aout_instance_t *p_aout = (aout_instance_t *)p_this;
177     struct aout_sys_t * p_sys = p_aout->output.p_sys;
178
179     p_aout->b_die = 1;
180     vlc_thread_join( p_aout );
181
182     close( p_sys->i_fd );
183     free( p_sys );
184 }
185
186 /*****************************************************************************
187  * ESDThread: asynchronous thread used to DMA the data to the device
188  *****************************************************************************/
189 static int ESDThread( aout_instance_t * p_aout )
190 {
191     struct aout_sys_t * p_sys = p_aout->output.p_sys;
192
193     while ( !p_aout->b_die )
194     {
195         aout_buffer_t * p_buffer;
196         int i_tmp, i_size;
197         byte_t * p_bytes;
198
199         if( !p_sys->b_initialized )
200         {
201             msleep( THREAD_SLEEP );
202             continue;
203         }
204
205         /* Get the presentation date of the next write() operation. It
206          * is equal to the current date + buffered samples + esd latency */
207         p_buffer = aout_OutputNextBuffer( p_aout, mdate() + p_sys->latency, 0 );
208
209         if ( p_buffer != NULL )
210         {
211             p_bytes = p_buffer->p_buffer;
212             i_size = p_buffer->i_nb_bytes;
213         }
214         else
215         {
216             i_size = ESD_BUF_SIZE * 2
217                       / p_aout->output.output.i_frame_length
218                       * p_aout->output.output.i_bytes_per_frame;
219             p_bytes = alloca( i_size );
220             memset( p_bytes, 0, i_size );
221         }
222
223         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
224
225         if( i_tmp < 0 )
226         {
227             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
228         }
229
230         if ( p_buffer != NULL )
231         {
232             aout_BufferFree( p_buffer );
233         }
234     }
235
236     return 0;
237 }
238
239 #if 0
240 /*****************************************************************************
241  * Play: play a sound samples buffer
242  *****************************************************************************
243  * This function writes a buffer of i_length bytes in the socket
244  *****************************************************************************/
245 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
246 {
247     int i_amount;
248
249     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
250     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
251
252     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
253
254     write( p_aout->output.p_sys->i_fd, buffer, i_size );
255 }
256 #endif
257