1 /*****************************************************************************
2 * aout.c : QNX audio output
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 the VideoLAN team
6 * Authors: Henri Fallon <henri@videolan.org>
7 * Jon Lech Johansen <jon-vl@nanocrew.net>
8 * Pascal Levesque <pascal.levesque@mindready.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
39 #include <sys/asoundlib.h>
43 snd_pcm_t * p_pcm_handle;
47 byte_t * p_silent_buffer;
50 #define DEFAULT_FRAME_SIZE 2048
52 /*****************************************************************************
54 *****************************************************************************/
55 int E_(OpenAudio) ( vlc_object_t *p_this );
56 void E_(CloseAudio) ( vlc_object_t *p_this );
57 static int GetBufInfo ( aout_instance_t * );
58 static void Play ( aout_instance_t * );
59 static int QNXaoutThread ( aout_instance_t * );
61 /*****************************************************************************
62 * Open : creates a handle and opens an alsa device
63 *****************************************************************************
64 * This function opens an alsa device, through the alsa API
65 *****************************************************************************/
66 int E_(OpenAudio)( vlc_object_t *p_this )
68 aout_instance_t *p_aout = (aout_instance_t *)p_this;
70 int i_bytes_per_sample;
72 snd_pcm_channel_info_t pi;
73 snd_pcm_channel_params_t pp;
74 aout_instance_t *p_aout = (aout_instance_t *)p_this;
76 /* allocate structure */
77 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
78 if( p_aout->output.p_sys == NULL )
80 msg_Err( p_aout, "out of memory" );
84 /* open audio device */
85 if( ( i_ret = snd_pcm_open_preferred( &p_aout->output.p_sys->p_pcm_handle,
86 &p_aout->output.p_sys->i_card,
87 &p_aout->output.p_sys->i_device,
88 SND_PCM_OPEN_PLAYBACK ) ) < 0 )
90 msg_Err( p_aout, "unable to open audio device (%s)",
91 snd_strerror( i_ret ) );
92 free( p_aout->output.p_sys );
97 if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
98 PLUGIN_DISABLE_MMAP ) ) < 0 )
100 msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
101 E_(CloseAudio)( p_this );
102 free( p_aout->output.p_sys );
106 p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
107 p_aout->output.pf_play = Play;
108 aout_VolumeSoftInit( p_aout );
110 memset( &pi, 0, sizeof(pi) );
111 memset( &pp, 0, sizeof(pp) );
113 pi.channel = SND_PCM_CHANNEL_PLAYBACK;
114 if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
117 msg_Err( p_aout, "unable to get plugin info (%s)",
118 snd_strerror( i_ret ) );
119 E_(CloseAudio)( p_this );
120 free( p_aout->output.p_sys );
124 pp.mode = SND_PCM_MODE_BLOCK;
125 pp.channel = SND_PCM_CHANNEL_PLAYBACK;
126 pp.start_mode = SND_PCM_START_FULL;
127 pp.stop_mode = SND_PCM_STOP_STOP;
129 pp.buf.block.frags_max = 3;
130 pp.buf.block.frags_min = 1;
132 pp.format.interleave = 1;
133 pp.format.rate = p_aout->output.output.i_rate;
135 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
136 if ( i_nb_channels > 2 )
138 /* I don't know if QNX supports more than two channels. */
140 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
142 pp.format.voices = i_nb_channels;
144 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
145 p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
146 pp.format.format = SND_PCM_SFMT_S16;
147 i_bytes_per_sample = 2;
149 pp.buf.block.frag_size = p_aout->output.i_nb_samples *
150 p_aout->output.output.i_channels *
154 if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
157 msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
158 E_(CloseAudio)( p_this );
159 free( p_aout->output.p_sys );
163 /* prepare channel */
164 if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
165 SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
167 msg_Err( p_aout, "unable to prepare channel (%s)",
168 snd_strerror( i_ret ) );
169 E_(CloseAudio)( p_this );
170 free( p_aout->output.p_sys );
174 /* Create audio thread and wait for its readiness. */
175 if( vlc_thread_create( p_aout, "aout", QNXaoutThread,
176 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
178 msg_Err( p_aout, "cannot create QNX audio thread (%s)", strerror(errno) );
179 E_(CloseAudio)( p_this );
180 free( p_aout->output.p_sys );
187 /*****************************************************************************
188 * GetBufInfo: buffer status query
189 *****************************************************************************
190 * This function returns the number of used byte in the queue.
191 * It also deals with errors : indeed if the device comes to run out
192 * of data to play, it switches to the "underrun" status. It has to
193 * be flushed and re-prepared
194 *****************************************************************************/
195 static int GetBufInfo( aout_instance_t *p_aout )
198 snd_pcm_channel_status_t status;
200 /* get current pcm status */
201 memset( &status, 0, sizeof(status) );
202 if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
205 msg_Err( p_aout, "unable to get device status (%s)",
206 snd_strerror( i_ret ) );
210 /* check for underrun */
211 switch( status.status )
213 case SND_PCM_STATUS_READY:
214 case SND_PCM_STATUS_UNDERRUN:
215 if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
216 SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
218 msg_Err( p_aout, "unable to prepare channel (%s)",
219 snd_strerror( i_ret ) );
224 return( status.count );
227 /*****************************************************************************
228 * Play : plays a sample
229 *****************************************************************************
230 * Plays a sample using the snd_pcm_write function from the alsa API
231 *****************************************************************************/
232 static void Play( aout_instance_t *p_aout )
236 /*****************************************************************************
237 * CloseAudio: close the audio device
238 *****************************************************************************/
239 void E_(CloseAudio) ( vlc_object_t *p_this )
241 aout_instance_t *p_aout = (aout_instance_t *)p_this;
244 vlc_object_kill( p_aout );
245 vlc_thread_join( p_aout );
247 if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
249 msg_Err( p_aout, "unable to close audio device (%s)",
250 snd_strerror( i_ret ) );
253 free( p_aout->output.p_sys->p_silent_buffer );
254 free( p_aout->output.p_sys );
258 /*****************************************************************************
259 * QNXaoutThread: asynchronous thread used to DMA the data to the device
260 *****************************************************************************/
261 static int QNXaoutThread( aout_instance_t * p_aout )
263 struct aout_sys_t * p_sys = p_aout->output.p_sys;
265 while ( !p_aout->b_die )
267 aout_buffer_t * p_buffer;
271 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
273 mtime_t next_date = 0;
275 /* Get the presentation date of the next write() operation. It
276 * is equal to the current date + duration of buffered samples.
277 * Order is important here, since GetBufInfo is believed to take
278 * more time than mdate(). */
279 next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
280 / p_aout->output.output.i_bytes_per_frame
281 / p_aout->output.output.i_rate
282 * p_aout->output.output.i_frame_length;
283 next_date += mdate();
285 p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
289 p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
292 if ( p_buffer != NULL )
294 p_bytes = p_buffer->p_buffer;
295 i_size = p_buffer->i_nb_bytes;
299 i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
300 * p_aout->output.output.i_bytes_per_frame;
301 p_bytes = p_aout->output.p_sys->p_silent_buffer;
302 memset( p_bytes, 0, i_size );
305 i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
311 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
314 if ( p_buffer != NULL )
316 aout_BufferFree( p_buffer );