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