]> git.sesse.net Git - vlc/blob - plugins/esd/aout_esd.c
62b188b2fa1f48b55e7df135028f9d0a69abcd20
[vlc] / plugins / esd / aout_esd.c
1 /*****************************************************************************
2  * aout_esd.c : Esound functions library
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  *
6  * Authors:
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 /*****************************************************************************
55  * aout_sys_t: esd audio output method descriptor
56  *****************************************************************************
57  * This structure is part of the audio output thread descriptor.
58  * It describes some esd specific variables.
59  *****************************************************************************/
60 typedef struct aout_sys_s
61 {
62     esd_format_t esd_format;
63
64 } aout_sys_t;
65
66 /*****************************************************************************
67  * aout_EsdOpen: opens an esd socket
68  *****************************************************************************/
69 int aout_EsdOpen( aout_thread_t *p_aout )
70 {
71     /* mpg123 does it this way */
72     int i_bits = ESD_BITS16;
73     int i_mode = ESD_STREAM;
74     int i_func = ESD_PLAY;
75
76     fprintf(stderr, "aout-esd !!\n");
77     /* Allocate structure */
78     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
79     if( p_aout->p_sys == NULL )
80     {
81         intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
82         return( 1 );
83     }
84
85     /* Initialize some variables */
86     p_aout->i_format = AOUT_DEFAULT_FORMAT;
87     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
88     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
89
90     i_bits = ESD_BITS16;
91     i_mode = ESD_STREAM;
92     i_func = ESD_PLAY;
93     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
94
95     if( p_aout->i_channels == 1 )
96         p_aout->p_sys->esd_format |= ESD_MONO;
97     else
98         p_aout->p_sys->esd_format |= ESD_STEREO;
99
100     /* open a socket for playing a stream
101      * and try to open /dev/dsp if there's no EsounD */
102     if ( (p_aout->i_fd
103             = esd_play_stream_fallback(p_aout->p_sys->esd_format,
104                 p_aout->l_rate, NULL, "vlc")) < 0 )
105     {
106         intf_ErrMsg( "aout error: can't open esound socket"
107                      " (format 0x%08x at %ld Hz)\n",
108                      p_aout->p_sys->esd_format, p_aout->l_rate );
109         return( -1 );
110     }
111
112     return( 0 );
113 }
114
115 /*****************************************************************************
116  * aout_EsdReset: resets the dsp
117  *****************************************************************************/
118 int aout_EsdReset( aout_thread_t *p_aout )
119 {
120     return( 0 );
121 }
122
123 /*****************************************************************************
124  * aout_EsdSetFormat: sets the dsp output format
125  *****************************************************************************/
126 int aout_EsdSetFormat( aout_thread_t *p_aout )
127 {
128     return( 0 );
129 }
130
131 /*****************************************************************************
132  * aout_EsdSetChannels: sets the dsp's stereo or mono mode
133  *****************************************************************************/
134 int aout_EsdSetChannels( aout_thread_t *p_aout )
135 {
136     return( 0 );
137 }
138
139 /*****************************************************************************
140  * aout_EsdSetRate: sets the dsp's audio output rate
141  *****************************************************************************/
142 int aout_EsdSetRate( aout_thread_t *p_aout )
143 {
144     return( 0 );
145 }
146
147 /*****************************************************************************
148  * aout_EsdGetBufInfo: buffer status query
149  *****************************************************************************/
150 long aout_EsdGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
151 {
152     /* arbitrary value that should be changed */
153     return( l_buffer_limit );
154 }
155
156 /*****************************************************************************
157  * aout_EsdPlaySamples: plays a sound samples buffer
158  *****************************************************************************
159  * This function writes a buffer of i_length bytes in the dsp
160  *****************************************************************************/
161 void aout_EsdPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
162 {
163     int amount;
164
165     if (p_aout->p_sys->esd_format & ESD_STEREO)
166     {
167         if (p_aout->p_sys->esd_format & ESD_BITS16)
168             amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
169         else
170             amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
171     }
172     else
173     {
174         if (p_aout->p_sys->esd_format & ESD_BITS16)
175             amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
176         else
177             amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
178     }
179
180     intf_DbgMsg( "aout: latency is %i\n", amount );
181
182     write( p_aout->i_fd, buffer, i_size );
183 }
184
185 /*****************************************************************************
186  * aout_EsdClose: closes the dsp audio device
187  *****************************************************************************/
188 void aout_EsdClose( aout_thread_t *p_aout )
189 {
190     close( p_aout->i_fd );
191 }
192