aout_esd.c 6.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*****************************************************************************
 * aout_esd.c : Esound functions library
 *****************************************************************************
 * Copyright (C) 2000 VideoLAN
 *
 * Authors:
 *
 * 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.
 * 
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/* TODO:
 *
 * - use the libesd function to get latency when it's not buggy anymore
 *
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <fcntl.h>                                       /* open(), O_WRONLY */
#include <string.h>                                            /* strerror() */
#include <unistd.h>                                      /* write(), close() */
#include <stdio.h>                                           /* "intf_msg.h" */
#include <stdlib.h>                            /* calloc(), malloc(), free() */

#include <esd.h>

#include "config.h"
#include "common.h"                                     /* boolean_t, byte_t */
#include "threads.h"
#include "mtime.h"
#include "plugins.h"

#include "audio_output.h"                                   /* aout_thread_t */

#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
#include "main.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.
 *****************************************************************************/
typedef struct aout_sys_s
{
    esd_format_t esd_format;

} aout_sys_t;

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
67
 * aout_EsdOpen: opens an esd socket
68
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
69
int aout_EsdOpen( aout_thread_t *p_aout )
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
    /* mpg123 does it this way */
    int i_bits = ESD_BITS16;
    int i_mode = ESD_STREAM;
    int i_func = ESD_PLAY;

    /* Allocate structure */
    p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
        return( 1 );
    }

    /* Initialize some variables */
85
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
    p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );

    i_bits = ESD_BITS16;
    i_mode = ESD_STREAM;
    i_func = ESD_PLAY;
    p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);

    if( p_aout->i_channels == 1 )
        p_aout->p_sys->esd_format |= ESD_MONO;
    else
        p_aout->p_sys->esd_format |= ESD_STEREO;

    /* open a socket for playing a stream
     * and try to open /dev/dsp if there's no EsounD */
    if ( (p_aout->i_fd
            = esd_play_stream_fallback(p_aout->p_sys->esd_format,
                p_aout->l_rate, NULL, "vlc")) < 0 )
    {
        intf_ErrMsg( "aout error: can't open esound socket"
                     " (format 0x%08x at %ld Hz)\n",
                     p_aout->p_sys->esd_format, p_aout->l_rate );
        return( -1 );
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
115
 * aout_EsdReset: resets the dsp
116
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
117
int aout_EsdReset( aout_thread_t *p_aout )
118 119 120 121 122
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
123
 * aout_EsdSetFormat: sets the dsp output format
124
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
125
int aout_EsdSetFormat( aout_thread_t *p_aout )
126 127 128 129 130
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
131
 * aout_EsdSetChannels: sets the dsp's stereo or mono mode
132
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
133
int aout_EsdSetChannels( aout_thread_t *p_aout )
134 135 136 137 138
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
139
 * aout_EsdSetRate: sets the dsp's audio output rate
140
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
141
int aout_EsdSetRate( aout_thread_t *p_aout )
142 143 144 145 146
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
147
 * aout_EsdGetBufInfo: buffer status query
148
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
149
long aout_EsdGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
150 151 152 153 154 155
{
    /* arbitrary value that should be changed */
    return( l_buffer_limit );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
156
 * aout_EsdPlaySamples: plays a sound samples buffer
157 158 159
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
160
void aout_EsdPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
{
    int amount;

    if (p_aout->p_sys->esd_format & ESD_STEREO)
    {
        if (p_aout->p_sys->esd_format & ESD_BITS16)
            amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
        else
            amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
    }
    else
    {
        if (p_aout->p_sys->esd_format & ESD_BITS16)
            amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
        else
            amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
    }

    intf_DbgMsg( "aout: latency is %i\n", amount );

    write( p_aout->i_fd, buffer, i_size );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
185
 * aout_EsdClose: closes the dsp audio device
186
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
187
void aout_EsdClose( aout_thread_t *p_aout )
188 189 190 191
{
    close( p_aout->i_fd );
}