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-09-22 04:33:28 +02:00
|
|
|
#include <unistd.h>
|
2002-09-01 00:41:29 +02:00
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
#include "config.h"
|
2002-09-01 00:41:29 +02:00
|
|
|
|
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
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
// sanitizes ai structure before calling other functions
|
2013-12-21 20:24:20 +01:00
|
|
|
int audio_in_init(audio_in_t *ai, struct mp_log *log, int type)
|
2002-08-22 00:50:40 +02:00
|
|
|
{
|
|
|
|
ai->type = type;
|
|
|
|
ai->setup = 0;
|
2013-12-21 20:24:20 +01:00
|
|
|
ai->log = log;
|
2002-08-22 00:50:40 +02:00
|
|
|
|
|
|
|
ai->channels = -1;
|
|
|
|
ai->samplerate = -1;
|
|
|
|
ai->blocksize = -1;
|
|
|
|
ai->bytes_per_sample = -1;
|
|
|
|
ai->samplesize = -1;
|
|
|
|
|
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->alsa.handle = NULL;
|
|
|
|
ai->alsa.log = NULL;
|
|
|
|
ai->alsa.device = strdup("default");
|
|
|
|
return 0;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->oss.audio_fd = -1;
|
|
|
|
ai->oss.device = strdup("/dev/dsp");
|
|
|
|
return 0;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->sndio.hdl = NULL;
|
|
|
|
ai->sndio.device = strdup("default");
|
|
|
|
return 0;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_setup(audio_in_t *ai)
|
|
|
|
{
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
if (ai_alsa_init(ai) < 0) return -1;
|
|
|
|
ai->setup = 1;
|
|
|
|
return 0;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
if (ai_oss_init(ai) < 0) return -1;
|
|
|
|
ai->setup = 1;
|
|
|
|
return 0;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
2014-04-13 18:00:51 +02:00
|
|
|
if (ai_sndio_init(ai) < 0) return -1;
|
|
|
|
ai->setup = 1;
|
|
|
|
return 0;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_set_samplerate(audio_in_t *ai, int rate)
|
|
|
|
{
|
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->req_samplerate = rate;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_alsa_setup(ai) < 0) return -1;
|
|
|
|
return ai->samplerate;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->req_samplerate = rate;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_oss_set_samplerate(ai) < 0) return -1;
|
|
|
|
return ai->samplerate;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->req_samplerate = rate;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_sndio_setup(ai) < 0) return -1;
|
|
|
|
return ai->samplerate;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_set_channels(audio_in_t *ai, int channels)
|
|
|
|
{
|
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->req_channels = channels;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_alsa_setup(ai) < 0) return -1;
|
|
|
|
return ai->channels;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
ai->req_channels = channels;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_oss_set_channels(ai) < 0) return -1;
|
|
|
|
return ai->channels;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
|
|
|
ai->req_channels = channels;
|
|
|
|
if (!ai->setup) return 0;
|
|
|
|
if (ai_sndio_setup(ai) < 0) return -1;
|
|
|
|
return ai->channels;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_set_device(audio_in_t *ai, char *device)
|
|
|
|
{
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
int i;
|
2002-11-23 11:58:14 +01:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
if (ai->setup) return -1;
|
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
free(ai->alsa.device);
|
|
|
|
ai->alsa.device = strdup(device);
|
|
|
|
/* mplayer cannot handle colons in arguments */
|
|
|
|
for (i = 0; i < (int)strlen(ai->alsa.device); i++) {
|
|
|
|
if (ai->alsa.device[i] == '.') ai->alsa.device[i] = ':';
|
|
|
|
}
|
|
|
|
return 0;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
free(ai->oss.device);
|
|
|
|
ai->oss.device = strdup(device);
|
|
|
|
return 0;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
|
|
|
if (ai->sndio.device) free(ai->sndio.device);
|
|
|
|
ai->sndio.device = strdup(device);
|
|
|
|
return 0;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_uninit(audio_in_t *ai)
|
|
|
|
{
|
|
|
|
if (ai->setup) {
|
2014-04-13 18:00:51 +02:00
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2014-04-13 18:00:51 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
|
|
|
if (ai->alsa.log)
|
|
|
|
snd_output_close(ai->alsa.log);
|
|
|
|
if (ai->alsa.handle) {
|
|
|
|
snd_pcm_close(ai->alsa.handle);
|
|
|
|
}
|
|
|
|
ai->setup = 0;
|
|
|
|
return 0;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2014-04-13 18:00:51 +02:00
|
|
|
case AUDIO_IN_OSS:
|
|
|
|
close(ai->oss.audio_fd);
|
|
|
|
ai->setup = 0;
|
|
|
|
return 0;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
|
|
|
if (ai->sndio.hdl)
|
|
|
|
sio_close(ai->sndio.hdl);
|
|
|
|
ai->setup = 0;
|
|
|
|
return 0;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2014-04-13 18:00:51 +02:00
|
|
|
}
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
2002-09-22 04:33:28 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_start_capture(audio_in_t *ai)
|
|
|
|
{
|
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
return snd_pcm_start(ai->alsa.handle);
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
return 0;
|
2013-09-28 18:01:12 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
|
|
|
if (!sio_start(ai->sndio.hdl))
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int audio_in_read_chunk(audio_in_t *ai, unsigned char *buffer)
|
|
|
|
{
|
|
|
|
int ret;
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2002-08-22 00:50:40 +02:00
|
|
|
switch (ai->type) {
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_ALSA
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_ALSA:
|
2014-04-13 18:00:51 +02:00
|
|
|
ret = snd_pcm_readi(ai->alsa.handle, buffer, ai->alsa.chunk_size);
|
|
|
|
if (ret != ai->alsa.chunk_size) {
|
|
|
|
if (ret < 0) {
|
|
|
|
MP_ERR(ai, "\nError reading audio: %s\n", snd_strerror(ret));
|
|
|
|
if (ret == -EPIPE) {
|
|
|
|
if (ai_alsa_xrun(ai) == 0) {
|
|
|
|
MP_ERR(ai, "Recovered from cross-run, some frames may be left out!\n");
|
|
|
|
} else {
|
|
|
|
MP_ERR(ai, "Fatal error, cannot recover!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MP_ERR(ai, "\nNot enough audio samples!\n");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
2002-08-22 00:50:40 +02:00
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_OSS_AUDIO
|
2002-08-22 00:50:40 +02:00
|
|
|
case AUDIO_IN_OSS:
|
2014-04-13 18:00:51 +02:00
|
|
|
ret = read(ai->oss.audio_fd, buffer, ai->blocksize);
|
2013-09-28 18:01:12 +02:00
|
|
|
if (ret != ai->blocksize) {
|
|
|
|
if (ret < 0) {
|
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
|
|
|
MP_ERR(ai, "\nError reading audio: %s\n", mp_strerror(errno));
|
2013-09-28 18:01:12 +02:00
|
|
|
|
|
|
|
} else {
|
2013-12-21 20:24:20 +01:00
|
|
|
MP_ERR(ai, "\nNot enough audio samples!\n");
|
2013-09-28 18:01:12 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
#endif
|
2013-07-16 13:28:28 +02:00
|
|
|
#if HAVE_SNDIO
|
2013-09-28 18:01:12 +02:00
|
|
|
case AUDIO_IN_SNDIO:
|
|
|
|
ret = sio_read(ai->sndio.hdl, buffer, ai->blocksize);
|
2014-04-13 18:00:51 +02:00
|
|
|
if (ret != ai->blocksize) {
|
|
|
|
if (ret < 0) {
|
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
|
|
|
MP_ERR(ai, "\nError reading audio: %s\n", mp_strerror(errno));
|
2014-04-13 18:00:51 +02:00
|
|
|
} else {
|
|
|
|
MP_ERR(ai, "\nNot enough audio samples!\n");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
2002-10-22 17:14:30 +02:00
|
|
|
#endif
|
2002-08-22 00:50:40 +02:00
|
|
|
default:
|
2014-04-13 18:00:51 +02:00
|
|
|
return -1;
|
2002-08-22 00:50:40 +02:00
|
|
|
}
|
|
|
|
}
|