]> git.sesse.net Git - vlc/blob - modules/gui/qnx/aout.c
* ./bootstrap : Fixed an issue with old shell versions
[vlc] / modules / gui / qnx / aout.c
1 /*****************************************************************************
2  * aout.c : QNX audio output
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  *
6  * Authors: Henri Fallon <henri@videolan.org>
7  *          Jon Lech Johansen <jon-vl@nanocrew.net>
8  *          Pascal Levesque <pascal.levesque@mindready.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdlib.h>                            /* calloc(), malloc(), free() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef HAVE_ALLOCA_H
35 #   include <alloca.h>
36 #endif
37
38 #include <vlc/aout.h>
39 #include "aout_internal.h"
40
41 #include <sys/asoundlib.h>
42
43 struct aout_sys_t
44 {
45     snd_pcm_t  * p_pcm_handle;
46     int          i_card;
47     int          i_device;
48
49     byte_t *     p_silent_buffer;
50     vlc_bool_t   b_initialized;
51 };
52
53 #define DEFAULT_FRAME_SIZE 2048
54
55 /*****************************************************************************
56  * Local prototypes
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 * );
64
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 )
71 {
72     aout_instance_t *p_aout = (aout_instance_t *)p_this;
73     int i_ret;
74
75     /* allocate structure */
76     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
77     if( p_aout->output.p_sys == NULL )
78     {
79         msg_Err( p_aout, "out of memory" );
80         return -1;
81     }
82
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 )
88     {
89         msg_Err( p_aout, "unable to open audio device (%s)",
90                          snd_strerror( i_ret ) );
91         free( p_aout->output.p_sys );
92         return -1;
93     }
94
95     /* disable mmap */
96     if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
97                                               PLUGIN_DISABLE_MMAP ) ) < 0 )
98     {
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 );
102         return -1;
103     }
104
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,
108                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
109     {
110         msg_Err( p_aout, "cannot create QNX audio thread (%s)", strerror(errno) );
111         E_(CloseAudio)( p_this );
112         free( p_aout->output.p_sys );
113         return -1;
114     }
115
116     p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
117
118     p_aout->output.pf_setformat = SetFormat;
119     p_aout->output.pf_play = Play;
120
121     return( 0 );
122 }
123
124 /*****************************************************************************
125  * SetFormat : set the audio output format
126  *****************************************************************************
127  * This function prepares the device, sets the rate, format, the mode
128  * ("play as soon as you have data"), and buffer information.
129  *****************************************************************************/
130 static int SetFormat( aout_instance_t *p_this )
131 {
132     int i_ret;
133     int i_bytes_per_sample;
134     snd_pcm_channel_info_t pi;
135     snd_pcm_channel_params_t pp;
136     aout_instance_t *p_aout = (aout_instance_t *)p_this;
137
138     p_aout->output.p_sys->b_initialized = VLC_FALSE;
139
140     memset( &pi, 0, sizeof(pi) );
141     memset( &pp, 0, sizeof(pp) );
142
143     pi.channel = SND_PCM_CHANNEL_PLAYBACK;
144     if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
145                                        &pi ) ) < 0 )
146     {
147         msg_Err( p_aout, "unable to get plugin info (%s)",
148                          snd_strerror( i_ret ) );
149         return -1;
150     }
151
152     pp.mode       = SND_PCM_MODE_BLOCK;
153     pp.channel    = SND_PCM_CHANNEL_PLAYBACK;
154     pp.start_mode = SND_PCM_START_FULL;
155     pp.stop_mode  = SND_PCM_STOP_STOP;
156
157     pp.buf.block.frags_max   = 3;
158     pp.buf.block.frags_min   = 1;
159
160     pp.format.interleave     = 1;
161     pp.format.rate           = p_aout->output.output.i_rate;
162     pp.format.voices         = p_aout->output.output.i_channels;
163
164     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
165     p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
166
167     switch( p_aout->output.output.i_format )
168     {
169         case AOUT_FMT_S16_LE:
170             pp.format.format = SND_PCM_SFMT_S16_LE;
171             i_bytes_per_sample = 2;
172             break;
173
174         default:
175             pp.format.format = SND_PCM_SFMT_S16_BE;
176             i_bytes_per_sample = 2;
177             break;
178     }
179
180     pp.buf.block.frag_size = p_aout->output.i_nb_samples *
181                             p_aout->output.output.i_channels *
182                             i_bytes_per_sample;
183
184     /* set parameters */
185     if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
186                                          &pp ) ) < 0 )
187     {
188         msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
189         return -1;
190     }
191
192     /* prepare channel */
193     if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
194                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
195     {
196         msg_Err( p_aout, "unable to prepare channel (%s)",
197                          snd_strerror( i_ret ) );
198         return -1;
199     }
200
201     p_aout->output.p_sys->b_initialized = VLC_TRUE;
202
203     return( 0 );
204 }
205
206 /*****************************************************************************
207  * GetBufInfo: buffer status query
208  *****************************************************************************
209  * This function returns the number of used byte in the queue.
210  * It also deals with errors : indeed if the device comes to run out
211  * of data to play, it switches to the "underrun" status. It has to
212  * be flushed and re-prepared
213  *****************************************************************************/
214 static int GetBufInfo( aout_instance_t *p_aout )
215 {
216     int i_ret;
217     snd_pcm_channel_status_t status;
218
219     /* get current pcm status */
220     memset( &status, 0, sizeof(status) );
221     if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
222                                          &status ) ) < 0 )
223     {
224         msg_Err( p_aout, "unable to get device status (%s)",
225                          snd_strerror( i_ret ) );
226         return( -1 );
227     }
228
229     /* check for underrun */
230     switch( status.status )
231     {
232         case SND_PCM_STATUS_READY:
233         case SND_PCM_STATUS_UNDERRUN:
234             if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
235                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
236             {
237                 msg_Err( p_aout, "unable to prepare channel (%s)",
238                                  snd_strerror( i_ret ) );
239             }
240             break;
241     }
242
243     return( status.count );
244 }
245
246 /*****************************************************************************
247  * Play : plays a sample
248  *****************************************************************************
249  * Plays a sample using the snd_pcm_write function from the alsa API
250  *****************************************************************************/
251 static void Play( aout_instance_t *p_aout )
252 {
253 }
254
255 /*****************************************************************************
256  * CloseAudio: close the audio device
257  *****************************************************************************/
258 void E_(CloseAudio) ( vlc_object_t *p_this )
259 {
260     aout_instance_t *p_aout = (aout_instance_t *)p_this;
261     int i_ret;
262
263     p_aout->b_die = 1;
264     vlc_thread_join( p_aout );
265
266     if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
267     {
268         msg_Err( p_aout, "unable to close audio device (%s)",
269                          snd_strerror( i_ret ) );
270     }
271
272     free( p_aout->output.p_sys->p_silent_buffer );
273     free( p_aout->output.p_sys );
274 }
275
276
277 /*****************************************************************************
278  * QNXaoutThread: asynchronous thread used to DMA the data to the device
279  *****************************************************************************/
280 static int QNXaoutThread( aout_instance_t * p_aout )
281 {
282     struct aout_sys_t * p_sys = p_aout->output.p_sys;
283
284     while ( !p_aout->b_die )
285     {
286         aout_buffer_t * p_buffer;
287         int i_tmp, i_size;
288         byte_t * p_bytes;
289
290         if( !p_sys->b_initialized )
291         {
292             msleep( THREAD_SLEEP );
293             continue;
294         }
295
296         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
297         {
298             mtime_t next_date = 0;
299
300             /* Get the presentation date of the next write() operation. It
301              * is equal to the current date + duration of buffered samples.
302              * Order is important here, since GetBufInfo is believed to take
303              * more time than mdate(). */
304             next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
305                       / p_aout->output.output.i_bytes_per_frame
306                       / p_aout->output.output.i_rate
307                       * p_aout->output.output.i_frame_length;
308             next_date += mdate();
309
310             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
311         }
312         else
313         {
314             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
315         }
316
317         if ( p_buffer != NULL )
318         {
319             p_bytes = p_buffer->p_buffer;
320             i_size = p_buffer->i_nb_bytes;
321         }
322         else
323         {
324             i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
325                       * p_aout->output.output.i_bytes_per_frame;
326             p_bytes = p_aout->output.p_sys->p_silent_buffer;
327             memset( p_bytes, 0, i_size );
328         }
329
330         i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
331                                         (void *) p_bytes,
332                                         (size_t) i_size );
333
334         if( i_tmp < 0 )
335         {
336             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
337         }
338
339         if ( p_buffer != NULL )
340         {
341             aout_BufferFree( p_buffer );
342         }
343     }
344
345     return 0;
346 }
347