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