]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
* modules/demux/mpeg: Added DVB stream type for A/52 streams (0x6),
[vlc] / modules / audio_output / esd.c
1 /*****************************************************************************
2  * esd.c : EsounD module
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: esd.c,v 1.14 2002/10/20 12:23:47 massiot 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <fcntl.h>                                       /* open(), O_WRONLY */
29 #include <string.h>                                            /* strerror() */
30 #include <unistd.h>                                      /* write(), close() */
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <esd.h>
38
39 /*****************************************************************************
40  * aout_sys_t: esd audio output method descriptor
41  *****************************************************************************
42  * This structure is part of the audio output thread descriptor.
43  * It describes some esd specific variables.
44  *****************************************************************************/
45 struct aout_sys_t
46 {
47     esd_format_t esd_format;
48     int          i_fd;
49
50     mtime_t      latency;
51 };
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static int  Open         ( vlc_object_t * );
57 static void Close        ( vlc_object_t * );
58 static void Play         ( aout_instance_t * );
59 static int  ESDThread    ( aout_instance_t * );
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_description( _("EsounD audio module") );
66     set_capability( "audio output", 50 );
67     set_callbacks( Open, Close );
68     add_shortcut( "esound" );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Open: open an esd socket
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     aout_instance_t *p_aout = (aout_instance_t *)p_this;
77     struct aout_sys_t * p_sys;
78     int i_nb_channels;
79
80     /* Allocate structure */
81     p_sys = malloc( sizeof( aout_sys_t ) );
82     if( p_sys == NULL )
83     {
84         msg_Err( p_aout, "out of memory" );
85         return -1;
86     }
87
88     p_aout->output.p_sys = p_sys;
89
90     p_aout->output.pf_play = Play;
91     aout_VolumeSoftInit( p_aout );
92
93     /* Initialize some variables */
94     p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
95     p_sys->esd_format &= ~ESD_MASK_CHAN;
96
97     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
98     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
99     if ( i_nb_channels > 2 )
100     {
101         /* EsounD doesn't support more than two channels. */
102         i_nb_channels = 2;
103         p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
104     }
105
106     switch( i_nb_channels )
107     {
108     case 1:
109         p_sys->esd_format |= ESD_MONO;
110         break;
111     case 2:
112         p_sys->esd_format |= ESD_STEREO;
113         break;
114     }
115
116     /* open a socket for playing a stream
117      * and try to open /dev/dsp if there's no EsounD */
118     p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
119                               p_aout->output.output.i_rate, NULL, "vlc" );
120     if( p_sys->i_fd < 0 )
121     {
122         msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %ld Hz)",
123                          p_sys->esd_format, p_aout->output.output.i_rate );
124         free( p_sys );
125         return -1;
126     }
127
128     p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
129
130     /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
131      * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
132     p_sys->latency =
133         (mtime_t)( esd_get_latency( esd_open_sound(NULL) ) + ESD_BUF_SIZE/2
134                     * p_aout->output.output.i_bytes_per_frame
135                     * p_aout->output.output.i_rate
136                     / ESD_DEFAULT_RATE )
137       * (mtime_t)1000000
138       / p_aout->output.output.i_bytes_per_frame
139       / p_aout->output.output.i_rate;
140
141     /* Create ESD thread and wait for its readiness. */
142     if( vlc_thread_create( p_aout, "aout", ESDThread,
143                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
144     {
145         msg_Err( p_aout, "cannot create ESD thread (%s)", strerror(errno) );
146         close( p_sys->i_fd );
147         free( p_sys );
148         return -1;
149     }
150
151     return 0;
152 }
153
154 /*****************************************************************************
155  * Play: nothing to do
156  *****************************************************************************/
157 static void Play( aout_instance_t *p_aout )
158 {
159 }
160
161 /*****************************************************************************
162  * Close: close the Esound socket
163  *****************************************************************************/
164 static void Close( vlc_object_t *p_this )
165 {
166     aout_instance_t *p_aout = (aout_instance_t *)p_this;
167     struct aout_sys_t * p_sys = p_aout->output.p_sys;
168
169     p_aout->b_die = 1;
170     vlc_thread_join( p_aout );
171
172     close( p_sys->i_fd );
173     free( p_sys );
174 }
175
176 /*****************************************************************************
177  * ESDThread: asynchronous thread used to DMA the data to the device
178  *****************************************************************************/
179 static int ESDThread( aout_instance_t * p_aout )
180 {
181     struct aout_sys_t * p_sys = p_aout->output.p_sys;
182
183     while ( !p_aout->b_die )
184     {
185         aout_buffer_t * p_buffer;
186         int i_tmp, i_size;
187         byte_t * p_bytes;
188
189         /* Get the presentation date of the next write() operation. It
190          * is equal to the current date + buffered samples + esd latency */
191         p_buffer = aout_OutputNextBuffer( p_aout, mdate() + p_sys->latency,
192                                                   VLC_FALSE );
193
194         if ( p_buffer != NULL )
195         {
196             p_bytes = p_buffer->p_buffer;
197             i_size = p_buffer->i_nb_bytes;
198         }
199         else
200         {
201             i_size = ESD_BUF_SIZE * 2
202                       / p_aout->output.output.i_frame_length
203                       * p_aout->output.output.i_bytes_per_frame;
204             p_bytes = alloca( i_size );
205             memset( p_bytes, 0, i_size );
206         }
207
208         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
209
210         if( i_tmp < 0 )
211         {
212             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
213         }
214
215         if ( p_buffer != NULL )
216         {
217             aout_BufferFree( p_buffer );
218         }
219     }
220
221     return 0;
222 }
223
224 #if 0
225 /*****************************************************************************
226  * Play: play a sound samples buffer
227  *****************************************************************************
228  * This function writes a buffer of i_length bytes in the socket
229  *****************************************************************************/
230 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
231 {
232     int i_amount;
233
234     int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
235     int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
236
237     i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
238
239     write( p_aout->output.p_sys->i_fd, buffer, i_size );
240 }
241 #endif
242