]> git.sesse.net Git - vlc/blob - plugins/esd/aout_esd.c
* ./plugins/macosx/intf_vlc_wrapper.m: fix for non-ASCII filenames in the
[vlc] / plugins / esd / aout_esd.c
1 /*****************************************************************************
2  * aout_esd.c : Esound functions library
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: aout_esd.c,v 1.21 2002/02/24 22:06:50 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 /* TODO:
25  *
26  * - use the libesd function to get latency when it's not buggy anymore
27  *
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <fcntl.h>                                       /* open(), O_WRONLY */
35 #include <string.h>                                            /* strerror() */
36 #include <unistd.h>                                      /* write(), close() */
37 #include <stdio.h>                                           /* "intf_msg.h" */
38 #include <stdlib.h>                            /* calloc(), malloc(), free() */
39
40 #include <esd.h>
41
42 #include <videolan/vlc.h>
43
44 #include "audio_output.h"                                   /* aout_thread_t */
45
46 /*****************************************************************************
47  * aout_sys_t: esd audio output method descriptor
48  *****************************************************************************
49  * This structure is part of the audio output thread descriptor.
50  * It describes some esd specific variables.
51  *****************************************************************************/
52 typedef struct aout_sys_s
53 {
54     esd_format_t esd_format;
55     int          i_fd;
56
57 } aout_sys_t;
58
59 /*****************************************************************************
60  * Local prototypes.
61  *****************************************************************************/
62 static int     aout_Open        ( aout_thread_t *p_aout );
63 static int     aout_SetFormat   ( aout_thread_t *p_aout );
64 static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
65 static void    aout_Play        ( aout_thread_t *p_aout,
66                                   byte_t *buffer, int i_size );
67 static void    aout_Close       ( aout_thread_t *p_aout );
68
69 /*****************************************************************************
70  * Functions exported as capabilities. They are declared as static so that
71  * we don't pollute the namespace too much.
72  *****************************************************************************/
73 void _M( aout_getfunctions )( function_list_t * p_function_list )
74 {
75     p_function_list->functions.aout.pf_open = aout_Open;
76     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
77     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
78     p_function_list->functions.aout.pf_play = aout_Play;
79     p_function_list->functions.aout.pf_close = aout_Close;
80 }
81
82 /*****************************************************************************
83  * aout_Open: open an esd socket
84  *****************************************************************************/
85 static int aout_Open( aout_thread_t *p_aout )
86 {
87     /* mpg123 does it this way */
88     int i_bits = ESD_BITS16;
89     int i_mode = ESD_STREAM;
90     int i_func = ESD_PLAY;
91
92     /* Allocate structure */
93     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
94     if( p_aout->p_sys == NULL )
95     {
96         intf_ErrMsg("error: %s", strerror(ENOMEM) );
97         return( 1 );
98     }
99
100     /* Initialize some variables */
101     p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
102                                       * initial value */
103
104     i_bits = ESD_BITS16;
105     i_mode = ESD_STREAM;
106     i_func = ESD_PLAY;
107     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
108
109     if( p_aout->i_channels == 1 )
110     {
111         p_aout->p_sys->esd_format |= ESD_MONO;
112     }
113     else
114     {
115         p_aout->p_sys->esd_format |= ESD_STEREO;
116     }
117
118     /* open a socket for playing a stream
119      * and try to open /dev/dsp if there's no EsounD */
120     if ( (p_aout->p_sys->i_fd
121             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
122                 p_aout->i_rate, NULL, "vlc")) < 0 )
123     {
124         intf_ErrMsg( "aout error: can't open esound socket"
125                      " (format 0x%08x at %ld Hz)",
126                      p_aout->p_sys->esd_format, p_aout->i_rate );
127         return( -1 );
128     }
129
130     return( 0 );
131 }
132
133 /*****************************************************************************
134  * aout_SetFormat: set the output format
135  *****************************************************************************/
136 static int aout_SetFormat( aout_thread_t *p_aout )
137 {
138     int i_fd;
139
140     i_fd = esd_open_sound(NULL);
141     p_aout->i_latency = esd_get_latency(i_fd);
142    
143     intf_WarnMsg(2, "aout_esd_latency: %d",p_aout->i_latency);
144
145     return( 0 );
146 }
147
148 /*****************************************************************************
149  * aout_GetBufInfo: buffer status query
150  *****************************************************************************/
151 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
152 {
153     /* arbitrary value that should be changed */
154     return( i_buffer_limit );
155 }
156
157 /*****************************************************************************
158  * aout_Play: play a sound samples buffer
159  *****************************************************************************
160  * This function writes a buffer of i_length bytes in the socket
161  *****************************************************************************/
162 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
163 {
164     int i_amount;
165     
166     if (p_aout->p_sys->esd_format & ESD_STEREO)
167     {
168         if (p_aout->p_sys->esd_format & ESD_BITS16)
169         {
170             i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
171         }
172         else
173         {
174             i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
175         }
176     }
177     else
178     {
179         if (p_aout->p_sys->esd_format & ESD_BITS16)
180         {
181             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
182         }
183         else
184         {
185             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
186         }
187     }
188
189     write( p_aout->p_sys->i_fd, buffer, i_size );
190 }
191
192 /*****************************************************************************
193  * aout_Close: close the Esound socket
194  *****************************************************************************/
195 static void aout_Close( aout_thread_t *p_aout )
196 {
197     close( p_aout->p_sys->i_fd );
198 }
199