2008-09-07 14:09:51 +00:00
|
|
|
/*
|
|
|
|
* OpenAL audio output driver for MPlayer
|
2006-02-16 20:45:25 +00:00
|
|
|
*
|
2007-07-09 19:50:36 +00:00
|
|
|
* Copyleft 2006 by Reimar Döffinger (Reimar.Doeffinger@stud.uni-karlsruhe.de)
|
2008-09-07 14:09:51 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2008-09-07 14:09:51 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2008-09-07 14:09:51 +00: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 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2008-09-07 14:09:51 +00: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 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2006-02-16 20:45:25 +00:00
|
|
|
*/
|
|
|
|
|
2006-12-10 14:07:08 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-18 12:45:00 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2006-02-16 20:45:25 +00:00
|
|
|
#include <inttypes.h>
|
2006-12-10 14:07:08 +00:00
|
|
|
#ifdef OPENAL_AL_H
|
|
|
|
#include <OpenAL/alc.h>
|
|
|
|
#include <OpenAL/al.h>
|
2012-03-12 23:24:38 +00:00
|
|
|
#include <OpenAL/alext.h>
|
2006-12-10 14:07:08 +00:00
|
|
|
#else
|
2006-02-16 20:45:25 +00:00
|
|
|
#include <AL/alc.h>
|
|
|
|
#include <AL/al.h>
|
2012-03-12 23:24:38 +00:00
|
|
|
#include <AL/alext.h>
|
2006-12-10 14:07:08 +00:00
|
|
|
#endif
|
2006-02-16 20:45:25 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2006-02-16 20:45:25 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "ao.h"
|
2014-03-07 14:24:32 +00:00
|
|
|
#include "internal.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "audio/format.h"
|
2006-02-16 20:45:25 +00:00
|
|
|
#include "osdep/timer.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
2006-02-16 20:45:25 +00:00
|
|
|
|
2013-05-09 16:06:26 +00:00
|
|
|
#define MAX_CHANS MP_NUM_CHANNELS
|
2006-02-16 20:45:25 +00:00
|
|
|
#define NUM_BUF 128
|
|
|
|
#define CHUNK_SIZE 512
|
2013-11-10 22:39:22 +00:00
|
|
|
#define CHUNK_SAMPLES (CHUNK_SIZE / 2)
|
2006-02-16 20:45:25 +00:00
|
|
|
static ALuint buffers[MAX_CHANS][NUM_BUF];
|
|
|
|
static ALuint sources[MAX_CHANS];
|
|
|
|
|
|
|
|
static int cur_buf[MAX_CHANS];
|
|
|
|
static int unqueue_buf[MAX_CHANS];
|
|
|
|
|
2013-06-03 23:42:57 +00:00
|
|
|
static struct ao *ao_data;
|
|
|
|
|
2013-07-21 20:23:12 +00:00
|
|
|
struct priv {
|
|
|
|
char *cfg_device;
|
|
|
|
};
|
|
|
|
|
2013-06-03 23:42:57 +00:00
|
|
|
static void reset(struct ao *ao);
|
2006-02-16 20:45:25 +00:00
|
|
|
|
2013-06-03 23:42:57 +00:00
|
|
|
static int control(struct ao *ao, enum aocontrol cmd, void *arg)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
|
|
|
switch (cmd) {
|
2006-12-10 22:45:32 +00:00
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
case AOCONTROL_SET_VOLUME: {
|
2013-06-03 23:34:53 +00:00
|
|
|
ALfloat volume;
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
|
|
|
if (cmd == AOCONTROL_SET_VOLUME) {
|
|
|
|
volume = (vol->left + vol->right) / 200.0;
|
|
|
|
alListenerf(AL_GAIN, volume);
|
|
|
|
}
|
|
|
|
alGetListenerf(AL_GAIN, &volume);
|
|
|
|
vol->left = vol->right = volume * 100;
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
}
|
2014-09-06 00:30:57 +00:00
|
|
|
case AOCONTROL_HAS_SOFT_VOLUME:
|
|
|
|
return CONTROL_TRUE;
|
2006-12-10 22:45:32 +00:00
|
|
|
}
|
2013-06-03 23:34:53 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 19:03:36 +00:00
|
|
|
static int validate_device_opt(struct mp_log *log, const m_option_t *opt,
|
|
|
|
struct bstr name, struct bstr param)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
2013-07-21 20:23:12 +00:00
|
|
|
if (bstr_equals0(param, "help")) {
|
|
|
|
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_TRUE) {
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_fatal(log, "Device listing not supported.\n");
|
2013-07-21 20:23:12 +00:00
|
|
|
return M_OPT_EXIT;
|
|
|
|
}
|
|
|
|
const char *list = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_info(log, "OpenAL devices:\n");
|
2013-07-21 20:23:12 +00:00
|
|
|
while (list && *list) {
|
2013-12-21 19:03:36 +00:00
|
|
|
mp_info(log, " '%s'\n", list);
|
2013-07-21 20:23:12 +00:00
|
|
|
list = list + strlen(list) + 1;
|
|
|
|
}
|
|
|
|
return M_OPT_EXIT - 1;
|
2013-06-03 23:34:53 +00:00
|
|
|
}
|
2013-07-21 20:23:12 +00:00
|
|
|
return 0;
|
2012-03-12 23:24:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 14:42:56 +00:00
|
|
|
static void list_devs(struct ao *ao, struct ao_device_list *list)
|
|
|
|
{
|
|
|
|
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_TRUE)
|
|
|
|
return;
|
|
|
|
const char *devs = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
|
|
|
|
while (devs && *devs) {
|
|
|
|
ao_device_list_add(list, ao, &(struct ao_device_desc){devs, devs});
|
|
|
|
devs = devs + strlen(devs) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 10:10:09 +00:00
|
|
|
struct speaker {
|
|
|
|
int id;
|
|
|
|
float pos[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct speaker speaker_pos[] = {
|
|
|
|
{MP_SPEAKER_ID_FL, {-1, 0, 0.5}},
|
|
|
|
{MP_SPEAKER_ID_FR, { 1, 0, 0.5}},
|
|
|
|
{MP_SPEAKER_ID_FC, { 0, 0, 1}},
|
|
|
|
{MP_SPEAKER_ID_LFE, { 0, 0, 0.1}},
|
|
|
|
{MP_SPEAKER_ID_BL, {-1, 0, -1}},
|
|
|
|
{MP_SPEAKER_ID_BR, { 1, 0, -1}},
|
|
|
|
{MP_SPEAKER_ID_BC, { 0, 0, -1}},
|
|
|
|
{MP_SPEAKER_ID_SL, {-1, 0, 0}},
|
|
|
|
{MP_SPEAKER_ID_SR, { 1, 0, 0}},
|
|
|
|
{-1},
|
|
|
|
};
|
|
|
|
|
2013-07-22 20:57:51 +00:00
|
|
|
static int init(struct ao *ao)
|
2013-04-05 21:06:22 +00:00
|
|
|
{
|
2013-06-03 23:34:53 +00:00
|
|
|
float position[3] = {0, 0, 0};
|
|
|
|
float direction[6] = {0, 0, 1, 0, -1, 0};
|
|
|
|
ALCdevice *dev = NULL;
|
|
|
|
ALCcontext *ctx = NULL;
|
|
|
|
ALCint freq = 0;
|
2013-06-03 23:42:57 +00:00
|
|
|
ALCint attribs[] = {ALC_FREQUENCY, ao->samplerate, 0, 0};
|
2013-06-03 23:34:53 +00:00
|
|
|
int i;
|
2013-07-21 20:23:12 +00:00
|
|
|
struct priv *p = ao->priv;
|
2013-06-03 23:42:57 +00:00
|
|
|
if (ao_data) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_FATAL(ao, "Not reentrant!\n");
|
2013-06-03 23:42:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ao_data = ao;
|
2013-06-03 23:34:53 +00:00
|
|
|
struct mp_chmap_sel sel = {0};
|
|
|
|
for (i = 0; speaker_pos[i].id != -1; i++)
|
|
|
|
mp_chmap_sel_add_speaker(&sel, speaker_pos[i].id);
|
2013-06-03 23:42:57 +00:00
|
|
|
if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels))
|
2013-06-03 23:34:53 +00:00
|
|
|
goto err_out;
|
|
|
|
struct speaker speakers[MAX_CHANS];
|
2013-06-03 23:42:57 +00:00
|
|
|
for (i = 0; i < ao->channels.num; i++) {
|
2013-06-03 23:34:53 +00:00
|
|
|
speakers[i].id = -1;
|
|
|
|
for (int n = 0; speaker_pos[n].id >= 0; n++) {
|
2013-06-03 23:42:57 +00:00
|
|
|
if (speaker_pos[n].id == ao->channels.speaker[i])
|
2013-06-03 23:34:53 +00:00
|
|
|
speakers[i] = speaker_pos[n];
|
|
|
|
}
|
|
|
|
if (speakers[i].id < 0) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_FATAL(ao, "Unknown channel layout\n");
|
2013-06-03 23:34:53 +00:00
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 14:42:56 +00:00
|
|
|
char *dev_name = p->cfg_device;
|
|
|
|
if (!dev_name || !dev_name[0])
|
|
|
|
dev_name = ao->device;
|
|
|
|
dev = alcOpenDevice(dev_name && dev_name[0] ? dev_name : NULL);
|
2013-06-03 23:34:53 +00:00
|
|
|
if (!dev) {
|
2013-08-22 21:12:35 +00:00
|
|
|
MP_FATAL(ao, "could not open device\n");
|
2013-06-03 23:34:53 +00:00
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
ctx = alcCreateContext(dev, attribs);
|
|
|
|
alcMakeContextCurrent(ctx);
|
|
|
|
alListenerfv(AL_POSITION, position);
|
|
|
|
alListenerfv(AL_ORIENTATION, direction);
|
2013-06-03 23:42:57 +00:00
|
|
|
alGenSources(ao->channels.num, sources);
|
|
|
|
for (i = 0; i < ao->channels.num; i++) {
|
2013-06-03 23:34:53 +00:00
|
|
|
cur_buf[i] = 0;
|
|
|
|
unqueue_buf[i] = 0;
|
|
|
|
alGenBuffers(NUM_BUF, buffers[i]);
|
|
|
|
alSourcefv(sources[i], AL_POSITION, speakers[i].pos);
|
|
|
|
alSource3f(sources[i], AL_VELOCITY, 0, 0, 0);
|
|
|
|
}
|
|
|
|
alcGetIntegerv(dev, ALC_FREQUENCY, 1, &freq);
|
|
|
|
if (alcGetError(dev) == ALC_NO_ERROR && freq)
|
2013-06-03 23:42:57 +00:00
|
|
|
ao->samplerate = freq;
|
2013-11-10 22:39:22 +00:00
|
|
|
ao->format = AF_FORMAT_S16P;
|
2013-06-03 23:42:57 +00:00
|
|
|
return 0;
|
2006-02-16 20:45:25 +00:00
|
|
|
|
|
|
|
err_out:
|
2013-06-03 23:42:57 +00:00
|
|
|
return -1;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
2014-03-08 23:49:39 +00:00
|
|
|
static void uninit(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
|
|
|
ALCcontext *ctx = alcGetCurrentContext();
|
|
|
|
ALCdevice *dev = alcGetContextsDevice(ctx);
|
2013-06-03 23:42:57 +00:00
|
|
|
reset(ao);
|
2013-06-03 23:34:53 +00:00
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
alcDestroyContext(ctx);
|
|
|
|
alcCloseDevice(dev);
|
2013-06-03 23:42:57 +00:00
|
|
|
ao_data = NULL;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 23:49:39 +00:00
|
|
|
static void drain(struct ao *ao)
|
|
|
|
{
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
while (state == AL_PLAYING) {
|
|
|
|
mp_sleep_us(10000);
|
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 23:34:53 +00:00
|
|
|
static void unqueue_buffers(void)
|
|
|
|
{
|
|
|
|
ALint p;
|
|
|
|
int s;
|
2013-06-03 23:42:57 +00:00
|
|
|
for (s = 0; s < ao_data->channels.num; s++) {
|
2013-06-03 23:34:53 +00:00
|
|
|
int till_wrap = NUM_BUF - unqueue_buf[s];
|
|
|
|
alGetSourcei(sources[s], AL_BUFFERS_PROCESSED, &p);
|
|
|
|
if (p >= till_wrap) {
|
|
|
|
alSourceUnqueueBuffers(sources[s], till_wrap,
|
|
|
|
&buffers[s][unqueue_buf[s]]);
|
|
|
|
unqueue_buf[s] = 0;
|
|
|
|
p -= till_wrap;
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
alSourceUnqueueBuffers(sources[s], p, &buffers[s][unqueue_buf[s]]);
|
|
|
|
unqueue_buf[s] += p;
|
|
|
|
}
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief stop playing and empty buffers (for seeking/pause)
|
|
|
|
*/
|
2013-06-03 23:42:57 +00:00
|
|
|
static void reset(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
2013-06-03 23:42:57 +00:00
|
|
|
alSourceStopv(ao->channels.num, sources);
|
2013-06-03 23:34:53 +00:00
|
|
|
unqueue_buffers();
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief stop playing, keep buffers (for pause)
|
|
|
|
*/
|
2013-06-03 23:42:57 +00:00
|
|
|
static void audio_pause(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
2013-06-03 23:42:57 +00:00
|
|
|
alSourcePausev(ao->channels.num, sources);
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief resume playing, after audio_pause()
|
|
|
|
*/
|
2013-06-03 23:42:57 +00:00
|
|
|
static void audio_resume(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
2013-06-03 23:42:57 +00:00
|
|
|
alSourcePlayv(ao->channels.num, sources);
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 23:42:57 +00:00
|
|
|
static int get_space(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
|
|
|
ALint queued;
|
|
|
|
unqueue_buffers();
|
|
|
|
alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
|
|
|
|
queued = NUM_BUF - queued - 3;
|
|
|
|
if (queued < 0)
|
|
|
|
return 0;
|
2013-11-10 22:39:22 +00:00
|
|
|
return queued * CHUNK_SAMPLES;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief write data into buffer and reset underrun flag
|
|
|
|
*/
|
2013-11-10 22:24:21 +00:00
|
|
|
static int play(struct ao *ao, void **data, int samples, int flags)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
|
|
|
ALint state;
|
2013-11-10 22:39:22 +00:00
|
|
|
int num = samples / CHUNK_SAMPLES;
|
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
for (int ch = 0; ch < ao->channels.num; ch++) {
|
|
|
|
int16_t *d = data[ch];
|
|
|
|
d += i * CHUNK_SAMPLES;
|
|
|
|
alBufferData(buffers[ch][cur_buf[ch]], AL_FORMAT_MONO16, d,
|
2013-06-03 23:42:57 +00:00
|
|
|
CHUNK_SIZE, ao->samplerate);
|
2013-06-03 23:34:53 +00:00
|
|
|
alSourceQueueBuffers(sources[ch], 1, &buffers[ch][cur_buf[ch]]);
|
|
|
|
cur_buf[ch] = (cur_buf[ch] + 1) % NUM_BUF;
|
|
|
|
}
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
2013-06-03 23:34:53 +00:00
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
if (state != AL_PLAYING) // checked here in case of an underrun
|
2013-06-03 23:42:57 +00:00
|
|
|
alSourcePlayv(ao->channels.num, sources);
|
2013-11-10 22:39:22 +00:00
|
|
|
return num * CHUNK_SAMPLES;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-11-09 10:45:04 +00:00
|
|
|
static double get_delay(struct ao *ao)
|
2013-06-03 23:34:53 +00:00
|
|
|
{
|
|
|
|
ALint queued;
|
|
|
|
unqueue_buffers();
|
|
|
|
alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
|
2014-11-09 10:45:04 +00:00
|
|
|
return queued * CHUNK_SAMPLES / (double)ao->samplerate;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
2013-06-03 23:42:57 +00:00
|
|
|
|
2013-07-21 20:23:12 +00:00
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
2013-06-03 23:42:57 +00:00
|
|
|
const struct ao_driver audio_out_openal = {
|
2013-10-23 17:07:27 +00:00
|
|
|
.description = "OpenAL audio output",
|
|
|
|
.name = "openal",
|
2013-06-03 23:42:57 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.control = control,
|
|
|
|
.get_space = get_space,
|
|
|
|
.play = play,
|
|
|
|
.get_delay = get_delay,
|
|
|
|
.pause = audio_pause,
|
|
|
|
.resume = audio_resume,
|
|
|
|
.reset = reset,
|
2014-03-08 23:49:39 +00:00
|
|
|
.drain = drain,
|
2014-10-13 14:42:56 +00:00
|
|
|
.list_devs = list_devs,
|
2013-07-21 20:23:12 +00:00
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.options = (const struct m_option[]) {
|
|
|
|
OPT_STRING_VALIDATE("device", cfg_device, 0, validate_device_opt),
|
|
|
|
{0}
|
|
|
|
},
|
2013-06-03 23:42:57 +00:00
|
|
|
};
|