]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* modules/demux/mpeg: Added DVB stream type for A/52 streams (0x6),
[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.30 2002/10/20 12:23:47 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 /* This must be a power of 2. */
68 #define FRAME_SIZE 1024
69 #define FRAME_COUNT 8
70 #define A52_FRAME_NB 1536
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int  Open         ( vlc_object_t * );
76 static void Close        ( vlc_object_t * );
77
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", aout_FindAndRestart,
87               N_("OSS dsp device"), NULL );
88     set_description( _("Linux OSS /dev/dsp module") );
89     set_capability( "audio output", 100 );
90     add_shortcut( "oss" );
91     set_callbacks( Open, Close );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Open: open the audio device (the digital sound processor)
96  *****************************************************************************
97  * This function opens the dsp as a usual non-blocking write-only file, and
98  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     aout_instance_t * p_aout = (aout_instance_t *)p_this;
103     struct aout_sys_t * p_sys;
104     char * psz_device;
105
106     /* Allocate structure */
107     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
108     if( p_sys == NULL )
109     {
110         msg_Err( p_aout, "out of memory" );
111         return VLC_ENOMEM;
112     }
113
114     /* Get device name */
115     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
116     {
117         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
118         free( p_sys );
119         return VLC_EGENERIC;
120     }
121
122     /* Open the sound device */
123     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
124     {
125         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
126         free( psz_device );
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130     free( psz_device );
131
132     p_aout->output.pf_play = Play;
133
134     /* Reset the DSP device */
135     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
136     {
137         msg_Err( p_aout, "cannot reset OSS audio device" );
138         close( p_sys->i_fd );
139         free( p_sys );
140         return VLC_EGENERIC;
141     }
142     
143     /* Set the output format */
144     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
145     {
146         int i_format = AFMT_AC3;
147
148         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
149              || i_format != AFMT_AC3 )
150         {
151             p_aout->output.output.i_format = AOUT_FMT_S16_NE;
152         }
153         else
154         {
155             p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
156             p_aout->output.i_nb_samples = A52_FRAME_NB;
157             p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
158             p_aout->output.output.i_frame_length = A52_FRAME_NB;
159
160             aout_VolumeNoneInit( p_aout );
161         }
162     }
163
164     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
165     {
166         int i_format = AFMT_S16_NE;
167         int i_frame_size, i_fragments;
168         int i_rate;
169         int i_nb_channels;
170
171         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
172         {
173             msg_Err( p_aout, "cannot set audio output format" );
174             close( p_sys->i_fd );
175             free( p_sys );
176             return VLC_EGENERIC;
177         }
178
179         switch ( i_format )
180         {
181         case AFMT_U8:
182             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
183             break;
184         case AFMT_S8:
185             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
186             break;
187         case AFMT_U16_LE:
188             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
189             break;
190         case AFMT_S16_LE:
191             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
192             break;
193         case AFMT_U16_BE:
194             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
195             break;
196         case AFMT_S16_BE:
197             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
198             break;
199         default:
200             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
201                      i_format );
202             close( p_sys->i_fd );
203             free( p_sys );
204             return VLC_EGENERIC;
205         }
206
207         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
208         p_aout->output.i_nb_samples = FRAME_SIZE;
209
210         aout_VolumeSoftInit( p_aout );
211
212         /* Set the fragment size
213          * i_fragment = xxxxyyyy where: xxxx        is fragtotal
214          *                              1 << yyyy   is fragsize */
215         i_fragments = 0;
216         i_frame_size = FRAME_SIZE;
217         while( i_frame_size >>= 1 )
218         {
219             ++i_fragments;
220         }
221         i_fragments |= FRAME_COUNT << 16;
222         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
223         {
224             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
225         }
226
227         /* These cases are desperate because of the OSS API and A/52 spec. */
228         switch ( p_aout->output.output.i_channels )
229         {
230             case AOUT_CHAN_3F:
231             case AOUT_CHAN_2F1R:
232             case AOUT_CHAN_3F1R:
233             case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
234             case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
235             case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
236             case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
237                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
238                 break;
239             case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
240             case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
241             case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
242             case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
243             case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
244                 p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
245                 break;
246             case AOUT_CHAN_3F2R:
247                 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
248                 break;
249         }
250         /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
251            AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
252
253         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
254
255         if ( i_nb_channels > 2 )
256         {
257             /* Check that the device supports this. */
258
259 #ifdef SNDCTL_DSP_GETCHANNELMASK
260             int i_chanmask;
261             if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
262                         &i_chanmask ) == 0 )
263             {
264                 if ( !(i_chanmask & DSP_BIND_FRONT) )
265                 {
266                     msg_Err( p_aout, "No front channels ! (%x)",
267                              i_chanmask );
268                     close( p_sys->i_fd );
269                     free( p_sys );
270                     return VLC_EGENERIC;
271                 }
272
273                 if ( !(i_chanmask & DSP_BIND_SURR) )
274                 {
275                     p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
276                     i_nb_channels = 2;
277                 }
278  
279                 if ( p_aout->output.output.i_channels ==
280                          (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
281                       && !(i_chanmask & DSP_BIND_CENTER_LFE) )
282                 {
283                     p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
284                     i_nb_channels = 4;
285                 }
286             }
287             else
288 #endif
289             {
290                 /* The driver doesn't support this call, assume it is stereo. */
291                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
292                 i_nb_channels = 2;
293             }
294         }
295
296         /* Set the number of channels */
297         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
298         {
299             msg_Err( p_aout, "cannot set number of audio channels (%i)",
300                               p_aout->output.output.i_channels );
301             close( p_sys->i_fd );
302             free( p_sys );
303             return VLC_EGENERIC;
304         }
305
306         if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
307         {
308             switch ( i_nb_channels )
309             {
310             case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
311             case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
312             case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
313             default:
314                 msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
315                 close( p_sys->i_fd );
316                 free( p_sys );
317                 return VLC_EGENERIC;
318             }
319         }
320
321         /* Set the output rate */
322         i_rate = p_aout->output.output.i_rate;
323         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
324         {
325             msg_Err( p_aout, "cannot set audio output rate (%i)",
326                              p_aout->output.output.i_rate );
327             close( p_sys->i_fd );
328             free( p_sys );
329             return VLC_EGENERIC;
330         }
331
332         if( i_rate != p_aout->output.output.i_rate )
333         {
334             p_aout->output.output.i_rate = i_rate;
335         }
336     }
337
338     /* Create OSS thread and wait for its readiness. */
339     if( vlc_thread_create( p_aout, "aout", OSSThread,
340                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
341     {
342         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
343         close( p_sys->i_fd );
344         free( psz_device );
345         free( p_sys );
346         return VLC_ETHREAD;
347     }
348
349     return VLC_SUCCESS;
350 }
351
352 /*****************************************************************************
353  * Play: nothing to do
354  *****************************************************************************/
355 static void Play( aout_instance_t *p_aout )
356 {
357 }
358
359 /*****************************************************************************
360  * Close: close the dsp audio device
361  *****************************************************************************/
362 static void Close( vlc_object_t * p_this )
363 {
364     aout_instance_t *p_aout = (aout_instance_t *)p_this;
365     struct aout_sys_t * p_sys = p_aout->output.p_sys;
366
367     p_aout->b_die = VLC_TRUE;
368     vlc_thread_join( p_aout );
369     p_aout->b_die = VLC_FALSE;
370
371     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
372     close( p_sys->i_fd );
373
374     free( p_sys );
375 }
376
377
378 /*****************************************************************************
379  * BufferDuration: buffer status query
380  *****************************************************************************
381  * This function returns the duration in microseconds of the current buffer.
382  *****************************************************************************/
383 static mtime_t BufferDuration( aout_instance_t * p_aout )
384 {
385     struct aout_sys_t * p_sys = p_aout->output.p_sys;
386     audio_buf_info audio_buf;
387     int i_bytes;
388
389     /* Fill the audio_buf_info structure:
390      * - fragstotal: total number of fragments allocated
391      * - fragsize: size of a fragment in bytes
392      * - bytes: available space in bytes (includes partially used fragments)
393      * Note! 'bytes' could be more than fragments*fragsize */
394     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
395
396     /* calculate number of available fragments (not partially used ones) */
397     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
398
399     /* Return the fragment duration */
400     return (mtime_t)i_bytes * 1000000
401             / p_aout->output.output.i_bytes_per_frame
402             / p_aout->output.output.i_rate
403             * p_aout->output.output.i_frame_length;
404 }
405
406 /*****************************************************************************
407  * OSSThread: asynchronous thread used to DMA the data to the device
408  *****************************************************************************/
409 static int OSSThread( aout_instance_t * p_aout )
410 {
411     struct aout_sys_t * p_sys = p_aout->output.p_sys;
412     mtime_t next_date = 0;
413
414     while ( !p_aout->b_die )
415     {
416         aout_buffer_t * p_buffer = NULL;
417         int i_tmp, i_size;
418         byte_t * p_bytes;
419
420         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
421         {
422             mtime_t buffered = BufferDuration( p_aout );
423
424             /* Wait a bit - we don't want our buffer to be full */
425             while( buffered > AOUT_PTS_TOLERANCE * 2 )
426             {
427                 msleep( buffered / 2 - 10000 );
428                 buffered = BufferDuration( p_aout );
429             }
430
431             if( !next_date )
432             {
433                 /* This is the _real_ presentation date */
434                 next_date = mdate() + buffered;
435             }
436             else
437             {
438                 /* Give a hint to the audio output about our drift, but
439                  * not too much because we want to make it happy with our
440                  * nicely calculated dates. */
441                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
442             }
443
444             /* Next buffer will be played at mdate()+buffered */
445             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
446         }
447         else
448         {
449             /* emu10k1 driver does not report Buffer Duration correctly in
450              * passthrough mode so we have to cheat */
451             if( !next_date )
452             {
453                 next_date = mdate();
454             }
455             else
456             {
457                 mtime_t delay = next_date - mdate();
458                 if( delay > AOUT_PTS_TOLERANCE )
459                 {
460                     msleep( delay / 2 );
461                 }
462             }
463             
464             while( !p_aout->b_die && ! ( p_buffer =
465                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
466             {
467                 msleep( 1000 );
468                 next_date = mdate();
469             }
470         }
471
472         if ( p_buffer != NULL )
473         {
474             p_bytes = p_buffer->p_buffer;
475             i_size = p_buffer->i_nb_bytes;
476             /* This is theoretical ... we'll see next iteration whether
477              * we're drifting */
478             next_date += p_buffer->end_date - p_buffer->start_date;
479         }
480         else
481         {
482             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
483                       * p_aout->output.output.i_bytes_per_frame;
484             p_bytes = malloc( i_size );
485             memset( p_bytes, 0, i_size );
486             next_date = 0;
487         }
488
489         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
490
491         if( i_tmp < 0 )
492         {
493             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
494         }
495
496         if ( p_buffer != NULL )
497         {
498             aout_BufferFree( p_buffer );
499         }
500         else
501         {
502             free( p_bytes );
503         }
504     }
505
506     return VLC_SUCCESS;
507 }