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
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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
|
|
|
|
* along with MPlayer; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
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>
|
|
|
|
#else
|
2006-02-16 20:45:25 +00:00
|
|
|
#include <AL/alc.h>
|
|
|
|
#include <AL/al.h>
|
2006-12-10 14:07:08 +00:00
|
|
|
#endif
|
2006-02-16 20:45:25 +00:00
|
|
|
|
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
|
|
|
|
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
|
|
|
#include "libaf/af_format.h"
|
|
|
|
#include "osdep/timer.h"
|
|
|
|
#include "subopt-helper.h"
|
|
|
|
|
2009-05-13 02:58:57 +00:00
|
|
|
static const ao_info_t info =
|
2006-02-16 20:45:25 +00:00
|
|
|
{
|
|
|
|
"OpenAL audio output",
|
|
|
|
"openal",
|
2007-07-09 19:50:36 +00:00
|
|
|
"Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>",
|
2006-02-16 20:45:25 +00:00
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(openal)
|
|
|
|
|
2009-08-19 16:03:05 +00:00
|
|
|
#define MAX_CHANS 8
|
2006-02-16 20:45:25 +00:00
|
|
|
#define NUM_BUF 128
|
|
|
|
#define CHUNK_SIZE 512
|
|
|
|
static ALuint buffers[MAX_CHANS][NUM_BUF];
|
|
|
|
static ALuint sources[MAX_CHANS];
|
|
|
|
|
|
|
|
static int cur_buf[MAX_CHANS];
|
|
|
|
static int unqueue_buf[MAX_CHANS];
|
|
|
|
static int16_t *tmpbuf;
|
|
|
|
|
|
|
|
|
|
|
|
static int control(int cmd, void *arg) {
|
2006-12-10 22:45:32 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
case AOCONTROL_SET_VOLUME: {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2006-02-16 20:45:25 +00:00
|
|
|
return CONTROL_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief print suboption usage help
|
|
|
|
*/
|
|
|
|
static void print_help(void) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_FATAL,
|
|
|
|
"\n-ao openal commandline help:\n"
|
|
|
|
"Example: mplayer -ao openal\n"
|
|
|
|
"\nOptions:\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init(int rate, int channels, int format, int flags) {
|
2006-12-09 11:45:41 +00:00
|
|
|
float position[3] = {0, 0, 0};
|
2006-12-09 12:14:14 +00:00
|
|
|
float direction[6] = {0, 0, 1, 0, -1, 0};
|
2009-08-19 16:03:05 +00:00
|
|
|
float sppos[MAX_CHANS][3] = {
|
2006-12-09 12:14:14 +00:00
|
|
|
{-1, 0, 0.5}, {1, 0, 0.5},
|
|
|
|
{-1, 0, -1}, {1, 0, -1},
|
|
|
|
{0, 0, 1}, {0, 0, 0.1},
|
2009-08-19 16:03:05 +00:00
|
|
|
{-1, 0, 0}, {1, 0, 0},
|
2006-12-09 11:45:41 +00:00
|
|
|
};
|
2006-02-16 20:45:25 +00:00
|
|
|
ALCdevice *dev = NULL;
|
|
|
|
ALCcontext *ctx = NULL;
|
2006-12-10 01:08:32 +00:00
|
|
|
ALCint freq = 0;
|
|
|
|
ALCint attribs[] = {ALC_FREQUENCY, rate, 0, 0};
|
2006-02-16 20:45:25 +00:00
|
|
|
int i;
|
2009-09-02 11:33:37 +00:00
|
|
|
const opt_t subopts[] = {
|
2006-02-16 20:45:25 +00:00
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
if (subopt_parse(ao_subdevice, subopts) != 0) {
|
|
|
|
print_help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (channels > MAX_CHANS) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_FATAL, "[OpenAL] Invalid number of channels: %i\n", channels);
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
dev = alcOpenDevice(NULL);
|
|
|
|
if (!dev) {
|
|
|
|
mp_msg(MSGT_AO, MSGL_FATAL, "[OpenAL] could not open device\n");
|
|
|
|
goto err_out;
|
|
|
|
}
|
2006-12-10 01:10:53 +00:00
|
|
|
ctx = alcCreateContext(dev, attribs);
|
2006-02-16 20:45:25 +00:00
|
|
|
alcMakeContextCurrent(ctx);
|
2006-12-09 11:45:41 +00:00
|
|
|
alListenerfv(AL_POSITION, position);
|
|
|
|
alListenerfv(AL_ORIENTATION, direction);
|
|
|
|
alGenSources(channels, sources);
|
2006-02-16 20:45:25 +00:00
|
|
|
for (i = 0; i < channels; i++) {
|
|
|
|
cur_buf[i] = 0;
|
|
|
|
unqueue_buf[i] = 0;
|
|
|
|
alGenBuffers(NUM_BUF, buffers[i]);
|
2006-12-09 11:45:41 +00:00
|
|
|
alSourcefv(sources[i], AL_POSITION, sppos[i]);
|
|
|
|
alSource3f(sources[i], AL_VELOCITY, 0, 0, 0);
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
2006-12-09 12:14:14 +00:00
|
|
|
if (channels == 1)
|
|
|
|
alSource3f(sources[0], AL_POSITION, 0, 0, 1);
|
2006-02-16 20:45:25 +00:00
|
|
|
ao_data.channels = channels;
|
2006-12-10 01:08:32 +00:00
|
|
|
alcGetIntegerv(dev, ALC_FREQUENCY, 1, &freq);
|
|
|
|
if (alcGetError(dev) == ALC_NO_ERROR && freq)
|
|
|
|
rate = freq;
|
|
|
|
ao_data.samplerate = rate;
|
2006-02-16 20:45:25 +00:00
|
|
|
ao_data.format = AF_FORMAT_S16_NE;
|
|
|
|
ao_data.bps = channels * rate * 2;
|
|
|
|
ao_data.buffersize = CHUNK_SIZE * NUM_BUF;
|
|
|
|
ao_data.outburst = channels * CHUNK_SIZE;
|
2006-07-03 13:21:54 +00:00
|
|
|
tmpbuf = malloc(CHUNK_SIZE);
|
2006-02-16 20:45:25 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
err_out:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// close audio device
|
|
|
|
static void uninit(int immed) {
|
|
|
|
ALCcontext *ctx = alcGetCurrentContext();
|
|
|
|
ALCdevice *dev = alcGetContextsDevice(ctx);
|
|
|
|
free(tmpbuf);
|
|
|
|
if (!immed) {
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
while (state == AL_PLAYING) {
|
|
|
|
usec_sleep(10000);
|
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reset();
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
alcDestroyContext(ctx);
|
|
|
|
alcCloseDevice(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unqueue_buffers(void) {
|
|
|
|
ALint p;
|
2008-01-30 23:49:36 +00:00
|
|
|
int s;
|
2006-02-16 20:45:25 +00:00
|
|
|
for (s = 0; s < ao_data.channels; s++) {
|
2008-01-30 23:37:30 +00:00
|
|
|
int till_wrap = NUM_BUF - unqueue_buf[s];
|
2006-02-16 20:45:25 +00:00
|
|
|
alGetSourcei(sources[s], AL_BUFFERS_PROCESSED, &p);
|
2008-01-30 23:37:30 +00:00
|
|
|
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)
|
|
|
|
*/
|
|
|
|
static void reset(void) {
|
2008-01-30 22:28:41 +00:00
|
|
|
alSourceStopv(ao_data.channels, sources);
|
2006-02-16 20:45:25 +00:00
|
|
|
unqueue_buffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief stop playing, keep buffers (for pause)
|
|
|
|
*/
|
|
|
|
static void audio_pause(void) {
|
|
|
|
alSourcePausev(ao_data.channels, sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief resume playing, after audio_pause()
|
|
|
|
*/
|
|
|
|
static void audio_resume(void) {
|
|
|
|
alSourcePlayv(ao_data.channels, sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_space(void) {
|
|
|
|
ALint queued;
|
|
|
|
unqueue_buffers();
|
|
|
|
alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
|
2008-01-30 23:48:48 +00:00
|
|
|
queued = NUM_BUF - queued - 3;
|
|
|
|
if (queued < 0) return 0;
|
|
|
|
return queued * CHUNK_SIZE * ao_data.channels;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief write data into buffer and reset underrun flag
|
|
|
|
*/
|
|
|
|
static int play(void *data, int len, int flags) {
|
|
|
|
ALint state;
|
|
|
|
int i, j, k;
|
|
|
|
int ch;
|
|
|
|
int16_t *d = data;
|
2008-01-30 23:39:01 +00:00
|
|
|
len /= ao_data.channels * CHUNK_SIZE;
|
2006-02-16 20:45:25 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
for (ch = 0; ch < ao_data.channels; ch++) {
|
|
|
|
for (j = 0, k = ch; j < CHUNK_SIZE / 2; j++, k += ao_data.channels)
|
|
|
|
tmpbuf[j] = d[k];
|
|
|
|
alBufferData(buffers[ch][cur_buf[ch]], AL_FORMAT_MONO16, tmpbuf,
|
|
|
|
CHUNK_SIZE, ao_data.samplerate);
|
|
|
|
alSourceQueueBuffers(sources[ch], 1, &buffers[ch][cur_buf[ch]]);
|
|
|
|
cur_buf[ch] = (cur_buf[ch] + 1) % NUM_BUF;
|
|
|
|
}
|
|
|
|
d += ao_data.channels * CHUNK_SIZE / 2;
|
|
|
|
}
|
|
|
|
alGetSourcei(sources[0], AL_SOURCE_STATE, &state);
|
|
|
|
if (state != AL_PLAYING) // checked here in case of an underrun
|
|
|
|
alSourcePlayv(ao_data.channels, sources);
|
2008-01-30 23:39:01 +00:00
|
|
|
return len * ao_data.channels * CHUNK_SIZE;
|
2006-02-16 20:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static float get_delay(void) {
|
|
|
|
ALint queued;
|
|
|
|
unqueue_buffers();
|
|
|
|
alGetSourcei(sources[0], AL_BUFFERS_QUEUED, &queued);
|
|
|
|
return queued * CHUNK_SIZE / 2 / (float)ao_data.samplerate;
|
|
|
|
}
|