]> git.sesse.net Git - vlc/blob - modules/gui/qnx/aout.c
A bit of headers cleanup
[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 #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 };
51
52 #define DEFAULT_FRAME_SIZE 2048
53
54 /*****************************************************************************
55  * Local prototypes
56  *****************************************************************************/
57 int            E_(OpenAudio)    ( vlc_object_t *p_this );
58 void           E_(CloseAudio)   ( vlc_object_t *p_this );
59 static int     GetBufInfo       ( aout_instance_t * );
60 static void    Play             ( aout_instance_t * );
61 static int     QNXaoutThread    ( aout_instance_t * );
62
63 /*****************************************************************************
64  * Open : creates a handle and opens an alsa device
65  *****************************************************************************
66  * This function opens an alsa device, through the alsa API
67  *****************************************************************************/
68 int E_(OpenAudio)( vlc_object_t *p_this )
69 {
70     aout_instance_t *p_aout = (aout_instance_t *)p_this;
71     int i_ret;
72     int i_bytes_per_sample;
73     int i_nb_channels;
74     snd_pcm_channel_info_t pi;
75     snd_pcm_channel_params_t pp;
76     aout_instance_t *p_aout = (aout_instance_t *)p_this;
77
78     /* allocate structure */
79     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
80     if( p_aout->output.p_sys == NULL )
81     {
82         msg_Err( p_aout, "out of memory" );
83         return -1;
84     }
85
86     /* open audio device */
87     if( ( i_ret = snd_pcm_open_preferred( &p_aout->output.p_sys->p_pcm_handle,
88                                           &p_aout->output.p_sys->i_card,
89                                           &p_aout->output.p_sys->i_device,
90                                           SND_PCM_OPEN_PLAYBACK ) ) < 0 )
91     {
92         msg_Err( p_aout, "unable to open audio device (%s)",
93                          snd_strerror( i_ret ) );
94         free( p_aout->output.p_sys );
95         return -1;
96     }
97
98     /* disable mmap */
99     if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
100                                               PLUGIN_DISABLE_MMAP ) ) < 0 )
101     {
102         msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
103         E_(CloseAudio)( p_this );
104         free( p_aout->output.p_sys );
105         return -1;
106     }
107
108     p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
109     p_aout->output.pf_play = Play;
110     aout_VolumeSoftInit( p_aout );
111
112     memset( &pi, 0, sizeof(pi) );
113     memset( &pp, 0, sizeof(pp) );
114
115     pi.channel = SND_PCM_CHANNEL_PLAYBACK;
116     if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
117                                        &pi ) ) < 0 )
118     {
119         msg_Err( p_aout, "unable to get plugin info (%s)",
120                          snd_strerror( i_ret ) );
121         E_(CloseAudio)( p_this );
122         free( p_aout->output.p_sys );
123         return -1;
124     }
125
126     pp.mode       = SND_PCM_MODE_BLOCK;
127     pp.channel    = SND_PCM_CHANNEL_PLAYBACK;
128     pp.start_mode = SND_PCM_START_FULL;
129     pp.stop_mode  = SND_PCM_STOP_STOP;
130
131     pp.buf.block.frags_max   = 3;
132     pp.buf.block.frags_min   = 1;
133
134     pp.format.interleave     = 1;
135     pp.format.rate           = p_aout->output.output.i_rate;
136
137     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
138     if ( i_nb_channels > 2 )
139     {
140         /* I don't know if QNX supports more than two channels. */
141         i_nb_channels = 2;
142         p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
143     }
144     pp.format.voices         = i_nb_channels;
145
146     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
147     p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
148     pp.format.format = SND_PCM_SFMT_S16;
149     i_bytes_per_sample = 2;
150
151     pp.buf.block.frag_size = p_aout->output.i_nb_samples *
152                             p_aout->output.output.i_channels *
153                             i_bytes_per_sample;
154
155     /* set parameters */
156     if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
157                                          &pp ) ) < 0 )
158     {
159         msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
160         E_(CloseAudio)( p_this );
161         free( p_aout->output.p_sys );
162         return -1;
163     }
164
165     /* prepare channel */
166     if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
167                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
168     {
169         msg_Err( p_aout, "unable to prepare channel (%s)",
170                          snd_strerror( i_ret ) );
171         E_(CloseAudio)( p_this );
172         free( p_aout->output.p_sys );
173         return -1;
174     }
175
176     /* Create audio thread and wait for its readiness. */
177     if( vlc_thread_create( p_aout, "aout", QNXaoutThread,
178                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
179     {
180         msg_Err( p_aout, "cannot create QNX audio thread (%s)", strerror(errno) );
181         E_(CloseAudio)( p_this );
182         free( p_aout->output.p_sys );
183         return -1;
184     }
185
186     return( 0 );
187 }
188
189 /*****************************************************************************
190  * GetBufInfo: buffer status query
191  *****************************************************************************
192  * This function returns the number of used byte in the queue.
193  * It also deals with errors : indeed if the device comes to run out
194  * of data to play, it switches to the "underrun" status. It has to
195  * be flushed and re-prepared
196  *****************************************************************************/
197 static int GetBufInfo( aout_instance_t *p_aout )
198 {
199     int i_ret;
200     snd_pcm_channel_status_t status;
201
202     /* get current pcm status */
203     memset( &status, 0, sizeof(status) );
204     if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
205                                          &status ) ) < 0 )
206     {
207         msg_Err( p_aout, "unable to get device status (%s)",
208                          snd_strerror( i_ret ) );
209         return( -1 );
210     }
211
212     /* check for underrun */
213     switch( status.status )
214     {
215         case SND_PCM_STATUS_READY:
216         case SND_PCM_STATUS_UNDERRUN:
217             if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
218                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
219             {
220                 msg_Err( p_aout, "unable to prepare channel (%s)",
221                                  snd_strerror( i_ret ) );
222             }
223             break;
224     }
225
226     return( status.count );
227 }
228
229 /*****************************************************************************
230  * Play : plays a sample
231  *****************************************************************************
232  * Plays a sample using the snd_pcm_write function from the alsa API
233  *****************************************************************************/
234 static void Play( aout_instance_t *p_aout )
235 {
236 }
237
238 /*****************************************************************************
239  * CloseAudio: close the audio device
240  *****************************************************************************/
241 void E_(CloseAudio) ( vlc_object_t *p_this )
242 {
243     aout_instance_t *p_aout = (aout_instance_t *)p_this;
244     int i_ret;
245
246     p_aout->b_die = 1;
247     vlc_thread_join( p_aout );
248
249     if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
250     {
251         msg_Err( p_aout, "unable to close audio device (%s)",
252                          snd_strerror( i_ret ) );
253     }
254
255     free( p_aout->output.p_sys->p_silent_buffer );
256     free( p_aout->output.p_sys );
257 }
258
259
260 /*****************************************************************************
261  * QNXaoutThread: asynchronous thread used to DMA the data to the device
262  *****************************************************************************/
263 static int QNXaoutThread( aout_instance_t * p_aout )
264 {
265     struct aout_sys_t * p_sys = p_aout->output.p_sys;
266
267     while ( !p_aout->b_die )
268     {
269         aout_buffer_t * p_buffer;
270         int i_tmp, i_size;
271         byte_t * p_bytes;
272
273         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
274         {
275             mtime_t next_date = 0;
276
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();
286
287             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
288         }
289         else
290         {
291             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
292         }
293
294         if ( p_buffer != NULL )
295         {
296             p_bytes = p_buffer->p_buffer;
297             i_size = p_buffer->i_nb_bytes;
298         }
299         else
300         {
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 );
305         }
306
307         i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
308                                         (void *) p_bytes,
309                                         (size_t) i_size );
310
311         if( i_tmp < 0 )
312         {
313             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
314         }
315
316         if ( p_buffer != NULL )
317         {
318             aout_BufferFree( p_buffer );
319         }
320     }
321
322     return 0;
323 }
324