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