aout_esd.c 7.89 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * aout_esd.c : Esound functions library
 *****************************************************************************
 * Copyright (C) 2000 VideoLAN
 *
6
 * Authors: Samuel Hocevar <sam@zoy.org>
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
 *
 * 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"

54 55
#include "modules.h"

56 57 58 59 60 61 62 63 64 65 66 67 68
/*****************************************************************************
 * 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;

/*****************************************************************************
69
 * Local prototypes.
70
 *****************************************************************************/
71 72 73 74 75 76 77 78 79 80 81 82
static int     aout_Probe       ( probedata_t *p_data );
static int     aout_Open        ( aout_thread_t *p_aout );
static int     aout_SetFormat   ( aout_thread_t *p_aout );
static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
static void    aout_Play        ( aout_thread_t *p_aout,
                                  byte_t *buffer, int i_size );
static void    aout_Close       ( aout_thread_t *p_aout );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
83
void aout_getfunctions( function_list_t * p_function_list )
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
    p_function_list->p_probe = aout_Probe;
    p_function_list->functions.aout.p_open = aout_Open;
    p_function_list->functions.aout.p_setformat = aout_SetFormat;
    p_function_list->functions.aout.p_getbufinfo = aout_GetBufInfo;
    p_function_list->functions.aout.p_play = aout_Play;
    p_function_list->functions.aout.p_close = aout_Close;
}

/*****************************************************************************
 * aout_Probe: probes the audio device and return a score
 *****************************************************************************
 * This function tries to open the dps and returns a score to the plugin
 * manager so that it can 
 *****************************************************************************/
static int aout_Probe( probedata_t *p_data )
{
    /* We don't have to test anything -- if we managed to open this plugin,
     * it means we have the appropriate libs. */
    return( 50 );
}

/*****************************************************************************
 * aout_Open: open an esd socket
 *****************************************************************************/
static int aout_Open( aout_thread_t *p_aout )
110 111 112 113 114 115 116 117 118 119
{
    /* 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 )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
120
        intf_ErrMsg("error: %s", strerror(ENOMEM) );
121 122 123 124
        return( 1 );
    }

    /* Initialize some variables */
125
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    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"
Sam Hocevar's avatar
 
Sam Hocevar committed
146
                     " (format 0x%08x at %ld Hz)",
147 148 149 150 151 152 153 154
                     p_aout->p_sys->esd_format, p_aout->l_rate );
        return( -1 );
    }

    return( 0 );
}

/*****************************************************************************
155
 * aout_SetFormat: set the output format
156
 *****************************************************************************/
157
static int aout_SetFormat( aout_thread_t *p_aout )
158 159 160 161 162
{
    return( 0 );
}

/*****************************************************************************
163
 * aout_GetBufInfo: buffer status query
164
 *****************************************************************************/
165
long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
166 167 168 169 170 171
{
    /* arbitrary value that should be changed */
    return( l_buffer_limit );
}

/*****************************************************************************
172
 * aout_Play: play a sound samples buffer
173
 *****************************************************************************
174
 * This function writes a buffer of i_length bytes in the socket
175
 *****************************************************************************/
176
void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
{
    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;
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
195
    intf_DbgMsg( "aout: latency is %i", amount );
196 197 198 199 200

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

/*****************************************************************************
201
 * aout_Close: close the Esound socket
202
 *****************************************************************************/
203
void aout_Close( aout_thread_t *p_aout )
204 205 206 207
{
    close( p_aout->i_fd );
}