]> git.sesse.net Git - vlc/blob - plugins/esd/aout_esd.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[vlc] / plugins / esd / aout_esd.c
1 /*****************************************************************************
2  * aout_esd.c : Esound functions library
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  *
6  * Authors: Samuel Hocevar <sam@zoy.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 #define MODULE_NAME esd
24 #include "modules_inner.h"
25
26 /* TODO:
27  *
28  * - use the libesd function to get latency when it's not buggy anymore
29  *
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include "defs.h"
36
37 #include <errno.h>                                                 /* ENOMEM */
38 #include <fcntl.h>                                       /* open(), O_WRONLY */
39 #include <string.h>                                            /* strerror() */
40 #include <unistd.h>                                      /* write(), close() */
41 #include <stdio.h>                                           /* "intf_msg.h" */
42 #include <stdlib.h>                            /* calloc(), malloc(), free() */
43
44 #include <esd.h>
45
46 #include "config.h"
47 #include "common.h"                                     /* boolean_t, byte_t */
48 #include "threads.h"
49 #include "mtime.h"
50 #include "tests.h"
51
52 #include "audio_output.h"                                   /* aout_thread_t */
53
54 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
55 #include "main.h"
56
57 #include "modules.h"
58
59 /*****************************************************************************
60  * aout_sys_t: esd audio output method descriptor
61  *****************************************************************************
62  * This structure is part of the audio output thread descriptor.
63  * It describes some esd specific variables.
64  *****************************************************************************/
65 typedef struct aout_sys_s
66 {
67     esd_format_t esd_format;
68
69 } aout_sys_t;
70
71 /*****************************************************************************
72  * Local prototypes.
73  *****************************************************************************/
74 static int     aout_Probe       ( probedata_t *p_data );
75 static int     aout_Open        ( aout_thread_t *p_aout );
76 static int     aout_SetFormat   ( aout_thread_t *p_aout );
77 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
78 static void    aout_Play        ( aout_thread_t *p_aout,
79                                   byte_t *buffer, int i_size );
80 static void    aout_Close       ( aout_thread_t *p_aout );
81
82 /*****************************************************************************
83  * Functions exported as capabilities. They are declared as static so that
84  * we don't pollute the namespace too much.
85  *****************************************************************************/
86 void _M( aout_getfunctions )( function_list_t * p_function_list )
87 {
88     p_function_list->pf_probe = aout_Probe;
89     p_function_list->functions.aout.pf_open = aout_Open;
90     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
91     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
92     p_function_list->functions.aout.pf_play = aout_Play;
93     p_function_list->functions.aout.pf_close = aout_Close;
94 }
95
96 /*****************************************************************************
97  * aout_Probe: probes the audio device and return a score
98  *****************************************************************************
99  * This function tries to open the dps and returns a score to the plugin
100  * manager so that it can 
101  *****************************************************************************/
102 static int aout_Probe( probedata_t *p_data )
103 {
104     if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
105     {
106         return( 999 );
107     }
108
109     /* We don't have to test anything -- if we managed to open this plugin,
110      * it means we have the appropriate libs. */
111     return( 50 );
112 }
113
114 /*****************************************************************************
115  * aout_Open: open an esd socket
116  *****************************************************************************/
117 static int aout_Open( aout_thread_t *p_aout )
118 {
119     /* mpg123 does it this way */
120     int i_bits = ESD_BITS16;
121     int i_mode = ESD_STREAM;
122     int i_func = ESD_PLAY;
123
124     /* Allocate structure */
125     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
126     if( p_aout->p_sys == NULL )
127     {
128         intf_ErrMsg("error: %s", strerror(ENOMEM) );
129         return( 1 );
130     }
131
132     /* Initialize some variables */
133     p_aout->i_format = AOUT_FORMAT_DEFAULT;
134     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
135     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
136
137     i_bits = ESD_BITS16;
138     i_mode = ESD_STREAM;
139     i_func = ESD_PLAY;
140     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
141
142     if( p_aout->i_channels == 1 )
143         p_aout->p_sys->esd_format |= ESD_MONO;
144     else
145         p_aout->p_sys->esd_format |= ESD_STEREO;
146
147     /* open a socket for playing a stream
148      * and try to open /dev/dsp if there's no EsounD */
149     if ( (p_aout->i_fd
150             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
151                 p_aout->l_rate, NULL, "vlc")) < 0 )
152     {
153         intf_ErrMsg( "aout error: can't open esound socket"
154                      " (format 0x%08x at %ld Hz)",
155                      p_aout->p_sys->esd_format, p_aout->l_rate );
156         return( -1 );
157     }
158
159     return( 0 );
160 }
161
162 /*****************************************************************************
163  * aout_SetFormat: set the output format
164  *****************************************************************************/
165 static int aout_SetFormat( aout_thread_t *p_aout )
166 {
167     return( 0 );
168 }
169
170 /*****************************************************************************
171  * aout_GetBufInfo: buffer status query
172  *****************************************************************************/
173 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
174 {
175     /* arbitrary value that should be changed */
176     return( l_buffer_limit );
177 }
178
179 /*****************************************************************************
180  * aout_Play: play a sound samples buffer
181  *****************************************************************************
182  * This function writes a buffer of i_length bytes in the socket
183  *****************************************************************************/
184 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
185 {
186     int amount;
187
188     if (p_aout->p_sys->esd_format & ESD_STEREO)
189     {
190         if (p_aout->p_sys->esd_format & ESD_BITS16)
191             amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
192         else
193             amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
194     }
195     else
196     {
197         if (p_aout->p_sys->esd_format & ESD_BITS16)
198             amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
199         else
200             amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
201     }
202
203     intf_DbgMsg( "aout: latency is %i", amount );
204
205     write( p_aout->i_fd, buffer, i_size );
206 }
207
208 /*****************************************************************************
209  * aout_Close: close the Esound socket
210  *****************************************************************************/
211 static void aout_Close( aout_thread_t *p_aout )
212 {
213     close( p_aout->i_fd );
214 }
215