]> git.sesse.net Git - vlc/blob - modules/gui/qnx/aout.c
Replace strerror() with %m (or Linux DVB: strerror_r) - refs #1297
[vlc] / modules / gui / qnx / aout.c
1 /*****************************************************************************
2  * aout.c : QNX audio output
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29
30 #include <vlc/vlc.h>
31
32 #ifdef HAVE_ALLOCA_H
33 #   include <alloca.h>
34 #endif
35
36 #include <vlc_aout.h>
37
38 #include <sys/asoundlib.h>
39
40 struct aout_sys_t
41 {
42     snd_pcm_t  * p_pcm_handle;
43     int          i_card;
44     int          i_device;
45
46     byte_t *     p_silent_buffer;
47 };
48
49 #define DEFAULT_FRAME_SIZE 2048
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 int            E_(OpenAudio)    ( vlc_object_t *p_this );
55 void           E_(CloseAudio)   ( vlc_object_t *p_this );
56 static int     GetBufInfo       ( aout_instance_t * );
57 static void    Play             ( aout_instance_t * );
58 static int     QNXaoutThread    ( aout_instance_t * );
59
60 /*****************************************************************************
61  * Open : creates a handle and opens an alsa device
62  *****************************************************************************
63  * This function opens an alsa device, through the alsa API
64  *****************************************************************************/
65 int E_(OpenAudio)( vlc_object_t *p_this )
66 {
67     aout_instance_t *p_aout = (aout_instance_t *)p_this;
68     int i_ret;
69     int i_bytes_per_sample;
70     int i_nb_channels;
71     snd_pcm_channel_info_t pi;
72     snd_pcm_channel_params_t pp;
73     aout_instance_t *p_aout = (aout_instance_t *)p_this;
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     p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
106     p_aout->output.pf_play = Play;
107     aout_VolumeSoftInit( p_aout );
108
109     memset( &pi, 0, sizeof(pi) );
110     memset( &pp, 0, sizeof(pp) );
111
112     pi.channel = SND_PCM_CHANNEL_PLAYBACK;
113     if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
114                                        &pi ) ) < 0 )
115     {
116         msg_Err( p_aout, "unable to get plugin info (%s)",
117                          snd_strerror( i_ret ) );
118         E_(CloseAudio)( p_this );
119         free( p_aout->output.p_sys );
120         return -1;
121     }
122
123     pp.mode       = SND_PCM_MODE_BLOCK;
124     pp.channel    = SND_PCM_CHANNEL_PLAYBACK;
125     pp.start_mode = SND_PCM_START_FULL;
126     pp.stop_mode  = SND_PCM_STOP_STOP;
127
128     pp.buf.block.frags_max   = 3;
129     pp.buf.block.frags_min   = 1;
130
131     pp.format.interleave     = 1;
132     pp.format.rate           = p_aout->output.output.i_rate;
133
134     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
135     if ( i_nb_channels > 2 )
136     {
137         /* I don't know if QNX supports more than two channels. */
138         i_nb_channels = 2;
139         p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
140     }
141     pp.format.voices         = i_nb_channels;
142
143     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
144     p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
145     pp.format.format = SND_PCM_SFMT_S16;
146     i_bytes_per_sample = 2;
147
148     pp.buf.block.frag_size = p_aout->output.i_nb_samples *
149                             p_aout->output.output.i_channels *
150                             i_bytes_per_sample;
151
152     /* set parameters */
153     if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
154                                          &pp ) ) < 0 )
155     {
156         msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
157         E_(CloseAudio)( p_this );
158         free( p_aout->output.p_sys );
159         return -1;
160     }
161
162     /* prepare channel */
163     if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
164                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
165     {
166         msg_Err( p_aout, "unable to prepare channel (%s)",
167                          snd_strerror( i_ret ) );
168         E_(CloseAudio)( p_this );
169         free( p_aout->output.p_sys );
170         return -1;
171     }
172
173     /* Create audio thread and wait for its readiness. */
174     if( vlc_thread_create( p_aout, "aout", QNXaoutThread,
175                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
176     {
177         msg_Err( p_aout, "cannot create QNX audio thread (%m)" );
178         E_(CloseAudio)( p_this );
179         free( p_aout->output.p_sys );
180         return -1;
181     }
182
183     return( 0 );
184 }
185
186 /*****************************************************************************
187  * GetBufInfo: buffer status query
188  *****************************************************************************
189  * This function returns the number of used byte in the queue.
190  * It also deals with errors : indeed if the device comes to run out
191  * of data to play, it switches to the "underrun" status. It has to
192  * be flushed and re-prepared
193  *****************************************************************************/
194 static int GetBufInfo( aout_instance_t *p_aout )
195 {
196     int i_ret;
197     snd_pcm_channel_status_t status;
198
199     /* get current pcm status */
200     memset( &status, 0, sizeof(status) );
201     if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
202                                          &status ) ) < 0 )
203     {
204         msg_Err( p_aout, "unable to get device status (%s)",
205                          snd_strerror( i_ret ) );
206         return( -1 );
207     }
208
209     /* check for underrun */
210     switch( status.status )
211     {
212         case SND_PCM_STATUS_READY:
213         case SND_PCM_STATUS_UNDERRUN:
214             if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
215                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
216             {
217                 msg_Err( p_aout, "unable to prepare channel (%s)",
218                                  snd_strerror( i_ret ) );
219             }
220             break;
221     }
222
223     return( status.count );
224 }
225
226 /*****************************************************************************
227  * Play : plays a sample
228  *****************************************************************************
229  * Plays a sample using the snd_pcm_write function from the alsa API
230  *****************************************************************************/
231 static void Play( aout_instance_t *p_aout )
232 {
233 }
234
235 /*****************************************************************************
236  * CloseAudio: close the audio device
237  *****************************************************************************/
238 void E_(CloseAudio) ( vlc_object_t *p_this )
239 {
240     aout_instance_t *p_aout = (aout_instance_t *)p_this;
241     int i_ret;
242
243     vlc_object_kill( p_aout );
244     vlc_thread_join( p_aout );
245
246     if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
247     {
248         msg_Err( p_aout, "unable to close audio device (%s)",
249                          snd_strerror( i_ret ) );
250     }
251
252     free( p_aout->output.p_sys->p_silent_buffer );
253     free( p_aout->output.p_sys );
254 }
255
256
257 /*****************************************************************************
258  * QNXaoutThread: asynchronous thread used to DMA the data to the device
259  *****************************************************************************/
260 static int QNXaoutThread( aout_instance_t * p_aout )
261 {
262     struct aout_sys_t * p_sys = p_aout->output.p_sys;
263
264     while ( !p_aout->b_die )
265     {
266         aout_buffer_t * p_buffer;
267         int i_tmp, i_size;
268         byte_t * p_bytes;
269
270         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
271         {
272             mtime_t next_date = 0;
273
274             /* Get the presentation date of the next write() operation. It
275              * is equal to the current date + duration of buffered samples.
276              * Order is important here, since GetBufInfo is believed to take
277              * more time than mdate(). */
278             next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
279                       / p_aout->output.output.i_bytes_per_frame
280                       / p_aout->output.output.i_rate
281                       * p_aout->output.output.i_frame_length;
282             next_date += mdate();
283
284             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
285         }
286         else
287         {
288             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
289         }
290
291         if ( p_buffer != NULL )
292         {
293             p_bytes = p_buffer->p_buffer;
294             i_size = p_buffer->i_nb_bytes;
295         }
296         else
297         {
298             i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
299                       * p_aout->output.output.i_bytes_per_frame;
300             p_bytes = p_aout->output.p_sys->p_silent_buffer;
301             memset( p_bytes, 0, i_size );
302         }
303
304         i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
305                                         (void *) p_bytes,
306                                         (size_t) i_size );
307
308         if( i_tmp < 0 )
309         {
310             msg_Err( p_aout, "write failed (%m)" );
311         }
312
313         if ( p_buffer != NULL )
314         {
315             aout_BufferFree( p_buffer );
316         }
317     }
318
319     return 0;
320 }
321