]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
aout3 API change :
[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.21 2002/08/30 23:27:06 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_fragments;
106     vlc_bool_t b_stereo;
107
108     /* Allocate structure */
109     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
110     if( p_sys == NULL )
111     {
112         msg_Err( p_aout, "out of memory" );
113         return VLC_ENOMEM;
114     }
115
116     /* Initialize some variables */
117     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
118     {
119         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
120         free( p_sys );
121         return VLC_EGENERIC;
122     }
123
124     /* Open the sound device */
125     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
126     {
127         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
128         free( psz_device );
129         free( p_sys );
130         return VLC_EGENERIC;
131     }
132     free( psz_device );
133
134     p_aout->output.pf_play = Play;
135
136     /* Reset the DSP device */
137     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
138     {
139         msg_Err( p_aout, "cannot reset OSS audio device" );
140         return VLC_EGENERIC;
141     }
142
143     /* Set the fragment size */
144     i_fragments = FRAME_COUNT << 16 | FRAME_SIZE;
145     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
146     {
147         msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
148         return VLC_EGENERIC;
149     }
150
151     /* Set the output format */
152     if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
153     {
154         i_format = AOUT_FMT_SPDIF;
155         p_aout->output.i_nb_samples = A52_FRAME_NB;
156         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
157         p_aout->output.output.i_frame_length = A52_FRAME_NB;
158     }
159     else
160     {
161         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
162         p_aout->output.i_nb_samples = FRAME_SIZE;
163     }
164
165     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
166          || i_format != p_aout->output.output.i_format )
167     {
168         msg_Err( p_aout, "cannot set audio output format (%i)", i_format );
169         return VLC_EGENERIC;
170     }
171
172     if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
173     {
174         /* FIXME */
175         if ( p_aout->output.output.i_channels > 2 )
176         {
177             msg_Warn( p_aout, "only two channels are supported at the moment" );
178             /* Trigger downmixing */
179             p_aout->output.output.i_channels = 2;
180         }
181
182         /* Set the number of channels */
183         b_stereo = p_aout->output.output.i_channels - 1;
184
185         if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
186         {
187             msg_Err( p_aout, "cannot set number of audio channels (%i)",
188                               p_aout->output.output.i_channels );
189             return VLC_EGENERIC;
190         }
191
192         if ( b_stereo + 1 != p_aout->output.output.i_channels )
193         {
194             msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
195                               p_aout->output.output.i_channels,
196                               b_stereo + 1 );
197             p_aout->output.output.i_channels = b_stereo + 1;
198         }
199
200         /* Set the output rate */
201         i_rate = p_aout->output.output.i_rate;
202         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
203         {
204             msg_Err( p_aout, "cannot set audio output rate (%i)",
205                              p_aout->output.output.i_rate );
206             return VLC_EGENERIC;
207         }
208
209         if( i_rate != p_aout->output.output.i_rate )
210         {
211             msg_Warn( p_aout, "driver forced resampling %li->%li",
212                               p_aout->output.output.i_rate, i_rate );
213             p_aout->output.output.i_rate = i_rate;
214         }
215     }
216
217     /* Create OSS thread and wait for its readiness. */
218     if( vlc_thread_create( p_aout, "aout", OSSThread,
219                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
220     {
221         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
222         close( p_sys->i_fd );
223         free( psz_device );
224         free( p_sys );
225         return VLC_ETHREAD;
226     }
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * Play: nothing to do
233  *****************************************************************************/
234 static void Play( aout_instance_t *p_aout )
235 {
236 }
237
238 /*****************************************************************************
239  * Close: close the dsp audio device
240  *****************************************************************************/
241 static void Close( vlc_object_t * p_this )
242 {
243     aout_instance_t *p_aout = (aout_instance_t *)p_this;
244     struct aout_sys_t * p_sys = p_aout->output.p_sys;
245
246     p_aout->b_die = VLC_TRUE;
247     vlc_thread_join( p_aout );
248     p_aout->b_die = VLC_FALSE;
249
250     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
251     close( p_sys->i_fd );
252
253     free( p_sys );
254 }
255
256
257 /*****************************************************************************
258  * BufferDuration: buffer status query
259  *****************************************************************************
260  * This function returns the duration in microseconfs of the current buffer.
261  *****************************************************************************/
262 static mtime_t BufferDuration( aout_instance_t * p_aout )
263 {
264     struct aout_sys_t * p_sys = p_aout->output.p_sys;
265     audio_buf_info audio_buf;
266     int i_bytes;
267
268     /* Fill the audio_buf_info structure:
269      * - fragstotal: total number of fragments allocated
270      * - fragsize: size of a fragment in bytes
271      * - bytes: available space in bytes (includes partially used fragments)
272      * Note! 'bytes' could be more than fragments*fragsize */
273     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
274
275     /* calculate number of available fragments (not partially used ones) */
276     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
277
278     /* Return the fragment duration */
279     return (mtime_t)i_bytes * 1000000
280             / p_aout->output.output.i_bytes_per_frame
281             / p_aout->output.output.i_rate
282             * p_aout->output.output.i_frame_length;
283 }
284
285 /*****************************************************************************
286  * OSSThread: asynchronous thread used to DMA the data to the device
287  *****************************************************************************/
288 static int OSSThread( aout_instance_t * p_aout )
289 {
290     struct aout_sys_t * p_sys = p_aout->output.p_sys;
291     mtime_t next_date = 0;
292
293     while ( !p_aout->b_die )
294     {
295         aout_buffer_t * p_buffer;
296         int i_tmp, i_size;
297         byte_t * p_bytes;
298
299         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
300         {
301             mtime_t buffered = BufferDuration( p_aout );
302
303             /* Wait a bit - we don't want our buffer to be full */
304             while( buffered > AOUT_PTS_TOLERANCE * 2 )
305             {
306                 msleep( buffered / 2 - 10000 );
307                 buffered = BufferDuration( p_aout );
308             }
309
310             if( !next_date )
311             {
312                 /* This is the _real_ presentation date */
313                 next_date = mdate() + buffered;
314             }
315             else
316             {
317                 /* Give a hint to the audio output about our drift, but
318                  * not too much because we want to make it happy with our
319                  * nicely calculated dates. */
320                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
321             }
322
323             /* Next buffer will be played at mdate()+buffered */
324             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
325         }
326         else
327         {
328             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
329         }
330
331         if ( p_buffer != NULL )
332         {
333             p_bytes = p_buffer->p_buffer;
334             i_size = p_buffer->i_nb_bytes;
335             /* This is theoretical ... we'll see next iteration whether
336              * we're drifting */
337             next_date += p_buffer->end_date - p_buffer->start_date;
338         }
339         else
340         {
341             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
342                       * p_aout->output.output.i_bytes_per_frame;
343             p_bytes = malloc( i_size );
344             memset( p_bytes, 0, i_size );
345             next_date = 0;
346         }
347
348         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
349
350         if( i_tmp < 0 )
351         {
352             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
353         }
354
355         if ( p_buffer != NULL )
356         {
357             aout_BufferFree( p_buffer );
358         }
359         else
360         {
361             free( p_bytes );
362         }
363     }
364
365     return VLC_SUCCESS;
366 }