esd.c 8.21 KB
Newer Older
1 2 3
/*****************************************************************************
 * esd.c : EsounD module
 *****************************************************************************
4
 * Copyright (C) 2000, 2001 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 *
14 15 16 17 18 19 20
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <unistd.h>                                      /* write(), close() */

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
36
#include <vlc_aout.h>
37

38 39
#include <sys/socket.h>

40 41 42
#include <sys/time.h>
#include <time.h>

43 44 45 46 47 48 49 50 51 52 53 54
#include <esd.h>

/*****************************************************************************
 * aout_sys_t: esd audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes some esd specific variables.
 *****************************************************************************/
struct aout_sys_t
{
    esd_format_t esd_format;
    int          i_fd;
55

56
    mtime_t      latency;       /* unused, but we might do something with it */
57 58 59 60 61 62 63
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );
64
static void Play         ( aout_instance_t * );
65 66 67 68 69

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
70
    set_description( N_("EsounD audio output") );
71
    set_shortname( "EsounD" );
72
    set_capability( "audio output", 50 );
73
    add_string( "esdserver", "", NULL, N_("Esound server"), NULL, false );
zorglub's avatar
zorglub committed
74 75
    set_category( CAT_AUDIO );
    set_subcategory( SUBCAT_AUDIO_AOUT );
76 77 78 79 80 81 82 83 84
    set_callbacks( Open, Close );
    add_shortcut( "esound" );
vlc_module_end();

/*****************************************************************************
 * Open: open an esd socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
85 86
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
    struct aout_sys_t * p_sys;
87
    char * psz_server;
88
    int i_nb_channels;
89
    int i_newfd;
90 91

    /* Allocate structure */
92 93
    p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_sys == NULL )
94
        return VLC_ENOMEM;
95

96
    p_aout->output.p_sys = p_sys;
97

98
    p_aout->output.pf_play = Play;
99
    aout_VolumeSoftInit( p_aout );
100 101 102 103 104

    /* Initialize some variables */
    p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
    p_sys->esd_format &= ~ESD_MASK_CHAN;

105 106 107 108 109 110
    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
    i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
    if ( i_nb_channels > 2 )
    {
        /* EsounD doesn't support more than two channels. */
        i_nb_channels = 2;
gbazin's avatar
 
gbazin committed
111 112
        p_aout->output.output.i_physical_channels =
            AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
113 114 115
    }

    switch( i_nb_channels )
116
    {
117 118 119 120 121 122
    case 1:
        p_sys->esd_format |= ESD_MONO;
        break;
    case 2:
        p_sys->esd_format |= ESD_STEREO;
        break;
123 124
    }

125 126
    /* Force the rate, otherwise the sound is very noisy */
    p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
127
    p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
128

129
    /* Open a socket for playing a stream
130
     * and try to open /dev/dsp if there's no EsounD */
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    psz_server = config_GetPsz( p_aout, "esdserver" );
    if( psz_server && *psz_server )
    {
        p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
                                                p_aout->output.output.i_rate,
                                                psz_server, "vlc" );
    }
    else
    {
        p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
                                                p_aout->output.output.i_rate,
                                                NULL, "vlc" );
    }

145
    if( p_sys->i_fd < 0 )
146
    {
147
        msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
148
                         p_sys->esd_format, p_aout->output.output.i_rate );
149
        free( p_sys );
150
        return VLC_EGENERIC;
151 152
    }

153 154 155 156 157 158 159 160
    if( psz_server && *psz_server )
    {
        struct timeval start, stop;
        esd_server_info_t * p_info;

        gettimeofday( &start, NULL );
        p_info = esd_get_server_info( p_sys->i_fd );
        gettimeofday( &stop, NULL );
161

162 163 164 165 166 167 168 169
        p_sys->latency = (mtime_t)( stop.tv_sec - start.tv_sec )
                           * (mtime_t)1000000;
        p_sys->latency += stop.tv_usec - start.tv_usec;
    }
    else
    {
        p_sys->latency = 0;
    }
170

171 172
    /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
     * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
173 174 175 176 177 178
    p_sys->latency +=
        (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) )
                    + ESD_BUF_SIZE / 2
                      * p_aout->output.output.i_bytes_per_frame
                      * p_aout->output.output.i_rate
                      / ESD_DEFAULT_RATE )
179
      * (mtime_t)1000000
180
      / p_aout->output.output.i_bytes_per_frame
181
      / p_aout->output.output.i_rate;
182

183
    close( i_newfd );
184
    return VLC_SUCCESS;
185 186 187
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
188
 * Play: nothing to do
189
 *****************************************************************************/
190
static void Play( aout_instance_t *p_aout )
191
{
192
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
193 194
    aout_buffer_t * p_buffer;
    int i_tmp;
195

196
    p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
197

198
    if ( p_buffer != NULL )
199
    {
200 201
        unsigned int pos;
        unsigned char *data = p_buffer->p_buffer;
202

203
        for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
204
             pos += ESD_BUF_SIZE )
205
        {
206 207 208
            i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
            if( i_tmp < 0 )
            {
209
                msg_Err( p_aout, "write failed (%m)" );
210
            }
211 212 213
        }
        aout_BufferFree( p_buffer );
    }
214 215
}

216
/*****************************************************************************
217
 * Close: close the Esound socket
218
 *****************************************************************************/
219
static void Close( vlc_object_t *p_this )
220
{
221
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
222 223
    struct aout_sys_t * p_sys = p_aout->output.p_sys;

224 225
    close( p_sys->i_fd );
    free( p_sys );
226 227
}

228
#if 0
229
/*****************************************************************************
230 231 232
 * Play: play a sound samples buffer
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the socket
233
 *****************************************************************************/
234
static void Play( aout_thread_t *p_aout, uint8_t *buffer, int i_size )
235
{
236 237 238 239 240 241
    int i_amount;

    int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
    int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;

    i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
242

243
    write( p_aout->output.p_sys->i_fd, buffer, i_size );
244
}
245
#endif
246