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 */
34 #include <vlc_common.h>
42 #include <sys/asoundlib.h>
46 snd_pcm_t * p_pcm_handle;
50 uint8_t * p_silent_buffer;
53 #define DEFAULT_FRAME_SIZE 2048
55 /*****************************************************************************
57 *****************************************************************************/
58 int OpenAudio ( vlc_object_t *p_this );
59 void CloseAudio ( vlc_object_t *p_this );
60 static int GetBufInfo ( aout_instance_t * );
61 static void Play ( aout_instance_t * );
62 static void* QNXaoutThread ( vlc_object_t * );
64 /*****************************************************************************
65 * Open : creates a handle and opens an alsa device
66 *****************************************************************************
67 * This function opens an alsa device, through the alsa API
68 *****************************************************************************/
69 int OpenAudio( vlc_object_t *p_this )
71 aout_instance_t *p_aout = (aout_instance_t *)p_this;
73 int i_bytes_per_sample;
75 snd_pcm_channel_info_t pi;
76 snd_pcm_channel_params_t pp;
77 aout_instance_t *p_aout = (aout_instance_t *)p_this;
79 /* allocate structure */
80 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
81 if( p_aout->output.p_sys == NULL )
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 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 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 = VLC_CODEC_S16N;
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 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 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 ) )
178 msg_Err( p_aout, "cannot create QNX audio thread (%m)" );
179 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 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 void* QNXaoutThread( vlc_object_t *p_this )
263 aout_instance_t * p_aout = (aout_instance_t*)p_this;
264 struct aout_sys_t * p_sys = p_aout->output.p_sys;
265 int canc = vlc_savecancel ();
267 while ( vlc_object_alive (p_aout) )
269 aout_buffer_t * p_buffer;
273 if ( p_aout->output.output.i_format != VLC_CODEC_SPDIFL )
275 mtime_t next_date = 0;
277 /* Get the presentation date of the next write() operation. It
278 * is equal to the current date + duration of buffered samples.
279 * Order is important here, since GetBufInfo is believed to take
280 * more time than mdate(). */
281 next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
282 / p_aout->output.output.i_bytes_per_frame
283 / p_aout->output.output.i_rate
284 * p_aout->output.output.i_frame_length;
285 next_date += mdate();
287 p_buffer = aout_OutputNextBuffer( p_aout, next_date, false );
291 p_buffer = aout_OutputNextBuffer( p_aout, 0, true );
294 if ( p_buffer != NULL )
296 p_bytes = p_buffer->p_buffer;
297 i_size = p_buffer->i_nb_bytes;
301 i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
302 * p_aout->output.output.i_bytes_per_frame;
303 p_bytes = p_aout->output.p_sys->p_silent_buffer;
304 memset( p_bytes, 0, i_size );
307 i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
313 msg_Err( p_aout, "write failed (%m)" );
316 if ( p_buffer != NULL )
318 aout_BufferFree( p_buffer );
322 vlc_restorecancel (canc);