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 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <fcntl.h> /* open(), O_WRONLY */
36 #include <sys/ioctl.h> /* ioctl() */
37 #include <unistd.h> /* write(), close() */
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
48 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
49 * SNDCTL_DSP_GETOSPACE */
50 #ifdef HAVE_SOUNDCARD_H
51 # include <soundcard.h>
52 #elif defined( HAVE_SYS_SOUNDCARD_H )
53 # include <sys/soundcard.h>
54 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
55 # include <machine/soundcard.h>
58 /* Patches for ignorant OSS versions */
60 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
64 # ifdef WORDS_BIGENDIAN
65 # define AFMT_S16_NE AFMT_S16_BE
67 # define AFMT_S16_NE AFMT_S16_LE
71 /*****************************************************************************
72 * aout_sys_t: OSS audio output method descriptor
73 *****************************************************************************
74 * This structure is part of the audio output thread descriptor.
75 * It describes the DSP specific properties of an audio device.
76 *****************************************************************************/
80 int b_workaround_buggy_driver;
82 mtime_t max_buffer_duration;
85 /* This must be a power of 2. */
86 #define FRAME_SIZE 1024
87 #define FRAME_COUNT 32
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Open ( vlc_object_t * );
93 static void Close ( vlc_object_t * );
95 static void Play ( aout_instance_t * );
96 static int OSSThread ( aout_instance_t * );
98 static mtime_t BufferDuration( aout_instance_t * p_aout );
100 /*****************************************************************************
102 *****************************************************************************/
103 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
104 #define BUGGY_LONGTEXT N_( \
105 "Some buggy OSS drivers just don't like when their internal buffers " \
106 "are completely filled (the sound gets heavily hashed). If you have one " \
107 "of these drivers, then you need to enable this option." )
110 set_shortname( "OSS" );
111 set_description( N_("UNIX OSS audio output") );
113 set_category( CAT_AUDIO );
114 set_subcategory( SUBCAT_AUDIO_AOUT );
115 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
116 N_("OSS DSP device"), NULL, false );
117 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, true );
119 set_capability( "audio output", 100 );
120 add_shortcut( "oss" );
121 set_callbacks( Open, Close );
124 /*****************************************************************************
125 * Probe: probe the audio device for available formats and channels
126 *****************************************************************************/
127 static void Probe( aout_instance_t * p_aout )
129 struct aout_sys_t * p_sys = p_aout->output.p_sys;
130 vlc_value_t val, text;
131 int i_format, i_nb_channels;
133 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
134 text.psz_string = _("Audio Device");
135 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
137 /* Test for multi-channel. */
138 #ifdef SNDCTL_DSP_GETCHANNELMASK
139 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
141 /* Check that the device supports this. */
146 i_format = AFMT_S16_NE;
147 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
148 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
150 msg_Err( p_aout, "cannot reset OSS audio device" );
151 var_Destroy( p_aout, "audio-device" );
155 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
158 if ( !(i_chanmask & DSP_BIND_FRONT) )
160 msg_Err( p_aout, "no front channels! (%x)",
165 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
166 && (p_aout->output.output.i_physical_channels ==
167 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
168 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
171 val.i_int = AOUT_VAR_5_1;
172 text.psz_string = "5.1";
173 var_Change( p_aout, "audio-device",
174 VLC_VAR_ADDCHOICE, &val, &text );
177 if ( (i_chanmask & DSP_BIND_SURR)
178 && (p_aout->output.output.i_physical_channels &
179 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
180 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
182 val.i_int = AOUT_VAR_2F2R;
183 text.psz_string = N_("2 Front 2 Rear");
184 var_Change( p_aout, "audio-device",
185 VLC_VAR_ADDCHOICE, &val, &text );
192 i_format = AFMT_S16_NE;
193 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
194 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
196 msg_Err( p_aout, "cannot reset OSS audio device" );
197 var_Destroy( p_aout, "audio-device" );
201 /* Test for stereo. */
203 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
204 && i_nb_channels == 2 )
206 val.i_int = AOUT_VAR_STEREO;
207 text.psz_string = N_("Stereo");
208 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
212 i_format = AFMT_S16_NE;
213 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
214 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
216 msg_Err( p_aout, "cannot reset OSS audio device" );
217 var_Destroy( p_aout, "audio-device" );
223 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
224 && i_nb_channels == 1 )
226 val.i_int = AOUT_VAR_MONO;
227 text.psz_string = N_("Mono");
228 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
229 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
231 var_Set( p_aout, "audio-device", val );
235 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
237 msg_Err( p_aout, "cannot reset OSS audio device" );
238 var_Destroy( p_aout, "audio-device" );
242 /* Test for spdif. */
243 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
247 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
248 && i_format == AFMT_AC3 )
250 val.i_int = AOUT_VAR_SPDIF;
251 text.psz_string = N_("A/52 over S/PDIF");
252 var_Change( p_aout, "audio-device",
253 VLC_VAR_ADDCHOICE, &val, &text );
254 if( config_GetInt( p_aout, "spdif" ) )
255 var_Set( p_aout, "audio-device", val );
257 else if( config_GetInt( p_aout, "spdif" ) )
259 msg_Warn( p_aout, "S/PDIF not supported by card" );
263 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
267 /*****************************************************************************
268 * Open: open the audio device (the digital sound processor)
269 *****************************************************************************
270 * This function opens the DSP as a usual non-blocking write-only file, and
271 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
272 *****************************************************************************/
273 static int Open( vlc_object_t *p_this )
275 aout_instance_t * p_aout = (aout_instance_t *)p_this;
276 struct aout_sys_t * p_sys;
280 /* Allocate structure */
281 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
285 /* Get device name */
286 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
288 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
293 /* Open the sound device in non-blocking mode, because ALSA's OSS
294 * emulation and some broken OSS drivers would make a blocking call
295 * wait forever until the device is available. Since this breaks the
296 * OSS spec, we immediately put it back to blocking mode if the
297 * operation was successful. */
298 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
299 if( p_sys->i_fd < 0 )
301 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
307 /* if the opening was ok, put the device back in blocking mode */
308 fcntl( p_sys->i_fd, F_SETFL,
309 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
313 p_aout->output.pf_play = Play;
315 if ( var_Type( p_aout, "audio-device" ) == 0 )
320 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
322 /* Probe() has failed. */
327 if ( val.i_int == AOUT_VAR_SPDIF )
329 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
331 else if ( val.i_int == AOUT_VAR_5_1 )
333 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
334 p_aout->output.output.i_physical_channels
335 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
336 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
339 else if ( val.i_int == AOUT_VAR_2F2R )
341 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
342 p_aout->output.output.i_physical_channels
343 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
344 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
346 else if ( val.i_int == AOUT_VAR_STEREO )
348 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
349 p_aout->output.output.i_physical_channels
350 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
352 else if ( val.i_int == AOUT_VAR_MONO )
354 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
355 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
359 /* This should not happen ! */
360 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
366 var_Set( p_aout, "intf-change", val );
368 /* Reset the DSP device */
369 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
371 msg_Err( p_aout, "cannot reset OSS audio device" );
372 close( p_sys->i_fd );
377 /* Set the output format */
378 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
380 int i_format = AFMT_AC3;
382 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
383 || i_format != AFMT_AC3 )
385 msg_Err( p_aout, "cannot reset OSS audio device" );
386 close( p_sys->i_fd );
391 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
392 p_aout->output.i_nb_samples = A52_FRAME_NB;
393 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
394 p_aout->output.output.i_frame_length = A52_FRAME_NB;
396 aout_VolumeNoneInit( p_aout );
399 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
401 unsigned int i_format = AFMT_S16_NE;
402 unsigned int i_frame_size, i_fragments;
404 unsigned int i_nb_channels;
405 audio_buf_info audio_buf;
407 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
409 msg_Err( p_aout, "cannot set audio output format" );
410 close( p_sys->i_fd );
418 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
421 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
424 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
427 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
430 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
433 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
436 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
438 close( p_sys->i_fd );
443 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
445 /* Set the number of channels */
446 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
447 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
449 msg_Err( p_aout, "cannot set number of audio channels (%s)",
450 aout_FormatPrintChannels( &p_aout->output.output) );
451 close( p_sys->i_fd );
456 /* Set the output rate */
457 i_rate = p_aout->output.output.i_rate;
458 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
460 msg_Err( p_aout, "cannot set audio output rate (%i)",
461 p_aout->output.output.i_rate );
462 close( p_sys->i_fd );
467 if( i_rate != p_aout->output.output.i_rate )
469 p_aout->output.output.i_rate = i_rate;
472 /* Set the fragment size */
473 aout_FormatPrepare( &p_aout->output.output );
475 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
476 * 1 << yyyy is fragsize */
477 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;
479 while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
483 i_fragments |= FRAME_COUNT << 16;
484 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
486 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
489 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
491 msg_Err( p_aout, "cannot get fragment size" );
492 close( p_sys->i_fd );
498 /* Number of fragments actually allocated */
499 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
501 /* Maximum duration the soundcard's buffer can hold */
502 p_aout->output.p_sys->max_buffer_duration =
503 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
504 / p_aout->output.output.i_bytes_per_frame
505 / p_aout->output.output.i_rate
506 * p_aout->output.output.i_frame_length;
508 p_aout->output.i_nb_samples = audio_buf.fragsize /
509 p_aout->output.output.i_bytes_per_frame;
512 aout_VolumeSoftInit( p_aout );
515 p_aout->output.p_sys->b_workaround_buggy_driver =
516 config_GetInt( p_aout, "oss-buggy" );
518 /* Create OSS thread and wait for its readiness. */
519 if( vlc_thread_create( p_aout, "aout", OSSThread,
520 VLC_THREAD_PRIORITY_OUTPUT, false ) )
522 msg_Err( p_aout, "cannot create OSS thread (%m)" );
523 close( p_sys->i_fd );
531 /*****************************************************************************
532 * Play: nothing to do
533 *****************************************************************************/
534 static void Play( aout_instance_t *p_aout )
539 /*****************************************************************************
540 * Close: close the DSP audio device
541 *****************************************************************************/
542 static void Close( vlc_object_t * p_this )
544 aout_instance_t *p_aout = (aout_instance_t *)p_this;
545 struct aout_sys_t * p_sys = p_aout->output.p_sys;
547 vlc_object_kill( p_aout );
548 vlc_thread_join( p_aout );
549 p_aout->b_die = false;
551 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
552 close( p_sys->i_fd );
557 /*****************************************************************************
558 * BufferDuration: buffer status query
559 *****************************************************************************
560 * This function returns the duration in microseconds of the current buffer.
561 *****************************************************************************/
562 static mtime_t BufferDuration( aout_instance_t * p_aout )
564 struct aout_sys_t * p_sys = p_aout->output.p_sys;
565 audio_buf_info audio_buf;
568 /* Fill the audio_buf_info structure:
569 * - fragstotal: total number of fragments allocated
570 * - fragsize: size of a fragment in bytes
571 * - bytes: available space in bytes (includes partially used fragments)
572 * Note! 'bytes' could be more than fragments*fragsize */
573 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
575 /* calculate number of available fragments (not partially used ones) */
576 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
578 /* Return the fragment duration */
579 return (mtime_t)i_bytes * 1000000
580 / p_aout->output.output.i_bytes_per_frame
581 / p_aout->output.output.i_rate
582 * p_aout->output.output.i_frame_length;
585 /*****************************************************************************
586 * OSSThread: asynchronous thread used to DMA the data to the device
587 *****************************************************************************/
588 static int OSSThread( aout_instance_t * p_aout )
590 struct aout_sys_t * p_sys = p_aout->output.p_sys;
591 mtime_t next_date = 0;
593 while ( vlc_object_alive (p_aout) )
595 aout_buffer_t * p_buffer = NULL;
599 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
601 mtime_t buffered = BufferDuration( p_aout );
603 if( p_aout->output.p_sys->b_workaround_buggy_driver )
605 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
606 /* Wait a bit - we don't want our buffer to be full */
607 if( buffered > (p_aout->output.p_sys->max_buffer_duration
608 / i_fragstotal * (i_fragstotal - 1)) )
610 msleep((p_aout->output.p_sys->max_buffer_duration
612 buffered = BufferDuration( p_aout );
617 /* Next buffer will be played at mdate() + buffered */
618 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
621 if( p_buffer == NULL &&
622 buffered > ( p_aout->output.p_sys->max_buffer_duration
623 / p_aout->output.p_sys->i_fragstotal ) )
625 /* If we have at least a fragment full, then we can wait a
626 * little and retry to get a new audio buffer instead of
627 * playing a blank sample */
628 msleep( ( p_aout->output.p_sys->max_buffer_duration
629 / p_aout->output.p_sys->i_fragstotal / 2 ) );
635 /* emu10k1 driver does not report Buffer Duration correctly in
636 * passthrough mode so we have to cheat */
643 mtime_t delay = next_date - mdate();
644 if( delay > AOUT_PTS_TOLERANCE )
650 while( vlc_object_alive (p_aout) && ! ( p_buffer =
651 aout_OutputNextBuffer( p_aout, next_date, true ) ) )
658 if ( p_buffer != NULL )
660 p_bytes = p_buffer->p_buffer;
661 i_size = p_buffer->i_nb_bytes;
662 /* This is theoretical ... we'll see next iteration whether
664 next_date += p_buffer->end_date - p_buffer->start_date;
668 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
669 * p_aout->output.output.i_bytes_per_frame;
670 p_bytes = malloc( i_size );
671 memset( p_bytes, 0, i_size );
675 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
679 msg_Err( p_aout, "write failed (%m)" );
682 if ( p_buffer != NULL )
684 aout_BufferFree( p_buffer );