]> git.sesse.net Git - vlc/blob - plugins/esd/aout_esd.c
* Fixed the BeOS compile typo.
[vlc] / plugins / esd / aout_esd.c
1 /*****************************************************************************
2  * aout_esd.c : Esound functions library
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: aout_esd.c,v 1.13 2001/05/30 17:03:12 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 #define MODULE_NAME esd
25 #include "modules_inner.h"
26
27 /* TODO:
28  *
29  * - use the libesd function to get latency when it's not buggy anymore
30  *
31  */
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include "defs.h"
37
38 #include <errno.h>                                                 /* ENOMEM */
39 #include <fcntl.h>                                       /* open(), O_WRONLY */
40 #include <string.h>                                            /* strerror() */
41 #include <unistd.h>                                      /* write(), close() */
42 #include <stdio.h>                                           /* "intf_msg.h" */
43 #include <stdlib.h>                            /* calloc(), malloc(), free() */
44
45 #include <esd.h>
46
47 #include "config.h"
48 #include "common.h"                                     /* boolean_t, byte_t */
49 #include "threads.h"
50 #include "mtime.h"
51 #include "tests.h"
52
53 #include "audio_output.h"                                   /* aout_thread_t */
54
55 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
56 #include "main.h"
57
58 #include "modules.h"
59 #include "modules_export.h"
60
61 /*****************************************************************************
62  * aout_sys_t: esd audio output method descriptor
63  *****************************************************************************
64  * This structure is part of the audio output thread descriptor.
65  * It describes some esd specific variables.
66  *****************************************************************************/
67 typedef struct aout_sys_s
68 {
69     esd_format_t esd_format;
70
71 } aout_sys_t;
72
73 /*****************************************************************************
74  * Local prototypes.
75  *****************************************************************************/
76 static int     aout_Probe       ( probedata_t *p_data );
77 static int     aout_Open        ( aout_thread_t *p_aout );
78 static int     aout_SetFormat   ( aout_thread_t *p_aout );
79 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
80 static void    aout_Play        ( aout_thread_t *p_aout,
81                                   byte_t *buffer, int i_size );
82 static void    aout_Close       ( aout_thread_t *p_aout );
83
84 /*****************************************************************************
85  * Functions exported as capabilities. They are declared as static so that
86  * we don't pollute the namespace too much.
87  *****************************************************************************/
88 void _M( aout_getfunctions )( function_list_t * p_function_list )
89 {
90     p_function_list->pf_probe = aout_Probe;
91     p_function_list->functions.aout.pf_open = aout_Open;
92     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
93     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
94     p_function_list->functions.aout.pf_play = aout_Play;
95     p_function_list->functions.aout.pf_close = aout_Close;
96 }
97
98 /*****************************************************************************
99  * aout_Probe: probes the audio device and return a score
100  *****************************************************************************
101  * This function tries to open the dps and returns a score to the plugin
102  * manager so that it can 
103  *****************************************************************************/
104 static int aout_Probe( probedata_t *p_data )
105 {
106     if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
107     {
108         return( 999 );
109     }
110
111     /* We don't have to test anything -- if we managed to open this plugin,
112      * it means we have the appropriate libs. */
113     return( 50 );
114 }
115
116 /*****************************************************************************
117  * aout_Open: open an esd socket
118  *****************************************************************************/
119 static int aout_Open( aout_thread_t *p_aout )
120 {
121     /* mpg123 does it this way */
122     int i_bits = ESD_BITS16;
123     int i_mode = ESD_STREAM;
124     int i_func = ESD_PLAY;
125
126     /* Allocate structure */
127     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
128     if( p_aout->p_sys == NULL )
129     {
130         intf_ErrMsg("error: %s", strerror(ENOMEM) );
131         return( 1 );
132     }
133
134     /* Initialize some variables */
135     p_aout->i_format = AOUT_FORMAT_DEFAULT;
136     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
137     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
138
139     i_bits = ESD_BITS16;
140     i_mode = ESD_STREAM;
141     i_func = ESD_PLAY;
142     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
143
144     if( p_aout->i_channels == 1 )
145     {
146         p_aout->p_sys->esd_format |= ESD_MONO;
147     }
148     else
149     {
150         p_aout->p_sys->esd_format |= ESD_STEREO;
151     }
152
153     /* open a socket for playing a stream
154      * and try to open /dev/dsp if there's no EsounD */
155     if ( (p_aout->i_fd
156             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
157                 p_aout->l_rate, NULL, "vlc")) < 0 )
158     {
159         intf_ErrMsg( "aout error: can't open esound socket"
160                      " (format 0x%08x at %ld Hz)",
161                      p_aout->p_sys->esd_format, p_aout->l_rate );
162         return( -1 );
163     }
164
165     intf_ErrMsg( "aout error: you are using the Esound plugin. There is no way yet to get the\n"
166                  "            driver latency because esd_get_latency() hangs, so expect a one\n"
167                  "            second delay with sound. Type `esdctl off' to disable esd." );
168
169     return( 0 );
170 }
171
172 /*****************************************************************************
173  * aout_SetFormat: set the output format
174  *****************************************************************************/
175 static int aout_SetFormat( aout_thread_t *p_aout )
176 {
177     return( 0 );
178 }
179
180 /*****************************************************************************
181  * aout_GetBufInfo: buffer status query
182  *****************************************************************************/
183 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
184 {
185     /* arbitrary value that should be changed */
186     return( l_buffer_limit );
187 }
188
189 /*****************************************************************************
190  * aout_Play: play a sound samples buffer
191  *****************************************************************************
192  * This function writes a buffer of i_length bytes in the socket
193  *****************************************************************************/
194 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
195 {
196     int i_amount;
197
198     if (p_aout->p_sys->esd_format & ESD_STEREO)
199     {
200         if (p_aout->p_sys->esd_format & ESD_BITS16)
201         {
202             i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
203         }
204         else
205         {
206             i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
207         }
208     }
209     else
210     {
211         if (p_aout->p_sys->esd_format & ESD_BITS16)
212         {
213             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
214         }
215         else
216         {
217             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
218         }
219     }
220
221     write( p_aout->i_fd, buffer, i_size );
222 }
223
224 /*****************************************************************************
225  * aout_Close: close the Esound socket
226  *****************************************************************************/
227 static void aout_Close( aout_thread_t *p_aout )
228 {
229     close( p_aout->i_fd );
230 }
231