~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to plugins/esd/aout_esd.c

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************************
2
 
 * aout_esd.c : Esound functions library
3
 
 *****************************************************************************
4
 
 * Copyright (C) 2000-2001 VideoLAN
5
 
 * $Id: aout_esd.c,v 1.16 2001/12/07 18:33:07 sam 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
 
#define MODULE_NAME esd
25
 
#include "modules_inner.h"
26
 
 
27
 
/* TODO:
28
 
 *
29
 
 * - use the libesd function to get latency when it's not buggy anymore
30
 
 *
31
 
 */
32
 
 
33
 
/*****************************************************************************
34
 
 * Preamble
35
 
 *****************************************************************************/
36
 
#include "defs.h"
37
 
 
38
 
#include <errno.h>                                                 /* ENOMEM */
39
 
#include <fcntl.h>                                       /* open(), O_WRONLY */
40
 
#include <string.h>                                            /* strerror() */
41
 
#include <unistd.h>                                      /* write(), close() */
42
 
#include <stdio.h>                                           /* "intf_msg.h" */
43
 
#include <stdlib.h>                            /* calloc(), malloc(), free() */
44
 
 
45
 
#include <esd.h>
46
 
 
47
 
#include "common.h"                                     /* boolean_t, byte_t */
48
 
#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
49
 
#include "threads.h"
50
 
#include "mtime.h"
51
 
#include "tests.h"
52
 
 
53
 
#include "audio_output.h"                                   /* aout_thread_t */
54
 
 
55
 
#include "modules.h"
56
 
#include "modules_export.h"
57
 
 
58
 
/*****************************************************************************
59
 
 * aout_sys_t: esd audio output method descriptor
60
 
 *****************************************************************************
61
 
 * This structure is part of the audio output thread descriptor.
62
 
 * It describes some esd specific variables.
63
 
 *****************************************************************************/
64
 
typedef struct aout_sys_s
65
 
{
66
 
    esd_format_t esd_format;
67
 
 
68
 
} aout_sys_t;
69
 
 
70
 
/*****************************************************************************
71
 
 * Local prototypes.
72
 
 *****************************************************************************/
73
 
static int     aout_Probe       ( probedata_t *p_data );
74
 
static int     aout_Open        ( aout_thread_t *p_aout );
75
 
static int     aout_SetFormat   ( aout_thread_t *p_aout );
76
 
static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
77
 
static void    aout_Play        ( aout_thread_t *p_aout,
78
 
                                  byte_t *buffer, int i_size );
79
 
static void    aout_Close       ( aout_thread_t *p_aout );
80
 
 
81
 
/*****************************************************************************
82
 
 * Functions exported as capabilities. They are declared as static so that
83
 
 * we don't pollute the namespace too much.
84
 
 *****************************************************************************/
85
 
void _M( aout_getfunctions )( function_list_t * p_function_list )
86
 
{
87
 
    p_function_list->pf_probe = aout_Probe;
88
 
    p_function_list->functions.aout.pf_open = aout_Open;
89
 
    p_function_list->functions.aout.pf_setformat = aout_SetFormat;
90
 
    p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
91
 
    p_function_list->functions.aout.pf_play = aout_Play;
92
 
    p_function_list->functions.aout.pf_close = aout_Close;
93
 
}
94
 
 
95
 
/*****************************************************************************
96
 
 * aout_Probe: probes the audio device and return a score
97
 
 *****************************************************************************
98
 
 * This function tries to open the dps and returns a score to the plugin
99
 
 * manager so that it can 
100
 
 *****************************************************************************/
101
 
static int aout_Probe( probedata_t *p_data )
102
 
{
103
 
    if( TestMethod( AOUT_METHOD_VAR, "esd" ) )
104
 
    {
105
 
        return( 999 );
106
 
    }
107
 
 
108
 
    /* We don't have to test anything -- if we managed to open this plugin,
109
 
     * it means we have the appropriate libs. */
110
 
    return( 50 );
111
 
}
112
 
 
113
 
/*****************************************************************************
114
 
 * aout_Open: open an esd socket
115
 
 *****************************************************************************/
116
 
static int aout_Open( aout_thread_t *p_aout )
117
 
{
118
 
    /* mpg123 does it this way */
119
 
    int i_bits = ESD_BITS16;
120
 
    int i_mode = ESD_STREAM;
121
 
    int i_func = ESD_PLAY;
122
 
 
123
 
    /* Allocate structure */
124
 
    p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
125
 
    if( p_aout->p_sys == NULL )
126
 
    {
127
 
        intf_ErrMsg("error: %s", strerror(ENOMEM) );
128
 
        return( 1 );
129
 
    }
130
 
 
131
 
    /* Initialize some variables */
132
 
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
133
 
    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
134
 
    p_aout->l_rate = esd_audio_rate; /* We use actual esd rate value, not AOUT_RATE_DEFAULT */
135
 
 
136
 
    i_bits = ESD_BITS16;
137
 
    i_mode = ESD_STREAM;
138
 
    i_func = ESD_PLAY;
139
 
    p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
140
 
 
141
 
    if( p_aout->i_channels == 1 )
142
 
    {
143
 
        p_aout->p_sys->esd_format |= ESD_MONO;
144
 
    }
145
 
    else
146
 
    {
147
 
        p_aout->p_sys->esd_format |= ESD_STEREO;
148
 
    }
149
 
 
150
 
    /* open a socket for playing a stream
151
 
     * and try to open /dev/dsp if there's no EsounD */
152
 
    if ( (p_aout->i_fd
153
 
            = esd_play_stream_fallback(p_aout->p_sys->esd_format,
154
 
                p_aout->l_rate, NULL, "vlc")) < 0 )
155
 
    {
156
 
        intf_ErrMsg( "aout error: can't open esound socket"
157
 
                     " (format 0x%08x at %ld Hz)",
158
 
                     p_aout->p_sys->esd_format, p_aout->l_rate );
159
 
        return( -1 );
160
 
    }
161
 
 
162
 
    return( 0 );
163
 
}
164
 
 
165
 
/*****************************************************************************
166
 
 * aout_SetFormat: set the output format
167
 
 *****************************************************************************/
168
 
static int aout_SetFormat( aout_thread_t *p_aout )
169
 
{
170
 
    int i_fd;
171
 
 
172
 
    i_fd = esd_open_sound(NULL);
173
 
    p_aout->i_latency = esd_get_latency(i_fd);
174
 
   
175
 
    intf_WarnMsg(2, "aout_esd_latency: %d",p_aout->i_latency);
176
 
 
177
 
    return( 0 );
178
 
}
179
 
 
180
 
/*****************************************************************************
181
 
 * aout_GetBufInfo: buffer status query
182
 
 *****************************************************************************/
183
 
static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
184
 
{
185
 
    /* arbitrary value that should be changed */
186
 
    return( l_buffer_limit );
187
 
}
188
 
 
189
 
/*****************************************************************************
190
 
 * aout_Play: play a sound samples buffer
191
 
 *****************************************************************************
192
 
 * This function writes a buffer of i_length bytes in the socket
193
 
 *****************************************************************************/
194
 
static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
195
 
{
196
 
    int i_amount;
197
 
    
198
 
    if (p_aout->p_sys->esd_format & ESD_STEREO)
199
 
    {
200
 
        if (p_aout->p_sys->esd_format & ESD_BITS16)
201
 
        {
202
 
            i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
203
 
        }
204
 
        else
205
 
        {
206
 
            i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
207
 
        }
208
 
    }
209
 
    else
210
 
    {
211
 
        if (p_aout->p_sys->esd_format & ESD_BITS16)
212
 
        {
213
 
            i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
214
 
        }
215
 
        else
216
 
        {
217
 
            i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
218
 
        }
219
 
    }
220
 
 
221
 
    write( p_aout->i_fd, buffer, i_size );
222
 
}
223
 
 
224
 
/*****************************************************************************
225
 
 * aout_Close: close the Esound socket
226
 
 *****************************************************************************/
227
 
static void aout_Close( aout_thread_t *p_aout )
228
 
{
229
 
    close( p_aout->i_fd );
230
 
}
231