2014-03-08 23:04:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "ao.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "audio/format.h"
|
|
|
|
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/common.h"
|
|
|
|
|
2014-04-15 20:38:16 +00:00
|
|
|
#include "input/input.h"
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
#include "osdep/threads.h"
|
2014-04-17 20:50:49 +00:00
|
|
|
#include "osdep/timer.h"
|
2014-03-08 23:04:37 +00:00
|
|
|
#include "compat/atomics.h"
|
|
|
|
|
|
|
|
#include "audio/audio.h"
|
|
|
|
#include "audio/audio_buffer.h"
|
|
|
|
|
|
|
|
struct ao_push_state {
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_mutex_t lock;
|
2014-04-15 20:38:16 +00:00
|
|
|
|
|
|
|
// uses a separate lock to avoid lock order issues with ao_need_data()
|
|
|
|
pthread_mutex_t wakeup_lock;
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_cond_t wakeup;
|
|
|
|
|
2014-04-15 20:38:16 +00:00
|
|
|
// --- protected by lock
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
struct mp_audio_buffer *buffer;
|
|
|
|
|
|
|
|
bool terminate;
|
|
|
|
bool playing;
|
|
|
|
|
|
|
|
// Whether the current buffer contains the complete audio.
|
|
|
|
bool final_chunk;
|
2014-04-17 20:50:49 +00:00
|
|
|
double expected_end_time;
|
2014-04-15 20:38:16 +00:00
|
|
|
|
|
|
|
// -- protected by wakeup_lock
|
|
|
|
bool need_wakeup;
|
2014-03-08 23:04:37 +00:00
|
|
|
};
|
|
|
|
|
2014-04-15 20:38:16 +00:00
|
|
|
static void wakeup_playthread(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->wakeup_lock);
|
|
|
|
p->need_wakeup = true;
|
|
|
|
pthread_cond_signal(&p->wakeup);
|
|
|
|
pthread_mutex_unlock(&p->wakeup_lock);
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:04:37 +00:00
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
|
|
|
{
|
|
|
|
int r = CONTROL_UNKNOWN;
|
|
|
|
if (ao->driver->control) {
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
r = ao->driver->control(ao, cmd, arg);
|
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float get_delay(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
double driver_delay = 0;
|
|
|
|
if (ao->driver->get_delay)
|
|
|
|
driver_delay = ao->driver->get_delay(ao);
|
|
|
|
double delay = driver_delay + mp_audio_buffer_seconds(p->buffer);
|
|
|
|
pthread_mutex_unlock(&p->lock);
|
2014-04-17 20:50:49 +00:00
|
|
|
if (delay >= AO_EOF_DELAY && p->expected_end_time) {
|
|
|
|
if (mp_time_sec() > p->expected_end_time) {
|
|
|
|
MP_ERR(ao, "Audio device EOF reporting is broken!\n");
|
|
|
|
MP_ERR(ao, "Please report this problem.\n");
|
|
|
|
delay = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 23:04:37 +00:00
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
if (ao->driver->reset)
|
|
|
|
ao->driver->reset(ao);
|
|
|
|
mp_audio_buffer_clear(p->buffer);
|
|
|
|
p->playing = false;
|
2014-04-15 20:38:16 +00:00
|
|
|
wakeup_playthread(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pause(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
if (ao->driver->pause)
|
|
|
|
ao->driver->pause(ao);
|
|
|
|
p->playing = false;
|
2014-04-15 20:38:16 +00:00
|
|
|
wakeup_playthread(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
if (ao->driver->resume)
|
|
|
|
ao->driver->resume(ao);
|
|
|
|
p->playing = true; // tentatively
|
2014-04-17 20:50:49 +00:00
|
|
|
p->expected_end_time = 0;
|
2014-04-15 20:38:16 +00:00
|
|
|
wakeup_playthread(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
if (ao->driver->drain) {
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
ao->driver->drain(ao);
|
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
} else {
|
|
|
|
ao_wait_drain(ao);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2014-05-11 18:51:49 +00:00
|
|
|
static int unlocked_get_space(struct ao *ao)
|
2014-03-08 23:04:37 +00:00
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
audio/out: reduce amount of audio buffering
Since the addition of the AO feed thread, 200ms of latency (MIN_BUFFER)
was added to all push-based AOs. This is not so nice, because even AOs
with relatively small buffering (e.g. ao_alsa on my system with ~170ms
of buffer size), the additional latency becomes noticable when e.g.
toggling mute with softvol.
Fix this by trying to keep not only 200ms minimum buffer, but also 200ms
maximum buffer. In other words, never buffer beyond 200ms in total. Do
this by estimating the AO's buffer fill status using get_space and the
initially known AO buffer size (the get_space return value on
initialization, before any audio was played). We limit the maximum
amount of data written to the soft buffer so that soft buffer size and
audio buffer size equal to 200ms (MIN_BUFFER).
To avoid weird problems with weird AOs, we buffer beyond MIN_BUFFER if
the AO's get_space requests more data than that, and as long as the soft
buffer is large enough.
Note that this is just a hack to improve the latency. When the audio
chain gains the ability to refilter data, this won't be needed anymore,
and instead we can introduce some sort of buffer replacement function in
order to update data in the soft buffer.
2014-03-10 00:13:40 +00:00
|
|
|
int space = mp_audio_buffer_get_write_available(p->buffer);
|
|
|
|
if (ao->driver->get_space) {
|
|
|
|
// The following code attempts to keep the total buffered audio to
|
|
|
|
// MIN_BUFFER in order to improve latency.
|
|
|
|
int device_space = ao->driver->get_space(ao);
|
|
|
|
int device_buffered = ao->device_buffer - device_space;
|
|
|
|
int soft_buffered = mp_audio_buffer_samples(p->buffer);
|
|
|
|
int min_buffer = MIN_BUFFER * ao->samplerate;
|
|
|
|
int missing = min_buffer - device_buffered - soft_buffered;
|
|
|
|
// But always keep the device's buffer filled as much as we can.
|
|
|
|
int device_missing = device_space - soft_buffered;
|
|
|
|
missing = MPMAX(missing, device_missing);
|
|
|
|
space = MPMIN(space, missing);
|
|
|
|
space = MPMAX(0, space);
|
|
|
|
}
|
2014-05-11 18:51:49 +00:00
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_space(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
int space = unlocked_get_space(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
audio/out: reduce amount of audio buffering
Since the addition of the AO feed thread, 200ms of latency (MIN_BUFFER)
was added to all push-based AOs. This is not so nice, because even AOs
with relatively small buffering (e.g. ao_alsa on my system with ~170ms
of buffer size), the additional latency becomes noticable when e.g.
toggling mute with softvol.
Fix this by trying to keep not only 200ms minimum buffer, but also 200ms
maximum buffer. In other words, never buffer beyond 200ms in total. Do
this by estimating the AO's buffer fill status using get_space and the
initially known AO buffer size (the get_space return value on
initialization, before any audio was played). We limit the maximum
amount of data written to the soft buffer so that soft buffer size and
audio buffer size equal to 200ms (MIN_BUFFER).
To avoid weird problems with weird AOs, we buffer beyond MIN_BUFFER if
the AO's get_space requests more data than that, and as long as the soft
buffer is large enough.
Note that this is just a hack to improve the latency. When the audio
chain gains the ability to refilter data, this won't be needed anymore,
and instead we can introduce some sort of buffer replacement function in
order to update data in the soft buffer.
2014-03-10 00:13:40 +00:00
|
|
|
return space;
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
|
|
|
|
int write_samples = mp_audio_buffer_get_write_available(p->buffer);
|
|
|
|
write_samples = MPMIN(write_samples, samples);
|
|
|
|
|
|
|
|
struct mp_audio audio;
|
|
|
|
mp_audio_buffer_get_format(p->buffer, &audio);
|
|
|
|
for (int n = 0; n < ao->num_planes; n++)
|
|
|
|
audio.planes[n] = data[n];
|
|
|
|
audio.samples = write_samples;
|
|
|
|
mp_audio_buffer_append(p->buffer, &audio);
|
|
|
|
|
|
|
|
p->final_chunk = !!(flags & AOPLAY_FINAL_CHUNK);
|
|
|
|
p->playing = true;
|
2014-04-17 20:50:49 +00:00
|
|
|
p->expected_end_time = 0;
|
2014-03-08 23:04:37 +00:00
|
|
|
|
2014-04-15 20:38:16 +00:00
|
|
|
wakeup_playthread(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
return write_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called locked
|
|
|
|
static int ao_play_data(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
struct mp_audio data;
|
|
|
|
mp_audio_buffer_peek(p->buffer, &data);
|
|
|
|
int max = data.samples;
|
|
|
|
int space = ao->driver->get_space ? ao->driver->get_space(ao) : INT_MAX;
|
|
|
|
if (data.samples > space)
|
|
|
|
data.samples = space;
|
|
|
|
if (data.samples <= 0)
|
|
|
|
return 0;
|
2014-05-11 14:26:23 +00:00
|
|
|
MP_STATS(ao, "start ao fill");
|
2014-03-08 23:04:37 +00:00
|
|
|
int flags = 0;
|
|
|
|
if (p->final_chunk && data.samples == max)
|
|
|
|
flags |= AOPLAY_FINAL_CHUNK;
|
|
|
|
int r = ao->driver->play(ao, data.planes, data.samples, flags);
|
|
|
|
if (r > data.samples) {
|
2014-05-11 14:26:23 +00:00
|
|
|
MP_WARN(ao, "Audio device returned non-sense value.\n");
|
2014-03-08 23:04:37 +00:00
|
|
|
r = data.samples;
|
|
|
|
}
|
2014-05-11 18:51:49 +00:00
|
|
|
if (r > 0)
|
2014-03-08 23:04:37 +00:00
|
|
|
mp_audio_buffer_skip(p->buffer, r);
|
2014-04-17 20:50:49 +00:00
|
|
|
if (p->final_chunk && mp_audio_buffer_samples(p->buffer) == 0) {
|
2014-03-08 23:04:37 +00:00
|
|
|
p->playing = false;
|
2014-04-17 20:50:49 +00:00
|
|
|
p->expected_end_time = mp_time_sec() + AO_EOF_DELAY + 0.25; // + margin
|
|
|
|
if (ao->driver->get_delay)
|
|
|
|
p->expected_end_time += ao->driver->get_delay(ao);
|
|
|
|
}
|
2014-05-11 14:26:23 +00:00
|
|
|
MP_STATS(ao, "end ao fill");
|
2014-03-08 23:04:37 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *playthread(void *arg)
|
|
|
|
{
|
|
|
|
struct ao *ao = arg;
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
2014-04-15 20:38:16 +00:00
|
|
|
while (1) {
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
if (p->terminate) {
|
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-08 23:04:37 +00:00
|
|
|
double timeout = 2.0;
|
|
|
|
if (p->playing) {
|
|
|
|
int r = ao_play_data(ao);
|
|
|
|
// The device buffers are not necessarily full, but writing to the
|
|
|
|
// AO buffer will wake up this thread anyway.
|
|
|
|
bool buffers_full = r <= 0;
|
|
|
|
// We have to estimate when the AO needs data again.
|
|
|
|
if (buffers_full && ao->driver->get_delay) {
|
|
|
|
float buffered_audio = ao->driver->get_delay(ao);
|
|
|
|
timeout = buffered_audio - 0.050;
|
2014-04-15 20:38:16 +00:00
|
|
|
// Keep extra safety margin if the buffers are large
|
|
|
|
if (timeout > 0.100)
|
2014-03-08 23:04:37 +00:00
|
|
|
timeout = MPMAX(timeout - 0.200, 0.100);
|
|
|
|
} else {
|
2014-04-15 20:38:16 +00:00
|
|
|
timeout = 0;
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
2014-04-15 20:38:16 +00:00
|
|
|
// Half of the buffer played -> wakeup playback thread to get more.
|
2014-05-04 18:41:00 +00:00
|
|
|
double min_wait = ao->device_buffer / (double)ao->samplerate;
|
2014-05-11 18:51:49 +00:00
|
|
|
if (timeout <= min_wait / 2 + 0.001 && unlocked_get_space(ao) > 0)
|
2014-04-15 20:38:16 +00:00
|
|
|
mp_input_wakeup(ao->input_ctx);
|
|
|
|
// Avoid wasting CPU - this assumes ao_play_data() usually fills the
|
|
|
|
// audio buffer as far as possible, so even if the device buffer
|
|
|
|
// is not full, we can only wait for the core.
|
2014-05-04 18:41:00 +00:00
|
|
|
timeout = MPMAX(timeout, min_wait * 0.75);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
2014-04-15 20:38:16 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
2014-05-11 14:26:23 +00:00
|
|
|
MP_STATS(ao, "start audio wait");
|
2014-04-15 20:38:16 +00:00
|
|
|
pthread_mutex_lock(&p->wakeup_lock);
|
|
|
|
if (!p->need_wakeup)
|
2014-04-23 18:56:39 +00:00
|
|
|
mpthread_cond_timedwait(&p->wakeup, &p->wakeup_lock, timeout);
|
2014-04-15 20:38:16 +00:00
|
|
|
p->need_wakeup = false;
|
|
|
|
pthread_mutex_unlock(&p->wakeup_lock);
|
2014-05-11 14:26:23 +00:00
|
|
|
MP_STATS(ao, "end audio wait");
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2014-03-08 23:04:37 +00:00
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&p->lock);
|
|
|
|
p->terminate = true;
|
2014-04-15 20:38:16 +00:00
|
|
|
wakeup_playthread(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_mutex_unlock(&p->lock);
|
|
|
|
|
|
|
|
pthread_join(p->thread, NULL);
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
ao->driver->uninit(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
|
|
|
|
pthread_cond_destroy(&p->wakeup);
|
|
|
|
pthread_mutex_destroy(&p->lock);
|
2014-04-15 20:38:16 +00:00
|
|
|
pthread_mutex_destroy(&p->wakeup_lock);
|
2014-03-08 23:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int init(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct ao_push_state *p = ao->api_priv;
|
|
|
|
|
|
|
|
pthread_mutex_init(&p->lock, NULL);
|
2014-04-15 20:38:16 +00:00
|
|
|
pthread_mutex_init(&p->wakeup_lock, NULL);
|
2014-03-08 23:04:37 +00:00
|
|
|
pthread_cond_init(&p->wakeup, NULL);
|
|
|
|
|
|
|
|
p->buffer = mp_audio_buffer_create(ao);
|
|
|
|
mp_audio_buffer_reinit_fmt(p->buffer, ao->format,
|
|
|
|
&ao->channels, ao->samplerate);
|
|
|
|
mp_audio_buffer_preallocate_min(p->buffer, ao->buffer);
|
|
|
|
if (pthread_create(&p->thread, NULL, playthread, ao)) {
|
2014-03-08 23:49:39 +00:00
|
|
|
ao->driver->uninit(ao);
|
2014-03-08 23:04:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct ao_driver ao_api_push = {
|
|
|
|
.init = init,
|
|
|
|
.control = control,
|
|
|
|
.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 = drain,
|
2014-03-08 23:04:37 +00:00
|
|
|
.priv_size = sizeof(struct ao_push_state),
|
|
|
|
};
|
|
|
|
|
2014-04-15 20:38:16 +00:00
|
|
|
// Must be called locked.
|
2014-03-08 23:04:37 +00:00
|
|
|
int ao_play_silence(struct ao *ao, int samples)
|
|
|
|
{
|
|
|
|
assert(ao->api == &ao_api_push);
|
|
|
|
if (samples <= 0 || AF_FORMAT_IS_SPECIAL(ao->format) || !ao->driver->play)
|
|
|
|
return 0;
|
|
|
|
char *p = talloc_size(NULL, samples * ao->sstride);
|
|
|
|
af_fill_silence(p, samples * ao->sstride, ao->format);
|
|
|
|
void *tmp[MP_NUM_CHANNELS];
|
|
|
|
for (int n = 0; n < MP_NUM_CHANNELS; n++)
|
|
|
|
tmp[n] = p;
|
|
|
|
int r = ao->driver->play(ao, tmp, samples, 0);
|
|
|
|
talloc_free(p);
|
|
|
|
return r;
|
|
|
|
}
|
2014-04-15 20:38:16 +00:00
|
|
|
|
|
|
|
// Notify the core that new data should be sent to the AO. Normally, the core
|
|
|
|
// uses a heuristic based on ao_delay() when to refill the buffers, but this
|
|
|
|
// can be used to reduce wait times. Can be called from any thread.
|
|
|
|
void ao_need_data(struct ao *ao)
|
|
|
|
{
|
|
|
|
assert(ao->api == &ao_api_push);
|
|
|
|
|
|
|
|
// wakeup the play thread at least once
|
|
|
|
wakeup_playthread(ao);
|
|
|
|
}
|