]> git.sesse.net Git - vlc/blob - plugins/esd/esd.c
* ALL: changed "struct foo_s" into "struct foo_t" to make greppers happy.
[vlc] / plugins / esd / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: esd.c,v 1.16 2002/07/20 18:01:42 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
36 #include <esd.h>
37
38 /*****************************************************************************
39  * aout_sys_t: esd audio output method descriptor
40  *****************************************************************************
41  * This structure is part of the audio output thread descriptor.
42  * It describes some esd specific variables.
43  *****************************************************************************/
44 struct aout_sys_t
45 {
46     esd_format_t esd_format;
47     int          i_fd;
48 };
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static void aout_getfunctions ( function_list_t * p_function_list );
54 static int  aout_Open         ( aout_thread_t * );
55 static int  aout_SetFormat    ( aout_thread_t * );
56 static int  aout_GetBufInfo   ( aout_thread_t *, int );
57 static void aout_Play         ( aout_thread_t *, byte_t *, int );
58 static void aout_Close        ( aout_thread_t * );
59
60 /*****************************************************************************
61  * Build configuration tree.
62  *****************************************************************************/
63 MODULE_CONFIG_START
64 MODULE_CONFIG_STOP
65
66 MODULE_INIT_START
67     SET_DESCRIPTION( _("EsounD audio module") )
68     ADD_CAPABILITY( AOUT, 50 )
69     ADD_SHORTCUT( "esound" )
70 MODULE_INIT_STOP
71
72 MODULE_ACTIVATE_START
73     aout_getfunctions( &p_module->p_functions->aout );
74 MODULE_ACTIVATE_STOP
75
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
78
79 /*****************************************************************************
80  * Functions exported as capabilities. They are declared as static so that
81  * we don't pollute the namespace too much.
82  *****************************************************************************/
83 static void aout_getfunctions( function_list_t * p_function_list )
84 {
85     p_function_list->functions.aout.pf_open = aout_Open;
86     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
87     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
88     p_function_list->functions.aout.pf_play = aout_Play;
89     p_function_list->functions.aout.pf_close = aout_Close;
90 }
91
92 /*****************************************************************************
93  * aout_Open: open an esd socket
94  *****************************************************************************/
95 static int aout_Open( aout_thread_t *p_aout )
96 {
97     /* mpg123 does it this way */
98     int i_bits = ESD_BITS16;
99     int i_mode = ESD_STREAM;
100     int i_func = ESD_PLAY;
101
102     /* Allocate structure */
103     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
104     if( p_aout->p_sys == NULL )
105     {
106         msg_Err( p_aout, "out of memory" );
107         return( 1 );
108     }
109
110     /* Initialize some variables */
111     p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
112                                       * initial value */
113
114     i_bits = ESD_BITS16;
115     i_mode = ESD_STREAM;
116     i_func = ESD_PLAY;
117     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
118
119     if( p_aout->i_channels == 1 )
120     {
121         p_aout->p_sys->esd_format |= ESD_MONO;
122     }
123     else
124     {
125         p_aout->p_sys->esd_format |= ESD_STEREO;
126     }
127
128     /* open a socket for playing a stream
129      * and try to open /dev/dsp if there's no EsounD */
130     if ( (p_aout->p_sys->i_fd
131             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
132                 p_aout->i_rate, NULL, "vlc")) < 0 )
133     {
134         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %ld Hz)",
135                          p_aout->p_sys->esd_format, p_aout->i_rate );
136         return( -1 );
137     }
138
139     return( 0 );
140 }
141
142 /*****************************************************************************
143  * aout_SetFormat: set the output format
144  *****************************************************************************/
145 static int aout_SetFormat( aout_thread_t *p_aout )
146 {
147     int i_fd;
148
149     i_fd = esd_open_sound(NULL);
150     p_aout->i_latency = esd_get_latency(i_fd);
151    
152     msg_Dbg( p_aout, "aout_esd_latency: %d", p_aout->i_latency );
153
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * aout_GetBufInfo: buffer status query
159  *****************************************************************************/
160 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
161 {
162     /* arbitrary value that should be changed */
163     return( i_buffer_limit );
164 }
165
166 /*****************************************************************************
167  * aout_Play: play a sound samples buffer
168  *****************************************************************************
169  * This function writes a buffer of i_length bytes in the socket
170  *****************************************************************************/
171 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
172 {
173     int i_amount;
174     
175     if (p_aout->p_sys->esd_format & ESD_STEREO)
176     {
177         if (p_aout->p_sys->esd_format & ESD_BITS16)
178         {
179             i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
180         }
181         else
182         {
183             i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
184         }
185     }
186     else
187     {
188         if (p_aout->p_sys->esd_format & ESD_BITS16)
189         {
190             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
191         }
192         else
193         {
194             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
195         }
196     }
197
198     write( p_aout->p_sys->i_fd, buffer, i_size );
199 }
200
201 /*****************************************************************************
202  * aout_Close: close the Esound socket
203  *****************************************************************************/
204 static void aout_Close( aout_thread_t *p_aout )
205 {
206     close( p_aout->p_sys->i_fd );
207 }
208