]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* Made audio_sample_format->i_format a FOURCC to allow the creation of
[vlc] / modules / audio_output / oss.c
1 /*****************************************************************************
2  * oss.c : OSS /dev/dsp module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VideoLAN
5  * $Id: oss.c,v 1.27 2002/09/30 21:32:32 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <fcntl.h>                                       /* open(), O_WRONLY */
31 #include <sys/ioctl.h>                                            /* ioctl() */
32 #include <string.h>                                            /* strerror() */
33 #include <unistd.h>                                      /* write(), close() */
34 #include <stdlib.h>                            /* calloc(), malloc(), free() */
35
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_ALLOCA_H
39 #   include <alloca.h>
40 #endif
41
42 #include <vlc/aout.h>
43
44 #include "aout_internal.h"
45
46 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
47  * SNDCTL_DSP_GETOSPACE */
48 #ifdef HAVE_SOUNDCARD_H
49 #   include <soundcard.h>
50 #elif defined( HAVE_SYS_SOUNDCARD_H )
51 #   include <sys/soundcard.h>
52 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
53 #   include <machine/soundcard.h>
54 #endif
55
56 /*****************************************************************************
57  * aout_sys_t: OSS audio output method descriptor
58  *****************************************************************************
59  * This structure is part of the audio output thread descriptor.
60  * It describes the dsp specific properties of an audio device.
61  *****************************************************************************/
62 struct aout_sys_t
63 {
64     int                   i_fd;
65 };
66
67 #define FRAME_SIZE 1024
68 #define FRAME_COUNT 8
69 #define A52_FRAME_NB 1536
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 static int  Open         ( vlc_object_t * );
75 static void Close        ( vlc_object_t * );
76
77 static void Play         ( aout_instance_t * );
78 static int  OSSThread    ( aout_instance_t * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin();
84     add_category_hint( N_("OSS"), NULL );
85     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
86     set_description( _("Linux OSS /dev/dsp module") );
87     set_capability( "audio output", 100 );
88     add_shortcut( "dsp" );
89     set_callbacks( Open, Close );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Open: open the audio device (the digital sound processor)
94  *****************************************************************************
95  * This function opens the dsp as a usual non-blocking write-only file, and
96  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
97  *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     aout_instance_t * p_aout = (aout_instance_t *)p_this;
101     struct aout_sys_t * p_sys;
102     char * psz_device;
103     int i_format;
104     int i_rate;
105     int i_frame_size;
106     int i_fragments;
107     vlc_bool_t b_stereo;
108
109     /* Allocate structure */
110     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
111     if( p_sys == NULL )
112     {
113         msg_Err( p_aout, "out of memory" );
114         return VLC_ENOMEM;
115     }
116
117     /* Initialize some variables */
118     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
119     {
120         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
121         free( p_sys );
122         return VLC_EGENERIC;
123     }
124
125     /* Open the sound device */
126     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
127     {
128         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
129         free( psz_device );
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133     free( psz_device );
134
135     p_aout->output.pf_play = Play;
136
137     /* Reset the DSP device */
138     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
139     {
140         msg_Err( p_aout, "cannot reset OSS audio device" );
141         return VLC_EGENERIC;
142     }
143     
144     /* Set the fragment size
145      * i_fragment = xxxxyyyy where: xxxx        is fragtotal
146      *                              1 << yyyy   is fragsize */
147     i_fragments = 0;
148     i_frame_size = FRAME_SIZE;
149     while( i_frame_size >>= 1 )
150     {
151         ++i_fragments;
152     }
153     i_fragments |= FRAME_COUNT << 16;
154     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
155     {
156         msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
157         return VLC_EGENERIC;
158     }
159
160     /* Set the output format */
161     if ( p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i') )
162     {
163         i_format = VLC_FOURCC('s','p','d','i');
164         p_aout->output.i_nb_samples = A52_FRAME_NB;
165         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
166         p_aout->output.output.i_frame_length = A52_FRAME_NB;
167
168         aout_VolumeNoneInit( p_aout );
169     }
170     else
171     {
172         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
173         p_aout->output.i_nb_samples = FRAME_SIZE;
174
175         aout_VolumeSoftInit( p_aout );
176     }
177
178     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
179          || i_format != p_aout->output.output.i_format )
180     {
181         if ( i_format == VLC_FOURCC('s','p','d','i') )
182         {
183             /* Retry with S16 */
184             msg_Warn( p_aout, "cannot set audio output format (%i)", i_format );
185             p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
186             p_aout->output.i_nb_samples = FRAME_SIZE;
187             if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
188                  || i_format != p_aout->output.output.i_format )
189             {
190                 msg_Err( p_aout, "cannot set audio output format (%i)",
191                          i_format );
192                 return VLC_EGENERIC;
193             }
194         }
195         else
196         {
197             msg_Err( p_aout, "cannot set audio output format (%i)", i_format );
198             return VLC_EGENERIC;
199         }
200     }
201
202     if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
203     {
204         /* FIXME */
205         if ( p_aout->output.output.i_channels > 2 )
206         {
207             msg_Warn( p_aout, "only two channels are supported at the moment" );
208             /* Trigger downmixing */
209             p_aout->output.output.i_channels = 2;
210         }
211
212         /* Set the number of channels */
213         b_stereo = p_aout->output.output.i_channels - 1;
214
215         if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
216         {
217             msg_Err( p_aout, "cannot set number of audio channels (%i)",
218                               p_aout->output.output.i_channels );
219             return VLC_EGENERIC;
220         }
221
222         if ( b_stereo + 1 != p_aout->output.output.i_channels )
223         {
224             msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
225                               p_aout->output.output.i_channels,
226                               b_stereo + 1 );
227             p_aout->output.output.i_channels = b_stereo + 1;
228         }
229
230         /* Set the output rate */
231         i_rate = p_aout->output.output.i_rate;
232         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
233         {
234             msg_Err( p_aout, "cannot set audio output rate (%i)",
235                              p_aout->output.output.i_rate );
236             return VLC_EGENERIC;
237         }
238
239         if( i_rate != p_aout->output.output.i_rate )
240         {
241             msg_Warn( p_aout, "driver forced resampling %li->%li",
242                               p_aout->output.output.i_rate, i_rate );
243             p_aout->output.output.i_rate = i_rate;
244         }
245     }
246
247     /* Create OSS thread and wait for its readiness. */
248     if( vlc_thread_create( p_aout, "aout", OSSThread,
249                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
250     {
251         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
252         close( p_sys->i_fd );
253         free( psz_device );
254         free( p_sys );
255         return VLC_ETHREAD;
256     }
257
258     return VLC_SUCCESS;
259 }
260
261 /*****************************************************************************
262  * Play: nothing to do
263  *****************************************************************************/
264 static void Play( aout_instance_t *p_aout )
265 {
266 }
267
268 /*****************************************************************************
269  * Close: close the dsp audio device
270  *****************************************************************************/
271 static void Close( vlc_object_t * p_this )
272 {
273     aout_instance_t *p_aout = (aout_instance_t *)p_this;
274     struct aout_sys_t * p_sys = p_aout->output.p_sys;
275
276     p_aout->b_die = VLC_TRUE;
277     vlc_thread_join( p_aout );
278     p_aout->b_die = VLC_FALSE;
279
280     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
281     close( p_sys->i_fd );
282
283     free( p_sys );
284 }
285
286
287 /*****************************************************************************
288  * BufferDuration: buffer status query
289  *****************************************************************************
290  * This function returns the duration in microseconds of the current buffer.
291  *****************************************************************************/
292 static mtime_t BufferDuration( aout_instance_t * p_aout )
293 {
294     struct aout_sys_t * p_sys = p_aout->output.p_sys;
295     audio_buf_info audio_buf;
296     int i_bytes;
297
298     /* Fill the audio_buf_info structure:
299      * - fragstotal: total number of fragments allocated
300      * - fragsize: size of a fragment in bytes
301      * - bytes: available space in bytes (includes partially used fragments)
302      * Note! 'bytes' could be more than fragments*fragsize */
303     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
304
305     /* calculate number of available fragments (not partially used ones) */
306     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
307
308     /* Return the fragment duration */
309     return (mtime_t)i_bytes * 1000000
310             / p_aout->output.output.i_bytes_per_frame
311             / p_aout->output.output.i_rate
312             * p_aout->output.output.i_frame_length;
313 }
314
315 /*****************************************************************************
316  * OSSThread: asynchronous thread used to DMA the data to the device
317  *****************************************************************************/
318 static int OSSThread( aout_instance_t * p_aout )
319 {
320     struct aout_sys_t * p_sys = p_aout->output.p_sys;
321     mtime_t next_date = 0;
322
323     while ( !p_aout->b_die )
324     {
325         aout_buffer_t * p_buffer = NULL;
326         int i_tmp, i_size;
327         byte_t * p_bytes;
328
329         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
330         {
331             mtime_t buffered = BufferDuration( p_aout );
332
333             /* Wait a bit - we don't want our buffer to be full */
334             while( buffered > AOUT_PTS_TOLERANCE * 2 )
335             {
336                 msleep( buffered / 2 - 10000 );
337                 buffered = BufferDuration( p_aout );
338             }
339
340             if( !next_date )
341             {
342                 /* This is the _real_ presentation date */
343                 next_date = mdate() + buffered;
344             }
345             else
346             {
347                 /* Give a hint to the audio output about our drift, but
348                  * not too much because we want to make it happy with our
349                  * nicely calculated dates. */
350                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
351             }
352
353             /* Next buffer will be played at mdate()+buffered */
354             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
355         }
356         else
357         {
358             /* emu10k1 driver does not report Buffer Duration correctly in
359              * passthrough mode so we have to cheat */
360             if( !next_date )
361             {
362                 next_date = mdate();
363             }
364             else
365             {
366                 mtime_t delay = next_date - mdate();
367                 if( delay > AOUT_PTS_TOLERANCE )
368                 {
369                     msleep( delay / 2 );
370                 }
371             }
372             
373             while( !p_aout->b_die && ! ( p_buffer =
374                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
375             {
376                 msleep( 1000 );
377                 next_date = mdate();
378             }
379         }
380
381         if ( p_buffer != NULL )
382         {
383             p_bytes = p_buffer->p_buffer;
384             i_size = p_buffer->i_nb_bytes;
385             /* This is theoretical ... we'll see next iteration whether
386              * we're drifting */
387             next_date += p_buffer->end_date - p_buffer->start_date;
388         }
389         else
390         {
391             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
392                       * p_aout->output.output.i_bytes_per_frame;
393             p_bytes = malloc( i_size );
394             memset( p_bytes, 0, i_size );
395             next_date = 0;
396         }
397
398         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
399
400         if( i_tmp < 0 )
401         {
402             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
403         }
404
405         if ( p_buffer != NULL )
406         {
407             aout_BufferFree( p_buffer );
408         }
409         else
410         {
411             free( p_bytes );
412         }
413     }
414
415     return VLC_SUCCESS;
416 }