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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*****************************************************************************
* aout_qnx.c : QNX audio output
*****************************************************************************
* Copyright (C) 2000, 2001 VideoLAN
*
* Authors: Henri Fallon <henri@videolan.org>
* Jon Lech Johansen <jon-vl@nanocrew.net>
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <errno.h> /* ENOMEM */
#include <string.h> /* strerror() */
#include <stdlib.h> /* calloc(), malloc(), free() */
#include <vlc/vlc.h>
#include <vlc/aout.h>
#include <sys/asoundlib.h>
struct aout_sys_t
{
snd_pcm_t * p_pcm_handle;
int i_card;
int i_device;
};
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int SetFormat ( aout_thread_t * );
static int GetBufInfo ( aout_thread_t *, int );
static void Play ( aout_thread_t *, byte_t *, int );
/*****************************************************************************
* Open : creates a handle and opens an alsa device
*****************************************************************************
* This function opens an alsa device, through the alsa API
*****************************************************************************/
int E_(OpenAudio)( vlc_object_t *p_this )
{
aout_thread_t *p_aout = (aout_thread_t *)p_this;
int i_ret;
/* allocate structure */
p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
if( p_aout->p_sys == NULL )
{
msg_Err( p_aout, "out of memory" );
return( 1 );
}
/* open audio device */
if( ( i_ret = snd_pcm_open_preferred( &p_aout->p_sys->p_pcm_handle,
&p_aout->p_sys->i_card,
&p_aout->p_sys->i_device,
SND_PCM_OPEN_PLAYBACK ) ) < 0 )
{
msg_Err( p_aout, "unable to open audio device (%s)",
snd_strerror( i_ret ) );
free( p_aout->p_sys );
return( 1 );
}
/* disable mmap */
if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->p_sys->p_pcm_handle,
PLUGIN_DISABLE_MMAP ) ) < 0 )
{
msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
Close( p_this );
free( p_aout->p_sys );
return( 1 );
}
p_aout->pf_setformat = SetFormat;
p_aout->pf_getbufinfo = GetBufInfo;
p_aout->pf_play = Play;
return( 0 );
}
/*****************************************************************************
* SetFormat : set the audio output format
*****************************************************************************
* This function prepares the device, sets the rate, format, the mode
* ("play as soon as you have data"), and buffer information.
*****************************************************************************/
static int SetFormat( aout_thread_t *p_aout )
{
int i_ret;
int i_bytes_per_sample;
snd_pcm_channel_info_t pi;
snd_pcm_channel_params_t pp;
memset( &pi, 0, sizeof(pi) );
memset( &pp, 0, sizeof(pp) );
pi.channel = SND_PCM_CHANNEL_PLAYBACK;
if( ( i_ret = snd_pcm_plugin_info( p_aout->p_sys->p_pcm_handle,
&pi ) ) < 0 )
{
msg_Err( p_aout, "unable to get plugin info (%s)",
snd_strerror( i_ret ) );
return( 1 );
}
pp.mode = SND_PCM_MODE_BLOCK;
pp.channel = SND_PCM_CHANNEL_PLAYBACK;
pp.start_mode = SND_PCM_START_FULL;
pp.stop_mode = SND_PCM_STOP_STOP;
pp.buf.block.frags_max = 1;
pp.buf.block.frags_min = 1;
pp.format.interleave = 1;
pp.format.rate = p_aout->i_rate;
pp.format.voices = p_aout->i_channels;
switch( p_aout->i_format )
{
case AOUT_FMT_S16_LE:
pp.format.format = SND_PCM_SFMT_S16_LE;
i_bytes_per_sample = 2;
break;
default:
pp.format.format = SND_PCM_SFMT_S16_BE;
i_bytes_per_sample = 2;
break;
}
pp.buf.block.frag_size =
(((s64)p_aout->i_rate * AOUT_BUFFER_DURATION) / 1000000) *
p_aout->i_channels * i_bytes_per_sample;
/* set parameters */
if( ( i_ret = snd_pcm_plugin_params( p_aout->p_sys->p_pcm_handle,
&pp ) ) < 0 )
{
msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
return( 1 );
}
/* prepare channel */
if( ( i_ret = snd_pcm_plugin_prepare( p_aout->p_sys->p_pcm_handle,
SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
{
msg_Err( p_aout, "unable to prepare channel (%s)",
snd_strerror( i_ret ) );
return( 1 );
}
return( 0 );
}
/*****************************************************************************
* GetBufInfo: buffer status query
*****************************************************************************
* This function returns the number of used byte in the queue.
* It also deals with errors : indeed if the device comes to run out
* of data to play, it switches to the "underrun" status. It has to
* be flushed and re-prepared
*****************************************************************************/
static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
{
int i_ret;
snd_pcm_channel_status_t status;
/* get current pcm status */
memset( &status, 0, sizeof(status) );
if( ( i_ret = snd_pcm_plugin_status( p_aout->p_sys->p_pcm_handle,
&status ) ) < 0 )
{
msg_Err( p_aout, "unable to get device status (%s)",
snd_strerror( i_ret ) );
return( -1 );
}
/* check for underrun */
switch( status.status )
{
case SND_PCM_STATUS_READY:
case SND_PCM_STATUS_UNDERRUN:
if( ( i_ret = snd_pcm_plugin_prepare( p_aout->p_sys->p_pcm_handle,
SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
{
msg_Err( p_aout, "unable to prepare channel (%s)",
snd_strerror( i_ret ) );
}
break;
}
return( status.count );
}
/*****************************************************************************
* Play : plays a sample
*****************************************************************************
* Plays a sample using the snd_pcm_write function from the alsa API
*****************************************************************************/
static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
{
int i_ret;
if( ( i_ret = snd_pcm_plugin_write( p_aout->p_sys->p_pcm_handle,
(void *) buffer,
(size_t) i_size ) ) <= 0 )
{
msg_Err( p_aout, "unable to write data (%s)", snd_strerror(i_ret) );
}
}
/*****************************************************************************
* CloseAudio: close the audio device
*****************************************************************************/
static void E_(CloseAudio) ( vlc_object_t *p_this )
{
aout_thread_t *p_aout = (aout_thread_t *)p_this;
int i_ret;
if( ( i_ret = snd_pcm_close( p_aout->p_sys->p_pcm_handle ) ) < 0 )
{
msg_Err( p_aout, "unable to close audio device (%s)",
snd_strerror( i_ret ) );
}
free( p_aout->p_sys );
}