]> git.sesse.net Git - vlc/blob - plugins/esd/aout_esd.c
. complete commenting of modules_core.h and small modifications
[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 "plugins.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->p_probe = aout_Probe;
86     p_function_list->functions.aout.p_open = aout_Open;
87     p_function_list->functions.aout.p_setformat = aout_SetFormat;
88     p_function_list->functions.aout.p_getbufinfo = aout_GetBufInfo;
89     p_function_list->functions.aout.p_play = aout_Play;
90     p_function_list->functions.aout.p_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     /* We don't have to test anything -- if we managed to open this plugin,
102      * it means we have the appropriate libs. */
103     return( 50 );
104 }
105
106 /*****************************************************************************
107  * aout_Open: open an esd socket
108  *****************************************************************************/
109 static int aout_Open( aout_thread_t *p_aout )
110 {
111     /* mpg123 does it this way */
112     int i_bits = ESD_BITS16;
113     int i_mode = ESD_STREAM;
114     int i_func = ESD_PLAY;
115
116     /* Allocate structure */
117     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
118     if( p_aout->p_sys == NULL )
119     {
120         intf_ErrMsg("error: %s", strerror(ENOMEM) );
121         return( 1 );
122     }
123
124     /* Initialize some variables */
125     p_aout->i_format = AOUT_FORMAT_DEFAULT;
126     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
127     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
128
129     i_bits = ESD_BITS16;
130     i_mode = ESD_STREAM;
131     i_func = ESD_PLAY;
132     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
133
134     if( p_aout->i_channels == 1 )
135         p_aout->p_sys->esd_format |= ESD_MONO;
136     else
137         p_aout->p_sys->esd_format |= ESD_STEREO;
138
139     /* open a socket for playing a stream
140      * and try to open /dev/dsp if there's no EsounD */
141     if ( (p_aout->i_fd
142             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
143                 p_aout->l_rate, NULL, "vlc")) < 0 )
144     {
145         intf_ErrMsg( "aout error: can't open esound socket"
146                      " (format 0x%08x at %ld Hz)",
147                      p_aout->p_sys->esd_format, p_aout->l_rate );
148         return( -1 );
149     }
150
151     return( 0 );
152 }
153
154 /*****************************************************************************
155  * aout_SetFormat: set the output format
156  *****************************************************************************/
157 static int aout_SetFormat( aout_thread_t *p_aout )
158 {
159     return( 0 );
160 }
161
162 /*****************************************************************************
163  * aout_GetBufInfo: buffer status query
164  *****************************************************************************/
165 long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
166 {
167     /* arbitrary value that should be changed */
168     return( l_buffer_limit );
169 }
170
171 /*****************************************************************************
172  * aout_Play: play a sound samples buffer
173  *****************************************************************************
174  * This function writes a buffer of i_length bytes in the socket
175  *****************************************************************************/
176 void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
177 {
178     int amount;
179
180     if (p_aout->p_sys->esd_format & ESD_STEREO)
181     {
182         if (p_aout->p_sys->esd_format & ESD_BITS16)
183             amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
184         else
185             amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
186     }
187     else
188     {
189         if (p_aout->p_sys->esd_format & ESD_BITS16)
190             amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
191         else
192             amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
193     }
194
195     intf_DbgMsg( "aout: latency is %i", amount );
196
197     write( p_aout->i_fd, buffer, i_size );
198 }
199
200 /*****************************************************************************
201  * aout_Close: close the Esound socket
202  *****************************************************************************/
203 void aout_Close( aout_thread_t *p_aout )
204 {
205     close( p_aout->i_fd );
206 }
207