1 /*****************************************************************************
2 * aout.c : QNX audio output
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <errno.h> /* ENOMEM */
29 #include <string.h> /* strerror() */
30 #include <stdlib.h> /* calloc(), malloc(), free() */
39 #include "aout_internal.h"
41 #include <sys/asoundlib.h>
45 snd_pcm_t * p_pcm_handle;
49 byte_t * p_silent_buffer;
50 vlc_bool_t b_initialized;
53 #define DEFAULT_FRAME_SIZE 2048
55 /*****************************************************************************
57 *****************************************************************************/
58 int E_(OpenAudio) ( vlc_object_t *p_this );
59 void E_(CloseAudio) ( vlc_object_t *p_this );
60 static int GetBufInfo ( aout_instance_t * );
61 static int SetFormat ( aout_instance_t * );
62 static void Play ( aout_instance_t * );
63 static int QNXaoutThread ( aout_instance_t * );
65 /*****************************************************************************
66 * Open : creates a handle and opens an alsa device
67 *****************************************************************************
68 * This function opens an alsa device, through the alsa API
69 *****************************************************************************/
70 int E_(OpenAudio)( vlc_object_t *p_this )
72 aout_instance_t *p_aout = (aout_instance_t *)p_this;
75 /* allocate structure */
76 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
77 if( p_aout->output.p_sys == NULL )
79 msg_Err( p_aout, "out of memory" );
83 /* open audio device */
84 if( ( i_ret = snd_pcm_open_preferred( &p_aout->output.p_sys->p_pcm_handle,
85 &p_aout->output.p_sys->i_card,
86 &p_aout->output.p_sys->i_device,
87 SND_PCM_OPEN_PLAYBACK ) ) < 0 )
89 msg_Err( p_aout, "unable to open audio device (%s)",
90 snd_strerror( i_ret ) );
91 free( p_aout->output.p_sys );
96 if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
97 PLUGIN_DISABLE_MMAP ) ) < 0 )
99 msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
100 E_(CloseAudio)( p_this );
101 free( p_aout->output.p_sys );
105 /* Create audio thread and wait for its readiness. */
106 p_aout->output.p_sys->b_initialized = VLC_FALSE;
107 if( vlc_thread_create( p_aout, "aout", QNXaoutThread, VLC_FALSE ) )
109 msg_Err( p_aout, "cannot create QNX audio thread (%s)", strerror(errno) );
110 E_(CloseAudio)( p_this );
111 free( p_aout->output.p_sys );
115 p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
117 p_aout->output.pf_setformat = SetFormat;
118 p_aout->output.pf_play = Play;
123 /*****************************************************************************
124 * SetFormat : set the audio output format
125 *****************************************************************************
126 * This function prepares the device, sets the rate, format, the mode
127 * ("play as soon as you have data"), and buffer information.
128 *****************************************************************************/
129 static int SetFormat( aout_instance_t *p_this )
132 int i_bytes_per_sample;
133 snd_pcm_channel_info_t pi;
134 snd_pcm_channel_params_t pp;
135 aout_instance_t *p_aout = (aout_instance_t *)p_this;
137 p_aout->output.p_sys->b_initialized = VLC_FALSE;
139 memset( &pi, 0, sizeof(pi) );
140 memset( &pp, 0, sizeof(pp) );
142 pi.channel = SND_PCM_CHANNEL_PLAYBACK;
143 if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
146 msg_Err( p_aout, "unable to get plugin info (%s)",
147 snd_strerror( i_ret ) );
151 pp.mode = SND_PCM_MODE_BLOCK;
152 pp.channel = SND_PCM_CHANNEL_PLAYBACK;
153 pp.start_mode = SND_PCM_START_FULL;
154 pp.stop_mode = SND_PCM_STOP_STOP;
156 pp.buf.block.frags_max = 3;
157 pp.buf.block.frags_min = 1;
159 pp.format.interleave = 1;
160 pp.format.rate = p_aout->output.output.i_rate;
161 pp.format.voices = p_aout->output.output.i_channels;
163 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
164 p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
166 switch( p_aout->output.output.i_format )
168 case AOUT_FMT_S16_LE:
169 pp.format.format = SND_PCM_SFMT_S16_LE;
170 i_bytes_per_sample = 2;
174 pp.format.format = SND_PCM_SFMT_S16_BE;
175 i_bytes_per_sample = 2;
179 pp.buf.block.frag_size = p_aout->output.i_nb_samples *
180 p_aout->output.output.i_channels *
184 if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
187 msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
191 /* prepare channel */
192 if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
193 SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
195 msg_Err( p_aout, "unable to prepare channel (%s)",
196 snd_strerror( i_ret ) );
200 p_aout->output.p_sys->b_initialized = VLC_TRUE;
205 /*****************************************************************************
206 * GetBufInfo: buffer status query
207 *****************************************************************************
208 * This function returns the number of used byte in the queue.
209 * It also deals with errors : indeed if the device comes to run out
210 * of data to play, it switches to the "underrun" status. It has to
211 * be flushed and re-prepared
212 *****************************************************************************/
213 static int GetBufInfo( aout_instance_t *p_aout )
216 snd_pcm_channel_status_t status;
218 /* get current pcm status */
219 memset( &status, 0, sizeof(status) );
220 if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
223 msg_Err( p_aout, "unable to get device status (%s)",
224 snd_strerror( i_ret ) );
228 /* check for underrun */
229 switch( status.status )
231 case SND_PCM_STATUS_READY:
232 case SND_PCM_STATUS_UNDERRUN:
233 if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
234 SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
236 msg_Err( p_aout, "unable to prepare channel (%s)",
237 snd_strerror( i_ret ) );
242 return( status.count );
245 /*****************************************************************************
246 * Play : plays a sample
247 *****************************************************************************
248 * Plays a sample using the snd_pcm_write function from the alsa API
249 *****************************************************************************/
250 static void Play( aout_instance_t *p_aout )
254 /*****************************************************************************
255 * CloseAudio: close the audio device
256 *****************************************************************************/
257 void E_(CloseAudio) ( vlc_object_t *p_this )
259 aout_instance_t *p_aout = (aout_instance_t *)p_this;
263 vlc_thread_join( p_aout );
265 if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
267 msg_Err( p_aout, "unable to close audio device (%s)",
268 snd_strerror( i_ret ) );
271 free( p_aout->output.p_sys->p_silent_buffer );
272 free( p_aout->output.p_sys );
276 /*****************************************************************************
277 * QNXaoutThread: asynchronous thread used to DMA the data to the device
278 *****************************************************************************/
279 static int QNXaoutThread( aout_instance_t * p_aout )
281 struct aout_sys_t * p_sys = p_aout->output.p_sys;
283 while ( !p_aout->b_die )
285 aout_buffer_t * p_buffer;
289 if( !p_sys->b_initialized )
291 msleep( THREAD_SLEEP );
295 if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
297 mtime_t next_date = 0;
299 /* Get the presentation date of the next write() operation. It
300 * is equal to the current date + duration of buffered samples.
301 * Order is important here, since GetBufInfo is believed to take
302 * more time than mdate(). */
303 next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
304 / p_aout->output.output.i_bytes_per_frame
305 / p_aout->output.output.i_rate
306 * p_aout->output.output.i_frame_length;
307 next_date += mdate();
309 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0, VLC_FALSE );
313 p_buffer = aout_OutputNextBuffer( p_aout, 0, 0, VLC_TRUE );
316 if ( p_buffer != NULL )
318 p_bytes = p_buffer->p_buffer;
319 i_size = p_buffer->i_nb_bytes;
323 i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
324 * p_aout->output.output.i_bytes_per_frame;
325 p_bytes = p_aout->output.p_sys->p_silent_buffer;
326 memset( p_bytes, 0, i_size );
329 i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
335 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
338 if ( p_buffer != NULL )
340 aout_BufferFree( p_buffer );