2009-01-26 15:06:44 +00:00
|
|
|
/*
|
|
|
|
* null audio output driver
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2009-01-26 15:06:44 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2009-01-26 15:06:44 +00:00
|
|
|
*
|
audio/out: change license of some core files to LGPL
All contributors of the current code have agreed. ao.c requires a
"driver" entry for each audio output - we assume that if someone who
didn't agree to LGPL added a line, it's fine for ao.c to be LGPL
anyway. If the affected audio output is not disabled at compilation
time, the resulting binary will be GPL anyway, and ootherwise the
code is not included.
The audio output code itself was inspired or partially copied from
libao in 7a2eec4b59f4 (thus why MPlayer's audio code is named libao2).
Just to be sure we got permission from Aaron Holtzman, Jack Moffitt, and
Stan Seibert, who according to libao's SVN history and README are the
initial author. (Something similar was done for libvo, although the
commit relicensing it forgot to mention it.)
242aa6ebd40: anders mostly disagreed with the LGPL relicensing, but we
got permission for this particular commit.
0ef8e555735: nick could not be reached, but the include statement was
removed again anyway.
879e05a7c17: iive agreed to LGPL v3+ only, but this line of code was
removed anyway, so ao_null.c can be LGPL v2.1+.
9dd8f241ac2: patch author could not be reached, but the corresponding
code (old slave mode interface) was completely removed later.
2017-05-20 09:35:25 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-26 15:06:44 +00:00
|
|
|
*/
|
|
|
|
|
2013-11-17 15:08:39 +00:00
|
|
|
/*
|
|
|
|
* Note: this does much more than just ignoring audio output. It simulates
|
|
|
|
* (to some degree) an ideal AO.
|
|
|
|
*/
|
|
|
|
|
2001-06-02 23:25:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-01-30 20:30:54 +00:00
|
|
|
#include <math.h>
|
2011-05-04 14:03:55 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2005-01-12 22:00:02 +00:00
|
|
|
#include "config.h"
|
2011-05-04 14:03:55 +00:00
|
|
|
#include "osdep/timer.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2014-04-17 20:35:05 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
struct priv {
|
2013-11-13 18:39:18 +00:00
|
|
|
bool paused;
|
2013-05-25 16:31:06 +00:00
|
|
|
double last_time;
|
2013-11-17 15:08:39 +00:00
|
|
|
bool playing_final;
|
|
|
|
float buffered; // samples
|
|
|
|
int buffersize; // samples
|
2013-11-13 18:47:41 +00:00
|
|
|
|
|
|
|
int untimed;
|
2013-11-17 15:08:39 +00:00
|
|
|
float bufferlen; // seconds
|
2014-03-07 14:24:49 +00:00
|
|
|
float speed; // multiplier
|
2014-05-04 14:54:47 +00:00
|
|
|
float latency_sec; // seconds
|
|
|
|
float latency; // samples
|
2014-04-17 20:35:05 +00:00
|
|
|
int broken_eof;
|
2015-01-30 20:30:54 +00:00
|
|
|
int broken_delay;
|
2013-11-17 15:08:39 +00:00
|
|
|
|
|
|
|
// Minimal unit of audio samples that can be written at once. If play() is
|
|
|
|
// called with sizes not aligned to this, a rounded size will be returned.
|
|
|
|
// (This is not needed by the AO API, but many AOs behave this way.)
|
|
|
|
int outburst; // samples
|
2015-04-27 21:18:54 +00:00
|
|
|
|
2016-08-05 10:23:42 +00:00
|
|
|
struct m_channels channel_layouts;
|
2018-01-29 05:08:52 +00:00
|
|
|
int format;
|
2001-06-02 23:25:43 +00:00
|
|
|
};
|
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2013-11-13 18:47:41 +00:00
|
|
|
if (ao->untimed) {
|
|
|
|
priv->buffered = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 18:39:18 +00:00
|
|
|
if (priv->paused)
|
|
|
|
return;
|
|
|
|
|
2013-05-25 16:31:06 +00:00
|
|
|
double now = mp_time_sec();
|
2013-11-17 15:08:39 +00:00
|
|
|
if (priv->buffered > 0) {
|
2014-03-07 14:24:49 +00:00
|
|
|
priv->buffered -= (now - priv->last_time) * ao->samplerate * priv->speed;
|
2013-11-17 15:08:39 +00:00
|
|
|
if (priv->buffered < 0) {
|
|
|
|
if (!priv->playing_final)
|
|
|
|
MP_ERR(ao, "buffer underrun\n");
|
|
|
|
priv->buffered = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-05-04 14:03:55 +00:00
|
|
|
priv->last_time = now;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2011-05-04 14:03:55 +00:00
|
|
|
{
|
2013-11-13 18:47:41 +00:00
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
2018-01-29 05:08:52 +00:00
|
|
|
if (priv->format)
|
|
|
|
ao->format = priv->format;
|
|
|
|
|
2013-11-13 18:47:41 +00:00
|
|
|
ao->untimed = priv->untimed;
|
2013-05-09 16:06:26 +00:00
|
|
|
|
2015-04-27 21:18:54 +00:00
|
|
|
struct mp_chmap_sel sel = {.tmp = ao};
|
2016-08-05 10:23:42 +00:00
|
|
|
if (priv->channel_layouts.num_chmaps) {
|
|
|
|
for (int n = 0; n < priv->channel_layouts.num_chmaps; n++)
|
|
|
|
mp_chmap_sel_add_map(&sel, &priv->channel_layouts.chmaps[n]);
|
2015-04-27 21:18:54 +00:00
|
|
|
} else {
|
|
|
|
mp_chmap_sel_add_any(&sel);
|
|
|
|
}
|
2013-05-09 16:06:26 +00:00
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
2014-07-26 18:26:57 +00:00
|
|
|
mp_chmap_from_channels(&ao->channels, 2);
|
2013-05-09 16:06:26 +00:00
|
|
|
|
2014-05-04 14:54:47 +00:00
|
|
|
priv->latency = priv->latency_sec * ao->samplerate;
|
|
|
|
|
2013-11-17 15:08:39 +00:00
|
|
|
// A "buffer" for this many seconds of audio
|
|
|
|
int bursts = (int)(ao->samplerate * priv->bufferlen + 1) / priv->outburst;
|
2014-04-17 20:35:05 +00:00
|
|
|
priv->buffersize = priv->outburst * bursts + priv->latency;
|
2013-11-10 22:38:41 +00:00
|
|
|
|
2013-05-25 16:31:06 +00:00
|
|
|
priv->last_time = mp_time_sec();
|
2011-05-04 14:03:55 +00:00
|
|
|
|
2017-06-25 13:57:15 +00:00
|
|
|
ao->period_size = priv->outburst;
|
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
return 0;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wait_drain(struct ao *ao)
|
2011-05-04 14:03:55 +00:00
|
|
|
{
|
2013-11-17 15:08:39 +00:00
|
|
|
struct priv *priv = ao->priv;
|
2014-03-08 23:49:39 +00:00
|
|
|
drain(ao);
|
|
|
|
if (!priv->paused)
|
2014-03-07 14:24:49 +00:00
|
|
|
mp_sleep_us(1000000.0 * priv->buffered / ao->samplerate / priv->speed);
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop playing and empty buffers (for seeking/pause)
|
2011-05-04 14:03:55 +00:00
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2013-11-10 22:38:41 +00:00
|
|
|
priv->buffered = 0;
|
2013-11-17 15:08:39 +00:00
|
|
|
priv->playing_final = false;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
2001-06-05 18:40:44 +00:00
|
|
|
// stop playing, keep buffers (for pause)
|
2011-05-04 14:03:55 +00:00
|
|
|
static void pause(struct ao *ao)
|
2001-06-05 18:40:44 +00:00
|
|
|
{
|
2013-11-13 18:39:18 +00:00
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
2013-11-17 15:08:39 +00:00
|
|
|
drain(ao);
|
2013-11-13 18:39:18 +00:00
|
|
|
priv->paused = true;
|
2001-06-05 18:40:44 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 18:39:18 +00:00
|
|
|
// resume playing, after pause()
|
2011-05-04 14:03:55 +00:00
|
|
|
static void resume(struct ao *ao)
|
2001-06-05 18:40:44 +00:00
|
|
|
{
|
2013-11-13 18:39:18 +00:00
|
|
|
struct priv *priv = ao->priv;
|
|
|
|
|
|
|
|
drain(ao);
|
|
|
|
priv->paused = false;
|
|
|
|
priv->last_time = mp_time_sec();
|
2001-06-05 18:40:44 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
drain(ao);
|
2014-09-06 10:59:00 +00:00
|
|
|
int samples = priv->buffersize - priv->latency - priv->buffered;
|
|
|
|
return samples / priv->outburst * priv->outburst;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 22:24:21 +00:00
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
2011-05-04 14:03:55 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2013-11-17 15:08:39 +00:00
|
|
|
int accepted;
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2013-11-13 18:39:18 +00:00
|
|
|
resume(ao);
|
2013-11-17 15:08:39 +00:00
|
|
|
|
2014-04-17 20:35:05 +00:00
|
|
|
if (priv->buffered <= 0)
|
|
|
|
priv->buffered = priv->latency; // emulate fixed latency
|
|
|
|
|
2013-11-17 15:08:39 +00:00
|
|
|
priv->playing_final = flags & AOPLAY_FINAL_CHUNK;
|
|
|
|
if (priv->playing_final) {
|
|
|
|
// Last audio chunk - don't round to outburst.
|
|
|
|
accepted = MPMIN(priv->buffersize - priv->buffered, samples);
|
|
|
|
} else {
|
|
|
|
int maxbursts = (priv->buffersize - priv->buffered) / priv->outburst;
|
|
|
|
int playbursts = samples / priv->outburst;
|
|
|
|
int bursts = playbursts > maxbursts ? maxbursts : playbursts;
|
|
|
|
accepted = bursts * priv->outburst;
|
|
|
|
}
|
|
|
|
priv->buffered += accepted;
|
|
|
|
return accepted;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 10:45:04 +00:00
|
|
|
static double get_delay(struct ao *ao)
|
2011-05-04 14:03:55 +00:00
|
|
|
{
|
|
|
|
struct priv *priv = ao->priv;
|
2001-06-02 23:25:43 +00:00
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
drain(ao);
|
2014-03-07 14:24:49 +00:00
|
|
|
|
|
|
|
// Note how get_delay returns the delay in audio device time (instead of
|
|
|
|
// adjusting for speed), since most AOs seem to also do that.
|
2014-05-04 14:54:47 +00:00
|
|
|
double delay = priv->buffered;
|
2014-04-17 20:35:05 +00:00
|
|
|
|
|
|
|
// Drivers with broken EOF handling usually always report the same device-
|
|
|
|
// level delay that is additional to the buffer time.
|
|
|
|
if (priv->broken_eof && priv->buffered < priv->latency)
|
|
|
|
delay = priv->latency;
|
|
|
|
|
2015-01-30 20:30:54 +00:00
|
|
|
delay /= ao->samplerate;
|
|
|
|
|
|
|
|
if (priv->broken_delay) { // Report only multiples of outburst
|
|
|
|
double q = priv->outburst / (double)ao->samplerate;
|
|
|
|
if (delay > 0)
|
|
|
|
delay = (int)(delay / q) * q;
|
|
|
|
}
|
|
|
|
|
|
|
|
return delay;
|
2001-06-02 23:25:43 +00:00
|
|
|
}
|
2011-05-04 14:03:55 +00:00
|
|
|
|
2013-11-13 18:47:41 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2011-05-04 14:03:55 +00:00
|
|
|
const struct ao_driver audio_out_null = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "Null audio output",
|
|
|
|
.name = "null",
|
2011-05-04 14:03:55 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.reset = reset,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
|
|
|
.get_delay = get_delay,
|
|
|
|
.pause = pause,
|
|
|
|
.resume = resume,
|
2014-03-08 23:49:39 +00:00
|
|
|
.drain = wait_drain,
|
2013-11-13 18:47:41 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
2013-11-17 15:08:39 +00:00
|
|
|
.priv_defaults = &(const struct priv) {
|
2013-11-19 21:14:23 +00:00
|
|
|
.bufferlen = 0.2,
|
2013-11-17 15:08:39 +00:00
|
|
|
.outburst = 256,
|
2014-03-07 14:24:49 +00:00
|
|
|
.speed = 1,
|
2013-11-17 15:08:39 +00:00
|
|
|
},
|
2013-11-13 18:47:41 +00:00
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_FLAG("untimed", untimed, 0),
|
2013-11-17 15:08:39 +00:00
|
|
|
OPT_FLOATRANGE("buffer", bufferlen, 0, 0, 100),
|
|
|
|
OPT_INTRANGE("outburst", outburst, 0, 1, 100000),
|
2014-03-07 14:24:49 +00:00
|
|
|
OPT_FLOATRANGE("speed", speed, 0, 0, 10000),
|
2014-05-04 14:54:47 +00:00
|
|
|
OPT_FLOATRANGE("latency", latency_sec, 0, 0, 100),
|
2014-04-17 20:35:05 +00:00
|
|
|
OPT_FLAG("broken-eof", broken_eof, 0),
|
2015-01-30 20:30:54 +00:00
|
|
|
OPT_FLAG("broken-delay", broken_delay, 0),
|
2016-08-05 10:23:42 +00:00
|
|
|
OPT_CHANNELS("channel-layouts", channel_layouts, 0),
|
2018-01-29 05:08:52 +00:00
|
|
|
OPT_AUDIOFORMAT("format", format, 0),
|
2013-11-13 18:47:41 +00:00
|
|
|
{0}
|
|
|
|
},
|
2016-11-25 20:00:39 +00:00
|
|
|
.options_prefix = "ao-null",
|
2011-05-04 14:03:55 +00:00
|
|
|
};
|