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