From 49867bd432352d19172ab26cf873bd5651e69e25 Mon Sep 17 00:00:00 2001 From: ben Date: Mon, 31 Jul 2006 17:39:17 +0000 Subject: introduce new 'stream' directory for all stream layer related components and split them from libmpdemux git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@19277 b3059339-0415-0410-9bf9-f77b7e298cf2 --- stream/ai_oss.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 stream/ai_oss.c (limited to 'stream/ai_oss.c') diff --git a/stream/ai_oss.c b/stream/ai_oss.c new file mode 100644 index 0000000000..bd7c7c312a --- /dev/null +++ b/stream/ai_oss.c @@ -0,0 +1,139 @@ +#include +#include + +#include "config.h" + +#include /* strerror */ +#include +#include +#include + +#ifdef HAVE_SYS_SOUNDCARD_H +#include +#else +#ifdef HAVE_SOUNDCARD_H +#include +#else +#include +#endif +#endif + +#include "audio_in.h" +#include "mp_msg.h" +#include "help_mp.h" + +int ai_oss_set_samplerate(audio_in_t *ai) +{ + int tmp = ai->req_samplerate; + if (ioctl(ai->oss.audio_fd, SNDCTL_DSP_SPEED, &tmp) == -1) return -1; + ai->samplerate = tmp; + return 0; +} + +int ai_oss_set_channels(audio_in_t *ai) +{ + int err; + int ioctl_param; + + if (ai->req_channels > 2) + { + ioctl_param = ai->req_channels; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp channels: %d\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param)); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetChanCount, + ai->req_channels); + return -1; + } + ai->channels = ioctl_param; + } + else + { + ioctl_param = (ai->req_channels == 2); + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp stereo: %d (req: %d)\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_STEREO, &ioctl_param), + ioctl_param); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetStereo, + ai->req_channels == 2); + return -1; + } + ai->channels = ioctl_param ? 2 : 1; + } + return 0; +} + +int ai_oss_init(audio_in_t *ai) +{ + int err; + int ioctl_param; + + ai->oss.audio_fd = open(ai->oss.device, O_RDONLY); + if (ai->oss.audio_fd < 0) + { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2Open, + ai->oss.device, strerror(errno)); + return -1; + } + + ioctl_param = 0 ; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp getfmt: %d\n", + ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param)); + + mp_msg(MSGT_TV, MSGL_V, "Supported formats: %x\n", ioctl_param); + if (!(ioctl_param & AFMT_S16_LE)) + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_UnsupportedFmt); + + ioctl_param = AFMT_S16_LE; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp setfmt: %d\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param)); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetAudioFmt); + return -1; + } + + if (ai_oss_set_channels(ai) < 0) return -1; + + ioctl_param = ai->req_samplerate; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp speed: %d\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SPEED, &ioctl_param)); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetSamplerate, + ai->req_samplerate); + return -1; + } + ai->samplerate = ioctl_param; + + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp trigger: %d\n", + ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETTRIGGER, &ioctl_param)); + mp_msg(MSGT_TV, MSGL_V, "trigger: %x\n", ioctl_param); + ioctl_param = PCM_ENABLE_INPUT; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp trigger: %d\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param)); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2SetTrigger, + PCM_ENABLE_INPUT); + } + + ai->blocksize = 0; + mp_msg(MSGT_TV, MSGL_V, "ioctl dsp getblocksize: %d\n", + err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETBLKSIZE, &ai->blocksize)); + if (err < 0) { + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_Unable2GetBlockSize); + } + mp_msg(MSGT_TV, MSGL_V, "blocksize: %d\n", ai->blocksize); + + // correct the blocksize to a reasonable value + if (ai->blocksize <= 0) { + ai->blocksize = 4096*ai->channels*2; + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_AudioBlockSizeZero, ai->blocksize); + } else if (ai->blocksize < 4096*ai->channels*2) { + ai->blocksize *= 4096*ai->channels*2/ai->blocksize; + mp_msg(MSGT_TV, MSGL_ERR, MSGTR_MPDEMUX_AIOSS_AudioBlockSize2Low, ai->blocksize); + } + + ai->samplesize = 16; + ai->bytes_per_sample = 2; + + return 0; +} -- cgit v1.2.3