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 <fcntl.h> /* open(), O_WRONLY */
35 #include <sys/ioctl.h> /* ioctl() */
36 #include <unistd.h> /* write(), close() */
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
44 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
45 * SNDCTL_DSP_GETOSPACE */
46 #ifdef HAVE_SOUNDCARD_H
47 # include <soundcard.h>
48 #elif defined( HAVE_SYS_SOUNDCARD_H )
49 # include <sys/soundcard.h>
52 /* Patches for ignorant OSS versions */
54 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
58 # ifdef WORDS_BIGENDIAN
59 # define AFMT_S16_NE AFMT_S16_BE
61 # define AFMT_S16_NE AFMT_S16_LE
65 /*****************************************************************************
66 * aout_sys_t: OSS audio output method descriptor
67 *****************************************************************************
68 * This structure is part of the audio output thread descriptor.
69 * It describes the DSP specific properties of an audio device.
70 *****************************************************************************/
75 mtime_t max_buffer_duration;
78 /* This must be a power of 2. */
79 #define FRAME_SIZE 1024
80 #define FRAME_COUNT 32
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open ( vlc_object_t * );
86 static void Close ( vlc_object_t * );
88 static void Play ( aout_instance_t * );
89 static void* OSSThread ( vlc_object_t * );
91 static mtime_t BufferDuration( aout_instance_t * p_aout );
93 /*****************************************************************************
95 *****************************************************************************/
97 set_shortname( "OSS" )
98 set_description( N_("Open Sound System") )
100 set_category( CAT_AUDIO )
101 set_subcategory( SUBCAT_AUDIO_AOUT )
102 add_loadfile( "oss-audio-device", "/dev/dsp",
103 N_("OSS DSP device"), NULL, false )
104 add_deprecated_alias( "dspdev" ) /* deprecated since 0.9.3 */
106 set_capability( "audio output", 100 )
107 add_shortcut( "oss" )
108 set_callbacks( Open, Close )
111 /*****************************************************************************
112 * Probe: probe the audio device for available formats and channels
113 *****************************************************************************/
114 static void Probe( aout_instance_t * p_aout )
116 struct aout_sys_t * p_sys = p_aout->output.p_sys;
117 vlc_value_t val, text;
118 int i_format, i_nb_channels;
120 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
121 text.psz_string = _("Audio Device");
122 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
124 /* Test for multi-channel. */
125 #ifdef SNDCTL_DSP_GETCHANNELMASK
126 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
128 /* Check that the device supports this. */
133 i_format = AFMT_S16_NE;
134 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
135 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
137 msg_Err( p_aout, "cannot reset OSS audio device" );
138 var_Destroy( p_aout, "audio-device" );
142 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
145 if ( !(i_chanmask & DSP_BIND_FRONT) )
147 msg_Err( p_aout, "no front channels! (%x)",
152 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
153 && (p_aout->output.output.i_physical_channels ==
154 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
155 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
158 val.i_int = AOUT_VAR_5_1;
159 text.psz_string = (char*) "5.1";
160 var_Change( p_aout, "audio-device",
161 VLC_VAR_ADDCHOICE, &val, &text );
164 if ( (i_chanmask & DSP_BIND_SURR)
165 && (p_aout->output.output.i_physical_channels &
166 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
167 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
169 val.i_int = AOUT_VAR_2F2R;
170 text.psz_string = _("2 Front 2 Rear");
171 var_Change( p_aout, "audio-device",
172 VLC_VAR_ADDCHOICE, &val, &text );
179 i_format = AFMT_S16_NE;
180 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
181 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
183 msg_Err( p_aout, "cannot reset OSS audio device" );
184 var_Destroy( p_aout, "audio-device" );
188 /* Test for stereo. */
190 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
191 && i_nb_channels == 2 )
193 val.i_int = AOUT_VAR_STEREO;
194 text.psz_string = _("Stereo");
195 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
199 i_format = AFMT_S16_NE;
200 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
201 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
203 msg_Err( p_aout, "cannot reset OSS audio device" );
204 var_Destroy( p_aout, "audio-device" );
210 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
211 && i_nb_channels == 1 )
213 val.i_int = AOUT_VAR_MONO;
214 text.psz_string = _("Mono");
215 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
216 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
218 var_Set( p_aout, "audio-device", val );
222 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
224 msg_Err( p_aout, "cannot reset OSS audio device" );
225 var_Destroy( p_aout, "audio-device" );
229 /* Test for spdif. */
230 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
234 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
235 && i_format == AFMT_AC3 )
237 val.i_int = AOUT_VAR_SPDIF;
238 text.psz_string = _("A/52 over S/PDIF");
239 var_Change( p_aout, "audio-device",
240 VLC_VAR_ADDCHOICE, &val, &text );
241 if( var_InheritBool( p_aout, "spdif" ) )
242 var_Set( p_aout, "audio-device", val );
244 else if( var_InheritBool( p_aout, "spdif" ) )
246 msg_Warn( p_aout, "S/PDIF not supported by card" );
250 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
254 /*****************************************************************************
255 * Open: open the audio device (the digital sound processor)
256 *****************************************************************************
257 * This function opens the DSP as a usual non-blocking write-only file, and
258 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
259 *****************************************************************************/
260 static int Open( vlc_object_t *p_this )
262 aout_instance_t * p_aout = (aout_instance_t *)p_this;
263 struct aout_sys_t * p_sys;
267 /* Allocate structure */
268 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
272 /* Get device name */
273 if( (psz_device = var_InheritString( p_aout, "oss-audio-device" )) == NULL )
275 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
280 /* Open the sound device in non-blocking mode, because ALSA's OSS
281 * emulation and some broken OSS drivers would make a blocking call
282 * wait forever until the device is available. Since this breaks the
283 * OSS spec, we immediately put it back to blocking mode if the
284 * operation was successful. */
285 p_sys->i_fd = vlc_open( psz_device, O_WRONLY | O_NDELAY );
286 if( p_sys->i_fd < 0 )
288 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
294 /* if the opening was ok, put the device back in blocking mode */
295 fcntl( p_sys->i_fd, F_SETFL,
296 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
300 p_aout->output.pf_play = Play;
302 if ( var_Type( p_aout, "audio-device" ) == 0 )
307 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
309 /* Probe() has failed. */
310 close( p_sys->i_fd );
315 if ( val.i_int == AOUT_VAR_SPDIF )
317 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
319 else if ( val.i_int == AOUT_VAR_5_1 )
321 p_aout->output.output.i_format = VLC_CODEC_S16N;
322 p_aout->output.output.i_physical_channels
323 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
324 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
327 else if ( val.i_int == AOUT_VAR_2F2R )
329 p_aout->output.output.i_format = VLC_CODEC_S16N;
330 p_aout->output.output.i_physical_channels
331 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
332 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
334 else if ( val.i_int == AOUT_VAR_STEREO )
336 p_aout->output.output.i_format = VLC_CODEC_S16N;
337 p_aout->output.output.i_physical_channels
338 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
340 else if ( val.i_int == AOUT_VAR_MONO )
342 p_aout->output.output.i_format = VLC_CODEC_S16N;
343 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
347 /* This should not happen ! */
348 msg_Err( p_aout, "internal: can't find audio-device (%"PRId64")",
350 close( p_sys->i_fd );
355 var_TriggerCallback( p_aout, "intf-change" );
357 /* Reset the DSP device */
358 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
360 msg_Err( p_aout, "cannot reset OSS audio device" );
361 close( p_sys->i_fd );
366 /* Set the output format */
367 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
369 int i_format = AFMT_AC3;
371 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
372 || i_format != AFMT_AC3 )
374 msg_Err( p_aout, "cannot reset OSS audio device" );
375 close( p_sys->i_fd );
380 p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
381 p_aout->output.i_nb_samples = A52_FRAME_NB;
382 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
383 p_aout->output.output.i_frame_length = A52_FRAME_NB;
385 aout_VolumeNoneInit( p_aout );
388 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
390 unsigned int i_format = AFMT_S16_NE;
391 unsigned int i_frame_size, i_fragments;
393 unsigned int i_nb_channels;
394 audio_buf_info audio_buf;
396 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
398 msg_Err( p_aout, "cannot set audio output format" );
399 close( p_sys->i_fd );
407 p_aout->output.output.i_format = VLC_CODEC_U8;
410 p_aout->output.output.i_format = VLC_CODEC_S8;
413 p_aout->output.output.i_format = VLC_CODEC_U16L;
416 p_aout->output.output.i_format = VLC_CODEC_S16L;
419 p_aout->output.output.i_format = VLC_CODEC_U16B;
422 p_aout->output.output.i_format = VLC_CODEC_S16B;
425 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
427 close( p_sys->i_fd );
432 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
434 /* Set the number of channels */
435 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
436 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
438 msg_Err( p_aout, "cannot set number of audio channels (%s)",
439 aout_FormatPrintChannels( &p_aout->output.output) );
440 close( p_sys->i_fd );
445 /* Set the output rate */
446 i_rate = p_aout->output.output.i_rate;
447 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
449 msg_Err( p_aout, "cannot set audio output rate (%i)",
450 p_aout->output.output.i_rate );
451 close( p_sys->i_fd );
456 if( i_rate != p_aout->output.output.i_rate )
458 p_aout->output.output.i_rate = i_rate;
461 /* Set the fragment size */
462 aout_FormatPrepare( &p_aout->output.output );
464 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
465 * 1 << yyyy is fragsize */
466 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;
468 while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
472 i_fragments |= FRAME_COUNT << 16;
473 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
475 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
478 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
480 msg_Err( p_aout, "cannot get fragment size" );
481 close( p_sys->i_fd );
487 /* Number of fragments actually allocated */
488 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
490 /* Maximum duration the soundcard's buffer can hold */
491 p_aout->output.p_sys->max_buffer_duration =
492 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
493 / p_aout->output.output.i_bytes_per_frame
494 / p_aout->output.output.i_rate
495 * p_aout->output.output.i_frame_length;
497 p_aout->output.i_nb_samples = audio_buf.fragsize /
498 p_aout->output.output.i_bytes_per_frame;
501 aout_VolumeSoftInit( p_aout );
504 /* Create OSS thread and wait for its readiness. */
505 if( vlc_thread_create( p_aout, OSSThread,
506 VLC_THREAD_PRIORITY_OUTPUT ) )
508 msg_Err( p_aout, "cannot create OSS thread (%m)" );
509 close( p_sys->i_fd );
517 /*****************************************************************************
518 * Play: nothing to do
519 *****************************************************************************/
520 static void Play( aout_instance_t *p_aout )
525 /*****************************************************************************
526 * Close: close the DSP audio device
527 *****************************************************************************/
528 static void Close( vlc_object_t * p_this )
530 aout_instance_t *p_aout = (aout_instance_t *)p_this;
531 struct aout_sys_t * p_sys = p_aout->output.p_sys;
533 vlc_object_kill( p_aout );
534 vlc_thread_join( p_aout );
535 p_aout->b_die = false;
537 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
538 close( p_sys->i_fd );
543 /*****************************************************************************
544 * BufferDuration: buffer status query
545 *****************************************************************************
546 * This function returns the duration in microseconds of the current buffer.
547 *****************************************************************************/
548 static mtime_t BufferDuration( aout_instance_t * p_aout )
550 struct aout_sys_t * p_sys = p_aout->output.p_sys;
551 audio_buf_info audio_buf;
554 /* Fill the audio_buf_info structure:
555 * - fragstotal: total number of fragments allocated
556 * - fragsize: size of a fragment in bytes
557 * - bytes: available space in bytes (includes partially used fragments)
558 * Note! 'bytes' could be more than fragments*fragsize */
559 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
561 /* calculate number of available fragments (not partially used ones) */
562 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
564 /* Return the fragment duration */
565 return (mtime_t)i_bytes * 1000000
566 / p_aout->output.output.i_bytes_per_frame
567 / p_aout->output.output.i_rate
568 * p_aout->output.output.i_frame_length;
571 /*****************************************************************************
572 * OSSThread: asynchronous thread used to DMA the data to the device
573 *****************************************************************************/
574 static void* OSSThread( vlc_object_t *p_this )
576 aout_instance_t * p_aout = (aout_instance_t*)p_this;
577 struct aout_sys_t * p_sys = p_aout->output.p_sys;
578 mtime_t next_date = 0;
579 int canc = vlc_savecancel ();
581 while ( vlc_object_alive (p_aout) )
583 aout_buffer_t * p_buffer = NULL;
587 if ( p_aout->output.output.i_format != VLC_CODEC_SPDIFL )
589 mtime_t buffered = BufferDuration( p_aout );
591 /* Next buffer will be played at mdate() + buffered */
592 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
595 if( p_buffer == NULL &&
596 buffered > ( p_aout->output.p_sys->max_buffer_duration
597 / p_aout->output.p_sys->i_fragstotal ) )
599 /* If we have at least a fragment full, then we can wait a
600 * little and retry to get a new audio buffer instead of
601 * playing a blank sample */
602 msleep( ( p_aout->output.p_sys->max_buffer_duration
603 / p_aout->output.p_sys->i_fragstotal / 2 ) );
609 /* emu10k1 driver does not report Buffer Duration correctly in
610 * passthrough mode so we have to cheat */
617 mtime_t delay = next_date - mdate();
618 if( delay > AOUT_PTS_TOLERANCE )
624 while( vlc_object_alive (p_aout) && ! ( p_buffer =
625 aout_OutputNextBuffer( p_aout, next_date, true ) ) )
627 msleep( VLC_HARD_MIN_SLEEP );
632 if ( p_buffer != NULL )
634 p_bytes = p_buffer->p_buffer;
635 i_size = p_buffer->i_buffer;
636 /* This is theoretical ... we'll see next iteration whether
638 next_date += p_buffer->i_length;
642 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
643 * p_aout->output.output.i_bytes_per_frame;
644 p_bytes = malloc( i_size );
645 memset( p_bytes, 0, i_size );
649 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
653 msg_Err( p_aout, "write failed (%m)" );
656 if ( p_buffer != NULL )
658 aout_BufferFree( p_buffer );
666 vlc_restorecancel (canc);