-/*****************************************************************************
- * OpenAudioDev:
- *****************************************************************************/
-static int OpenAudioDev( demux_t *p_demux, char *psz_device )
-{
- demux_sys_t *p_sys = p_demux->p_sys;
- int i_fd, i_format;
-
- if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
- {
- msg_Err( p_demux, "cannot open audio device (%m)" );
- goto adev_fail;
- }
-
- i_format = AFMT_S16_LE;
- if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
- || i_format != AFMT_S16_LE )
- {
- msg_Err( p_demux, "cannot set audio format (16b little endian) "
- "(%m)" );
- goto adev_fail;
- }
-
- if( ioctl( i_fd, SNDCTL_DSP_STEREO,
- &p_sys->b_stereo ) < 0 )
- {
- msg_Err( p_demux, "cannot set audio channels count (%m)" );
- goto adev_fail;
- }
-
- if( ioctl( i_fd, SNDCTL_DSP_SPEED,
- &p_sys->i_sample_rate ) < 0 )
- {
- msg_Err( p_demux, "cannot set audio sample rate (%m)" );
- goto adev_fail;
- }
-
- msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
- psz_device, p_sys->b_stereo ? "stereo" : "mono",
- p_sys->i_sample_rate );
-
- p_sys->i_audio_max_frame_size = 6 * 1024;
-
- return i_fd;
-
- adev_fail:
-
- if( i_fd >= 0 ) close( i_fd );
- return -1;
-}
-
-/*****************************************************************************
- * GrabAudio: grab audio
- *****************************************************************************/
-static block_t *GrabAudio( demux_t *p_demux )
-{
- demux_sys_t *p_sys = p_demux->p_sys;
- struct audio_buf_info buf_info;
- int i_read, i_correct;
- block_t *p_block;
-
- if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
- else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
-
- if( !p_block )
- {
- msg_Warn( p_demux, "cannot get block" );
- return 0;
- }
-
- p_sys->p_block_audio = p_block;
-
- i_read = read( p_sys->fd_audio, p_block->p_buffer,
- p_sys->i_audio_max_frame_size );
-
- if( i_read <= 0 ) return 0;
-
- p_block->i_buffer = i_read;
- p_sys->p_block_audio = 0;
-
- /* Correct the date because of kernel buffering */
- i_correct = i_read;
- if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
- {
- i_correct += buf_info.bytes;
- }
-
- p_block->i_pts = p_block->i_dts =
- mdate() - INT64_C(1000000) * (mtime_t)i_correct /
- 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
-
- return p_block;
-}
-