]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* ./src/audio_output/output.c: added an argument to aout_OutputNextBuffer
[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.15 2002/08/24 10:19:42 sam 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     volatile vlc_bool_t   b_initialized;
66 };
67
68 #define FRAME_SIZE 1024
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 int  SetFormat    ( aout_instance_t * );
78 static void Play         ( aout_instance_t * );
79 static int  OSSThread    ( aout_instance_t * );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin();
85     add_category_hint( N_("OSS"), NULL );
86     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
87     set_description( _("Linux OSS /dev/dsp module") );
88     set_capability( "audio output", 100 );
89     add_shortcut( "dsp" );
90     set_callbacks( Open, Close );
91 vlc_module_end();
92
93 /*****************************************************************************
94  * Open: open the audio device (the digital sound processor)
95  *****************************************************************************
96  * This function opens the dsp as a usual non-blocking write-only file, and
97  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     aout_instance_t * p_aout = (aout_instance_t *)p_this;
102     struct aout_sys_t * p_sys;
103     char * psz_device;
104
105     /* Allocate structure */
106     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
107     if( p_sys == NULL )
108     {
109         msg_Err( p_aout, "out of memory" );
110         return 1;
111     }
112
113     /* Initialize some variables */
114     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
115     {
116         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
117         free( p_sys );
118         return -1;
119     }
120
121     /* Open the sound device */
122     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
123     {
124         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
125         free( psz_device );
126         free( p_sys );
127         return -1;
128     }
129     free( psz_device );
130
131     /* Create OSS thread and wait for its readiness. */
132     p_sys->b_initialized = VLC_FALSE;
133     if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
134     {
135         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
136         close( p_sys->i_fd );
137         free( psz_device );
138         free( p_sys );
139         return -1;
140     }
141
142     p_aout->output.pf_setformat = SetFormat;
143     p_aout->output.pf_play = Play;
144
145     return 0;
146 }
147
148 /*****************************************************************************
149  * SetFormat: reset the dsp and set its format
150  *****************************************************************************
151  * This functions resets the DSP device, tries to initialize the output
152  * format with the value contained in the dsp structure, and if this value
153  * could not be set, the default value returned by ioctl is set. It then
154  * does the same for the stereo mode, and for the output rate.
155  *****************************************************************************/
156 static int SetFormat( aout_instance_t *p_aout )
157 {
158     struct aout_sys_t * p_sys = p_aout->output.p_sys;
159     int i_format;
160     int i_rate;
161     vlc_bool_t b_stereo;
162
163     p_sys->b_initialized = VLC_FALSE;
164
165     /* Reset the DSP device */
166     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
167     {
168         msg_Err( p_aout, "cannot reset OSS audio device" );
169         return -1;
170     }
171
172     /* Set the output format */
173     if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
174     {
175         i_format = AOUT_FMT_SPDIF;
176         p_aout->output.i_nb_samples = A52_FRAME_NB;
177         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
178         p_aout->output.output.i_frame_length = A52_FRAME_NB;
179     }
180     else
181     {
182         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
183         p_aout->output.i_nb_samples = FRAME_SIZE;
184     }
185
186     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
187          || i_format != p_aout->output.output.i_format )
188     {
189         msg_Err( p_aout, "cannot set audio output format (%i)",
190                           i_format );
191         return -1;
192     }
193
194     if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
195     {
196         /* FIXME */
197         if ( p_aout->output.output.i_channels > 2 )
198         {
199             msg_Warn( p_aout, "only two channels are supported at the moment" );
200             /* Trigger downmixing */
201             p_aout->output.output.i_channels = 2;
202         }
203
204         /* Set the number of channels */
205         b_stereo = p_aout->output.output.i_channels - 1;
206
207         if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
208         {
209             msg_Err( p_aout, "cannot set number of audio channels (%i)",
210                               p_aout->output.output.i_channels );
211             return -1;
212         }
213
214         if ( b_stereo + 1 != p_aout->output.output.i_channels )
215         {
216             msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
217                               p_aout->output.output.i_channels,
218                               b_stereo + 1 );
219             p_aout->output.output.i_channels = b_stereo + 1;
220         }
221
222         /* Set the output rate */
223         i_rate = p_aout->output.output.i_rate;
224         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
225         {
226             msg_Err( p_aout, "cannot set audio output rate (%i)",
227                              p_aout->output.output.i_rate );
228             return -1;
229         }
230
231         if( i_rate != p_aout->output.output.i_rate )
232         {
233             msg_Warn( p_aout, "driver forced resampling %li->%li",
234                               p_aout->output.output.i_rate, i_rate );
235             p_aout->output.output.i_rate = i_rate;
236         }
237     }
238
239     p_sys->b_initialized = VLC_TRUE;
240
241     return 0;
242 }
243
244 /*****************************************************************************
245  * Play: queue a buffer for playing by OSSThread
246  *****************************************************************************/
247 static void Play( aout_instance_t *p_aout )
248 {
249 }
250
251 /*****************************************************************************
252  * Close: close the dsp audio device
253  *****************************************************************************/
254 static void Close( vlc_object_t * p_this )
255 {
256     aout_instance_t *p_aout = (aout_instance_t *)p_this;
257     struct aout_sys_t * p_sys = p_aout->output.p_sys;
258
259     p_aout->b_die = 1;
260     vlc_thread_join( p_aout );
261
262     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
263     close( p_sys->i_fd );
264
265     free( p_sys );
266 }
267
268
269 /*****************************************************************************
270  * GetBufInfo: buffer status query
271  *****************************************************************************
272  * This function fills in the audio_buf_info structure :
273  * - returns : number of available fragments (not partially used ones)
274  * - int fragstotal : total number of fragments allocated
275  * - int fragsize : size of a fragment in bytes
276  * - int bytes : available space in bytes (includes partially used fragments)
277  * Note! 'bytes' could be more than fragments*fragsize
278  *****************************************************************************/
279 static int GetBufInfo( aout_instance_t * p_aout )
280 {
281     struct aout_sys_t * p_sys = p_aout->output.p_sys;
282     audio_buf_info audio_buf;
283
284     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
285
286     /* returns the allocated space in bytes */
287     return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
288 }
289
290 /*****************************************************************************
291  * OSSThread: asynchronous thread used to DMA the data to the device
292  *****************************************************************************/
293 static int OSSThread( aout_instance_t * p_aout )
294 {
295     struct aout_sys_t * p_sys = p_aout->output.p_sys;
296
297     while ( !p_aout->b_die )
298     {
299         aout_buffer_t * p_buffer;
300         int i_tmp, i_size;
301         byte_t * p_bytes;
302
303         if( !p_sys->b_initialized )
304         {
305             msleep( THREAD_SLEEP );
306             continue;
307         }
308
309         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
310         {
311             mtime_t buffered = (mtime_t)GetBufInfo( p_aout ) * 1000000
312                                 / p_aout->output.output.i_bytes_per_frame
313                                 / p_aout->output.output.i_rate
314                                 * p_aout->output.output.i_frame_length;
315
316             /* Next buffer will be played at mdate()+buffered, and we tell
317              * the audio output that it can wait for a new packet for
318              * buffered/2 microseconds. */
319             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
320                                               buffered / 2, VLC_FALSE );
321         }
322         else
323         {
324             p_buffer = aout_OutputNextBuffer( p_aout, 0, 0, VLC_TRUE );
325         }
326
327         if ( p_buffer != NULL )
328         {
329             p_bytes = p_buffer->p_buffer;
330             i_size = p_buffer->i_nb_bytes;
331         }
332         else
333         {
334             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
335                       * p_aout->output.output.i_bytes_per_frame;
336             p_bytes = alloca( i_size );
337             memset( p_bytes, 0, i_size );
338         }
339
340         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
341
342         if( i_tmp < 0 )
343         {
344             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
345         }
346
347         if ( p_buffer != NULL )
348         {
349             aout_BufferFree( p_buffer );
350         }
351     }
352
353     return 0;
354 }