1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 the VideoLAN team
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <fcntl.h> /* open(), O_WRONLY */
31 #include <sys/ioctl.h> /* ioctl() */
32 #include <unistd.h> /* write(), close() */
39 #include <vlc_plugin.h>
47 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
48 * SNDCTL_DSP_GETOSPACE */
49 #ifdef HAVE_SOUNDCARD_H
50 # include <soundcard.h>
51 #elif defined( HAVE_SYS_SOUNDCARD_H )
52 # include <sys/soundcard.h>
53 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
54 # include <machine/soundcard.h>
57 /* Patches for ignorant OSS versions */
59 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
63 # ifdef WORDS_BIGENDIAN
64 # define AFMT_S16_NE AFMT_S16_BE
66 # define AFMT_S16_NE AFMT_S16_LE
70 /*****************************************************************************
71 * aout_sys_t: OSS audio output method descriptor
72 *****************************************************************************
73 * This structure is part of the audio output thread descriptor.
74 * It describes the DSP specific properties of an audio device.
75 *****************************************************************************/
79 int b_workaround_buggy_driver;
81 mtime_t max_buffer_duration;
84 /* This must be a power of 2. */
85 #define FRAME_SIZE 1024
86 #define FRAME_COUNT 32
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open ( vlc_object_t * );
92 static void Close ( vlc_object_t * );
94 static void Play ( aout_instance_t * );
95 static int OSSThread ( aout_instance_t * );
97 static mtime_t BufferDuration( aout_instance_t * p_aout );
99 /*****************************************************************************
101 *****************************************************************************/
102 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
103 #define BUGGY_LONGTEXT N_( \
104 "Some buggy OSS drivers just don't like when their internal buffers " \
105 "are completely filled (the sound gets heavily hashed). If you have one " \
106 "of these drivers, then you need to enable this option." )
109 set_shortname( "OSS" );
110 set_description( N_("UNIX OSS audio output") );
112 set_category( CAT_AUDIO );
113 set_subcategory( SUBCAT_AUDIO_AOUT );
114 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
115 N_("OSS DSP device"), NULL, false );
116 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, true );
118 set_capability( "audio output", 100 );
119 add_shortcut( "oss" );
120 set_callbacks( Open, Close );
123 /*****************************************************************************
124 * Probe: probe the audio device for available formats and channels
125 *****************************************************************************/
126 static void Probe( aout_instance_t * p_aout )
128 struct aout_sys_t * p_sys = p_aout->output.p_sys;
129 vlc_value_t val, text;
130 int i_format, i_nb_channels;
132 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
133 text.psz_string = _("Audio Device");
134 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
136 /* Test for multi-channel. */
137 #ifdef SNDCTL_DSP_GETCHANNELMASK
138 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
140 /* Check that the device supports this. */
145 i_format = AFMT_S16_NE;
146 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
147 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
149 msg_Err( p_aout, "cannot reset OSS audio device" );
150 var_Destroy( p_aout, "audio-device" );
154 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
157 if ( !(i_chanmask & DSP_BIND_FRONT) )
159 msg_Err( p_aout, "no front channels! (%x)",
164 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
165 && (p_aout->output.output.i_physical_channels ==
166 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
167 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
170 val.i_int = AOUT_VAR_5_1;
171 text.psz_string = "5.1";
172 var_Change( p_aout, "audio-device",
173 VLC_VAR_ADDCHOICE, &val, &text );
176 if ( (i_chanmask & DSP_BIND_SURR)
177 && (p_aout->output.output.i_physical_channels &
178 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
179 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
181 val.i_int = AOUT_VAR_2F2R;
182 text.psz_string = N_("2 Front 2 Rear");
183 var_Change( p_aout, "audio-device",
184 VLC_VAR_ADDCHOICE, &val, &text );
191 i_format = AFMT_S16_NE;
192 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
193 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
195 msg_Err( p_aout, "cannot reset OSS audio device" );
196 var_Destroy( p_aout, "audio-device" );
200 /* Test for stereo. */
202 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
203 && i_nb_channels == 2 )
205 val.i_int = AOUT_VAR_STEREO;
206 text.psz_string = N_("Stereo");
207 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
211 i_format = AFMT_S16_NE;
212 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
213 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
215 msg_Err( p_aout, "cannot reset OSS audio device" );
216 var_Destroy( p_aout, "audio-device" );
222 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
223 && i_nb_channels == 1 )
225 val.i_int = AOUT_VAR_MONO;
226 text.psz_string = N_("Mono");
227 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
228 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
230 var_Set( p_aout, "audio-device", val );
234 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
236 msg_Err( p_aout, "cannot reset OSS audio device" );
237 var_Destroy( p_aout, "audio-device" );
241 /* Test for spdif. */
242 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
246 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
247 && i_format == AFMT_AC3 )
249 val.i_int = AOUT_VAR_SPDIF;
250 text.psz_string = N_("A/52 over S/PDIF");
251 var_Change( p_aout, "audio-device",
252 VLC_VAR_ADDCHOICE, &val, &text );
253 if( config_GetInt( p_aout, "spdif" ) )
254 var_Set( p_aout, "audio-device", val );
256 else if( config_GetInt( p_aout, "spdif" ) )
258 msg_Warn( p_aout, "S/PDIF not supported by card" );
262 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
266 /*****************************************************************************
267 * Open: open the audio device (the digital sound processor)
268 *****************************************************************************
269 * This function opens the DSP as a usual non-blocking write-only file, and
270 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
271 *****************************************************************************/
272 static int Open( vlc_object_t *p_this )
274 aout_instance_t * p_aout = (aout_instance_t *)p_this;
275 struct aout_sys_t * p_sys;
279 /* Allocate structure */
280 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
283 msg_Err( p_aout, "out of memory" );
287 /* Get device name */
288 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
290 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
295 /* Open the sound device in non-blocking mode, because ALSA's OSS
296 * emulation and some broken OSS drivers would make a blocking call
297 * wait forever until the device is available. Since this breaks the
298 * OSS spec, we immediately put it back to blocking mode if the
299 * operation was successful. */
300 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
301 if( p_sys->i_fd < 0 )
303 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
308 /* if the opening was ok, put the device back in blocking mode */
309 fcntl( p_sys->i_fd, F_SETFL,
310 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
314 p_aout->output.pf_play = Play;
316 if ( var_Type( p_aout, "audio-device" ) == 0 )
321 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
323 /* Probe() has failed. */
328 if ( val.i_int == AOUT_VAR_SPDIF )
330 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
332 else if ( val.i_int == AOUT_VAR_5_1 )
334 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
335 p_aout->output.output.i_physical_channels
336 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
337 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
340 else if ( val.i_int == AOUT_VAR_2F2R )
342 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
343 p_aout->output.output.i_physical_channels
344 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
345 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
347 else if ( val.i_int == AOUT_VAR_STEREO )
349 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
350 p_aout->output.output.i_physical_channels
351 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
353 else if ( val.i_int == AOUT_VAR_MONO )
355 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
356 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
360 /* This should not happen ! */
361 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
367 var_Set( p_aout, "intf-change", val );
369 /* Reset the DSP device */
370 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
372 msg_Err( p_aout, "cannot reset OSS audio device" );
373 close( p_sys->i_fd );
378 /* Set the output format */
379 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
381 int i_format = AFMT_AC3;
383 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
384 || i_format != AFMT_AC3 )
386 msg_Err( p_aout, "cannot reset OSS audio device" );
387 close( p_sys->i_fd );
392 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
393 p_aout->output.i_nb_samples = A52_FRAME_NB;
394 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
395 p_aout->output.output.i_frame_length = A52_FRAME_NB;
397 aout_VolumeNoneInit( p_aout );
400 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
402 unsigned int i_format = AFMT_S16_NE;
403 unsigned int i_frame_size, i_fragments;
405 unsigned int i_nb_channels;
406 audio_buf_info audio_buf;
408 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
410 msg_Err( p_aout, "cannot set audio output format" );
411 close( p_sys->i_fd );
419 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
422 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
425 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
428 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
431 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
434 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
437 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
439 close( p_sys->i_fd );
444 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
446 /* Set the number of channels */
447 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
448 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
450 msg_Err( p_aout, "cannot set number of audio channels (%s)",
451 aout_FormatPrintChannels( &p_aout->output.output) );
452 close( p_sys->i_fd );
457 /* Set the output rate */
458 i_rate = p_aout->output.output.i_rate;
459 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
461 msg_Err( p_aout, "cannot set audio output rate (%i)",
462 p_aout->output.output.i_rate );
463 close( p_sys->i_fd );
468 if( i_rate != p_aout->output.output.i_rate )
470 p_aout->output.output.i_rate = i_rate;
473 /* Set the fragment size */
474 aout_FormatPrepare( &p_aout->output.output );
476 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
477 * 1 << yyyy is fragsize */
478 i_frame_size = ((uint64_t)p_aout->output.output.i_bytes_per_frame * p_aout->output.output.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
480 while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
484 i_fragments |= FRAME_COUNT << 16;
485 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
487 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
490 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
492 msg_Err( p_aout, "cannot get fragment size" );
493 close( p_sys->i_fd );
499 /* Number of fragments actually allocated */
500 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
502 /* Maximum duration the soundcard's buffer can hold */
503 p_aout->output.p_sys->max_buffer_duration =
504 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
505 / p_aout->output.output.i_bytes_per_frame
506 / p_aout->output.output.i_rate
507 * p_aout->output.output.i_frame_length;
509 p_aout->output.i_nb_samples = audio_buf.fragsize /
510 p_aout->output.output.i_bytes_per_frame;
513 aout_VolumeSoftInit( p_aout );
516 p_aout->output.p_sys->b_workaround_buggy_driver =
517 config_GetInt( p_aout, "oss-buggy" );
519 /* Create OSS thread and wait for its readiness. */
520 if( vlc_thread_create( p_aout, "aout", OSSThread,
521 VLC_THREAD_PRIORITY_OUTPUT, false ) )
523 msg_Err( p_aout, "cannot create OSS thread (%m)" );
524 close( p_sys->i_fd );
532 /*****************************************************************************
533 * Play: nothing to do
534 *****************************************************************************/
535 static void Play( aout_instance_t *p_aout )
540 /*****************************************************************************
541 * Close: close the DSP audio device
542 *****************************************************************************/
543 static void Close( vlc_object_t * p_this )
545 aout_instance_t *p_aout = (aout_instance_t *)p_this;
546 struct aout_sys_t * p_sys = p_aout->output.p_sys;
548 vlc_object_kill( p_aout );
549 vlc_thread_join( p_aout );
550 p_aout->b_die = false;
552 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
553 close( p_sys->i_fd );
558 /*****************************************************************************
559 * BufferDuration: buffer status query
560 *****************************************************************************
561 * This function returns the duration in microseconds of the current buffer.
562 *****************************************************************************/
563 static mtime_t BufferDuration( aout_instance_t * p_aout )
565 struct aout_sys_t * p_sys = p_aout->output.p_sys;
566 audio_buf_info audio_buf;
569 /* Fill the audio_buf_info structure:
570 * - fragstotal: total number of fragments allocated
571 * - fragsize: size of a fragment in bytes
572 * - bytes: available space in bytes (includes partially used fragments)
573 * Note! 'bytes' could be more than fragments*fragsize */
574 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
576 /* calculate number of available fragments (not partially used ones) */
577 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
579 /* Return the fragment duration */
580 return (mtime_t)i_bytes * 1000000
581 / p_aout->output.output.i_bytes_per_frame
582 / p_aout->output.output.i_rate
583 * p_aout->output.output.i_frame_length;
586 /*****************************************************************************
587 * OSSThread: asynchronous thread used to DMA the data to the device
588 *****************************************************************************/
589 static int OSSThread( aout_instance_t * p_aout )
591 struct aout_sys_t * p_sys = p_aout->output.p_sys;
592 mtime_t next_date = 0;
594 while ( !p_aout->b_die )
596 aout_buffer_t * p_buffer = NULL;
600 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
602 mtime_t buffered = BufferDuration( p_aout );
604 if( p_aout->output.p_sys->b_workaround_buggy_driver )
606 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
607 /* Wait a bit - we don't want our buffer to be full */
608 if( buffered > (p_aout->output.p_sys->max_buffer_duration
609 / i_fragstotal * (i_fragstotal - 1)) )
611 msleep((p_aout->output.p_sys->max_buffer_duration
613 buffered = BufferDuration( p_aout );
618 /* Next buffer will be played at mdate() + buffered */
619 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
622 if( p_buffer == NULL &&
623 buffered > ( p_aout->output.p_sys->max_buffer_duration
624 / p_aout->output.p_sys->i_fragstotal ) )
626 /* If we have at least a fragment full, then we can wait a
627 * little and retry to get a new audio buffer instead of
628 * playing a blank sample */
629 msleep( ( p_aout->output.p_sys->max_buffer_duration
630 / p_aout->output.p_sys->i_fragstotal / 2 ) );
636 /* emu10k1 driver does not report Buffer Duration correctly in
637 * passthrough mode so we have to cheat */
644 mtime_t delay = next_date - mdate();
645 if( delay > AOUT_PTS_TOLERANCE )
651 while( !p_aout->b_die && ! ( p_buffer =
652 aout_OutputNextBuffer( p_aout, next_date, true ) ) )
659 if ( p_buffer != NULL )
661 p_bytes = p_buffer->p_buffer;
662 i_size = p_buffer->i_nb_bytes;
663 /* This is theoretical ... we'll see next iteration whether
665 next_date += p_buffer->end_date - p_buffer->start_date;
669 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
670 * p_aout->output.output.i_bytes_per_frame;
671 p_bytes = malloc( i_size );
672 memset( p_bytes, 0, i_size );
676 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
680 msg_Err( p_aout, "write failed (%m)" );
683 if ( p_buffer != NULL )
685 aout_BufferFree( p_buffer );