]> git.sesse.net Git - vlc/blob - modules/gui/qnx/aout.c
* Made audio_sample_format->i_format a FOURCC to allow the creation of
[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 };
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     snd_pcm_channel_info_t pi;
74     snd_pcm_channel_params_t pp;
75     aout_instance_t *p_aout = (aout_instance_t *)p_this;
76
77     /* allocate structure */
78     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
79     if( p_aout->output.p_sys == NULL )
80     {
81         msg_Err( p_aout, "out of memory" );
82         return -1;
83     }
84
85     /* open audio device */
86     if( ( i_ret = snd_pcm_open_preferred( &p_aout->output.p_sys->p_pcm_handle,
87                                           &p_aout->output.p_sys->i_card,
88                                           &p_aout->output.p_sys->i_device,
89                                           SND_PCM_OPEN_PLAYBACK ) ) < 0 )
90     {
91         msg_Err( p_aout, "unable to open audio device (%s)",
92                          snd_strerror( i_ret ) );
93         free( p_aout->output.p_sys );
94         return -1;
95     }
96
97     /* disable mmap */
98     if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
99                                               PLUGIN_DISABLE_MMAP ) ) < 0 )
100     {
101         msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
102         E_(CloseAudio)( p_this );
103         free( p_aout->output.p_sys );
104         return -1;
105     }
106
107     p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
108     p_aout->output.pf_play = Play;
109     aout_VolumeSoftInit( p_aout );
110
111     memset( &pi, 0, sizeof(pi) );
112     memset( &pp, 0, sizeof(pp) );
113
114     pi.channel = SND_PCM_CHANNEL_PLAYBACK;
115     if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
116                                        &pi ) ) < 0 )
117     {
118         msg_Err( p_aout, "unable to get plugin info (%s)",
119                          snd_strerror( i_ret ) );
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     pp.format.voices         = p_aout->output.output.i_channels;
134
135     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
136     p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
137
138     switch( p_aout->output.output.i_format )
139     {
140         case AOUT_FMT_S16_LE:
141             pp.format.format = SND_PCM_SFMT_S16_LE;
142             i_bytes_per_sample = 2;
143             break;
144
145         default:
146             pp.format.format = SND_PCM_SFMT_S16_BE;
147             i_bytes_per_sample = 2;
148             break;
149     }
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         return -1;
161     }
162
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 )
166     {
167         msg_Err( p_aout, "unable to prepare channel (%s)",
168                          snd_strerror( i_ret ) );
169         return -1;
170     }
171
172     /* Create audio thread and wait for its readiness. */
173     if( vlc_thread_create( p_aout, "aout", QNXaoutThread,
174                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
175     {
176         msg_Err( p_aout, "cannot create QNX audio thread (%s)", strerror(errno) );
177         E_(CloseAudio)( p_this );
178         free( p_aout->output.p_sys );
179         return -1;
180     }
181
182     return( 0 );
183 }
184
185 /*****************************************************************************
186  * GetBufInfo: buffer status query
187  *****************************************************************************
188  * This function returns the number of used byte in the queue.
189  * It also deals with errors : indeed if the device comes to run out
190  * of data to play, it switches to the "underrun" status. It has to
191  * be flushed and re-prepared
192  *****************************************************************************/
193 static int GetBufInfo( aout_instance_t *p_aout )
194 {
195     int i_ret;
196     snd_pcm_channel_status_t status;
197
198     /* get current pcm status */
199     memset( &status, 0, sizeof(status) );
200     if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
201                                          &status ) ) < 0 )
202     {
203         msg_Err( p_aout, "unable to get device status (%s)",
204                          snd_strerror( i_ret ) );
205         return( -1 );
206     }
207
208     /* check for underrun */
209     switch( status.status )
210     {
211         case SND_PCM_STATUS_READY:
212         case SND_PCM_STATUS_UNDERRUN:
213             if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
214                                           SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
215             {
216                 msg_Err( p_aout, "unable to prepare channel (%s)",
217                                  snd_strerror( i_ret ) );
218             }
219             break;
220     }
221
222     return( status.count );
223 }
224
225 /*****************************************************************************
226  * Play : plays a sample
227  *****************************************************************************
228  * Plays a sample using the snd_pcm_write function from the alsa API
229  *****************************************************************************/
230 static void Play( aout_instance_t *p_aout )
231 {
232 }
233
234 /*****************************************************************************
235  * CloseAudio: close the audio device
236  *****************************************************************************/
237 void E_(CloseAudio) ( vlc_object_t *p_this )
238 {
239     aout_instance_t *p_aout = (aout_instance_t *)p_this;
240     int i_ret;
241
242     p_aout->b_die = 1;
243     vlc_thread_join( p_aout );
244
245     if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
246     {
247         msg_Err( p_aout, "unable to close audio device (%s)",
248                          snd_strerror( i_ret ) );
249     }
250
251     free( p_aout->output.p_sys->p_silent_buffer );
252     free( p_aout->output.p_sys );
253 }
254
255
256 /*****************************************************************************
257  * QNXaoutThread: asynchronous thread used to DMA the data to the device
258  *****************************************************************************/
259 static int QNXaoutThread( aout_instance_t * p_aout )
260 {
261     struct aout_sys_t * p_sys = p_aout->output.p_sys;
262
263     while ( !p_aout->b_die )
264     {
265         aout_buffer_t * p_buffer;
266         int i_tmp, i_size;
267         byte_t * p_bytes;
268
269         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
270         {
271             mtime_t next_date = 0;
272
273             /* Get the presentation date of the next write() operation. It
274              * is equal to the current date + duration of buffered samples.
275              * Order is important here, since GetBufInfo is believed to take
276              * more time than mdate(). */
277             next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
278                       / p_aout->output.output.i_bytes_per_frame
279                       / p_aout->output.output.i_rate
280                       * p_aout->output.output.i_frame_length;
281             next_date += mdate();
282
283             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
284         }
285         else
286         {
287             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
288         }
289
290         if ( p_buffer != NULL )
291         {
292             p_bytes = p_buffer->p_buffer;
293             i_size = p_buffer->i_nb_bytes;
294         }
295         else
296         {
297             i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
298                       * p_aout->output.output.i_bytes_per_frame;
299             p_bytes = p_aout->output.p_sys->p_silent_buffer;
300             memset( p_bytes, 0, i_size );
301         }
302
303         i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
304                                         (void *) p_bytes,
305                                         (size_t) i_size );
306
307         if( i_tmp < 0 )
308         {
309             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
310         }
311
312         if ( p_buffer != NULL )
313         {
314             aout_BufferFree( p_buffer );
315         }
316     }
317
318     return 0;
319 }
320