2010-01-30 23:26:47 +01:00
|
|
|
/*
|
2015-04-13 09:36:54 +02:00
|
|
|
* This file is part of mpv.
|
2010-01-30 23:26:47 +01:00
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2010-01-30 23:26:47 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2015-04-13 09:36:54 +02:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 23:26:47 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2015-04-13 09:36:54 +02:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-30 23:26:47 +01:00
|
|
|
*/
|
|
|
|
|
2002-09-01 00:41:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
#include "config.h"
|
2002-08-26 19:20:17 +02:00
|
|
|
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
#include <string.h>
|
2002-09-01 00:41:29 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SYS_SOUNDCARD_H
|
2002-08-22 10:05:27 +02:00
|
|
|
#include <sys/soundcard.h>
|
|
|
|
#else
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SOUNDCARD_H
|
2002-08-22 10:05:27 +02:00
|
|
|
#include <soundcard.h>
|
|
|
|
#else
|
2002-08-22 00:50:40 +02:00
|
|
|
#include <linux/soundcard.h>
|
2002-08-22 10:05:27 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
|
2013-11-30 22:40:51 +01:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
#include "audio_in.h"
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 21:21:56 +01:00
|
|
|
#include "common/common.h"
|
2013-12-17 02:39:45 +01:00
|
|
|
#include "common/msg.h"
|
2002-08-22 00:50:40 +02:00
|
|
|
|
|
|
|
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;
|
2004-01-11 11:01:18 +01:00
|
|
|
ai->samplerate = tmp;
|
2002-08-22 00:50:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ai_oss_set_channels(audio_in_t *ai)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int ioctl_param;
|
|
|
|
|
|
|
|
if (ai->req_channels > 2)
|
|
|
|
{
|
2014-04-13 18:00:51 +02:00
|
|
|
ioctl_param = ai->req_channels;
|
|
|
|
MP_VERBOSE(ai, "ioctl dsp channels: %d\n",
|
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_CHANNELS, &ioctl_param));
|
|
|
|
if (err < 0) {
|
|
|
|
MP_ERR(ai, "Unable to set channel count: %d\n",
|
|
|
|
ai->req_channels);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ai->channels = ioctl_param;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-13 18:00:51 +02:00
|
|
|
ioctl_param = (ai->req_channels == 2);
|
|
|
|
MP_VERBOSE(ai, "ioctl dsp stereo: %d (req: %d)\n",
|
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_STEREO, &ioctl_param),
|
|
|
|
ioctl_param);
|
|
|
|
if (err < 0) {
|
|
|
|
MP_ERR(ai, "Unable to set stereo: %d\n",
|
|
|
|
ai->req_channels == 2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ai->channels = ioctl_param ? 2 : 1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ai_oss_init(audio_in_t *ai)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int ioctl_param;
|
|
|
|
|
2015-10-30 15:51:26 +01:00
|
|
|
const char *device = ai->oss.device;
|
|
|
|
if (!device)
|
|
|
|
device = "/dev/dsp";
|
|
|
|
|
|
|
|
ai->oss.audio_fd = open(device, O_RDONLY | O_CLOEXEC);
|
2002-08-22 00:50:40 +02:00
|
|
|
if (ai->oss.audio_fd < 0)
|
|
|
|
{
|
2015-10-30 15:51:26 +01:00
|
|
|
MP_ERR(ai, "Unable to open '%s': %s\n", device, mp_strerror(errno));
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
ioctl_param = 0 ;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp getfmt: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETFMTS, &ioctl_param));
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "Supported formats: %x\n", ioctl_param);
|
2014-09-23 21:04:37 +02:00
|
|
|
if (!(ioctl_param & AFMT_S16_NE))
|
2014-04-13 18:00:51 +02:00
|
|
|
MP_ERR(ai, "unsupported format\n");
|
2002-08-22 00:50:40 +02:00
|
|
|
|
2014-09-23 21:04:37 +02:00
|
|
|
ioctl_param = AFMT_S16_NE;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp setfmt: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETFMT, &ioctl_param));
|
2002-08-22 00:50:40 +02:00
|
|
|
if (err < 0) {
|
2014-04-13 18:00:51 +02:00
|
|
|
MP_ERR(ai, "Unable to set audio format.");
|
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ai_oss_set_channels(ai) < 0) return -1;
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
ioctl_param = ai->req_samplerate;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp speed: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SPEED, &ioctl_param));
|
2002-08-22 00:50:40 +02:00
|
|
|
if (err < 0) {
|
2014-04-13 18:00:51 +02:00
|
|
|
MP_ERR(ai, "Unable to set samplerate: %d\n",
|
|
|
|
ai->req_samplerate);
|
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
2004-01-29 13:43:54 +01:00
|
|
|
ai->samplerate = ioctl_param;
|
2002-08-22 00:50:40 +02:00
|
|
|
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp trigger: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETTRIGGER, &ioctl_param));
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "trigger: %x\n", ioctl_param);
|
2002-08-22 00:50:40 +02:00
|
|
|
ioctl_param = PCM_ENABLE_INPUT;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp trigger: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_SETTRIGGER, &ioctl_param));
|
2002-08-22 00:50:40 +02:00
|
|
|
if (err < 0) {
|
2014-04-13 18:00:51 +02:00
|
|
|
MP_ERR(ai, "Unable to set trigger: %d\n",
|
|
|
|
PCM_ENABLE_INPUT);
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ai->blocksize = 0;
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "ioctl dsp getblocksize: %d\n",
|
2014-04-13 18:00:51 +02:00
|
|
|
err = ioctl(ai->oss.audio_fd, SNDCTL_DSP_GETBLKSIZE, &ai->blocksize));
|
2002-08-22 00:50:40 +02:00
|
|
|
if (err < 0) {
|
2014-04-13 18:00:51 +02:00
|
|
|
MP_ERR(ai, "Unable to get block size!\n");
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_VERBOSE(ai, "blocksize: %d\n", ai->blocksize);
|
2002-08-22 00:50:40 +02:00
|
|
|
|
|
|
|
// correct the blocksize to a reasonable value
|
|
|
|
if (ai->blocksize <= 0) {
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->blocksize = 4096*ai->channels*2;
|
|
|
|
MP_ERR(ai, "Audio block size is zero, setting to %d!\n", ai->blocksize);
|
2002-08-22 00:50:40 +02:00
|
|
|
} else if (ai->blocksize < 4096*ai->channels*2) {
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->blocksize *= 4096*ai->channels*2/ai->blocksize;
|
|
|
|
MP_ERR(ai, "Audio block size too low, setting to %d!\n", ai->blocksize);
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ai->samplesize = 16;
|
|
|
|
ai->bytes_per_sample = 2;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|