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 *****************************************************************************/
76 mtime_t max_buffer_duration;
82 /* This must be a power of 2. */
83 #define FRAME_SIZE 1024
84 #define FRAME_COUNT 32
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
92 static void* OSSThread ( void * );
94 static mtime_t BufferDuration( audio_output_t * p_aout );
98 /*****************************************************************************
100 *****************************************************************************/
102 set_shortname( "OSS" )
103 set_description( N_("Open Sound System") )
105 set_category( CAT_AUDIO )
106 set_subcategory( SUBCAT_AUDIO_AOUT )
107 add_loadfile( "oss-audio-device", "/dev/dsp",
108 N_("OSS DSP device"), NULL, false )
111 set_capability( "audio output", 100 )
112 add_shortcut( "oss" )
113 set_callbacks( Open, Close )
116 /*****************************************************************************
117 * Probe: probe the audio device for available formats and channels
118 *****************************************************************************/
119 static void Probe( audio_output_t * p_aout )
121 struct aout_sys_t * p_sys = p_aout->sys;
122 vlc_value_t val, text;
123 int i_format, i_nb_channels;
125 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
126 text.psz_string = _("Audio Device");
127 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
129 /* Test for multi-channel. */
130 #ifdef SNDCTL_DSP_GETCHANNELMASK
131 if ( aout_FormatNbChannels( &p_aout->format ) > 2 )
133 /* Check that the device supports this. */
138 i_format = AFMT_S16_NE;
139 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
140 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
142 msg_Err( p_aout, "cannot reset OSS audio device" );
143 var_Destroy( p_aout, "audio-device" );
147 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
150 if ( !(i_chanmask & DSP_BIND_FRONT) )
152 msg_Err( p_aout, "no front channels! (%x)",
157 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
158 && (p_aout->format.i_physical_channels ==
159 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
160 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
163 val.i_int = AOUT_VAR_5_1;
164 text.psz_string = (char*) "5.1";
165 var_Change( p_aout, "audio-device",
166 VLC_VAR_ADDCHOICE, &val, &text );
169 if ( (i_chanmask & DSP_BIND_SURR)
170 && (p_aout->format.i_physical_channels &
171 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
172 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
174 val.i_int = AOUT_VAR_2F2R;
175 text.psz_string = _("2 Front 2 Rear");
176 var_Change( p_aout, "audio-device",
177 VLC_VAR_ADDCHOICE, &val, &text );
184 i_format = AFMT_S16_NE;
185 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
186 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
188 msg_Err( p_aout, "cannot reset OSS audio device" );
189 var_Destroy( p_aout, "audio-device" );
193 /* Test for stereo. */
195 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
196 && i_nb_channels == 2 )
198 val.i_int = AOUT_VAR_STEREO;
199 text.psz_string = _("Stereo");
200 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
204 i_format = AFMT_S16_NE;
205 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
206 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
208 msg_Err( p_aout, "cannot reset OSS audio device" );
209 var_Destroy( p_aout, "audio-device" );
215 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
216 && i_nb_channels == 1 )
218 val.i_int = AOUT_VAR_MONO;
219 text.psz_string = _("Mono");
220 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
221 if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER )
223 var_Set( p_aout, "audio-device", val );
227 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
229 msg_Err( p_aout, "cannot reset OSS audio device" );
230 var_Destroy( p_aout, "audio-device" );
234 /* Test for spdif. */
235 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
239 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
240 && i_format == AFMT_AC3 )
242 val.i_int = AOUT_VAR_SPDIF;
243 text.psz_string = _("A/52 over S/PDIF");
244 var_Change( p_aout, "audio-device",
245 VLC_VAR_ADDCHOICE, &val, &text );
246 if( var_InheritBool( p_aout, "spdif" ) )
247 var_Set( p_aout, "audio-device", val );
249 else if( var_InheritBool( p_aout, "spdif" ) )
251 msg_Warn( p_aout, "S/PDIF not supported by card" );
256 /*****************************************************************************
257 * Open: open the audio device (the digital sound processor)
258 *****************************************************************************
259 * This function opens the DSP as a usual non-blocking write-only file, and
260 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
261 *****************************************************************************/
262 static int Open( vlc_object_t *p_this )
264 audio_output_t * p_aout = (audio_output_t *)p_this;
265 struct aout_sys_t * p_sys;
269 /* Allocate structure */
270 p_aout->sys = p_sys = malloc( sizeof( aout_sys_t ) );
274 /* Get device name */
275 if( (psz_device = var_InheritString( p_aout, "oss-audio-device" )) == NULL )
277 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
282 /* Open the sound device in non-blocking mode, because ALSA's OSS
283 * emulation and some broken OSS drivers would make a blocking call
284 * wait forever until the device is available. Since this breaks the
285 * OSS spec, we immediately put it back to blocking mode if the
286 * operation was successful. */
287 p_sys->i_fd = vlc_open( psz_device, O_WRONLY | O_NDELAY );
288 if( p_sys->i_fd < 0 )
290 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
296 /* if the opening was ok, put the device back in blocking mode */
297 fcntl( p_sys->i_fd, F_SETFL,
298 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
302 p_aout->pf_play = aout_PacketPlay;
303 p_aout->pf_pause = aout_PacketPause;
304 p_aout->pf_flush = aout_PacketFlush;
306 if ( var_Type( p_aout, "audio-device" ) == 0 )
308 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
310 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
311 /* Probe() has failed. */
314 if ( val.i_int == AOUT_VAR_SPDIF )
316 p_aout->format.i_format = VLC_CODEC_SPDIFL;
318 else if ( val.i_int == AOUT_VAR_5_1 )
320 p_aout->format.i_format = VLC_CODEC_S16N;
321 p_aout->format.i_physical_channels
322 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
323 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
326 else if ( val.i_int == AOUT_VAR_2F2R )
328 p_aout->format.i_format = VLC_CODEC_S16N;
329 p_aout->format.i_physical_channels
330 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
331 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
333 else if ( val.i_int == AOUT_VAR_STEREO )
335 p_aout->format.i_format = VLC_CODEC_S16N;
336 p_aout->format.i_physical_channels
337 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
339 else if ( val.i_int == AOUT_VAR_MONO )
341 p_aout->format.i_format = VLC_CODEC_S16N;
342 p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
346 /* This should not happen ! */
347 msg_Err( p_aout, "internal: can't find audio-device (%"PRId64")",
352 /* Reset the DSP device */
353 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
355 msg_Err( p_aout, "cannot reset OSS audio device" );
359 /* Set the output format */
360 if ( AOUT_FMT_SPDIF( &p_aout->format ) )
362 int i_format = AFMT_AC3;
364 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
365 || i_format != AFMT_AC3 )
367 msg_Err( p_aout, "cannot reset OSS audio device" );
371 p_aout->format.i_format = VLC_CODEC_SPDIFL;
372 p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
373 p_aout->format.i_frame_length = A52_FRAME_NB;
375 aout_PacketInit( p_aout, &p_sys->packet, A52_FRAME_NB );
376 p_aout->volume_set = NULL;
377 p_aout->mute_set = NULL;
381 unsigned int i_format = AFMT_S16_NE;
382 unsigned int i_frame_size, i_fragments;
384 unsigned int i_nb_channels;
385 audio_buf_info audio_buf;
387 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
389 msg_Err( p_aout, "cannot set audio output format" );
396 p_aout->format.i_format = VLC_CODEC_U8;
399 p_aout->format.i_format = VLC_CODEC_S8;
402 p_aout->format.i_format = VLC_CODEC_U16L;
405 p_aout->format.i_format = VLC_CODEC_S16L;
408 p_aout->format.i_format = VLC_CODEC_U16B;
411 p_aout->format.i_format = VLC_CODEC_S16B;
414 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
419 i_nb_channels = aout_FormatNbChannels( &p_aout->format );
421 /* Set the number of channels */
422 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
423 i_nb_channels != aout_FormatNbChannels( &p_aout->format ) )
425 msg_Err( p_aout, "cannot set number of audio channels (%s)",
426 aout_FormatPrintChannels( &p_aout->format) );
430 /* Set the output rate */
431 i_rate = p_aout->format.i_rate;
432 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
434 msg_Err( p_aout, "cannot set audio output rate (%i)",
435 p_aout->format.i_rate );
439 if( i_rate != p_aout->format.i_rate )
441 p_aout->format.i_rate = i_rate;
444 /* Set the fragment size */
445 aout_FormatPrepare( &p_aout->format );
447 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
448 * 1 << yyyy is fragsize */
449 i_frame_size = ((uint64_t)p_aout->format.i_bytes_per_frame * p_aout->format.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
451 while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
455 i_fragments |= FRAME_COUNT << 16;
456 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
458 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
461 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
463 msg_Err( p_aout, "cannot get fragment size" );
467 /* Number of fragments actually allocated */
468 p_aout->sys->i_fragstotal = audio_buf.fragstotal;
470 /* Maximum duration the soundcard's buffer can hold */
471 p_aout->sys->max_buffer_duration =
472 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
473 / p_aout->format.i_bytes_per_frame
474 / p_aout->format.i_rate
475 * p_aout->format.i_frame_length;
477 aout_PacketInit( p_aout, &p_sys->packet,
478 audio_buf.fragsize/p_aout->format.i_bytes_per_frame );
479 aout_SoftVolumeInit( p_aout );
482 /* Create OSS thread and wait for its readiness. */
483 if( vlc_clone( &p_sys->thread, OSSThread, p_aout,
484 VLC_THREAD_PRIORITY_OUTPUT ) )
486 msg_Err( p_aout, "cannot create OSS thread (%m)" );
487 aout_PacketDestroy( p_aout );
494 var_DelCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
495 close( p_sys->i_fd );
500 /*****************************************************************************
501 * Close: close the DSP audio device
502 *****************************************************************************/
503 static void Close( vlc_object_t * p_this )
505 audio_output_t *p_aout = (audio_output_t *)p_this;
506 struct aout_sys_t * p_sys = p_aout->sys;
508 vlc_cancel( p_sys->thread );
509 vlc_join( p_sys->thread, NULL );
510 p_aout->b_die = false;
511 var_DelCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
513 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
514 close( p_sys->i_fd );
516 aout_PacketDestroy( p_aout );
520 /*****************************************************************************
521 * BufferDuration: buffer status query
522 *****************************************************************************
523 * This function returns the duration in microseconds of the current buffer.
524 *****************************************************************************/
525 static mtime_t BufferDuration( audio_output_t * p_aout )
527 struct aout_sys_t * p_sys = p_aout->sys;
528 audio_buf_info audio_buf;
531 #ifdef SNDCTL_DSP_GETODELAY
532 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETODELAY, &i_bytes ) < 0 )
535 /* Fall back to GETOSPACE and approximate latency. */
537 /* Fill the audio_buf_info structure:
538 * - fragstotal: total number of fragments allocated
539 * - fragsize: size of a fragment in bytes
540 * - bytes: available space in bytes (includes partially used fragments)
541 * Note! 'bytes' could be more than fragments*fragsize */
542 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
544 /* calculate number of available fragments (not partially used ones) */
545 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
548 /* Return the fragment duration */
549 return (mtime_t)i_bytes * 1000000
550 / p_aout->format.i_bytes_per_frame
551 / p_aout->format.i_rate
552 * p_aout->format.i_frame_length;
561 static void OSSThreadCleanup( void *data )
563 oss_thread_ctx_t *p_ctx = data;
564 if( p_ctx->p_buffer )
565 block_Release( p_ctx->p_buffer );
567 free( p_ctx->p_bytes );
570 /*****************************************************************************
571 * OSSThread: asynchronous thread used to DMA the data to the device
572 *****************************************************************************/
573 static void* OSSThread( void *obj )
575 audio_output_t * p_aout = (audio_output_t*)obj;
576 struct aout_sys_t * p_sys = p_aout->sys;
577 mtime_t next_date = 0;
581 block_t * p_buffer = NULL;
583 int canc = vlc_savecancel ();
584 if ( p_aout->format.i_format != VLC_CODEC_SPDIFL )
586 mtime_t buffered = BufferDuration( p_aout );
588 /* Next buffer will be played at mdate() + buffered */
589 p_buffer = aout_PacketNext( p_aout, mdate() + buffered );
591 if( p_buffer == NULL &&
592 buffered > ( p_aout->sys->max_buffer_duration
593 / p_aout->sys->i_fragstotal ) )
595 vlc_restorecancel (canc);
596 /* If we have at least a fragment full, then we can wait a
597 * little and retry to get a new audio buffer instead of
598 * playing a blank sample */
599 msleep( ( p_aout->sys->max_buffer_duration
600 / p_aout->sys->i_fragstotal / 2 ) );
606 vlc_restorecancel (canc);
608 /* emu10k1 driver does not report Buffer Duration correctly in
609 * passthrough mode so we have to cheat */
616 mtime_t delay = next_date - mdate();
617 if( delay > AOUT_MAX_PTS_ADVANCE )
625 canc = vlc_savecancel ();
626 p_buffer = aout_PacketNext( p_aout, next_date );
629 vlc_restorecancel (canc);
631 msleep( VLC_HARD_MIN_SLEEP );
638 if ( p_buffer != NULL )
640 p_bytes = p_buffer->p_buffer;
641 i_size = p_buffer->i_buffer;
642 /* This is theoretical ... we'll see next iteration whether
644 next_date += p_buffer->i_length;
648 i_size = FRAME_SIZE / p_aout->format.i_frame_length
649 * p_aout->format.i_bytes_per_frame;
650 p_bytes = malloc( i_size );
651 memset( p_bytes, 0, i_size );
655 oss_thread_ctx_t ctx = {
656 .p_buffer = p_buffer,
660 vlc_cleanup_push( OSSThreadCleanup, &ctx );
661 vlc_restorecancel( canc );
663 int i_tmp = write( p_sys->i_fd, p_bytes, i_size );
667 msg_Err( p_aout, "write failed (%m)" );